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.35 by root, Wed May 24 21:49:58 2006 UTC vs.
Revision 1.36 by root, Thu May 25 00:26:19 2006 UTC

17 can_focus => 1, 17 can_focus => 1,
18 list => glGenList, 18 list => glGenList,
19 @_ 19 @_
20 ); 20 );
21 21
22 $self->{completer} = new CFClient::MapWidget::Command::
23 command => $self->{command},
24 can_focus => 1,
25 ;
26
22 $self 27 $self
23} 28}
24 29
25sub DESTROY { 30sub add_command {
31 my ($self, $command, $tooltip, $widget, $cb) = @_;
32
33 (my $data = $command) =~ s/\\//g;
34
35 $tooltip =~ s/^\s+//;
36
37 $tooltip = "<big>$data</big>\n\n$tooltip";
38
39 $tooltip =~ s/\s+$//;
40
41 $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
42}
43
44sub clr_commands {
26 my $self = shift; 45 my ($self) = @_;
27 46
28 glDeleteList $self->{list}; 47 %{$self->{completer}{command}} = ();
29
30 $self->SUPER::DESTROY;
31} 48}
32 49
33sub button_down { 50sub button_down {
34 my ($self, $ev, $x, $y) = @_; 51 my ($self, $ev, $x, $y) = @_;
35 52
76 93
77 $self->{need_update} = 1; 94 $self->{need_update} = 1;
78 $self->SUPER::update; 95 $self->SUPER::update;
79} 96}
80 97
98my %DIR = (
99 CFClient::SDLK_KP8, [1, "north"],
100 CFClient::SDLK_KP9, [2, "northeast"],
101 CFClient::SDLK_KP6, [3, "east"],
102 CFClient::SDLK_KP3, [4, "southeast"],
103 CFClient::SDLK_KP2, [5, "south"],
104 CFClient::SDLK_KP1, [6, "southwest"],
105 CFClient::SDLK_KP4, [7, "west"],
106 CFClient::SDLK_KP7, [8, "northwest"],
107
108 CFClient::SDLK_UP, [1, "north"],
109 CFClient::SDLK_RIGHT, [3, "east"],
110 CFClient::SDLK_DOWN, [5, "south"],
111 CFClient::SDLK_LEFT, [7, "west"],
112);
113
114sub key_down {
115 my ($self, $ev) = @_;
116
117 return unless $::CONN;
118
119 my $mod = $ev->{mod};
120 my $sym = $ev->{sym};
121 my $uni = $ev->{unicode};
122
123 if ($sym == CFClient::SDLK_KP5) {
124 $::CONN->user_send ("stay fire");
125 } elsif ($uni == ord ",") {
126 $::CONN->user_send ("take");
127 } elsif ($uni == ord " ") {
128 $::CONN->user_send ("apply");
129 } elsif ($uni == ord "\t") {
130 # TODO: toggle inventory
131 } elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") {
132 $::CONN->user_send ("rotateshoottype +");
133 } elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") {
134 $::CONN->user_send ("rotateshoottype -");
135 } elsif ($uni == ord '"') {
136 $self->{completer}->set_prefix ("$::CFG->{say_command} ");
137 $self->{completer}->show;
138 } elsif ($uni == ord "'") {
139 $self->{completer}->set_prefix ("");
140 $self->{completer}->show;
141 } elsif (exists $DIR{$sym}) {
142 if ($mod & CFClient::KMOD_SHIFT) {
143 $self->{shft}++;
144 $::CONN->user_send ("fire $DIR{$sym}[0]");
145 } elsif ($mod & CFClient::KMOD_CTRL) {
146 $self->{ctrl}++;
147 $::CONN->user_send ("run $DIR{$sym}[0]");
148 } else {
149 $::CONN->user_send ("$DIR{$sym}[1]");
150 }
151 } elsif ($ev->{unicode}) {
152 $self->{completer}->key_down ($ev);
153 $self->{completer}->show;
154 }
155}
156
157sub key_up {
158 my ($self, $ev) = @_;
159
160 my $mod = $ev->{mod};
161 my $sym = $ev->{sym};
162
163 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
164 $::CONN->user_send ("fire_stop");
165 }
166 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
167 $::CONN->user_send ("run_stop");
168 }
169}
170
81sub draw { 171sub draw {
82 my ($self) = @_; 172 my ($self) = @_;
83 173
174 my $focused = $CFClient::UI::FOCUS == $self
175 || $CFClient::UI::FOCUS == $self->{completer}{entry};
176
84 return 177 return
85 unless $CFClient::UI::FOCUS == $self || !$::FAST; 178 unless $focused || !$::FAST;
86 179
87 if (delete $self->{need_update}) { 180 if (delete $self->{need_update}) {
88 glNewList $self->{list}; 181 glNewList $self->{list};
89 182
90 if ($::MAP) { 183 if ($::MAP) {
148 glPushMatrix; 241 glPushMatrix;
149 glCallList $self->{list}; 242 glCallList $self->{list};
150 glPopMatrix; 243 glPopMatrix;
151 244
152 # TNT2 emulates logops in software (or worse :) 245 # TNT2 emulates logops in software (or worse :)
153 if ($CFClient::UI::FOCUS == $self) { 246 if ($focused) {
154 (delete $self->{out_of_focus})->destroy 247 (delete $self->{out_of_focus})->destroy
155 if $self->{out_of_focus}; 248 if $self->{out_of_focus};
156 } else { 249 } else {
157 glColor 0.4, 0.2, 0.2, 0.6; 250 glColor 0.4, 0.2, 0.2, 0.6;
158 glEnable GL_BLEND; 251 glEnable GL_BLEND;
186 $label 279 $label
187 }; 280 };
188 } 281 }
189} 282}
190 283
191my %DIR = ( 284sub DESTROY {
192 CFClient::SDLK_KP8, [1, "north"],
193 CFClient::SDLK_KP9, [2, "northeast"],
194 CFClient::SDLK_KP6, [3, "east"],
195 CFClient::SDLK_KP3, [4, "southeast"],
196 CFClient::SDLK_KP2, [5, "south"],
197 CFClient::SDLK_KP1, [6, "southwest"],
198 CFClient::SDLK_KP4, [7, "west"],
199 CFClient::SDLK_KP7, [8, "northwest"],
200
201 CFClient::SDLK_UP, [1, "north"],
202 CFClient::SDLK_RIGHT, [3, "east"],
203 CFClient::SDLK_DOWN, [5, "south"],
204 CFClient::SDLK_LEFT, [7, "west"],
205);
206
207sub key_down {
208 my ($self, $ev) = @_;
209
210 return unless $::CONN;
211
212 my $mod = $ev->{mod};
213 my $sym = $ev->{sym};
214 my $uni = $ev->{unicode};
215
216 if ($sym == CFClient::SDLK_KP5) {
217 $::CONN->user_send ("stay fire");
218 } elsif ($uni == ord ",") {
219 $::CONN->user_send ("take");
220 } elsif ($uni == ord "\t" or $uni == ord " ") {
221 $::CONN->user_send ("apply");
222 } elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") {
223 $::CONN->user_send ("rotateshoottype +");
224 } elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") {
225 $::CONN->user_send ("rotateshoottype -");
226 } elsif ($uni == ord "'") {
227 $self->emit ('activate_console');
228 } elsif ($uni == ord "/") {
229 $self->emit (activate_console => '/');
230 } elsif (exists $DIR{$sym}) {
231 if ($mod & CFClient::KMOD_SHIFT) {
232 $self->{shft}++;
233 $::CONN->user_send ("fire $DIR{$sym}[0]");
234 } elsif ($mod & CFClient::KMOD_CTRL) {
235 $self->{ctrl}++;
236 $::CONN->user_send ("run $DIR{$sym}[0]");
237 } else {
238 $::CONN->user_send ("$DIR{$sym}[1]");
239 }
240 } elsif ($ev->{unicode}) {
241 $self->{command_widget} ||=
242 new CFClient::MapWidget::Command::
243 command => $self->{command},
244 can_focus => 1,
245 connect_execute => sub {
246 # todo: support callback instead of user_send
247 $::CONN->user_send ($_[1]);
248 },
249 connect_close => sub {
250 (delete $self->{command_widget})->hide;
251 $self->focus_in;
252 },
253 ;
254 $self->{command_widget}->key_down ($ev)
255 unless $ev->{unicode} == 20;
256 return unless $self->{command_widget};
257 $self->{command_widget}->show;
258 $self->{command_widget}->focus_in;
259 }
260}
261
262sub key_up {
263 my ($self, $ev) = @_;
264
265 my $mod = $ev->{mod};
266 my $sym = $ev->{sym};
267
268 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
269 $::CONN->user_send ("fire_stop");
270 }
271 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
272 $::CONN->user_send ("run_stop");
273 }
274}
275
276sub add_command {
277 my ($self, $command, $tooltip, $widget, $cb) = @_;
278
279 (my $data = $command) =~ s/\\//g;
280
281 $tooltip =~ s/^\s+//;
282
283 $tooltip = "<big>$data</big>\n\n$tooltip";
284
285 $tooltip =~ s/\s+$//;
286
287 $self->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
288}
289
290sub clr_commands {
291 my ($self) = @_; 285 my $self = shift;
292 286
293 %{$self->{command}} = (); 287 glDeleteList $self->{list};
288
289 $self->SUPER::DESTROY;
294} 290}
295 291
296package CFClient::MapWidget::MapMap; 292package CFClient::MapWidget::MapMap;
297 293
298our @ISA = CFClient::UI::Base::; 294our @ISA = CFClient::UI::Base::;
401 ]; 397 ];
402 398
403 $self->{entry} = new CFClient::UI::Entry 399 $self->{entry} = new CFClient::UI::Entry
404 connect_changed => sub { 400 connect_changed => sub {
405 $self->update_labels; 401 $self->update_labels;
402 },
403 connect_key_down => sub {
404 my ($entry, $ev) = @_;
405
406 my $self = $entry->{parent}{parent};
407
408 if ($ev->{sym} == 13) {
409 if (exists $self->{select}) {
410 $::CONN->user_send ($self->{select});
411 $self->hide;
412 }
413 } elsif ($ev->{sym} == 27) {
414 $self->hide;
415 return;
416 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
417 ++$self->{select_offset}
418 if $self->{select_offset} < $#{ $self->{last_match} || [] };
419 $self->update_labels;
420 } elsif ($ev->{sym} == CFClient::SDLK_UP) {
421 --$self->{select_offset}
422 if $self->{select_offset};
423 $self->update_labels;
424 } else {
425 return 0;
426 }
427
428 1
406 }; 429 }
430 ;
407 431
408 $self->{vbox}->add ( 432 $self->{vbox}->add (
409 $self->{entry}, 433 $self->{entry},
410 @{$self->{label}}, 434 @{$self->{label}},
411 ); 435 );
412 436
413 $self 437 $self
414} 438}
415 439
440sub set_prefix {
441 my ($self, $prefix) = @_;
442
443 $self->{entry}->set_text ($prefix);
444 $self->show;
445}
446
416sub size_allocate { 447sub size_allocate {
417 my ($self, $w, $h) = @_; 448 my ($self, $w, $h) = @_;
418 449
419 $self->SUPER::size_allocate ($w, $h); 450 $self->SUPER::size_allocate ($w, $h);
420 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 451 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
421} 452}
422 453
454sub show {
455 my ($self) = @_;
456
457 $self->SUPER::show;
458 $self->{entry}->focus_in;
459}
460
461sub hide {
462 my ($self) = @_;
463
464 $self->SUPER::hide;
465 $self->{entry}->set_text ("");
466}
467
423sub key_down { 468sub key_down {
424 my ($self, $ev) = @_; 469 my ($self, $ev) = @_;
425 470
426 if ($ev->{sym} == 13) { 471 $self->{entry}->key_down ($ev);
427 if (exists $self->{select}) { 472}
428 $self->emit (execute => $self->{select}); 473
429 $self->emit ("close"); 474sub update_labels {
475 my ($self) = @_;
476
477 my $text = $self->{entry}->get_text;
478
479 length $text
480 or return $self->hide;
481
482 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
483
484 if ($text ne $self->{last_search}) {
485 my @match;
486
487 if ($text =~ /^(.*?)\s+$/) {
488 @match = [$cmd, "(appended whitespace suppresses completion)"];
489 } else {
490 my $regexp = do {
491 my ($beg, @chr) = split //, lc $cmd;
492
493 # the following regex is used to match our "completion entry"
494 # to an actual command - the parentheses match kind of "overhead"
495 # - the more characters the parentheses match, the less attractive
496 # is the match.
497 my $regexp = "^\Q$beg\E"
498 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
499 qr<$regexp>
500 };
501
502 my @penalty;
503
504 for (keys %{$self->{command}}) {
505 if (@penalty = $_ =~ $regexp) {
506 push @match, [$_, length join "", map "::$_", grep defined, @penalty];
507 }
508 }
509
510 @match = map $self->{command}{$_->[0]},
511 sort {
512 $a->[1] <=> $b->[1]
513 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
514 or (length $a->[0]) <=> (length $b->[0])
515 } @match;
430 } 516 }
431 } elsif ($ev->{sym} == 27) {
432 $self->{entry}->set_text ("");
433 $self->emit ("close");
434 return;
435 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
436 ++$self->{select_offset}
437 if $self->{select_offset} < $#{ $self->{last_match} || [] };
438 $self->update_labels;
439 } elsif ($ev->{sym} == CFClient::SDLK_UP) {
440 --$self->{select_offset}
441 if $self->{select_offset};
442 $self->update_labels;
443 } else {
444 #$self->{entry}{force_alloc} = 1;
445 $self->{entry}->key_down ($ev);
446 }
447}
448
449sub update_labels {
450 my ($self) = @_;
451
452 my $text = $self->{entry}->get_text;
453
454 length $text
455 or return $self->emit ("close");
456
457 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
458
459 if ($cmd ne $self->{last_search}) {
460 my $regexp = do {
461 my ($beg, @chr) = split //, lc $cmd;
462
463 # the following regex is used to match our "completion entry"
464 # to an actual command - the parentheses match kind of "overhead"
465 # - the more characters the parentheses match, the less attractive
466 # is the match.
467 my $regexp = "^\Q$beg\E"
468 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
469 qr<$regexp>
470 };
471
472 my @match;
473 my @penalty;
474
475 for (keys %{$self->{command}}) {
476 if (@penalty = $_ =~ $regexp) {
477 push @match, [$_, length join "", map "::$_", grep defined, @penalty];
478 }
479 }
480
481 @match = map $self->{command}{$_->[0]},
482 sort {
483 $a->[1] <=> $b->[1]
484 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
485 or (length $a->[0]) <=> (length $b->[0])
486 } @match;
487 517
488 $self->{last_search} = $cmd; 518 $self->{last_search} = $cmd;
489 $self->{last_match} = \@match; 519 $self->{last_match} = \@match;
490 520
491 $self->{select_offset} = 0; 521 $self->{select_offset} = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines