… | |
… | |
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 | 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 | ; |
|
|
34 | |
|
|
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 { |
24 | my $self = shift; |
53 | my ($self) = @_; |
25 | |
54 | |
26 | glDeleteList $self->{list}; |
55 | %{$self->{completer}{command}} = (); |
27 | |
|
|
28 | $self->SUPER::DESTROY; |
|
|
29 | } |
56 | } |
30 | |
57 | |
31 | sub button_down { |
58 | sub button_down { |
32 | my ($self, $ev, $x, $y) = @_; |
59 | my ($self, $ev, $x, $y) = @_; |
33 | |
60 | |
… | |
… | |
74 | |
101 | |
75 | $self->{need_update} = 1; |
102 | $self->{need_update} = 1; |
76 | $self->SUPER::update; |
103 | $self->SUPER::update; |
77 | } |
104 | } |
78 | |
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 | |
79 | sub draw { |
182 | sub draw { |
80 | my ($self) = @_; |
183 | my ($self) = @_; |
|
|
184 | |
|
|
185 | my $focused = $CFClient::UI::FOCUS == $self |
|
|
186 | || $CFClient::UI::FOCUS == $self->{completer}{entry}; |
|
|
187 | |
|
|
188 | return |
|
|
189 | unless $focused || !$::FAST; |
81 | |
190 | |
82 | if (delete $self->{need_update}) { |
191 | if (delete $self->{need_update}) { |
83 | glNewList $self->{list}; |
192 | glNewList $self->{list}; |
84 | |
193 | |
85 | if ($::MAP) { |
194 | if ($::MAP) { |
… | |
… | |
110 | 0.05, 0.13, 0.05, |
219 | 0.05, 0.13, 0.05, |
111 | ); |
220 | ); |
112 | glEnable GL_CONVOLUTION_2D; |
221 | glEnable GL_CONVOLUTION_2D; |
113 | } |
222 | } |
114 | |
223 | |
|
|
224 | $self->{fow_texture_name} ||= glGenTexture; |
|
|
225 | # try to re-use the texture name: TODO improve texture class instead |
|
|
226 | |
115 | $self->{fow_texture} = new CFClient::Texture |
227 | $self->{fow_texture} = new CFClient::Texture |
116 | w => $w, |
228 | w => $w, |
117 | h => $h, |
229 | h => $h, |
118 | data => $data, |
230 | data => $data, |
|
|
231 | name => $self->{fow_texture_name}, |
119 | internalformat => GL_ALPHA, |
232 | internalformat => GL_ALPHA, |
120 | format => GL_ALPHA; |
233 | format => GL_ALPHA; |
121 | |
234 | |
122 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
235 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
123 | |
236 | |
124 | glEnable GL_BLEND; |
|
|
125 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
126 | glEnable GL_TEXTURE_2D; |
237 | glEnable GL_TEXTURE_2D; |
127 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
238 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
128 | |
239 | |
129 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
240 | glColor +($::CFG->{fow_intensity}) x 3, 0.8; |
130 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
241 | $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32); |
131 | |
242 | |
132 | glDisable GL_TEXTURE_2D; |
243 | glDisable GL_TEXTURE_2D; |
133 | glDisable GL_BLEND; |
|
|
134 | } |
244 | } |
135 | |
245 | |
136 | glPopMatrix; |
246 | glPopMatrix; |
137 | } |
247 | } |
138 | |
248 | |
… | |
… | |
141 | |
251 | |
142 | glPushMatrix; |
252 | glPushMatrix; |
143 | glCallList $self->{list}; |
253 | glCallList $self->{list}; |
144 | glPopMatrix; |
254 | glPopMatrix; |
145 | |
255 | |
146 | if ($CFClient::UI::FOCUS != $self) { |
256 | # TNT2 emulates logops in software (or worse :) |
147 | glColor 64/255, 64/255, 64/255; |
257 | if ($focused) { |
148 | glLogicOp GL_AND; |
258 | (delete $self->{out_of_focus})->destroy |
149 | 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; |
150 | glBegin GL_QUADS; |
264 | glBegin GL_QUADS; |
151 | glVertex 0, 0; |
265 | glVertex 0, 0; |
152 | glVertex 0, $::HEIGHT; |
266 | glVertex 0, $::HEIGHT; |
153 | glVertex $::WIDTH, $::HEIGHT; |
267 | glVertex $::WIDTH, $::HEIGHT; |
154 | glVertex $::WIDTH, 0; |
268 | glVertex $::WIDTH, 0; |
155 | glEnd; |
269 | glEnd; |
156 | glDisable GL_COLOR_LOGIC_OP; |
270 | glDisable GL_BLEND; |
157 | } |
|
|
158 | } |
|
|
159 | |
271 | |
160 | my %DIR = ( |
272 | $self->{out_of_focus} ||= do { |
161 | CFClient::SDLK_KP8, [1, "north"], |
273 | my $label = new CFClient::UI::Label |
162 | CFClient::SDLK_KP9, [2, "northeast"], |
274 | x => 0, |
163 | CFClient::SDLK_KP6, [3, "east"], |
275 | y => 0, |
164 | CFClient::SDLK_KP3, [4, "southeast"], |
276 | z => 1, |
165 | CFClient::SDLK_KP2, [5, "south"], |
277 | ellipsise => 0, |
166 | CFClient::SDLK_KP1, [6, "southwest"], |
278 | text => "map out of focus (click map to play)"; |
167 | CFClient::SDLK_KP4, [7, "west"], |
|
|
168 | CFClient::SDLK_KP7, [8, "northwest"], |
|
|
169 | |
279 | |
170 | CFClient::SDLK_UP, [1, "north"], |
280 | $label->show; |
171 | CFClient::SDLK_RIGHT, [3, "east"], |
281 | $label->update; |
172 | CFClient::SDLK_DOWN, [5, "south"], |
|
|
173 | CFClient::SDLK_LEFT, [7, "west"], |
|
|
174 | ); |
|
|
175 | |
282 | |
176 | sub key_down { |
283 | $CFClient::UI::ROOT->on_post_alloc ("$self$label" => sub { |
177 | my ($self, $ev) = @_; |
284 | $label->move ( |
|
|
285 | ($::WIDTH - $label->{w}) * 0.5, |
|
|
286 | ($::HEIGHT - $label->{h}) * 0.5, |
|
|
287 | ); |
|
|
288 | }); |
178 | |
289 | |
179 | my $mod = $ev->{mod}; |
290 | $label |
180 | my $sym = $ev->{sym}; |
|
|
181 | |
|
|
182 | if ($sym == CFClient::SDLK_KP5) { |
|
|
183 | $::CONN->user_send ("stay fire"); |
|
|
184 | } elsif ($sym == ord ",") { |
|
|
185 | $::CONN->user_send ("take"); |
|
|
186 | } elsif ($sym == ord "a") { |
|
|
187 | $::CONN->user_send ("apply"); |
|
|
188 | } elsif ($sym == ord "'") { |
|
|
189 | $self->emit ('activate_console'); |
|
|
190 | } elsif ($sym == ord "/") { |
|
|
191 | $self->emit (activate_console => '/'); |
|
|
192 | } elsif (exists $DIR{$sym}) { |
|
|
193 | if ($mod & CFClient::KMOD_SHIFT) { |
|
|
194 | $self->{shft}++; |
|
|
195 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
|
|
196 | } elsif ($mod & CFClient::KMOD_CTRL) { |
|
|
197 | $self->{ctrl}++; |
|
|
198 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
|
|
199 | } else { |
|
|
200 | $::CONN->user_send ("$DIR{$sym}[1]"); |
|
|
201 | } |
291 | }; |
202 | } elsif ($ev->{unicode}) { |
|
|
203 | $self->{command_widget} ||= |
|
|
204 | new CFClient::MapWidget::Command:: |
|
|
205 | command => $self->{command}, |
|
|
206 | can_focus => 1, |
|
|
207 | connect_execute => sub { |
|
|
208 | # todo: support callback instead of user_send |
|
|
209 | $::CONN->user_send ($_[1][1]); |
|
|
210 | }, |
|
|
211 | connect_close => sub { |
|
|
212 | (delete $self->{command_widget})->hide; |
|
|
213 | $self->focus_in; |
|
|
214 | }, |
|
|
215 | ; |
|
|
216 | $self->{command_widget}->key_down ($ev); |
|
|
217 | return unless $self->{command_widget}; |
|
|
218 | $self->{command_widget}->show; |
|
|
219 | $self->{command_widget}->focus_in; |
|
|
220 | } |
292 | } |
221 | } |
293 | } |
222 | |
294 | |
223 | sub key_up { |
295 | sub DESTROY { |
224 | my ($self, $ev) = @_; |
296 | my $self = shift; |
225 | |
297 | |
226 | my $mod = $ev->{mod}; |
298 | glDeleteList $self->{list}; |
227 | my $sym = $ev->{sym}; |
|
|
228 | |
299 | |
229 | if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { |
300 | $self->SUPER::DESTROY; |
230 | $::CONN->user_send ("fire_stop"); |
|
|
231 | } |
|
|
232 | if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
233 | $::CONN->user_send ("run_stop"); |
|
|
234 | } |
|
|
235 | } |
|
|
236 | |
|
|
237 | sub add_command { |
|
|
238 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
|
|
239 | |
|
|
240 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
|
|
241 | |
|
|
242 | push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; |
|
|
243 | } |
301 | } |
244 | |
302 | |
245 | package CFClient::MapWidget::MapMap; |
303 | package CFClient::MapWidget::MapMap; |
246 | |
304 | |
247 | use base CFClient::UI::Base::; |
305 | our @ISA = CFClient::UI::Base::; |
248 | |
306 | |
249 | use Time::HiRes qw(time); |
307 | use Time::HiRes qw(time); |
250 | use CFClient::OpenGL; |
308 | use CFClient::OpenGL; |
251 | |
309 | |
252 | sub size_request { |
310 | sub size_request { |
… | |
… | |
325 | |
383 | |
326 | use strict; |
384 | use strict; |
327 | |
385 | |
328 | use CFClient::OpenGL; |
386 | use CFClient::OpenGL; |
329 | |
387 | |
330 | our @ISA = CFClient::UI::VBox::; |
388 | our @ISA = CFClient::UI::Frame::; |
331 | |
389 | |
332 | sub new { |
390 | sub new { |
333 | my $class = shift; |
391 | my $class = shift; |
334 | |
392 | |
335 | my $self = $class->SUPER::new ( |
393 | my $self = $class->SUPER::new ( |
|
|
394 | bg => [0, 0, 0, 0.8], |
336 | @_, |
395 | @_, |
337 | children => [map |
396 | ); |
|
|
397 | |
|
|
398 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
399 | |
|
|
400 | $self->{label} = [ |
|
|
401 | map |
338 | CFClient::UI::Label->new ( |
402 | CFClient::UI::Label->new ( |
339 | can_hover => 1, |
403 | can_hover => 1, |
340 | can_events => 1, |
404 | can_events => 1, |
|
|
405 | tooltip_width => 0.33, |
341 | fontsize => $_, |
406 | fontsize => $_, |
342 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
407 | ), (0.8) x 16 |
|
|
408 | ]; |
|
|
409 | |
|
|
410 | $self->{entry} = new CFClient::UI::Entry |
|
|
411 | connect_changed => sub { |
|
|
412 | $self->update_labels; |
343 | ], |
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}}, |
344 | ); |
447 | ); |
345 | |
448 | |
346 | $self |
449 | $self |
|
|
450 | } |
|
|
451 | |
|
|
452 | sub set_prefix { |
|
|
453 | my ($self, $prefix) = @_; |
|
|
454 | |
|
|
455 | $self->{entry}->set_text ($prefix); |
|
|
456 | $self->show; |
347 | } |
457 | } |
348 | |
458 | |
349 | sub size_allocate { |
459 | sub size_allocate { |
350 | my ($self, $w, $h) = @_; |
460 | my ($self, $w, $h) = @_; |
351 | |
461 | |
352 | $self->SUPER::size_allocate ($w, $h); |
462 | $self->SUPER::size_allocate ($w, $h); |
353 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
463 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
354 | } |
464 | } |
355 | |
465 | |
356 | sub update_labels { |
466 | sub show { |
357 | my ($self) = @_; |
467 | my ($self) = @_; |
358 | |
468 | |
359 | my $command = $self->{command}; |
469 | $self->SUPER::show; |
360 | my $search_abbrev = qr/^\Q$self->{search}/; |
470 | $self->{entry}->focus_in; |
361 | my $search_full = qr/\Q$self->{search}/; |
471 | } |
362 | |
472 | |
363 | my @found; |
473 | sub hide { |
|
|
474 | my ($self) = @_; |
364 | |
475 | |
365 | for (@$command) { |
476 | $self->SUPER::hide; |
366 | if ($_->[0] =~ $search_abbrev) { |
|
|
367 | push @found, [$_->[0], $_]; |
|
|
368 | } elsif ($_->[1] =~ $search_full) { |
|
|
369 | push @found, [$_->[1], $_]; |
|
|
370 | } |
|
|
371 | } |
|
|
372 | |
|
|
373 | @found = sort { $a->[0] cmp $b->[0] } @found; |
|
|
374 | |
|
|
375 | $self->{children}[0]->set_text ("$self->{search}_"); |
|
|
376 | |
|
|
377 | for (0..5) { |
|
|
378 | $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : ""); |
|
|
379 | $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : ""); |
|
|
380 | } |
|
|
381 | |
|
|
382 | $self->{select} = $found[0][1] |
|
|
383 | if @found; |
|
|
384 | |
|
|
385 | if (@found > 6) { |
|
|
386 | $self->{children}[6]->set_text ("..."); |
477 | $self->{entry}->set_text (""); |
387 | } |
|
|
388 | |
|
|
389 | $self->check_size; |
|
|
390 | } |
478 | } |
391 | |
479 | |
392 | sub key_down { |
480 | sub key_down { |
393 | my ($self, $ev) = @_; |
481 | my ($self, $ev) = @_; |
394 | |
482 | |
395 | if ($ev->{sym} == 8) { |
483 | $self->{entry}->key_down ($ev); |
396 | substr $self->{search}, -1, 1, ""; |
484 | } |
397 | $self->update_labels; |
485 | |
398 | } elsif ($ev->{sym} == 13) { |
486 | sub update_labels { |
399 | if (exists $self->{select}) { |
487 | my ($self) = @_; |
400 | $self->emit (execute => $self->{select}); |
488 | |
401 | $self->emit ("close"); |
489 | my $text = $self->{entry}->get_text; |
|
|
490 | |
|
|
491 | length $text |
|
|
492 | or return $self->hide; |
|
|
493 | |
|
|
494 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
495 | |
|
|
496 | if ($text ne $self->{last_search}) { |
|
|
497 | my @match; |
|
|
498 | |
|
|
499 | if ($text =~ /^(.*?)\s+$/) { |
|
|
500 | @match = [$cmd, "(appended whitespace suppresses completion)"]; |
|
|
501 | } else { |
|
|
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 $a->[0]) <=> (length $b->[0]) |
|
|
527 | } @match; |
402 | } |
528 | } |
403 | } elsif ($ev->{sym} == 27) { |
529 | |
404 | $self->emit ("close"); |
530 | $self->{last_search} = $cmd; |
405 | return; |
531 | $self->{last_match} = \@match; |
406 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
532 | |
407 | $self->{search} .= chr $ev->{unicode}; |
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 | |
408 | $self->update_labels; |
579 | $self->update; |
409 | } |
580 | ### |
|
|
581 | } |
410 | |
582 | |
411 | length $self->{search} |
583 | sub _draw { |
412 | or $self->emit ("close"); |
584 | my ($self) = @_; |
|
|
585 | |
|
|
586 | # hack |
|
|
587 | local $CFClient::UI::FOCUS = $self->{entry}; |
|
|
588 | |
|
|
589 | $self->SUPER::_draw; |
413 | } |
590 | } |
414 | |
591 | |
415 | 1 |
592 | 1 |