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