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 | |
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 | my $self = $class->SUPER::new ( |
15 | z => -1, |
16 | z => -1, |
16 | can_focus => 1, |
17 | can_focus => 1, |
17 | list => glGenList, |
18 | list => glGenList, |
18 | @_ |
19 | @_ |
19 | ) |
20 | ); |
|
|
21 | |
|
|
22 | $self->add_command ("killpets", "kills all your summoned and charmed creates"); |
|
|
23 | |
|
|
24 | $self |
20 | } |
25 | } |
21 | |
26 | |
22 | sub DESTROY { |
27 | sub DESTROY { |
23 | my $self = shift; |
28 | my $self = shift; |
24 | |
29 | |
… | |
… | |
80 | |
85 | |
81 | if (delete $self->{need_update}) { |
86 | if (delete $self->{need_update}) { |
82 | glNewList $self->{list}; |
87 | glNewList $self->{list}; |
83 | |
88 | |
84 | if ($::MAP) { |
89 | if ($::MAP) { |
85 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}); |
90 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
86 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}); |
91 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
87 | |
|
|
88 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
|
|
89 | |
92 | |
90 | my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
93 | 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; |
94 | my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
95 | |
|
|
96 | glPushMatrix; |
|
|
97 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
92 | |
98 | |
93 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
99 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
94 | |
100 | |
95 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
101 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
96 | |
102 | |
… | |
… | |
129 | |
135 | |
130 | glDisable GL_TEXTURE_2D; |
136 | glDisable GL_TEXTURE_2D; |
131 | glDisable GL_BLEND; |
137 | glDisable GL_BLEND; |
132 | } |
138 | } |
133 | |
139 | |
134 | # HACK BEGIN |
140 | 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 | } |
141 | } |
173 | |
142 | |
174 | glEndList; |
143 | glEndList; |
175 | } |
144 | } |
176 | |
145 | |
… | |
… | |
214 | my $mod = $ev->{mod}; |
183 | my $mod = $ev->{mod}; |
215 | my $sym = $ev->{sym}; |
184 | my $sym = $ev->{sym}; |
216 | |
185 | |
217 | if ($sym == CFClient::SDLK_KP5) { |
186 | if ($sym == CFClient::SDLK_KP5) { |
218 | $::CONN->user_send ("stay fire"); |
187 | $::CONN->user_send ("stay fire"); |
|
|
188 | } elsif ($sym == ord ",") { |
|
|
189 | $::CONN->user_send ("take"); |
219 | } elsif ($sym == ord "a") { |
190 | } elsif ($sym == ord "a") { |
220 | $::CONN->user_send ("apply"); |
191 | $::CONN->user_send ("apply"); |
221 | } elsif ($sym == ord "'") { |
192 | } elsif ($sym == ord "'") { |
222 | $self->emit ('activate_console'); |
193 | $self->emit ('activate_console'); |
223 | } elsif ($sym == ord "/") { |
194 | } elsif ($sym == ord "/") { |
… | |
… | |
236 | $self->{command_widget} ||= |
207 | $self->{command_widget} ||= |
237 | new CFClient::MapWidget::Command:: |
208 | new CFClient::MapWidget::Command:: |
238 | command => $self->{command}, |
209 | command => $self->{command}, |
239 | can_focus => 1, |
210 | can_focus => 1, |
240 | connect_execute => sub { |
211 | connect_execute => sub { |
|
|
212 | # todo: support callback instead of user_send |
241 | $::CONN->user_send ($_[1]); |
213 | $::CONN->user_send ($_[1][1]); |
242 | }, |
214 | }, |
243 | connect_close => sub { |
215 | connect_close => sub { |
244 | (delete $self->{command_widget})->hide; |
216 | (delete $self->{command_widget})->hide; |
245 | $self->focus_in; |
217 | $self->focus_in; |
246 | }, |
218 | }, |
247 | ; |
219 | ; |
248 | $self->{command_widget}->key_down ($ev); |
220 | $self->{command_widget}->key_down ($ev); |
|
|
221 | return unless $self->{command_widget}; |
249 | $self->{command_widget}->show; |
222 | $self->{command_widget}->show; |
250 | $self->{command_widget}->focus_in; |
223 | $self->{command_widget}->focus_in; |
251 | } |
224 | } |
252 | } |
225 | } |
253 | |
226 | |
… | |
… | |
264 | $::CONN->user_send ("run_stop"); |
237 | $::CONN->user_send ("run_stop"); |
265 | } |
238 | } |
266 | } |
239 | } |
267 | |
240 | |
268 | sub add_command { |
241 | sub add_command { |
269 | my ($self, $command, $widget, $cb) = @_; |
242 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
270 | |
243 | |
271 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
244 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
272 | |
245 | |
273 | push @{$self->{command}}, [$abbrev, $command]; |
246 | $self->{command} = [ |
274 | #warn "$command|$abbrev|$widget\n";#d# |
247 | [$abbrev, $command, $tooltip, $widget, $cb], |
|
|
248 | grep $_->[1] ne $command, @{ $self->{command} }, |
|
|
249 | ]; |
|
|
250 | } |
|
|
251 | |
|
|
252 | package CFClient::MapWidget::MapMap; |
|
|
253 | |
|
|
254 | our @ISA = CFClient::UI::Base::; |
|
|
255 | |
|
|
256 | use Time::HiRes qw(time); |
|
|
257 | use CFClient::OpenGL; |
|
|
258 | |
|
|
259 | sub size_request { |
|
|
260 | ($::HEIGHT * 0.25, $::HEIGHT * 0.25) |
|
|
261 | } |
|
|
262 | |
|
|
263 | sub size_allocate { |
|
|
264 | my ($self, $w, $h) = @_; |
|
|
265 | |
|
|
266 | $self->SUPER::size_allocate ($w, $h); |
|
|
267 | $self->update; |
|
|
268 | } |
|
|
269 | |
|
|
270 | sub update { |
|
|
271 | my ($self) = @_; |
|
|
272 | |
|
|
273 | delete $self->{texture_atime}; |
|
|
274 | $self->SUPER::update; |
|
|
275 | } |
|
|
276 | |
|
|
277 | sub _draw { |
|
|
278 | my ($self) = @_; |
|
|
279 | |
|
|
280 | $::MAP or return; |
|
|
281 | |
|
|
282 | my ($w, $h) = @$self{qw(w h)}; |
|
|
283 | |
|
|
284 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
|
|
285 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
|
|
286 | |
|
|
287 | my $sx = int $::CFG->{map_shift_x} / 32; |
|
|
288 | my $sy = int $::CFG->{map_shift_y} / 32; |
|
|
289 | |
|
|
290 | my $ox = 0.5 * ($w - $sw); |
|
|
291 | my $oy = 0.5 * ($h - $sh); |
|
|
292 | |
|
|
293 | glEnable GL_BLEND; |
|
|
294 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
295 | glEnable GL_TEXTURE_2D; |
|
|
296 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
297 | |
|
|
298 | if ($self->{texture_atime} < time) { |
|
|
299 | $self->{texture_atime} = time + 1/3; |
|
|
300 | |
|
|
301 | $self->{texture} = |
|
|
302 | new CFClient::Texture |
|
|
303 | w => $w, |
|
|
304 | h => $h, |
|
|
305 | data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
|
|
306 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
307 | } |
|
|
308 | |
|
|
309 | $self->{texture}->draw_quad (0, 0); |
|
|
310 | |
|
|
311 | glDisable GL_TEXTURE_2D; |
|
|
312 | |
|
|
313 | glTranslate 0.375, 0.375; |
|
|
314 | |
|
|
315 | #TODO: map scale is completely borked |
|
|
316 | |
|
|
317 | my $x0 = int $ox - $sx + 0.5; |
|
|
318 | my $y0 = int $oy - $sy + 0.5; |
|
|
319 | |
|
|
320 | glColor 1, 1, 0, 1; |
|
|
321 | glBegin GL_LINE_LOOP; |
|
|
322 | glVertex $x0 , $y0 ; |
|
|
323 | glVertex $x0 , $y0 + $sh; |
|
|
324 | glVertex $x0 + $sw, $y0 + $sh; |
|
|
325 | glVertex $x0 + $sw, $y0 ; |
|
|
326 | glEnd; |
|
|
327 | |
|
|
328 | glDisable GL_BLEND; |
275 | } |
329 | } |
276 | |
330 | |
277 | package CFClient::MapWidget::Command; |
331 | package CFClient::MapWidget::Command; |
278 | |
332 | |
279 | use strict; |
333 | use strict; |
… | |
… | |
287 | |
341 | |
288 | my $self = $class->SUPER::new ( |
342 | my $self = $class->SUPER::new ( |
289 | @_, |
343 | @_, |
290 | children => [map |
344 | children => [map |
291 | CFClient::UI::Label->new ( |
345 | CFClient::UI::Label->new ( |
292 | can_hover => 1, |
346 | can_hover => 1, |
|
|
347 | can_events => 1, |
293 | fontsize => $_, |
348 | fontsize => $_, |
294 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
349 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
295 | ], |
350 | ], |
296 | ); |
351 | ); |
297 | |
352 | |
298 | $self |
353 | $self |
… | |
… | |
315 | my @found; |
370 | my @found; |
316 | |
371 | |
317 | for (@$command) { |
372 | for (@$command) { |
318 | if ($_->[0] =~ $search_abbrev) { |
373 | if ($_->[0] =~ $search_abbrev) { |
319 | push @found, [$_->[0], $_]; |
374 | push @found, [$_->[0], $_]; |
320 | } elsif ($_[1] =~ $search_full) { |
375 | } elsif (2 < length $self->{search} and $_->[1] =~ $search_full) { |
321 | push @found, [$_->[1], $_]; |
376 | push @found, [$_->[1], $_]; |
322 | } |
377 | } |
323 | } |
378 | } |
324 | |
379 | |
325 | @found = sort { $a->[0] cmp $b->[0] } @found; |
380 | @found = sort { $a->[0] cmp $b->[0] } @found; |
326 | |
381 | |
327 | $self->{children}[0]->set_text ("$self->{search}_"); |
382 | $self->{children}[0]->set_text ("$self->{search}_"); |
328 | |
383 | |
329 | for (0..5) { |
384 | for (0..5) { |
330 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
385 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
386 | $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : ""); |
331 | } |
387 | } |
332 | |
388 | |
333 | $self->{select} = $found[0][1][1] |
389 | $self->{select} = $found[0][1] |
334 | if @found; |
390 | if @found; |
335 | |
391 | |
336 | if (@found > 6) { |
392 | if (@found > 6) { |
337 | $self->{children}[6]->set_text ("..."); |
393 | $self->{children}[6]->set_text ("..."); |
338 | } |
394 | } |
… | |
… | |
351 | $self->emit (execute => $self->{select}); |
407 | $self->emit (execute => $self->{select}); |
352 | $self->emit ("close"); |
408 | $self->emit ("close"); |
353 | } |
409 | } |
354 | } elsif ($ev->{sym} == 27) { |
410 | } elsif ($ev->{sym} == 27) { |
355 | $self->emit ("close"); |
411 | $self->emit ("close"); |
356 | } elsif ($ev->{unicode}) { |
412 | return; |
|
|
413 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
357 | $self->{search} .= chr $ev->{unicode}; |
414 | $self->{search} .= chr $ev->{unicode}; |
358 | $self->update_labels; |
415 | $self->update_labels; |
359 | } |
416 | } |
|
|
417 | |
|
|
418 | length $self->{search} |
|
|
419 | or $self->emit ("close"); |
360 | } |
420 | } |
361 | |
421 | |
362 | 1 |
422 | 1 |