ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/MapWidget.pm (file contents):
Revision 1.22 by root, Sat May 13 00:27:08 2006 UTC vs.
Revision 1.25 by root, Wed May 17 15:18:57 2006 UTC

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->add_command ("killpets", "kills all your summoned and charmed creates");
23 21
24 $self 22 $self
25} 23}
26 24
27sub DESTROY { 25sub DESTROY {
188 186
189 if ($sym == CFClient::SDLK_KP5) { 187 if ($sym == CFClient::SDLK_KP5) {
190 $::CONN->user_send ("stay fire"); 188 $::CONN->user_send ("stay fire");
191 } elsif ($uni == ord ",") { 189 } elsif ($uni == ord ",") {
192 $::CONN->user_send ("take"); 190 $::CONN->user_send ("take");
193 } elsif ($uni == ord "a") { 191 } elsif ($uni == ord "\t") {
194 $::CONN->user_send ("apply"); 192 $::CONN->user_send ("apply");
195 } elsif ($uni == ord "'") { 193 } elsif ($uni == ord "'") {
196 $self->emit ('activate_console'); 194 $self->emit ('activate_console');
197 } elsif ($uni == ord "/") { 195 } elsif ($uni == ord "/") {
198 $self->emit (activate_console => '/'); 196 $self->emit (activate_console => '/');
211 new CFClient::MapWidget::Command:: 209 new CFClient::MapWidget::Command::
212 command => $self->{command}, 210 command => $self->{command},
213 can_focus => 1, 211 can_focus => 1,
214 connect_execute => sub { 212 connect_execute => sub {
215 # todo: support callback instead of user_send 213 # todo: support callback instead of user_send
216 $::CONN->user_send ($_[1][1]); 214 $::CONN->user_send ($_[1]);
217 }, 215 },
218 connect_close => sub { 216 connect_close => sub {
219 (delete $self->{command_widget})->hide; 217 (delete $self->{command_widget})->hide;
220 $self->focus_in; 218 $self->focus_in;
221 }, 219 },
222 ; 220 ;
223 $self->{command_widget}->key_down ($ev); 221 $self->{command_widget}->key_down ($ev)
222 unless $ev->{unicode} == 20;
224 return unless $self->{command_widget}; 223 return unless $self->{command_widget};
225 $self->{command_widget}->show; 224 $self->{command_widget}->show;
226 $self->{command_widget}->focus_in; 225 $self->{command_widget}->focus_in;
227 } 226 }
228} 227}
242} 241}
243 242
244sub add_command { 243sub add_command {
245 my ($self, $command, $tooltip, $widget, $cb) = @_; 244 my ($self, $command, $tooltip, $widget, $cb) = @_;
246 245
247 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; 246 (my $data = $command) =~ s/\\//g;
248 247
248 $tooltip =~ s/^\s+//;
249
250 $tooltip = "<big>$data</big>\n\n$tooltip";
251
252 $tooltip =~ s/\s+$//;
253
254 $self->{command}{$command} = [$data, $tooltip, $widget, $cb];
255}
256
257sub clr_commands {
258 my ($self) = @_;
259
249 $self->{command} = [ 260 %{$self->{command}} = ();
250 [$abbrev, $command, $tooltip, $widget, $cb],
251 grep $_->[1] ne $command, @{ $self->{command} },
252 ];
253} 261}
254 262
255package CFClient::MapWidget::MapMap; 263package CFClient::MapWidget::MapMap;
256 264
257our @ISA = CFClient::UI::Base::; 265our @ISA = CFClient::UI::Base::;
335 343
336use strict; 344use strict;
337 345
338use CFClient::OpenGL; 346use CFClient::OpenGL;
339 347
340our @ISA = CFClient::UI::VBox::; 348our @ISA = CFClient::UI::Frame::;
341 349
342sub new { 350sub new {
343 my $class = shift; 351 my $class = shift;
344 352
345 my $self = $class->SUPER::new ( 353 my $self = $class->SUPER::new (
354 bg => [0, 0, 0, 0.8],
346 @_, 355 @_,
347 children => [map 356 );
357
358 $self->add ($self->{vbox} = new CFClient::UI::VBox);
359
360 $self->{label} = [
361 map
348 CFClient::UI::Label->new ( 362 CFClient::UI::Label->new (
349 can_hover => 1, 363 can_hover => 1,
350 can_events => 1, 364 can_events => 1,
365 tooltip_width => 0.33,
351 fontsize => $_, 366 fontsize => $_,
352 ), 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;
353 ], 373 };
374
375 $self->{vbox}->add (
376 $self->{entry},
377 @{$self->{label}},
354 ); 378 );
355 379
356 $self 380 $self
357} 381}
358 382
361 385
362 $self->SUPER::size_allocate ($w, $h); 386 $self->SUPER::size_allocate ($w, $h);
363 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 387 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
364} 388}
365 389
366sub update_labels {
367 my ($self) = @_;
368
369 my $command = $self->{command};
370 my $search_abbrev = qr/^\Q$self->{search}/;
371 my $search_full = qr/\Q$self->{search}/;
372
373 my @found;
374
375 for (@$command) {
376 if ($_->[0] =~ $search_abbrev) {
377 push @found, [$_->[0], $_];
378 } elsif (2 < length $self->{search} and $_->[1] =~ $search_full) {
379 push @found, [$_->[1], $_];
380 }
381 }
382
383 @found = sort { $a->[0] cmp $b->[0] } @found;
384
385 $self->{children}[0]->set_text ("$self->{search}_");
386
387 for (0..5) {
388 $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : "");
389 $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : "");
390 }
391
392 $self->{select} = $found[0][1]
393 if @found;
394
395 if (@found > 6) {
396 $self->{children}[6]->set_text ("...");
397 }
398
399 $self->check_size;
400}
401
402sub key_down { 390sub key_down {
403 my ($self, $ev) = @_; 391 my ($self, $ev) = @_;
404 392
405 if ($ev->{sym} == 8) { 393 if ($ev->{sym} == 13) {
406 substr $self->{search}, -1, 1, "";
407 $self->update_labels;
408 } elsif ($ev->{sym} == 13) {
409 if (exists $self->{select}) { 394 if (exists $self->{select}) {
410 $self->emit (execute => $self->{select}); 395 $self->emit (execute => $self->{select});
411 $self->emit ("close"); 396 $self->emit ("close");
412 } 397 }
413 } elsif ($ev->{sym} == 27) { 398 } elsif ($ev->{sym} == 27) {
399 $self->{entry}->set_text ("");
414 $self->emit ("close"); 400 $self->emit ("close");
415 return; 401 return;
416 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { 402 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
417 $self->{search} .= chr $ev->{unicode}; 403 ++$self->{select_offset}
404 if $self->{select_offset} < $#{ $self->{last_match} || [] };
418 $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);
419 } 413 }
414}
420 415
421 length $self->{search} 416sub update_labels {
417 my ($self) = @_;
418
419 my $text = $self->{entry}->get_text;
420
421 length $text
422 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
507sub _draw {
508 my ($self) = @_;
509
510 # hack
511 local $CFClient::UI::FOCUS = $self->{entry};
512
513 $self->SUPER::_draw;
423} 514}
424 515
4251 5161

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines