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.132 by root, Sun Jul 13 12:17:00 2008 UTC vs.
Revision 1.158 by root, Mon Dec 21 23:52:34 2009 UTC

11use DC::Macro; 11use DC::Macro;
12 12
13our @ISA = DC::UI::Base::; 13our @ISA = DC::UI::Base::;
14 14
15my $magicmap_tex = 15my $magicmap_tex =
16 new_from_file DC::Texture DC::find_rcfile "magicmap.png", 16 new_from_resource DC::Texture "magicmap.png",
17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA; 17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
18 18
19sub new { 19sub new {
20 my $class = shift; 20 my $class = shift;
21 21
25 tilesize => 32, 25 tilesize => 32,
26 @_ 26 @_
27 ); 27 );
28 28
29 $self 29 $self
30}
31
32sub set_tilesize {
33 my ($self, $tilesize) = @_;
34
35 $self->{tilesize} = $tilesize;
36} 30}
37 31
38sub add_command { 32sub add_command {
39 my ($self, $command, $tooltip, $widget, $cb) = @_; 33 my ($self, $command, $tooltip, $widget, $cb) = @_;
40 34
169sub invoke_button_down { 163sub invoke_button_down {
170 my ($self, $ev, $x, $y) = @_; 164 my ($self, $ev, $x, $y) = @_;
171 165
172 if ($ev->{button} == 1) { 166 if ($ev->{button} == 1) {
173 $self->grab_focus; 167 $self->grab_focus;
174 return unless $::CONN; 168 return unless $::CONN && $self->{ctilesize};
175 169
176 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; 170 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize};
177 my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; 171 my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize};
178 172
179 $x -= DC::floor $::MAP->w * 0.5; 173 $x -= DC::floor $::MAP->w * 0.5;
207 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], 201 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
208 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], 202 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
209 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], 203 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
210 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], 204 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
211# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], 205# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
212 [
213 $::PICKUP_ENABLE->{state}
214 ? "Disable automatic pickup"
215 : "Enable automatic pickup",
216 sub { $::PICKUP_ENABLE->toggle }
217 ],
218 ); 206 );
219 207
220 if ($::CONN && $::CONN->{editor_support}) { 208 if ($::CONN && $::CONN->{editor_support}) {
221# push @items, [ 209# push @items, [
222# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", 210# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>",
291 $self->{need_update} = 1; 279 $self->{need_update} = 1;
292 $self->SUPER::update; 280 $self->SUPER::update;
293} 281}
294 282
295my %DIR = ( 283my %DIR = (
296 ( "," . DC::SDLK_KP5 ), [0, "stay fire"], 284 ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
297 ( "," . DC::SDLK_KP8 ), [1, "north"], 285 ( "," . DC::SDLK_KP8 ), [1, "north"],
298 ( "," . DC::SDLK_KP9 ), [2, "northeast"], 286 ( "," . DC::SDLK_KP9 ), [2, "northeast"],
299 ( "," . DC::SDLK_KP6 ), [3, "east"], 287 ( "," . DC::SDLK_KP6 ), [3, "east"],
300 ( "," . DC::SDLK_KP3 ), [4, "southeast"], 288 ( "," . DC::SDLK_KP3 ), [4, "southeast"],
301 ( "," . DC::SDLK_KP2 ), [5, "south"], 289 ( "," . DC::SDLK_KP2 ), [5, "south"],
302 ( "," . DC::SDLK_KP1 ), [6, "southwest"], 290 ( "," . DC::SDLK_KP1 ), [6, "southwest"],
303 ( "," . DC::SDLK_KP4 ), [7, "west"], 291 ( "," . DC::SDLK_KP4 ), [7, "west"],
304 ( "," . DC::SDLK_KP7 ), [8, "northwest"], 292 ( "," . DC::SDLK_KP7 ), [8, "northwest"],
305 293
294 ( "," . DC::SDLK_PAGEUP ), [2, "northeast"],
295 ( "," . DC::SDLK_PAGEDOWN ), [4, "southeast"],
296 ( "," . DC::SDLK_END ), [6, "southwest"],
297 ( "," . DC::SDLK_HOME ), [8, "northwest"],
298
306 ( "," . DC::SDLK_UP ), [1, "north"], 299 ( "," . DC::SDLK_UP ), [1, "north"],
307 ("1," . DC::SDLK_UP ), [2, "northeast"], 300 ("1," . DC::SDLK_UP ), [2, "northeast"],
308 ( "," . DC::SDLK_RIGHT), [3, "east"], 301 ( "," . DC::SDLK_RIGHT ), [3, "east"],
309 ("1," . DC::SDLK_RIGHT), [4, "southeast"], 302 ("1," . DC::SDLK_RIGHT ), [4, "southeast"],
310 ( "," . DC::SDLK_DOWN ), [5, "south"], 303 ( "," . DC::SDLK_DOWN ), [5, "south"],
311 ("1," . DC::SDLK_DOWN ), [6, "southwest"], 304 ("1," . DC::SDLK_DOWN ), [6, "southwest"],
312 ( "," . DC::SDLK_LEFT ), [7, "west"], 305 ( "," . DC::SDLK_LEFT ), [7, "west"],
313 ("1," . DC::SDLK_LEFT ), [8, "northwest"], 306 ("1," . DC::SDLK_LEFT ), [8, "northwest"],
314); 307);
315 308
316sub invoke_key_down { 309sub invoke_key_down {
317 my ($self, $ev) = @_; 310 my ($self, $ev) = @_;
318 311
319 my $mod = $ev->{mod}; 312 my $mod = $ev->{mod};
320 my $sym = $ev->{sym}; 313 my $sym = $ev->{sym};
321 my $uni = $ev->{unicode}; 314 my $uni = $ev->{unicode};
322 315
323 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_SHIFT; 316 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT;
324 317
325 # ignore repeated keypresses 318 # ignore repeated keypresses
326 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym; 319 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym;
327 $self->{last_mod} = $mod; 320 $self->{last_mod} = $mod;
328 $self->{last_sym} = $sym; 321 $self->{last_sym} = $sym;
329 322
330 if ($::CONN && (my $dir = $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"})) { 323 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
324
325 if ($::CONN && $dir) {
331 if ($mod & DC::KMOD_SHIFT) { 326 if ($mod & DC::KMOD_SHIFT) {
332 $self->{shft}++; 327 $self->{shft}++;
333 if ($dir->[0] != $self->{fire_dir}) { 328 if ($dir->[0] != $self->{fire_dir}) {
334 $::CONN->user_send ("fire $dir->[0]"); 329 $::CONN->user_send ("fire $dir->[0]");
335 } 330 }
362 $::CONN->user_send ("fire_stop"); 357 $::CONN->user_send ("fire_stop");
363 delete $self->{fire_dir}; 358 delete $self->{fire_dir};
364 $res = 1; 359 $res = 1;
365 } 360 }
366 } else { 361 } else {
367 if (exists $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"} && delete $self->{shft}) { 362 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
363
364 if ($dir && delete $self->{shft}) {
368 $::CONN->user_send ("fire_stop"); 365 $::CONN->user_send ("fire_stop");
369 delete $self->{fire_dir}; 366 delete $self->{fire_dir};
370 $res = 1; 367 $res = 1;
371 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT) 368 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT)
372 && delete $self->{shft}) { # XXX: is RSHIFT ok? 369 && delete $self->{shft}) { # XXX: is RSHIFT ok?
374 delete $self->{fire_dir}; 371 delete $self->{fire_dir};
375 $res = 1; 372 $res = 1;
376 } 373 }
377 } 374 }
378 375
379 if (!($mod & DC::KMOD_CTRL ) && delete $self->{ctrl}) { 376 if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) {
380 $::CONN->user_send ("run_stop"); 377 $::CONN->user_send ("run_stop");
381 $res = 1; 378 $res = 1;
382 } 379 }
383 380
384 $res 381 $res
390 $self->refresh_hook; 387 $self->refresh_hook;
391 388
392 0 389 0
393} 390}
394 391
392sub set_tilesize {
393 my ($self, $tilesize) = @_;
394
395 $self->{tilesize} = $tilesize;
396}
397
398sub scroll {
399 my ($self, $dx, $dy) = @_;
400
401 $self->movement_update;
402
403 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
404 $self->{sdy} += $dy * $self->{tilesize};
405
406 # save old fow texture, if applicable
407 $self->{last_fow_texture} = $self->{fow_texture}
408 if $::CFG->{smooth_transitions};
409 $self->{lfdx} = $dx;
410 $self->{lfdy} = $dy;
411 $self->{lmdx} = $self->{dx};
412 $self->{lmdy} = $self->{dy};
413
414 $::MAP->scroll ($dx, $dy);
415}
416
395sub set_magicmap { 417sub set_magicmap {
396 my ($self, $w, $h, $x, $y, $data) = @_; 418 my ($self, $w, $h, $x, $y, $data) = @_;
397 419
398 $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w; 420 $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w;
399 $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h; 421 $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h;
401 $self->{magicmap} = [$x, $y, $w, $h, $data]; 423 $self->{magicmap} = [$x, $y, $w, $h, $data];
402 424
403 $self->update; 425 $self->update;
404} 426}
405 427
428sub movement_update {
429 my ($self) = @_;
430
431 if ($::CFG->{smooth_movement}) {
432 if ($self->{sdx} || $self->{sdy}) {
433 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH);
434 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
435
436 # the minimum time for a single tile movement
437 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
438
439 $spd *= $self->{tilesize};
440
441 # jump if "impossibly high" speed
442 if (
443 (max abs $self->{sdx}, abs $self->{sdy})
444 > $spd * $mintime * 2.1
445 ) {
446 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d#
447 $self->{sdx} = $self->{sdy} = 0;
448 } else {
449 $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction
450
451 my $dx = $self->{sdx} < 0 ? -$spd : $spd;
452 my $dy = $self->{sdy} < 0 ? -$spd : $spd;
453
454 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx }
455 if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy }
456 }
457
458 $self->update;
459 }
460 } else {
461 $self->{sdx} = $self->{sdy} = 0;
462 }
463
464 $self->{last_update} = EV::time;
465}
466
406sub refresh_hook { 467sub refresh_hook {
407 my ($self) = @_; 468 my ($self) = @_;
408 469
409 if ($::MAP && $::CONN) { 470 if ($::MAP && $::CONN) {
410 if (delete $self->{need_update}) { 471 if (delete $self->{need_update}) {
472 $self->movement_update;
473
411 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1; 474 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
412 475
476 my $sdx_t = DC::ceil $self->{sdx} / $tilesize;
477 my $sdy_t = DC::ceil $self->{sdy} / $tilesize;
478
479 # width/height of map, in tiles
413 my $sw = $self->{sw} = 1 + DC::ceil $self->{w} / $tilesize; 480 my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize;
414 my $sh = $self->{sh} = 1 + DC::ceil $self->{h} / $tilesize; 481 my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize;
415 482
483 # the map displacement, in tiles
416 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize; 484 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t;
417 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize; 485 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t;
418 486
487 # the upper left "visible" corner, in pixels
419 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize; 488 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
420 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize; 489 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
421 490
422 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; 491 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
423 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; 492 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
424 493
425 if ($::CFG->{fow_enable}) { 494 if ($::CFG->{fow_enable}) {
495 # draw_fow_texture REQUIRES the fow texture to stay the same size.
426 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); 496 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
427 497
428 $self->{fow_texture} = new DC::Texture 498 $self->{fow_texture} = new DC::Texture
429 w => $w, 499 w => $w,
430 h => $h, 500 h => $h,
438 glNewList ($self->{list} ||= glGenList); 508 glNewList ($self->{list} ||= glGenList);
439 509
440 glPushMatrix; 510 glPushMatrix;
441 glTranslate $sx0, $sy0; 511 glTranslate $sx0, $sy0;
442 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 512 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
513 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
443 514
444 $::MAP->draw ($dx, $dy, $sw, $sh, 515 $::MAP->draw ($dx, $dy, $sw, $sh,
445 $self->{tilesize}, 516 $self->{tilesize},
446 $::CONN->{player}{tag}, 0, 0); 517 $::CONN->{player}{tag},
518 -$self->{sdx}, -$self->{sdy});
447 519
448 glScale $self->{tilesize}, $self->{tilesize}; 520 glScale $self->{tilesize}, $self->{tilesize};
449 521
450 if (my $tex = $self->{fow_texture}) { 522 if (my $tex = $self->{fow_texture}) {
451 glPushMatrix; 523 if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d#
452 glScale 1/3, 1/3; 524 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
453 glEnable GL_TEXTURE_2D; 525 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
454 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
455 526
456 glColor +($::CFG->{fow_intensity}) x 3, 0.9; 527 if ($d1 * $d2) {
457 $self->{fow_texture}->draw_quad_alpha (0, 0); 528 DC::Texture::draw_fow_texture
529 $::CFG->{fow_intensity},
530 $self->{hidden_tex}{name},
531 @{$self->{fow_texture}}{qw(name data s t w h)},
532 (min 1, $d1 / $d2),
533 $self->{lmdx} - $dx - $self->{lfdx},
534 $self->{lmdy} - $dy - $self->{lfdy},
535 @{$self->{last_fow_texture}}{qw(name data)};
536 } else {
537 delete $self->{last_fow_texture};
538 }
539 }
458 540
459 glDisable GL_TEXTURE_2D; 541 unless ($self->{last_fow_texture}) {
460 glPopMatrix; 542 DC::Texture::draw_fow_texture
543 $::CFG->{fow_intensity},
544 $self->{hidden_tex}{name},
545 @{$self->{fow_texture}}{qw(name data s t w h)};
546 }
461 } 547 }
462 548
463 if ($self->{magicmap}) { 549 if ($self->{magicmap}) {
464 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 550 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
465 551
466 $x += $::MAP->ox + $self->{dx}; 552 $x += $::MAP->ox + $self->{dx};
467 $y += $::MAP->oy + $self->{dy}; 553 $y += $::MAP->oy + $self->{dy};
468 554
469 glTranslate - $x - 1, - $y - 1; 555 glTranslate - $x - 1, - $y - 1;
470 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 556 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
471 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 557 $::MAP->draw_magicmap ($w, $h, $data);
472 } 558 }
473 559
474 glPopMatrix; 560 glPopMatrix;
475 glEndList; 561 glEndList;
476 } 562 }
477 } else { 563 } else {
564 delete $self->{last_fow_texture};
565 delete $self->{fow_texture};
566
478 glDeleteList delete $self->{list} 567 glDeleteList delete $self->{list}
479 if $self->{list}; 568 if $self->{list};
480 } 569 }
481} 570}
482 571
535sub refresh_hook { 624sub refresh_hook {
536 my ($self) = @_; 625 my ($self) = @_;
537 626
538 if ($::MAP && $self->{texture_atime} < time) { 627 if ($::MAP && $self->{texture_atime} < time) {
539 my ($w, $h) = @$self{qw(w h)}; 628 my ($w, $h) = @$self{qw(w h)};
629
630 return unless $w && $h;
540 631
541 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 632 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
542 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 633 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
543 634
544 my $ox = 0.5 * ($w - $sw); 635 my $ox = 0.5 * ($w - $sw);
773 my $text = $self->{entry}->get_text; 864 my $text = $self->{entry}->get_text;
774 865
775 length $text 866 length $text
776 or return $self->hide; 867 or return $self->hide;
777 868
778 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
779
780 if ($text ne $self->{last_search}) { 869 if ($text ne $self->{last_search}) {
781 my @match; 870 my @match;
782 871
783 if ($text =~ /^(.*?)\s+$/) { 872 if ($text =~ /^(.*?)\s+$/) {
873 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
784 @match = [$cmd, "(appended whitespace suppresses completion)"]; 874 @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]);
785 } else { 875 } else {
876 # @match is [command, penalty, command with arguments] until sort
877
878 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
879
786 my $regexp = do { 880 my $regexp_abbrev = do {
787 my ($beg, @chr) = split //, lc $cmd; 881 my ($beg, @chr) = split //, lc $cmd;
788 882
789 # the following regex is used to match our "completion entry" 883 # the following regex is used to match our "completion entry"
790 # to an actual command - the parentheses match kind of "overhead" 884 # to an actual command - the parentheses match kind of "overhead"
791 # - the more characters the parentheses match, the less attractive 885 # - the more characters the parentheses match, the less attractive
793 my $regexp = "^\Q$beg\E" 887 my $regexp = "^\Q$beg\E"
794 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 888 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
795 qr<$regexp> 889 qr<$regexp>
796 }; 890 };
797 891
798 my @penalty; 892 my $regexp_partial = do {
893 my $regexp = "^\Q$text\E(.*)";
894 qr<$regexp>
895 };
799 896
800 for (keys %{$self->{command}}) { 897 for (keys %{$self->{command}}) {
801 if (@penalty = $_ =~ $regexp) { 898 my @scores;
802 push @match, [$_, length join "", map "::$_", grep defined, @penalty]; 899
900 # 1. Complete command [with args]
901 # command is a prefix of the text
902 # score is length of complete command matched
903 # e.g. "invoke summon pet monster bat"
904 # "invoke" "summon pet monster bat" = 6
905 # "invoke summon pet monster" "bat" = 25
906 if ($text =~ /^\Q$_\E(.*)/) {
907 push @scores, [$_, length $_, $text];
803 } 908 }
909
910 # 2. Partial command
911 # text is a prefix of the full command
912 # score is the length of the input text
913 # e.g. "invoke s"
914 # "invoke small fireball" = 8
915 # "invoke summon pet monster" = 8
916
917 if ($_ =~ $regexp_partial) {
918 push @scores, [$_, length $text, $_];
919 }
920
921 # 3. Abbreviation match
922 # attempts to use first word of text as an abbreviated command
923 # score is length of word + 1 - 3 per non-word-initial character
924
925 if (my @penalty = $_ =~ $regexp_abbrev) {
926 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
927 }
928
929 # Pick the best option for this command
930 push @match, (sort {
931 $b->[1] <=> $a->[1]
932 } @scores)[0];
804 } 933 }
805 934
935 # @match is now [command object, command with arguments]
806 @match = map $self->{command}{$_->[0]}, 936 @match = map [$self->{command}{$_->[0]}, $_->[2]],
807 sort { 937 sort {
808 $a->[1] <=> $b->[1] 938 $b->[1] <=> $a->[1]
809 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] 939 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
810 or (length $b->[0]) <=> (length $a->[0]) 940 or (length $b->[0]) <=> (length $a->[0])
811 } @match; 941 } @match;
812 } 942 }
813 943
832 $label->{fg} = [1, 1, 1, 1]; 962 $label->{fg} = [1, 1, 1, 1];
833 $label->{bg} = [0, 0, 0, 0]; 963 $label->{bg} = [0, 0, 0, 0];
834 } 964 }
835 965
836 if (@matches) { 966 if (@matches) {
837 $self->{select} = "$matches[0][0]$arg"; 967 $self->{select} = "$matches[0][1]";
838 968
839 $labels[0]->{fg} = [0, 0, 0, 1]; 969 $labels[0]->{fg} = [0, 0, 0, 1];
840 $labels[0]->{bg} = [1, 1, 1, 0.8]; 970 $labels[0]->{bg} = [1, 1, 1, 0.8];
841 } else { 971 } else {
842 $self->{select} = "$cmd$arg"; 972 $self->{select} = "$text";
843 } 973 }
844 974
845 for my $match (@matches) { 975 for my $match (@matches) {
846 my $label = shift @labels; 976 my $label = shift @labels;
847 977
848 if (@labels) { 978 if (@labels) {
849 $label->set_text ("$match->[0]$arg"); 979 $label->set_text ("$match->[1]");
850 $label->set_tooltip ($match->[1]); 980 $label->set_tooltip ("$match->[0][1]");
851 } else { 981 } else {
852 $label->set_text ("..."); 982 $label->set_text ("...");
853 $label->set_tooltip ("Use Cursor-Down to view more matches"); 983 $label->set_tooltip ("Use Cursor-Down to view more matches");
854 last; 984 last;
855 } 985 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines