… | |
… | |
10 | our @ISA = CFClient::UI::Base::; |
10 | our @ISA = CFClient::UI::Base::; |
11 | |
11 | |
12 | sub new { |
12 | sub new { |
13 | my $class = shift; |
13 | my $class = shift; |
14 | |
14 | |
15 | $class->SUPER::new ( |
15 | my $self = $class->SUPER::new ( |
16 | z => -1, |
16 | z => -1, |
17 | can_focus => 1, |
17 | can_focus => 1, |
18 | list => glGenList, |
18 | list => glGenList, |
19 | @_ |
19 | @_ |
20 | ) |
20 | ); |
21 | } |
|
|
22 | |
21 | |
23 | sub DESTROY { |
22 | $self->{completer} = new CFClient::MapWidget::Command:: |
|
|
23 | command => $self->{command}, |
|
|
24 | can_focus => 1, |
|
|
25 | ; |
|
|
26 | |
|
|
27 | $self |
|
|
28 | } |
|
|
29 | |
|
|
30 | sub add_command { |
|
|
31 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
|
|
32 | |
|
|
33 | (my $data = $command) =~ s/\\//g; |
|
|
34 | |
|
|
35 | $tooltip =~ s/^\s+//; |
|
|
36 | |
|
|
37 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
|
|
38 | |
|
|
39 | $tooltip =~ s/\s+$//; |
|
|
40 | |
|
|
41 | $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
|
|
42 | } |
|
|
43 | |
|
|
44 | sub clr_commands { |
24 | my $self = shift; |
45 | my ($self) = @_; |
25 | |
46 | |
26 | glDeleteList $self->{list}; |
47 | %{$self->{completer}{command}} = (); |
27 | |
|
|
28 | $self->SUPER::DESTROY; |
|
|
29 | } |
48 | } |
30 | |
49 | |
31 | sub button_down { |
50 | sub button_down { |
32 | my ($self, $ev, $x, $y) = @_; |
51 | my ($self, $ev, $x, $y) = @_; |
33 | |
52 | |
… | |
… | |
74 | |
93 | |
75 | $self->{need_update} = 1; |
94 | $self->{need_update} = 1; |
76 | $self->SUPER::update; |
95 | $self->SUPER::update; |
77 | } |
96 | } |
78 | |
97 | |
|
|
98 | my %DIR = ( |
|
|
99 | CFClient::SDLK_KP8, [1, "north"], |
|
|
100 | CFClient::SDLK_KP9, [2, "northeast"], |
|
|
101 | CFClient::SDLK_KP6, [3, "east"], |
|
|
102 | CFClient::SDLK_KP3, [4, "southeast"], |
|
|
103 | CFClient::SDLK_KP2, [5, "south"], |
|
|
104 | CFClient::SDLK_KP1, [6, "southwest"], |
|
|
105 | CFClient::SDLK_KP4, [7, "west"], |
|
|
106 | CFClient::SDLK_KP7, [8, "northwest"], |
|
|
107 | |
|
|
108 | CFClient::SDLK_UP, [1, "north"], |
|
|
109 | CFClient::SDLK_RIGHT, [3, "east"], |
|
|
110 | CFClient::SDLK_DOWN, [5, "south"], |
|
|
111 | CFClient::SDLK_LEFT, [7, "west"], |
|
|
112 | ); |
|
|
113 | |
|
|
114 | sub key_down { |
|
|
115 | my ($self, $ev) = @_; |
|
|
116 | |
|
|
117 | return unless $::CONN; |
|
|
118 | |
|
|
119 | my $mod = $ev->{mod}; |
|
|
120 | my $sym = $ev->{sym}; |
|
|
121 | my $uni = $ev->{unicode}; |
|
|
122 | |
|
|
123 | if ($sym == CFClient::SDLK_KP5) { |
|
|
124 | $::CONN->user_send ("stay fire"); |
|
|
125 | } elsif ($uni == ord ",") { |
|
|
126 | $::CONN->user_send ("take"); |
|
|
127 | } elsif ($uni == ord " ") { |
|
|
128 | $::CONN->user_send ("apply"); |
|
|
129 | } elsif ($uni == ord "\t") { |
|
|
130 | # TODO: toggle inventory |
|
|
131 | } elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") { |
|
|
132 | $::CONN->user_send ("rotateshoottype +"); |
|
|
133 | } elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") { |
|
|
134 | $::CONN->user_send ("rotateshoottype -"); |
|
|
135 | } elsif ($uni == ord '"') { |
|
|
136 | $self->{completer}->set_prefix ("$::CFG->{say_command} "); |
|
|
137 | $self->{completer}->show; |
|
|
138 | } elsif ($uni == ord "'") { |
|
|
139 | $self->{completer}->set_prefix (""); |
|
|
140 | $self->{completer}->show; |
|
|
141 | } elsif (exists $DIR{$sym}) { |
|
|
142 | if ($mod & CFClient::KMOD_SHIFT) { |
|
|
143 | $self->{shft}++; |
|
|
144 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
|
|
145 | } elsif ($mod & CFClient::KMOD_CTRL) { |
|
|
146 | $self->{ctrl}++; |
|
|
147 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
|
|
148 | } else { |
|
|
149 | $::CONN->user_send ("$DIR{$sym}[1]"); |
|
|
150 | } |
|
|
151 | } elsif ($ev->{unicode}) { |
|
|
152 | $self->{completer}->key_down ($ev); |
|
|
153 | $self->{completer}->show; |
|
|
154 | } |
|
|
155 | } |
|
|
156 | |
|
|
157 | sub key_up { |
|
|
158 | my ($self, $ev) = @_; |
|
|
159 | |
|
|
160 | my $mod = $ev->{mod}; |
|
|
161 | my $sym = $ev->{sym}; |
|
|
162 | |
|
|
163 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
|
|
164 | $::CONN->user_send ("fire_stop"); |
|
|
165 | } |
|
|
166 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
167 | $::CONN->user_send ("run_stop"); |
|
|
168 | } |
|
|
169 | } |
|
|
170 | |
79 | sub draw { |
171 | sub draw { |
80 | my ($self) = @_; |
172 | my ($self) = @_; |
|
|
173 | |
|
|
174 | my $focused = $CFClient::UI::FOCUS == $self |
|
|
175 | || $CFClient::UI::FOCUS == $self->{completer}{entry}; |
|
|
176 | |
|
|
177 | return |
|
|
178 | unless $focused || !$::FAST; |
81 | |
179 | |
82 | if (delete $self->{need_update}) { |
180 | if (delete $self->{need_update}) { |
83 | glNewList $self->{list}; |
181 | glNewList $self->{list}; |
84 | |
182 | |
85 | if ($::MAP) { |
183 | if ($::MAP) { |
86 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
184 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
87 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
185 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
88 | |
186 | |
|
|
187 | my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
188 | my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
189 | |
89 | glPushMatrix; |
190 | glPushMatrix; |
90 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
191 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
91 | |
|
|
92 | my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
93 | my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
94 | |
192 | |
95 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
193 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
96 | |
194 | |
97 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
195 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
98 | |
196 | |
… | |
… | |
110 | 0.05, 0.13, 0.05, |
208 | 0.05, 0.13, 0.05, |
111 | ); |
209 | ); |
112 | glEnable GL_CONVOLUTION_2D; |
210 | glEnable GL_CONVOLUTION_2D; |
113 | } |
211 | } |
114 | |
212 | |
|
|
213 | $self->{fow_texture_name} ||= glGenTexture; |
|
|
214 | # try to re-use the texture name: TODO improve texture class instead |
|
|
215 | |
115 | $self->{fow_texture} = new CFClient::Texture |
216 | $self->{fow_texture} = new CFClient::Texture |
116 | w => $w, |
217 | w => $w, |
117 | h => $h, |
218 | h => $h, |
118 | data => $data, |
219 | data => $data, |
|
|
220 | name => $self->{fow_texture_name}, |
119 | internalformat => GL_ALPHA, |
221 | internalformat => GL_ALPHA, |
120 | format => GL_ALPHA; |
222 | format => GL_ALPHA; |
121 | |
223 | |
122 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
224 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
123 | |
225 | |
124 | glEnable GL_BLEND; |
|
|
125 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
126 | glEnable GL_TEXTURE_2D; |
226 | glEnable GL_TEXTURE_2D; |
127 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
227 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
128 | |
228 | |
129 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
229 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
130 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
230 | $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32); |
131 | |
231 | |
132 | glDisable GL_TEXTURE_2D; |
232 | glDisable GL_TEXTURE_2D; |
133 | glDisable GL_BLEND; |
|
|
134 | } |
233 | } |
135 | |
234 | |
136 | # HACK BEGIN |
|
|
137 | { |
|
|
138 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
139 | |
|
|
140 | glTranslate 0, 30; |
|
|
141 | my ($w, $h) = (250, 250); |
|
|
142 | |
|
|
143 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
|
|
144 | |
|
|
145 | glEnable GL_BLEND; |
|
|
146 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
147 | glEnable GL_TEXTURE_2D; |
|
|
148 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
149 | |
|
|
150 | glPopMatrix; |
235 | glPopMatrix; |
151 | |
|
|
152 | $self->{mapmap_texture} = |
|
|
153 | new CFClient::Texture |
|
|
154 | w => $w, |
|
|
155 | h => $h, |
|
|
156 | data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h), |
|
|
157 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
158 | |
|
|
159 | $self->{mapmap_texture}->draw_quad (0, 0); |
|
|
160 | |
|
|
161 | glDisable GL_TEXTURE_2D; |
|
|
162 | |
|
|
163 | glTranslate 0.375, 0.375; |
|
|
164 | |
|
|
165 | glColor 1, 1, 0, 1; |
|
|
166 | glBegin GL_LINE_LOOP; |
|
|
167 | glVertex $w * 0.5 - $sx , $h * 0.5 - $sy ; |
|
|
168 | glVertex $w * 0.5 - $sx , $h * 0.5 - $sy + $sh; |
|
|
169 | glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy + $sh; |
|
|
170 | glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy ; |
|
|
171 | glEnd; |
|
|
172 | |
|
|
173 | glDisable GL_BLEND; |
|
|
174 | } |
|
|
175 | # HACK END |
|
|
176 | } |
236 | } |
177 | |
237 | |
178 | glEndList; |
238 | glEndList; |
179 | } |
239 | } |
180 | |
240 | |
181 | glPushMatrix; |
241 | glPushMatrix; |
182 | glCallList $self->{list}; |
242 | glCallList $self->{list}; |
183 | glPopMatrix; |
243 | glPopMatrix; |
184 | |
244 | |
185 | if ($CFClient::UI::FOCUS != $self) { |
245 | # TNT2 emulates logops in software (or worse :) |
186 | glColor 64/255, 64/255, 64/255; |
246 | if ($focused) { |
187 | glLogicOp GL_AND; |
247 | (delete $self->{out_of_focus})->destroy |
188 | glEnable GL_COLOR_LOGIC_OP; |
248 | if $self->{out_of_focus}; |
|
|
249 | } else { |
|
|
250 | glColor 0.4, 0.2, 0.2, 0.6; |
|
|
251 | glEnable GL_BLEND; |
|
|
252 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
189 | glBegin GL_QUADS; |
253 | glBegin GL_QUADS; |
190 | glVertex 0, 0; |
254 | glVertex 0, 0; |
191 | glVertex 0, $::HEIGHT; |
255 | glVertex 0, $::HEIGHT; |
192 | glVertex $::WIDTH, $::HEIGHT; |
256 | glVertex $::WIDTH, $::HEIGHT; |
193 | glVertex $::WIDTH, 0; |
257 | glVertex $::WIDTH, 0; |
194 | glEnd; |
258 | glEnd; |
195 | glDisable GL_COLOR_LOGIC_OP; |
259 | glDisable GL_BLEND; |
|
|
260 | |
|
|
261 | $self->{out_of_focus} ||= do { |
|
|
262 | my $label = new CFClient::UI::Label |
|
|
263 | x => 0, |
|
|
264 | y => 0, |
|
|
265 | z => 1, |
|
|
266 | ellipsise => 0, |
|
|
267 | text => "map out of focus (click map to play)"; |
|
|
268 | |
|
|
269 | $label->show; |
|
|
270 | $label->update; |
|
|
271 | |
|
|
272 | $CFClient::UI::ROOT->on_post_alloc ("$self$label" => sub { |
|
|
273 | $label->move ( |
|
|
274 | ($::WIDTH - $label->{w}) * 0.5, |
|
|
275 | ($::HEIGHT - $label->{h}) * 0.5, |
|
|
276 | ); |
|
|
277 | }); |
|
|
278 | |
|
|
279 | $label |
|
|
280 | }; |
|
|
281 | } |
|
|
282 | } |
|
|
283 | |
|
|
284 | sub DESTROY { |
|
|
285 | my $self = shift; |
|
|
286 | |
|
|
287 | glDeleteList $self->{list}; |
|
|
288 | |
|
|
289 | $self->SUPER::DESTROY; |
|
|
290 | } |
|
|
291 | |
|
|
292 | package CFClient::MapWidget::MapMap; |
|
|
293 | |
|
|
294 | our @ISA = CFClient::UI::Base::; |
|
|
295 | |
|
|
296 | use Time::HiRes qw(time); |
|
|
297 | use CFClient::OpenGL; |
|
|
298 | |
|
|
299 | sub size_request { |
|
|
300 | ($::HEIGHT * 0.25, $::HEIGHT * 0.25) |
|
|
301 | } |
|
|
302 | |
|
|
303 | sub size_allocate { |
|
|
304 | my ($self, $w, $h) = @_; |
|
|
305 | |
|
|
306 | $self->SUPER::size_allocate ($w, $h); |
|
|
307 | $self->update; |
|
|
308 | } |
|
|
309 | |
|
|
310 | sub update { |
|
|
311 | my ($self) = @_; |
|
|
312 | |
|
|
313 | delete $self->{texture_atime}; |
|
|
314 | $self->SUPER::update; |
|
|
315 | } |
|
|
316 | |
|
|
317 | sub _draw { |
|
|
318 | my ($self) = @_; |
|
|
319 | |
|
|
320 | $::MAP or return; |
|
|
321 | |
|
|
322 | my ($w, $h) = @$self{qw(w h)}; |
|
|
323 | |
|
|
324 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
|
|
325 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
|
|
326 | |
|
|
327 | my $sx = int $::CFG->{map_shift_x} / 32; |
|
|
328 | my $sy = int $::CFG->{map_shift_y} / 32; |
|
|
329 | |
|
|
330 | my $ox = 0.5 * ($w - $sw); |
|
|
331 | my $oy = 0.5 * ($h - $sh); |
|
|
332 | |
|
|
333 | glEnable GL_BLEND; |
|
|
334 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
335 | glEnable GL_TEXTURE_2D; |
|
|
336 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
337 | |
|
|
338 | if ($self->{texture_atime} < time) { |
|
|
339 | $self->{texture_atime} = time + 1/3; |
|
|
340 | |
|
|
341 | $self->{texture} = |
|
|
342 | new CFClient::Texture |
|
|
343 | w => $w, |
|
|
344 | h => $h, |
|
|
345 | data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
|
|
346 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
347 | } |
|
|
348 | |
|
|
349 | $self->{texture}->draw_quad (0, 0); |
|
|
350 | |
|
|
351 | glDisable GL_TEXTURE_2D; |
|
|
352 | |
|
|
353 | glTranslate 0.375, 0.375; |
|
|
354 | |
|
|
355 | #TODO: map scale is completely borked |
|
|
356 | |
|
|
357 | my $x0 = int $ox - $sx + 0.5; |
|
|
358 | my $y0 = int $oy - $sy + 0.5; |
|
|
359 | |
|
|
360 | glColor 1, 1, 0, 1; |
|
|
361 | glBegin GL_LINE_LOOP; |
|
|
362 | glVertex $x0 , $y0 ; |
|
|
363 | glVertex $x0 , $y0 + $sh; |
|
|
364 | glVertex $x0 + $sw, $y0 + $sh; |
|
|
365 | glVertex $x0 + $sw, $y0 ; |
|
|
366 | glEnd; |
196 | } |
367 | |
|
|
368 | glDisable GL_BLEND; |
197 | } |
369 | } |
198 | |
370 | |
199 | my %DIR = ( |
371 | package CFClient::MapWidget::Command; |
200 | CFClient::SDLK_KP8, [1, "north"], |
|
|
201 | CFClient::SDLK_KP9, [2, "northeast"], |
|
|
202 | CFClient::SDLK_KP6, [3, "east"], |
|
|
203 | CFClient::SDLK_KP3, [4, "southeast"], |
|
|
204 | CFClient::SDLK_KP2, [5, "south"], |
|
|
205 | CFClient::SDLK_KP1, [6, "southwest"], |
|
|
206 | CFClient::SDLK_KP4, [7, "west"], |
|
|
207 | CFClient::SDLK_KP7, [8, "northwest"], |
|
|
208 | |
372 | |
209 | CFClient::SDLK_UP, [1, "north"], |
373 | use strict; |
210 | CFClient::SDLK_RIGHT, [3, "east"], |
374 | |
211 | CFClient::SDLK_DOWN, [5, "south"], |
375 | use CFClient::OpenGL; |
212 | CFClient::SDLK_LEFT, [7, "west"], |
376 | |
213 | ); |
377 | our @ISA = CFClient::UI::Frame::; |
|
|
378 | |
|
|
379 | sub new { |
|
|
380 | my $class = shift; |
|
|
381 | |
|
|
382 | my $self = $class->SUPER::new ( |
|
|
383 | bg => [0, 0, 0, 0.8], |
|
|
384 | @_, |
|
|
385 | ); |
|
|
386 | |
|
|
387 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
388 | |
|
|
389 | $self->{label} = [ |
|
|
390 | map |
|
|
391 | CFClient::UI::Label->new ( |
|
|
392 | can_hover => 1, |
|
|
393 | can_events => 1, |
|
|
394 | tooltip_width => 0.33, |
|
|
395 | fontsize => $_, |
|
|
396 | ), (0.8) x 16 |
|
|
397 | ]; |
|
|
398 | |
|
|
399 | $self->{entry} = new CFClient::UI::Entry |
|
|
400 | connect_changed => sub { |
|
|
401 | $self->update_labels; |
|
|
402 | }, |
|
|
403 | connect_key_down => sub { |
|
|
404 | my ($entry, $ev) = @_; |
|
|
405 | |
|
|
406 | my $self = $entry->{parent}{parent}; |
|
|
407 | |
|
|
408 | if ($ev->{sym} == 13) { |
|
|
409 | if (exists $self->{select}) { |
|
|
410 | $::CONN->user_send ($self->{select}); |
|
|
411 | $self->hide; |
|
|
412 | } |
|
|
413 | } elsif ($ev->{sym} == 27) { |
|
|
414 | $self->hide; |
|
|
415 | return; |
|
|
416 | } elsif ($ev->{sym} == CFClient::SDLK_DOWN) { |
|
|
417 | ++$self->{select_offset} |
|
|
418 | if $self->{select_offset} < $#{ $self->{last_match} || [] }; |
|
|
419 | $self->update_labels; |
|
|
420 | } elsif ($ev->{sym} == CFClient::SDLK_UP) { |
|
|
421 | --$self->{select_offset} |
|
|
422 | if $self->{select_offset}; |
|
|
423 | $self->update_labels; |
|
|
424 | } else { |
|
|
425 | return 0; |
|
|
426 | } |
|
|
427 | |
|
|
428 | 1 |
|
|
429 | } |
|
|
430 | ; |
|
|
431 | |
|
|
432 | $self->{vbox}->add ( |
|
|
433 | $self->{entry}, |
|
|
434 | @{$self->{label}}, |
|
|
435 | ); |
|
|
436 | |
|
|
437 | $self |
|
|
438 | } |
|
|
439 | |
|
|
440 | sub set_prefix { |
|
|
441 | my ($self, $prefix) = @_; |
|
|
442 | |
|
|
443 | $self->{entry}->set_text ($prefix); |
|
|
444 | $self->show; |
|
|
445 | } |
|
|
446 | |
|
|
447 | sub size_allocate { |
|
|
448 | my ($self, $w, $h) = @_; |
|
|
449 | |
|
|
450 | $self->SUPER::size_allocate ($w, $h); |
|
|
451 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
|
|
452 | } |
|
|
453 | |
|
|
454 | sub show { |
|
|
455 | my ($self) = @_; |
|
|
456 | |
|
|
457 | $self->SUPER::show; |
|
|
458 | $self->{entry}->focus_in; |
|
|
459 | } |
|
|
460 | |
|
|
461 | sub hide { |
|
|
462 | my ($self) = @_; |
|
|
463 | |
|
|
464 | $self->SUPER::hide; |
|
|
465 | $self->{entry}->set_text (""); |
|
|
466 | } |
214 | |
467 | |
215 | sub key_down { |
468 | sub key_down { |
216 | my ($self, $ev) = @_; |
469 | my ($self, $ev) = @_; |
217 | |
470 | |
218 | my $mod = $ev->{mod}; |
471 | $self->{entry}->key_down ($ev); |
219 | my $sym = $ev->{sym}; |
472 | } |
220 | |
473 | |
221 | if ($sym == CFClient::SDLK_KP5) { |
474 | sub update_labels { |
222 | $::CONN->user_send ("stay fire"); |
475 | my ($self) = @_; |
223 | } elsif ($sym == ord ",") { |
476 | |
224 | $::CONN->user_send ("take"); |
477 | my $text = $self->{entry}->get_text; |
225 | } elsif ($sym == ord "a") { |
478 | |
226 | $::CONN->user_send ("apply"); |
479 | length $text |
227 | } elsif ($sym == ord "'") { |
480 | or return $self->hide; |
228 | $self->emit ('activate_console'); |
481 | |
229 | } elsif ($sym == ord "/") { |
482 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
230 | $self->emit (activate_console => '/'); |
483 | |
231 | } elsif (exists $DIR{$sym}) { |
484 | if ($text ne $self->{last_search}) { |
232 | if ($mod & CFClient::KMOD_SHIFT) { |
485 | my @match; |
233 | $self->{shft}++; |
486 | |
234 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
487 | if ($text =~ /^(.*?)\s+$/) { |
235 | } elsif ($mod & CFClient::KMOD_CTRL) { |
488 | @match = [$cmd, "(appended whitespace suppresses completion)"]; |
236 | $self->{ctrl}++; |
|
|
237 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
|
|
238 | } else { |
489 | } else { |
239 | $::CONN->user_send ("$DIR{$sym}[1]"); |
490 | my $regexp = do { |
|
|
491 | my ($beg, @chr) = split //, lc $cmd; |
|
|
492 | |
|
|
493 | # the following regex is used to match our "completion entry" |
|
|
494 | # to an actual command - the parentheses match kind of "overhead" |
|
|
495 | # - the more characters the parentheses match, the less attractive |
|
|
496 | # is the match. |
|
|
497 | my $regexp = "^\Q$beg\E" |
|
|
498 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
|
|
499 | qr<$regexp> |
|
|
500 | }; |
|
|
501 | |
|
|
502 | my @penalty; |
|
|
503 | |
|
|
504 | for (keys %{$self->{command}}) { |
|
|
505 | if (@penalty = $_ =~ $regexp) { |
|
|
506 | push @match, [$_, length join "", map "::$_", grep defined, @penalty]; |
|
|
507 | } |
|
|
508 | } |
|
|
509 | |
|
|
510 | @match = map $self->{command}{$_->[0]}, |
|
|
511 | sort { |
|
|
512 | $a->[1] <=> $b->[1] |
|
|
513 | or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4] |
|
|
514 | or (length $a->[0]) <=> (length $b->[0]) |
|
|
515 | } @match; |
240 | } |
516 | } |
241 | } elsif ($ev->{unicode}) { |
|
|
242 | $self->{command_widget} ||= |
|
|
243 | new CFClient::MapWidget::Command:: |
|
|
244 | command => $self->{command}, |
|
|
245 | can_focus => 1, |
|
|
246 | connect_execute => sub { |
|
|
247 | # todo: support callback instead of user_send |
|
|
248 | $::CONN->user_send ($_[1][1]); |
|
|
249 | }, |
|
|
250 | connect_close => sub { |
|
|
251 | (delete $self->{command_widget})->hide; |
|
|
252 | $self->focus_in; |
|
|
253 | }, |
|
|
254 | ; |
|
|
255 | $self->{command_widget}->key_down ($ev); |
|
|
256 | return unless $self->{command_widget}; |
|
|
257 | $self->{command_widget}->show; |
|
|
258 | $self->{command_widget}->focus_in; |
|
|
259 | } |
|
|
260 | } |
|
|
261 | |
517 | |
262 | sub key_up { |
518 | $self->{last_search} = $cmd; |
263 | my ($self, $ev) = @_; |
519 | $self->{last_match} = \@match; |
264 | |
520 | |
265 | my $mod = $ev->{mod}; |
521 | $self->{select_offset} = 0; |
266 | my $sym = $ev->{sym}; |
|
|
267 | |
|
|
268 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
|
|
269 | $::CONN->user_send ("fire_stop"); |
|
|
270 | } |
522 | } |
271 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
272 | $::CONN->user_send ("run_stop"); |
|
|
273 | } |
|
|
274 | } |
|
|
275 | |
523 | |
276 | sub add_command { |
524 | my @labels = @{ $self->{label} }; |
277 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
525 | my @matches = @{ $self->{last_match} || [] }; |
278 | |
526 | |
279 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
527 | if ($self->{select_offset}) { |
|
|
528 | splice @matches, 0, $self->{select_offset}, (); |
280 | |
529 | |
281 | push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; |
530 | my $label = shift @labels; |
282 | } |
531 | $label->set_text ("..."); |
|
|
532 | $label->set_tooltip ("Use Cursor-Up to view previous matches"); |
|
|
533 | } |
283 | |
534 | |
284 | package CFClient::MapWidget::Command; |
535 | for my $label (@labels) { |
|
|
536 | $label->{fg} = [1, 1, 1, 1]; |
|
|
537 | $label->{bg} = [0, 0, 0, 0]; |
|
|
538 | } |
285 | |
539 | |
286 | use strict; |
540 | if (@matches) { |
|
|
541 | $self->{select} = "$matches[0][0]$arg"; |
287 | |
542 | |
288 | use CFClient::OpenGL; |
543 | $labels[0]->{fg} = [0, 0, 0, 1]; |
|
|
544 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
|
|
545 | } else { |
|
|
546 | $self->{select} = "$cmd$arg"; |
|
|
547 | } |
289 | |
548 | |
290 | our @ISA = CFClient::UI::VBox::; |
549 | for my $match (@matches) { |
|
|
550 | my $label = shift @labels; |
291 | |
551 | |
292 | sub new { |
552 | if (@labels) { |
293 | my $class = shift; |
553 | $label->set_text ("$match->[0]$arg"); |
294 | |
554 | $label->set_tooltip ($match->[1]); |
295 | my $self = $class->SUPER::new ( |
555 | } else { |
296 | @_, |
556 | $label->set_text ("..."); |
297 | children => [map |
557 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
298 | CFClient::UI::Label->new ( |
558 | last; |
299 | can_hover => 1, |
|
|
300 | can_events => 1, |
|
|
301 | fontsize => $_, |
|
|
302 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
|
|
303 | ], |
|
|
304 | ); |
|
|
305 | |
|
|
306 | $self |
|
|
307 | } |
|
|
308 | |
|
|
309 | sub size_allocate { |
|
|
310 | my ($self, $w, $h) = @_; |
|
|
311 | |
|
|
312 | $self->SUPER::size_allocate ($w, $h); |
|
|
313 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
|
|
314 | } |
|
|
315 | |
|
|
316 | sub update_labels { |
|
|
317 | my ($self) = @_; |
|
|
318 | |
|
|
319 | my $command = $self->{command}; |
|
|
320 | my $search_abbrev = qr/^\Q$self->{search}/; |
|
|
321 | my $search_full = qr/\Q$self->{search}/; |
|
|
322 | |
|
|
323 | my @found; |
|
|
324 | |
|
|
325 | for (@$command) { |
|
|
326 | if ($_->[0] =~ $search_abbrev) { |
|
|
327 | push @found, [$_->[0], $_]; |
|
|
328 | } elsif ($_->[1] =~ $search_full) { |
|
|
329 | push @found, [$_->[1], $_]; |
|
|
330 | } |
559 | } |
331 | } |
560 | } |
332 | |
561 | |
333 | @found = sort { $a->[0] cmp $b->[0] } @found; |
562 | for my $label (@labels) { |
334 | |
563 | $label->set_text (""); |
335 | $self->{children}[0]->set_text ("$self->{search}_"); |
564 | $label->set_tooltip (""); |
336 | |
|
|
337 | for (0..5) { |
|
|
338 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
339 | $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : ""); |
|
|
340 | } |
565 | } |
341 | |
566 | |
342 | $self->{select} = $found[0][1] |
567 | $self->update; |
343 | if @found; |
568 | ### |
344 | |
|
|
345 | if (@found > 6) { |
|
|
346 | $self->{children}[6]->set_text ("..."); |
|
|
347 | } |
|
|
348 | |
|
|
349 | $self->check_size; |
|
|
350 | } |
569 | } |
351 | |
570 | |
352 | sub key_down { |
571 | sub _draw { |
353 | my ($self, $ev) = @_; |
572 | my ($self) = @_; |
354 | |
573 | |
355 | if ($ev->{sym} == 8) { |
574 | # hack |
356 | substr $self->{search}, -1, 1, ""; |
575 | local $CFClient::UI::FOCUS = $self->{entry}; |
357 | $self->update_labels; |
|
|
358 | } elsif ($ev->{sym} == 13) { |
|
|
359 | if (exists $self->{select}) { |
|
|
360 | $self->emit (execute => $self->{select}); |
|
|
361 | $self->emit ("close"); |
|
|
362 | } |
|
|
363 | } elsif ($ev->{sym} == 27) { |
|
|
364 | $self->emit ("close"); |
|
|
365 | return; |
|
|
366 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
|
|
367 | $self->{search} .= chr $ev->{unicode}; |
|
|
368 | $self->update_labels; |
|
|
369 | } |
|
|
370 | |
576 | |
371 | length $self->{search} |
577 | $self->SUPER::_draw; |
372 | or $self->emit ("close"); |
|
|
373 | } |
578 | } |
374 | |
579 | |
375 | 1 |
580 | 1 |