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 SDL; |
8 | use CFClient::OpenGL; |
8 | use SDL::OpenGL; |
|
|
9 | |
9 | |
10 | our @ISA = CFClient::UI::Base::; |
10 | our @ISA = CFClient::UI::Base::; |
11 | |
11 | |
12 | sub new { |
12 | sub new { |
13 | my $class = shift; |
13 | my $class = shift; |
14 | |
14 | |
15 | $class->SUPER::new ( |
15 | $class->SUPER::new ( |
16 | z => -1, |
16 | z => -1, |
17 | can_focus => 1, |
17 | can_focus => 1, |
18 | list => (glGenLists 1), |
18 | list => glGenList, |
19 | @_ |
19 | @_ |
20 | ) |
20 | ) |
21 | } |
21 | } |
22 | |
22 | |
23 | sub key_down { |
23 | sub DESTROY { |
24 | print "MAPKEYDOWN\n"; |
24 | my $self = shift; |
25 | } |
|
|
26 | |
25 | |
27 | sub key_up { |
26 | glDeleteList $self->{list}; |
|
|
27 | |
|
|
28 | $self->SUPER::DESTROY; |
28 | } |
29 | } |
29 | |
30 | |
30 | sub button_down { |
31 | sub button_down { |
31 | my ($self, $ev, $x, $y) = @_; |
32 | my ($self, $ev, $x, $y) = @_; |
32 | |
33 | |
33 | $self->focus_in; |
34 | $self->focus_in; |
34 | |
35 | |
35 | if ($ev->button == 2) { |
36 | if ($ev->{button} == 2) { |
36 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
37 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
37 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
38 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
38 | |
39 | |
39 | $self->{motion} = sub { |
40 | $self->{motion} = sub { |
40 | my ($ev, $x, $y) = @_; |
41 | my ($ev, $x, $y) = @_; |
41 | |
42 | |
42 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
43 | ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
44 | |
44 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
45 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
45 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
46 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
46 | |
47 | |
47 | $self->update; |
48 | $self->update; |
… | |
… | |
77 | |
78 | |
78 | sub draw { |
79 | sub draw { |
79 | my ($self) = @_; |
80 | my ($self) = @_; |
80 | |
81 | |
81 | if (delete $self->{need_update}) { |
82 | if (delete $self->{need_update}) { |
82 | glNewList $self->{list}, GL_COMPILE; |
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}) + 0.99; |
86 | my $sh = int $::HEIGHT / 32; |
87 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
87 | |
88 | |
88 | 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; |
89 | 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}; |
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 | |
… | |
… | |
99 | GL_CONVOLUTION_2D, |
103 | GL_CONVOLUTION_2D, |
100 | GL_ALPHA, |
104 | GL_ALPHA, |
101 | 3, 3, |
105 | 3, 3, |
102 | GL_ALPHA, GL_FLOAT, |
106 | GL_ALPHA, GL_FLOAT, |
103 | pack "f*", |
107 | pack "f*", |
104 | 0.1, 0.1, 0.1, |
108 | 0.05, 0.13, 0.05, |
105 | 0.1, 0.2, 0.1, |
109 | 0.13, 0.30, 0.13, |
106 | 0.1, 0.1, 0.1, |
110 | 0.05, 0.13, 0.05, |
107 | ); |
111 | ); |
108 | glEnable GL_CONVOLUTION_2D; |
112 | glEnable GL_CONVOLUTION_2D; |
109 | } |
113 | } |
110 | |
114 | |
111 | $self->{fow_texture} = new CFClient::Texture |
115 | $self->{fow_texture} = new CFClient::Texture |
… | |
… | |
120 | glEnable GL_BLEND; |
124 | glEnable GL_BLEND; |
121 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
125 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
122 | glEnable GL_TEXTURE_2D; |
126 | glEnable GL_TEXTURE_2D; |
123 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
127 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
124 | |
128 | |
125 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
129 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
126 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
130 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
127 | |
131 | |
128 | glDisable GL_TEXTURE_2D; |
132 | glDisable GL_TEXTURE_2D; |
129 | glDisable GL_BLEND; |
133 | glDisable GL_BLEND; |
130 | } |
134 | } |
131 | |
135 | |
132 | # HACK BEGIN |
136 | glPopMatrix; |
133 | { |
|
|
134 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
135 | my ($w, $h) = (250, 250); |
|
|
136 | |
|
|
137 | glEnable GL_BLEND; |
|
|
138 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
139 | glEnable GL_TEXTURE_2D; |
|
|
140 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
141 | |
|
|
142 | $self->{mapmap_texture} = |
|
|
143 | new CFClient::Texture |
|
|
144 | w => $w, |
|
|
145 | h => $h, |
|
|
146 | data => $::MAP->mapmap ($w, $h), |
|
|
147 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
148 | |
|
|
149 | $self->{mapmap_texture}->draw_quad (100, 100); |
|
|
150 | |
|
|
151 | glDisable GL_TEXTURE_2D; |
|
|
152 | glDisable GL_BLEND; |
|
|
153 | } |
|
|
154 | # HACK END |
|
|
155 | } |
137 | } |
156 | |
138 | |
157 | glEndList; |
139 | glEndList; |
158 | } |
140 | } |
159 | |
141 | |
… | |
… | |
174 | glDisable GL_COLOR_LOGIC_OP; |
156 | glDisable GL_COLOR_LOGIC_OP; |
175 | } |
157 | } |
176 | } |
158 | } |
177 | |
159 | |
178 | my %DIR = ( |
160 | my %DIR = ( |
179 | SDLK_KP8, [1, "north"], |
161 | CFClient::SDLK_KP8, [1, "north"], |
180 | SDLK_KP9, [2, "northeast"], |
162 | CFClient::SDLK_KP9, [2, "northeast"], |
181 | SDLK_KP6, [3, "east"], |
163 | CFClient::SDLK_KP6, [3, "east"], |
182 | SDLK_KP3, [4, "southeast"], |
164 | CFClient::SDLK_KP3, [4, "southeast"], |
183 | SDLK_KP2, [5, "south"], |
165 | CFClient::SDLK_KP2, [5, "south"], |
184 | SDLK_KP1, [6, "southwest"], |
166 | CFClient::SDLK_KP1, [6, "southwest"], |
185 | SDLK_KP4, [7, "west"], |
167 | CFClient::SDLK_KP4, [7, "west"], |
186 | SDLK_KP7, [8, "northwest"], |
168 | CFClient::SDLK_KP7, [8, "northwest"], |
187 | |
169 | |
188 | SDLK_UP, [1, "north"], |
170 | CFClient::SDLK_UP, [1, "north"], |
189 | SDLK_RIGHT, [3, "east"], |
171 | CFClient::SDLK_RIGHT, [3, "east"], |
190 | SDLK_DOWN, [5, "south"], |
172 | CFClient::SDLK_DOWN, [5, "south"], |
191 | SDLK_LEFT, [7, "west"], |
173 | CFClient::SDLK_LEFT, [7, "west"], |
192 | ); |
174 | ); |
193 | |
175 | |
194 | sub key_down { |
176 | sub key_down { |
195 | my ($self, $ev) = @_; |
177 | my ($self, $ev) = @_; |
196 | |
178 | |
197 | my $mod = $ev->key_mod; |
179 | my $mod = $ev->{mod}; |
198 | my $sym = $ev->key_sym; |
180 | my $sym = $ev->{sym}; |
199 | |
181 | |
200 | if ($sym == SDLK_KP5) { |
182 | if ($sym == CFClient::SDLK_KP5) { |
201 | $::CONN->user_send ("stay fire"); |
183 | $::CONN->user_send ("stay fire"); |
|
|
184 | } elsif ($sym == ord ",") { |
|
|
185 | $::CONN->user_send ("take"); |
202 | } elsif ($sym == SDLK_a) { |
186 | } elsif ($sym == ord "a") { |
203 | $::CONN->user_send ("apply"); |
187 | $::CONN->user_send ("apply"); |
204 | } elsif ($sym == SDLK_QUOTE) { |
188 | } elsif ($sym == ord "'") { |
205 | $self->emit ('activate_console'); |
189 | $self->emit ('activate_console'); |
206 | } elsif ($sym == SDLK_SLASH) { |
190 | } elsif ($sym == ord "/") { |
207 | $self->emit ('activate_console' => '/'); |
191 | $self->emit (activate_console => '/'); |
208 | } elsif (exists $DIR{$sym}) { |
192 | } elsif (exists $DIR{$sym}) { |
209 | if ($mod & KMOD_SHIFT) { |
193 | if ($mod & CFClient::KMOD_SHIFT) { |
210 | $self->{shft}++; |
194 | $self->{shft}++; |
211 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
195 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
212 | } elsif ($mod & KMOD_CTRL) { |
196 | } elsif ($mod & CFClient::KMOD_CTRL) { |
213 | $self->{ctrl}++; |
197 | $self->{ctrl}++; |
214 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
198 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
215 | } else { |
199 | } else { |
216 | $::CONN->user_send ("$DIR{$sym}[1]"); |
200 | $::CONN->user_send ("$DIR{$sym}[1]"); |
217 | } |
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; |
218 | } |
220 | } |
219 | } |
221 | } |
220 | |
222 | |
221 | sub key_up { |
223 | sub key_up { |
222 | my ($self, $ev) = @_; |
224 | my ($self, $ev) = @_; |
223 | |
225 | |
224 | my $mod = $ev->key_mod; |
226 | my $mod = $ev->{mod}; |
225 | my $sym = $ev->key_sym; |
227 | my $sym = $ev->{sym}; |
226 | |
228 | |
227 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
229 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
228 | $::CONN->user_send ("fire_stop"); |
230 | $::CONN->user_send ("fire_stop"); |
229 | } |
231 | } |
230 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
232 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
231 | $::CONN->user_send ("run_stop"); |
233 | $::CONN->user_send ("run_stop"); |
232 | } |
234 | } |
233 | } |
235 | } |
234 | |
236 | |
|
|
237 | sub add_command { |
|
|
238 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
|
|
239 | |
|
|
240 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
|
|
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"); |
|
|
413 | } |
|
|
414 | |
235 | 1 |
415 | 1 |