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.135 by root, Sun Jul 20 02:51:40 2008 UTC vs.
Revision 1.159 by root, Tue Dec 22 00:35:44 2009 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
163sub invoke_button_down { 168sub invoke_button_down {
164 my ($self, $ev, $x, $y) = @_; 169 my ($self, $ev, $x, $y) = @_;
165 170
166 if ($ev->{button} == 1) { 171 if ($ev->{button} == 1) {
167 $self->grab_focus; 172 $self->grab_focus;
168 return unless $::CONN; 173 return unless $::CONN && $self->{ctilesize};
169 174
170 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};
171 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};
172 177
173 $x -= DC::floor $::MAP->w * 0.5; 178 $x -= DC::floor $::MAP->w * 0.5;
201 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], 206 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
202 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], 207 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
203 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], 208 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
204 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], 209 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
205# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], 210# ["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 ); 211 );
213 212
214 if ($::CONN && $::CONN->{editor_support}) { 213 if ($::CONN && $::CONN->{editor_support}) {
215# push @items, [ 214# push @items, [
216# "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>",
285 $self->{need_update} = 1; 284 $self->{need_update} = 1;
286 $self->SUPER::update; 285 $self->SUPER::update;
287} 286}
288 287
289my %DIR = ( 288my %DIR = (
290 ( "," . DC::SDLK_KP5 ), [0, "stay fire"], 289 ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
291 ( "," . DC::SDLK_KP8 ), [1, "north"], 290 ( "," . DC::SDLK_KP8 ), [1, "north"],
292 ( "," . DC::SDLK_KP9 ), [2, "northeast"], 291 ( "," . DC::SDLK_KP9 ), [2, "northeast"],
293 ( "," . DC::SDLK_KP6 ), [3, "east"], 292 ( "," . DC::SDLK_KP6 ), [3, "east"],
294 ( "," . DC::SDLK_KP3 ), [4, "southeast"], 293 ( "," . DC::SDLK_KP3 ), [4, "southeast"],
295 ( "," . DC::SDLK_KP2 ), [5, "south"], 294 ( "," . DC::SDLK_KP2 ), [5, "south"],
296 ( "," . DC::SDLK_KP1 ), [6, "southwest"], 295 ( "," . DC::SDLK_KP1 ), [6, "southwest"],
297 ( "," . DC::SDLK_KP4 ), [7, "west"], 296 ( "," . DC::SDLK_KP4 ), [7, "west"],
298 ( "," . DC::SDLK_KP7 ), [8, "northwest"], 297 ( "," . DC::SDLK_KP7 ), [8, "northwest"],
299 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
300 ( "," . DC::SDLK_UP ), [1, "north"], 304 ( "," . DC::SDLK_UP ), [1, "north"],
301 ("1," . DC::SDLK_UP ), [2, "northeast"], 305 ("1," . DC::SDLK_UP ), [2, "northeast"],
302 ( "," . DC::SDLK_RIGHT), [3, "east"], 306 ( "," . DC::SDLK_RIGHT ), [3, "east"],
303 ("1," . DC::SDLK_RIGHT), [4, "southeast"], 307 ("1," . DC::SDLK_RIGHT ), [4, "southeast"],
304 ( "," . DC::SDLK_DOWN ), [5, "south"], 308 ( "," . DC::SDLK_DOWN ), [5, "south"],
305 ("1," . DC::SDLK_DOWN ), [6, "southwest"], 309 ("1," . DC::SDLK_DOWN ), [6, "southwest"],
306 ( "," . DC::SDLK_LEFT ), [7, "west"], 310 ( "," . DC::SDLK_LEFT ), [7, "west"],
307 ("1," . DC::SDLK_LEFT ), [8, "northwest"], 311 ("1," . DC::SDLK_LEFT ), [8, "northwest"],
308); 312);
309 313
310sub invoke_key_down { 314sub invoke_key_down {
311 my ($self, $ev) = @_; 315 my ($self, $ev) = @_;
312 316
313 my $mod = $ev->{mod}; 317 my $mod = $ev->{mod};
314 my $sym = $ev->{sym}; 318 my $sym = $ev->{sym};
315 my $uni = $ev->{unicode}; 319 my $uni = $ev->{unicode};
316 320
317 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_SHIFT; 321 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT;
318 322
319 # ignore repeated keypresses 323 # ignore repeated keypresses
320 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym; 324 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym;
321 $self->{last_mod} = $mod; 325 $self->{last_mod} = $mod;
322 $self->{last_sym} = $sym; 326 $self->{last_sym} = $sym;
323 327
324 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) {
325 if ($mod & DC::KMOD_SHIFT) { 331 if ($mod & DC::KMOD_SHIFT) {
326 $self->{shft}++; 332 $self->{shft}++;
327 if ($dir->[0] != $self->{fire_dir}) { 333 if ($dir->[0] != $self->{fire_dir}) {
328 $::CONN->user_send ("fire $dir->[0]"); 334 $::CONN->user_send ("fire $dir->[0]");
329 } 335 }
356 $::CONN->user_send ("fire_stop"); 362 $::CONN->user_send ("fire_stop");
357 delete $self->{fire_dir}; 363 delete $self->{fire_dir};
358 $res = 1; 364 $res = 1;
359 } 365 }
360 } else { 366 } else {
361 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}) {
362 $::CONN->user_send ("fire_stop"); 370 $::CONN->user_send ("fire_stop");
363 delete $self->{fire_dir}; 371 delete $self->{fire_dir};
364 $res = 1; 372 $res = 1;
365 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT) 373 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT)
366 && delete $self->{shft}) { # XXX: is RSHIFT ok? 374 && delete $self->{shft}) { # XXX: is RSHIFT ok?
368 delete $self->{fire_dir}; 376 delete $self->{fire_dir};
369 $res = 1; 377 $res = 1;
370 } 378 }
371 } 379 }
372 380
373 if (!($mod & DC::KMOD_CTRL ) && delete $self->{ctrl}) { 381 if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) {
374 $::CONN->user_send ("run_stop"); 382 $::CONN->user_send ("run_stop");
375 $res = 1; 383 $res = 1;
376 } 384 }
377 385
378 $res 386 $res
393} 401}
394 402
395sub scroll { 403sub scroll {
396 my ($self, $dx, $dy) = @_; 404 my ($self, $dx, $dy) = @_;
397 405
398 $::MAP->scroll ($dx, $dy);
399
400 $self->movement_update; 406 $self->movement_update;
401 407
402 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement 408 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
403 $self->{sdy} += $dy * $self->{tilesize}; 409 $self->{sdy} += $dy * $self->{tilesize};
410
411 # save old fow texture, if applicable
412 $self->{last_fow_texture} = $self->{fow_texture}
413 if $::CFG->{smooth_transitions};
414 $self->{lfdx} = $dx;
415 $self->{lfdy} = $dy;
416 $self->{lmdx} = $self->{dx};
417 $self->{lmdy} = $self->{dy};
418
419 $::MAP->scroll ($dx, $dy);
404} 420}
405 421
406sub set_magicmap { 422sub set_magicmap {
407 my ($self, $w, $h, $x, $y, $data) = @_; 423 my ($self, $w, $h, $x, $y, $data) = @_;
408 424
421 if ($self->{sdx} || $self->{sdy}) { 437 if ($self->{sdx} || $self->{sdy}) {
422 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); 438 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH);
423 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; 439 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
424 440
425 # the minimum time for a single tile movement 441 # the minimum time for a single tile movement
426 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK); 442 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
443
444 $spd *= $self->{tilesize};
427 445
428 # jump if "impossibly high" speed 446 # jump if "impossibly high" speed
429 if ( 447 if (
430 (max abs $self->{sdx}, abs $self->{sdy}) 448 (max abs $self->{sdx}, abs $self->{sdy})
431 > $spd * $self->{tilesize} * $mintime * 1.1 449 > $spd * $mintime * 2.1
432 ) { 450 ) {
433 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 1.0;#d# 451 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d#
434 $self->{sdx} = $self->{sdy} = 0; 452 $self->{sdx} = $self->{sdy} = 0;
435 } else { 453 } else {
436 $spd *= $self->{tilesize} * $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction 454 $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction
437 455
438 my $dx = $self->{sdx} < 0 ? -$spd : $spd; 456 my $dx = $self->{sdx} < 0 ? -$spd : $spd;
439 my $dy = $self->{sdy} < 0 ? -$spd : $spd; 457 my $dy = $self->{sdy} < 0 ? -$spd : $spd;
440 458
441 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx } 459 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 } 460 if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy }
443
444 $self->update;
445 } 461 }
462
463 $self->update;
446 } 464 }
447 } else { 465 } else {
448 $self->{sdx} = $self->{sdy} = 0; 466 $self->{sdx} = $self->{sdy} = 0;
449 } 467 }
450 468
458 if (delete $self->{need_update}) { 476 if (delete $self->{need_update}) {
459 $self->movement_update; 477 $self->movement_update;
460 478
461 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1; 479 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
462 480
481 my $sdx_t = DC::ceil $self->{sdx} / $tilesize;
482 my $sdy_t = DC::ceil $self->{sdy} / $tilesize;
483
463 # width/height of map, in tiles 484 # width/height of map, in tiles
464 my $sw = $self->{sw} = 1 + DC::ceil $self->{w} / $tilesize; 485 my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize;
465 my $sh = $self->{sh} = 1 + DC::ceil $self->{h} / $tilesize; 486 my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize;
466 487
467 # the map displacement, in tiles 488 # the map displacement, in tiles
468 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $self->{sdx} / $self->{tilesize}; 489 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t;
469 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $self->{sdy} / $self->{tilesize}; 490 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t;
470 491
471 # the upper left "visible" corner, in pixels 492 # the upper left "visible" corner, in pixels
472 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize; 493 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
473 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize; 494 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
474 495
475 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; 496 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
476 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; 497 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
477 498
478 if ($::CFG->{fow_enable}) { 499 if ($::CFG->{fow_enable}) {
500 # draw_fow_texture REQUIRES the fow texture to stay the same size.
479 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); 501 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
480 502
481 $self->{fow_texture} = new DC::Texture 503 $self->{fow_texture} = new DC::Texture
482 w => $w, 504 w => $w,
483 h => $h, 505 h => $h,
491 glNewList ($self->{list} ||= glGenList); 513 glNewList ($self->{list} ||= glGenList);
492 514
493 glPushMatrix; 515 glPushMatrix;
494 glTranslate $sx0, $sy0; 516 glTranslate $sx0, $sy0;
495 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 517 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
496 glTranslate $self->{sdx}, $self->{sdy}; 518 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
497 519
498 warn "mapdraw -$self->{sdx}\n";#d#
499 $::MAP->draw ($dx, $dy, $sw, $sh, 520 $::MAP->draw ($dx, $dy, $sw, $sh,
500 $self->{tilesize}, 521 $self->{tilesize},
501 $::CONN->{player}{tag}, 522 $::CONN->{player}{tag},
502 -$self->{sdx}, -$self->{sdy}); 523 -$self->{sdx}, -$self->{sdy});
503 524
504 glTranslate -$self->{sdx}, -$self->{sdy};
505 glScale $self->{tilesize}, $self->{tilesize}; 525 glScale $self->{tilesize}, $self->{tilesize};
506 526
507 if (my $tex = $self->{fow_texture}) { 527 if (my $tex = $self->{fow_texture}) {
508 glPushMatrix; 528 if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {
509 glScale 1/3, 1/3; 529 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
510 glEnable GL_TEXTURE_2D; 530 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
512 531
513 glColor +($::CFG->{fow_intensity}) x 3, 0.9; 532 if ($d1 * $d2) {
514 $self->{fow_texture}->draw_quad_alpha (0, 0); 533 DC::Texture::draw_fow_texture
534 $::CFG->{fow_intensity},
535 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
536 @{$self->{fow_texture}}{qw(name data s t w h)},
537 (min 1, $d1 / $d2),
538 $self->{lmdx} - $dx - $self->{lfdx},
539 $self->{lmdy} - $dy - $self->{lfdy},
540 @{$self->{last_fow_texture}}{qw(name data)};
541 } else {
542 delete $self->{last_fow_texture};
543 }
544 }
515 545
516 glDisable GL_TEXTURE_2D; 546 unless ($self->{last_fow_texture}) {
517 glPopMatrix; 547 DC::Texture::draw_fow_texture
548 $::CFG->{fow_intensity},
549 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
550 @{$self->{fow_texture}}{qw(name data s t w h)};
551 }
518 } 552 }
519 553
520 if ($self->{magicmap}) { 554 if ($self->{magicmap}) {
521 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 555 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
522 556
523 $x += $::MAP->ox + $self->{dx}; 557 $x += $::MAP->ox + $self->{dx};
524 $y += $::MAP->oy + $self->{dy}; 558 $y += $::MAP->oy + $self->{dy};
525 559
526 glTranslate - $x - 1, - $y - 1; 560 glTranslate - $x - 1, - $y - 1;
527 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 561 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
528 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 562 $::MAP->draw_magicmap ($w, $h, $data);
529 } 563 }
530 564
531 glPopMatrix; 565 glPopMatrix;
532 glEndList; 566 glEndList;
533 } 567 }
534 } else { 568 } else {
569 delete $self->{last_fow_texture};
570 delete $self->{fow_texture};
571
535 glDeleteList delete $self->{list} 572 glDeleteList delete $self->{list}
536 if $self->{list}; 573 if $self->{list};
537 } 574 }
538} 575}
539 576
592sub refresh_hook { 629sub refresh_hook {
593 my ($self) = @_; 630 my ($self) = @_;
594 631
595 if ($::MAP && $self->{texture_atime} < time) { 632 if ($::MAP && $self->{texture_atime} < time) {
596 my ($w, $h) = @$self{qw(w h)}; 633 my ($w, $h) = @$self{qw(w h)};
634
635 return unless $w && $h;
597 636
598 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 637 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
599 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 638 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
600 639
601 my $ox = 0.5 * ($w - $sw); 640 my $ox = 0.5 * ($w - $sw);
830 my $text = $self->{entry}->get_text; 869 my $text = $self->{entry}->get_text;
831 870
832 length $text 871 length $text
833 or return $self->hide; 872 or return $self->hide;
834 873
835 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
836
837 if ($text ne $self->{last_search}) { 874 if ($text ne $self->{last_search}) {
838 my @match; 875 my @match;
839 876
840 if ($text =~ /^(.*?)\s+$/) { 877 if ($text =~ /^(.*?)\s+$/) {
878 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
841 @match = [$cmd, "(appended whitespace suppresses completion)"]; 879 @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]);
842 } else { 880 } else {
881 # @match is [command, penalty, command with arguments] until sort
882
883 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
884
843 my $regexp = do { 885 my $regexp_abbrev = do {
844 my ($beg, @chr) = split //, lc $cmd; 886 my ($beg, @chr) = split //, lc $cmd;
845 887
846 # the following regex is used to match our "completion entry" 888 # the following regex is used to match our "completion entry"
847 # to an actual command - the parentheses match kind of "overhead" 889 # to an actual command - the parentheses match kind of "overhead"
848 # - the more characters the parentheses match, the less attractive 890 # - the more characters the parentheses match, the less attractive
850 my $regexp = "^\Q$beg\E" 892 my $regexp = "^\Q$beg\E"
851 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 893 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
852 qr<$regexp> 894 qr<$regexp>
853 }; 895 };
854 896
855 my @penalty; 897 my $regexp_partial = do {
898 my $regexp = "^\Q$text\E(.*)";
899 qr<$regexp>
900 };
856 901
857 for (keys %{$self->{command}}) { 902 for (keys %{$self->{command}}) {
858 if (@penalty = $_ =~ $regexp) { 903 my @scores;
859 push @match, [$_, length join "", map "::$_", grep defined, @penalty]; 904
905 # 1. Complete command [with args]
906 # command is a prefix of the text
907 # score is length of complete command matched
908 # e.g. "invoke summon pet monster bat"
909 # "invoke" "summon pet monster bat" = 6
910 # "invoke summon pet monster" "bat" = 25
911 if ($text =~ /^\Q$_\E(.*)/) {
912 push @scores, [$_, length $_, $text];
860 } 913 }
914
915 # 2. Partial command
916 # text is a prefix of the full command
917 # score is the length of the input text
918 # e.g. "invoke s"
919 # "invoke small fireball" = 8
920 # "invoke summon pet monster" = 8
921
922 if ($_ =~ $regexp_partial) {
923 push @scores, [$_, length $text, $_];
924 }
925
926 # 3. Abbreviation match
927 # attempts to use first word of text as an abbreviated command
928 # score is length of word + 1 - 3 per non-word-initial character
929
930 if (my @penalty = $_ =~ $regexp_abbrev) {
931 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
932 }
933
934 # Pick the best option for this command
935 push @match, (sort {
936 $b->[1] <=> $a->[1]
937 } @scores)[0];
861 } 938 }
862 939
940 # @match is now [command object, command with arguments]
863 @match = map $self->{command}{$_->[0]}, 941 @match = map [$self->{command}{$_->[0]}, $_->[2]],
864 sort { 942 sort {
865 $a->[1] <=> $b->[1] 943 $b->[1] <=> $a->[1]
866 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] 944 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
867 or (length $b->[0]) <=> (length $a->[0]) 945 or (length $b->[0]) <=> (length $a->[0])
868 } @match; 946 } @match;
869 } 947 }
870 948
889 $label->{fg} = [1, 1, 1, 1]; 967 $label->{fg} = [1, 1, 1, 1];
890 $label->{bg} = [0, 0, 0, 0]; 968 $label->{bg} = [0, 0, 0, 0];
891 } 969 }
892 970
893 if (@matches) { 971 if (@matches) {
894 $self->{select} = "$matches[0][0]$arg"; 972 $self->{select} = "$matches[0][1]";
895 973
896 $labels[0]->{fg} = [0, 0, 0, 1]; 974 $labels[0]->{fg} = [0, 0, 0, 1];
897 $labels[0]->{bg} = [1, 1, 1, 0.8]; 975 $labels[0]->{bg} = [1, 1, 1, 0.8];
898 } else { 976 } else {
899 $self->{select} = "$cmd$arg"; 977 $self->{select} = "$text";
900 } 978 }
901 979
902 for my $match (@matches) { 980 for my $match (@matches) {
903 my $label = shift @labels; 981 my $label = shift @labels;
904 982
905 if (@labels) { 983 if (@labels) {
906 $label->set_text ("$match->[0]$arg"); 984 $label->set_text ("$match->[1]");
907 $label->set_tooltip ($match->[1]); 985 $label->set_tooltip ("$match->[0][1]");
908 } else { 986 } else {
909 $label->set_text ("..."); 987 $label->set_text ("...");
910 $label->set_tooltip ("Use Cursor-Down to view more matches"); 988 $label->set_tooltip ("Use Cursor-Down to view more matches");
911 last; 989 last;
912 } 990 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines