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.25 by root, Wed May 17 15:18:57 2006 UTC vs.
Revision 1.40 by root, Sat May 27 20:46:54 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) = @_;
184
185 my $focused = $CFClient::UI::FOCUS == $self
186 || $CFClient::UI::FOCUS == $self->{completer}{entry};
187
188 return
189 unless $focused || !$::FAST;
83 190
84 if (delete $self->{need_update}) { 191 if (delete $self->{need_update}) {
85 glNewList $self->{list}; 192 glNewList $self->{list};
86 193
87 if ($::MAP) { 194 if ($::MAP) {
97 glTranslate $sx0 - 32, $sy0 - 32, 0; 204 glTranslate $sx0 - 32, $sy0 - 32, 0;
98 205
99 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); 206 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
100 207
101 if ($::CFG->{fow_enable}) { 208 if ($::CFG->{fow_enable}) {
102 if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war 209 if ($::CFG->{fow_smooth} && $CFClient::OpenGL::GL_VERSION >= 1.2) { # smooth fog of war
103 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); 210 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
104 glConvolutionFilter2D ( 211 glConvolutionFilter2D (
105 GL_CONVOLUTION_2D, 212 GL_CONVOLUTION_2D,
106 GL_ALPHA, 213 GL_ALPHA,
107 3, 3, 214 3, 3,
112 0.05, 0.13, 0.05, 219 0.05, 0.13, 0.05,
113 ); 220 );
114 glEnable GL_CONVOLUTION_2D; 221 glEnable GL_CONVOLUTION_2D;
115 } 222 }
116 223
224 $self->{fow_texture_name} ||= glGenTexture;
225 # try to re-use the texture name: TODO improve texture class instead
226
117 $self->{fow_texture} = new CFClient::Texture 227 $self->{fow_texture} = new CFClient::Texture
118 w => $w, 228 w => $w,
119 h => $h, 229 h => $h,
120 data => $data, 230 data => $data,
231 name => $self->{fow_texture_name},
121 internalformat => GL_ALPHA, 232 internalformat => GL_ALPHA,
122 format => GL_ALPHA; 233 format => GL_ALPHA;
123 234
124 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; 235 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
125 236
126 glEnable GL_BLEND;
127 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
128 glEnable GL_TEXTURE_2D; 237 glEnable GL_TEXTURE_2D;
129 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 238 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
130 239
131 glColor +($::CFG->{fow_intensity}) x 3, 0.8; 240 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
132 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); 241 $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32);
133 242
134 glDisable GL_TEXTURE_2D; 243 glDisable GL_TEXTURE_2D;
135 glDisable GL_BLEND;
136 } 244 }
137 245
138 glPopMatrix; 246 glPopMatrix;
139 } 247 }
140 248
143 251
144 glPushMatrix; 252 glPushMatrix;
145 glCallList $self->{list}; 253 glCallList $self->{list};
146 glPopMatrix; 254 glPopMatrix;
147 255
148 if ($CFClient::UI::FOCUS != $self) { 256 # TNT2 emulates logops in software (or worse :)
149 glColor 64/255, 64/255, 64/255; 257 if ($focused) {
150 glLogicOp GL_AND; 258 (delete $self->{out_of_focus})->destroy
151 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;
152 glBegin GL_QUADS; 264 glBegin GL_QUADS;
153 glVertex 0, 0; 265 glVertex 0, 0;
154 glVertex 0, $::HEIGHT; 266 glVertex 0, $::HEIGHT;
155 glVertex $::WIDTH, $::HEIGHT; 267 glVertex $::WIDTH, $::HEIGHT;
156 glVertex $::WIDTH, 0; 268 glVertex $::WIDTH, 0;
157 glEnd; 269 glEnd;
158 glDisable GL_COLOR_LOGIC_OP; 270 glDisable GL_BLEND;
159 }
160}
161 271
162my %DIR = ( 272# $self->{out_of_focus} ||= do {
163 CFClient::SDLK_KP8, [1, "north"], 273# my $label = new CFClient::UI::Label
164 CFClient::SDLK_KP9, [2, "northeast"], 274# x => 0,
165 CFClient::SDLK_KP6, [3, "east"], 275# y => 0,
166 CFClient::SDLK_KP3, [4, "southeast"], 276# z => 1,
167 CFClient::SDLK_KP2, [5, "south"], 277# ellipsise => 0,
168 CFClient::SDLK_KP1, [6, "southwest"], 278# text => "map out of focus (click map to play)";
169 CFClient::SDLK_KP4, [7, "west"], 279#
170 CFClient::SDLK_KP7, [8, "northwest"], 280# $label->show;
171 281# $label->update;
172 CFClient::SDLK_UP, [1, "north"], 282#
173 CFClient::SDLK_RIGHT, [3, "east"], 283# $CFClient::UI::ROOT->on_post_alloc ("$self$label" => sub {
174 CFClient::SDLK_DOWN, [5, "south"], 284# $label->move (
175 CFClient::SDLK_LEFT, [7, "west"], 285# ($::WIDTH - $label->{w}) * 0.5,
176); 286# ($::HEIGHT - $label->{h}) * 0.5,
177 287# );
178sub key_down { 288# });
179 my ($self, $ev) = @_; 289#
180 290# $label
181 return unless $::CONN;
182
183 my $mod = $ev->{mod};
184 my $sym = $ev->{sym};
185 my $uni = $ev->{unicode};
186
187 if ($sym == CFClient::SDLK_KP5) {
188 $::CONN->user_send ("stay fire");
189 } elsif ($uni == ord ",") {
190 $::CONN->user_send ("take");
191 } elsif ($uni == ord "\t") {
192 $::CONN->user_send ("apply");
193 } elsif ($uni == ord "'") {
194 $self->emit ('activate_console');
195 } elsif ($uni == ord "/") {
196 $self->emit (activate_console => '/');
197 } elsif (exists $DIR{$sym}) {
198 if ($mod & CFClient::KMOD_SHIFT) {
199 $self->{shft}++;
200 $::CONN->user_send ("fire $DIR{$sym}[0]");
201 } elsif ($mod & CFClient::KMOD_CTRL) {
202 $self->{ctrl}++;
203 $::CONN->user_send ("run $DIR{$sym}[0]");
204 } else {
205 $::CONN->user_send ("$DIR{$sym}[1]");
206 } 291# };
207 } elsif ($ev->{unicode}) {
208 $self->{command_widget} ||=
209 new CFClient::MapWidget::Command::
210 command => $self->{command},
211 can_focus => 1,
212 connect_execute => sub {
213 # todo: support callback instead of user_send
214 $::CONN->user_send ($_[1]);
215 },
216 connect_close => sub {
217 (delete $self->{command_widget})->hide;
218 $self->focus_in;
219 },
220 ;
221 $self->{command_widget}->key_down ($ev)
222 unless $ev->{unicode} == 20;
223 return unless $self->{command_widget};
224 $self->{command_widget}->show;
225 $self->{command_widget}->focus_in;
226 } 292 }
227} 293}
228 294
229sub key_up { 295sub DESTROY {
230 my ($self, $ev) = @_;
231
232 my $mod = $ev->{mod};
233 my $sym = $ev->{sym};
234
235 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
236 $::CONN->user_send ("fire_stop");
237 }
238 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
239 $::CONN->user_send ("run_stop");
240 }
241}
242
243sub add_command {
244 my ($self, $command, $tooltip, $widget, $cb) = @_;
245
246 (my $data = $command) =~ s/\\//g;
247
248 $tooltip =~ s/^\s+//;
249
250 $tooltip = "<big>$data</big>\n\n$tooltip";
251
252 $tooltip =~ s/\s+$//;
253
254 $self->{command}{$command} = [$data, $tooltip, $widget, $cb];
255}
256
257sub clr_commands {
258 my ($self) = @_; 296 my $self = shift;
259 297
260 %{$self->{command}} = (); 298 glDeleteList $self->{list};
299
300 $self->SUPER::DESTROY;
261} 301}
262 302
263package CFClient::MapWidget::MapMap; 303package CFClient::MapWidget::MapMap;
264 304
265our @ISA = CFClient::UI::Base::; 305our @ISA = CFClient::UI::Base::;
362 CFClient::UI::Label->new ( 402 CFClient::UI::Label->new (
363 can_hover => 1, 403 can_hover => 1,
364 can_events => 1, 404 can_events => 1,
365 tooltip_width => 0.33, 405 tooltip_width => 0.33,
366 fontsize => $_, 406 fontsize => $_,
367 ), (0.8) x 6 407 ), (0.8) x 16
368 ]; 408 ];
369 409
370 $self->{entry} = new CFClient::UI::Entry 410 $self->{entry} = new CFClient::UI::Entry
371 connect_changed => sub { 411 on_changed => sub {
372 $self->update_labels; 412 $self->update_labels;
413 },
414 on_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
373 }; 441 }
442 ;
374 443
375 $self->{vbox}->add ( 444 $self->{vbox}->add (
376 $self->{entry}, 445 $self->{entry},
377 @{$self->{label}}, 446 @{$self->{label}},
378 ); 447 );
379 448
380 $self 449 $self
381} 450}
382 451
452sub set_prefix {
453 my ($self, $prefix) = @_;
454
455 $self->{entry}->set_text ($prefix);
456 $self->show;
457}
458
383sub size_allocate { 459sub size_allocate {
384 my ($self, $w, $h) = @_; 460 my ($self, $w, $h) = @_;
385 461
386 $self->SUPER::size_allocate ($w, $h); 462 $self->SUPER::size_allocate ($w, $h);
387 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 463 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
388} 464}
389 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
390sub key_down { 480sub key_down {
391 my ($self, $ev) = @_; 481 my ($self, $ev) = @_;
392 482
393 if ($ev->{sym} == 13) { 483 $self->{entry}->key_down ($ev);
394 if (exists $self->{select}) { 484}
395 $self->emit (execute => $self->{select}); 485
396 $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 $b->[0]) <=> (length $a->[0])
527 } @match;
397 } 528 }
398 } elsif ($ev->{sym} == 27) {
399 $self->{entry}->set_text ("");
400 $self->emit ("close");
401 return;
402 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
403 ++$self->{select_offset}
404 if $self->{select_offset} < $#{ $self->{last_match} || [] };
405 $self->update_labels;
406 } elsif ($ev->{sym} == CFClient::SDLK_UP) {
407 --$self->{select_offset}
408 if $self->{select_offset};
409 $self->update_labels;
410 } else {
411 #$self->{entry}{force_alloc} = 1;
412 $self->{entry}->key_down ($ev);
413 }
414}
415 529
416sub update_labels {
417 my ($self) = @_;
418
419 my $text = $self->{entry}->get_text;
420
421 length $text
422 or return $self->emit ("close");
423
424 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
425
426 if ($cmd ne $self->{last_search}) {
427 my $regexp = do {
428 my ($beg, @chr) = split //, lc $cmd;
429
430 # the following regex is used to match our "completion entry"
431 # to an actual command - the parentheses match kind of "overhead"
432 # - the more characters the parentheses match, the less attractive
433 # is the match.
434 my $regexp = "^\Q$beg\E"
435 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
436 qr<$regexp>
437 };
438
439 my @match;
440 my @penalty;
441
442 for (keys %{$self->{command}}) {
443 if (@penalty = $_ =~ $regexp) {
444 push @match, [$_, length join "", map "::$_", grep defined, @penalty];
445 }
446 }
447
448 @match = map $self->{command}{$_->[0]},
449 sort {
450 $a->[1] <=> $b->[1]
451 or (length $a->[0]) <=> (length $b->[0])
452 } @match;
453
454 $self->{last_search} = $cmd; 530 $self->{last_search} = $text;
455 $self->{last_match} = \@match; 531 $self->{last_match} = \@match;
456 532
457 $self->{select_offset} = 0; 533 $self->{select_offset} = 0;
458 } 534 }
459 535

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines