--- deliantra/Deliantra-Client/DC/MapWidget.pm 2010/10/14 00:02:39 1.163 +++ deliantra/Deliantra-Client/DC/MapWidget.pm 2012/11/21 13:23:10 1.168 @@ -4,6 +4,8 @@ use List::Util qw(min max); +use AnyEvent (); + use DC; use DC::OpenGL; use DC::UI; @@ -33,27 +35,6 @@ $self } -sub add_command { - my ($self, $command, $tooltip, $widget, $cb) = @_; - - (my $data = $command) =~ s/\\//g; - - $tooltip =~ s/^\s+//; - $tooltip = "$data\n\n$tooltip"; - $tooltip =~ s/\s+$//; - - $::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) = @_; @@ -433,7 +414,7 @@ if ($::CFG->{smooth_movement}) { if ($self->{sdx} || $self->{sdy}) { - my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); + my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; # the minimum time for a single tile movement @@ -464,7 +445,7 @@ $self->{sdx} = $self->{sdy} = 0; } - $self->{last_update} = EV::time; + $self->{last_update} = AE::time; } sub refresh_hook { @@ -622,7 +603,7 @@ sub refresh_hook { my ($self) = @_; - if ($::MAP && $self->{texture_atime} < EV::now) { + if ($::MAP && $self->{texture_atime} < AE::now) { my ($w, $h) = @$self{qw(w h)}; return unless $w && $h; @@ -647,7 +628,7 @@ $self->{x0} = $x0; $self->{y0} = $y0; - $self->{texture_atime} = EV::now + 1/2; + $self->{texture_atime} = AE::now + 1/2; $self->{texture} = new DC::Texture @@ -818,6 +799,13 @@ $self } +sub reset { + my ($self) = @_; + + $self->hide; + delete $self->{command_list}; +} + sub set_prefix { my ($self, $prefix) = @_; @@ -864,6 +852,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; @@ -875,6 +871,8 @@ my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; + my $first_char = substr $cmd, 0, 1; + my $regexp_abbrev = do { my ($beg, @chr) = split //, lc $cmd; @@ -883,7 +881,7 @@ # - the more characters the parentheses match, the less attractive # is the match. my $regexp = "^\Q$beg\E" - . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; + . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; qr<$regexp> }; @@ -892,42 +890,48 @@ qr<$regexp> }; - for (keys %{$self->{command}}) { - my @scores; + 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]; + } - # 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 ($text =~ /^\Q$_\E(.*)/) { - 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]