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 * $::CFG->{map_scale}); |
86 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
86 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}); |
87 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
87 | |
|
|
88 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
89 | |
88 | |
90 | my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; 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; |
91 | my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; 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}; |
92 | |
94 | |
93 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
95 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
94 | |
96 | |
95 | 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); |
96 | |
98 | |
… | |
… | |
129 | |
131 | |
130 | glDisable GL_TEXTURE_2D; |
132 | glDisable GL_TEXTURE_2D; |
131 | glDisable GL_BLEND; |
133 | glDisable GL_BLEND; |
132 | } |
134 | } |
133 | |
135 | |
134 | # HACK BEGIN |
136 | glPopMatrix; |
135 | { |
|
|
136 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
137 | |
|
|
138 | glTranslate 0, 30; |
|
|
139 | my ($w, $h) = (250, 250); |
|
|
140 | |
|
|
141 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
|
|
142 | |
|
|
143 | glEnable GL_BLEND; |
|
|
144 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
145 | glEnable GL_TEXTURE_2D; |
|
|
146 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
147 | |
|
|
148 | $self->{mapmap_texture} = |
|
|
149 | new CFClient::Texture |
|
|
150 | w => $w, |
|
|
151 | h => $h, |
|
|
152 | data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h), |
|
|
153 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
154 | |
|
|
155 | $self->{mapmap_texture}->draw_quad (0, 0); |
|
|
156 | |
|
|
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 | |
|
|
169 | glDisable GL_BLEND; |
|
|
170 | } |
|
|
171 | # HACK END |
|
|
172 | } |
137 | } |
173 | |
138 | |
174 | glEndList; |
139 | glEndList; |
175 | } |
140 | } |
176 | |
141 | |
… | |
… | |
214 | my $mod = $ev->{mod}; |
179 | my $mod = $ev->{mod}; |
215 | my $sym = $ev->{sym}; |
180 | my $sym = $ev->{sym}; |
216 | |
181 | |
217 | if ($sym == CFClient::SDLK_KP5) { |
182 | if ($sym == CFClient::SDLK_KP5) { |
218 | $::CONN->user_send ("stay fire"); |
183 | $::CONN->user_send ("stay fire"); |
|
|
184 | } elsif ($sym == ord ",") { |
|
|
185 | $::CONN->user_send ("take"); |
219 | } elsif ($sym == ord "a") { |
186 | } elsif ($sym == ord "a") { |
220 | $::CONN->user_send ("apply"); |
187 | $::CONN->user_send ("apply"); |
221 | } elsif ($sym == ord "'") { |
188 | } elsif ($sym == ord "'") { |
222 | $self->emit ('activate_console'); |
189 | $self->emit ('activate_console'); |
223 | } elsif ($sym == ord "/") { |
190 | } elsif ($sym == ord "/") { |
… | |
… | |
236 | $self->{command_widget} ||= |
203 | $self->{command_widget} ||= |
237 | new CFClient::MapWidget::Command:: |
204 | new CFClient::MapWidget::Command:: |
238 | command => $self->{command}, |
205 | command => $self->{command}, |
239 | can_focus => 1, |
206 | can_focus => 1, |
240 | connect_execute => sub { |
207 | connect_execute => sub { |
|
|
208 | # todo: support callback instead of user_send |
241 | $::CONN->user_send ($_[1]); |
209 | $::CONN->user_send ($_[1][1]); |
242 | }, |
210 | }, |
243 | connect_close => sub { |
211 | connect_close => sub { |
244 | (delete $self->{command_widget})->hide; |
212 | (delete $self->{command_widget})->hide; |
245 | $self->focus_in; |
213 | $self->focus_in; |
246 | }, |
214 | }, |
247 | ; |
215 | ; |
248 | $self->{command_widget}->key_down ($ev); |
216 | $self->{command_widget}->key_down ($ev); |
|
|
217 | return unless $self->{command_widget}; |
249 | $self->{command_widget}->show; |
218 | $self->{command_widget}->show; |
250 | $self->{command_widget}->focus_in; |
219 | $self->{command_widget}->focus_in; |
251 | } |
220 | } |
252 | } |
221 | } |
253 | |
222 | |
… | |
… | |
264 | $::CONN->user_send ("run_stop"); |
233 | $::CONN->user_send ("run_stop"); |
265 | } |
234 | } |
266 | } |
235 | } |
267 | |
236 | |
268 | sub add_command { |
237 | sub add_command { |
269 | my ($self, $command, $widget, $cb) = @_; |
238 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
270 | |
239 | |
271 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
240 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
272 | |
241 | |
273 | push @{$self->{command}}, [$abbrev, $command]; |
242 | push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; |
274 | #warn "$command|$abbrev|$widget\n";#d# |
243 | } |
|
|
244 | |
|
|
245 | package CFClient::MapWidget::MapMap; |
|
|
246 | |
|
|
247 | use base 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; |
275 | } |
322 | } |
276 | |
323 | |
277 | package CFClient::MapWidget::Command; |
324 | package CFClient::MapWidget::Command; |
278 | |
325 | |
279 | use strict; |
326 | use strict; |
… | |
… | |
287 | |
334 | |
288 | my $self = $class->SUPER::new ( |
335 | my $self = $class->SUPER::new ( |
289 | @_, |
336 | @_, |
290 | children => [map |
337 | children => [map |
291 | CFClient::UI::Label->new ( |
338 | CFClient::UI::Label->new ( |
292 | can_hover => 1, |
339 | can_hover => 1, |
|
|
340 | can_events => 1, |
293 | fontsize => $_, |
341 | fontsize => $_, |
294 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
342 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
295 | ], |
343 | ], |
296 | ); |
344 | ); |
297 | |
345 | |
298 | $self |
346 | $self |
… | |
… | |
315 | my @found; |
363 | my @found; |
316 | |
364 | |
317 | for (@$command) { |
365 | for (@$command) { |
318 | if ($_->[0] =~ $search_abbrev) { |
366 | if ($_->[0] =~ $search_abbrev) { |
319 | push @found, [$_->[0], $_]; |
367 | push @found, [$_->[0], $_]; |
320 | } elsif ($_[1] =~ $search_full) { |
368 | } elsif ($_->[1] =~ $search_full) { |
321 | push @found, [$_->[1], $_]; |
369 | push @found, [$_->[1], $_]; |
322 | } |
370 | } |
323 | } |
371 | } |
324 | |
372 | |
325 | @found = sort { $a->[0] cmp $b->[0] } @found; |
373 | @found = sort { $a->[0] cmp $b->[0] } @found; |
326 | |
374 | |
327 | $self->{children}[0]->set_text ("$self->{search}_"); |
375 | $self->{children}[0]->set_text ("$self->{search}_"); |
328 | |
376 | |
329 | for (0..5) { |
377 | for (0..5) { |
330 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
378 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
379 | $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : ""); |
331 | } |
380 | } |
332 | |
381 | |
333 | $self->{select} = $found[0][1][1] |
382 | $self->{select} = $found[0][1] |
334 | if @found; |
383 | if @found; |
335 | |
384 | |
336 | if (@found > 6) { |
385 | if (@found > 6) { |
337 | $self->{children}[6]->set_text ("..."); |
386 | $self->{children}[6]->set_text ("..."); |
338 | } |
387 | } |
… | |
… | |
351 | $self->emit (execute => $self->{select}); |
400 | $self->emit (execute => $self->{select}); |
352 | $self->emit ("close"); |
401 | $self->emit ("close"); |
353 | } |
402 | } |
354 | } elsif ($ev->{sym} == 27) { |
403 | } elsif ($ev->{sym} == 27) { |
355 | $self->emit ("close"); |
404 | $self->emit ("close"); |
|
|
405 | return; |
356 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
406 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
357 | $self->{search} .= chr $ev->{unicode}; |
407 | $self->{search} .= chr $ev->{unicode}; |
358 | $self->update_labels; |
408 | $self->update_labels; |
359 | } |
409 | } |
360 | |
410 | |