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 | |
42 | if ($ev->button == 2) { |
36 | if ($ev->{button} == 2) { |
43 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
37 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
44 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
38 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
45 | |
39 | |
46 | $self->{motion} = sub { |
40 | $self->{motion} = sub { |
47 | my ($ev, $x, $y) = @_; |
41 | my ($ev, $x, $y) = @_; |
48 | |
42 | |
49 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
43 | ($x, $y) = ($ev->{x}, $ev->{y}); |
50 | |
44 | |
51 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
45 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
52 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
46 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
53 | |
47 | |
54 | $self->update; |
48 | $self->update; |
… | |
… | |
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 | |
95 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
89 | 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; |
90 | my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
91 | |
|
|
92 | glPushMatrix; |
|
|
93 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
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 | } |
138 | |
135 | |
139 | # HACK BEGIN |
136 | glPopMatrix; |
140 | { |
|
|
141 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
142 | my ($w, $h) = (250, 250); |
|
|
143 | |
|
|
144 | glEnable GL_BLEND; |
|
|
145 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
146 | glEnable GL_TEXTURE_2D; |
|
|
147 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
148 | |
|
|
149 | $self->{mapmap_texture} = |
|
|
150 | new CFClient::Texture |
|
|
151 | w => $w, |
|
|
152 | h => $h, |
|
|
153 | data => $::MAP->mapmap ($w, $h), |
|
|
154 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
155 | |
|
|
156 | $self->{mapmap_texture}->draw_quad (100, 100); |
|
|
157 | |
|
|
158 | glDisable GL_TEXTURE_2D; |
|
|
159 | glDisable GL_BLEND; |
|
|
160 | } |
|
|
161 | # HACK END |
|
|
162 | } |
137 | } |
163 | |
138 | |
164 | glEndList; |
139 | glEndList; |
165 | } |
140 | } |
166 | |
141 | |
… | |
… | |
199 | ); |
174 | ); |
200 | |
175 | |
201 | sub key_down { |
176 | sub key_down { |
202 | my ($self, $ev) = @_; |
177 | my ($self, $ev) = @_; |
203 | |
178 | |
204 | my $mod = $ev->key_mod; |
179 | my $mod = $ev->{mod}; |
205 | my $sym = $ev->key_sym; |
180 | my $sym = $ev->{sym}; |
206 | |
181 | |
207 | if ($sym == CFClient::SDLK_KP5) { |
182 | if ($sym == CFClient::SDLK_KP5) { |
208 | $::CONN->user_send ("stay fire"); |
183 | $::CONN->user_send ("stay fire"); |
|
|
184 | } elsif ($sym == ord ",") { |
|
|
185 | $::CONN->user_send ("take"); |
209 | } elsif ($sym == ord "a") { |
186 | } elsif ($sym == ord "a") { |
210 | $::CONN->user_send ("apply"); |
187 | $::CONN->user_send ("apply"); |
211 | } elsif ($sym == ord "'") { |
188 | } elsif ($sym == ord "'") { |
212 | $self->emit ('activate_console'); |
189 | $self->emit ('activate_console'); |
213 | } elsif ($sym == ord "/") { |
190 | } elsif ($sym == ord "/") { |
214 | $self->emit ('activate_console' => '/'); |
191 | $self->emit (activate_console => '/'); |
215 | } elsif (exists $DIR{$sym}) { |
192 | } elsif (exists $DIR{$sym}) { |
216 | if ($mod & CFClient::KMOD_SHIFT) { |
193 | if ($mod & CFClient::KMOD_SHIFT) { |
217 | $self->{shft}++; |
194 | $self->{shft}++; |
218 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
195 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
219 | } elsif ($mod & CFClient::KMOD_CTRL) { |
196 | } elsif ($mod & CFClient::KMOD_CTRL) { |
220 | $self->{ctrl}++; |
197 | $self->{ctrl}++; |
221 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
198 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
222 | } else { |
199 | } else { |
223 | $::CONN->user_send ("$DIR{$sym}[1]"); |
200 | $::CONN->user_send ("$DIR{$sym}[1]"); |
224 | } |
201 | } |
|
|
202 | } elsif ($ev->{unicode}) { |
|
|
203 | $self->{command_widget} ||= |
|
|
204 | new CFClient::MapWidget::Command:: |
|
|
205 | command => $self->{command}, |
|
|
206 | can_focus => 1, |
|
|
207 | connect_execute => sub { |
|
|
208 | # todo: support callback instead of user_send |
|
|
209 | $::CONN->user_send ($_[1][1]); |
|
|
210 | }, |
|
|
211 | connect_close => sub { |
|
|
212 | (delete $self->{command_widget})->hide; |
|
|
213 | $self->focus_in; |
|
|
214 | }, |
|
|
215 | ; |
|
|
216 | $self->{command_widget}->key_down ($ev); |
|
|
217 | return unless $self->{command_widget}; |
|
|
218 | $self->{command_widget}->show; |
|
|
219 | $self->{command_widget}->focus_in; |
225 | } |
220 | } |
226 | } |
221 | } |
227 | |
222 | |
228 | sub key_up { |
223 | sub key_up { |
229 | my ($self, $ev) = @_; |
224 | my ($self, $ev) = @_; |
230 | |
225 | |
231 | my $mod = $ev->key_mod; |
226 | my $mod = $ev->{mod}; |
232 | my $sym = $ev->key_sym; |
227 | my $sym = $ev->{sym}; |
233 | |
228 | |
234 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
229 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
235 | $::CONN->user_send ("fire_stop"); |
230 | $::CONN->user_send ("fire_stop"); |
236 | } |
231 | } |
237 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
232 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
238 | $::CONN->user_send ("run_stop"); |
233 | $::CONN->user_send ("run_stop"); |
239 | } |
234 | } |
240 | } |
235 | } |
241 | |
236 | |
242 | sub add_command { |
237 | sub add_command { |
243 | my ($self, $command, $widget, $cb) = @_; |
238 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
244 | |
239 | |
245 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
240 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
246 | warn "$command|$abbrev|$widget\n";#d# |
241 | |
|
|
242 | push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; |
|
|
243 | } |
|
|
244 | |
|
|
245 | package CFClient::MapWidget::MapMap; |
|
|
246 | |
|
|
247 | our @ISA = CFClient::UI::Base::; |
|
|
248 | |
|
|
249 | use Time::HiRes qw(time); |
|
|
250 | use CFClient::OpenGL; |
|
|
251 | |
|
|
252 | sub size_request { |
|
|
253 | ($::HEIGHT * 0.25, $::HEIGHT * 0.25) |
|
|
254 | } |
|
|
255 | |
|
|
256 | sub size_allocate { |
|
|
257 | my ($self, $w, $h) = @_; |
|
|
258 | |
|
|
259 | $self->SUPER::size_allocate ($w, $h); |
|
|
260 | $self->update; |
|
|
261 | } |
|
|
262 | |
|
|
263 | sub update { |
|
|
264 | my ($self) = @_; |
|
|
265 | |
|
|
266 | delete $self->{texture_atime}; |
|
|
267 | $self->SUPER::update; |
|
|
268 | } |
|
|
269 | |
|
|
270 | sub _draw { |
|
|
271 | my ($self) = @_; |
|
|
272 | |
|
|
273 | $::MAP or return; |
|
|
274 | |
|
|
275 | my ($w, $h) = @$self{qw(w h)}; |
|
|
276 | |
|
|
277 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
|
|
278 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
|
|
279 | |
|
|
280 | my $sx = int $::CFG->{map_shift_x} / 32; |
|
|
281 | my $sy = int $::CFG->{map_shift_y} / 32; |
|
|
282 | |
|
|
283 | my $ox = 0.5 * ($w - $sw); |
|
|
284 | my $oy = 0.5 * ($h - $sh); |
|
|
285 | |
|
|
286 | glEnable GL_BLEND; |
|
|
287 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
288 | glEnable GL_TEXTURE_2D; |
|
|
289 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
290 | |
|
|
291 | if ($self->{texture_atime} < time) { |
|
|
292 | $self->{texture_atime} = time + 1/3; |
|
|
293 | |
|
|
294 | $self->{texture} = |
|
|
295 | new CFClient::Texture |
|
|
296 | w => $w, |
|
|
297 | h => $h, |
|
|
298 | data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
|
|
299 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
300 | } |
|
|
301 | |
|
|
302 | $self->{texture}->draw_quad (0, 0); |
|
|
303 | |
|
|
304 | glDisable GL_TEXTURE_2D; |
|
|
305 | |
|
|
306 | glTranslate 0.375, 0.375; |
|
|
307 | |
|
|
308 | #TODO: map scale is completely borked |
|
|
309 | |
|
|
310 | my $x0 = int $ox - $sx + 0.5; |
|
|
311 | my $y0 = int $oy - $sy + 0.5; |
|
|
312 | |
|
|
313 | glColor 1, 1, 0, 1; |
|
|
314 | glBegin GL_LINE_LOOP; |
|
|
315 | glVertex $x0 , $y0 ; |
|
|
316 | glVertex $x0 , $y0 + $sh; |
|
|
317 | glVertex $x0 + $sw, $y0 + $sh; |
|
|
318 | glVertex $x0 + $sw, $y0 ; |
|
|
319 | glEnd; |
|
|
320 | |
|
|
321 | glDisable GL_BLEND; |
|
|
322 | } |
|
|
323 | |
|
|
324 | package CFClient::MapWidget::Command; |
|
|
325 | |
|
|
326 | use strict; |
|
|
327 | |
|
|
328 | use CFClient::OpenGL; |
|
|
329 | |
|
|
330 | our @ISA = CFClient::UI::VBox::; |
|
|
331 | |
|
|
332 | sub new { |
|
|
333 | my $class = shift; |
|
|
334 | |
|
|
335 | my $self = $class->SUPER::new ( |
|
|
336 | @_, |
|
|
337 | children => [map |
|
|
338 | CFClient::UI::Label->new ( |
|
|
339 | can_hover => 1, |
|
|
340 | can_events => 1, |
|
|
341 | fontsize => $_, |
|
|
342 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
|
|
343 | ], |
|
|
344 | ); |
|
|
345 | |
|
|
346 | $self |
|
|
347 | } |
|
|
348 | |
|
|
349 | sub size_allocate { |
|
|
350 | my ($self, $w, $h) = @_; |
|
|
351 | |
|
|
352 | $self->SUPER::size_allocate ($w, $h); |
|
|
353 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
|
|
354 | } |
|
|
355 | |
|
|
356 | sub update_labels { |
|
|
357 | my ($self) = @_; |
|
|
358 | |
|
|
359 | my $command = $self->{command}; |
|
|
360 | my $search_abbrev = qr/^\Q$self->{search}/; |
|
|
361 | my $search_full = qr/\Q$self->{search}/; |
|
|
362 | |
|
|
363 | my @found; |
|
|
364 | |
|
|
365 | for (@$command) { |
|
|
366 | if ($_->[0] =~ $search_abbrev) { |
|
|
367 | push @found, [$_->[0], $_]; |
|
|
368 | } elsif ($_->[1] =~ $search_full) { |
|
|
369 | push @found, [$_->[1], $_]; |
|
|
370 | } |
|
|
371 | } |
|
|
372 | |
|
|
373 | @found = sort { $a->[0] cmp $b->[0] } @found; |
|
|
374 | |
|
|
375 | $self->{children}[0]->set_text ("$self->{search}_"); |
|
|
376 | |
|
|
377 | for (0..5) { |
|
|
378 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
379 | $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : ""); |
|
|
380 | } |
|
|
381 | |
|
|
382 | $self->{select} = $found[0][1] |
|
|
383 | if @found; |
|
|
384 | |
|
|
385 | if (@found > 6) { |
|
|
386 | $self->{children}[6]->set_text ("..."); |
|
|
387 | } |
|
|
388 | |
|
|
389 | $self->check_size; |
|
|
390 | } |
|
|
391 | |
|
|
392 | sub key_down { |
|
|
393 | my ($self, $ev) = @_; |
|
|
394 | |
|
|
395 | if ($ev->{sym} == 8) { |
|
|
396 | substr $self->{search}, -1, 1, ""; |
|
|
397 | $self->update_labels; |
|
|
398 | } elsif ($ev->{sym} == 13) { |
|
|
399 | if (exists $self->{select}) { |
|
|
400 | $self->emit (execute => $self->{select}); |
|
|
401 | $self->emit ("close"); |
|
|
402 | } |
|
|
403 | } elsif ($ev->{sym} == 27) { |
|
|
404 | $self->emit ("close"); |
|
|
405 | return; |
|
|
406 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
|
|
407 | $self->{search} .= chr $ev->{unicode}; |
|
|
408 | $self->update_labels; |
|
|
409 | } |
|
|
410 | |
|
|
411 | length $self->{search} |
|
|
412 | or $self->emit ("close"); |
247 | } |
413 | } |
248 | |
414 | |
249 | 1 |
415 | 1 |