1 | package DC::MapWidget; |
1 | package DC::MapWidget; |
2 | |
2 | |
3 | use strict; |
3 | use common::sense; |
4 | use utf8; |
|
|
5 | |
4 | |
6 | use List::Util qw(min max); |
5 | use List::Util qw(min max); |
7 | |
6 | |
8 | use DC; |
7 | use DC; |
9 | use DC::OpenGL; |
8 | use DC::OpenGL; |
10 | use DC::UI; |
9 | use DC::UI; |
11 | use DC::Macro; |
10 | use DC::Macro; |
12 | |
11 | |
13 | our @ISA = DC::UI::Base::; |
12 | our @ISA = DC::UI::Base::; |
|
|
13 | |
|
|
14 | our @TEX_HIDDEN = map { |
|
|
15 | new_from_resource DC::Texture # MUST be POT |
|
|
16 | "hidden-$_.png", mipmap => 1, wrap => 1 |
|
|
17 | } 0, 1, 2; |
14 | |
18 | |
15 | my $magicmap_tex = |
19 | my $magicmap_tex = |
16 | new_from_resource DC::Texture "magicmap.png", |
20 | new_from_resource DC::Texture "magicmap.png", |
17 | mipmap => 1, wrap => 0, internalformat => GL_ALPHA; |
21 | mipmap => 1, wrap => 0, internalformat => GL_ALPHA; |
18 | |
22 | |
… | |
… | |
163 | sub invoke_button_down { |
167 | sub invoke_button_down { |
164 | my ($self, $ev, $x, $y) = @_; |
168 | my ($self, $ev, $x, $y) = @_; |
165 | |
169 | |
166 | if ($ev->{button} == 1) { |
170 | if ($ev->{button} == 1) { |
167 | $self->grab_focus; |
171 | $self->grab_focus; |
168 | return unless $::CONN; |
172 | return unless $::CONN && $self->{ctilesize}; |
169 | |
173 | |
170 | my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; |
174 | 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}; |
175 | my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; |
172 | |
176 | |
173 | $x -= DC::floor $::MAP->w * 0.5; |
177 | $x -= DC::floor $::MAP->w * 0.5; |
… | |
… | |
201 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
205 | ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }], |
202 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
206 | ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }], |
203 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
207 | ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }], |
204 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
208 | ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }], |
205 | # ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }], |
209 | # ["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 | ); |
210 | ); |
213 | |
211 | |
214 | if ($::CONN && $::CONN->{editor_support}) { |
212 | if ($::CONN && $::CONN->{editor_support}) { |
215 | # push @items, [ |
213 | # push @items, [ |
216 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
214 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
217 | # \&editor_invoke, |
215 | # \&editor_invoke, |
218 | # ]; |
216 | # ]; |
219 | |
217 | |
220 | for my $type (qw(test name)) { |
218 | for my $type (@{ $::CONN->{editor_support}{servertypes} }) { |
221 | $::CONN->{editor_support}{type} ne $type |
219 | $::CONN->{editor_support}{servertype} ne $type |
222 | or next; |
220 | or next; |
223 | my $server = $::CONN->{editor_support}{"${type}server"} |
221 | my $server = $::CONN->{editor_support}{"${type}server"} |
224 | or next; |
222 | or next; |
225 | |
223 | |
226 | push @items, [ |
224 | push @items, [ |
… | |
… | |
402 | } |
400 | } |
403 | |
401 | |
404 | sub scroll { |
402 | sub scroll { |
405 | my ($self, $dx, $dy) = @_; |
403 | my ($self, $dx, $dy) = @_; |
406 | |
404 | |
407 | $::MAP->scroll ($dx, $dy); |
|
|
408 | |
|
|
409 | $self->movement_update; |
405 | $self->movement_update; |
410 | |
406 | |
411 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
407 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
412 | $self->{sdy} += $dy * $self->{tilesize}; |
408 | $self->{sdy} += $dy * $self->{tilesize}; |
|
|
409 | |
|
|
410 | # save old fow texture, if applicable |
|
|
411 | $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture}; |
|
|
412 | $self->{lfdx} = $dx; |
|
|
413 | $self->{lfdy} = $dy; |
|
|
414 | $self->{lmdx} = $self->{dx}; |
|
|
415 | $self->{lmdy} = $self->{dy}; |
|
|
416 | |
|
|
417 | $::MAP->scroll ($dx, $dy); |
413 | } |
418 | } |
414 | |
419 | |
415 | sub set_magicmap { |
420 | sub set_magicmap { |
416 | my ($self, $w, $h, $x, $y, $data) = @_; |
421 | my ($self, $w, $h, $x, $y, $data) = @_; |
417 | |
422 | |
… | |
… | |
488 | |
493 | |
489 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
494 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
490 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
495 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
491 | |
496 | |
492 | if ($::CFG->{fow_enable}) { |
497 | if ($::CFG->{fow_enable}) { |
493 | $sdx_t = $sdy_t = 0;#d# |
498 | # draw_fow_texture REQUIRES the fow texture to stay the same size. |
494 | my ($w, $h, $data) = $::MAP->fow_texture ( |
499 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
495 | $dx + (min 0, $sdx_t), |
|
|
496 | $dy + (min 0, $sdy_t), |
|
|
497 | $sw + abs $sdx_t, |
|
|
498 | $sh + abs $sdy_t |
|
|
499 | ); |
|
|
500 | |
500 | |
501 | $self->{fow_texture} = new DC::Texture |
501 | $self->{fow_texture} = new DC::Texture |
502 | w => $w, |
502 | w => $w, |
503 | h => $h, |
503 | h => $h, |
504 | data => $data, |
504 | data => $data, |
… | |
… | |
511 | glNewList ($self->{list} ||= glGenList); |
511 | glNewList ($self->{list} ||= glGenList); |
512 | |
512 | |
513 | glPushMatrix; |
513 | glPushMatrix; |
514 | glTranslate $sx0, $sy0; |
514 | glTranslate $sx0, $sy0; |
515 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
515 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
516 | glTranslate $self->{sdx}, $self->{sdy}; |
516 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
517 | |
517 | |
518 | $::MAP->draw ($dx, $dy, $sw, $sh, |
518 | $::MAP->draw ($dx, $dy, $sw, $sh, |
519 | $self->{tilesize}, |
519 | $self->{tilesize}, |
520 | $::CONN->{player}{tag}, |
520 | $::CONN->{player}{tag}, |
521 | -$self->{sdx}, -$self->{sdy}); |
521 | -$self->{sdx}, -$self->{sdy}); |
522 | |
522 | |
523 | #glTranslate -$self->{sdx}, -$self->{sdy}; # anchro fow at player |
|
|
524 | glScale $self->{tilesize}, $self->{tilesize}; |
523 | glScale $self->{tilesize}, $self->{tilesize}; |
525 | |
524 | |
526 | if (my $tex = $self->{fow_texture}) { |
525 | if (my $tex = $self->{fow_texture}) { |
527 | glPushMatrix; |
526 | my @prev_fow_params; |
528 | glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); |
|
|
529 | glScale 1/3, 1/3; |
|
|
530 | glEnable GL_TEXTURE_2D; |
|
|
531 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
532 | |
527 | |
|
|
528 | if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) { |
|
|
529 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
|
|
530 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
|
|
531 | |
|
|
532 | if ($d1 * $d2) { |
|
|
533 | @prev_fow_params = ( |
|
|
534 | (min 1, $d1 / $d2), |
|
|
535 | $self->{lmdx} - $dx - $self->{lfdx}, |
|
|
536 | $self->{lmdy} - $dy - $self->{lfdy}, |
|
|
537 | @{$self->{prev_fow_texture}}{qw(name data)} |
|
|
538 | ); |
|
|
539 | } |
|
|
540 | } |
|
|
541 | |
|
|
542 | DC::Texture::draw_fow_texture |
533 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
543 | $::CFG->{fow_intensity}, |
534 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
544 | $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, |
535 | |
545 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
536 | glDisable GL_TEXTURE_2D; |
546 | @prev_fow_params; |
537 | glPopMatrix; |
|
|
538 | } |
547 | } |
539 | |
548 | |
540 | if ($self->{magicmap}) { |
549 | if ($self->{magicmap}) { |
541 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
550 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
542 | |
551 | |
543 | $x += $::MAP->ox + $self->{dx}; |
552 | $x += $::MAP->ox + $self->{dx}; |
544 | $y += $::MAP->oy + $self->{dy}; |
553 | $y += $::MAP->oy + $self->{dy}; |
545 | |
554 | |
546 | glTranslate - $x - 1, - $y - 1; |
555 | glTranslate - $x - 1, - $y - 1; |
547 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
556 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
548 | $::MAP->draw_magicmap ($x, $y, $w, $h, $data); |
557 | $::MAP->draw_magicmap ($w, $h, $data); |
549 | } |
558 | } |
550 | |
559 | |
551 | glPopMatrix; |
560 | glPopMatrix; |
552 | glEndList; |
561 | glEndList; |
553 | } |
562 | } |
554 | } else { |
563 | } else { |
|
|
564 | delete $self->{last_fow_texture}; |
|
|
565 | delete $self->{fow_texture}; |
|
|
566 | |
555 | glDeleteList delete $self->{list} |
567 | glDeleteList delete $self->{list} |
556 | if $self->{list}; |
568 | if $self->{list}; |
557 | } |
569 | } |
558 | } |
570 | } |
559 | |
571 | |
… | |
… | |
595 | $self->SUPER::DESTROY; |
607 | $self->SUPER::DESTROY; |
596 | } |
608 | } |
597 | |
609 | |
598 | package DC::MapWidget::MapMap; |
610 | package DC::MapWidget::MapMap; |
599 | |
611 | |
600 | use strict; |
612 | use common::sense; |
601 | use utf8; |
|
|
602 | |
613 | |
603 | our @ISA = DC::UI::Base::; |
614 | our @ISA = DC::UI::Base::; |
604 | |
615 | |
605 | use Time::HiRes qw(time); |
616 | use Time::HiRes qw(time); |
606 | use DC::OpenGL; |
617 | use DC::OpenGL; |
… | |
… | |
700 | glDisable GL_BLEND; |
711 | glDisable GL_BLEND; |
701 | } |
712 | } |
702 | |
713 | |
703 | package DC::MapWidget::Command; |
714 | package DC::MapWidget::Command; |
704 | |
715 | |
705 | use strict; |
716 | use common::sense; |
706 | |
717 | |
707 | use DC::OpenGL; |
718 | use DC::OpenGL; |
708 | |
719 | |
709 | our @ISA = DC::UI::Frame::; |
720 | our @ISA = DC::UI::Frame::; |
710 | |
721 | |
… | |
… | |
852 | my $text = $self->{entry}->get_text; |
863 | my $text = $self->{entry}->get_text; |
853 | |
864 | |
854 | length $text |
865 | length $text |
855 | or return $self->hide; |
866 | or return $self->hide; |
856 | |
867 | |
857 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
858 | |
|
|
859 | if ($text ne $self->{last_search}) { |
868 | if ($text ne $self->{last_search}) { |
860 | my @match; |
869 | my @match; |
861 | |
870 | |
862 | if ($text =~ /^(.*?)\s+$/) { |
871 | if ($text =~ /^(.*?)\s+$/) { |
|
|
872 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
863 | @match = [$cmd, "(appended whitespace suppresses completion)"]; |
873 | @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]); |
864 | } else { |
874 | } else { |
|
|
875 | # @match is [command, penalty, command with arguments] until sort |
|
|
876 | |
|
|
877 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
878 | |
865 | my $regexp = do { |
879 | my $regexp_abbrev = do { |
866 | my ($beg, @chr) = split //, lc $cmd; |
880 | my ($beg, @chr) = split //, lc $cmd; |
867 | |
881 | |
868 | # the following regex is used to match our "completion entry" |
882 | # the following regex is used to match our "completion entry" |
869 | # to an actual command - the parentheses match kind of "overhead" |
883 | # to an actual command - the parentheses match kind of "overhead" |
870 | # - the more characters the parentheses match, the less attractive |
884 | # - the more characters the parentheses match, the less attractive |
… | |
… | |
872 | my $regexp = "^\Q$beg\E" |
886 | my $regexp = "^\Q$beg\E" |
873 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
887 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
874 | qr<$regexp> |
888 | qr<$regexp> |
875 | }; |
889 | }; |
876 | |
890 | |
877 | my @penalty; |
891 | my $regexp_partial = do { |
|
|
892 | my $regexp = "^\Q$text\E(.*)"; |
|
|
893 | qr<$regexp> |
|
|
894 | }; |
878 | |
895 | |
879 | for (keys %{$self->{command}}) { |
896 | for (keys %{$self->{command}}) { |
880 | if (@penalty = $_ =~ $regexp) { |
897 | my @scores; |
881 | push @match, [$_, length join "", map "::$_", grep defined, @penalty]; |
898 | |
|
|
899 | # 1. Complete command [with args] |
|
|
900 | # command is a prefix of the text |
|
|
901 | # score is length of complete command matched |
|
|
902 | # e.g. "invoke summon pet monster bat" |
|
|
903 | # "invoke" "summon pet monster bat" = 6 |
|
|
904 | # "invoke summon pet monster" "bat" = 25 |
|
|
905 | if ($text =~ /^\Q$_\E(.*)/) { |
|
|
906 | push @scores, [$_, length $_, $text]; |
882 | } |
907 | } |
|
|
908 | |
|
|
909 | # 2. Partial command |
|
|
910 | # text is a prefix of the full command |
|
|
911 | # score is the length of the input text |
|
|
912 | # e.g. "invoke s" |
|
|
913 | # "invoke small fireball" = 8 |
|
|
914 | # "invoke summon pet monster" = 8 |
|
|
915 | |
|
|
916 | if ($_ =~ $regexp_partial) { |
|
|
917 | push @scores, [$_, length $text, $_]; |
|
|
918 | } |
|
|
919 | |
|
|
920 | # 3. Abbreviation match |
|
|
921 | # attempts to use first word of text as an abbreviated command |
|
|
922 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
923 | |
|
|
924 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
925 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
926 | } |
|
|
927 | |
|
|
928 | # Pick the best option for this command |
|
|
929 | push @match, (sort { |
|
|
930 | $b->[1] <=> $a->[1] |
|
|
931 | } @scores)[0]; |
883 | } |
932 | } |
884 | |
933 | |
|
|
934 | # @match is now [command object, command with arguments] |
885 | @match = map $self->{command}{$_->[0]}, |
935 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
886 | sort { |
936 | sort { |
887 | $a->[1] <=> $b->[1] |
937 | $b->[1] <=> $a->[1] |
888 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
938 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
889 | or (length $b->[0]) <=> (length $a->[0]) |
939 | or (length $b->[0]) <=> (length $a->[0]) |
890 | } @match; |
940 | } @match; |
891 | } |
941 | } |
892 | |
942 | |
… | |
… | |
911 | $label->{fg} = [1, 1, 1, 1]; |
961 | $label->{fg} = [1, 1, 1, 1]; |
912 | $label->{bg} = [0, 0, 0, 0]; |
962 | $label->{bg} = [0, 0, 0, 0]; |
913 | } |
963 | } |
914 | |
964 | |
915 | if (@matches) { |
965 | if (@matches) { |
916 | $self->{select} = "$matches[0][0]$arg"; |
966 | $self->{select} = "$matches[0][1]"; |
917 | |
967 | |
918 | $labels[0]->{fg} = [0, 0, 0, 1]; |
968 | $labels[0]->{fg} = [0, 0, 0, 1]; |
919 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
969 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
920 | } else { |
970 | } else { |
921 | $self->{select} = "$cmd$arg"; |
971 | $self->{select} = "$text"; |
922 | } |
972 | } |
923 | |
973 | |
924 | for my $match (@matches) { |
974 | for my $match (@matches) { |
925 | my $label = shift @labels; |
975 | my $label = shift @labels; |
926 | |
976 | |
927 | if (@labels) { |
977 | if (@labels) { |
928 | $label->set_text ("$match->[0]$arg"); |
978 | $label->set_text ("$match->[1]"); |
929 | $label->set_tooltip ($match->[1]); |
979 | $label->set_tooltip ("$match->[0][1]"); |
930 | } else { |
980 | } else { |
931 | $label->set_text ("..."); |
981 | $label->set_text ("..."); |
932 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
982 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
933 | last; |
983 | last; |
934 | } |
984 | } |