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::OpenGL; |
8 | use CFClient::OpenGL; |
8 | |
9 | |
9 | our @ISA = CFClient::UI::Base::; |
10 | our @ISA = CFClient::UI::Base::; |
10 | |
11 | |
11 | sub new { |
12 | sub new { |
12 | my $class = shift; |
13 | my $class = shift; |
13 | |
14 | |
14 | $class->SUPER::new ( |
15 | $class->SUPER::new ( |
15 | z => -1, |
16 | z => -1, |
16 | can_focus => 1, |
17 | can_focus => 1, |
17 | list => (glGenLists 1), |
18 | list => glGenList, |
18 | @_ |
19 | @_ |
19 | ) |
20 | ) |
20 | } |
21 | } |
21 | |
22 | |
22 | sub key_down { |
23 | sub DESTROY { |
23 | print "MAPKEYDOWN\n"; |
24 | my $self = shift; |
24 | } |
|
|
25 | |
25 | |
26 | sub key_up { |
26 | glDeleteList $self->{list}; |
|
|
27 | |
|
|
28 | $self->SUPER::DESTROY; |
27 | } |
29 | } |
28 | |
30 | |
29 | sub button_down { |
31 | sub button_down { |
30 | my ($self, $ev, $x, $y) = @_; |
32 | my ($self, $ev, $x, $y) = @_; |
31 | |
33 | |
32 | $self->focus_in; |
34 | $self->focus_in; |
33 | |
35 | |
34 | if ($ev->button == 2) { |
36 | if ($ev->{button} == 2) { |
35 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
37 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
36 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
38 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
37 | |
39 | |
38 | $self->{motion} = sub { |
40 | $self->{motion} = sub { |
39 | my ($ev, $x, $y) = @_; |
41 | my ($ev, $x, $y) = @_; |
40 | |
42 | |
41 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
43 | ($x, $y) = ($ev->{x}, $ev->{y}); |
42 | |
44 | |
43 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
45 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
44 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
46 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
45 | |
47 | |
46 | $self->update; |
48 | $self->update; |
… | |
… | |
76 | |
78 | |
77 | sub draw { |
79 | sub draw { |
78 | my ($self) = @_; |
80 | my ($self) = @_; |
79 | |
81 | |
80 | if (delete $self->{need_update}) { |
82 | if (delete $self->{need_update}) { |
81 | glNewList $self->{list}, GL_COMPILE; |
83 | glNewList $self->{list}; |
82 | |
84 | |
83 | if ($::MAP) { |
85 | if ($::MAP) { |
84 | my $sw = int $::WIDTH / 32; |
86 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
85 | my $sh = int $::HEIGHT / 32; |
87 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
86 | |
88 | |
|
|
89 | glPushMatrix; |
|
|
90 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
91 | |
87 | 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; |
88 | 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; |
89 | |
94 | |
90 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
95 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
91 | |
96 | |
92 | 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); |
93 | |
98 | |
… | |
… | |
98 | GL_CONVOLUTION_2D, |
103 | GL_CONVOLUTION_2D, |
99 | GL_ALPHA, |
104 | GL_ALPHA, |
100 | 3, 3, |
105 | 3, 3, |
101 | GL_ALPHA, GL_FLOAT, |
106 | GL_ALPHA, GL_FLOAT, |
102 | pack "f*", |
107 | pack "f*", |
103 | 0.1, 0.1, 0.1, |
108 | 0.05, 0.13, 0.05, |
104 | 0.1, 0.2, 0.1, |
109 | 0.13, 0.30, 0.13, |
105 | 0.1, 0.1, 0.1, |
110 | 0.05, 0.13, 0.05, |
106 | ); |
111 | ); |
107 | glEnable GL_CONVOLUTION_2D; |
112 | glEnable GL_CONVOLUTION_2D; |
108 | } |
113 | } |
109 | |
114 | |
110 | $self->{fow_texture} = new CFClient::Texture |
115 | $self->{fow_texture} = new CFClient::Texture |
… | |
… | |
119 | glEnable GL_BLEND; |
124 | glEnable GL_BLEND; |
120 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
125 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
121 | glEnable GL_TEXTURE_2D; |
126 | glEnable GL_TEXTURE_2D; |
122 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
127 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
123 | |
128 | |
124 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
129 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
125 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
130 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
126 | |
131 | |
127 | glDisable GL_TEXTURE_2D; |
132 | glDisable GL_TEXTURE_2D; |
128 | glDisable GL_BLEND; |
133 | glDisable GL_BLEND; |
129 | } |
134 | } |
130 | |
135 | |
131 | # HACK BEGIN |
136 | # HACK BEGIN |
132 | { |
137 | { |
133 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
138 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
139 | |
|
|
140 | glTranslate 0, 30; |
134 | my ($w, $h) = (250, 250); |
141 | my ($w, $h) = (250, 250); |
|
|
142 | |
|
|
143 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
135 | |
144 | |
136 | glEnable GL_BLEND; |
145 | glEnable GL_BLEND; |
137 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
146 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
138 | glEnable GL_TEXTURE_2D; |
147 | glEnable GL_TEXTURE_2D; |
139 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
148 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
140 | |
149 | |
|
|
150 | glPopMatrix; |
|
|
151 | |
141 | $self->{mapmap_texture} = |
152 | $self->{mapmap_texture} = |
142 | new CFClient::Texture |
153 | new CFClient::Texture |
143 | w => $w, |
154 | w => $w, |
144 | h => $h, |
155 | h => $h, |
145 | data => $::MAP->mapmap ($w, $h), |
156 | data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h), |
146 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
157 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
147 | |
158 | |
148 | $self->{mapmap_texture}->draw_quad (100, 100); |
159 | $self->{mapmap_texture}->draw_quad (0, 0); |
149 | |
160 | |
150 | glDisable GL_TEXTURE_2D; |
161 | glDisable GL_TEXTURE_2D; |
|
|
162 | |
|
|
163 | glTranslate 0.375, 0.375; |
|
|
164 | |
|
|
165 | glColor 1, 1, 0, 1; |
|
|
166 | glBegin GL_LINE_LOOP; |
|
|
167 | glVertex $w * 0.5 - $sx , $h * 0.5 - $sy ; |
|
|
168 | glVertex $w * 0.5 - $sx , $h * 0.5 - $sy + $sh; |
|
|
169 | glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy + $sh; |
|
|
170 | glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy ; |
|
|
171 | glEnd; |
|
|
172 | |
151 | glDisable GL_BLEND; |
173 | glDisable GL_BLEND; |
152 | } |
174 | } |
153 | # HACK END |
175 | # HACK END |
154 | } |
176 | } |
155 | |
177 | |
… | |
… | |
191 | ); |
213 | ); |
192 | |
214 | |
193 | sub key_down { |
215 | sub key_down { |
194 | my ($self, $ev) = @_; |
216 | my ($self, $ev) = @_; |
195 | |
217 | |
196 | my $mod = $ev->key_mod; |
218 | my $mod = $ev->{mod}; |
197 | my $sym = $ev->key_sym; |
219 | my $sym = $ev->{sym}; |
198 | |
220 | |
199 | if ($sym == CFClient::SDLK_KP5) { |
221 | if ($sym == CFClient::SDLK_KP5) { |
200 | $::CONN->user_send ("stay fire"); |
222 | $::CONN->user_send ("stay fire"); |
|
|
223 | } elsif ($sym == ord ",") { |
|
|
224 | $::CONN->user_send ("take"); |
201 | } elsif ($sym == ord "a") { |
225 | } elsif ($sym == ord "a") { |
202 | $::CONN->user_send ("apply"); |
226 | $::CONN->user_send ("apply"); |
203 | } elsif ($sym == ord "'") { |
227 | } elsif ($sym == ord "'") { |
204 | $self->emit ('activate_console'); |
228 | $self->emit ('activate_console'); |
205 | } elsif ($sym == ord "/") { |
229 | } elsif ($sym == ord "/") { |
206 | $self->emit ('activate_console' => '/'); |
230 | $self->emit (activate_console => '/'); |
207 | } elsif (exists $DIR{$sym}) { |
231 | } elsif (exists $DIR{$sym}) { |
208 | if ($mod & CFClient::KMOD_SHIFT) { |
232 | if ($mod & CFClient::KMOD_SHIFT) { |
209 | $self->{shft}++; |
233 | $self->{shft}++; |
210 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
234 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
211 | } elsif ($mod & CFClient::KMOD_CTRL) { |
235 | } elsif ($mod & CFClient::KMOD_CTRL) { |
212 | $self->{ctrl}++; |
236 | $self->{ctrl}++; |
213 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
237 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
214 | } else { |
238 | } else { |
215 | $::CONN->user_send ("$DIR{$sym}[1]"); |
239 | $::CONN->user_send ("$DIR{$sym}[1]"); |
216 | } |
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; |
217 | } |
259 | } |
218 | } |
260 | } |
219 | |
261 | |
220 | sub key_up { |
262 | sub key_up { |
221 | my ($self, $ev) = @_; |
263 | my ($self, $ev) = @_; |
222 | |
264 | |
223 | my $mod = $ev->key_mod; |
265 | my $mod = $ev->{mod}; |
224 | my $sym = $ev->key_sym; |
266 | my $sym = $ev->{sym}; |
225 | |
267 | |
226 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
268 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
227 | $::CONN->user_send ("fire_stop"); |
269 | $::CONN->user_send ("fire_stop"); |
228 | } |
270 | } |
229 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
271 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
230 | $::CONN->user_send ("run_stop"); |
272 | $::CONN->user_send ("run_stop"); |
231 | } |
273 | } |
232 | } |
274 | } |
233 | |
275 | |
234 | sub add_command { |
276 | sub add_command { |
235 | my ($self, $command, $widget, $cb) = @_; |
277 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
236 | |
278 | |
237 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
279 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
238 | 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"); |
239 | } |
373 | } |
240 | |
374 | |
241 | 1 |
375 | 1 |