… | |
… | |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use List::Util qw(min max); |
5 | use List::Util qw(min max); |
6 | |
6 | |
7 | use SDL; |
7 | use CFClient::OpenGL; |
8 | use SDL::OpenGL; |
|
|
9 | |
8 | |
10 | our @ISA = CFClient::UI::Base::; |
9 | our @ISA = CFClient::UI::Base::; |
11 | |
10 | |
12 | sub new { |
11 | sub new { |
13 | my $class = shift; |
12 | my $class = shift; |
14 | |
13 | |
15 | $class->SUPER::new ( |
14 | $class->SUPER::new ( |
16 | z => -1, |
15 | z => -1, |
17 | can_focus => 1, |
16 | can_focus => 1, |
18 | list => (glGenLists 1), |
17 | list => glGenList, |
19 | @_ |
18 | @_ |
20 | ) |
19 | ) |
21 | } |
20 | } |
22 | |
21 | |
23 | sub key_down { |
22 | sub DESTROY { |
24 | print "MAPKEYDOWN\n"; |
23 | my $self = shift; |
25 | } |
|
|
26 | |
24 | |
27 | sub key_up { |
25 | glDeleteList $self->{list}; |
|
|
26 | |
|
|
27 | $self->SUPER::DESTROY; |
28 | } |
28 | } |
29 | |
29 | |
30 | sub button_down { |
30 | sub button_down { |
31 | my ($self, $ev, $x, $y) = @_; |
31 | my ($self, $ev, $x, $y) = @_; |
32 | |
32 | |
33 | $self->focus_in; |
33 | $self->focus_in; |
34 | |
34 | |
35 | if ($ev->button == 2) { |
35 | if ($ev->{button} == 2) { |
36 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
36 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
37 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
37 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
38 | |
38 | |
39 | $self->{motion} = sub { |
39 | $self->{motion} = sub { |
40 | my ($ev, $x, $y) = @_; |
40 | my ($ev, $x, $y) = @_; |
41 | |
41 | |
42 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
42 | ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
43 | |
44 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
44 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
45 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
45 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
46 | |
46 | |
47 | $self->update; |
47 | $self->update; |
… | |
… | |
77 | |
77 | |
78 | sub draw { |
78 | sub draw { |
79 | my ($self) = @_; |
79 | my ($self) = @_; |
80 | |
80 | |
81 | if (delete $self->{need_update}) { |
81 | if (delete $self->{need_update}) { |
82 | glNewList $self->{list}, GL_COMPILE; |
82 | glNewList $self->{list}; |
83 | |
83 | |
84 | if ($::MAP) { |
84 | if ($::MAP) { |
85 | my $sw = int $::WIDTH / 32; |
85 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}); |
86 | my $sh = int $::HEIGHT / 32; |
86 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}); |
87 | |
87 | |
|
|
88 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
89 | |
88 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
90 | 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; |
91 | my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
90 | |
92 | |
91 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
93 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
92 | |
94 | |
93 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
95 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
94 | |
96 | |
… | |
… | |
99 | GL_CONVOLUTION_2D, |
101 | GL_CONVOLUTION_2D, |
100 | GL_ALPHA, |
102 | GL_ALPHA, |
101 | 3, 3, |
103 | 3, 3, |
102 | GL_ALPHA, GL_FLOAT, |
104 | GL_ALPHA, GL_FLOAT, |
103 | pack "f*", |
105 | pack "f*", |
104 | 0.1, 0.1, 0.1, |
106 | 0.05, 0.13, 0.05, |
105 | 0.1, 0.2, 0.1, |
107 | 0.13, 0.30, 0.13, |
106 | 0.1, 0.1, 0.1, |
108 | 0.05, 0.13, 0.05, |
107 | ); |
109 | ); |
108 | glEnable GL_CONVOLUTION_2D; |
110 | glEnable GL_CONVOLUTION_2D; |
109 | } |
111 | } |
110 | |
112 | |
111 | $self->{fow_texture} = new CFClient::Texture |
113 | $self->{fow_texture} = new CFClient::Texture |
… | |
… | |
120 | glEnable GL_BLEND; |
122 | glEnable GL_BLEND; |
121 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
123 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
122 | glEnable GL_TEXTURE_2D; |
124 | glEnable GL_TEXTURE_2D; |
123 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
125 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
124 | |
126 | |
125 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
127 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
126 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
128 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
127 | |
129 | |
128 | glDisable GL_TEXTURE_2D; |
130 | glDisable GL_TEXTURE_2D; |
129 | glDisable GL_BLEND; |
131 | glDisable GL_BLEND; |
130 | } |
132 | } |
131 | |
133 | |
132 | # HACK BEGIN |
134 | # HACK BEGIN |
133 | { |
135 | { |
134 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
136 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
137 | |
|
|
138 | glTranslate 0, 30; |
135 | my ($w, $h) = (250, 250); |
139 | my ($w, $h) = (250, 250); |
|
|
140 | |
|
|
141 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
136 | |
142 | |
137 | glEnable GL_BLEND; |
143 | glEnable GL_BLEND; |
138 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
144 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
139 | glEnable GL_TEXTURE_2D; |
145 | glEnable GL_TEXTURE_2D; |
140 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
146 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
141 | |
147 | |
142 | $self->{mapmap_texture} = |
148 | $self->{mapmap_texture} = |
143 | new CFClient::Texture |
149 | new CFClient::Texture |
144 | w => $w, |
150 | w => $w, |
145 | h => $h, |
151 | h => $h, |
146 | data => $::MAP->mapmap ($w, $h), |
152 | data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h), |
147 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
153 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
148 | |
154 | |
149 | $self->{mapmap_texture}->draw_quad (100, 100); |
155 | $self->{mapmap_texture}->draw_quad (0, 0); |
150 | |
156 | |
151 | glDisable GL_TEXTURE_2D; |
157 | glDisable GL_TEXTURE_2D; |
|
|
158 | |
|
|
159 | glTranslate 0.375, 0.375; |
|
|
160 | |
|
|
161 | glColor 1, 1, 0, 1; |
|
|
162 | glBegin GL_LINE_LOOP; |
|
|
163 | glVertex $w * 0.5 - $sx , $h * 0.5 - $sy ; |
|
|
164 | glVertex $w * 0.5 - $sx , $h * 0.5 - $sy + $sh; |
|
|
165 | glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy + $sh; |
|
|
166 | glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy ; |
|
|
167 | glEnd; |
|
|
168 | |
152 | glDisable GL_BLEND; |
169 | glDisable GL_BLEND; |
153 | } |
170 | } |
154 | # HACK END |
171 | # HACK END |
155 | } |
172 | } |
156 | |
173 | |
… | |
… | |
174 | glDisable GL_COLOR_LOGIC_OP; |
191 | glDisable GL_COLOR_LOGIC_OP; |
175 | } |
192 | } |
176 | } |
193 | } |
177 | |
194 | |
178 | my %DIR = ( |
195 | my %DIR = ( |
179 | SDLK_KP8, [1, "north"], |
196 | CFClient::SDLK_KP8, [1, "north"], |
180 | SDLK_KP9, [2, "northeast"], |
197 | CFClient::SDLK_KP9, [2, "northeast"], |
181 | SDLK_KP6, [3, "east"], |
198 | CFClient::SDLK_KP6, [3, "east"], |
182 | SDLK_KP3, [4, "southeast"], |
199 | CFClient::SDLK_KP3, [4, "southeast"], |
183 | SDLK_KP2, [5, "south"], |
200 | CFClient::SDLK_KP2, [5, "south"], |
184 | SDLK_KP1, [6, "southwest"], |
201 | CFClient::SDLK_KP1, [6, "southwest"], |
185 | SDLK_KP4, [7, "west"], |
202 | CFClient::SDLK_KP4, [7, "west"], |
186 | SDLK_KP7, [8, "northwest"], |
203 | CFClient::SDLK_KP7, [8, "northwest"], |
187 | |
204 | |
188 | SDLK_UP, [1, "north"], |
205 | CFClient::SDLK_UP, [1, "north"], |
189 | SDLK_RIGHT, [3, "east"], |
206 | CFClient::SDLK_RIGHT, [3, "east"], |
190 | SDLK_DOWN, [5, "south"], |
207 | CFClient::SDLK_DOWN, [5, "south"], |
191 | SDLK_LEFT, [7, "west"], |
208 | CFClient::SDLK_LEFT, [7, "west"], |
192 | ); |
209 | ); |
193 | |
210 | |
194 | sub key_down { |
211 | sub key_down { |
195 | my ($self, $ev) = @_; |
212 | my ($self, $ev) = @_; |
196 | |
213 | |
197 | my $mod = $ev->key_mod; |
214 | my $mod = $ev->{mod}; |
198 | my $sym = $ev->key_sym; |
215 | my $sym = $ev->{sym}; |
199 | |
216 | |
200 | if ($sym == SDLK_KP5) { |
217 | if ($sym == CFClient::SDLK_KP5) { |
201 | $::CONN->user_send ("stay fire"); |
218 | $::CONN->user_send ("stay fire"); |
202 | } elsif ($sym == SDLK_a) { |
219 | } elsif ($sym == ord "a") { |
203 | $::CONN->user_send ("apply"); |
220 | $::CONN->user_send ("apply"); |
204 | } elsif ($sym == SDLK_QUOTE) { |
221 | } elsif ($sym == ord "'") { |
205 | $self->emit ('activate_console'); |
222 | $self->emit ('activate_console'); |
206 | } elsif ($sym == SDLK_SLASH) { |
223 | } elsif ($sym == ord "/") { |
207 | $self->emit ('activate_console' => '/'); |
224 | $self->emit (activate_console => '/'); |
208 | } elsif (exists $DIR{$sym}) { |
225 | } elsif (exists $DIR{$sym}) { |
209 | if ($mod & KMOD_SHIFT) { |
226 | if ($mod & CFClient::KMOD_SHIFT) { |
210 | $self->{shft}++; |
227 | $self->{shft}++; |
211 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
228 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
212 | } elsif ($mod & KMOD_CTRL) { |
229 | } elsif ($mod & CFClient::KMOD_CTRL) { |
213 | $self->{ctrl}++; |
230 | $self->{ctrl}++; |
214 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
231 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
215 | } else { |
232 | } else { |
216 | $::CONN->user_send ("$DIR{$sym}[1]"); |
233 | $::CONN->user_send ("$DIR{$sym}[1]"); |
217 | } |
234 | } |
|
|
235 | } elsif ($ev->{unicode}) { |
|
|
236 | $self->{command_widget} ||= |
|
|
237 | new CFClient::MapWidget::Command:: |
|
|
238 | command => $self->{command}, |
|
|
239 | can_focus => 1, |
|
|
240 | connect_execute => sub { |
|
|
241 | $::CONN->user_send ($_[1]); |
|
|
242 | }, |
|
|
243 | connect_close => sub { |
|
|
244 | (delete $self->{command_widget})->hide; |
|
|
245 | $self->focus_in; |
|
|
246 | }, |
|
|
247 | ; |
|
|
248 | $self->{command_widget}->key_down ($ev); |
|
|
249 | $self->{command_widget}->show; |
|
|
250 | $self->{command_widget}->focus_in; |
218 | } |
251 | } |
219 | } |
252 | } |
220 | |
253 | |
221 | sub key_up { |
254 | sub key_up { |
222 | my ($self, $ev) = @_; |
255 | my ($self, $ev) = @_; |
223 | |
256 | |
224 | my $mod = $ev->key_mod; |
257 | my $mod = $ev->{mod}; |
225 | my $sym = $ev->key_sym; |
258 | my $sym = $ev->{sym}; |
226 | |
259 | |
227 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
260 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
228 | $::CONN->user_send ("fire_stop"); |
261 | $::CONN->user_send ("fire_stop"); |
229 | } |
262 | } |
230 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
263 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
231 | $::CONN->user_send ("run_stop"); |
264 | $::CONN->user_send ("run_stop"); |
232 | } |
265 | } |
233 | } |
266 | } |
234 | |
267 | |
|
|
268 | sub add_command { |
|
|
269 | my ($self, $command, $widget, $cb) = @_; |
|
|
270 | |
|
|
271 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
|
|
272 | |
|
|
273 | push @{$self->{command}}, [$abbrev, $command]; |
|
|
274 | #warn "$command|$abbrev|$widget\n";#d# |
|
|
275 | } |
|
|
276 | |
|
|
277 | package CFClient::MapWidget::Command; |
|
|
278 | |
|
|
279 | use strict; |
|
|
280 | |
|
|
281 | use CFClient::OpenGL; |
|
|
282 | |
|
|
283 | our @ISA = CFClient::UI::VBox::; |
|
|
284 | |
|
|
285 | sub new { |
|
|
286 | my $class = shift; |
|
|
287 | |
|
|
288 | my $self = $class->SUPER::new ( |
|
|
289 | @_, |
|
|
290 | children => [map |
|
|
291 | CFClient::UI::Label->new ( |
|
|
292 | can_hover => 1, |
|
|
293 | fontsize => $_, |
|
|
294 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
|
|
295 | ], |
|
|
296 | ); |
|
|
297 | |
|
|
298 | $self |
|
|
299 | } |
|
|
300 | |
|
|
301 | sub size_allocate { |
|
|
302 | my ($self, $w, $h) = @_; |
|
|
303 | |
|
|
304 | $self->SUPER::size_allocate ($w, $h); |
|
|
305 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
|
|
306 | } |
|
|
307 | |
|
|
308 | sub update_labels { |
|
|
309 | my ($self) = @_; |
|
|
310 | |
|
|
311 | my $command = $self->{command}; |
|
|
312 | my $search_abbrev = qr/^\Q$self->{search}/; |
|
|
313 | my $search_full = qr/\Q$self->{search}/; |
|
|
314 | |
|
|
315 | my @found; |
|
|
316 | |
|
|
317 | for (@$command) { |
|
|
318 | if ($_->[0] =~ $search_abbrev) { |
|
|
319 | push @found, [$_->[0], $_]; |
|
|
320 | } elsif ($_[1] =~ $search_full) { |
|
|
321 | push @found, [$_->[1], $_]; |
|
|
322 | } |
|
|
323 | } |
|
|
324 | |
|
|
325 | @found = sort { $a->[0] cmp $b->[0] } @found; |
|
|
326 | |
|
|
327 | $self->{children}[0]->set_text ("$self->{search}_"); |
|
|
328 | |
|
|
329 | for (0..5) { |
|
|
330 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
331 | } |
|
|
332 | |
|
|
333 | $self->{select} = $found[0][1][1] |
|
|
334 | if @found; |
|
|
335 | |
|
|
336 | if (@found > 6) { |
|
|
337 | $self->{children}[6]->set_text ("..."); |
|
|
338 | } |
|
|
339 | |
|
|
340 | $self->check_size; |
|
|
341 | } |
|
|
342 | |
|
|
343 | sub key_down { |
|
|
344 | my ($self, $ev) = @_; |
|
|
345 | |
|
|
346 | if ($ev->{sym} == 8) { |
|
|
347 | substr $self->{search}, -1, 1, ""; |
|
|
348 | $self->update_labels; |
|
|
349 | } elsif ($ev->{sym} == 13) { |
|
|
350 | if (exists $self->{select}) { |
|
|
351 | $self->emit (execute => $self->{select}); |
|
|
352 | $self->emit ("close"); |
|
|
353 | } |
|
|
354 | } elsif ($ev->{sym} == 27) { |
|
|
355 | $self->emit ("close"); |
|
|
356 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
|
|
357 | $self->{search} .= chr $ev->{unicode}; |
|
|
358 | $self->update_labels; |
|
|
359 | } |
|
|
360 | |
|
|
361 | length $self->{search} |
|
|
362 | or $self->emit ("close"); |
|
|
363 | } |
|
|
364 | |
235 | 1 |
365 | 1 |