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.150 by elmex, Wed Aug 12 18:41:39 2009 UTC vs.
Revision 1.171 by root, Mon Nov 26 13:14:43 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, [
396} 381}
397 382
398sub scroll { 383sub scroll {
399 my ($self, $dx, $dy) = @_; 384 my ($self, $dx, $dy) = @_;
400 385
401 $::MAP->scroll ($dx, $dy);
402
403 $self->movement_update; 386 $self->movement_update;
404 387
405 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement 388 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
406 $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);
407} 399}
408 400
409sub set_magicmap { 401sub set_magicmap {
410 my ($self, $w, $h, $x, $y, $data) = @_; 402 my ($self, $w, $h, $x, $y, $data) = @_;
411 403
420sub movement_update { 412sub movement_update {
421 my ($self) = @_; 413 my ($self) = @_;
422 414
423 if ($::CFG->{smooth_movement}) { 415 if ($::CFG->{smooth_movement}) {
424 if ($self->{sdx} || $self->{sdy}) { 416 if ($self->{sdx} || $self->{sdy}) {
425 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); 417 my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH);
426 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; 418 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
427 419
428 # the minimum time for a single tile movement 420 # the minimum time for a single tile movement
429 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);
430 422
451 } 443 }
452 } else { 444 } else {
453 $self->{sdx} = $self->{sdy} = 0; 445 $self->{sdx} = $self->{sdy} = 0;
454 } 446 }
455 447
456 $self->{last_update} = EV::time; 448 $self->{last_update} = AE::time;
457} 449}
458 450
459sub refresh_hook { 451sub refresh_hook {
460 my ($self) = @_; 452 my ($self) = @_;
461 453
482 474
483 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;
484 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;
485 477
486 if ($::CFG->{fow_enable}) { 478 if ($::CFG->{fow_enable}) {
487 $sdx_t = $sdy_t = 0;#d# 479 # draw_fow_texture REQUIRES the fow texture to stay the same size.
488 my ($w, $h, $data) = $::MAP->fow_texture ( 480 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
489 $dx + (min 0, $sdx_t),
490 $dy + (min 0, $sdy_t),
491 $sw + abs $sdx_t,
492 $sh + abs $sdy_t
493 );
494 481
495 $self->{fow_texture} = new DC::Texture 482 $self->{fow_texture} = new DC::Texture
496 w => $w, 483 w => $w,
497 h => $h, 484 h => $h,
498 data => $data, 485 data => $data,
508 glTranslate $sx0, $sy0; 495 glTranslate $sx0, $sy0;
509 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 496 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
510 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; 497 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
511 498
512 $::MAP->draw ($dx, $dy, $sw, $sh, 499 $::MAP->draw ($dx, $dy, $sw, $sh,
513 $self->{tilesize}, 500 ($self->{tilesize}) x 2,
514 $::CONN->{player}{tag}, 501 $::CONN->{player}{tag},
515 -$self->{sdx}, -$self->{sdy}); 502 -$self->{sdx}, -$self->{sdy});
516 503
517 #glTranslate -$self->{sdx}, -$self->{sdy}; # anchor fow at player
518 glScale $self->{tilesize}, $self->{tilesize}; 504 glScale $self->{tilesize}, $self->{tilesize};
519 505
520 if (my $tex = $self->{fow_texture}) { 506 if (my $tex = $self->{fow_texture}) {
521 glPushMatrix; 507 my @prev_fow_params;
522 glTranslate +(min 0, $sdx_t), (min 0, $sdy_t);
523 glScale 1/3, 1/3;
524 glEnable GL_TEXTURE_2D;
525 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
526 508
509 if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) {
510 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
511 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
512
513 if ($d1 * $d2) {
514 @prev_fow_params = (
515 (min 1, $d1 / $d2),
516 $self->{lmdx} - $dx - $self->{lfdx},
517 $self->{lmdy} - $dy - $self->{lfdy},
518 @{$self->{prev_fow_texture}}{qw(name data)}
519 );
520 }
521 }
522
523 DC::Texture::draw_fow_texture
527 glColor +($::CFG->{fow_intensity}) x 3, 0.9; 524 $::CFG->{fow_intensity},
528 $self->{fow_texture}->draw_quad_alpha (0, 0); 525 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
529 526 @{$self->{fow_texture}}{qw(name data s t w h)},
530 glDisable GL_TEXTURE_2D; 527 @prev_fow_params;
531 glPopMatrix;
532 } 528 }
533 529
534 if ($self->{magicmap}) { 530 if ($self->{magicmap}) {
535 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 531 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
536 532
537 $x += $::MAP->ox + $self->{dx}; 533 $x += $::MAP->ox + $self->{dx};
538 $y += $::MAP->oy + $self->{dy}; 534 $y += $::MAP->oy + $self->{dy};
539 535
540 glTranslate - $x - 1, - $y - 1; 536 glTranslate - $x - 1, - $y - 1;
541 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 537 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
542 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 538 $::MAP->draw_magicmap ($w, $h, $data);
543 } 539 }
544 540
545 glPopMatrix; 541 glPopMatrix;
546 glEndList; 542 glEndList;
547 } 543 }
548 } else { 544 } else {
545 delete $self->{last_fow_texture};
546 delete $self->{fow_texture};
547
549 glDeleteList delete $self->{list} 548 glDeleteList delete $self->{list}
550 if $self->{list}; 549 if $self->{list};
551 } 550 }
552} 551}
553 552
589 $self->SUPER::DESTROY; 588 $self->SUPER::DESTROY;
590} 589}
591 590
592package DC::MapWidget::MapMap; 591package DC::MapWidget::MapMap;
593 592
594use strict; 593use common::sense;
595use utf8;
596 594
597our @ISA = DC::UI::Base::; 595our @ISA = DC::UI::Base::;
598 596
599use Time::HiRes qw(time);
600use DC::OpenGL; 597use DC::OpenGL;
601 598
602sub size_request { 599sub size_request {
603 ($::HEIGHT * 0.2, $::HEIGHT * 0.2) 600 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
604} 601}
605 602
606sub refresh_hook { 603sub refresh_hook {
607 my ($self) = @_; 604 my ($self) = @_;
608 605
609 if ($::MAP && $self->{texture_atime} < time) { 606 if ($::MAP && $self->{texture_atime} < AE::now) {
610 my ($w, $h) = @$self{qw(w h)}; 607 my ($w, $h) = @$self{qw(w h)};
611 608
612 return unless $w && $h; 609 return unless $w && $h;
613 610
614 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 611 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
629 $self->{sh} = $sh; 626 $self->{sh} = $sh;
630 627
631 $self->{x0} = $x0; 628 $self->{x0} = $x0;
632 $self->{y0} = $y0; 629 $self->{y0} = $y0;
633 630
634 $self->{texture_atime} = time + 1/3; 631 $self->{texture_atime} = AE::now + 1/2;
635 632
636 $self->{texture} = 633 $self->{texture} =
637 new DC::Texture 634 new DC::Texture
638 w => $w, 635 w => $w,
639 h => $h, 636 h => $h,
694 glDisable GL_BLEND; 691 glDisable GL_BLEND;
695} 692}
696 693
697package DC::MapWidget::Command; 694package DC::MapWidget::Command;
698 695
699use strict; 696use common::sense;
700 697
701use DC::OpenGL; 698use DC::OpenGL;
702 699
703our @ISA = DC::UI::Frame::; 700our @ISA = DC::UI::Frame::;
704 701
705sub new { 702sub new {
706 my $class = shift; 703 my $class = shift;
707 704
708 my $self = $class->SUPER::new ( 705 my $self = $class->SUPER::new (
706 min_w => $::WIDTH * 0.25, # workaround for layout problems #d#
709 bg => [0, 0, 0, 0.8], 707 bg => [0, 0, 0, 0.8],
710 @_, 708 @_,
711 ); 709 );
712 710
713 $self->add ($self->{vbox} = new DC::UI::VBox); 711 $self->add ($self->{vbox} = new DC::UI::VBox);
800 ); 798 );
801 799
802 $self 800 $self
803} 801}
804 802
803sub reset {
804 my ($self) = @_;
805
806 $self->hide;
807 delete $self->{command_lists};
808 delete $self->{command_list};
809}
810
805sub set_prefix { 811sub set_prefix {
806 my ($self, $prefix) = @_; 812 my ($self, $prefix) = @_;
807 813
808 $self->{entry}->set_text ($prefix); 814 $self->{entry}->set_text ($prefix);
809 $self->show; 815 $self->show;
841} 847}
842 848
843sub update_labels { 849sub update_labels {
844 my ($self) = @_; 850 my ($self) = @_;
845 851
852 use sort qw(stable);
853
846 my $text = $self->{entry}->get_text; 854 my $text = $self->{entry}->get_text;
847 855
848 length $text 856 length $text
849 or return $self->hide; 857 or return $self->hide;
850 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
851 if ($text ne $self->{last_search}) { 867 if ($text ne $self->{last_search}) {
852 my @match; 868 my @match;
853 869
854 if ($text =~ /^(.*?)\s+$/) { 870 if ($text =~ /^(.*?)\s+$/) {
855 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; 871 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
856 @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]); 872 @match = ([[$cmd,' (appended whitespace suppresses completion)'], $text]);
857 } else { 873 } else {
858 # @match is [command, penalty, command with arguments] until sort 874 # @match is [command, penalty, command with arguments] until sort
859 875
860 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; 876 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
877
878 my $first_char = substr $cmd, 0, 1;
861 879
862 my $regexp_abbrev = do { 880 my $regexp_abbrev = do {
863 my ($beg, @chr) = split //, lc $cmd; 881 my ($beg, @chr) = split //, lc $cmd;
864 882
865 # the following regex is used to match our "completion entry" 883 # the following regex is used to match our "completion entry"
866 # to an actual command - the parentheses match kind of "overhead" 884 # to an actual command - the parentheses match kind of "overhead"
867 # - the more characters the parentheses match, the less attractive 885 # - the more characters the parentheses match, the less attractive
868 # is the match. 886 # is the match.
869 my $regexp = "^\Q$beg\E" 887 my $regexp = "^\Q$beg\E"
870 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 888 . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr;
871 qr<$regexp> 889 qr<$regexp>
872 }; 890 };
873 891
874 my $regexp_partial = do { 892 my $regexp_partial = do {
875 my $regexp = "^\Q$text\E(.*)"; 893 my $regexp = "^\Q$text\E(.*)";
876 qr<$regexp> 894 qr<$regexp>
877 }; 895 };
878 896
879 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
880 my @scores; 903 my @scores;
881 904
882 # 1. Complete command [with args] 905 # 1. Complete command [with args]
883 # command is a prefix of the text 906 # command is a prefix of the text
884 # score is length of complete command matched 907 # score is length of complete command matched
885 # e.g. "invoke summon pet monster bat" 908 # e.g. "invoke summon pet monster bat"
886 # "invoke" "summon pet monster bat" = 6 909 # "invoke" "summon pet monster bat" = 6
887 # "invoke summon pet monster" "bat" = 25 910 # "invoke summon pet monster" "bat" = 25
888 if ($text =~ /^\Q$_\E(.*)/) { 911 if ((substr $text, 0, length $_) eq $_) {
889 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];
890 } 936 }
891
892 # 2. Partial command
893 # text is a prefix of the full command
894 # score is the length of the input text
895 # e.g. "invoke s"
896 # "invoke small fireball" = 8
897 # "invoke summon pet monster" = 8
898
899 if ($_ =~ $regexp_partial) {
900 push @scores, [$_, length $text, $_];
901 }
902
903 # 3. Abbreviation match
904 # attempts to use first word of text as an abbreviated command
905 # score is length of word + 1 - 3 per non-word-initial character
906
907 if (my @penalty = $_ =~ $regexp_abbrev) {
908 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
909 }
910
911 # Pick the best option for this command
912 push @match, (sort {
913 $b->[1] <=> $a->[1]
914 } @scores)[0];
915 } 937 }
916 938
917 # @match is now [command object, command with arguments] 939 # @match is now [command object, command with arguments]
918 @match = map [$self->{command}{$_->[0]}, $_->[2]], 940 @match = map [$_->[0], $_->[2]],
919 sort {
920 $b->[1] <=> $a->[1] 941 sort { $b->[1] <=> $a->[1] }
921 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
922 or (length $b->[0]) <=> (length $a->[0])
923 } @match; 942 @match;
924 } 943 }
925 944
926 $self->{last_search} = $text; 945 $self->{last_search} = $text;
927 $self->{last_match} = \@match; 946 $self->{last_match} = \@match;
928 947
957 for my $match (@matches) { 976 for my $match (@matches) {
958 my $label = shift @labels; 977 my $label = shift @labels;
959 978
960 if (@labels) { 979 if (@labels) {
961 $label->set_text ("$match->[1]"); 980 $label->set_text ("$match->[1]");
962 $label->set_tooltip ("$match->[0][1]"); 981 $label->set_tooltip ("#(command/$match->[1])");
963 } else { 982 } else {
964 $label->set_text ("..."); 983 $label->set_text ("...");
965 $label->set_tooltip ("Use Cursor-Down to view more matches"); 984 $label->set_tooltip ("Use Cursor-Down to view more matches");
966 last; 985 last;
967 } 986 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines