ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.157
Committed: Mon Dec 21 03:30:23 2009 UTC (14 years, 5 months ago) by root
Branch: MAIN
Changes since 1.156: +19 -26 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package DC::MapWidget;
2
3 use strict;
4 use utf8;
5
6 use List::Util qw(min max);
7
8 use DC;
9 use DC::OpenGL;
10 use DC::UI;
11 use DC::Macro;
12
13 our @ISA = DC::UI::Base::;
14
15 my $magicmap_tex =
16 new_from_resource DC::Texture "magicmap.png",
17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
18
19 sub new {
20 my $class = shift;
21
22 my $self = $class->SUPER::new (
23 z => -1,
24 can_focus => 1,
25 tilesize => 32,
26 @_
27 );
28
29 $self
30 }
31
32 sub 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
44 sub clr_commands {
45 my ($self) = @_;
46
47 %{$::COMPLETER->{command}} = ();
48
49 $::COMPLETER->hide
50 if $::COMPLETER;
51 }
52
53 sub server_login {
54 my ($server) = @_;
55
56 ::stop_game ();
57 local $::PROFILE->{host} = $server;
58 ::start_game ();
59 }
60
61 sub editor_invoke {
62 my $editsup = $::CONN && $::CONN->{editor_support}
63 or return;
64
65 DC::background {
66 print "preparing editor startup...\n";
67
68 my $server = $editsup->{gameserver} || "default";
69 $server =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
70
71 local $ENV{CROSSFIRE_MAPDIR} = my $mapdir = "$Deliantra::VARDIR/map.$server"; mkdir $mapdir;
72 local $ENV{CROSSFIRE_LIBDIR} = my $libdir = "$Deliantra::VARDIR/lib.$server"; mkdir $libdir;
73
74 print "map directory is $mapdir\n";
75 print "lib directory is $libdir\n";
76
77 my $ua = DC::lwp_useragent;
78
79 for my $file (qw(archetypes crossfire.0)) {
80 my $url = "$editsup->{lib_root}$file";
81 print "mirroring $url...\n";
82 DC::lwp_check $ua->mirror ($url, "$libdir/$file");
83 printf "%s size %d octets\n", $file, -s "$libdir/$file";
84 }
85
86 if (1) { # upload a map
87 my $mapname = $::CONN->{map_info}[0];
88
89 my $mappath = "$mapdir/$mapname";
90
91 -e $mappath and die "$mappath already exists\n";
92
93 print "getting map revision for $mapname...\n";
94
95 # try to get the most recent head revision, what a hack,
96 # this should have been returned while downloading *sigh*
97 my $log = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?view=log&logsort=rev"))->decoded_content;
98
99 if ($log =~ /\?rev=(\d+\.\d+)"/) {
100 my $rev = $1;
101
102 print "downloading revision $rev...\n";
103
104 my $map = (DC::lwp_check $ua->get ("$editsup->{cvs_root}/$mapname?rev=$rev"))->decoded_content;
105
106 my $meta = {
107 %$editsup,
108 path => $mapname,
109 revision => $rev,
110 cf_login => $::PROFILE->{user},
111 };
112
113 require File::Basename;
114 require File::Path;
115
116 File::Path::mkpath (File::Basename::dirname ($mappath));
117 open my $fh, ">:raw:perlio", "$mappath.meta"
118 or die "$mappath.meta: $!\n";
119 print $fh DC::encode_json $meta;
120 close $fh;
121 open my $fh, ">:raw:perlio:utf8", $mappath
122 or die "$mappath: $!\n";
123 print $fh $map;
124 close $fh;
125
126 print "saved as $mappath\n";
127
128 print "invoking editor...\n";
129 exec "/root/s2/gce $mappath";#d#
130
131 # now upload it
132 # require HTTP::Request::Common;
133 #
134 # my $res = $ua->post (
135 # $ENV{CFPLUS_UPLOAD},
136 # Content_Type => 'multipart/form-data',
137 # Content => [
138 # path => $mapname,
139 # mapdir => $ENV{CROSSFIRE_MAPDIR},
140 # map => $map,
141 # revision => $rev,
142 # cf_login => $ENV{CFPLUS_LOGIN},
143 # cf_password => $ENV{CFPLUS_PASSWORD},
144 # comment => "",
145 # ]
146 # );
147 #
148 # if ($res->is_error) {
149 # # fatal condition
150 # warn $res->status_line;
151 # } else {
152 # # script replies are marked as {{..}}
153 # my @msgs = $res->decoded_content =~ m/\{\{(.*?)\}\}/g;
154 # warn map "$_\n", @msgs;
155 # }
156 } else {
157 die "viewvc parse error, unable to detect revision\n";
158 }
159 }
160 }
161 }
162
163 sub invoke_button_down {
164 my ($self, $ev, $x, $y) = @_;
165
166 if ($ev->{button} == 1) {
167 $self->grab_focus;
168 return unless $::CONN && $self->{ctilesize};
169
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};
172
173 $x -= DC::floor $::MAP->w * 0.5;
174 $y -= DC::floor $::MAP->h * 0.5;
175
176 if ($::CONN) {
177 $::CONN->lookat ($x, $y)
178 }
179
180 } elsif ($ev->{button} == 2) {
181 $self->grab_focus;
182 return unless $::CONN;
183
184 my ($ox, $oy) = ($ev->{x}, $ev->{y});
185 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
186
187 $self->{motion} = sub {
188 my ($ev, $x, $y) = @_;
189
190 ($x, $y) = ($ev->{x}, $ev->{y});
191
192 $::CFG->{map_shift_x} = $bw + $x - $ox;
193 $::CFG->{map_shift_y} = $bh + $y - $oy;
194
195 $self->update;
196 };
197 } elsif ($ev->{button} == 3) {
198 my @items = (
199 ["Help Browser…\tF1", sub { $::HELP_WINDOW->toggle_visibility }],
200 ["Statistics\tF2", sub { ::toggle_player_page ($::STATS_PAGE) }],
201 ["Skills\tF3", sub { ::toggle_player_page ($::SKILL_PAGE) }],
202 ["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
203 ["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
204 ["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
205 # ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
206 );
207
208 if ($::CONN && $::CONN->{editor_support}) {
209 # push @items, [
210 # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>",
211 # \&editor_invoke,
212 # ];
213
214 for my $type (qw(test name)) {
215 $::CONN->{editor_support}{type} ne $type
216 or next;
217 my $server = $::CONN->{editor_support}{"${type}server"}
218 or next;
219
220 push @items, [
221 "Login on $type server <span size='xx-small'>(" . (DC::asxml $server) . ")</span>",
222 sub { server_login $server },
223 ];
224 }
225 }
226
227 push @items,
228 ["Quit",
229 sub {
230 if ($::CONN) {
231 &::open_quit_dialog;
232 } else {
233 exit;
234 }
235 }
236 ],
237 ;
238
239 (new DC::UI::Menu
240 items => \@items,
241 )->popup ($ev);
242 }
243
244 1
245 }
246
247 sub invoke_button_up {
248 my ($self, $ev, $x, $y) = @_;
249
250 delete $self->{motion};
251
252 1
253 }
254
255 sub invoke_mouse_motion {
256 my ($self, $ev, $x, $y) = @_;
257
258 if ($self->{motion}) {
259 $self->{motion}->($ev, $x, $y);
260 } else {
261 return 0;
262 }
263
264 1
265 }
266
267 sub size_request {
268 my ($self) = @_;
269
270 (
271 $self->{tilesize} * DC::ceil $::WIDTH / $self->{tilesize},
272 $self->{tilesize} * DC::ceil $::HEIGHT / $self->{tilesize},
273 )
274 }
275
276 sub update {
277 my ($self) = @_;
278
279 $self->{need_update} = 1;
280 $self->SUPER::update;
281 }
282
283 my %DIR = (
284 ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
285 ( "," . DC::SDLK_KP8 ), [1, "north"],
286 ( "," . DC::SDLK_KP9 ), [2, "northeast"],
287 ( "," . DC::SDLK_KP6 ), [3, "east"],
288 ( "," . DC::SDLK_KP3 ), [4, "southeast"],
289 ( "," . DC::SDLK_KP2 ), [5, "south"],
290 ( "," . DC::SDLK_KP1 ), [6, "southwest"],
291 ( "," . DC::SDLK_KP4 ), [7, "west"],
292 ( "," . DC::SDLK_KP7 ), [8, "northwest"],
293
294 ( "," . DC::SDLK_PAGEUP ), [2, "northeast"],
295 ( "," . DC::SDLK_PAGEDOWN ), [4, "southeast"],
296 ( "," . DC::SDLK_END ), [6, "southwest"],
297 ( "," . DC::SDLK_HOME ), [8, "northwest"],
298
299 ( "," . DC::SDLK_UP ), [1, "north"],
300 ("1," . DC::SDLK_UP ), [2, "northeast"],
301 ( "," . DC::SDLK_RIGHT ), [3, "east"],
302 ("1," . DC::SDLK_RIGHT ), [4, "southeast"],
303 ( "," . DC::SDLK_DOWN ), [5, "south"],
304 ("1," . DC::SDLK_DOWN ), [6, "southwest"],
305 ( "," . DC::SDLK_LEFT ), [7, "west"],
306 ("1," . DC::SDLK_LEFT ), [8, "northwest"],
307 );
308
309 sub invoke_key_down {
310 my ($self, $ev) = @_;
311
312 my $mod = $ev->{mod};
313 my $sym = $ev->{sym};
314 my $uni = $ev->{unicode};
315
316 $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT;
317
318 # ignore repeated keypresses
319 return if $self->{last_mod} == $mod && $self->{last_sym} == $sym;
320 $self->{last_mod} = $mod;
321 $self->{last_sym} = $sym;
322
323 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
324
325 if ($::CONN && $dir) {
326 if ($mod & DC::KMOD_SHIFT) {
327 $self->{shft}++;
328 if ($dir->[0] != $self->{fire_dir}) {
329 $::CONN->user_send ("fire $dir->[0]");
330 }
331 $self->{fire_dir} = $dir->[0];
332 } elsif ($mod & DC::KMOD_CTRL) {
333 $self->{ctrl}++;
334 $::CONN->user_send ("run $dir->[0]");
335 } else {
336 $::CONN->user_send ("$dir->[1]");
337 }
338
339 return 1;
340 }
341
342 0
343 }
344
345 sub invoke_key_up {
346 my ($self, $ev) = @_;
347
348 my $res = 0;
349 my $mod = $ev->{mod};
350 my $sym = $ev->{sym};
351
352 delete $self->{last_mod};
353 delete $self->{last_sym};
354
355 if ($::CFG->{shift_fire_stop}) {
356 if (!($mod & DC::KMOD_SHIFT) && delete $self->{shft}) {
357 $::CONN->user_send ("fire_stop");
358 delete $self->{fire_dir};
359 $res = 1;
360 }
361 } else {
362 my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
363
364 if ($dir && delete $self->{shft}) {
365 $::CONN->user_send ("fire_stop");
366 delete $self->{fire_dir};
367 $res = 1;
368 } elsif (($sym == DC::SDLK_LSHIFT || $sym == DC::SDLK_RSHIFT)
369 && delete $self->{shft}) { # XXX: is RSHIFT ok?
370 $::CONN->user_send ("fire_stop");
371 delete $self->{fire_dir};
372 $res = 1;
373 }
374 }
375
376 if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) {
377 $::CONN->user_send ("run_stop");
378 $res = 1;
379 }
380
381 $res
382 }
383
384 sub invoke_visibility_change {
385 my ($self) = @_;
386
387 $self->refresh_hook;
388
389 0
390 }
391
392 sub set_tilesize {
393 my ($self, $tilesize) = @_;
394
395 $self->{tilesize} = $tilesize;
396 }
397
398 sub scroll {
399 my ($self, $dx, $dy) = @_;
400
401 $self->movement_update;
402
403 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
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);
415 }
416
417 sub set_magicmap {
418 my ($self, $w, $h, $x, $y, $data) = @_;
419
420 $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w;
421 $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h;
422
423 $self->{magicmap} = [$x, $y, $w, $h, $data];
424
425 $self->update;
426 }
427
428 sub movement_update {
429 my ($self) = @_;
430
431 if ($::CFG->{smooth_movement}) {
432 if ($self->{sdx} || $self->{sdy}) {
433 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH);
434 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
435
436 # the minimum time for a single tile movement
437 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
438
439 $spd *= $self->{tilesize};
440
441 # jump if "impossibly high" speed
442 if (
443 (max abs $self->{sdx}, abs $self->{sdy})
444 > $spd * $mintime * 2.1
445 ) {
446 #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d#
447 $self->{sdx} = $self->{sdy} = 0;
448 } else {
449 $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction
450
451 my $dx = $self->{sdx} < 0 ? -$spd : $spd;
452 my $dy = $self->{sdy} < 0 ? -$spd : $spd;
453
454 if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx }
455 if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy }
456 }
457
458 $self->update;
459 }
460 } else {
461 $self->{sdx} = $self->{sdy} = 0;
462 }
463
464 $self->{last_update} = EV::time;
465 }
466
467 sub refresh_hook {
468 my ($self) = @_;
469
470 if ($::MAP && $::CONN) {
471 if (delete $self->{need_update}) {
472 $self->movement_update;
473
474 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
475
476 my $sdx_t = DC::ceil $self->{sdx} / $tilesize;
477 my $sdy_t = DC::ceil $self->{sdy} / $tilesize;
478
479 # width/height of map, in tiles
480 my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize;
481 my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize;
482
483 # the map displacement, in tiles
484 my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t;
485 my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t;
486
487 # the upper left "visible" corner, in pixels
488 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
489 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
490
491 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
492 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
493
494 if ($::CFG->{fow_enable}) {
495 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
496
497 $self->{fow_texture} = new DC::Texture
498 w => $w,
499 h => $h,
500 data => $data,
501 internalformat => GL_ALPHA,
502 format => GL_ALPHA;
503 } else {
504 delete $self->{fow_texture};
505 }
506
507 glNewList ($self->{list} ||= glGenList);
508
509 glPushMatrix;
510 glTranslate $sx0, $sy0;
511 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
512 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
513
514 $::MAP->draw ($dx, $dy, $sw, $sh,
515 $self->{tilesize},
516 $::CONN->{player}{tag},
517 -$self->{sdx}, -$self->{sdy});
518
519 glScale $self->{tilesize}, $self->{tilesize};
520
521 if (my $tex = $self->{fow_texture}) {
522 if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d#
523 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
524 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
525
526 if ($d1 * $d2) {
527 DC::Texture::draw_fow_texture
528 $::CFG->{fow_intensity},
529 @{$self->{fow_texture}}{qw(name s t w h)},
530 (min 1, $d1 / $d2),
531 $self->{lmdx} - $dx - $self->{lfdx},
532 $self->{lmdy} - $dy - $self->{lfdy},
533 @{$self->{last_fow_texture}}{qw(name s t w h)};
534 } else {
535 delete $self->{last_fow_texture};
536 }
537 }
538
539 unless ($self->{last_fow_texture}) {
540 DC::Texture::draw_fow_texture
541 $::CFG->{fow_intensity},
542 @{$self->{fow_texture}}{qw(name s t w h)};
543 }
544 }
545
546 if ($self->{magicmap}) {
547 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
548
549 $x += $::MAP->ox + $self->{dx};
550 $y += $::MAP->oy + $self->{dy};
551
552 glTranslate - $x - 1, - $y - 1;
553 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
554 $::MAP->draw_magicmap ($w, $h, $data);
555 }
556
557 glPopMatrix;
558 glEndList;
559 }
560 } else {
561 glDeleteList delete $self->{list}
562 if $self->{list};
563 }
564 }
565
566 sub draw {
567 my ($self) = @_;
568
569 $self->{root}->on_post_alloc (prepare => sub { $self->refresh_hook });
570
571 return unless $self->{list};
572
573 my $focused = $DC::UI::FOCUS == $self
574 || $DC::UI::FOCUS == $::COMPLETER->{entry};
575
576 return
577 unless $focused || !$::FAST;
578
579 glCallList $self->{list};
580
581 # TNT2 emulates logops in software (or worse :)
582 unless ($focused) {
583 glColor_premultiply 0, 0, 1, 0.25;
584 glEnable GL_BLEND;
585 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
586 glBegin GL_QUADS;
587 glVertex 0, 0;
588 glVertex 0, $::HEIGHT;
589 glVertex $::WIDTH, $::HEIGHT;
590 glVertex $::WIDTH, 0;
591 glEnd;
592 glDisable GL_BLEND;
593 }
594 }
595
596 sub DESTROY {
597 my $self = shift;
598
599 glDeleteList $self->{list};
600
601 $self->SUPER::DESTROY;
602 }
603
604 package DC::MapWidget::MapMap;
605
606 use strict;
607 use utf8;
608
609 our @ISA = DC::UI::Base::;
610
611 use Time::HiRes qw(time);
612 use DC::OpenGL;
613
614 sub size_request {
615 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
616 }
617
618 sub refresh_hook {
619 my ($self) = @_;
620
621 if ($::MAP && $self->{texture_atime} < time) {
622 my ($w, $h) = @$self{qw(w h)};
623
624 return unless $w && $h;
625
626 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
627 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
628
629 my $ox = 0.5 * ($w - $sw);
630 my $oy = 0.5 * ($h - $sh);
631
632 my $sx = int $::CFG->{map_shift_x} / $::MAPWIDGET->{tilesize};
633 my $sy = int $::CFG->{map_shift_y} / $::MAPWIDGET->{tilesize};
634
635 #TODO: map scale is completely borked
636
637 my $x0 = int $ox - $sx + 0.5;
638 my $y0 = int $oy - $sy + 0.5;
639
640 $self->{sw} = $sw;
641 $self->{sh} = $sh;
642
643 $self->{x0} = $x0;
644 $self->{y0} = $y0;
645
646 $self->{texture_atime} = time + 1/3;
647
648 $self->{texture} =
649 new DC::Texture
650 w => $w,
651 h => $h,
652 data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
653 type => $DC::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
654 }
655 }
656
657 sub invoke_visibility_change {
658 my ($self) = @_;
659
660 $self->refresh_hook;
661
662 0
663 }
664
665 sub invoke_size_allocate {
666 my ($self, $w, $h) = @_;
667
668 $self->update;
669
670 1
671 }
672
673 sub update {
674 my ($self) = @_;
675
676 delete $self->{texture_atime};
677 $self->SUPER::update;
678 }
679
680 sub _draw {
681 my ($self) = @_;
682
683 $self->{root}->on_post_alloc (texture => sub { $self->refresh_hook });
684
685 $self->{texture} or return;
686
687 glEnable GL_BLEND;
688 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
689 glEnable GL_TEXTURE_2D;
690 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
691
692 $self->{texture}->draw_quad (0, 0);
693
694 glDisable GL_TEXTURE_2D;
695
696 glTranslate 0.375, 0.375;
697
698 glColor 1, 1, 0, 1;
699 glBegin GL_LINE_LOOP;
700 glVertex $self->{x0} , $self->{y0} ;
701 glVertex $self->{x0} , $self->{y0} + $self->{sh};
702 glVertex $self->{x0} + $self->{sw}, $self->{y0} + $self->{sh};
703 glVertex $self->{x0} + $self->{sw}, $self->{y0} ;
704 glEnd;
705
706 glDisable GL_BLEND;
707 }
708
709 package DC::MapWidget::Command;
710
711 use strict;
712
713 use DC::OpenGL;
714
715 our @ISA = DC::UI::Frame::;
716
717 sub new {
718 my $class = shift;
719
720 my $self = $class->SUPER::new (
721 bg => [0, 0, 0, 0.8],
722 @_,
723 );
724
725 $self->add ($self->{vbox} = new DC::UI::VBox);
726
727 $self->{label} = [
728 map
729 DC::UI::Label->new (
730 align => 0,
731 can_hover => 1,
732 can_events => 1,
733 tooltip_width => 0.33,
734 fontsize => $_,
735 ), (0.8) x 16
736 ];
737
738 $self->{entry} = new DC::UI::Entry
739 on_changed => sub {
740 $self->update_labels;
741 0
742 },
743 on_button_down => sub {
744 my ($entry, $ev, $x, $y) = @_;
745
746 if ($ev->{button} == 3) {
747 (new DC::UI::Menu
748 items => [
749 ["bind <i>" . (DC::asxml $self->{select}) . "</i> to a key"
750 => sub { DC::Macro::quick_macro [$self->{select}], sub { $entry->grab_focus } }]
751 ],
752 )->popup ($ev);
753 return 1;
754 }
755 0
756 },
757 on_key_down => sub {
758 my ($entry, $ev) = @_;
759
760 my $self = $entry->{parent}{parent};
761
762 if ($ev->{sym} == 13) {
763 if (exists $self->{select}) {
764 $self->{last_command} = $self->{select};
765 $::CONN->user_send ($self->{select});
766
767 unshift @{$self->{history}}, $self->{entry}->get_text;
768 $self->{hist_ptr} = 0;
769
770 $self->hide;
771 }
772 } elsif ($ev->{sym} == 27) {
773 $self->{hist_ptr} = 0;
774 $self->hide;
775 } elsif ($ev->{sym} == DC::SDLK_DOWN) {
776 if ($self->{hist_ptr} > 1) {
777 $self->{hist_ptr}--;
778 $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1]);
779 } elsif ($self->{hist_ptr} > 0) {
780 $self->{hist_ptr}--;
781 $self->{entry}->set_text ($self->{hist_saveback});
782 } else {
783 ++$self->{select_offset}
784 if $self->{select_offset} < $#{ $self->{last_match} || [] };
785 }
786 $self->update_labels;
787 } elsif ($ev->{sym} == DC::SDLK_UP) {
788 if ($self->{select_offset}) {
789 --$self->{select_offset}
790 } else {
791 unless ($self->{hist_ptr}) {
792 $self->{hist_saveback} = $self->{entry}->get_text;
793 }
794 if ($self->{hist_ptr} <= $#{$self->{history}}) {
795 $self->{hist_ptr}++;
796 }
797 $self->{entry}->set_text ($self->{history}->[$self->{hist_ptr} - 1])
798 if exists $self->{history}->[$self->{hist_ptr} - 1];
799 }
800 $self->update_labels;
801 } else {
802 return 0;
803 }
804
805 1
806 }
807 ;
808
809 $self->{vbox}->add (
810 $self->{entry},
811 @{$self->{label}},
812 );
813
814 $self
815 }
816
817 sub set_prefix {
818 my ($self, $prefix) = @_;
819
820 $self->{entry}->set_text ($prefix);
821 $self->show;
822 }
823
824 sub invoke_size_allocate {
825 my ($self, $w, $h) = @_;
826
827 $self->move_abs (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
828
829 $self->SUPER::invoke_size_allocate ($w, $h)
830 }
831
832 sub show {
833 my ($self) = @_;
834
835 $self->SUPER::show;
836 $self->{entry}->grab_focus;
837 }
838
839 sub hide {
840 my ($self) = @_;
841
842 $self->{hist_ptr} = 0;
843
844 $self->SUPER::hide;
845 $self->{entry}->set_text ("");
846 }
847
848 sub inject_key_down {
849 my ($self, $ev) = @_;
850
851 $self->{entry}->grab_focus;
852 $self->{entry}->emit (key_down => $ev);
853 }
854
855 sub update_labels {
856 my ($self) = @_;
857
858 my $text = $self->{entry}->get_text;
859
860 length $text
861 or return $self->hide;
862
863 if ($text ne $self->{last_search}) {
864 my @match;
865
866 if ($text =~ /^(.*?)\s+$/) {
867 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
868 @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]);
869 } else {
870 # @match is [command, penalty, command with arguments] until sort
871
872 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
873
874 my $regexp_abbrev = do {
875 my ($beg, @chr) = split //, lc $cmd;
876
877 # the following regex is used to match our "completion entry"
878 # to an actual command - the parentheses match kind of "overhead"
879 # - the more characters the parentheses match, the less attractive
880 # is the match.
881 my $regexp = "^\Q$beg\E"
882 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
883 qr<$regexp>
884 };
885
886 my $regexp_partial = do {
887 my $regexp = "^\Q$text\E(.*)";
888 qr<$regexp>
889 };
890
891 for (keys %{$self->{command}}) {
892 my @scores;
893
894 # 1. Complete command [with args]
895 # command is a prefix of the text
896 # score is length of complete command matched
897 # e.g. "invoke summon pet monster bat"
898 # "invoke" "summon pet monster bat" = 6
899 # "invoke summon pet monster" "bat" = 25
900 if ($text =~ /^\Q$_\E(.*)/) {
901 push @scores, [$_, length $_, $text];
902 }
903
904 # 2. Partial command
905 # text is a prefix of the full command
906 # score is the length of the input text
907 # e.g. "invoke s"
908 # "invoke small fireball" = 8
909 # "invoke summon pet monster" = 8
910
911 if ($_ =~ $regexp_partial) {
912 push @scores, [$_, length $text, $_];
913 }
914
915 # 3. Abbreviation match
916 # attempts to use first word of text as an abbreviated command
917 # score is length of word + 1 - 3 per non-word-initial character
918
919 if (my @penalty = $_ =~ $regexp_abbrev) {
920 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
921 }
922
923 # Pick the best option for this command
924 push @match, (sort {
925 $b->[1] <=> $a->[1]
926 } @scores)[0];
927 }
928
929 # @match is now [command object, command with arguments]
930 @match = map [$self->{command}{$_->[0]}, $_->[2]],
931 sort {
932 $b->[1] <=> $a->[1]
933 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
934 or (length $b->[0]) <=> (length $a->[0])
935 } @match;
936 }
937
938 $self->{last_search} = $text;
939 $self->{last_match} = \@match;
940
941 $self->{select_offset} = 0;
942 }
943
944 my @labels = @{ $self->{label} };
945 my @matches = @{ $self->{last_match} || [] };
946
947 if ($self->{select_offset}) {
948 splice @matches, 0, $self->{select_offset}, ();
949
950 my $label = shift @labels;
951 $label->set_text ("...");
952 $label->set_tooltip ("Use Cursor-Up to view previous matches");
953 }
954
955 for my $label (@labels) {
956 $label->{fg} = [1, 1, 1, 1];
957 $label->{bg} = [0, 0, 0, 0];
958 }
959
960 if (@matches) {
961 $self->{select} = "$matches[0][1]";
962
963 $labels[0]->{fg} = [0, 0, 0, 1];
964 $labels[0]->{bg} = [1, 1, 1, 0.8];
965 } else {
966 $self->{select} = "$text";
967 }
968
969 for my $match (@matches) {
970 my $label = shift @labels;
971
972 if (@labels) {
973 $label->set_text ("$match->[1]");
974 $label->set_tooltip ("$match->[0][1]");
975 } else {
976 $label->set_text ("...");
977 $label->set_tooltip ("Use Cursor-Down to view more matches");
978 last;
979 }
980 }
981
982 for my $label (@labels) {
983 $label->set_text ("");
984 $label->set_tooltip ("");
985 }
986
987 $self->update;
988 }
989
990 sub _draw {
991 my ($self) = @_;
992
993 # hack
994 local $DC::UI::FOCUS = $self->{entry};
995
996 $self->SUPER::_draw;
997 }
998
999 1
1000