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 | |
… | |
… | |
80 | |
81 | |
81 | if (delete $self->{need_update}) { |
82 | if (delete $self->{need_update}) { |
82 | glNewList $self->{list}; |
83 | glNewList $self->{list}; |
83 | |
84 | |
84 | if ($::MAP) { |
85 | if ($::MAP) { |
85 | my $sw = int $::WIDTH / 32; |
86 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}); |
86 | my $sh = int $::HEIGHT / 32; |
87 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}); |
87 | |
88 | |
|
|
89 | glPushMatrix; |
|
|
90 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
91 | |
88 | 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; |
89 | 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; |
90 | |
94 | |
91 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
95 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
92 | |
96 | |
93 | 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); |
94 | |
98 | |
… | |
… | |
141 | glEnable GL_BLEND; |
145 | glEnable GL_BLEND; |
142 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
146 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
143 | glEnable GL_TEXTURE_2D; |
147 | glEnable GL_TEXTURE_2D; |
144 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
148 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
145 | |
149 | |
|
|
150 | glPopMatrix; |
|
|
151 | |
146 | $self->{mapmap_texture} = |
152 | $self->{mapmap_texture} = |
147 | new CFClient::Texture |
153 | new CFClient::Texture |
148 | w => $w, |
154 | w => $w, |
149 | h => $h, |
155 | h => $h, |
150 | data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h), |
156 | data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h), |
… | |
… | |
212 | my $mod = $ev->{mod}; |
218 | my $mod = $ev->{mod}; |
213 | my $sym = $ev->{sym}; |
219 | my $sym = $ev->{sym}; |
214 | |
220 | |
215 | if ($sym == CFClient::SDLK_KP5) { |
221 | if ($sym == CFClient::SDLK_KP5) { |
216 | $::CONN->user_send ("stay fire"); |
222 | $::CONN->user_send ("stay fire"); |
|
|
223 | } elsif ($sym == ord ",") { |
|
|
224 | $::CONN->user_send ("take"); |
217 | } elsif ($sym == ord "a") { |
225 | } elsif ($sym == ord "a") { |
218 | $::CONN->user_send ("apply"); |
226 | $::CONN->user_send ("apply"); |
219 | } elsif ($sym == ord "'") { |
227 | } elsif ($sym == ord "'") { |
220 | $self->emit ('activate_console'); |
228 | $self->emit ('activate_console'); |
221 | } elsif ($sym == ord "/") { |
229 | } elsif ($sym == ord "/") { |
… | |
… | |
234 | $self->{command_widget} ||= |
242 | $self->{command_widget} ||= |
235 | new CFClient::MapWidget::Command:: |
243 | new CFClient::MapWidget::Command:: |
236 | command => $self->{command}, |
244 | command => $self->{command}, |
237 | can_focus => 1, |
245 | can_focus => 1, |
238 | connect_execute => sub { |
246 | connect_execute => sub { |
|
|
247 | # todo: support callback instead of user_send |
239 | $::CONN->user_send ($_[1]); |
248 | $::CONN->user_send ($_[1][1]); |
240 | }, |
249 | }, |
241 | connect_close => sub { |
250 | connect_close => sub { |
242 | (delete $self->{command_widget})->hide; |
251 | (delete $self->{command_widget})->hide; |
243 | $self->focus_in; |
252 | $self->focus_in; |
244 | }, |
253 | }, |
245 | ; |
254 | ; |
246 | $self->{command_widget}->key_down ($ev); |
255 | $self->{command_widget}->key_down ($ev); |
|
|
256 | return unless $self->{command_widget}; |
247 | $self->{command_widget}->show; |
257 | $self->{command_widget}->show; |
248 | $self->{command_widget}->focus_in; |
258 | $self->{command_widget}->focus_in; |
249 | } |
259 | } |
250 | } |
260 | } |
251 | |
261 | |
… | |
… | |
262 | $::CONN->user_send ("run_stop"); |
272 | $::CONN->user_send ("run_stop"); |
263 | } |
273 | } |
264 | } |
274 | } |
265 | |
275 | |
266 | sub add_command { |
276 | sub add_command { |
267 | my ($self, $command, $widget, $cb) = @_; |
277 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
268 | |
278 | |
269 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
279 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
270 | |
280 | |
271 | push @{$self->{command}}, [$abbrev, $command]; |
281 | push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; |
272 | #warn "$command|$abbrev|$widget\n";#d# |
|
|
273 | } |
282 | } |
274 | |
283 | |
275 | package CFClient::MapWidget::Command; |
284 | package CFClient::MapWidget::Command; |
276 | |
285 | |
277 | use strict; |
286 | use strict; |
… | |
… | |
285 | |
294 | |
286 | my $self = $class->SUPER::new ( |
295 | my $self = $class->SUPER::new ( |
287 | @_, |
296 | @_, |
288 | children => [map |
297 | children => [map |
289 | CFClient::UI::Label->new ( |
298 | CFClient::UI::Label->new ( |
290 | can_hover => 1, |
299 | can_hover => 1, |
|
|
300 | can_events => 1, |
291 | fontsize => $_, |
301 | fontsize => $_, |
292 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
302 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
293 | ], |
303 | ], |
294 | ); |
304 | ); |
295 | |
305 | |
296 | $self |
306 | $self |
… | |
… | |
313 | my @found; |
323 | my @found; |
314 | |
324 | |
315 | for (@$command) { |
325 | for (@$command) { |
316 | if ($_->[0] =~ $search_abbrev) { |
326 | if ($_->[0] =~ $search_abbrev) { |
317 | push @found, [$_->[0], $_]; |
327 | push @found, [$_->[0], $_]; |
318 | } elsif ($_[1] =~ $search_full) { |
328 | } elsif ($_->[1] =~ $search_full) { |
319 | push @found, [$_->[1], $_]; |
329 | push @found, [$_->[1], $_]; |
320 | } |
330 | } |
321 | } |
331 | } |
322 | |
332 | |
323 | @found = sort { $a->[0] cmp $b->[0] } @found; |
333 | @found = sort { $a->[0] cmp $b->[0] } @found; |
324 | |
334 | |
325 | $self->{children}[0]->set_text ("$self->{search}_"); |
335 | $self->{children}[0]->set_text ("$self->{search}_"); |
326 | |
336 | |
327 | for (0..5) { |
337 | for (0..5) { |
328 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
338 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
339 | $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : ""); |
329 | } |
340 | } |
330 | |
341 | |
331 | $self->{select} = $found[0][1][1] |
342 | $self->{select} = $found[0][1] |
332 | if @found; |
343 | if @found; |
333 | |
344 | |
334 | if (@found > 6) { |
345 | if (@found > 6) { |
335 | $self->{children}[6]->set_text ("..."); |
346 | $self->{children}[6]->set_text ("..."); |
336 | } |
347 | } |
… | |
… | |
349 | $self->emit (execute => $self->{select}); |
360 | $self->emit (execute => $self->{select}); |
350 | $self->emit ("close"); |
361 | $self->emit ("close"); |
351 | } |
362 | } |
352 | } elsif ($ev->{sym} == 27) { |
363 | } elsif ($ev->{sym} == 27) { |
353 | $self->emit ("close"); |
364 | $self->emit ("close"); |
354 | } elsif ($ev->{unicode}) { |
365 | return; |
|
|
366 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
355 | $self->{search} .= chr $ev->{unicode}; |
367 | $self->{search} .= chr $ev->{unicode}; |
356 | $self->update_labels; |
368 | $self->update_labels; |
357 | } |
369 | } |
|
|
370 | |
|
|
371 | length $self->{search} |
|
|
372 | or $self->emit ("close"); |
358 | } |
373 | } |
359 | |
374 | |
360 | 1 |
375 | 1 |