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.20 by root, Tue Apr 25 14:04:37 2006 UTC vs.
Revision 1.26 by root, Wed May 17 16:17:01 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines