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