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.161 by root, Sat Mar 20 01:25:56 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines