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 | |
… | |
… | |
209 | # push @items, [ |
213 | # push @items, [ |
210 | # "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>", |
211 | # \&editor_invoke, |
215 | # \&editor_invoke, |
212 | # ]; |
216 | # ]; |
213 | |
217 | |
214 | for my $type (qw(test name)) { |
218 | for my $type (@{ $::CONN->{editor_support}{servertypes} }) { |
215 | $::CONN->{editor_support}{type} ne $type |
219 | $::CONN->{editor_support}{servertype} ne $type |
216 | or next; |
220 | or next; |
217 | my $server = $::CONN->{editor_support}{"${type}server"} |
221 | my $server = $::CONN->{editor_support}{"${type}server"} |
218 | or next; |
222 | or next; |
219 | |
223 | |
220 | push @items, [ |
224 | push @items, [ |
… | |
… | |
396 | } |
400 | } |
397 | |
401 | |
398 | sub scroll { |
402 | sub scroll { |
399 | my ($self, $dx, $dy) = @_; |
403 | my ($self, $dx, $dy) = @_; |
400 | |
404 | |
401 | $::MAP->scroll ($dx, $dy); |
|
|
402 | |
|
|
403 | $self->movement_update; |
405 | $self->movement_update; |
404 | |
406 | |
405 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
407 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
406 | $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); |
407 | } |
418 | } |
408 | |
419 | |
409 | sub set_magicmap { |
420 | sub set_magicmap { |
410 | my ($self, $w, $h, $x, $y, $data) = @_; |
421 | my ($self, $w, $h, $x, $y, $data) = @_; |
411 | |
422 | |
… | |
… | |
482 | |
493 | |
483 | 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; |
484 | 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; |
485 | |
496 | |
486 | if ($::CFG->{fow_enable}) { |
497 | if ($::CFG->{fow_enable}) { |
487 | $sdx_t = $sdy_t = 0;#d# |
498 | # draw_fow_texture REQUIRES the fow texture to stay the same size. |
488 | my ($w, $h, $data) = $::MAP->fow_texture ( |
499 | 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 | |
500 | |
495 | $self->{fow_texture} = new DC::Texture |
501 | $self->{fow_texture} = new DC::Texture |
496 | w => $w, |
502 | w => $w, |
497 | h => $h, |
503 | h => $h, |
498 | data => $data, |
504 | data => $data, |
… | |
… | |
505 | glNewList ($self->{list} ||= glGenList); |
511 | glNewList ($self->{list} ||= glGenList); |
506 | |
512 | |
507 | glPushMatrix; |
513 | glPushMatrix; |
508 | glTranslate $sx0, $sy0; |
514 | glTranslate $sx0, $sy0; |
509 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
515 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
510 | glTranslate $self->{sdx}, $self->{sdy}; |
516 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
511 | |
517 | |
512 | $::MAP->draw ($dx, $dy, $sw, $sh, |
518 | $::MAP->draw ($dx, $dy, $sw, $sh, |
513 | $self->{tilesize}, |
519 | $self->{tilesize}, |
514 | $::CONN->{player}{tag}, |
520 | $::CONN->{player}{tag}, |
515 | -$self->{sdx}, -$self->{sdy}); |
521 | -$self->{sdx}, -$self->{sdy}); |
516 | |
522 | |
517 | #glTranslate -$self->{sdx}, -$self->{sdy}; # anchor fow at player |
|
|
518 | glScale $self->{tilesize}, $self->{tilesize}; |
523 | glScale $self->{tilesize}, $self->{tilesize}; |
519 | |
524 | |
520 | if (my $tex = $self->{fow_texture}) { |
525 | if (my $tex = $self->{fow_texture}) { |
521 | glPushMatrix; |
526 | 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 | |
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 |
527 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
543 | $::CFG->{fow_intensity}, |
528 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
544 | $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, |
529 | |
545 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
530 | glDisable GL_TEXTURE_2D; |
546 | @prev_fow_params; |
531 | glPopMatrix; |
|
|
532 | } |
547 | } |
533 | |
548 | |
534 | if ($self->{magicmap}) { |
549 | if ($self->{magicmap}) { |
535 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
550 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
536 | |
551 | |
537 | $x += $::MAP->ox + $self->{dx}; |
552 | $x += $::MAP->ox + $self->{dx}; |
538 | $y += $::MAP->oy + $self->{dy}; |
553 | $y += $::MAP->oy + $self->{dy}; |
539 | |
554 | |
540 | glTranslate - $x - 1, - $y - 1; |
555 | glTranslate - $x - 1, - $y - 1; |
541 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
556 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
542 | $::MAP->draw_magicmap ($x, $y, $w, $h, $data); |
557 | $::MAP->draw_magicmap ($w, $h, $data); |
543 | } |
558 | } |
544 | |
559 | |
545 | glPopMatrix; |
560 | glPopMatrix; |
546 | glEndList; |
561 | glEndList; |
547 | } |
562 | } |
548 | } else { |
563 | } else { |
|
|
564 | delete $self->{last_fow_texture}; |
|
|
565 | delete $self->{fow_texture}; |
|
|
566 | |
549 | glDeleteList delete $self->{list} |
567 | glDeleteList delete $self->{list} |
550 | if $self->{list}; |
568 | if $self->{list}; |
551 | } |
569 | } |
552 | } |
570 | } |
553 | |
571 | |
… | |
… | |
589 | $self->SUPER::DESTROY; |
607 | $self->SUPER::DESTROY; |
590 | } |
608 | } |
591 | |
609 | |
592 | package DC::MapWidget::MapMap; |
610 | package DC::MapWidget::MapMap; |
593 | |
611 | |
594 | use strict; |
612 | use common::sense; |
595 | use utf8; |
|
|
596 | |
613 | |
597 | our @ISA = DC::UI::Base::; |
614 | our @ISA = DC::UI::Base::; |
598 | |
615 | |
599 | use Time::HiRes qw(time); |
616 | use Time::HiRes qw(time); |
600 | use DC::OpenGL; |
617 | use DC::OpenGL; |
… | |
… | |
694 | glDisable GL_BLEND; |
711 | glDisable GL_BLEND; |
695 | } |
712 | } |
696 | |
713 | |
697 | package DC::MapWidget::Command; |
714 | package DC::MapWidget::Command; |
698 | |
715 | |
699 | use strict; |
716 | use common::sense; |
700 | |
717 | |
701 | use DC::OpenGL; |
718 | use DC::OpenGL; |
702 | |
719 | |
703 | our @ISA = DC::UI::Frame::; |
720 | our @ISA = DC::UI::Frame::; |
704 | |
721 | |
… | |
… | |
846 | my $text = $self->{entry}->get_text; |
863 | my $text = $self->{entry}->get_text; |
847 | |
864 | |
848 | length $text |
865 | length $text |
849 | or return $self->hide; |
866 | or return $self->hide; |
850 | |
867 | |
851 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
852 | |
|
|
853 | if ($text ne $self->{last_search}) { |
868 | if ($text ne $self->{last_search}) { |
854 | my @match; |
869 | my @match; |
855 | |
870 | |
856 | if ($text =~ /^(.*?)\s+$/) { |
871 | if ($text =~ /^(.*?)\s+$/) { |
|
|
872 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
857 | @match = [$cmd, "(appended whitespace suppresses completion)"]; |
873 | @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]); |
858 | } else { |
874 | } else { |
|
|
875 | # @match is [command, penalty, command with arguments] until sort |
|
|
876 | |
|
|
877 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
878 | |
859 | my $regexp = do { |
879 | my $regexp_abbrev = do { |
860 | my ($beg, @chr) = split //, lc $cmd; |
880 | my ($beg, @chr) = split //, lc $cmd; |
861 | |
881 | |
862 | # the following regex is used to match our "completion entry" |
882 | # the following regex is used to match our "completion entry" |
863 | # to an actual command - the parentheses match kind of "overhead" |
883 | # to an actual command - the parentheses match kind of "overhead" |
864 | # - the more characters the parentheses match, the less attractive |
884 | # - the more characters the parentheses match, the less attractive |
… | |
… | |
866 | my $regexp = "^\Q$beg\E" |
886 | my $regexp = "^\Q$beg\E" |
867 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
887 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
868 | qr<$regexp> |
888 | qr<$regexp> |
869 | }; |
889 | }; |
870 | |
890 | |
871 | my @penalty; |
891 | my $regexp_partial = do { |
|
|
892 | my $regexp = "^\Q$text\E(.*)"; |
|
|
893 | qr<$regexp> |
|
|
894 | }; |
872 | |
895 | |
873 | for (keys %{$self->{command}}) { |
896 | for (keys %{$self->{command}}) { |
874 | if (@penalty = $_ =~ $regexp) { |
897 | my @scores; |
875 | 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]; |
876 | } |
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]; |
877 | } |
932 | } |
878 | |
933 | |
|
|
934 | # @match is now [command object, command with arguments] |
879 | @match = map $self->{command}{$_->[0]}, |
935 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
880 | sort { |
936 | sort { |
881 | $a->[1] <=> $b->[1] |
937 | $b->[1] <=> $a->[1] |
882 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
938 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
883 | or (length $b->[0]) <=> (length $a->[0]) |
939 | or (length $b->[0]) <=> (length $a->[0]) |
884 | } @match; |
940 | } @match; |
885 | } |
941 | } |
886 | |
942 | |
… | |
… | |
905 | $label->{fg} = [1, 1, 1, 1]; |
961 | $label->{fg} = [1, 1, 1, 1]; |
906 | $label->{bg} = [0, 0, 0, 0]; |
962 | $label->{bg} = [0, 0, 0, 0]; |
907 | } |
963 | } |
908 | |
964 | |
909 | if (@matches) { |
965 | if (@matches) { |
910 | $self->{select} = "$matches[0][0]$arg"; |
966 | $self->{select} = "$matches[0][1]"; |
911 | |
967 | |
912 | $labels[0]->{fg} = [0, 0, 0, 1]; |
968 | $labels[0]->{fg} = [0, 0, 0, 1]; |
913 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
969 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
914 | } else { |
970 | } else { |
915 | $self->{select} = "$cmd$arg"; |
971 | $self->{select} = "$text"; |
916 | } |
972 | } |
917 | |
973 | |
918 | for my $match (@matches) { |
974 | for my $match (@matches) { |
919 | my $label = shift @labels; |
975 | my $label = shift @labels; |
920 | |
976 | |
921 | if (@labels) { |
977 | if (@labels) { |
922 | $label->set_text ("$match->[0]$arg"); |
978 | $label->set_text ("$match->[1]"); |
923 | $label->set_tooltip ($match->[1]); |
979 | $label->set_tooltip ("$match->[0][1]"); |
924 | } else { |
980 | } else { |
925 | $label->set_text ("..."); |
981 | $label->set_text ("..."); |
926 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
982 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
927 | last; |
983 | last; |
928 | } |
984 | } |