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.142 by root, Sat Aug 30 08:04:01 2008 UTC vs.
Revision 1.158 by root, Mon Dec 21 23:52:34 2009 UTC

11use DC::Macro; 11use DC::Macro;
12 12
13our @ISA = DC::UI::Base::; 13our @ISA = DC::UI::Base::;
14 14
15my $magicmap_tex = 15my $magicmap_tex =
16 new_from_file DC::Texture DC::find_rcfile "magicmap.png", 16 new_from_resource DC::Texture "magicmap.png",
17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA; 17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
18 18
19sub new { 19sub new {
20 my $class = shift; 20 my $class = shift;
21 21
163sub invoke_button_down { 163sub invoke_button_down {
164 my ($self, $ev, $x, $y) = @_; 164 my ($self, $ev, $x, $y) = @_;
165 165
166 if ($ev->{button} == 1) { 166 if ($ev->{button} == 1) {
167 $self->grab_focus; 167 $self->grab_focus;
168 return unless $::CONN; 168 return unless $::CONN && $self->{ctilesize};
169 169
170 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; 170 my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize};
171 my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; 171 my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize};
172 172
173 $x -= DC::floor $::MAP->w * 0.5; 173 $x -= DC::floor $::MAP->w * 0.5;
201 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], 201 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
202 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], 202 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
203 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], 203 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
204 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], 204 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
205# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], 205# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
206 [
207 $::PICKUP_ENABLE->{state}
208 ? "Disable automatic pickup"
209 : "Enable automatic pickup",
210 sub { $::PICKUP_ENABLE->toggle }
211 ],
212 ); 206 );
213 207
214 if ($::CONN && $::CONN->{editor_support}) { 208 if ($::CONN && $::CONN->{editor_support}) {
215# push @items, [ 209# push @items, [
216# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", 210# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>",
402} 396}
403 397
404sub scroll { 398sub scroll {
405 my ($self, $dx, $dy) = @_; 399 my ($self, $dx, $dy) = @_;
406 400
407 $::MAP->scroll ($dx, $dy);
408
409 $self->movement_update; 401 $self->movement_update;
410 402
411 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement 403 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
412 $self->{sdy} += $dy * $self->{tilesize}; 404 $self->{sdy} += $dy * $self->{tilesize};
405
406 # save old fow texture, if applicable
407 $self->{last_fow_texture} = $self->{fow_texture}
408 if $::CFG->{smooth_transitions};
409 $self->{lfdx} = $dx;
410 $self->{lfdy} = $dy;
411 $self->{lmdx} = $self->{dx};
412 $self->{lmdy} = $self->{dy};
413
414 $::MAP->scroll ($dx, $dy);
413} 415}
414 416
415sub set_magicmap { 417sub set_magicmap {
416 my ($self, $w, $h, $x, $y, $data) = @_; 418 my ($self, $w, $h, $x, $y, $data) = @_;
417 419
432 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; 434 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
433 435
434 # the minimum time for a single tile movement 436 # the minimum time for a single tile movement
435 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); 437 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
436 438
439 $spd *= $self->{tilesize};
440
437 # jump if "impossibly high" speed 441 # jump if "impossibly high" speed
438 if ( 442 if (
439 (max abs $self->{sdx}, abs $self->{sdy}) 443 (max abs $self->{sdx}, abs $self->{sdy})
440 > $spd * $self->{tilesize} * $mintime * 1.1 444 > $spd * $mintime * 2.1
441 ) { 445 ) {
442 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 1.0;#d# 446 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d#
443 $self->{sdx} = $self->{sdy} = 0; 447 $self->{sdx} = $self->{sdy} = 0;
444 } else { 448 } else {
445 $spd *= $self->{tilesize} * $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction 449 $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction
446 450
447 my $dx = $self->{sdx} < 0 ? -$spd : $spd; 451 my $dx = $self->{sdx} < 0 ? -$spd : $spd;
448 my $dy = $self->{sdy} < 0 ? -$spd : $spd; 452 my $dy = $self->{sdy} < 0 ? -$spd : $spd;
449 453
450 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx } 454 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx }
451 if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy } 455 if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy }
452
453 $self->update;
454 } 456 }
457
458 $self->update;
455 } 459 }
456 } else { 460 } else {
457 $self->{sdx} = $self->{sdy} = 0; 461 $self->{sdx} = $self->{sdy} = 0;
458 } 462 }
459 463
486 490
487 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; 491 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
488 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; 492 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
489 493
490 if ($::CFG->{fow_enable}) { 494 if ($::CFG->{fow_enable}) {
491 $sdx_t = $sdy_t = 0;#d# 495 # draw_fow_texture REQUIRES the fow texture to stay the same size.
492 my ($w, $h, $data) = $::MAP->fow_texture ( 496 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
493 $dx + (min 0, $sdx_t),
494 $dy + (min 0, $sdy_t),
495 $sw + abs $sdx_t,
496 $sh + abs $sdy_t
497 );
498 497
499 $self->{fow_texture} = new DC::Texture 498 $self->{fow_texture} = new DC::Texture
500 w => $w, 499 w => $w,
501 h => $h, 500 h => $h,
502 data => $data, 501 data => $data,
509 glNewList ($self->{list} ||= glGenList); 508 glNewList ($self->{list} ||= glGenList);
510 509
511 glPushMatrix; 510 glPushMatrix;
512 glTranslate $sx0, $sy0; 511 glTranslate $sx0, $sy0;
513 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 512 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
514 glTranslate $self->{sdx}, $self->{sdy}; 513 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
515 514
516 $::MAP->draw ($dx, $dy, $sw, $sh, 515 $::MAP->draw ($dx, $dy, $sw, $sh,
517 $self->{tilesize}, 516 $self->{tilesize},
518 $::CONN->{player}{tag}, 517 $::CONN->{player}{tag},
519 -$self->{sdx}, -$self->{sdy}); 518 -$self->{sdx}, -$self->{sdy});
520 519
521 #glTranslate -$self->{sdx}, -$self->{sdy}; # anchro fow at player
522 glScale $self->{tilesize}, $self->{tilesize}; 520 glScale $self->{tilesize}, $self->{tilesize};
523 521
524 if (my $tex = $self->{fow_texture}) { 522 if (my $tex = $self->{fow_texture}) {
525 glPushMatrix; 523 if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d#
526 glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); 524 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
527 glScale 1/3, 1/3; 525 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
528 glEnable GL_TEXTURE_2D;
529 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
530 526
531 glColor +($::CFG->{fow_intensity}) x 3, 0.9; 527 if ($d1 * $d2) {
532 $self->{fow_texture}->draw_quad_alpha (0, 0); 528 DC::Texture::draw_fow_texture
529 $::CFG->{fow_intensity},
530 $self->{hidden_tex}{name},
531 @{$self->{fow_texture}}{qw(name data s t w h)},
532 (min 1, $d1 / $d2),
533 $self->{lmdx} - $dx - $self->{lfdx},
534 $self->{lmdy} - $dy - $self->{lfdy},
535 @{$self->{last_fow_texture}}{qw(name data)};
536 } else {
537 delete $self->{last_fow_texture};
538 }
539 }
533 540
534 glDisable GL_TEXTURE_2D; 541 unless ($self->{last_fow_texture}) {
535 glPopMatrix; 542 DC::Texture::draw_fow_texture
543 $::CFG->{fow_intensity},
544 $self->{hidden_tex}{name},
545 @{$self->{fow_texture}}{qw(name data s t w h)};
546 }
536 } 547 }
537 548
538 if ($self->{magicmap}) { 549 if ($self->{magicmap}) {
539 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 550 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
540 551
541 $x += $::MAP->ox + $self->{dx}; 552 $x += $::MAP->ox + $self->{dx};
542 $y += $::MAP->oy + $self->{dy}; 553 $y += $::MAP->oy + $self->{dy};
543 554
544 glTranslate - $x - 1, - $y - 1; 555 glTranslate - $x - 1, - $y - 1;
545 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 556 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
546 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 557 $::MAP->draw_magicmap ($w, $h, $data);
547 } 558 }
548 559
549 glPopMatrix; 560 glPopMatrix;
550 glEndList; 561 glEndList;
551 } 562 }
552 } else { 563 } else {
564 delete $self->{last_fow_texture};
565 delete $self->{fow_texture};
566
553 glDeleteList delete $self->{list} 567 glDeleteList delete $self->{list}
554 if $self->{list}; 568 if $self->{list};
555 } 569 }
556} 570}
557 571
610sub refresh_hook { 624sub refresh_hook {
611 my ($self) = @_; 625 my ($self) = @_;
612 626
613 if ($::MAP && $self->{texture_atime} < time) { 627 if ($::MAP && $self->{texture_atime} < time) {
614 my ($w, $h) = @$self{qw(w h)}; 628 my ($w, $h) = @$self{qw(w h)};
629
630 return unless $w && $h;
615 631
616 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 632 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
617 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 633 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
618 634
619 my $ox = 0.5 * ($w - $sw); 635 my $ox = 0.5 * ($w - $sw);
848 my $text = $self->{entry}->get_text; 864 my $text = $self->{entry}->get_text;
849 865
850 length $text 866 length $text
851 or return $self->hide; 867 or return $self->hide;
852 868
853 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
854
855 if ($text ne $self->{last_search}) { 869 if ($text ne $self->{last_search}) {
856 my @match; 870 my @match;
857 871
858 if ($text =~ /^(.*?)\s+$/) { 872 if ($text =~ /^(.*?)\s+$/) {
873 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
859 @match = [$cmd, "(appended whitespace suppresses completion)"]; 874 @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]);
860 } else { 875 } else {
876 # @match is [command, penalty, command with arguments] until sort
877
878 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
879
861 my $regexp = do { 880 my $regexp_abbrev = do {
862 my ($beg, @chr) = split //, lc $cmd; 881 my ($beg, @chr) = split //, lc $cmd;
863 882
864 # the following regex is used to match our "completion entry" 883 # the following regex is used to match our "completion entry"
865 # to an actual command - the parentheses match kind of "overhead" 884 # to an actual command - the parentheses match kind of "overhead"
866 # - the more characters the parentheses match, the less attractive 885 # - the more characters the parentheses match, the less attractive
868 my $regexp = "^\Q$beg\E" 887 my $regexp = "^\Q$beg\E"
869 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 888 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
870 qr<$regexp> 889 qr<$regexp>
871 }; 890 };
872 891
873 my @penalty; 892 my $regexp_partial = do {
893 my $regexp = "^\Q$text\E(.*)";
894 qr<$regexp>
895 };
874 896
875 for (keys %{$self->{command}}) { 897 for (keys %{$self->{command}}) {
876 if (@penalty = $_ =~ $regexp) { 898 my @scores;
877 push @match, [$_, length join "", map "::$_", grep defined, @penalty]; 899
900 # 1. Complete command [with args]
901 # command is a prefix of the text
902 # score is length of complete command matched
903 # e.g. "invoke summon pet monster bat"
904 # "invoke" "summon pet monster bat" = 6
905 # "invoke summon pet monster" "bat" = 25
906 if ($text =~ /^\Q$_\E(.*)/) {
907 push @scores, [$_, length $_, $text];
878 } 908 }
909
910 # 2. Partial command
911 # text is a prefix of the full command
912 # score is the length of the input text
913 # e.g. "invoke s"
914 # "invoke small fireball" = 8
915 # "invoke summon pet monster" = 8
916
917 if ($_ =~ $regexp_partial) {
918 push @scores, [$_, length $text, $_];
919 }
920
921 # 3. Abbreviation match
922 # attempts to use first word of text as an abbreviated command
923 # score is length of word + 1 - 3 per non-word-initial character
924
925 if (my @penalty = $_ =~ $regexp_abbrev) {
926 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
927 }
928
929 # Pick the best option for this command
930 push @match, (sort {
931 $b->[1] <=> $a->[1]
932 } @scores)[0];
879 } 933 }
880 934
935 # @match is now [command object, command with arguments]
881 @match = map $self->{command}{$_->[0]}, 936 @match = map [$self->{command}{$_->[0]}, $_->[2]],
882 sort { 937 sort {
883 $a->[1] <=> $b->[1] 938 $b->[1] <=> $a->[1]
884 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] 939 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
885 or (length $b->[0]) <=> (length $a->[0]) 940 or (length $b->[0]) <=> (length $a->[0])
886 } @match; 941 } @match;
887 } 942 }
888 943
907 $label->{fg} = [1, 1, 1, 1]; 962 $label->{fg} = [1, 1, 1, 1];
908 $label->{bg} = [0, 0, 0, 0]; 963 $label->{bg} = [0, 0, 0, 0];
909 } 964 }
910 965
911 if (@matches) { 966 if (@matches) {
912 $self->{select} = "$matches[0][0]$arg"; 967 $self->{select} = "$matches[0][1]";
913 968
914 $labels[0]->{fg} = [0, 0, 0, 1]; 969 $labels[0]->{fg} = [0, 0, 0, 1];
915 $labels[0]->{bg} = [1, 1, 1, 0.8]; 970 $labels[0]->{bg} = [1, 1, 1, 0.8];
916 } else { 971 } else {
917 $self->{select} = "$cmd$arg"; 972 $self->{select} = "$text";
918 } 973 }
919 974
920 for my $match (@matches) { 975 for my $match (@matches) {
921 my $label = shift @labels; 976 my $label = shift @labels;
922 977
923 if (@labels) { 978 if (@labels) {
924 $label->set_text ("$match->[0]$arg"); 979 $label->set_text ("$match->[1]");
925 $label->set_tooltip ($match->[1]); 980 $label->set_tooltip ("$match->[0][1]");
926 } else { 981 } else {
927 $label->set_text ("..."); 982 $label->set_text ("...");
928 $label->set_tooltip ("Use Cursor-Down to view more matches"); 983 $label->set_tooltip ("Use Cursor-Down to view more matches");
929 last; 984 last;
930 } 985 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines