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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines