ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/MapWidget.pm (file contents):
Revision 1.157 by root, Mon Dec 21 03:30:23 2009 UTC vs.
Revision 1.168 by root, Wed Nov 21 13:23:10 2012 UTC

1package DC::MapWidget; 1package DC::MapWidget;
2 2
3use strict; 3use common::sense;
4use utf8;
5 4
6use List::Util qw(min max); 5use List::Util qw(min max);
6
7use AnyEvent ();
7 8
8use DC; 9use DC;
9use DC::OpenGL; 10use DC::OpenGL;
10use DC::UI; 11use DC::UI;
11use DC::Macro; 12use DC::Macro;
12 13
13our @ISA = DC::UI::Base::; 14our @ISA = DC::UI::Base::;
15
16our @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
15my $magicmap_tex = 21my $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
32sub 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
44sub clr_commands {
45 my ($self) = @_;
46
47 %{$::COMPLETER->{command}} = ();
48
49 $::COMPLETER->hide
50 if $::COMPLETER;
51} 36}
52 37
53sub server_login { 38sub 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, [
402 387
403 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement 388 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
404 $self->{sdy} += $dy * $self->{tilesize}; 389 $self->{sdy} += $dy * $self->{tilesize};
405 390
406 # save old fow texture, if applicable 391 # save old fow texture, if applicable
407 $self->{last_fow_texture} = $self->{fow_texture} 392 $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture};
408 if $::CFG->{smooth_transitions};
409 $self->{lfdx} = $dx; 393 $self->{lfdx} = $dx;
410 $self->{lfdy} = $dy; 394 $self->{lfdy} = $dy;
411 $self->{lmdx} = $self->{dx}; 395 $self->{lmdx} = $self->{dx};
412 $self->{lmdy} = $self->{dy}; 396 $self->{lmdy} = $self->{dy};
413 397
428sub movement_update { 412sub movement_update {
429 my ($self) = @_; 413 my ($self) = @_;
430 414
431 if ($::CFG->{smooth_movement}) { 415 if ($::CFG->{smooth_movement}) {
432 if ($self->{sdx} || $self->{sdy}) { 416 if ($self->{sdx} || $self->{sdy}) {
433 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); 417 my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH);
434 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; 418 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
435 419
436 # the minimum time for a single tile movement 420 # the minimum time for a single tile movement
437 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);
438 422
459 } 443 }
460 } else { 444 } else {
461 $self->{sdx} = $self->{sdy} = 0; 445 $self->{sdx} = $self->{sdy} = 0;
462 } 446 }
463 447
464 $self->{last_update} = EV::time; 448 $self->{last_update} = AE::time;
465} 449}
466 450
467sub refresh_hook { 451sub refresh_hook {
468 my ($self) = @_; 452 my ($self) = @_;
469 453
490 474
491 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;
492 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;
493 477
494 if ($::CFG->{fow_enable}) { 478 if ($::CFG->{fow_enable}) {
479 # draw_fow_texture REQUIRES the fow texture to stay the same size.
495 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); 480 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
496 481
497 $self->{fow_texture} = new DC::Texture 482 $self->{fow_texture} = new DC::Texture
498 w => $w, 483 w => $w,
499 h => $h, 484 h => $h,
510 glTranslate $sx0, $sy0; 495 glTranslate $sx0, $sy0;
511 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 496 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
512 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; 497 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
513 498
514 $::MAP->draw ($dx, $dy, $sw, $sh, 499 $::MAP->draw ($dx, $dy, $sw, $sh,
515 $self->{tilesize}, 500 ($self->{tilesize}) x 2,
516 $::CONN->{player}{tag}, 501 $::CONN->{player}{tag},
517 -$self->{sdx}, -$self->{sdy}); 502 -$self->{sdx}, -$self->{sdy});
518 503
519 glScale $self->{tilesize}, $self->{tilesize}; 504 glScale $self->{tilesize}, $self->{tilesize};
520 505
521 if (my $tex = $self->{fow_texture}) { 506 if (my $tex = $self->{fow_texture}) {
507 my @prev_fow_params;
508
522 if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d# 509 if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) {
523 my $d1 = DC::distance $self->{sdx}, $self->{sdy}; 510 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
524 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; 511 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
525 512
526 if ($d1 * $d2) { 513 if ($d1 * $d2) {
527 DC::Texture::draw_fow_texture 514 @prev_fow_params = (
528 $::CFG->{fow_intensity},
529 @{$self->{fow_texture}}{qw(name s t w h)},
530 (min 1, $d1 / $d2), 515 (min 1, $d1 / $d2),
531 $self->{lmdx} - $dx - $self->{lfdx}, 516 $self->{lmdx} - $dx - $self->{lfdx},
532 $self->{lmdy} - $dy - $self->{lfdy}, 517 $self->{lmdy} - $dy - $self->{lfdy},
533 @{$self->{last_fow_texture}}{qw(name s t w h)}; 518 @{$self->{prev_fow_texture}}{qw(name data)}
534 } else { 519 );
535 delete $self->{last_fow_texture};
536 } 520 }
537 } 521 }
538 522
539 unless ($self->{last_fow_texture}) {
540 DC::Texture::draw_fow_texture 523 DC::Texture::draw_fow_texture
541 $::CFG->{fow_intensity}, 524 $::CFG->{fow_intensity},
525 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
542 @{$self->{fow_texture}}{qw(name s t w h)}; 526 @{$self->{fow_texture}}{qw(name data s t w h)},
543 } 527 @prev_fow_params;
544 } 528 }
545 529
546 if ($self->{magicmap}) { 530 if ($self->{magicmap}) {
547 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 531 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
548 532
556 540
557 glPopMatrix; 541 glPopMatrix;
558 glEndList; 542 glEndList;
559 } 543 }
560 } else { 544 } else {
545 delete $self->{last_fow_texture};
546 delete $self->{fow_texture};
547
561 glDeleteList delete $self->{list} 548 glDeleteList delete $self->{list}
562 if $self->{list}; 549 if $self->{list};
563 } 550 }
564} 551}
565 552
601 $self->SUPER::DESTROY; 588 $self->SUPER::DESTROY;
602} 589}
603 590
604package DC::MapWidget::MapMap; 591package DC::MapWidget::MapMap;
605 592
606use strict; 593use common::sense;
607use utf8;
608 594
609our @ISA = DC::UI::Base::; 595our @ISA = DC::UI::Base::;
610 596
611use Time::HiRes qw(time);
612use DC::OpenGL; 597use DC::OpenGL;
613 598
614sub size_request { 599sub size_request {
615 ($::HEIGHT * 0.2, $::HEIGHT * 0.2) 600 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
616} 601}
617 602
618sub refresh_hook { 603sub refresh_hook {
619 my ($self) = @_; 604 my ($self) = @_;
620 605
621 if ($::MAP && $self->{texture_atime} < time) { 606 if ($::MAP && $self->{texture_atime} < AE::now) {
622 my ($w, $h) = @$self{qw(w h)}; 607 my ($w, $h) = @$self{qw(w h)};
623 608
624 return unless $w && $h; 609 return unless $w && $h;
625 610
626 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 611 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
641 $self->{sh} = $sh; 626 $self->{sh} = $sh;
642 627
643 $self->{x0} = $x0; 628 $self->{x0} = $x0;
644 $self->{y0} = $y0; 629 $self->{y0} = $y0;
645 630
646 $self->{texture_atime} = time + 1/3; 631 $self->{texture_atime} = AE::now + 1/2;
647 632
648 $self->{texture} = 633 $self->{texture} =
649 new DC::Texture 634 new DC::Texture
650 w => $w, 635 w => $w,
651 h => $h, 636 h => $h,
706 glDisable GL_BLEND; 691 glDisable GL_BLEND;
707} 692}
708 693
709package DC::MapWidget::Command; 694package DC::MapWidget::Command;
710 695
711use strict; 696use common::sense;
712 697
713use DC::OpenGL; 698use DC::OpenGL;
714 699
715our @ISA = DC::UI::Frame::; 700our @ISA = DC::UI::Frame::;
716 701
812 ); 797 );
813 798
814 $self 799 $self
815} 800}
816 801
802sub reset {
803 my ($self) = @_;
804
805 $self->hide;
806 delete $self->{command_list};
807}
808
817sub set_prefix { 809sub set_prefix {
818 my ($self, $prefix) = @_; 810 my ($self, $prefix) = @_;
819 811
820 $self->{entry}->set_text ($prefix); 812 $self->{entry}->set_text ($prefix);
821 $self->show; 813 $self->show;
857 849
858 my $text = $self->{entry}->get_text; 850 my $text = $self->{entry}->get_text;
859 851
860 length $text 852 length $text
861 or return $self->hide; 853 or return $self->hide;
854
855 return unless $::CONN;
856
857 # regenerate spell list if necessary
858 $self->{command_list}{spells} ||= [
859 map { ("cast $_->{name}", "invoke $_->{name}") }
860 values %{ $::CONN->{spell} }
861 ];
862 862
863 if ($text ne $self->{last_search}) { 863 if ($text ne $self->{last_search}) {
864 my @match; 864 my @match;
865 865
866 if ($text =~ /^(.*?)\s+$/) { 866 if ($text =~ /^(.*?)\s+$/) {
869 } else { 869 } else {
870 # @match is [command, penalty, command with arguments] until sort 870 # @match is [command, penalty, command with arguments] until sort
871 871
872 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; 872 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
873 873
874 my $first_char = substr $cmd, 0, 1;
875
874 my $regexp_abbrev = do { 876 my $regexp_abbrev = do {
875 my ($beg, @chr) = split //, lc $cmd; 877 my ($beg, @chr) = split //, lc $cmd;
876 878
877 # the following regex is used to match our "completion entry" 879 # the following regex is used to match our "completion entry"
878 # to an actual command - the parentheses match kind of "overhead" 880 # to an actual command - the parentheses match kind of "overhead"
879 # - the more characters the parentheses match, the less attractive 881 # - the more characters the parentheses match, the less attractive
880 # is the match. 882 # is the match.
881 my $regexp = "^\Q$beg\E" 883 my $regexp = "^\Q$beg\E"
882 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 884 . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr;
883 qr<$regexp> 885 qr<$regexp>
884 }; 886 };
885 887
886 my $regexp_partial = do { 888 my $regexp_partial = do {
887 my $regexp = "^\Q$text\E(.*)"; 889 my $regexp = "^\Q$text\E(.*)";
888 qr<$regexp> 890 qr<$regexp>
889 }; 891 };
890 892
891 for (keys %{$self->{command}}) { 893 for my $list (values %{ $self->{command_list} }) {
894 for (@$list) {
895 # we only match and score if the first character matches,
896 # so quickly rule out all others first.
897 next unless $first_char = substr $_, 0, 1;
898
892 my @scores; 899 my @scores;
893 900
894 # 1. Complete command [with args] 901 # 1. Complete command [with args]
895 # command is a prefix of the text 902 # command is a prefix of the text
896 # score is length of complete command matched 903 # score is length of complete command matched
897 # e.g. "invoke summon pet monster bat" 904 # e.g. "invoke summon pet monster bat"
898 # "invoke" "summon pet monster bat" = 6 905 # "invoke" "summon pet monster bat" = 6
899 # "invoke summon pet monster" "bat" = 25 906 # "invoke summon pet monster" "bat" = 25
900 if ($text =~ /^\Q$_\E(.*)/) { 907 if ((substr $text, 0, length $_) eq $_) {
901 push @scores, [$_, length $_, $text]; 908 push @scores, [$_, length $_, $text];
909 }
910
911 # 2. Partial command
912 # text is a prefix of the full command
913 # score is the length of the input text
914 # e.g. "invoke s"
915 # "invoke small fireball" = 8
916 # "invoke summon pet monster" = 8
917
918 if ($_ =~ $regexp_partial) {
919 push @scores, [$_, length $text, $_];
920 }
921
922 # 3. Abbreviation match
923 # attempts to use first word of text as an abbreviated command
924 # score is length of word + 1 - 3 per non-word-initial character
925
926 if (my @penalty = $_ =~ $regexp_abbrev) {
927 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
928 }
929
930 # Pick the best option for this command
931 push @match, (sort {
932 $b->[1] <=> $a->[1]
933 } @scores)[0];
902 } 934 }
903
904 # 2. Partial command
905 # text is a prefix of the full command
906 # score is the length of the input text
907 # e.g. "invoke s"
908 # "invoke small fireball" = 8
909 # "invoke summon pet monster" = 8
910
911 if ($_ =~ $regexp_partial) {
912 push @scores, [$_, length $text, $_];
913 }
914
915 # 3. Abbreviation match
916 # attempts to use first word of text as an abbreviated command
917 # score is length of word + 1 - 3 per non-word-initial character
918
919 if (my @penalty = $_ =~ $regexp_abbrev) {
920 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
921 }
922
923 # Pick the best option for this command
924 push @match, (sort {
925 $b->[1] <=> $a->[1]
926 } @scores)[0];
927 } 935 }
928 936
929 # @match is now [command object, command with arguments] 937 # @match is now [command object, command with arguments]
930 @match = map [$self->{command}{$_->[0]}, $_->[2]], 938 @match = map [$self->{command}{$_->[0]}, $_->[2]],
931 sort { 939 sort {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines