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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines