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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines