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.38 by root, Thu May 25 02:23:14 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"); 22 $self->{completer} = new CFClient::MapWidget::Command::
23 command => $self->{command},
24 can_focus => 1,
25 tooltip => "<b>The Command Completer</b>\n\n"
26 . "This is your central interface to send text commands to the server. "
27 . "To enter a verbatim command to send to the server, just type the command, "
28 . "followed by a space, and press return. "
29 . "Typing the initial letters of words (or just any letters) displays guesses "
30 . "for commands you might want to use.\n"
31 . "You can use the cursor-up and cursor-down keys to select between those guesses.\n"
32 . "<b>Right-Click</b> opens a menu where you cna select further options, sich as redefining keybindings.",
33 ;
23 34
24 $self 35 $self
25} 36}
26 37
27sub DESTROY { 38sub add_command {
39 my ($self, $command, $tooltip, $widget, $cb) = @_;
40
41 (my $data = $command) =~ s/\\//g;
42
43 $tooltip =~ s/^\s+//;
44
45 $tooltip = "<big>$data</big>\n\n$tooltip";
46
47 $tooltip =~ s/\s+$//;
48
49 $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
50}
51
52sub clr_commands {
28 my $self = shift; 53 my ($self) = @_;
29 54
30 glDeleteList $self->{list}; 55 %{$self->{completer}{command}} = ();
31
32 $self->SUPER::DESTROY;
33} 56}
34 57
35sub button_down { 58sub button_down {
36 my ($self, $ev, $x, $y) = @_; 59 my ($self, $ev, $x, $y) = @_;
37 60
78 101
79 $self->{need_update} = 1; 102 $self->{need_update} = 1;
80 $self->SUPER::update; 103 $self->SUPER::update;
81} 104}
82 105
106my %DIR = (
107 CFClient::SDLK_KP8, [1, "north"],
108 CFClient::SDLK_KP9, [2, "northeast"],
109 CFClient::SDLK_KP6, [3, "east"],
110 CFClient::SDLK_KP3, [4, "southeast"],
111 CFClient::SDLK_KP2, [5, "south"],
112 CFClient::SDLK_KP1, [6, "southwest"],
113 CFClient::SDLK_KP4, [7, "west"],
114 CFClient::SDLK_KP7, [8, "northwest"],
115
116 CFClient::SDLK_UP, [1, "north"],
117 CFClient::SDLK_RIGHT, [3, "east"],
118 CFClient::SDLK_DOWN, [5, "south"],
119 CFClient::SDLK_LEFT, [7, "west"],
120);
121
122sub key_down {
123 my ($self, $ev) = @_;
124
125 return unless $::CONN;
126
127 my $mod = $ev->{mod};
128 my $sym = $ev->{sym};
129 my $uni = $ev->{unicode};
130
131 if ($sym == CFClient::SDLK_KP5) {
132 $::CONN->user_send ("stay fire");
133 } elsif ($uni == ord ",") {
134 $::CONN->user_send ("take");
135 } elsif ($uni == ord " ") {
136 $::CONN->user_send ("apply");
137 } elsif ($uni == ord ".") {
138 $::CONN->user_send ($self->{completer}{last_command})
139 if exists $self->{completer}{last_command};
140 } elsif ($uni == ord "\t") {
141 # TODO: toggle inventory
142 } elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") {
143 $::CONN->user_send ("rotateshoottype +");
144 } elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") {
145 $::CONN->user_send ("rotateshoottype -");
146 } elsif ($uni == ord '"') {
147 $self->{completer}->set_prefix ("$::CFG->{say_command} ");
148 $self->{completer}->show;
149 } elsif ($uni == ord "'") {
150 $self->{completer}->set_prefix ("");
151 $self->{completer}->show;
152 } elsif (exists $DIR{$sym}) {
153 if ($mod & CFClient::KMOD_SHIFT) {
154 $self->{shft}++;
155 $::CONN->user_send ("fire $DIR{$sym}[0]");
156 } elsif ($mod & CFClient::KMOD_CTRL) {
157 $self->{ctrl}++;
158 $::CONN->user_send ("run $DIR{$sym}[0]");
159 } else {
160 $::CONN->user_send ("$DIR{$sym}[1]");
161 }
162 } elsif ($ev->{unicode}) {
163 $self->{completer}->key_down ($ev);
164 $self->{completer}->show;
165 }
166}
167
168sub key_up {
169 my ($self, $ev) = @_;
170
171 my $mod = $ev->{mod};
172 my $sym = $ev->{sym};
173
174 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
175 $::CONN->user_send ("fire_stop");
176 }
177 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
178 $::CONN->user_send ("run_stop");
179 }
180}
181
83sub draw { 182sub draw {
84 my ($self) = @_; 183 my ($self) = @_;
184
185 my $focused = $CFClient::UI::FOCUS == $self
186 || $CFClient::UI::FOCUS == $self->{completer}{entry};
187
188 return
189 unless $focused || !$::FAST;
85 190
86 if (delete $self->{need_update}) { 191 if (delete $self->{need_update}) {
87 glNewList $self->{list}; 192 glNewList $self->{list};
88 193
89 if ($::MAP) { 194 if ($::MAP) {
114 0.05, 0.13, 0.05, 219 0.05, 0.13, 0.05,
115 ); 220 );
116 glEnable GL_CONVOLUTION_2D; 221 glEnable GL_CONVOLUTION_2D;
117 } 222 }
118 223
224 $self->{fow_texture_name} ||= glGenTexture;
225 # try to re-use the texture name: TODO improve texture class instead
226
119 $self->{fow_texture} = new CFClient::Texture 227 $self->{fow_texture} = new CFClient::Texture
120 w => $w, 228 w => $w,
121 h => $h, 229 h => $h,
122 data => $data, 230 data => $data,
231 name => $self->{fow_texture_name},
123 internalformat => GL_ALPHA, 232 internalformat => GL_ALPHA,
124 format => GL_ALPHA; 233 format => GL_ALPHA;
125 234
126 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; 235 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
127 236
128 glEnable GL_BLEND;
129 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
130 glEnable GL_TEXTURE_2D; 237 glEnable GL_TEXTURE_2D;
131 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 238 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
132 239
133 glColor +($::CFG->{fow_intensity}) x 3, 0.8; 240 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
134 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); 241 $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32);
135 242
136 glDisable GL_TEXTURE_2D; 243 glDisable GL_TEXTURE_2D;
137 glDisable GL_BLEND;
138 } 244 }
139 245
140 glPopMatrix; 246 glPopMatrix;
141 } 247 }
142 248
145 251
146 glPushMatrix; 252 glPushMatrix;
147 glCallList $self->{list}; 253 glCallList $self->{list};
148 glPopMatrix; 254 glPopMatrix;
149 255
150 if ($CFClient::UI::FOCUS != $self) { 256 # TNT2 emulates logops in software (or worse :)
151 glColor 64/255, 64/255, 64/255; 257 if ($focused) {
152 glLogicOp GL_AND; 258 (delete $self->{out_of_focus})->destroy
153 glEnable GL_COLOR_LOGIC_OP; 259 if $self->{out_of_focus};
260 } else {
261 glColor 0.4, 0.2, 0.2, 0.6;
262 glEnable GL_BLEND;
263 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
154 glBegin GL_QUADS; 264 glBegin GL_QUADS;
155 glVertex 0, 0; 265 glVertex 0, 0;
156 glVertex 0, $::HEIGHT; 266 glVertex 0, $::HEIGHT;
157 glVertex $::WIDTH, $::HEIGHT; 267 glVertex $::WIDTH, $::HEIGHT;
158 glVertex $::WIDTH, 0; 268 glVertex $::WIDTH, 0;
159 glEnd; 269 glEnd;
160 glDisable GL_COLOR_LOGIC_OP; 270 glDisable GL_BLEND;
161 }
162}
163 271
164my %DIR = ( 272# $self->{out_of_focus} ||= do {
165 CFClient::SDLK_KP8, [1, "north"], 273# my $label = new CFClient::UI::Label
166 CFClient::SDLK_KP9, [2, "northeast"], 274# x => 0,
167 CFClient::SDLK_KP6, [3, "east"], 275# y => 0,
168 CFClient::SDLK_KP3, [4, "southeast"], 276# z => 1,
169 CFClient::SDLK_KP2, [5, "south"], 277# ellipsise => 0,
170 CFClient::SDLK_KP1, [6, "southwest"], 278# text => "map out of focus (click map to play)";
171 CFClient::SDLK_KP4, [7, "west"], 279#
172 CFClient::SDLK_KP7, [8, "northwest"], 280# $label->show;
173 281# $label->update;
174 CFClient::SDLK_UP, [1, "north"], 282#
175 CFClient::SDLK_RIGHT, [3, "east"], 283# $CFClient::UI::ROOT->on_post_alloc ("$self$label" => sub {
176 CFClient::SDLK_DOWN, [5, "south"], 284# $label->move (
177 CFClient::SDLK_LEFT, [7, "west"], 285# ($::WIDTH - $label->{w}) * 0.5,
178); 286# ($::HEIGHT - $label->{h}) * 0.5,
179 287# );
180sub key_down { 288# });
181 my ($self, $ev) = @_; 289#
182 290# $label
183 my $mod = $ev->{mod};
184 my $sym = $ev->{sym};
185
186 if ($sym == CFClient::SDLK_KP5) {
187 $::CONN->user_send ("stay fire");
188 } elsif ($sym == ord ",") {
189 $::CONN->user_send ("take");
190 } elsif ($sym == ord "a") {
191 $::CONN->user_send ("apply");
192 } elsif ($sym == ord "'") {
193 $self->emit ('activate_console');
194 } elsif ($sym == ord "/") {
195 $self->emit (activate_console => '/');
196 } elsif (exists $DIR{$sym}) {
197 if ($mod & CFClient::KMOD_SHIFT) {
198 $self->{shft}++;
199 $::CONN->user_send ("fire $DIR{$sym}[0]");
200 } elsif ($mod & CFClient::KMOD_CTRL) {
201 $self->{ctrl}++;
202 $::CONN->user_send ("run $DIR{$sym}[0]");
203 } else {
204 $::CONN->user_send ("$DIR{$sym}[1]");
205 } 291# };
206 } elsif ($ev->{unicode}) {
207 $self->{command_widget} ||=
208 new CFClient::MapWidget::Command::
209 command => $self->{command},
210 can_focus => 1,
211 connect_execute => sub {
212 # todo: support callback instead of user_send
213 $::CONN->user_send ($_[1][1]);
214 },
215 connect_close => sub {
216 (delete $self->{command_widget})->hide;
217 $self->focus_in;
218 },
219 ;
220 $self->{command_widget}->key_down ($ev);
221 return unless $self->{command_widget};
222 $self->{command_widget}->show;
223 $self->{command_widget}->focus_in;
224 } 292 }
225} 293}
226 294
227sub key_up { 295sub DESTROY {
228 my ($self, $ev) = @_; 296 my $self = shift;
229 297
230 my $mod = $ev->{mod}; 298 glDeleteList $self->{list};
231 my $sym = $ev->{sym};
232 299
233 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { 300 $self->SUPER::DESTROY;
234 $::CONN->user_send ("fire_stop");
235 }
236 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
237 $::CONN->user_send ("run_stop");
238 }
239}
240
241sub add_command {
242 my ($self, $command, $tooltip, $widget, $cb) = @_;
243
244 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
245
246 $self->{command} = [
247 [$abbrev, $command, $tooltip, $widget, $cb],
248 grep $_->[1] ne $command, @{ $self->{command} },
249 ];
250} 301}
251 302
252package CFClient::MapWidget::MapMap; 303package CFClient::MapWidget::MapMap;
253 304
254our @ISA = CFClient::UI::Base::; 305our @ISA = CFClient::UI::Base::;
332 383
333use strict; 384use strict;
334 385
335use CFClient::OpenGL; 386use CFClient::OpenGL;
336 387
337our @ISA = CFClient::UI::VBox::; 388our @ISA = CFClient::UI::Frame::;
338 389
339sub new { 390sub new {
340 my $class = shift; 391 my $class = shift;
341 392
342 my $self = $class->SUPER::new ( 393 my $self = $class->SUPER::new (
394 bg => [0, 0, 0, 0.8],
343 @_, 395 @_,
344 children => [map 396 );
397
398 $self->add ($self->{vbox} = new CFClient::UI::VBox);
399
400 $self->{label} = [
401 map
345 CFClient::UI::Label->new ( 402 CFClient::UI::Label->new (
346 can_hover => 1, 403 can_hover => 1,
347 can_events => 1, 404 can_events => 1,
405 tooltip_width => 0.33,
348 fontsize => $_, 406 fontsize => $_,
349 ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 407 ), (0.8) x 16
408 ];
409
410 $self->{entry} = new CFClient::UI::Entry
411 connect_changed => sub {
412 $self->update_labels;
350 ], 413 },
414 connect_key_down => sub {
415 my ($entry, $ev) = @_;
416
417 my $self = $entry->{parent}{parent};
418
419 if ($ev->{sym} == 13) {
420 if (exists $self->{select}) {
421 $self->{last_command} = $self->{select};
422 $::CONN->user_send ($self->{select});
423 $self->hide;
424 }
425 } elsif ($ev->{sym} == 27) {
426 $self->hide;
427 return;
428 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
429 ++$self->{select_offset}
430 if $self->{select_offset} < $#{ $self->{last_match} || [] };
431 $self->update_labels;
432 } elsif ($ev->{sym} == CFClient::SDLK_UP) {
433 --$self->{select_offset}
434 if $self->{select_offset};
435 $self->update_labels;
436 } else {
437 return 0;
438 }
439
440 1
441 }
442 ;
443
444 $self->{vbox}->add (
445 $self->{entry},
446 @{$self->{label}},
351 ); 447 );
352 448
353 $self 449 $self
450}
451
452sub set_prefix {
453 my ($self, $prefix) = @_;
454
455 $self->{entry}->set_text ($prefix);
456 $self->show;
354} 457}
355 458
356sub size_allocate { 459sub size_allocate {
357 my ($self, $w, $h) = @_; 460 my ($self, $w, $h) = @_;
358 461
359 $self->SUPER::size_allocate ($w, $h); 462 $self->SUPER::size_allocate ($w, $h);
360 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 463 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
361} 464}
362 465
363sub update_labels { 466sub show {
364 my ($self) = @_; 467 my ($self) = @_;
365 468
366 my $command = $self->{command}; 469 $self->SUPER::show;
367 my $search_abbrev = qr/^\Q$self->{search}/; 470 $self->{entry}->focus_in;
368 my $search_full = qr/\Q$self->{search}/; 471}
369 472
370 my @found; 473sub hide {
474 my ($self) = @_;
371 475
372 for (@$command) { 476 $self->SUPER::hide;
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 ("..."); 477 $self->{entry}->set_text ("");
394 }
395
396 $self->check_size;
397} 478}
398 479
399sub key_down { 480sub key_down {
400 my ($self, $ev) = @_; 481 my ($self, $ev) = @_;
401 482
402 if ($ev->{sym} == 8) { 483 $self->{entry}->key_down ($ev);
403 substr $self->{search}, -1, 1, ""; 484}
404 $self->update_labels; 485
405 } elsif ($ev->{sym} == 13) { 486sub update_labels {
406 if (exists $self->{select}) { 487 my ($self) = @_;
407 $self->emit (execute => $self->{select}); 488
408 $self->emit ("close"); 489 my $text = $self->{entry}->get_text;
490
491 length $text
492 or return $self->hide;
493
494 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
495
496 if ($text ne $self->{last_search}) {
497 my @match;
498
499 if ($text =~ /^(.*?)\s+$/) {
500 @match = [$cmd, "(appended whitespace suppresses completion)"];
501 } else {
502 my $regexp = do {
503 my ($beg, @chr) = split //, lc $cmd;
504
505 # the following regex is used to match our "completion entry"
506 # to an actual command - the parentheses match kind of "overhead"
507 # - the more characters the parentheses match, the less attractive
508 # is the match.
509 my $regexp = "^\Q$beg\E"
510 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
511 qr<$regexp>
512 };
513
514 my @penalty;
515
516 for (keys %{$self->{command}}) {
517 if (@penalty = $_ =~ $regexp) {
518 push @match, [$_, length join "", map "::$_", grep defined, @penalty];
519 }
520 }
521
522 @match = map $self->{command}{$_->[0]},
523 sort {
524 $a->[1] <=> $b->[1]
525 or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
526 or (length $a->[0]) <=> (length $b->[0])
527 } @match;
409 } 528 }
410 } elsif ($ev->{sym} == 27) { 529
411 $self->emit ("close"); 530 $self->{last_search} = $cmd;
412 return; 531 $self->{last_match} = \@match;
413 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { 532
414 $self->{search} .= chr $ev->{unicode}; 533 $self->{select_offset} = 0;
534 }
535
536 my @labels = @{ $self->{label} };
537 my @matches = @{ $self->{last_match} || [] };
538
539 if ($self->{select_offset}) {
540 splice @matches, 0, $self->{select_offset}, ();
541
542 my $label = shift @labels;
543 $label->set_text ("...");
544 $label->set_tooltip ("Use Cursor-Up to view previous matches");
545 }
546
547 for my $label (@labels) {
548 $label->{fg} = [1, 1, 1, 1];
549 $label->{bg} = [0, 0, 0, 0];
550 }
551
552 if (@matches) {
553 $self->{select} = "$matches[0][0]$arg";
554
555 $labels[0]->{fg} = [0, 0, 0, 1];
556 $labels[0]->{bg} = [1, 1, 1, 0.8];
557 } else {
558 $self->{select} = "$cmd$arg";
559 }
560
561 for my $match (@matches) {
562 my $label = shift @labels;
563
564 if (@labels) {
565 $label->set_text ("$match->[0]$arg");
566 $label->set_tooltip ($match->[1]);
567 } else {
568 $label->set_text ("...");
569 $label->set_tooltip ("Use Cursor-Down to view more matches");
570 last;
571 }
572 }
573
574 for my $label (@labels) {
575 $label->set_text ("");
576 $label->set_tooltip ("");
577 }
578
415 $self->update_labels; 579 $self->update;
416 } 580 ###
581}
417 582
418 length $self->{search} 583sub _draw {
419 or $self->emit ("close"); 584 my ($self) = @_;
585
586 # hack
587 local $CFClient::UI::FOCUS = $self->{entry};
588
589 $self->SUPER::_draw;
420} 590}
421 591
4221 5921

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines