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.26 by root, Wed May 17 16:17:01 2006 UTC vs.
Revision 1.37 by root, Thu May 25 01:26:53 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 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 ;
34
22 $self 35 $self
23} 36}
24 37
25sub 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 {
26 my $self = shift; 53 my ($self) = @_;
27 54
28 glDeleteList $self->{list}; 55 %{$self->{completer}{command}} = ();
29
30 $self->SUPER::DESTROY;
31} 56}
32 57
33sub button_down { 58sub button_down {
34 my ($self, $ev, $x, $y) = @_; 59 my ($self, $ev, $x, $y) = @_;
35 60
76 101
77 $self->{need_update} = 1; 102 $self->{need_update} = 1;
78 $self->SUPER::update; 103 $self->SUPER::update;
79} 104}
80 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
81sub draw { 182sub draw {
82 my ($self) = @_; 183 my ($self) = @_;
83 184
185 my $focused = $CFClient::UI::FOCUS == $self
186 || $CFClient::UI::FOCUS == $self->{completer}{entry};
187
84 return 188 return
85 unless $CFClient::UI::FOCUS == $self || !$::FAST; 189 unless $focused || !$::FAST;
86 190
87 if (delete $self->{need_update}) { 191 if (delete $self->{need_update}) {
88 glNewList $self->{list}; 192 glNewList $self->{list};
89 193
90 if ($::MAP) { 194 if ($::MAP) {
115 0.05, 0.13, 0.05, 219 0.05, 0.13, 0.05,
116 ); 220 );
117 glEnable GL_CONVOLUTION_2D; 221 glEnable GL_CONVOLUTION_2D;
118 } 222 }
119 223
224 $self->{fow_texture_name} ||= glGenTexture;
225 # try to re-use the texture name: TODO improve texture class instead
226
120 $self->{fow_texture} = new CFClient::Texture 227 $self->{fow_texture} = new CFClient::Texture
121 w => $w, 228 w => $w,
122 h => $h, 229 h => $h,
123 data => $data, 230 data => $data,
231 name => $self->{fow_texture_name},
124 internalformat => GL_ALPHA, 232 internalformat => GL_ALPHA,
125 format => GL_ALPHA; 233 format => GL_ALPHA;
126 234
127 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; 235 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
128 236
129 glEnable GL_BLEND;
130 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
131 glEnable GL_TEXTURE_2D; 237 glEnable GL_TEXTURE_2D;
132 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 238 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
133 239
134 glColor +($::CFG->{fow_intensity}) x 3, 0.8; 240 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
135 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); 241 $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32);
136 242
137 glDisable GL_TEXTURE_2D; 243 glDisable GL_TEXTURE_2D;
138 glDisable GL_BLEND;
139 } 244 }
140 245
141 glPopMatrix; 246 glPopMatrix;
142 } 247 }
143 248
146 251
147 glPushMatrix; 252 glPushMatrix;
148 glCallList $self->{list}; 253 glCallList $self->{list};
149 glPopMatrix; 254 glPopMatrix;
150 255
151 if ($CFClient::UI::FOCUS != $self) { 256 # TNT2 emulates logops in software (or worse :)
152 glColor 64/255, 64/255, 64/255; 257 if ($focused) {
153 glLogicOp GL_AND; 258 (delete $self->{out_of_focus})->destroy
154 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;
155 glBegin GL_QUADS; 264 glBegin GL_QUADS;
156 glVertex 0, 0; 265 glVertex 0, 0;
157 glVertex 0, $::HEIGHT; 266 glVertex 0, $::HEIGHT;
158 glVertex $::WIDTH, $::HEIGHT; 267 glVertex $::WIDTH, $::HEIGHT;
159 glVertex $::WIDTH, 0; 268 glVertex $::WIDTH, 0;
160 glEnd; 269 glEnd;
161 glDisable GL_COLOR_LOGIC_OP; 270 glDisable GL_BLEND;
162 }
163}
164 271
165my %DIR = ( 272 $self->{out_of_focus} ||= do {
166 CFClient::SDLK_KP8, [1, "north"], 273 my $label = new CFClient::UI::Label
167 CFClient::SDLK_KP9, [2, "northeast"], 274 x => 0,
168 CFClient::SDLK_KP6, [3, "east"], 275 y => 0,
169 CFClient::SDLK_KP3, [4, "southeast"], 276 z => 1,
170 CFClient::SDLK_KP2, [5, "south"], 277 ellipsise => 0,
171 CFClient::SDLK_KP1, [6, "southwest"], 278 text => "map out of focus (click map to play)";
172 CFClient::SDLK_KP4, [7, "west"],
173 CFClient::SDLK_KP7, [8, "northwest"],
174 279
175 CFClient::SDLK_UP, [1, "north"], 280 $label->show;
176 CFClient::SDLK_RIGHT, [3, "east"], 281 $label->update;
177 CFClient::SDLK_DOWN, [5, "south"],
178 CFClient::SDLK_LEFT, [7, "west"],
179);
180 282
181sub key_down { 283 $CFClient::UI::ROOT->on_post_alloc ("$self$label" => sub {
182 my ($self, $ev) = @_; 284 $label->move (
285 ($::WIDTH - $label->{w}) * 0.5,
286 ($::HEIGHT - $label->{h}) * 0.5,
287 );
288 });
183 289
184 return unless $::CONN; 290 $label
185
186 my $mod = $ev->{mod};
187 my $sym = $ev->{sym};
188 my $uni = $ev->{unicode};
189
190 if ($sym == CFClient::SDLK_KP5) {
191 $::CONN->user_send ("stay fire");
192 } elsif ($uni == ord ",") {
193 $::CONN->user_send ("take");
194 } elsif ($uni == ord "\t") {
195 $::CONN->user_send ("apply");
196 } elsif ($uni == ord "'") {
197 $self->emit ('activate_console');
198 } elsif ($uni == ord "/") {
199 $self->emit (activate_console => '/');
200 } elsif (exists $DIR{$sym}) {
201 if ($mod & CFClient::KMOD_SHIFT) {
202 $self->{shft}++;
203 $::CONN->user_send ("fire $DIR{$sym}[0]");
204 } elsif ($mod & CFClient::KMOD_CTRL) {
205 $self->{ctrl}++;
206 $::CONN->user_send ("run $DIR{$sym}[0]");
207 } else {
208 $::CONN->user_send ("$DIR{$sym}[1]");
209 } 291 };
210 } elsif ($ev->{unicode}) {
211 $self->{command_widget} ||=
212 new CFClient::MapWidget::Command::
213 command => $self->{command},
214 can_focus => 1,
215 connect_execute => sub {
216 # todo: support callback instead of user_send
217 $::CONN->user_send ($_[1]);
218 },
219 connect_close => sub {
220 (delete $self->{command_widget})->hide;
221 $self->focus_in;
222 },
223 ;
224 $self->{command_widget}->key_down ($ev)
225 unless $ev->{unicode} == 20;
226 return unless $self->{command_widget};
227 $self->{command_widget}->show;
228 $self->{command_widget}->focus_in;
229 } 292 }
230} 293}
231 294
232sub key_up { 295sub DESTROY {
233 my ($self, $ev) = @_;
234
235 my $mod = $ev->{mod};
236 my $sym = $ev->{sym};
237
238 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
239 $::CONN->user_send ("fire_stop");
240 }
241 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
242 $::CONN->user_send ("run_stop");
243 }
244}
245
246sub add_command {
247 my ($self, $command, $tooltip, $widget, $cb) = @_;
248
249 (my $data = $command) =~ s/\\//g;
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) = @_; 296 my $self = shift;
262 297
263 %{$self->{command}} = (); 298 glDeleteList $self->{list};
299
300 $self->SUPER::DESTROY;
264} 301}
265 302
266package CFClient::MapWidget::MapMap; 303package CFClient::MapWidget::MapMap;
267 304
268our @ISA = CFClient::UI::Base::; 305our @ISA = CFClient::UI::Base::;
365 CFClient::UI::Label->new ( 402 CFClient::UI::Label->new (
366 can_hover => 1, 403 can_hover => 1,
367 can_events => 1, 404 can_events => 1,
368 tooltip_width => 0.33, 405 tooltip_width => 0.33,
369 fontsize => $_, 406 fontsize => $_,
370 ), (0.8) x 6 407 ), (0.8) x 16
371 ]; 408 ];
372 409
373 $self->{entry} = new CFClient::UI::Entry 410 $self->{entry} = new CFClient::UI::Entry
374 connect_changed => sub { 411 connect_changed => sub {
375 $self->update_labels; 412 $self->update_labels;
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
376 }; 441 }
442 ;
377 443
378 $self->{vbox}->add ( 444 $self->{vbox}->add (
379 $self->{entry}, 445 $self->{entry},
380 @{$self->{label}}, 446 @{$self->{label}},
381 ); 447 );
382 448
383 $self 449 $self
384} 450}
385 451
452sub set_prefix {
453 my ($self, $prefix) = @_;
454
455 $self->{entry}->set_text ($prefix);
456 $self->show;
457}
458
386sub size_allocate { 459sub size_allocate {
387 my ($self, $w, $h) = @_; 460 my ($self, $w, $h) = @_;
388 461
389 $self->SUPER::size_allocate ($w, $h); 462 $self->SUPER::size_allocate ($w, $h);
390 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 463 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
391} 464}
392 465
466sub show {
467 my ($self) = @_;
468
469 $self->SUPER::show;
470 $self->{entry}->focus_in;
471}
472
473sub hide {
474 my ($self) = @_;
475
476 $self->SUPER::hide;
477 $self->{entry}->set_text ("");
478}
479
393sub key_down { 480sub key_down {
394 my ($self, $ev) = @_; 481 my ($self, $ev) = @_;
395 482
396 if ($ev->{sym} == 13) { 483 $self->{entry}->key_down ($ev);
397 if (exists $self->{select}) { 484}
398 $self->emit (execute => $self->{select}); 485
399 $self->emit ("close"); 486sub update_labels {
487 my ($self) = @_;
488
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;
400 } 528 }
401 } elsif ($ev->{sym} == 27) {
402 $self->{entry}->set_text ("");
403 $self->emit ("close");
404 return;
405 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
406 ++$self->{select_offset}
407 if $self->{select_offset} < $#{ $self->{last_match} || [] };
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 }
417}
418
419sub update_labels {
420 my ($self) = @_;
421
422 my $text = $self->{entry}->get_text;
423
424 length $text
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 529
457 $self->{last_search} = $cmd; 530 $self->{last_search} = $cmd;
458 $self->{last_match} = \@match; 531 $self->{last_match} = \@match;
459 532
460 $self->{select_offset} = 0; 533 $self->{select_offset} = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines