… | |
… | |
31 | tilesize => 32, |
31 | tilesize => 32, |
32 | @_ |
32 | @_ |
33 | ); |
33 | ); |
34 | |
34 | |
35 | $self |
35 | $self |
36 | } |
|
|
37 | |
|
|
38 | sub add_command { |
|
|
39 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
|
|
40 | |
|
|
41 | (my $data = $command) =~ s/\\//g; |
|
|
42 | |
|
|
43 | $tooltip =~ s/^\s+//; |
|
|
44 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
|
|
45 | $tooltip =~ s/\s+$//; |
|
|
46 | |
|
|
47 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
|
|
48 | } |
|
|
49 | |
|
|
50 | sub clr_commands { |
|
|
51 | my ($self) = @_; |
|
|
52 | |
|
|
53 | %{$::COMPLETER->{command}} = (); |
|
|
54 | |
|
|
55 | $::COMPLETER->hide |
|
|
56 | if $::COMPLETER; |
|
|
57 | } |
36 | } |
58 | |
37 | |
59 | sub server_login { |
38 | sub server_login { |
60 | my ($server) = @_; |
39 | my ($server) = @_; |
61 | |
40 | |
… | |
… | |
722 | |
701 | |
723 | sub new { |
702 | sub new { |
724 | my $class = shift; |
703 | my $class = shift; |
725 | |
704 | |
726 | my $self = $class->SUPER::new ( |
705 | my $self = $class->SUPER::new ( |
|
|
706 | min_w => $::WIDTH * 0.25, # workaround for layout problems #d# |
727 | bg => [0, 0, 0, 0.8], |
707 | bg => [0, 0, 0, 0.8], |
728 | @_, |
708 | @_, |
729 | ); |
709 | ); |
730 | |
710 | |
731 | $self->add ($self->{vbox} = new DC::UI::VBox); |
711 | $self->add ($self->{vbox} = new DC::UI::VBox); |
… | |
… | |
818 | ); |
798 | ); |
819 | |
799 | |
820 | $self |
800 | $self |
821 | } |
801 | } |
822 | |
802 | |
|
|
803 | sub reset { |
|
|
804 | my ($self) = @_; |
|
|
805 | |
|
|
806 | $self->hide; |
|
|
807 | delete $self->{command_list}; |
|
|
808 | } |
|
|
809 | |
823 | sub set_prefix { |
810 | sub set_prefix { |
824 | my ($self, $prefix) = @_; |
811 | my ($self, $prefix) = @_; |
825 | |
812 | |
826 | $self->{entry}->set_text ($prefix); |
813 | $self->{entry}->set_text ($prefix); |
827 | $self->show; |
814 | $self->show; |
… | |
… | |
863 | |
850 | |
864 | my $text = $self->{entry}->get_text; |
851 | my $text = $self->{entry}->get_text; |
865 | |
852 | |
866 | length $text |
853 | length $text |
867 | or return $self->hide; |
854 | or return $self->hide; |
|
|
855 | |
|
|
856 | return unless $::CONN; |
|
|
857 | |
|
|
858 | # regenerate spell list if necessary |
|
|
859 | $self->{command_list}{spells} ||= [ |
|
|
860 | map { ("cast $_->{name}", "invoke $_->{name}") } |
|
|
861 | values %{ $::CONN->{spell} } |
|
|
862 | ]; |
868 | |
863 | |
869 | if ($text ne $self->{last_search}) { |
864 | if ($text ne $self->{last_search}) { |
870 | my @match; |
865 | my @match; |
871 | |
866 | |
872 | if ($text =~ /^(.*?)\s+$/) { |
867 | if ($text =~ /^(.*?)\s+$/) { |
… | |
… | |
885 | # the following regex is used to match our "completion entry" |
880 | # the following regex is used to match our "completion entry" |
886 | # to an actual command - the parentheses match kind of "overhead" |
881 | # to an actual command - the parentheses match kind of "overhead" |
887 | # - the more characters the parentheses match, the less attractive |
882 | # - the more characters the parentheses match, the less attractive |
888 | # is the match. |
883 | # is the match. |
889 | my $regexp = "^\Q$beg\E" |
884 | my $regexp = "^\Q$beg\E" |
890 | . join "", map "(?:.*?[ \\\\]|(.*?))\Q$_\E", @chr; |
885 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
891 | qr<$regexp> |
886 | qr<$regexp> |
892 | }; |
887 | }; |
893 | |
888 | |
894 | my $regexp_partial = do { |
889 | my $regexp_partial = do { |
895 | my $regexp = "^\Q$text\E(.*)"; |
890 | my $regexp = "^\Q$text\E(.*)"; |
896 | qr<$regexp> |
891 | qr<$regexp> |
897 | }; |
892 | }; |
898 | |
893 | |
899 | for (keys %{$self->{command}}) { |
894 | for my $list (values %{ $self->{command_list} }) { |
|
|
895 | for (@$list) { |
900 | # we only match and score if the first character matches, |
896 | # we only match and score if the first character matches, |
901 | # so quickly rule out all others first. |
897 | # so quickly rule out all others first. |
902 | next unless $first_char = substr $_, 0, 1; |
898 | next unless $first_char = substr $_, 0, 1; |
903 | |
899 | |
904 | my @scores; |
900 | my @scores; |
905 | |
901 | |
906 | # 1. Complete command [with args] |
902 | # 1. Complete command [with args] |
907 | # command is a prefix of the text |
903 | # command is a prefix of the text |
908 | # score is length of complete command matched |
904 | # score is length of complete command matched |
909 | # e.g. "invoke summon pet monster bat" |
905 | # e.g. "invoke summon pet monster bat" |
910 | # "invoke" "summon pet monster bat" = 6 |
906 | # "invoke" "summon pet monster bat" = 6 |
911 | # "invoke summon pet monster" "bat" = 25 |
907 | # "invoke summon pet monster" "bat" = 25 |
912 | if ((substr $text, 0, length $_) eq $_) { |
908 | if ((substr $text, 0, length $_) eq $_) { |
913 | push @scores, [$_, length $_, $text]; |
909 | push @scores, [$_, length $_, $text]; |
|
|
910 | } |
|
|
911 | |
|
|
912 | # 2. Partial command |
|
|
913 | # text is a prefix of the full command |
|
|
914 | # score is the length of the input text |
|
|
915 | # e.g. "invoke s" |
|
|
916 | # "invoke small fireball" = 8 |
|
|
917 | # "invoke summon pet monster" = 8 |
|
|
918 | |
|
|
919 | if ($_ =~ $regexp_partial) { |
|
|
920 | push @scores, [$_, length $text, $_]; |
|
|
921 | } |
|
|
922 | |
|
|
923 | # 3. Abbreviation match |
|
|
924 | # attempts to use first word of text as an abbreviated command |
|
|
925 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
926 | |
|
|
927 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
928 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
929 | } |
|
|
930 | |
|
|
931 | # Pick the best option for this command |
|
|
932 | push @match, (sort { |
|
|
933 | $b->[1] <=> $a->[1] |
|
|
934 | } @scores)[0]; |
914 | } |
935 | } |
915 | |
|
|
916 | # 2. Partial command |
|
|
917 | # text is a prefix of the full command |
|
|
918 | # score is the length of the input text |
|
|
919 | # e.g. "invoke s" |
|
|
920 | # "invoke small fireball" = 8 |
|
|
921 | # "invoke summon pet monster" = 8 |
|
|
922 | |
|
|
923 | if ($_ =~ $regexp_partial) { |
|
|
924 | push @scores, [$_, length $text, $_]; |
|
|
925 | } |
|
|
926 | |
|
|
927 | # 3. Abbreviation match |
|
|
928 | # attempts to use first word of text as an abbreviated command |
|
|
929 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
930 | |
|
|
931 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
932 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
933 | } |
|
|
934 | |
|
|
935 | # Pick the best option for this command |
|
|
936 | push @match, (sort { |
|
|
937 | $b->[1] <=> $a->[1] |
|
|
938 | } @scores)[0]; |
|
|
939 | } |
936 | } |
940 | |
937 | |
941 | # @match is now [command object, command with arguments] |
938 | # @match is now [command object, command with arguments] |
942 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
939 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
943 | sort { |
940 | sort { |