--- deliantra/Deliantra-Client/DC/MapWidget.pm 2012/11/10 18:29:32 1.166 +++ deliantra/Deliantra-Client/DC/MapWidget.pm 2012/11/20 14:32:12 1.167 @@ -35,7 +35,7 @@ $self } -sub add_command { +sub add_command {#d# remove my ($self, $command, $tooltip, $widget, $cb) = @_; (my $data = $command) =~ s/\\//g; @@ -47,15 +47,6 @@ $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; } -sub clr_commands { - my ($self) = @_; - - %{$::COMPLETER->{command}} = (); - - $::COMPLETER->hide - if $::COMPLETER; -} - sub server_login { my ($server) = @_; @@ -820,6 +811,13 @@ $self } +sub reset { + my ($self) = @_; + + $self->hide; + delete $self->{command_list}; +} + sub set_prefix { my ($self, $prefix) = @_; @@ -866,6 +864,14 @@ length $text or return $self->hide; + return unless $::CONN; + + # regenerate spell list if necessary + $self->{command_list}{spells} ||= [ + map { ("cast $_->{name}", "invoke $_->{name}") } + values %{ $::CONN->{spell} } + ]; + if ($text ne $self->{last_search}) { my @match; @@ -896,46 +902,48 @@ qr<$regexp> }; - for (keys %{$self->{command}}) { - # we only match and score if the first character matches, - # so quickly rule out all others first. - next unless $first_char = substr $_, 0, 1; - - my @scores; - - # 1. Complete command [with args] - # command is a prefix of the text - # score is length of complete command matched - # e.g. "invoke summon pet monster bat" - # "invoke" "summon pet monster bat" = 6 - # "invoke summon pet monster" "bat" = 25 - if ((substr $text, 0, length $_) eq $_) { - push @scores, [$_, length $_, $text]; - } + for my $list (values %{ $self->{command_list} }) { + for (@$list) { + # we only match and score if the first character matches, + # so quickly rule out all others first. + next unless $first_char = substr $_, 0, 1; + + my @scores; + + # 1. Complete command [with args] + # command is a prefix of the text + # score is length of complete command matched + # e.g. "invoke summon pet monster bat" + # "invoke" "summon pet monster bat" = 6 + # "invoke summon pet monster" "bat" = 25 + if ((substr $text, 0, length $_) eq $_) { + push @scores, [$_, length $_, $text]; + } - # 2. Partial command - # text is a prefix of the full command - # score is the length of the input text - # e.g. "invoke s" - # "invoke small fireball" = 8 - # "invoke summon pet monster" = 8 + # 2. Partial command + # text is a prefix of the full command + # score is the length of the input text + # e.g. "invoke s" + # "invoke small fireball" = 8 + # "invoke summon pet monster" = 8 - if ($_ =~ $regexp_partial) { - push @scores, [$_, length $text, $_]; - } + if ($_ =~ $regexp_partial) { + push @scores, [$_, length $text, $_]; + } - # 3. Abbreviation match - # attempts to use first word of text as an abbreviated command - # score is length of word + 1 - 3 per non-word-initial character + # 3. Abbreviation match + # attempts to use first word of text as an abbreviated command + # score is length of word + 1 - 3 per non-word-initial character - if (my @penalty = $_ =~ $regexp_abbrev) { - push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; - } + if (my @penalty = $_ =~ $regexp_abbrev) { + push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; + } - # Pick the best option for this command - push @match, (sort { - $b->[1] <=> $a->[1] - } @scores)[0]; + # Pick the best option for this command + push @match, (sort { + $b->[1] <=> $a->[1] + } @scores)[0]; + } } # @match is now [command object, command with arguments]