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.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 return unless $::CONN;
184
185 my $mod = $ev->{mod};
186 my $sym = $ev->{sym};
187 my $uni = $ev->{unicode};
188
189 if ($sym == CFClient::SDLK_KP5) {
190 $::CONN->user_send ("stay fire");
191 } elsif ($uni == ord ",") {
192 $::CONN->user_send ("take");
193 } elsif ($uni == ord "a") {
194 $::CONN->user_send ("apply");
195 } elsif ($uni == ord "'") {
196 $self->emit ('activate_console');
197 } elsif ($uni == ord "/") {
198 $self->emit (activate_console => '/');
199 } elsif (exists $DIR{$sym}) {
200 if ($mod & CFClient::KMOD_SHIFT) {
201 $self->{shft}++;
202 $::CONN->user_send ("fire $DIR{$sym}[0]");
203 } elsif ($mod & CFClient::KMOD_CTRL) {
204 $self->{ctrl}++;
205 $::CONN->user_send ("run $DIR{$sym}[0]");
206 } else {
207 $::CONN->user_send ("$DIR{$sym}[1]");
208 } 291# };
209 } elsif ($ev->{unicode}) {
210 $self->{command_widget} ||=
211 new CFClient::MapWidget::Command::
212 command => $self->{command},
213 can_focus => 1,
214 connect_execute => sub {
215 # todo: support callback instead of user_send
216 $::CONN->user_send ($_[1][1]);
217 },
218 connect_close => sub {
219 (delete $self->{command_widget})->hide;
220 $self->focus_in;
221 },
222 ;
223 $self->{command_widget}->key_down ($ev);
224 return unless $self->{command_widget};
225 $self->{command_widget}->show;
226 $self->{command_widget}->focus_in;
227 } 292 }
228} 293}
229 294
230sub key_up { 295sub DESTROY {
231 my ($self, $ev) = @_; 296 my $self = shift;
232 297
233 my $mod = $ev->{mod}; 298 glDeleteList $self->{list};
234 my $sym = $ev->{sym};
235 299
236 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) { 300 $self->SUPER::DESTROY;
237 $::CONN->user_send ("fire_stop");
238 }
239 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
240 $::CONN->user_send ("run_stop");
241 }
242}
243
244sub add_command {
245 my ($self, $command, $tooltip, $widget, $cb) = @_;
246
247 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
248
249 $self->{command} = [
250 [$abbrev, $command, $tooltip, $widget, $cb],
251 grep $_->[1] ne $command, @{ $self->{command} },
252 ];
253} 301}
254 302
255package CFClient::MapWidget::MapMap; 303package CFClient::MapWidget::MapMap;
256 304
257our @ISA = CFClient::UI::Base::; 305our @ISA = CFClient::UI::Base::;
335 383
336use strict; 384use strict;
337 385
338use CFClient::OpenGL; 386use CFClient::OpenGL;
339 387
340our @ISA = CFClient::UI::VBox::; 388our @ISA = CFClient::UI::Frame::;
341 389
342sub new { 390sub new {
343 my $class = shift; 391 my $class = shift;
344 392
345 my $self = $class->SUPER::new ( 393 my $self = $class->SUPER::new (
394 bg => [0, 0, 0, 0.8],
346 @_, 395 @_,
347 children => [map 396 );
397
398 $self->add ($self->{vbox} = new CFClient::UI::VBox);
399
400 $self->{label} = [
401 map
348 CFClient::UI::Label->new ( 402 CFClient::UI::Label->new (
349 can_hover => 1, 403 can_hover => 1,
350 can_events => 1, 404 can_events => 1,
405 tooltip_width => 0.33,
351 fontsize => $_, 406 fontsize => $_,
352 ), 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;
353 ], 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}},
354 ); 447 );
355 448
356 $self 449 $self
450}
451
452sub set_prefix {
453 my ($self, $prefix) = @_;
454
455 $self->{entry}->set_text ($prefix);
456 $self->show;
357} 457}
358 458
359sub size_allocate { 459sub size_allocate {
360 my ($self, $w, $h) = @_; 460 my ($self, $w, $h) = @_;
361 461
362 $self->SUPER::size_allocate ($w, $h); 462 $self->SUPER::size_allocate ($w, $h);
363 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 463 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
364} 464}
365 465
366sub update_labels { 466sub show {
367 my ($self) = @_; 467 my ($self) = @_;
368 468
369 my $command = $self->{command}; 469 $self->SUPER::show;
370 my $search_abbrev = qr/^\Q$self->{search}/; 470 $self->{entry}->focus_in;
371 my $search_full = qr/\Q$self->{search}/; 471}
372 472
373 my @found; 473sub hide {
474 my ($self) = @_;
374 475
375 for (@$command) { 476 $self->SUPER::hide;
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 ("..."); 477 $self->{entry}->set_text ("");
397 }
398
399 $self->check_size;
400} 478}
401 479
402sub key_down { 480sub key_down {
403 my ($self, $ev) = @_; 481 my ($self, $ev) = @_;
404 482
405 if ($ev->{sym} == 8) { 483 $self->{entry}->key_down ($ev);
406 substr $self->{search}, -1, 1, ""; 484}
407 $self->update_labels; 485
408 } elsif ($ev->{sym} == 13) { 486sub update_labels {
409 if (exists $self->{select}) { 487 my ($self) = @_;
410 $self->emit (execute => $self->{select}); 488
411 $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;
412 } 528 }
413 } elsif ($ev->{sym} == 27) { 529
414 $self->emit ("close"); 530 $self->{last_search} = $cmd;
415 return; 531 $self->{last_match} = \@match;
416 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { 532
417 $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
418 $self->update_labels; 579 $self->update;
419 } 580 ###
581}
420 582
421 length $self->{search} 583sub _draw {
422 or $self->emit ("close"); 584 my ($self) = @_;
585
586 # hack
587 local $CFClient::UI::FOCUS = $self->{entry};
588
589 $self->SUPER::_draw;
423} 590}
424 591
4251 5921

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines