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 |
23 |
} |
24 |
|
25 |
sub DESTROY { |
26 |
my $self = shift; |
27 |
|
28 |
glDeleteList $self->{list}; |
29 |
|
30 |
$self->SUPER::DESTROY; |
31 |
} |
32 |
|
33 |
sub button_down { |
34 |
my ($self, $ev, $x, $y) = @_; |
35 |
|
36 |
$self->focus_in; |
37 |
|
38 |
if ($ev->{button} == 2) { |
39 |
my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
40 |
my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
41 |
|
42 |
$self->{motion} = sub { |
43 |
my ($ev, $x, $y) = @_; |
44 |
|
45 |
($x, $y) = ($ev->{x}, $ev->{y}); |
46 |
|
47 |
$::CFG->{map_shift_x} = $bw + $x - $ox; |
48 |
$::CFG->{map_shift_y} = $bh + $y - $oy; |
49 |
|
50 |
$self->update; |
51 |
}; |
52 |
} |
53 |
} |
54 |
|
55 |
sub button_up { |
56 |
my ($self, $ev, $x, $y) = @_; |
57 |
|
58 |
delete $self->{motion}; |
59 |
} |
60 |
|
61 |
sub mouse_motion { |
62 |
my ($self, $ev, $x, $y) = @_; |
63 |
|
64 |
$self->{motion}->($ev, $x, $y) if $self->{motion}; |
65 |
} |
66 |
|
67 |
sub size_request { |
68 |
( |
69 |
1 + 32 * int $::WIDTH / 32, |
70 |
1 + 32 * int $::HEIGHT / 32, |
71 |
) |
72 |
} |
73 |
|
74 |
sub update { |
75 |
my ($self) = @_; |
76 |
|
77 |
$self->{need_update} = 1; |
78 |
$self->SUPER::update; |
79 |
} |
80 |
|
81 |
sub draw { |
82 |
my ($self) = @_; |
83 |
|
84 |
return |
85 |
unless $CFClient::UI::FOCUS == $self || !$::FAST; |
86 |
|
87 |
if (delete $self->{need_update}) { |
88 |
glNewList $self->{list}; |
89 |
|
90 |
if ($::MAP) { |
91 |
my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
92 |
my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
93 |
|
94 |
my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
95 |
my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
96 |
|
97 |
glPushMatrix; |
98 |
glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
99 |
|
100 |
glTranslate $sx0 - 32, $sy0 - 32, 0; |
101 |
|
102 |
my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
103 |
|
104 |
if ($::CFG->{fow_enable}) { |
105 |
if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war |
106 |
glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); |
107 |
glConvolutionFilter2D ( |
108 |
GL_CONVOLUTION_2D, |
109 |
GL_ALPHA, |
110 |
3, 3, |
111 |
GL_ALPHA, GL_FLOAT, |
112 |
pack "f*", |
113 |
0.05, 0.13, 0.05, |
114 |
0.13, 0.30, 0.13, |
115 |
0.05, 0.13, 0.05, |
116 |
); |
117 |
glEnable GL_CONVOLUTION_2D; |
118 |
} |
119 |
|
120 |
# try to re-use the texture name: TODO improve texture class instead |
121 |
my $prev_name = ((delete $self->{fow_texture}) || {})->{name}; |
122 |
|
123 |
$self->{fow_texture} = new CFClient::Texture |
124 |
w => $w, |
125 |
h => $h, |
126 |
data => $data, |
127 |
name => $prev_name, # a bit hackish |
128 |
internalformat => GL_ALPHA, |
129 |
format => GL_ALPHA; |
130 |
|
131 |
glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
132 |
|
133 |
glEnable GL_TEXTURE_2D; |
134 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
135 |
|
136 |
glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
137 |
$self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32); |
138 |
|
139 |
glDisable GL_TEXTURE_2D; |
140 |
} |
141 |
|
142 |
glPopMatrix; |
143 |
} |
144 |
|
145 |
glEndList; |
146 |
} |
147 |
|
148 |
glPushMatrix; |
149 |
glCallList $self->{list}; |
150 |
glPopMatrix; |
151 |
|
152 |
# TNT2 emulates logops in software (or worse :) |
153 |
if ($CFClient::UI::FOCUS != $self) { |
154 |
glColor 0.4, 0.2, 0.2, 0.9; |
155 |
glEnable GL_BLEND; |
156 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
157 |
glBegin GL_QUADS; |
158 |
glVertex 0, 0; |
159 |
glVertex 0, $::HEIGHT; |
160 |
glVertex $::WIDTH, $::HEIGHT; |
161 |
glVertex $::WIDTH, 0; |
162 |
glEnd; |
163 |
glDisable GL_BLEND; |
164 |
} |
165 |
} |
166 |
|
167 |
my %DIR = ( |
168 |
CFClient::SDLK_KP8, [1, "north"], |
169 |
CFClient::SDLK_KP9, [2, "northeast"], |
170 |
CFClient::SDLK_KP6, [3, "east"], |
171 |
CFClient::SDLK_KP3, [4, "southeast"], |
172 |
CFClient::SDLK_KP2, [5, "south"], |
173 |
CFClient::SDLK_KP1, [6, "southwest"], |
174 |
CFClient::SDLK_KP4, [7, "west"], |
175 |
CFClient::SDLK_KP7, [8, "northwest"], |
176 |
|
177 |
CFClient::SDLK_UP, [1, "north"], |
178 |
CFClient::SDLK_RIGHT, [3, "east"], |
179 |
CFClient::SDLK_DOWN, [5, "south"], |
180 |
CFClient::SDLK_LEFT, [7, "west"], |
181 |
); |
182 |
|
183 |
sub key_down { |
184 |
my ($self, $ev) = @_; |
185 |
|
186 |
return unless $::CONN; |
187 |
|
188 |
my $mod = $ev->{mod}; |
189 |
my $sym = $ev->{sym}; |
190 |
my $uni = $ev->{unicode}; |
191 |
|
192 |
if ($sym == CFClient::SDLK_KP5) { |
193 |
$::CONN->user_send ("stay fire"); |
194 |
} elsif ($uni == ord ",") { |
195 |
$::CONN->user_send ("take"); |
196 |
} elsif ($uni == ord "\t" or $uni == ord " ") { |
197 |
$::CONN->user_send ("apply"); |
198 |
} elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") { |
199 |
$::CONN->user_send ("rotateshoottype +"); |
200 |
} elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") { |
201 |
$::CONN->user_send ("rotateshoottype -"); |
202 |
} elsif ($uni == ord "'") { |
203 |
$self->emit ('activate_console'); |
204 |
} elsif ($uni == ord "/") { |
205 |
$self->emit (activate_console => '/'); |
206 |
} elsif (exists $DIR{$sym}) { |
207 |
if ($mod & CFClient::KMOD_SHIFT) { |
208 |
$self->{shft}++; |
209 |
$::CONN->user_send ("fire $DIR{$sym}[0]"); |
210 |
} elsif ($mod & CFClient::KMOD_CTRL) { |
211 |
$self->{ctrl}++; |
212 |
$::CONN->user_send ("run $DIR{$sym}[0]"); |
213 |
} else { |
214 |
$::CONN->user_send ("$DIR{$sym}[1]"); |
215 |
} |
216 |
} elsif ($ev->{unicode}) { |
217 |
$self->{command_widget} ||= |
218 |
new CFClient::MapWidget::Command:: |
219 |
command => $self->{command}, |
220 |
can_focus => 1, |
221 |
connect_execute => sub { |
222 |
# todo: support callback instead of user_send |
223 |
$::CONN->user_send ($_[1]); |
224 |
}, |
225 |
connect_close => sub { |
226 |
(delete $self->{command_widget})->hide; |
227 |
$self->focus_in; |
228 |
}, |
229 |
; |
230 |
$self->{command_widget}->key_down ($ev) |
231 |
unless $ev->{unicode} == 20; |
232 |
return unless $self->{command_widget}; |
233 |
$self->{command_widget}->show; |
234 |
$self->{command_widget}->focus_in; |
235 |
} |
236 |
} |
237 |
|
238 |
sub key_up { |
239 |
my ($self, $ev) = @_; |
240 |
|
241 |
my $mod = $ev->{mod}; |
242 |
my $sym = $ev->{sym}; |
243 |
|
244 |
if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
245 |
$::CONN->user_send ("fire_stop"); |
246 |
} |
247 |
if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
248 |
$::CONN->user_send ("run_stop"); |
249 |
} |
250 |
} |
251 |
|
252 |
sub add_command { |
253 |
my ($self, $command, $tooltip, $widget, $cb) = @_; |
254 |
|
255 |
(my $data = $command) =~ s/\\//g; |
256 |
|
257 |
$tooltip =~ s/^\s+//; |
258 |
|
259 |
$tooltip = "<big>$data</big>\n\n$tooltip"; |
260 |
|
261 |
$tooltip =~ s/\s+$//; |
262 |
|
263 |
$self->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
264 |
} |
265 |
|
266 |
sub clr_commands { |
267 |
my ($self) = @_; |
268 |
|
269 |
%{$self->{command}} = (); |
270 |
} |
271 |
|
272 |
package CFClient::MapWidget::MapMap; |
273 |
|
274 |
our @ISA = CFClient::UI::Base::; |
275 |
|
276 |
use Time::HiRes qw(time); |
277 |
use CFClient::OpenGL; |
278 |
|
279 |
sub size_request { |
280 |
($::HEIGHT * 0.25, $::HEIGHT * 0.25) |
281 |
} |
282 |
|
283 |
sub size_allocate { |
284 |
my ($self, $w, $h) = @_; |
285 |
|
286 |
$self->SUPER::size_allocate ($w, $h); |
287 |
$self->update; |
288 |
} |
289 |
|
290 |
sub update { |
291 |
my ($self) = @_; |
292 |
|
293 |
delete $self->{texture_atime}; |
294 |
$self->SUPER::update; |
295 |
} |
296 |
|
297 |
sub _draw { |
298 |
my ($self) = @_; |
299 |
|
300 |
$::MAP or return; |
301 |
|
302 |
my ($w, $h) = @$self{qw(w h)}; |
303 |
|
304 |
my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
305 |
my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
306 |
|
307 |
my $sx = int $::CFG->{map_shift_x} / 32; |
308 |
my $sy = int $::CFG->{map_shift_y} / 32; |
309 |
|
310 |
my $ox = 0.5 * ($w - $sw); |
311 |
my $oy = 0.5 * ($h - $sh); |
312 |
|
313 |
glEnable GL_BLEND; |
314 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
315 |
glEnable GL_TEXTURE_2D; |
316 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
317 |
|
318 |
if ($self->{texture_atime} < time) { |
319 |
$self->{texture_atime} = time + 1/3; |
320 |
|
321 |
$self->{texture} = |
322 |
new CFClient::Texture |
323 |
w => $w, |
324 |
h => $h, |
325 |
data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
326 |
type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
327 |
} |
328 |
|
329 |
$self->{texture}->draw_quad (0, 0); |
330 |
|
331 |
glDisable GL_TEXTURE_2D; |
332 |
|
333 |
glTranslate 0.375, 0.375; |
334 |
|
335 |
#TODO: map scale is completely borked |
336 |
|
337 |
my $x0 = int $ox - $sx + 0.5; |
338 |
my $y0 = int $oy - $sy + 0.5; |
339 |
|
340 |
glColor 1, 1, 0, 1; |
341 |
glBegin GL_LINE_LOOP; |
342 |
glVertex $x0 , $y0 ; |
343 |
glVertex $x0 , $y0 + $sh; |
344 |
glVertex $x0 + $sw, $y0 + $sh; |
345 |
glVertex $x0 + $sw, $y0 ; |
346 |
glEnd; |
347 |
|
348 |
glDisable GL_BLEND; |
349 |
} |
350 |
|
351 |
package CFClient::MapWidget::Command; |
352 |
|
353 |
use strict; |
354 |
|
355 |
use CFClient::OpenGL; |
356 |
|
357 |
our @ISA = CFClient::UI::Frame::; |
358 |
|
359 |
sub new { |
360 |
my $class = shift; |
361 |
|
362 |
my $self = $class->SUPER::new ( |
363 |
bg => [0, 0, 0, 0.8], |
364 |
@_, |
365 |
); |
366 |
|
367 |
$self->add ($self->{vbox} = new CFClient::UI::VBox); |
368 |
|
369 |
$self->{label} = [ |
370 |
map |
371 |
CFClient::UI::Label->new ( |
372 |
can_hover => 1, |
373 |
can_events => 1, |
374 |
tooltip_width => 0.33, |
375 |
fontsize => $_, |
376 |
), (0.8) x 16 |
377 |
]; |
378 |
|
379 |
$self->{entry} = new CFClient::UI::Entry |
380 |
connect_changed => sub { |
381 |
$self->update_labels; |
382 |
}; |
383 |
|
384 |
$self->{vbox}->add ( |
385 |
$self->{entry}, |
386 |
@{$self->{label}}, |
387 |
); |
388 |
|
389 |
$self |
390 |
} |
391 |
|
392 |
sub size_allocate { |
393 |
my ($self, $w, $h) = @_; |
394 |
|
395 |
$self->SUPER::size_allocate ($w, $h); |
396 |
$self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
397 |
} |
398 |
|
399 |
sub key_down { |
400 |
my ($self, $ev) = @_; |
401 |
|
402 |
if ($ev->{sym} == 13) { |
403 |
if (exists $self->{select}) { |
404 |
$self->emit (execute => $self->{select}); |
405 |
$self->emit ("close"); |
406 |
} |
407 |
} elsif ($ev->{sym} == 27) { |
408 |
$self->{entry}->set_text (""); |
409 |
$self->emit ("close"); |
410 |
return; |
411 |
} elsif ($ev->{sym} == CFClient::SDLK_DOWN) { |
412 |
++$self->{select_offset} |
413 |
if $self->{select_offset} < $#{ $self->{last_match} || [] }; |
414 |
$self->update_labels; |
415 |
} elsif ($ev->{sym} == CFClient::SDLK_UP) { |
416 |
--$self->{select_offset} |
417 |
if $self->{select_offset}; |
418 |
$self->update_labels; |
419 |
} else { |
420 |
#$self->{entry}{force_alloc} = 1; |
421 |
$self->{entry}->key_down ($ev); |
422 |
} |
423 |
} |
424 |
|
425 |
sub update_labels { |
426 |
my ($self) = @_; |
427 |
|
428 |
my $text = $self->{entry}->get_text; |
429 |
|
430 |
length $text |
431 |
or return $self->emit ("close"); |
432 |
|
433 |
my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
434 |
|
435 |
if ($cmd ne $self->{last_search}) { |
436 |
my $regexp = do { |
437 |
my ($beg, @chr) = split //, lc $cmd; |
438 |
|
439 |
# the following regex is used to match our "completion entry" |
440 |
# to an actual command - the parentheses match kind of "overhead" |
441 |
# - the more characters the parentheses match, the less attractive |
442 |
# is the match. |
443 |
my $regexp = "^\Q$beg\E" |
444 |
. join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
445 |
qr<$regexp> |
446 |
}; |
447 |
|
448 |
my @match; |
449 |
my @penalty; |
450 |
|
451 |
for (keys %{$self->{command}}) { |
452 |
if (@penalty = $_ =~ $regexp) { |
453 |
push @match, [$_, length join "", map "::$_", grep defined, @penalty]; |
454 |
} |
455 |
} |
456 |
|
457 |
@match = map $self->{command}{$_->[0]}, |
458 |
sort { |
459 |
$a->[1] <=> $b->[1] |
460 |
or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
461 |
} @match; |
462 |
|
463 |
$self->{last_search} = $cmd; |
464 |
$self->{last_match} = \@match; |
465 |
|
466 |
$self->{select_offset} = 0; |
467 |
} |
468 |
|
469 |
my @labels = @{ $self->{label} }; |
470 |
my @matches = @{ $self->{last_match} || [] }; |
471 |
|
472 |
if ($self->{select_offset}) { |
473 |
splice @matches, 0, $self->{select_offset}, (); |
474 |
|
475 |
my $label = shift @labels; |
476 |
$label->set_text ("..."); |
477 |
$label->set_tooltip ("Use Cursor-Up to view previous matches"); |
478 |
} |
479 |
|
480 |
for my $label (@labels) { |
481 |
$label->{fg} = [1, 1, 1, 1]; |
482 |
$label->{bg} = [0, 0, 0, 0]; |
483 |
} |
484 |
|
485 |
if (@matches) { |
486 |
$self->{select} = "$matches[0][0]$arg"; |
487 |
|
488 |
$labels[0]->{fg} = [0, 0, 0, 1]; |
489 |
$labels[0]->{bg} = [1, 1, 1, 0.8]; |
490 |
} else { |
491 |
$self->{select} = "$cmd$arg"; |
492 |
} |
493 |
|
494 |
for my $match (@matches) { |
495 |
my $label = shift @labels; |
496 |
|
497 |
if (@labels) { |
498 |
$label->set_text ("$match->[0]$arg"); |
499 |
$label->set_tooltip ($match->[1]); |
500 |
} else { |
501 |
$label->set_text ("..."); |
502 |
$label->set_tooltip ("Use Cursor-Down to view more matches"); |
503 |
last; |
504 |
} |
505 |
} |
506 |
|
507 |
for my $label (@labels) { |
508 |
$label->set_text (""); |
509 |
$label->set_tooltip (""); |
510 |
} |
511 |
|
512 |
$self->update; |
513 |
### |
514 |
} |
515 |
|
516 |
sub _draw { |
517 |
my ($self) = @_; |
518 |
|
519 |
# hack |
520 |
local $CFClient::UI::FOCUS = $self->{entry}; |
521 |
|
522 |
$self->SUPER::_draw; |
523 |
} |
524 |
|
525 |
1 |