1 | package CFClient::MapWidget; |
1 | package CFClient::MapWidget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
|
|
4 | use utf8; |
4 | |
5 | |
5 | use List::Util qw(min max); |
6 | use List::Util qw(min max); |
6 | |
7 | |
7 | use CFClient::OpenGL; |
8 | use CFClient::OpenGL; |
8 | |
9 | |
… | |
… | |
25 | glDeleteList $self->{list}; |
26 | glDeleteList $self->{list}; |
26 | |
27 | |
27 | $self->SUPER::DESTROY; |
28 | $self->SUPER::DESTROY; |
28 | } |
29 | } |
29 | |
30 | |
30 | sub key_down { |
|
|
31 | print "MAPKEYDOWN\n"; |
|
|
32 | } |
|
|
33 | |
|
|
34 | sub key_up { |
|
|
35 | } |
|
|
36 | |
|
|
37 | sub button_down { |
31 | sub button_down { |
38 | my ($self, $ev, $x, $y) = @_; |
32 | my ($self, $ev, $x, $y) = @_; |
39 | |
33 | |
40 | $self->focus_in; |
34 | $self->focus_in; |
41 | |
35 | |
… | |
… | |
87 | |
81 | |
88 | if (delete $self->{need_update}) { |
82 | if (delete $self->{need_update}) { |
89 | glNewList $self->{list}; |
83 | glNewList $self->{list}; |
90 | |
84 | |
91 | if ($::MAP) { |
85 | if ($::MAP) { |
92 | my $sw = int $::WIDTH / 32; |
86 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
93 | my $sh = int $::HEIGHT / 32; |
87 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
94 | |
88 | |
|
|
89 | glPushMatrix; |
|
|
90 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
91 | |
95 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
92 | my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
96 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
93 | my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
97 | |
94 | |
98 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
95 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
99 | |
96 | |
100 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
97 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
101 | |
98 | |
… | |
… | |
106 | GL_CONVOLUTION_2D, |
103 | GL_CONVOLUTION_2D, |
107 | GL_ALPHA, |
104 | GL_ALPHA, |
108 | 3, 3, |
105 | 3, 3, |
109 | GL_ALPHA, GL_FLOAT, |
106 | GL_ALPHA, GL_FLOAT, |
110 | pack "f*", |
107 | pack "f*", |
111 | 0.1, 0.1, 0.1, |
108 | 0.05, 0.13, 0.05, |
112 | 0.1, 0.2, 0.1, |
109 | 0.13, 0.30, 0.13, |
113 | 0.1, 0.1, 0.1, |
110 | 0.05, 0.13, 0.05, |
114 | ); |
111 | ); |
115 | glEnable GL_CONVOLUTION_2D; |
112 | glEnable GL_CONVOLUTION_2D; |
116 | } |
113 | } |
117 | |
114 | |
118 | $self->{fow_texture} = new CFClient::Texture |
115 | $self->{fow_texture} = new CFClient::Texture |
… | |
… | |
127 | glEnable GL_BLEND; |
124 | glEnable GL_BLEND; |
128 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
125 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
129 | glEnable GL_TEXTURE_2D; |
126 | glEnable GL_TEXTURE_2D; |
130 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
127 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
131 | |
128 | |
132 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
129 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
133 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
130 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
134 | |
131 | |
135 | glDisable GL_TEXTURE_2D; |
132 | glDisable GL_TEXTURE_2D; |
136 | glDisable GL_BLEND; |
133 | glDisable GL_BLEND; |
137 | } |
134 | } |
… | |
… | |
147 | |
144 | |
148 | glEnable GL_BLEND; |
145 | glEnable GL_BLEND; |
149 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
146 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
150 | glEnable GL_TEXTURE_2D; |
147 | glEnable GL_TEXTURE_2D; |
151 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
148 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
149 | |
|
|
150 | glPopMatrix; |
152 | |
151 | |
153 | $self->{mapmap_texture} = |
152 | $self->{mapmap_texture} = |
154 | new CFClient::Texture |
153 | new CFClient::Texture |
155 | w => $w, |
154 | w => $w, |
156 | h => $h, |
155 | h => $h, |
… | |
… | |
219 | my $mod = $ev->{mod}; |
218 | my $mod = $ev->{mod}; |
220 | my $sym = $ev->{sym}; |
219 | my $sym = $ev->{sym}; |
221 | |
220 | |
222 | if ($sym == CFClient::SDLK_KP5) { |
221 | if ($sym == CFClient::SDLK_KP5) { |
223 | $::CONN->user_send ("stay fire"); |
222 | $::CONN->user_send ("stay fire"); |
|
|
223 | } elsif ($sym == ord ",") { |
|
|
224 | $::CONN->user_send ("take"); |
224 | } elsif ($sym == ord "a") { |
225 | } elsif ($sym == ord "a") { |
225 | $::CONN->user_send ("apply"); |
226 | $::CONN->user_send ("apply"); |
226 | } elsif ($sym == ord "'") { |
227 | } elsif ($sym == ord "'") { |
227 | $self->emit ('activate_console'); |
228 | $self->emit ('activate_console'); |
228 | } elsif ($sym == ord "/") { |
229 | } elsif ($sym == ord "/") { |
229 | $self->emit ('activate_console' => '/'); |
230 | $self->emit (activate_console => '/'); |
230 | } elsif (exists $DIR{$sym}) { |
231 | } elsif (exists $DIR{$sym}) { |
231 | if ($mod & CFClient::KMOD_SHIFT) { |
232 | if ($mod & CFClient::KMOD_SHIFT) { |
232 | $self->{shft}++; |
233 | $self->{shft}++; |
233 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
234 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
234 | } elsif ($mod & CFClient::KMOD_CTRL) { |
235 | } elsif ($mod & CFClient::KMOD_CTRL) { |
235 | $self->{ctrl}++; |
236 | $self->{ctrl}++; |
236 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
237 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
237 | } else { |
238 | } else { |
238 | $::CONN->user_send ("$DIR{$sym}[1]"); |
239 | $::CONN->user_send ("$DIR{$sym}[1]"); |
239 | } |
240 | } |
|
|
241 | } elsif ($ev->{unicode}) { |
|
|
242 | $self->{command_widget} ||= |
|
|
243 | new CFClient::MapWidget::Command:: |
|
|
244 | command => $self->{command}, |
|
|
245 | can_focus => 1, |
|
|
246 | connect_execute => sub { |
|
|
247 | # todo: support callback instead of user_send |
|
|
248 | $::CONN->user_send ($_[1][1]); |
|
|
249 | }, |
|
|
250 | connect_close => sub { |
|
|
251 | (delete $self->{command_widget})->hide; |
|
|
252 | $self->focus_in; |
|
|
253 | }, |
|
|
254 | ; |
|
|
255 | $self->{command_widget}->key_down ($ev); |
|
|
256 | return unless $self->{command_widget}; |
|
|
257 | $self->{command_widget}->show; |
|
|
258 | $self->{command_widget}->focus_in; |
240 | } |
259 | } |
241 | } |
260 | } |
242 | |
261 | |
243 | sub key_up { |
262 | sub key_up { |
244 | my ($self, $ev) = @_; |
263 | my ($self, $ev) = @_; |
… | |
… | |
253 | $::CONN->user_send ("run_stop"); |
272 | $::CONN->user_send ("run_stop"); |
254 | } |
273 | } |
255 | } |
274 | } |
256 | |
275 | |
257 | sub add_command { |
276 | sub add_command { |
258 | my ($self, $command, $widget, $cb) = @_; |
277 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
259 | |
278 | |
260 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
279 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
261 | warn "$command|$abbrev|$widget\n";#d# |
280 | |
|
|
281 | push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; |
|
|
282 | } |
|
|
283 | |
|
|
284 | package CFClient::MapWidget::Command; |
|
|
285 | |
|
|
286 | use strict; |
|
|
287 | |
|
|
288 | use CFClient::OpenGL; |
|
|
289 | |
|
|
290 | our @ISA = CFClient::UI::VBox::; |
|
|
291 | |
|
|
292 | sub new { |
|
|
293 | my $class = shift; |
|
|
294 | |
|
|
295 | my $self = $class->SUPER::new ( |
|
|
296 | @_, |
|
|
297 | children => [map |
|
|
298 | CFClient::UI::Label->new ( |
|
|
299 | can_hover => 1, |
|
|
300 | can_events => 1, |
|
|
301 | fontsize => $_, |
|
|
302 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
|
|
303 | ], |
|
|
304 | ); |
|
|
305 | |
|
|
306 | $self |
|
|
307 | } |
|
|
308 | |
|
|
309 | sub size_allocate { |
|
|
310 | my ($self, $w, $h) = @_; |
|
|
311 | |
|
|
312 | $self->SUPER::size_allocate ($w, $h); |
|
|
313 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
|
|
314 | } |
|
|
315 | |
|
|
316 | sub update_labels { |
|
|
317 | my ($self) = @_; |
|
|
318 | |
|
|
319 | my $command = $self->{command}; |
|
|
320 | my $search_abbrev = qr/^\Q$self->{search}/; |
|
|
321 | my $search_full = qr/\Q$self->{search}/; |
|
|
322 | |
|
|
323 | my @found; |
|
|
324 | |
|
|
325 | for (@$command) { |
|
|
326 | if ($_->[0] =~ $search_abbrev) { |
|
|
327 | push @found, [$_->[0], $_]; |
|
|
328 | } elsif ($_->[1] =~ $search_full) { |
|
|
329 | push @found, [$_->[1], $_]; |
|
|
330 | } |
|
|
331 | } |
|
|
332 | |
|
|
333 | @found = sort { $a->[0] cmp $b->[0] } @found; |
|
|
334 | |
|
|
335 | $self->{children}[0]->set_text ("$self->{search}_"); |
|
|
336 | |
|
|
337 | for (0..5) { |
|
|
338 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
339 | $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : ""); |
|
|
340 | } |
|
|
341 | |
|
|
342 | $self->{select} = $found[0][1] |
|
|
343 | if @found; |
|
|
344 | |
|
|
345 | if (@found > 6) { |
|
|
346 | $self->{children}[6]->set_text ("..."); |
|
|
347 | } |
|
|
348 | |
|
|
349 | $self->check_size; |
|
|
350 | } |
|
|
351 | |
|
|
352 | sub key_down { |
|
|
353 | my ($self, $ev) = @_; |
|
|
354 | |
|
|
355 | if ($ev->{sym} == 8) { |
|
|
356 | substr $self->{search}, -1, 1, ""; |
|
|
357 | $self->update_labels; |
|
|
358 | } elsif ($ev->{sym} == 13) { |
|
|
359 | if (exists $self->{select}) { |
|
|
360 | $self->emit (execute => $self->{select}); |
|
|
361 | $self->emit ("close"); |
|
|
362 | } |
|
|
363 | } elsif ($ev->{sym} == 27) { |
|
|
364 | $self->emit ("close"); |
|
|
365 | return; |
|
|
366 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
|
|
367 | $self->{search} .= chr $ev->{unicode}; |
|
|
368 | $self->update_labels; |
|
|
369 | } |
|
|
370 | |
|
|
371 | length $self->{search} |
|
|
372 | or $self->emit ("close"); |
262 | } |
373 | } |
263 | |
374 | |
264 | 1 |
375 | 1 |