ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.47
Committed: Fri Jun 2 22:13:47 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.46: +23 -3 lines
Log Message:
add boolean results to callbacks

File Contents

# Content
1 package CFClient::MapWidget;
2
3 use strict;
4 use utf8;
5
6 use List::Util qw(min max);
7
8 use CFClient::OpenGL;
9
10 our @ISA = CFClient::UI::Base::;
11
12 sub new {
13 my $class = shift;
14
15 my $self = $class->SUPER::new (
16 z => -1,
17 can_focus => 1,
18 list => glGenList,
19 @_
20 );
21
22 $self->{completer} = new CFClient::MapWidget::Command::
23 command => $self->{command},
24 can_focus => 1,
25 tooltip => "<b>The Command Completer</b>\n\n"
26 . "This is your central interface to send text commands to the server. "
27 . "To enter a verbatim command to send to the server, just type the command, "
28 . "followed by a space, and press return. "
29 . "Typing the initial letters of words (or just any letters) displays guesses "
30 . "for commands you might want to use.\n"
31 . "You can use the cursor-up and cursor-down keys to select between those guesses.\n"
32 . "<b>Right-Click</b> opens a menu where you cna select further options, sich as redefining keybindings.",
33 ;
34
35 $self
36 }
37
38 sub add_command {
39 my ($self, $command, $tooltip, $widget, $cb) = @_;
40
41 (my $data = $command) =~ s/\\//g;
42
43 $tooltip =~ s/^\s+//;
44 $tooltip = "<big>$data</big>\n\n$tooltip";
45 $tooltip =~ s/\s+$//;
46
47 $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
48 }
49
50 sub clr_commands {
51 my ($self) = @_;
52
53 %{$self->{completer}{command}} = ();
54 }
55
56 sub button_down {
57 my ($self, $ev, $x, $y) = @_;
58
59 $self->focus_in;
60
61 if ($ev->{button} == 2) {
62 my ($ox, $oy) = ($ev->{x}, $ev->{y});
63 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
64
65 $self->{motion} = sub {
66 my ($ev, $x, $y) = @_;
67
68 ($x, $y) = ($ev->{x}, $ev->{y});
69
70 $::CFG->{map_shift_x} = $bw + $x - $ox;
71 $::CFG->{map_shift_y} = $bh + $y - $oy;
72
73 $self->update;
74 };
75 }
76
77 1
78 }
79
80 sub button_up {
81 my ($self, $ev, $x, $y) = @_;
82
83 delete $self->{motion};
84
85 1
86 }
87
88 sub mouse_motion {
89 my ($self, $ev, $x, $y) = @_;
90
91 if ($self->{motion}) {
92 $self->{motion}->($ev, $x, $y);
93 } else {
94 return 0;
95 }
96
97 1
98 }
99
100 sub size_request {
101 (
102 1 + 32 * int $::WIDTH / 32,
103 1 + 32 * int $::HEIGHT / 32,
104 )
105 }
106
107 sub update {
108 my ($self) = @_;
109
110 $self->{need_update} = 1;
111 $self->SUPER::update;
112 }
113
114 my %DIR = (
115 CFClient::SDLK_KP8, [1, "north"],
116 CFClient::SDLK_KP9, [2, "northeast"],
117 CFClient::SDLK_KP6, [3, "east"],
118 CFClient::SDLK_KP3, [4, "southeast"],
119 CFClient::SDLK_KP2, [5, "south"],
120 CFClient::SDLK_KP1, [6, "southwest"],
121 CFClient::SDLK_KP4, [7, "west"],
122 CFClient::SDLK_KP7, [8, "northwest"],
123
124 CFClient::SDLK_UP, [1, "north"],
125 CFClient::SDLK_RIGHT, [3, "east"],
126 CFClient::SDLK_DOWN, [5, "south"],
127 CFClient::SDLK_LEFT, [7, "west"],
128 );
129
130 sub key_down {
131 my ($self, $ev) = @_;
132
133 return 0 unless $::CONN;
134
135 my $mod = $ev->{mod};
136 my $sym = $ev->{sym};
137 my $uni = $ev->{unicode};
138
139 if ($sym == CFClient::SDLK_KP5) {
140 $::CONN->user_send ("stay fire");
141 } elsif ($uni == ord ",") {
142 $::CONN->user_send ("take");
143 } elsif ($uni == ord " ") {
144 $::CONN->user_send ("apply");
145 } elsif ($uni == ord ".") {
146 $::CONN->user_send ($self->{completer}{last_command})
147 if exists $self->{completer}{last_command};
148 } elsif ($uni == ord "\t") {
149 $::INV_WINDOW->toggle_visibility;
150 } elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") {
151 $::CONN->user_send ("rotateshoottype +");
152 } elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") {
153 $::CONN->user_send ("rotateshoottype -");
154 } elsif ($uni == ord '"') {
155 $self->{completer}->set_prefix ("$::CFG->{say_command} ");
156 $self->{completer}->show;
157 } elsif ($uni == ord "'") {
158 $self->{completer}->set_prefix ("");
159 $self->{completer}->show;
160 } elsif (exists $DIR{$sym}) {
161 if ($mod & CFClient::KMOD_SHIFT) {
162 $self->{shft}++;
163 $::CONN->user_send ("fire $DIR{$sym}[0]");
164 } elsif ($mod & CFClient::KMOD_CTRL) {
165 $self->{ctrl}++;
166 $::CONN->user_send ("run $DIR{$sym}[0]");
167 } else {
168 $::CONN->user_send ("$DIR{$sym}[1]");
169 }
170 } elsif ($sym == CFClient::SDLK_INSERT && $mod & CFClient::KMOD_CTRL) {
171 $::BIND_EDITOR->set_binding (undef, undef, [],
172 sub {
173 my ($mod, $sym, $cmds) = @_;
174 $::CFG->{bindings}->{$mod}->{$sym} = $cmds;
175 });
176 $::BIND_EDITOR->start;
177 $::BIND_EDITOR->show;
178 } elsif ($sym == CFClient::SDLK_INSERT && not ($mod & CFClient::KMOD_CTRL)) {
179 $::BIND_EDITOR->stop;
180 $::BIND_EDITOR->ask_for_bind_and_commit;
181 $::BIND_EDITOR->hide;
182 } elsif (my $bind_cmd = $::CFG->{bindings}->{$mod}->{$sym}) {
183 $::CONN->user_send ($_) for @$bind_cmd;
184 } elsif ((ord 'a') <= $uni && $uni <= (ord 'z')) {
185 $self->{completer}->key_down ($ev);
186 $self->{completer}->show;
187 } else {
188 return 0;
189 }
190
191 1
192 }
193
194 sub key_up {
195 my ($self, $ev) = @_;
196
197 my $res = 0;
198 my $mod = $ev->{mod};
199 my $sym = $ev->{sym};
200
201 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
202 $::CONN->user_send ("fire_stop");
203 $res = 1;
204 }
205
206 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
207 $::CONN->user_send ("run_stop");
208 $res = 1;
209 }
210
211 $res
212 }
213
214 sub draw {
215 my ($self) = @_;
216
217 my $focused = $CFClient::UI::FOCUS == $self
218 || $CFClient::UI::FOCUS == $self->{completer}{entry};
219
220 return
221 unless $focused || !$::FAST;
222
223 if (delete $self->{need_update}) {
224 glNewList $self->{list};
225
226 if ($::MAP) {
227 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
228 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
229
230 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
231 my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
232
233 glPushMatrix;
234 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
235
236 glTranslate $sx0 - 32, $sy0 - 32, 0;
237
238 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
239
240 if ($::CFG->{fow_enable}) {
241 if ($::CFG->{fow_smooth} && $CFClient::OpenGL::GL_VERSION >= 1.2) { # smooth fog of war
242 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
243 glConvolutionFilter2D (
244 GL_CONVOLUTION_2D,
245 GL_ALPHA,
246 3, 3,
247 GL_ALPHA, GL_FLOAT,
248 pack "f*",
249 0.05, 0.13, 0.05,
250 0.13, 0.30, 0.13,
251 0.05, 0.13, 0.05,
252 );
253 glEnable GL_CONVOLUTION_2D;
254 }
255
256 $self->{fow_texture_name} ||= glGenTexture;
257 # try to re-use the texture name: TODO improve texture class instead
258
259 $self->{fow_texture} = new CFClient::Texture
260 w => $w,
261 h => $h,
262 data => $data,
263 name => $self->{fow_texture_name},
264 internalformat => GL_ALPHA,
265 format => GL_ALPHA;
266
267 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
268
269 glEnable GL_TEXTURE_2D;
270 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
271
272 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
273 $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32);
274
275 glDisable GL_TEXTURE_2D;
276 }
277
278 glPopMatrix;
279 }
280
281 glEndList;
282 }
283
284 glPushMatrix;
285 glCallList $self->{list};
286 glPopMatrix;
287
288 # TNT2 emulates logops in software (or worse :)
289 if ($focused) {
290 (delete $self->{out_of_focus})->destroy
291 if $self->{out_of_focus};
292 } else {
293 glColor 0.4, 0.2, 0.2, 0.6;
294 glEnable GL_BLEND;
295 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
296 glBegin GL_QUADS;
297 glVertex 0, 0;
298 glVertex 0, $::HEIGHT;
299 glVertex $::WIDTH, $::HEIGHT;
300 glVertex $::WIDTH, 0;
301 glEnd;
302 glDisable GL_BLEND;
303
304 # $self->{out_of_focus} ||= do {
305 # my $label = new CFClient::UI::Label
306 # x => 0,
307 # y => 0,
308 # z => 1,
309 # ellipsise => 0,
310 # text => "map out of focus (click map to play)";
311 #
312 # $label->show;
313 # $label->update;
314 #
315 # $CFClient::UI::ROOT->on_post_alloc ("$self$label" => sub {
316 # $label->move_abs (
317 # ($::WIDTH - $label->{w}) * 0.5,
318 # ($::HEIGHT - $label->{h}) * 0.5,
319 # );
320 # });
321 #
322 # $label
323 # };
324 }
325 }
326
327 sub DESTROY {
328 my $self = shift;
329
330 glDeleteList $self->{list};
331
332 $self->SUPER::DESTROY;
333 }
334
335 package CFClient::MapWidget::MapMap;
336
337 our @ISA = CFClient::UI::Base::;
338
339 use Time::HiRes qw(time);
340 use CFClient::OpenGL;
341
342 sub size_request {
343 ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
344 }
345
346 sub size_allocate {
347 my ($self, $w, $h) = @_;
348
349 $self->SUPER::size_allocate ($w, $h);
350 $self->update;
351 }
352
353 sub update {
354 my ($self) = @_;
355
356 delete $self->{texture_atime};
357 $self->SUPER::update;
358 }
359
360 sub _draw {
361 my ($self) = @_;
362
363 $::MAP or return;
364
365 my ($w, $h) = @$self{qw(w h)};
366
367 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
368 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
369
370 my $sx = int $::CFG->{map_shift_x} / 32;
371 my $sy = int $::CFG->{map_shift_y} / 32;
372
373 my $ox = 0.5 * ($w - $sw);
374 my $oy = 0.5 * ($h - $sh);
375
376 glEnable GL_BLEND;
377 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
378 glEnable GL_TEXTURE_2D;
379 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
380
381 if ($self->{texture_atime} < time) {
382 $self->{texture_atime} = time + 1/3;
383
384 $self->{texture} =
385 new CFClient::Texture
386 w => $w,
387 h => $h,
388 data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
389 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
390 }
391
392 $self->{texture}->draw_quad (0, 0);
393
394 glDisable GL_TEXTURE_2D;
395
396 glTranslate 0.375, 0.375;
397
398 #TODO: map scale is completely borked
399
400 my $x0 = int $ox - $sx + 0.5;
401 my $y0 = int $oy - $sy + 0.5;
402
403 glColor 1, 1, 0, 1;
404 glBegin GL_LINE_LOOP;
405 glVertex $x0 , $y0 ;
406 glVertex $x0 , $y0 + $sh;
407 glVertex $x0 + $sw, $y0 + $sh;
408 glVertex $x0 + $sw, $y0 ;
409 glEnd;
410
411 glDisable GL_BLEND;
412 }
413
414 package CFClient::MapWidget::Command;
415
416 use strict;
417
418 use CFClient::OpenGL;
419
420 our @ISA = CFClient::UI::Frame::;
421
422 sub new {
423 my $class = shift;
424
425 my $self = $class->SUPER::new (
426 bg => [0, 0, 0, 0.8],
427 @_,
428 );
429
430 $self->add ($self->{vbox} = new CFClient::UI::VBox);
431
432 $self->{label} = [
433 map
434 CFClient::UI::Label->new (
435 can_hover => 1,
436 can_events => 1,
437 tooltip_width => 0.33,
438 fontsize => $_,
439 ), (0.8) x 16
440 ];
441
442 $self->{entry} = new CFClient::UI::Entry
443 on_changed => sub {
444 $self->update_labels;
445 },
446 on_key_down => sub {
447 my ($entry, $ev) = @_;
448
449 my $self = $entry->{parent}{parent};
450
451 if ($ev->{sym} == 13) {
452 if (exists $self->{select}) {
453 $self->{last_command} = $self->{select};
454 $::CONN->user_send ($self->{select});
455 $self->hide;
456 }
457 } elsif ($ev->{sym} == 27) {
458 $self->hide;
459 return;
460 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
461 ++$self->{select_offset}
462 if $self->{select_offset} < $#{ $self->{last_match} || [] };
463 $self->update_labels;
464 } elsif ($ev->{sym} == CFClient::SDLK_UP) {
465 --$self->{select_offset}
466 if $self->{select_offset};
467 $self->update_labels;
468 } else {
469 return 0;
470 }
471
472 1
473 }
474 ;
475
476 $self->{vbox}->add (
477 $self->{entry},
478 @{$self->{label}},
479 );
480
481 $self
482 }
483
484 sub set_prefix {
485 my ($self, $prefix) = @_;
486
487 $self->{entry}->set_text ($prefix);
488 $self->show;
489 }
490
491 sub size_allocate {
492 my ($self, $w, $h) = @_;
493
494 $self->SUPER::size_allocate ($w, $h);
495 $self->move_abs (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
496 }
497
498 sub show {
499 my ($self) = @_;
500
501 $self->SUPER::show;
502 $self->{entry}->focus_in;
503 }
504
505 sub hide {
506 my ($self) = @_;
507
508 $self->SUPER::hide;
509 $self->{entry}->set_text ("");
510 }
511
512 sub key_down {
513 my ($self, $ev) = @_;
514
515 $self->{entry}->key_down ($ev)
516 }
517
518 sub update_labels {
519 my ($self) = @_;
520
521 my $text = $self->{entry}->get_text;
522
523 length $text
524 or return $self->hide;
525
526 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
527
528 if ($text ne $self->{last_search}) {
529 my @match;
530
531 if ($text =~ /^(.*?)\s+$/) {
532 @match = [$cmd, "(appended whitespace suppresses completion)"];
533 } else {
534 my $regexp = do {
535 my ($beg, @chr) = split //, lc $cmd;
536
537 # the following regex is used to match our "completion entry"
538 # to an actual command - the parentheses match kind of "overhead"
539 # - the more characters the parentheses match, the less attractive
540 # is the match.
541 my $regexp = "^\Q$beg\E"
542 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
543 qr<$regexp>
544 };
545
546 my @penalty;
547
548 for (keys %{$self->{command}}) {
549 if (@penalty = $_ =~ $regexp) {
550 push @match, [$_, length join "", map "::$_", grep defined, @penalty];
551 }
552 }
553
554 @match = map $self->{command}{$_->[0]},
555 sort {
556 $a->[1] <=> $b->[1]
557 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
558 or (length $b->[0]) <=> (length $a->[0])
559 } @match;
560 }
561
562 $self->{last_search} = $text;
563 $self->{last_match} = \@match;
564
565 $self->{select_offset} = 0;
566 }
567
568 my @labels = @{ $self->{label} };
569 my @matches = @{ $self->{last_match} || [] };
570
571 if ($self->{select_offset}) {
572 splice @matches, 0, $self->{select_offset}, ();
573
574 my $label = shift @labels;
575 $label->set_text ("...");
576 $label->set_tooltip ("Use Cursor-Up to view previous matches");
577 }
578
579 for my $label (@labels) {
580 $label->{fg} = [1, 1, 1, 1];
581 $label->{bg} = [0, 0, 0, 0];
582 }
583
584 if (@matches) {
585 $self->{select} = "$matches[0][0]$arg";
586
587 $labels[0]->{fg} = [0, 0, 0, 1];
588 $labels[0]->{bg} = [1, 1, 1, 0.8];
589 } else {
590 $self->{select} = "$cmd$arg";
591 }
592
593 for my $match (@matches) {
594 my $label = shift @labels;
595
596 if (@labels) {
597 $label->set_text ("$match->[0]$arg");
598 $label->set_tooltip ($match->[1]);
599 } else {
600 $label->set_text ("...");
601 $label->set_tooltip ("Use Cursor-Down to view more matches");
602 last;
603 }
604 }
605
606 for my $label (@labels) {
607 $label->set_text ("");
608 $label->set_tooltip ("");
609 }
610
611 $self->update;
612 ###
613 }
614
615 sub _draw {
616 my ($self) = @_;
617
618 # hack
619 local $CFClient::UI::FOCUS = $self->{entry};
620
621 $self->SUPER::_draw;
622 }
623
624 1