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