ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/MapWidget.pm (file contents):
Revision 1.137 by root, Sun Jul 20 03:46:02 2008 UTC vs.
Revision 1.163 by root, Thu Oct 14 00:02:39 2010 UTC

1package DC::MapWidget; 1package DC::MapWidget;
2 2
3use strict; 3use common::sense;
4use utf8;
5 4
6use List::Util qw(min max); 5use List::Util qw(min max);
7 6
8use DC; 7use DC;
9use DC::OpenGL; 8use DC::OpenGL;
10use DC::UI; 9use DC::UI;
11use DC::Macro; 10use DC::Macro;
12 11
13our @ISA = DC::UI::Base::; 12our @ISA = DC::UI::Base::;
14 13
14our @TEX_HIDDEN = map {
15 new_from_resource DC::Texture # MUST be POT
16 "hidden-$_.png", mipmap => 1, wrap => 1
17 } 0, 1, 2;
18
15my $magicmap_tex = 19my $magicmap_tex =
16 new_from_file DC::Texture DC::find_rcfile "magicmap.png", 20 new_from_resource DC::Texture "magicmap.png",
17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA; 21 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
18 22
19sub new { 23sub new {
20 my $class = shift; 24 my $class = shift;
21 25
163sub invoke_button_down { 167sub invoke_button_down {
164 my ($self, $ev, $x, $y) = @_; 168 my ($self, $ev, $x, $y) = @_;
165 169
166 if ($ev->{button} == 1) { 170 if ($ev->{button} == 1) {
167 $self->grab_focus; 171 $self->grab_focus;
168 return unless $::CONN; 172 return unless $::CONN && $self->{ctilesize};
169 173
170 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; 174 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize};
171 my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; 175 my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize};
172 176
173 $x -= DC::floor $::MAP->w * 0.5; 177 $x -= DC::floor $::MAP->w * 0.5;
201 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], 205 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
202 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], 206 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
203 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], 207 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
204 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], 208 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
205# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], 209# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
206 [
207 $::PICKUP_ENABLE->{state}
208 ? "Disable automatic pickup"
209 : "Enable automatic pickup",
210 sub { $::PICKUP_ENABLE->toggle }
211 ],
212 ); 210 );
213 211
214 if ($::CONN && $::CONN->{editor_support}) { 212 if ($::CONN && $::CONN->{editor_support}) {
215# push @items, [ 213# push @items, [
216# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", 214# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>",
217# \&editor_invoke, 215# \&editor_invoke,
218# ]; 216# ];
219 217
220 for my $type (qw(test name)) { 218 for my $type (@{ $::CONN->{editor_support}{servertypes} }) {
221 $::CONN->{editor_support}{type} ne $type 219 $::CONN->{editor_support}{servertype} ne $type
222 or next; 220 or next;
223 my $server = $::CONN->{editor_support}{"${type}server"} 221 my $server = $::CONN->{editor_support}{"${type}server"}
224 or next; 222 or next;
225 223
226 push @items, [ 224 push @items, [
285 $self->{need_update} = 1; 283 $self->{need_update} = 1;
286 $self->SUPER::update; 284 $self->SUPER::update;
287} 285}
288 286
289my %DIR = ( 287my %DIR = (
290 ( "," . DC::SDLK_KP5 ), [0, "stay fire"], 288 ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
291 ( "," . DC::SDLK_KP8 ), [1, "north"], 289 ( "," . DC::SDLK_KP8 ), [1, "north"],
292 ( "," . DC::SDLK_KP9 ), [2, "northeast"], 290 ( "," . DC::SDLK_KP9 ), [2, "northeast"],
293 ( "," . DC::SDLK_KP6 ), [3, "east"], 291 ( "," . DC::SDLK_KP6 ), [3, "east"],
294 ( "," . DC::SDLK_KP3 ), [4, "southeast"], 292 ( "," . DC::SDLK_KP3 ), [4, "southeast"],
295 ( "," . DC::SDLK_KP2 ), [5, "south"], 293 ( "," . DC::SDLK_KP2 ), [5, "south"],
296 ( "," . DC::SDLK_KP1 ), [6, "southwest"], 294 ( "," . DC::SDLK_KP1 ), [6, "southwest"],
297 ( "," . DC::SDLK_KP4 ), [7, "west"], 295 ( "," . DC::SDLK_KP4 ), [7, "west"],
298 ( "," . DC::SDLK_KP7 ), [8, "northwest"], 296 ( "," . DC::SDLK_KP7 ), [8, "northwest"],
299 297
298 ( "," . DC::SDLK_PAGEUP ), [2, "northeast"],
299 ( "," . DC::SDLK_PAGEDOWN ), [4, "southeast"],
300 ( "," . DC::SDLK_END ), [6, "southwest"],
301 ( "," . DC::SDLK_HOME ), [8, "northwest"],
302
300 ( "," . DC::SDLK_UP ), [1, "north"], 303 ( "," . DC::SDLK_UP ), [1, "north"],
301 ("1," . DC::SDLK_UP ), [2, "northeast"], 304 ("1," . DC::SDLK_UP ), [2, "northeast"],
302 ( "," . DC::SDLK_RIGHT), [3, "east"], 305 ( "," . DC::SDLK_RIGHT ), [3, "east"],
303 ("1," . DC::SDLK_RIGHT), [4, "southeast"], 306 ("1," . DC::SDLK_RIGHT ), [4, "southeast"],
304 ( "," . DC::SDLK_DOWN ), [5, "south"], 307 ( "," . DC::SDLK_DOWN ), [5, "south"],
305 ("1," . DC::SDLK_DOWN ), [6, "southwest"], 308 ("1," . DC::SDLK_DOWN ), [6, "southwest"],
306 ( "," . DC::SDLK_LEFT ), [7, "west"], 309 ( "," . DC::SDLK_LEFT ), [7, "west"],
307 ("1," . DC::SDLK_LEFT ), [8, "northwest"], 310 ("1," . DC::SDLK_LEFT ), [8, "northwest"],
308); 311);
309 312
310sub invoke_key_down { 313sub invoke_key_down {
311 my ($self, $ev) = @_; 314 my ($self, $ev) = @_;
312 315
313 my $mod = $ev->{mod}; 316 my $mod = $ev->{mod};
314 my $sym = $ev->{sym}; 317 my $sym = $ev->{sym};
315 my $uni = $ev->{unicode}; 318 my $uni = $ev->{unicode};
316 319
317 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_SHIFT; 320 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT;
318 321
319 # ignore repeated keypresses 322 # ignore repeated keypresses
320 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym; 323 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym;
321 $self->{last_mod} = $mod; 324 $self->{last_mod} = $mod;
322 $self->{last_sym} = $sym; 325 $self->{last_sym} = $sym;
323 326
324 if ($::CONN && (my $dir = $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"})) { 327 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
328
329 if ($::CONN && $dir) {
325 if ($mod & DC::KMOD_SHIFT) { 330 if ($mod & DC::KMOD_SHIFT) {
326 $self->{shft}++; 331 $self->{shft}++;
327 if ($dir->[0] != $self->{fire_dir}) { 332 if ($dir->[0] != $self->{fire_dir}) {
328 $::CONN->user_send ("fire $dir->[0]"); 333 $::CONN->user_send ("fire $dir->[0]");
329 } 334 }
356 $::CONN->user_send ("fire_stop"); 361 $::CONN->user_send ("fire_stop");
357 delete $self->{fire_dir}; 362 delete $self->{fire_dir};
358 $res = 1; 363 $res = 1;
359 } 364 }
360 } else { 365 } else {
361 if (exists $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"} && delete $self->{shft}) { 366 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
367
368 if ($dir && delete $self->{shft}) {
362 $::CONN->user_send ("fire_stop"); 369 $::CONN->user_send ("fire_stop");
363 delete $self->{fire_dir}; 370 delete $self->{fire_dir};
364 $res = 1; 371 $res = 1;
365 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT) 372 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT)
366 && delete $self->{shft}) { # XXX: is RSHIFT ok? 373 && delete $self->{shft}) { # XXX: is RSHIFT ok?
368 delete $self->{fire_dir}; 375 delete $self->{fire_dir};
369 $res = 1; 376 $res = 1;
370 } 377 }
371 } 378 }
372 379
373 if (!($mod & DC::KMOD_CTRL ) && delete $self->{ctrl}) { 380 if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) {
374 $::CONN->user_send ("run_stop"); 381 $::CONN->user_send ("run_stop");
375 $res = 1; 382 $res = 1;
376 } 383 }
377 384
378 $res 385 $res
393} 400}
394 401
395sub scroll { 402sub scroll {
396 my ($self, $dx, $dy) = @_; 403 my ($self, $dx, $dy) = @_;
397 404
398 $::MAP->scroll ($dx, $dy);
399
400 $self->movement_update; 405 $self->movement_update;
401 406
402 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement 407 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
403 $self->{sdy} += $dy * $self->{tilesize}; 408 $self->{sdy} += $dy * $self->{tilesize};
409
410 # save old fow texture, if applicable
411 $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture};
412 $self->{lfdx} = $dx;
413 $self->{lfdy} = $dy;
414 $self->{lmdx} = $self->{dx};
415 $self->{lmdy} = $self->{dy};
416
417 $::MAP->scroll ($dx, $dy);
404} 418}
405 419
406sub set_magicmap { 420sub set_magicmap {
407 my ($self, $w, $h, $x, $y, $data) = @_; 421 my ($self, $w, $h, $x, $y, $data) = @_;
408 422
421 if ($self->{sdx} || $self->{sdy}) { 435 if ($self->{sdx} || $self->{sdy}) {
422 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); 436 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH);
423 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; 437 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
424 438
425 # the minimum time for a single tile movement 439 # the minimum time for a single tile movement
426 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK); 440 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
441
442 $spd *= $self->{tilesize};
427 443
428 # jump if "impossibly high" speed 444 # jump if "impossibly high" speed
429 if ( 445 if (
430 (max abs $self->{sdx}, abs $self->{sdy}) 446 (max abs $self->{sdx}, abs $self->{sdy})
431 > $spd * $self->{tilesize} * $mintime * 1.1 447 > $spd * $mintime * 2.1
432 ) { 448 ) {
433 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 1.0;#d# 449 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d#
434 $self->{sdx} = $self->{sdy} = 0; 450 $self->{sdx} = $self->{sdy} = 0;
435 } else { 451 } else {
436 $spd *= $self->{tilesize} * $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction 452 $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction
437 453
438 my $dx = $self->{sdx} < 0 ? -$spd : $spd; 454 my $dx = $self->{sdx} < 0 ? -$spd : $spd;
439 my $dy = $self->{sdy} < 0 ? -$spd : $spd; 455 my $dy = $self->{sdy} < 0 ? -$spd : $spd;
440 456
441 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx } 457 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx }
442 if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy } 458 if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy }
443
444 $self->update;
445 } 459 }
460
461 $self->update;
446 } 462 }
447 } else { 463 } else {
448 $self->{sdx} = $self->{sdy} = 0; 464 $self->{sdx} = $self->{sdy} = 0;
449 } 465 }
450 466
462 478
463 my $sdx_t = DC::ceil $self->{sdx} / $tilesize; 479 my $sdx_t = DC::ceil $self->{sdx} / $tilesize;
464 my $sdy_t = DC::ceil $self->{sdy} / $tilesize; 480 my $sdy_t = DC::ceil $self->{sdy} / $tilesize;
465 481
466 # width/height of map, in tiles 482 # width/height of map, in tiles
467 my $sw = $self->{sw} = 1 + DC::ceil $self->{w} / $tilesize; 483 my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize;
468 my $sh = $self->{sh} = 1 + DC::ceil $self->{h} / $tilesize; 484 my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize;
469 485
470 # the map displacement, in tiles 486 # the map displacement, in tiles
471 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t; 487 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t;
472 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t; 488 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t;
473 489
477 493
478 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; 494 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
479 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; 495 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
480 496
481 if ($::CFG->{fow_enable}) { 497 if ($::CFG->{fow_enable}) {
498 # draw_fow_texture REQUIRES the fow texture to stay the same size.
482 my ($w, $h, $data) = $::MAP->fow_texture ( 499 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
483 $dx + (min 0, $sdx_t),
484 $dy + (min 0, $sdy_t),
485 $sw + abs $sdx_t,
486 $sh + abs $sdy_t
487 );
488 500
489 $self->{fow_texture} = new DC::Texture 501 $self->{fow_texture} = new DC::Texture
490 w => $w, 502 w => $w,
491 h => $h, 503 h => $h,
492 data => $data, 504 data => $data,
499 glNewList ($self->{list} ||= glGenList); 511 glNewList ($self->{list} ||= glGenList);
500 512
501 glPushMatrix; 513 glPushMatrix;
502 glTranslate $sx0, $sy0; 514 glTranslate $sx0, $sy0;
503 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 515 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
504 glTranslate $self->{sdx}, $self->{sdy}; 516 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
505 517
506 $::MAP->draw ($dx, $dy, $sw, $sh, 518 $::MAP->draw ($dx, $dy, $sw, $sh,
507 $self->{tilesize}, 519 ($self->{tilesize}) x 2,
508 $::CONN->{player}{tag}, 520 $::CONN->{player}{tag},
509 -$self->{sdx}, -$self->{sdy}); 521 -$self->{sdx}, -$self->{sdy});
510 522
511 glTranslate -$self->{sdx}, -$self->{sdy};
512 glScale $self->{tilesize}, $self->{tilesize}; 523 glScale $self->{tilesize}, $self->{tilesize};
513 524
514 if (my $tex = $self->{fow_texture}) { 525 if (my $tex = $self->{fow_texture}) {
515 glPushMatrix; 526 my @prev_fow_params;
516 glTranslate +(min 0, $sdx_t), (min 0, $sdy_t);
517 glScale 1/3, 1/3;
518 glEnable GL_TEXTURE_2D;
519 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
520 527
528 if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) {
529 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
530 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
531
532 if ($d1 * $d2) {
533 @prev_fow_params = (
534 (min 1, $d1 / $d2),
535 $self->{lmdx} - $dx - $self->{lfdx},
536 $self->{lmdy} - $dy - $self->{lfdy},
537 @{$self->{prev_fow_texture}}{qw(name data)}
538 );
539 }
540 }
541
542 DC::Texture::draw_fow_texture
521 glColor +($::CFG->{fow_intensity}) x 3, 0.9; 543 $::CFG->{fow_intensity},
522 $self->{fow_texture}->draw_quad_alpha (0, 0); 544 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
523 545 @{$self->{fow_texture}}{qw(name data s t w h)},
524 glDisable GL_TEXTURE_2D; 546 @prev_fow_params;
525 glPopMatrix;
526 } 547 }
527 548
528 if ($self->{magicmap}) { 549 if ($self->{magicmap}) {
529 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 550 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
530 551
531 $x += $::MAP->ox + $self->{dx}; 552 $x += $::MAP->ox + $self->{dx};
532 $y += $::MAP->oy + $self->{dy}; 553 $y += $::MAP->oy + $self->{dy};
533 554
534 glTranslate - $x - 1, - $y - 1; 555 glTranslate - $x - 1, - $y - 1;
535 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 556 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
536 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 557 $::MAP->draw_magicmap ($w, $h, $data);
537 } 558 }
538 559
539 glPopMatrix; 560 glPopMatrix;
540 glEndList; 561 glEndList;
541 } 562 }
542 } else { 563 } else {
564 delete $self->{last_fow_texture};
565 delete $self->{fow_texture};
566
543 glDeleteList delete $self->{list} 567 glDeleteList delete $self->{list}
544 if $self->{list}; 568 if $self->{list};
545 } 569 }
546} 570}
547 571
583 $self->SUPER::DESTROY; 607 $self->SUPER::DESTROY;
584} 608}
585 609
586package DC::MapWidget::MapMap; 610package DC::MapWidget::MapMap;
587 611
588use strict; 612use common::sense;
589use utf8;
590 613
591our @ISA = DC::UI::Base::; 614our @ISA = DC::UI::Base::;
592 615
593use Time::HiRes qw(time);
594use DC::OpenGL; 616use DC::OpenGL;
595 617
596sub size_request { 618sub size_request {
597 ($::HEIGHT * 0.2, $::HEIGHT * 0.2) 619 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
598} 620}
599 621
600sub refresh_hook { 622sub refresh_hook {
601 my ($self) = @_; 623 my ($self) = @_;
602 624
603 if ($::MAP && $self->{texture_atime} < time) { 625 if ($::MAP && $self->{texture_atime} < EV::now) {
604 my ($w, $h) = @$self{qw(w h)}; 626 my ($w, $h) = @$self{qw(w h)};
627
628 return unless $w && $h;
605 629
606 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 630 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
607 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 631 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
608 632
609 my $ox = 0.5 * ($w - $sw); 633 my $ox = 0.5 * ($w - $sw);
621 $self->{sh} = $sh; 645 $self->{sh} = $sh;
622 646
623 $self->{x0} = $x0; 647 $self->{x0} = $x0;
624 $self->{y0} = $y0; 648 $self->{y0} = $y0;
625 649
626 $self->{texture_atime} = time + 1/3; 650 $self->{texture_atime} = EV::now + 1/2;
627 651
628 $self->{texture} = 652 $self->{texture} =
629 new DC::Texture 653 new DC::Texture
630 w => $w, 654 w => $w,
631 h => $h, 655 h => $h,
686 glDisable GL_BLEND; 710 glDisable GL_BLEND;
687} 711}
688 712
689package DC::MapWidget::Command; 713package DC::MapWidget::Command;
690 714
691use strict; 715use common::sense;
692 716
693use DC::OpenGL; 717use DC::OpenGL;
694 718
695our @ISA = DC::UI::Frame::; 719our @ISA = DC::UI::Frame::;
696 720
838 my $text = $self->{entry}->get_text; 862 my $text = $self->{entry}->get_text;
839 863
840 length $text 864 length $text
841 or return $self->hide; 865 or return $self->hide;
842 866
843 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
844
845 if ($text ne $self->{last_search}) { 867 if ($text ne $self->{last_search}) {
846 my @match; 868 my @match;
847 869
848 if ($text =~ /^(.*?)\s+$/) { 870 if ($text =~ /^(.*?)\s+$/) {
871 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
849 @match = [$cmd, "(appended whitespace suppresses completion)"]; 872 @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]);
850 } else { 873 } else {
874 # @match is [command, penalty, command with arguments] until sort
875
876 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
877
851 my $regexp = do { 878 my $regexp_abbrev = do {
852 my ($beg, @chr) = split //, lc $cmd; 879 my ($beg, @chr) = split //, lc $cmd;
853 880
854 # the following regex is used to match our "completion entry" 881 # the following regex is used to match our "completion entry"
855 # to an actual command - the parentheses match kind of "overhead" 882 # to an actual command - the parentheses match kind of "overhead"
856 # - the more characters the parentheses match, the less attractive 883 # - the more characters the parentheses match, the less attractive
858 my $regexp = "^\Q$beg\E" 885 my $regexp = "^\Q$beg\E"
859 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 886 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
860 qr<$regexp> 887 qr<$regexp>
861 }; 888 };
862 889
863 my @penalty; 890 my $regexp_partial = do {
891 my $regexp = "^\Q$text\E(.*)";
892 qr<$regexp>
893 };
864 894
865 for (keys %{$self->{command}}) { 895 for (keys %{$self->{command}}) {
866 if (@penalty = $_ =~ $regexp) { 896 my @scores;
867 push @match, [$_, length join "", map "::$_", grep defined, @penalty]; 897
898 # 1. Complete command [with args]
899 # command is a prefix of the text
900 # score is length of complete command matched
901 # e.g. "invoke summon pet monster bat"
902 # "invoke" "summon pet monster bat" = 6
903 # "invoke summon pet monster" "bat" = 25
904 if ($text =~ /^\Q$_\E(.*)/) {
905 push @scores, [$_, length $_, $text];
868 } 906 }
907
908 # 2. Partial command
909 # text is a prefix of the full command
910 # score is the length of the input text
911 # e.g. "invoke s"
912 # "invoke small fireball" = 8
913 # "invoke summon pet monster" = 8
914
915 if ($_ =~ $regexp_partial) {
916 push @scores, [$_, length $text, $_];
917 }
918
919 # 3. Abbreviation match
920 # attempts to use first word of text as an abbreviated command
921 # score is length of word + 1 - 3 per non-word-initial character
922
923 if (my @penalty = $_ =~ $regexp_abbrev) {
924 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
925 }
926
927 # Pick the best option for this command
928 push @match, (sort {
929 $b->[1] <=> $a->[1]
930 } @scores)[0];
869 } 931 }
870 932
933 # @match is now [command object, command with arguments]
871 @match = map $self->{command}{$_->[0]}, 934 @match = map [$self->{command}{$_->[0]}, $_->[2]],
872 sort { 935 sort {
873 $a->[1] <=> $b->[1] 936 $b->[1] <=> $a->[1]
874 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] 937 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
875 or (length $b->[0]) <=> (length $a->[0]) 938 or (length $b->[0]) <=> (length $a->[0])
876 } @match; 939 } @match;
877 } 940 }
878 941
897 $label->{fg} = [1, 1, 1, 1]; 960 $label->{fg} = [1, 1, 1, 1];
898 $label->{bg} = [0, 0, 0, 0]; 961 $label->{bg} = [0, 0, 0, 0];
899 } 962 }
900 963
901 if (@matches) { 964 if (@matches) {
902 $self->{select} = "$matches[0][0]$arg"; 965 $self->{select} = "$matches[0][1]";
903 966
904 $labels[0]->{fg} = [0, 0, 0, 1]; 967 $labels[0]->{fg} = [0, 0, 0, 1];
905 $labels[0]->{bg} = [1, 1, 1, 0.8]; 968 $labels[0]->{bg} = [1, 1, 1, 0.8];
906 } else { 969 } else {
907 $self->{select} = "$cmd$arg"; 970 $self->{select} = "$text";
908 } 971 }
909 972
910 for my $match (@matches) { 973 for my $match (@matches) {
911 my $label = shift @labels; 974 my $label = shift @labels;
912 975
913 if (@labels) { 976 if (@labels) {
914 $label->set_text ("$match->[0]$arg"); 977 $label->set_text ("$match->[1]");
915 $label->set_tooltip ($match->[1]); 978 $label->set_tooltip ("$match->[0][1]");
916 } else { 979 } else {
917 $label->set_text ("..."); 980 $label->set_text ("...");
918 $label->set_tooltip ("Use Cursor-Down to view more matches"); 981 $label->set_tooltip ("Use Cursor-Down to view more matches");
919 last; 982 last;
920 } 983 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines