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.143 by root, Wed Sep 3 06:07:39 2008 UTC vs.
Revision 1.161 by root, Sat Mar 20 01:25:56 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines