… | |
… | |
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 | $self |
21 | } |
23 | } |
22 | |
24 | |
23 | sub DESTROY { |
25 | sub DESTROY { |
24 | my $self = shift; |
26 | my $self = shift; |
25 | |
27 | |
… | |
… | |
174 | ); |
176 | ); |
175 | |
177 | |
176 | sub key_down { |
178 | sub key_down { |
177 | my ($self, $ev) = @_; |
179 | my ($self, $ev) = @_; |
178 | |
180 | |
|
|
181 | return unless $::CONN; |
|
|
182 | |
179 | my $mod = $ev->{mod}; |
183 | my $mod = $ev->{mod}; |
180 | my $sym = $ev->{sym}; |
184 | my $sym = $ev->{sym}; |
|
|
185 | my $uni = $ev->{unicode}; |
181 | |
186 | |
182 | if ($sym == CFClient::SDLK_KP5) { |
187 | if ($sym == CFClient::SDLK_KP5) { |
183 | $::CONN->user_send ("stay fire"); |
188 | $::CONN->user_send ("stay fire"); |
184 | } elsif ($sym == ord ",") { |
189 | } elsif ($uni == ord ",") { |
185 | $::CONN->user_send ("take"); |
190 | $::CONN->user_send ("take"); |
186 | } elsif ($sym == ord "a") { |
191 | } elsif ($uni == ord "\t") { |
187 | $::CONN->user_send ("apply"); |
192 | $::CONN->user_send ("apply"); |
188 | } elsif ($sym == ord "'") { |
193 | } elsif ($uni == ord "'") { |
189 | $self->emit ('activate_console'); |
194 | $self->emit ('activate_console'); |
190 | } elsif ($sym == ord "/") { |
195 | } elsif ($uni == ord "/") { |
191 | $self->emit (activate_console => '/'); |
196 | $self->emit (activate_console => '/'); |
192 | } elsif (exists $DIR{$sym}) { |
197 | } elsif (exists $DIR{$sym}) { |
193 | if ($mod & CFClient::KMOD_SHIFT) { |
198 | if ($mod & CFClient::KMOD_SHIFT) { |
194 | $self->{shft}++; |
199 | $self->{shft}++; |
195 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
200 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
… | |
… | |
204 | new CFClient::MapWidget::Command:: |
209 | new CFClient::MapWidget::Command:: |
205 | command => $self->{command}, |
210 | command => $self->{command}, |
206 | can_focus => 1, |
211 | can_focus => 1, |
207 | connect_execute => sub { |
212 | connect_execute => sub { |
208 | # todo: support callback instead of user_send |
213 | # todo: support callback instead of user_send |
209 | $::CONN->user_send ($_[1][1]); |
214 | $::CONN->user_send ($_[1]); |
210 | }, |
215 | }, |
211 | connect_close => sub { |
216 | connect_close => sub { |
212 | (delete $self->{command_widget})->hide; |
217 | (delete $self->{command_widget})->hide; |
213 | $self->focus_in; |
218 | $self->focus_in; |
214 | }, |
219 | }, |
215 | ; |
220 | ; |
216 | $self->{command_widget}->key_down ($ev); |
221 | $self->{command_widget}->key_down ($ev) |
|
|
222 | unless $ev->{unicode} == 20; |
217 | return unless $self->{command_widget}; |
223 | return unless $self->{command_widget}; |
218 | $self->{command_widget}->show; |
224 | $self->{command_widget}->show; |
219 | $self->{command_widget}->focus_in; |
225 | $self->{command_widget}->focus_in; |
220 | } |
226 | } |
221 | } |
227 | } |
… | |
… | |
235 | } |
241 | } |
236 | |
242 | |
237 | sub add_command { |
243 | sub add_command { |
238 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
244 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
239 | |
245 | |
240 | (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; |
246 | (my $data = $command) =~ s/\\//g; |
241 | |
247 | |
|
|
248 | $tooltip =~ s/^\s+//; |
|
|
249 | |
|
|
250 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
|
|
251 | |
|
|
252 | $tooltip =~ s/\s+$//; |
|
|
253 | |
242 | push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; |
254 | $self->{command}{$command} = [$data, $tooltip, $widget, $cb]; |
|
|
255 | } |
|
|
256 | |
|
|
257 | sub clr_commands { |
|
|
258 | my ($self) = @_; |
|
|
259 | |
|
|
260 | %{$self->{command}} = (); |
243 | } |
261 | } |
244 | |
262 | |
245 | package CFClient::MapWidget::MapMap; |
263 | package CFClient::MapWidget::MapMap; |
246 | |
264 | |
247 | our @ISA = CFClient::UI::Base::; |
265 | our @ISA = CFClient::UI::Base::; |
… | |
… | |
325 | |
343 | |
326 | use strict; |
344 | use strict; |
327 | |
345 | |
328 | use CFClient::OpenGL; |
346 | use CFClient::OpenGL; |
329 | |
347 | |
330 | our @ISA = CFClient::UI::VBox::; |
348 | our @ISA = CFClient::UI::Frame::; |
331 | |
349 | |
332 | sub new { |
350 | sub new { |
333 | my $class = shift; |
351 | my $class = shift; |
334 | |
352 | |
335 | my $self = $class->SUPER::new ( |
353 | my $self = $class->SUPER::new ( |
|
|
354 | bg => [0, 0, 0, 0.8], |
336 | @_, |
355 | @_, |
337 | children => [map |
356 | ); |
|
|
357 | |
|
|
358 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
359 | |
|
|
360 | $self->{label} = [ |
|
|
361 | map |
338 | CFClient::UI::Label->new ( |
362 | CFClient::UI::Label->new ( |
339 | can_hover => 1, |
363 | can_hover => 1, |
340 | can_events => 1, |
364 | can_events => 1, |
|
|
365 | tooltip_width => 0.33, |
341 | fontsize => $_, |
366 | fontsize => $_, |
342 | ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 |
367 | ), (0.8) x 6 |
|
|
368 | ]; |
|
|
369 | |
|
|
370 | $self->{entry} = new CFClient::UI::Entry |
|
|
371 | connect_changed => sub { |
|
|
372 | $self->update_labels; |
343 | ], |
373 | }; |
|
|
374 | |
|
|
375 | $self->{vbox}->add ( |
|
|
376 | $self->{entry}, |
|
|
377 | @{$self->{label}}, |
344 | ); |
378 | ); |
345 | |
379 | |
346 | $self |
380 | $self |
347 | } |
381 | } |
348 | |
382 | |
… | |
… | |
351 | |
385 | |
352 | $self->SUPER::size_allocate ($w, $h); |
386 | $self->SUPER::size_allocate ($w, $h); |
353 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
387 | $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); |
354 | } |
388 | } |
355 | |
389 | |
356 | sub update_labels { |
|
|
357 | my ($self) = @_; |
|
|
358 | |
|
|
359 | my $command = $self->{command}; |
|
|
360 | my $search_abbrev = qr/^\Q$self->{search}/; |
|
|
361 | my $search_full = qr/\Q$self->{search}/; |
|
|
362 | |
|
|
363 | my @found; |
|
|
364 | |
|
|
365 | for (@$command) { |
|
|
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 ("..."); |
|
|
387 | } |
|
|
388 | |
|
|
389 | $self->check_size; |
|
|
390 | } |
|
|
391 | |
|
|
392 | sub key_down { |
390 | sub key_down { |
393 | my ($self, $ev) = @_; |
391 | my ($self, $ev) = @_; |
394 | |
392 | |
395 | if ($ev->{sym} == 8) { |
393 | if ($ev->{sym} == 13) { |
396 | substr $self->{search}, -1, 1, ""; |
|
|
397 | $self->update_labels; |
|
|
398 | } elsif ($ev->{sym} == 13) { |
|
|
399 | if (exists $self->{select}) { |
394 | if (exists $self->{select}) { |
400 | $self->emit (execute => $self->{select}); |
395 | $self->emit (execute => $self->{select}); |
401 | $self->emit ("close"); |
396 | $self->emit ("close"); |
402 | } |
397 | } |
403 | } elsif ($ev->{sym} == 27) { |
398 | } elsif ($ev->{sym} == 27) { |
|
|
399 | $self->{entry}->set_text (""); |
404 | $self->emit ("close"); |
400 | $self->emit ("close"); |
405 | return; |
401 | return; |
406 | } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { |
402 | } elsif ($ev->{sym} == CFClient::SDLK_DOWN) { |
407 | $self->{search} .= chr $ev->{unicode}; |
403 | ++$self->{select_offset} |
|
|
404 | if $self->{select_offset} < $#{ $self->{last_match} || [] }; |
408 | $self->update_labels; |
405 | $self->update_labels; |
|
|
406 | } elsif ($ev->{sym} == CFClient::SDLK_UP) { |
|
|
407 | --$self->{select_offset} |
|
|
408 | if $self->{select_offset}; |
|
|
409 | $self->update_labels; |
|
|
410 | } else { |
|
|
411 | #$self->{entry}{force_alloc} = 1; |
|
|
412 | $self->{entry}->key_down ($ev); |
409 | } |
413 | } |
|
|
414 | } |
410 | |
415 | |
411 | length $self->{search} |
416 | sub update_labels { |
|
|
417 | my ($self) = @_; |
|
|
418 | |
|
|
419 | my $text = $self->{entry}->get_text; |
|
|
420 | |
|
|
421 | length $text |
412 | or $self->emit ("close"); |
422 | or return $self->emit ("close"); |
|
|
423 | |
|
|
424 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
|
|
425 | |
|
|
426 | if ($cmd ne $self->{last_search}) { |
|
|
427 | my $regexp = do { |
|
|
428 | my ($beg, @chr) = split //, lc $cmd; |
|
|
429 | |
|
|
430 | # the following regex is used to match our "completion entry" |
|
|
431 | # to an actual command - the parentheses match kind of "overhead" |
|
|
432 | # - the more characters the parentheses match, the less attractive |
|
|
433 | # is the match. |
|
|
434 | my $regexp = "^\Q$beg\E" |
|
|
435 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
|
|
436 | qr<$regexp> |
|
|
437 | }; |
|
|
438 | |
|
|
439 | my @match; |
|
|
440 | my @penalty; |
|
|
441 | |
|
|
442 | for (keys %{$self->{command}}) { |
|
|
443 | if (@penalty = $_ =~ $regexp) { |
|
|
444 | push @match, [$_, length join "", map "::$_", grep defined, @penalty]; |
|
|
445 | } |
|
|
446 | } |
|
|
447 | |
|
|
448 | @match = map $self->{command}{$_->[0]}, |
|
|
449 | sort { |
|
|
450 | $a->[1] <=> $b->[1] |
|
|
451 | or (length $a->[0]) <=> (length $b->[0]) |
|
|
452 | } @match; |
|
|
453 | |
|
|
454 | $self->{last_search} = $cmd; |
|
|
455 | $self->{last_match} = \@match; |
|
|
456 | |
|
|
457 | $self->{select_offset} = 0; |
|
|
458 | } |
|
|
459 | |
|
|
460 | my @labels = @{ $self->{label} }; |
|
|
461 | my @matches = @{ $self->{last_match} || [] }; |
|
|
462 | |
|
|
463 | if ($self->{select_offset}) { |
|
|
464 | splice @matches, 0, $self->{select_offset}, (); |
|
|
465 | |
|
|
466 | my $label = shift @labels; |
|
|
467 | $label->set_text ("..."); |
|
|
468 | $label->set_tooltip ("Use Cursor-Up to view previous matches"); |
|
|
469 | } |
|
|
470 | |
|
|
471 | for my $label (@labels) { |
|
|
472 | $label->{fg} = [1, 1, 1, 1]; |
|
|
473 | $label->{bg} = [0, 0, 0, 0]; |
|
|
474 | } |
|
|
475 | |
|
|
476 | if (@matches) { |
|
|
477 | $self->{select} = "$matches[0][0]$arg"; |
|
|
478 | |
|
|
479 | $labels[0]->{fg} = [0, 0, 0, 1]; |
|
|
480 | $labels[0]->{bg} = [1, 1, 1, 0.8]; |
|
|
481 | } else { |
|
|
482 | $self->{select} = "$cmd$arg"; |
|
|
483 | } |
|
|
484 | |
|
|
485 | for my $match (@matches) { |
|
|
486 | my $label = shift @labels; |
|
|
487 | |
|
|
488 | if (@labels) { |
|
|
489 | $label->set_text ("$match->[0]$arg"); |
|
|
490 | $label->set_tooltip ($match->[1]); |
|
|
491 | } else { |
|
|
492 | $label->set_text ("..."); |
|
|
493 | $label->set_tooltip ("Use Cursor-Down to view more matches"); |
|
|
494 | last; |
|
|
495 | } |
|
|
496 | } |
|
|
497 | |
|
|
498 | for my $label (@labels) { |
|
|
499 | $label->set_text (""); |
|
|
500 | $label->set_tooltip (""); |
|
|
501 | } |
|
|
502 | |
|
|
503 | $self->update; |
|
|
504 | ### |
|
|
505 | } |
|
|
506 | |
|
|
507 | sub _draw { |
|
|
508 | my ($self) = @_; |
|
|
509 | |
|
|
510 | # hack |
|
|
511 | local $CFClient::UI::FOCUS = $self->{entry}; |
|
|
512 | |
|
|
513 | $self->SUPER::_draw; |
413 | } |
514 | } |
414 | |
515 | |
415 | 1 |
516 | 1 |