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::; |
|
|
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; |
14 | |
20 | |
15 | my $magicmap_tex = |
21 | my $magicmap_tex = |
16 | new_from_resource DC::Texture "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 | |
… | |
… | |
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 | |
… | |
… | |
209 | # push @items, [ |
194 | # push @items, [ |
210 | # "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>", |
211 | # \&editor_invoke, |
196 | # \&editor_invoke, |
212 | # ]; |
197 | # ]; |
213 | |
198 | |
214 | for my $type (qw(test name)) { |
199 | for my $type (@{ $::CONN->{editor_support}{servertypes} }) { |
215 | $::CONN->{editor_support}{type} ne $type |
200 | $::CONN->{editor_support}{servertype} ne $type |
216 | or next; |
201 | or next; |
217 | my $server = $::CONN->{editor_support}{"${type}server"} |
202 | my $server = $::CONN->{editor_support}{"${type}server"} |
218 | or next; |
203 | or next; |
219 | |
204 | |
220 | push @items, [ |
205 | push @items, [ |
… | |
… | |
396 | } |
381 | } |
397 | |
382 | |
398 | sub scroll { |
383 | sub scroll { |
399 | my ($self, $dx, $dy) = @_; |
384 | my ($self, $dx, $dy) = @_; |
400 | |
385 | |
401 | $::MAP->scroll ($dx, $dy); |
|
|
402 | |
|
|
403 | $self->{last_fow_texture} = $self->{fow_texture}; |
|
|
404 | $self->{ldx} = $self->{sdx} + $dx * $self->{tilesize}; # smooth displacement |
|
|
405 | $self->{ldy} = $self->{sdy} + $dy * $self->{tilesize}; |
|
|
406 | |
|
|
407 | $self->movement_update; |
386 | $self->movement_update; |
408 | |
387 | |
409 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
388 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
410 | $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); |
411 | } |
399 | } |
412 | |
400 | |
413 | sub set_magicmap { |
401 | sub set_magicmap { |
414 | my ($self, $w, $h, $x, $y, $data) = @_; |
402 | my ($self, $w, $h, $x, $y, $data) = @_; |
415 | |
403 | |
… | |
… | |
424 | sub movement_update { |
412 | sub movement_update { |
425 | my ($self) = @_; |
413 | my ($self) = @_; |
426 | |
414 | |
427 | if ($::CFG->{smooth_movement}) { |
415 | if ($::CFG->{smooth_movement}) { |
428 | if ($self->{sdx} || $self->{sdy}) { |
416 | if ($self->{sdx} || $self->{sdy}) { |
429 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
417 | my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); |
430 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
418 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
431 | |
419 | |
432 | # the minimum time for a single tile movement |
420 | # the minimum time for a single tile movement |
433 | 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); |
434 | |
422 | |
… | |
… | |
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 | my ($sdx_t, $sdy_t);#d# do not anchor at player |
479 | # draw_fow_texture REQUIRES the fow texture to stay the same size. |
492 | |
|
|
493 | my ($w, $h, $data) = $::MAP->fow_texture ( |
480 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
494 | $dx + (min 0, $sdx_t), |
|
|
495 | $dy + (min 0, $sdy_t), |
|
|
496 | $sw + abs $sdx_t, |
|
|
497 | $sh + abs $sdy_t |
|
|
498 | ); |
|
|
499 | |
481 | |
500 | $self->{fow_texture} = new DC::Texture |
482 | $self->{fow_texture} = new DC::Texture |
501 | w => $w, |
483 | w => $w, |
502 | h => $h, |
484 | h => $h, |
503 | data => $data, |
485 | data => $data, |
… | |
… | |
513 | glTranslate $sx0, $sy0; |
495 | glTranslate $sx0, $sy0; |
514 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
496 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
515 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
497 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
516 | |
498 | |
517 | $::MAP->draw ($dx, $dy, $sw, $sh, |
499 | $::MAP->draw ($dx, $dy, $sw, $sh, |
518 | $self->{tilesize}, |
500 | ($self->{tilesize}) x 2, |
519 | $::CONN->{player}{tag}, |
501 | $::CONN->{player}{tag}, |
520 | -$self->{sdx}, -$self->{sdy}); |
502 | -$self->{sdx}, -$self->{sdy}); |
521 | |
503 | |
522 | #d#glTranslate -$self->{sdx}, -$self->{sdy}; # anchor fow at player |
|
|
523 | glScale $self->{tilesize}, $self->{tilesize}; |
504 | glScale $self->{tilesize}, $self->{tilesize}; |
524 | |
505 | |
525 | if (my $tex = $self->{fow_texture}) { |
506 | if (my $tex = $self->{fow_texture}) { |
526 | glEnable GL_TEXTURE_2D; |
507 | my @prev_fow_params; |
527 | glEnable GL_BLEND; |
|
|
528 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
529 | glBlendEquation GL_FUNC_REVERSE_SUBTRACT; |
|
|
530 | glBlendFunc GL_ONE, GL_ZERO; |
|
|
531 | |
508 | |
532 | my $a1 = 0.9; |
509 | if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) { |
533 | |
510 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
534 | if (0 && $self->{last_fow_texture}) { |
511 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
535 | my $d1 = ($self->{sdx} ** 2 + $self->{sdy} ** 2) ** 0.5; |
|
|
536 | my $d2 = ($self->{ldx} ** 2 + $self->{ldy} ** 2) ** 0.5; |
|
|
537 | |
512 | |
538 | if ($d1 * $d2) { |
513 | if ($d1 * $d2) { |
|
|
514 | @prev_fow_params = ( |
539 | my $a2 = $d1 / $d2; |
515 | (min 1, $d1 / $d2), |
540 | |
516 | $self->{lmdx} - $dx - $self->{lfdx}, |
541 | $a1 = 1 - $a2; |
517 | $self->{lmdy} - $dy - $self->{lfdy}, |
542 | |
518 | @{$self->{prev_fow_texture}}{qw(name data)} |
543 | #$a1 *= 0.9; |
|
|
544 | #$a2 *= 0.9; |
|
|
545 | |
|
|
546 | printf "%4g %4g %4g %4g\n", $d1, $d2, $a1, $a2;#d# |
|
|
547 | |
|
|
548 | #$a1 = 1 - (1-$a1) * 0.707; |
|
|
549 | #$a2 = 1 - (1-$a2) * 0.707; |
|
|
550 | |
|
|
551 | glColor 1,0,0,1; |
|
|
552 | glBegin GL_QUADS; |
|
|
553 | glVertex 10 + $sdx_t, 10; |
|
|
554 | glVertex 11 + $sdx_t, 10; |
|
|
555 | glVertex 11 + $sdx_t, 11; |
|
|
556 | glVertex 10 + $sdx_t, 11; |
|
|
557 | glEnd; |
519 | ); |
558 | |
|
|
559 | glColor +($::CFG->{fow_intensity}) x 3, $a2; |
|
|
560 | glPushMatrix; |
|
|
561 | glTranslate $sdx_t, $sdy_t; # anchor at player |
|
|
562 | glTranslate $self->{ldx} / -$tilesize, $self->{ldy} / -$tilesize; |
|
|
563 | glScale 1/3, 1/3; |
|
|
564 | $self->{last_fow_texture}->draw_quad_alpha (0, 0); |
|
|
565 | glPopMatrix; |
|
|
566 | } else { |
|
|
567 | delete $self->{last_fow_texture}; |
|
|
568 | } |
520 | } |
569 | } |
521 | } |
570 | |
522 | |
|
|
523 | DC::Texture::draw_fow_texture |
571 | glColor +($::CFG->{fow_intensity}) x 3, $a1; |
524 | $::CFG->{fow_intensity}, |
572 | glPushMatrix; |
525 | $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, |
573 | #d#glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); # anchor at player |
526 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
574 | glScale 1/3, 1/3; |
527 | @prev_fow_params; |
575 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
|
|
576 | glPopMatrix; |
|
|
577 | |
|
|
578 | glBlendEquation GL_FUNC_ADD; |
|
|
579 | glDisable GL_TEXTURE_2D; |
|
|
580 | glDisable GL_BLEND; |
|
|
581 | } |
528 | } |
582 | |
529 | |
583 | if ($self->{magicmap}) { |
530 | if ($self->{magicmap}) { |
584 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
531 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
585 | |
532 | |
586 | $x += $::MAP->ox + $self->{dx}; |
533 | $x += $::MAP->ox + $self->{dx}; |
587 | $y += $::MAP->oy + $self->{dy}; |
534 | $y += $::MAP->oy + $self->{dy}; |
588 | |
535 | |
589 | glTranslate - $x - 1, - $y - 1; |
536 | glTranslate - $x - 1, - $y - 1; |
590 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
537 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
591 | $::MAP->draw_magicmap ($x, $y, $w, $h, $data); |
538 | $::MAP->draw_magicmap ($w, $h, $data); |
592 | } |
539 | } |
593 | |
540 | |
594 | glPopMatrix; |
541 | glPopMatrix; |
595 | glEndList; |
542 | glEndList; |
596 | } |
543 | } |
597 | } else { |
544 | } else { |
|
|
545 | delete $self->{last_fow_texture}; |
|
|
546 | delete $self->{fow_texture}; |
|
|
547 | |
598 | glDeleteList delete $self->{list} |
548 | glDeleteList delete $self->{list} |
599 | if $self->{list}; |
549 | if $self->{list}; |
600 | } |
550 | } |
601 | } |
551 | } |
602 | |
552 | |
… | |
… | |
638 | $self->SUPER::DESTROY; |
588 | $self->SUPER::DESTROY; |
639 | } |
589 | } |
640 | |
590 | |
641 | package DC::MapWidget::MapMap; |
591 | package DC::MapWidget::MapMap; |
642 | |
592 | |
643 | use strict; |
593 | use common::sense; |
644 | use utf8; |
|
|
645 | |
594 | |
646 | our @ISA = DC::UI::Base::; |
595 | our @ISA = DC::UI::Base::; |
647 | |
596 | |
648 | use Time::HiRes qw(time); |
|
|
649 | use DC::OpenGL; |
597 | use DC::OpenGL; |
650 | |
598 | |
651 | sub size_request { |
599 | sub size_request { |
652 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
600 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
653 | } |
601 | } |
654 | |
602 | |
655 | sub refresh_hook { |
603 | sub refresh_hook { |
656 | my ($self) = @_; |
604 | my ($self) = @_; |
657 | |
605 | |
658 | if ($::MAP && $self->{texture_atime} < time) { |
606 | if ($::MAP && $self->{texture_atime} < AE::now) { |
659 | my ($w, $h) = @$self{qw(w h)}; |
607 | my ($w, $h) = @$self{qw(w h)}; |
660 | |
608 | |
661 | return unless $w && $h; |
609 | return unless $w && $h; |
662 | |
610 | |
663 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
611 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
… | |
… | |
678 | $self->{sh} = $sh; |
626 | $self->{sh} = $sh; |
679 | |
627 | |
680 | $self->{x0} = $x0; |
628 | $self->{x0} = $x0; |
681 | $self->{y0} = $y0; |
629 | $self->{y0} = $y0; |
682 | |
630 | |
683 | $self->{texture_atime} = time + 1/3; |
631 | $self->{texture_atime} = AE::now + 1/2; |
684 | |
632 | |
685 | $self->{texture} = |
633 | $self->{texture} = |
686 | new DC::Texture |
634 | new DC::Texture |
687 | w => $w, |
635 | w => $w, |
688 | h => $h, |
636 | h => $h, |
… | |
… | |
743 | glDisable GL_BLEND; |
691 | glDisable GL_BLEND; |
744 | } |
692 | } |
745 | |
693 | |
746 | package DC::MapWidget::Command; |
694 | package DC::MapWidget::Command; |
747 | |
695 | |
748 | use strict; |
696 | use common::sense; |
749 | |
697 | |
750 | use DC::OpenGL; |
698 | use DC::OpenGL; |
751 | |
699 | |
752 | our @ISA = DC::UI::Frame::; |
700 | our @ISA = DC::UI::Frame::; |
753 | |
701 | |
754 | sub new { |
702 | sub new { |
755 | my $class = shift; |
703 | my $class = shift; |
756 | |
704 | |
757 | my $self = $class->SUPER::new ( |
705 | my $self = $class->SUPER::new ( |
|
|
706 | min_w => $::WIDTH * 0.25, # workaround for layout problems #d# |
758 | bg => [0, 0, 0, 0.8], |
707 | bg => [0, 0, 0, 0.8], |
759 | @_, |
708 | @_, |
760 | ); |
709 | ); |
761 | |
710 | |
762 | $self->add ($self->{vbox} = new DC::UI::VBox); |
711 | $self->add ($self->{vbox} = new DC::UI::VBox); |
… | |
… | |
849 | ); |
798 | ); |
850 | |
799 | |
851 | $self |
800 | $self |
852 | } |
801 | } |
853 | |
802 | |
|
|
803 | sub reset { |
|
|
804 | my ($self) = @_; |
|
|
805 | |
|
|
806 | $self->hide; |
|
|
807 | delete $self->{command_lists}; |
|
|
808 | delete $self->{command_list}; |
|
|
809 | } |
|
|
810 | |
854 | sub set_prefix { |
811 | sub set_prefix { |
855 | my ($self, $prefix) = @_; |
812 | my ($self, $prefix) = @_; |
856 | |
813 | |
857 | $self->{entry}->set_text ($prefix); |
814 | $self->{entry}->set_text ($prefix); |
858 | $self->show; |
815 | $self->show; |
… | |
… | |
890 | } |
847 | } |
891 | |
848 | |
892 | sub update_labels { |
849 | sub update_labels { |
893 | my ($self) = @_; |
850 | my ($self) = @_; |
894 | |
851 | |
|
|
852 | use sort qw(stable); |
|
|
853 | |
895 | my $text = $self->{entry}->get_text; |
854 | my $text = $self->{entry}->get_text; |
896 | |
855 | |
897 | length $text |
856 | length $text |
898 | or return $self->hide; |
857 | or return $self->hide; |
899 | |
858 | |
|
|
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 | ]; |
|
|
866 | |
900 | if ($text ne $self->{last_search}) { |
867 | if ($text ne $self->{last_search}) { |
901 | my @match; |
868 | my @match; |
902 | |
869 | |
903 | if ($text =~ /^(.*?)\s+$/) { |
870 | if ($text =~ /^(.*?)\s+$/) { |
904 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
871 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
905 | @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]); |
872 | @match = ([[$cmd,' (appended whitespace suppresses completion)'], $text]); |
906 | } else { |
873 | } else { |
907 | # @match is [command, penalty, command with arguments] until sort |
874 | # @match is [command, penalty, command with arguments] until sort |
908 | |
875 | |
909 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
876 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
877 | |
|
|
878 | my $first_char = substr $cmd, 0, 1; |
910 | |
879 | |
911 | my $regexp_abbrev = do { |
880 | my $regexp_abbrev = do { |
912 | my ($beg, @chr) = split //, lc $cmd; |
881 | my ($beg, @chr) = split //, lc $cmd; |
913 | |
882 | |
914 | # the following regex is used to match our "completion entry" |
883 | # the following regex is used to match our "completion entry" |
915 | # to an actual command - the parentheses match kind of "overhead" |
884 | # to an actual command - the parentheses match kind of "overhead" |
916 | # - the more characters the parentheses match, the less attractive |
885 | # - the more characters the parentheses match, the less attractive |
917 | # is the match. |
886 | # is the match. |
918 | my $regexp = "^\Q$beg\E" |
887 | my $regexp = "^\Q$beg\E" |
919 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
888 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
920 | qr<$regexp> |
889 | qr<$regexp> |
921 | }; |
890 | }; |
922 | |
891 | |
923 | my $regexp_partial = do { |
892 | my $regexp_partial = do { |
924 | my $regexp = "^\Q$text\E(.*)"; |
893 | my $regexp = "^\Q$text\E(.*)"; |
925 | qr<$regexp> |
894 | qr<$regexp> |
926 | }; |
895 | }; |
927 | |
896 | |
928 | 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 | |
929 | my @scores; |
903 | my @scores; |
930 | |
904 | |
931 | # 1. Complete command [with args] |
905 | # 1. Complete command [with args] |
932 | # command is a prefix of the text |
906 | # command is a prefix of the text |
933 | # score is length of complete command matched |
907 | # score is length of complete command matched |
934 | # e.g. "invoke summon pet monster bat" |
908 | # e.g. "invoke summon pet monster bat" |
935 | # "invoke" "summon pet monster bat" = 6 |
909 | # "invoke" "summon pet monster bat" = 6 |
936 | # "invoke summon pet monster" "bat" = 25 |
910 | # "invoke summon pet monster" "bat" = 25 |
937 | if ($text =~ /^\Q$_\E(.*)/) { |
911 | if ((substr $text, 0, length $_) eq $_) { |
938 | push @scores, [$_, length $_, $text]; |
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 | |
|
|
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 { $b->[1] <=> $a->[1] } @scores)[0]; |
939 | } |
936 | } |
940 | |
|
|
941 | # 2. Partial command |
|
|
942 | # text is a prefix of the full command |
|
|
943 | # score is the length of the input text |
|
|
944 | # e.g. "invoke s" |
|
|
945 | # "invoke small fireball" = 8 |
|
|
946 | # "invoke summon pet monster" = 8 |
|
|
947 | |
|
|
948 | if ($_ =~ $regexp_partial) { |
|
|
949 | push @scores, [$_, length $text, $_]; |
|
|
950 | } |
|
|
951 | |
|
|
952 | # 3. Abbreviation match |
|
|
953 | # attempts to use first word of text as an abbreviated command |
|
|
954 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
955 | |
|
|
956 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
957 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
958 | } |
|
|
959 | |
|
|
960 | # Pick the best option for this command |
|
|
961 | push @match, (sort { |
|
|
962 | $b->[1] <=> $a->[1] |
|
|
963 | } @scores)[0]; |
|
|
964 | } |
937 | } |
965 | |
938 | |
966 | # @match is now [command object, command with arguments] |
939 | # @match is now [command object, command with arguments] |
967 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
940 | @match = map [$_->[0], $_->[2]], |
968 | sort { |
|
|
969 | $b->[1] <=> $a->[1] |
941 | sort { $b->[1] <=> $a->[1] } |
970 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
|
|
971 | or (length $b->[0]) <=> (length $a->[0]) |
|
|
972 | } @match; |
942 | @match; |
973 | } |
943 | } |
974 | |
944 | |
975 | $self->{last_search} = $text; |
945 | $self->{last_search} = $text; |
976 | $self->{last_match} = \@match; |
946 | $self->{last_match} = \@match; |
977 | |
947 | |
… | |
… | |
1006 | for my $match (@matches) { |
976 | for my $match (@matches) { |
1007 | my $label = shift @labels; |
977 | my $label = shift @labels; |
1008 | |
978 | |
1009 | if (@labels) { |
979 | if (@labels) { |
1010 | $label->set_text ("$match->[1]"); |
980 | $label->set_text ("$match->[1]"); |
1011 | $label->set_tooltip ("$match->[0][1]"); |
981 | $label->set_tooltip ("#(command/$match->[1])"); |
1012 | } else { |
982 | } else { |
1013 | $label->set_text ("..."); |
983 | $label->set_text ("..."); |
1014 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
984 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
1015 | last; |
985 | last; |
1016 | } |
986 | } |