1 | package DC::MapWidget; |
1 | package DC::MapWidget; |
2 | |
2 | |
3 | use strict; |
3 | use common::sense; |
4 | use utf8; |
|
|
5 | |
4 | |
6 | use List::Util qw(min max); |
5 | use List::Util qw(min max); |
|
|
6 | |
|
|
7 | use AnyEvent (); |
7 | |
8 | |
8 | use DC; |
9 | use DC; |
9 | use DC::OpenGL; |
10 | use DC::OpenGL; |
10 | use DC::UI; |
11 | use DC::UI; |
11 | use DC::Macro; |
12 | use DC::Macro; |
12 | |
13 | |
13 | our @ISA = DC::UI::Base::; |
14 | our @ISA = DC::UI::Base::; |
14 | |
15 | |
|
|
16 | our @TEX_HIDDEN = map { |
|
|
17 | new_from_resource DC::Texture # MUST be POT |
|
|
18 | "hidden-$_.png", mipmap => 1, wrap => 1 |
|
|
19 | } 0, 1, 2; |
|
|
20 | |
15 | my $magicmap_tex = |
21 | my $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 | |
19 | sub new { |
25 | sub new { |
20 | my $class = shift; |
26 | my $class = shift; |
21 | |
27 | |
… | |
… | |
25 | tilesize => 32, |
31 | tilesize => 32, |
26 | @_ |
32 | @_ |
27 | ); |
33 | ); |
28 | |
34 | |
29 | $self |
35 | $self |
30 | } |
|
|
31 | |
|
|
32 | sub add_command { |
|
|
33 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
|
|
34 | |
|
|
35 | (my $data = $command) =~ s/\\//g; |
|
|
36 | |
|
|
37 | $tooltip =~ s/^\s+//; |
|
|
38 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
|
|
39 | $tooltip =~ s/\s+$//; |
|
|
40 | |
|
|
41 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
|
|
42 | } |
|
|
43 | |
|
|
44 | sub clr_commands { |
|
|
45 | my ($self) = @_; |
|
|
46 | |
|
|
47 | %{$::COMPLETER->{command}} = (); |
|
|
48 | |
|
|
49 | $::COMPLETER->hide |
|
|
50 | if $::COMPLETER; |
|
|
51 | } |
36 | } |
52 | |
37 | |
53 | sub server_login { |
38 | sub server_login { |
54 | my ($server) = @_; |
39 | my ($server) = @_; |
55 | |
40 | |
… | |
… | |
163 | sub invoke_button_down { |
148 | sub invoke_button_down { |
164 | my ($self, $ev, $x, $y) = @_; |
149 | my ($self, $ev, $x, $y) = @_; |
165 | |
150 | |
166 | if ($ev->{button} == 1) { |
151 | if ($ev->{button} == 1) { |
167 | $self->grab_focus; |
152 | $self->grab_focus; |
168 | return unless $::CONN; |
153 | return unless $::CONN && $self->{ctilesize}; |
169 | |
154 | |
170 | my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; |
155 | 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}; |
156 | my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; |
172 | |
157 | |
173 | $x -= DC::floor $::MAP->w * 0.5; |
158 | $x -= DC::floor $::MAP->w * 0.5; |
… | |
… | |
201 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
186 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
202 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
187 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
203 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
188 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
204 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
189 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
205 | # ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], |
190 | # ["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 | ); |
191 | ); |
213 | |
192 | |
214 | if ($::CONN && $::CONN->{editor_support}) { |
193 | if ($::CONN && $::CONN->{editor_support}) { |
215 | # push @items, [ |
194 | # push @items, [ |
216 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
195 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
217 | # \&editor_invoke, |
196 | # \&editor_invoke, |
218 | # ]; |
197 | # ]; |
219 | |
198 | |
220 | for my $type (qw(test name)) { |
199 | for my $type (@{ $::CONN->{editor_support}{servertypes} }) { |
221 | $::CONN->{editor_support}{type} ne $type |
200 | $::CONN->{editor_support}{servertype} ne $type |
222 | or next; |
201 | or next; |
223 | my $server = $::CONN->{editor_support}{"${type}server"} |
202 | my $server = $::CONN->{editor_support}{"${type}server"} |
224 | or next; |
203 | or next; |
225 | |
204 | |
226 | push @items, [ |
205 | push @items, [ |
… | |
… | |
402 | } |
381 | } |
403 | |
382 | |
404 | sub scroll { |
383 | sub scroll { |
405 | my ($self, $dx, $dy) = @_; |
384 | my ($self, $dx, $dy) = @_; |
406 | |
385 | |
407 | $::MAP->scroll ($dx, $dy); |
|
|
408 | |
|
|
409 | $self->movement_update; |
386 | $self->movement_update; |
410 | |
387 | |
411 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
388 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
412 | $self->{sdy} += $dy * $self->{tilesize}; |
389 | $self->{sdy} += $dy * $self->{tilesize}; |
|
|
390 | |
|
|
391 | # save old fow texture, if applicable |
|
|
392 | $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture}; |
|
|
393 | $self->{lfdx} = $dx; |
|
|
394 | $self->{lfdy} = $dy; |
|
|
395 | $self->{lmdx} = $self->{dx}; |
|
|
396 | $self->{lmdy} = $self->{dy}; |
|
|
397 | |
|
|
398 | $::MAP->scroll ($dx, $dy); |
413 | } |
399 | } |
414 | |
400 | |
415 | sub set_magicmap { |
401 | sub set_magicmap { |
416 | my ($self, $w, $h, $x, $y, $data) = @_; |
402 | my ($self, $w, $h, $x, $y, $data) = @_; |
417 | |
403 | |
… | |
… | |
426 | sub movement_update { |
412 | sub movement_update { |
427 | my ($self) = @_; |
413 | my ($self) = @_; |
428 | |
414 | |
429 | if ($::CFG->{smooth_movement}) { |
415 | if ($::CFG->{smooth_movement}) { |
430 | if ($self->{sdx} || $self->{sdy}) { |
416 | if ($self->{sdx} || $self->{sdy}) { |
431 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
417 | my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); |
432 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
418 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
433 | |
419 | |
434 | # the minimum time for a single tile movement |
420 | # the minimum time for a single tile movement |
435 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
421 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
|
|
422 | |
|
|
423 | $spd *= $self->{tilesize}; |
436 | |
424 | |
437 | # jump if "impossibly high" speed |
425 | # jump if "impossibly high" speed |
438 | if ( |
426 | if ( |
439 | (max abs $self->{sdx}, abs $self->{sdy}) |
427 | (max abs $self->{sdx}, abs $self->{sdy}) |
440 | > $spd * $self->{tilesize} * $mintime * 1.1 |
428 | > $spd * $mintime * 2.1 |
441 | ) { |
429 | ) { |
442 | #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 1.0;#d# |
430 | #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d# |
443 | $self->{sdx} = $self->{sdy} = 0; |
431 | $self->{sdx} = $self->{sdy} = 0; |
444 | } else { |
432 | } else { |
445 | $spd *= $self->{tilesize} * $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction |
433 | $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction |
446 | |
434 | |
447 | my $dx = $self->{sdx} < 0 ? -$spd : $spd; |
435 | my $dx = $self->{sdx} < 0 ? -$spd : $spd; |
448 | my $dy = $self->{sdy} < 0 ? -$spd : $spd; |
436 | my $dy = $self->{sdy} < 0 ? -$spd : $spd; |
449 | |
437 | |
450 | if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx } |
438 | if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx } |
451 | if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy } |
439 | if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy } |
452 | |
|
|
453 | $self->update; |
|
|
454 | } |
440 | } |
|
|
441 | |
|
|
442 | $self->update; |
455 | } |
443 | } |
456 | } else { |
444 | } else { |
457 | $self->{sdx} = $self->{sdy} = 0; |
445 | $self->{sdx} = $self->{sdy} = 0; |
458 | } |
446 | } |
459 | |
447 | |
460 | $self->{last_update} = EV::time; |
448 | $self->{last_update} = AE::time; |
461 | } |
449 | } |
462 | |
450 | |
463 | sub refresh_hook { |
451 | sub refresh_hook { |
464 | my ($self) = @_; |
452 | my ($self) = @_; |
465 | |
453 | |
… | |
… | |
486 | |
474 | |
487 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
475 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
488 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
476 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
489 | |
477 | |
490 | if ($::CFG->{fow_enable}) { |
478 | if ($::CFG->{fow_enable}) { |
491 | $sdx_t = $sdy_t = 0;#d# |
479 | # draw_fow_texture REQUIRES the fow texture to stay the same size. |
492 | my ($w, $h, $data) = $::MAP->fow_texture ( |
480 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
493 | $dx + (min 0, $sdx_t), |
|
|
494 | $dy + (min 0, $sdy_t), |
|
|
495 | $sw + abs $sdx_t, |
|
|
496 | $sh + abs $sdy_t |
|
|
497 | ); |
|
|
498 | |
481 | |
499 | $self->{fow_texture} = new DC::Texture |
482 | $self->{fow_texture} = new DC::Texture |
500 | w => $w, |
483 | w => $w, |
501 | h => $h, |
484 | h => $h, |
502 | data => $data, |
485 | data => $data, |
… | |
… | |
509 | glNewList ($self->{list} ||= glGenList); |
492 | glNewList ($self->{list} ||= glGenList); |
510 | |
493 | |
511 | glPushMatrix; |
494 | glPushMatrix; |
512 | glTranslate $sx0, $sy0; |
495 | glTranslate $sx0, $sy0; |
513 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
496 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
514 | glTranslate $self->{sdx}, $self->{sdy}; |
497 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
515 | |
498 | |
516 | $::MAP->draw ($dx, $dy, $sw, $sh, |
499 | $::MAP->draw ($dx, $dy, $sw, $sh, |
517 | $self->{tilesize}, |
500 | ($self->{tilesize}) x 2, |
518 | $::CONN->{player}{tag}, |
501 | $::CONN->{player}{tag}, |
519 | -$self->{sdx}, -$self->{sdy}); |
502 | -$self->{sdx}, -$self->{sdy}); |
520 | |
503 | |
521 | #glTranslate -$self->{sdx}, -$self->{sdy}; # anchro fow at player |
|
|
522 | glScale $self->{tilesize}, $self->{tilesize}; |
504 | glScale $self->{tilesize}, $self->{tilesize}; |
523 | |
505 | |
524 | if (my $tex = $self->{fow_texture}) { |
506 | if (my $tex = $self->{fow_texture}) { |
525 | glPushMatrix; |
507 | my @prev_fow_params; |
526 | glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); |
|
|
527 | glScale 1/3, 1/3; |
|
|
528 | glEnable GL_TEXTURE_2D; |
|
|
529 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
530 | |
508 | |
|
|
509 | if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) { |
|
|
510 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
|
|
511 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
|
|
512 | |
|
|
513 | if ($d1 * $d2) { |
|
|
514 | @prev_fow_params = ( |
|
|
515 | (min 1, $d1 / $d2), |
|
|
516 | $self->{lmdx} - $dx - $self->{lfdx}, |
|
|
517 | $self->{lmdy} - $dy - $self->{lfdy}, |
|
|
518 | @{$self->{prev_fow_texture}}{qw(name data)} |
|
|
519 | ); |
|
|
520 | } |
|
|
521 | } |
|
|
522 | |
|
|
523 | DC::Texture::draw_fow_texture |
531 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
524 | $::CFG->{fow_intensity}, |
532 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
525 | $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, |
533 | |
526 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
534 | glDisable GL_TEXTURE_2D; |
527 | @prev_fow_params; |
535 | glPopMatrix; |
|
|
536 | } |
528 | } |
537 | |
529 | |
538 | if ($self->{magicmap}) { |
530 | if ($self->{magicmap}) { |
539 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
531 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
540 | |
532 | |
541 | $x += $::MAP->ox + $self->{dx}; |
533 | $x += $::MAP->ox + $self->{dx}; |
542 | $y += $::MAP->oy + $self->{dy}; |
534 | $y += $::MAP->oy + $self->{dy}; |
543 | |
535 | |
544 | glTranslate - $x - 1, - $y - 1; |
536 | glTranslate - $x - 1, - $y - 1; |
545 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
537 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
546 | $::MAP->draw_magicmap ($x, $y, $w, $h, $data); |
538 | $::MAP->draw_magicmap ($w, $h, $data); |
547 | } |
539 | } |
548 | |
540 | |
549 | glPopMatrix; |
541 | glPopMatrix; |
550 | glEndList; |
542 | glEndList; |
551 | } |
543 | } |
552 | } else { |
544 | } else { |
|
|
545 | delete $self->{last_fow_texture}; |
|
|
546 | delete $self->{fow_texture}; |
|
|
547 | |
553 | glDeleteList delete $self->{list} |
548 | glDeleteList delete $self->{list} |
554 | if $self->{list}; |
549 | if $self->{list}; |
555 | } |
550 | } |
556 | } |
551 | } |
557 | |
552 | |
… | |
… | |
593 | $self->SUPER::DESTROY; |
588 | $self->SUPER::DESTROY; |
594 | } |
589 | } |
595 | |
590 | |
596 | package DC::MapWidget::MapMap; |
591 | package DC::MapWidget::MapMap; |
597 | |
592 | |
598 | use strict; |
593 | use common::sense; |
599 | use utf8; |
|
|
600 | |
594 | |
601 | our @ISA = DC::UI::Base::; |
595 | our @ISA = DC::UI::Base::; |
602 | |
596 | |
603 | use Time::HiRes qw(time); |
|
|
604 | use DC::OpenGL; |
597 | use DC::OpenGL; |
605 | |
598 | |
606 | sub size_request { |
599 | sub size_request { |
607 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
600 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
608 | } |
601 | } |
609 | |
602 | |
610 | sub refresh_hook { |
603 | sub refresh_hook { |
611 | my ($self) = @_; |
604 | my ($self) = @_; |
612 | |
605 | |
613 | if ($::MAP && $self->{texture_atime} < time) { |
606 | if ($::MAP && $self->{texture_atime} < AE::now) { |
614 | my ($w, $h) = @$self{qw(w h)}; |
607 | my ($w, $h) = @$self{qw(w h)}; |
|
|
608 | |
|
|
609 | return unless $w && $h; |
615 | |
610 | |
616 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
611 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
617 | my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
612 | my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
618 | |
613 | |
619 | my $ox = 0.5 * ($w - $sw); |
614 | my $ox = 0.5 * ($w - $sw); |
… | |
… | |
631 | $self->{sh} = $sh; |
626 | $self->{sh} = $sh; |
632 | |
627 | |
633 | $self->{x0} = $x0; |
628 | $self->{x0} = $x0; |
634 | $self->{y0} = $y0; |
629 | $self->{y0} = $y0; |
635 | |
630 | |
636 | $self->{texture_atime} = time + 1/3; |
631 | $self->{texture_atime} = AE::now + 1/2; |
637 | |
632 | |
638 | $self->{texture} = |
633 | $self->{texture} = |
639 | new DC::Texture |
634 | new DC::Texture |
640 | w => $w, |
635 | w => $w, |
641 | h => $h, |
636 | h => $h, |
… | |
… | |
696 | glDisable GL_BLEND; |
691 | glDisable GL_BLEND; |
697 | } |
692 | } |
698 | |
693 | |
699 | package DC::MapWidget::Command; |
694 | package DC::MapWidget::Command; |
700 | |
695 | |
701 | use strict; |
696 | use common::sense; |
702 | |
697 | |
703 | use DC::OpenGL; |
698 | use DC::OpenGL; |
704 | |
699 | |
705 | our @ISA = DC::UI::Frame::; |
700 | our @ISA = DC::UI::Frame::; |
706 | |
701 | |
707 | sub new { |
702 | sub new { |
708 | my $class = shift; |
703 | my $class = shift; |
709 | |
704 | |
710 | my $self = $class->SUPER::new ( |
705 | my $self = $class->SUPER::new ( |
|
|
706 | min_w => $::WIDTH * 0.25, # workaround for layout problems #d# |
711 | bg => [0, 0, 0, 0.8], |
707 | bg => [0, 0, 0, 0.8], |
712 | @_, |
708 | @_, |
713 | ); |
709 | ); |
714 | |
710 | |
715 | $self->add ($self->{vbox} = new DC::UI::VBox); |
711 | $self->add ($self->{vbox} = new DC::UI::VBox); |
… | |
… | |
802 | ); |
798 | ); |
803 | |
799 | |
804 | $self |
800 | $self |
805 | } |
801 | } |
806 | |
802 | |
|
|
803 | sub reset { |
|
|
804 | my ($self) = @_; |
|
|
805 | |
|
|
806 | $self->hide; |
|
|
807 | delete $self->{command_lists}; |
|
|
808 | delete $self->{command_list}; |
|
|
809 | } |
|
|
810 | |
807 | sub set_prefix { |
811 | sub set_prefix { |
808 | my ($self, $prefix) = @_; |
812 | my ($self, $prefix) = @_; |
809 | |
813 | |
810 | $self->{entry}->set_text ($prefix); |
814 | $self->{entry}->set_text ($prefix); |
811 | $self->show; |
815 | $self->show; |
… | |
… | |
843 | } |
847 | } |
844 | |
848 | |
845 | sub update_labels { |
849 | sub update_labels { |
846 | my ($self) = @_; |
850 | my ($self) = @_; |
847 | |
851 | |
|
|
852 | use sort qw(stable); |
|
|
853 | |
848 | my $text = $self->{entry}->get_text; |
854 | my $text = $self->{entry}->get_text; |
849 | |
855 | |
850 | length $text |
856 | length $text |
851 | or return $self->hide; |
857 | or return $self->hide; |
852 | |
858 | |
853 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
859 | return unless $::CONN; |
|
|
860 | |
|
|
861 | # regenerate spell list if necessary |
|
|
862 | $self->{command_list}{spells} ||= [ |
|
|
863 | map { ("cast $_->{name}", "invoke $_->{name}") } |
|
|
864 | values %{ $::CONN->{spell} } |
|
|
865 | ]; |
854 | |
866 | |
855 | if ($text ne $self->{last_search}) { |
867 | if ($text ne $self->{last_search}) { |
856 | my @match; |
868 | my @match; |
857 | |
869 | |
858 | if ($text =~ /^(.*?)\s+$/) { |
870 | if ($text =~ /^(.*?)\s+$/) { |
|
|
871 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
859 | @match = [$cmd, "(appended whitespace suppresses completion)"]; |
872 | @match = ([[$cmd,' (appended whitespace suppresses completion)'], $text]); |
860 | } else { |
873 | } else { |
|
|
874 | # @match is [command, penalty, command with arguments] until sort |
|
|
875 | |
|
|
876 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
877 | |
|
|
878 | my $first_char = substr $cmd, 0, 1; |
|
|
879 | |
861 | my $regexp = do { |
880 | my $regexp_abbrev = do { |
862 | my ($beg, @chr) = split //, lc $cmd; |
881 | my ($beg, @chr) = split //, lc $cmd; |
863 | |
882 | |
864 | # the following regex is used to match our "completion entry" |
883 | # the following regex is used to match our "completion entry" |
865 | # to an actual command - the parentheses match kind of "overhead" |
884 | # to an actual command - the parentheses match kind of "overhead" |
866 | # - the more characters the parentheses match, the less attractive |
885 | # - the more characters the parentheses match, the less attractive |
867 | # is the match. |
886 | # is the match. |
868 | my $regexp = "^\Q$beg\E" |
887 | my $regexp = "^\Q$beg\E" |
869 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
888 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
870 | qr<$regexp> |
889 | qr<$regexp> |
871 | }; |
890 | }; |
872 | |
891 | |
873 | my @penalty; |
892 | my $regexp_partial = do { |
|
|
893 | my $regexp = "^\Q$text\E(.*)"; |
|
|
894 | qr<$regexp> |
|
|
895 | }; |
874 | |
896 | |
875 | for (keys %{$self->{command}}) { |
897 | for my $list (@{ $self->{command_lists} }, "spells") { |
|
|
898 | for (@{ $self->{command_list}{$list} }) { |
|
|
899 | # we only match and score if the first character matches, |
|
|
900 | # so quickly rule out all others first. |
|
|
901 | next unless $first_char = substr $_, 0, 1; |
|
|
902 | |
|
|
903 | my @scores; |
|
|
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 ((substr $text, 0, length $_) eq $_) { |
|
|
912 | push @scores, [$_, length $_, $text]; |
|
|
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 | |
876 | if (@penalty = $_ =~ $regexp) { |
930 | if (my @penalty = $_ =~ $regexp_abbrev) { |
877 | push @match, [$_, length join "", map "::$_", grep defined, @penalty]; |
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 { $b->[1] <=> $a->[1] } @scores)[0]; |
878 | } |
936 | } |
879 | } |
937 | } |
880 | |
938 | |
881 | @match = map $self->{command}{$_->[0]}, |
939 | # @match is now [command object, command with arguments] |
882 | sort { |
940 | @match = map [$_->[0], $_->[2]], |
883 | $a->[1] <=> $b->[1] |
941 | sort { $b->[1] <=> $a->[1] } |
884 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
|
|
885 | or (length $b->[0]) <=> (length $a->[0]) |
|
|
886 | } @match; |
942 | @match; |
887 | } |
943 | } |
888 | |
944 | |
889 | $self->{last_search} = $text; |
945 | $self->{last_search} = $text; |
890 | $self->{last_match} = \@match; |
946 | $self->{last_match} = \@match; |
891 | |
947 | |
… | |
… | |
907 | $label->{fg} = [1, 1, 1, 1]; |
963 | $label->{fg} = [1, 1, 1, 1]; |
908 | $label->{bg} = [0, 0, 0, 0]; |
964 | $label->{bg} = [0, 0, 0, 0]; |
909 | } |
965 | } |
910 | |
966 | |
911 | if (@matches) { |
967 | if (@matches) { |
912 | $self->{select} = "$matches[0][0]$arg"; |
968 | $self->{select} = "$matches[0][1]"; |
913 | |
969 | |
914 | $labels[0]->{fg} = [0, 0, 0, 1]; |
970 | $labels[0]->{fg} = [0, 0, 0, 1]; |
915 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
971 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
916 | } else { |
972 | } else { |
917 | $self->{select} = "$cmd$arg"; |
973 | $self->{select} = "$text"; |
918 | } |
974 | } |
919 | |
975 | |
920 | for my $match (@matches) { |
976 | for my $match (@matches) { |
921 | my $label = shift @labels; |
977 | my $label = shift @labels; |
922 | |
978 | |
923 | if (@labels) { |
979 | if (@labels) { |
924 | $label->set_text ("$match->[0]$arg"); |
980 | $label->set_text ("$match->[1]"); |
925 | $label->set_tooltip ($match->[1]); |
981 | $label->set_tooltip ("#(command/$match->[1])"); |
926 | } else { |
982 | } else { |
927 | $label->set_text ("..."); |
983 | $label->set_text ("..."); |
928 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
984 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
929 | last; |
985 | last; |
930 | } |
986 | } |