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.14 by root, Mon Apr 24 11:54:26 2006 UTC vs.
Revision 1.25 by root, Wed May 17 15:18:57 2006 UTC

10our @ISA = CFClient::UI::Base::; 10our @ISA = CFClient::UI::Base::;
11 11
12sub new { 12sub new {
13 my $class = shift; 13 my $class = shift;
14 14
15 $class->SUPER::new ( 15 my $self = $class->SUPER::new (
16 z => -1, 16 z => -1,
17 can_focus => 1, 17 can_focus => 1,
18 list => glGenList, 18 list => glGenList,
19 @_ 19 @_
20 ) 20 );
21
22 $self
21} 23}
22 24
23sub DESTROY { 25sub DESTROY {
24 my $self = shift; 26 my $self = shift;
25 27
81 83
82 if (delete $self->{need_update}) { 84 if (delete $self->{need_update}) {
83 glNewList $self->{list}; 85 glNewList $self->{list};
84 86
85 if ($::MAP) { 87 if ($::MAP) {
86 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}); 88 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
87 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}); 89 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
88
89 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
90 90
91 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; 91 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
92 my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; 92 my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
93
94 glPushMatrix;
95 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
93 96
94 glTranslate $sx0 - 32, $sy0 - 32, 0; 97 glTranslate $sx0 - 32, $sy0 - 32, 0;
95 98
96 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); 99 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
97 100
130 133
131 glDisable GL_TEXTURE_2D; 134 glDisable GL_TEXTURE_2D;
132 glDisable GL_BLEND; 135 glDisable GL_BLEND;
133 } 136 }
134 137
135 # HACK BEGIN 138 glPopMatrix;
136 {
137 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
138
139 glTranslate 0, 30;
140 my ($w, $h) = (250, 250);
141
142 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
143
144 glEnable GL_BLEND;
145 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
146 glEnable GL_TEXTURE_2D;
147 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
148
149 $self->{mapmap_texture} =
150 new CFClient::Texture
151 w => $w,
152 h => $h,
153 data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h),
154 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
155
156 $self->{mapmap_texture}->draw_quad (0, 0);
157
158 glDisable GL_TEXTURE_2D;
159
160 glTranslate 0.375, 0.375;
161
162 glColor 1, 1, 0, 1;
163 glBegin GL_LINE_LOOP;
164 glVertex $w * 0.5 - $sx , $h * 0.5 - $sy ;
165 glVertex $w * 0.5 - $sx , $h * 0.5 - $sy + $sh;
166 glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy + $sh;
167 glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy ;
168 glEnd;
169
170 glDisable GL_BLEND;
171 }
172 # HACK END
173 } 139 }
174 140
175 glEndList; 141 glEndList;
176 } 142 }
177 143
210); 176);
211 177
212sub key_down { 178sub key_down {
213 my ($self, $ev) = @_; 179 my ($self, $ev) = @_;
214 180
181 return unless $::CONN;
182
215 my $mod = $ev->{mod}; 183 my $mod = $ev->{mod};
216 my $sym = $ev->{sym}; 184 my $sym = $ev->{sym};
185 my $uni = $ev->{unicode};
217 186
218 if ($sym == CFClient::SDLK_KP5) { 187 if ($sym == CFClient::SDLK_KP5) {
219 $::CONN->user_send ("stay fire"); 188 $::CONN->user_send ("stay fire");
220 } elsif ($sym == ord ",") { 189 } elsif ($uni == ord ",") {
221 $::CONN->user_send ("take"); 190 $::CONN->user_send ("take");
222 } elsif ($sym == ord "a") { 191 } elsif ($uni == ord "\t") {
223 $::CONN->user_send ("apply"); 192 $::CONN->user_send ("apply");
224 } elsif ($sym == ord "'") { 193 } elsif ($uni == ord "'") {
225 $self->emit ('activate_console'); 194 $self->emit ('activate_console');
226 } elsif ($sym == ord "/") { 195 } elsif ($uni == ord "/") {
227 $self->emit (activate_console => '/'); 196 $self->emit (activate_console => '/');
228 } elsif (exists $DIR{$sym}) { 197 } elsif (exists $DIR{$sym}) {
229 if ($mod & CFClient::KMOD_SHIFT) { 198 if ($mod & CFClient::KMOD_SHIFT) {
230 $self->{shft}++; 199 $self->{shft}++;
231 $::CONN->user_send ("fire $DIR{$sym}[0]"); 200 $::CONN->user_send ("fire $DIR{$sym}[0]");
240 new CFClient::MapWidget::Command:: 209 new CFClient::MapWidget::Command::
241 command => $self->{command}, 210 command => $self->{command},
242 can_focus => 1, 211 can_focus => 1,
243 connect_execute => sub { 212 connect_execute => sub {
244 # todo: support callback instead of user_send 213 # todo: support callback instead of user_send
245 $::CONN->user_send ($_[1][1]); 214 $::CONN->user_send ($_[1]);
246 }, 215 },
247 connect_close => sub { 216 connect_close => sub {
248 (delete $self->{command_widget})->hide; 217 (delete $self->{command_widget})->hide;
249 $self->focus_in; 218 $self->focus_in;
250 }, 219 },
251 ; 220 ;
252 $self->{command_widget}->key_down ($ev); 221 $self->{command_widget}->key_down ($ev)
222 unless $ev->{unicode} == 20;
253 return unless $self->{command_widget}; 223 return unless $self->{command_widget};
254 $self->{command_widget}->show; 224 $self->{command_widget}->show;
255 $self->{command_widget}->focus_in; 225 $self->{command_widget}->focus_in;
256 } 226 }
257} 227}
271} 241}
272 242
273sub add_command { 243sub add_command {
274 my ($self, $command, $tooltip, $widget, $cb) = @_; 244 my ($self, $command, $tooltip, $widget, $cb) = @_;
275 245
276 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; 246 (my $data = $command) =~ s/\\//g;
277 247
248 $tooltip =~ s/^\s+//;
249
250 $tooltip = "<big>$data</big>\n\n$tooltip";
251
252 $tooltip =~ s/\s+$//;
253
278 push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb]; 254 $self->{command}{$command} = [$data, $tooltip, $widget, $cb];
255}
256
257sub clr_commands {
258 my ($self) = @_;
259
260 %{$self->{command}} = ();
261}
262
263package CFClient::MapWidget::MapMap;
264
265our @ISA = CFClient::UI::Base::;
266
267use Time::HiRes qw(time);
268use CFClient::OpenGL;
269
270sub size_request {
271 ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
272}
273
274sub size_allocate {
275 my ($self, $w, $h) = @_;
276
277 $self->SUPER::size_allocate ($w, $h);
278 $self->update;
279}
280
281sub update {
282 my ($self) = @_;
283
284 delete $self->{texture_atime};
285 $self->SUPER::update;
286}
287
288sub _draw {
289 my ($self) = @_;
290
291 $::MAP or return;
292
293 my ($w, $h) = @$self{qw(w h)};
294
295 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
296 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
297
298 my $sx = int $::CFG->{map_shift_x} / 32;
299 my $sy = int $::CFG->{map_shift_y} / 32;
300
301 my $ox = 0.5 * ($w - $sw);
302 my $oy = 0.5 * ($h - $sh);
303
304 glEnable GL_BLEND;
305 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
306 glEnable GL_TEXTURE_2D;
307 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
308
309 if ($self->{texture_atime} < time) {
310 $self->{texture_atime} = time + 1/3;
311
312 $self->{texture} =
313 new CFClient::Texture
314 w => $w,
315 h => $h,
316 data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
317 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
318 }
319
320 $self->{texture}->draw_quad (0, 0);
321
322 glDisable GL_TEXTURE_2D;
323
324 glTranslate 0.375, 0.375;
325
326 #TODO: map scale is completely borked
327
328 my $x0 = int $ox - $sx + 0.5;
329 my $y0 = int $oy - $sy + 0.5;
330
331 glColor 1, 1, 0, 1;
332 glBegin GL_LINE_LOOP;
333 glVertex $x0 , $y0 ;
334 glVertex $x0 , $y0 + $sh;
335 glVertex $x0 + $sw, $y0 + $sh;
336 glVertex $x0 + $sw, $y0 ;
337 glEnd;
338
339 glDisable GL_BLEND;
279} 340}
280 341
281package CFClient::MapWidget::Command; 342package CFClient::MapWidget::Command;
282 343
283use strict; 344use strict;
284 345
285use CFClient::OpenGL; 346use CFClient::OpenGL;
286 347
287our @ISA = CFClient::UI::VBox::; 348our @ISA = CFClient::UI::Frame::;
288 349
289sub new { 350sub new {
290 my $class = shift; 351 my $class = shift;
291 352
292 my $self = $class->SUPER::new ( 353 my $self = $class->SUPER::new (
354 bg => [0, 0, 0, 0.8],
293 @_, 355 @_,
294 children => [map 356 );
357
358 $self->add ($self->{vbox} = new CFClient::UI::VBox);
359
360 $self->{label} = [
361 map
295 CFClient::UI::Label->new ( 362 CFClient::UI::Label->new (
296 can_hover => 1, 363 can_hover => 1,
297 can_events => 1, 364 can_events => 1,
365 tooltip_width => 0.33,
298 fontsize => $_, 366 fontsize => $_,
299 ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8 367 ), (0.8) x 6
368 ];
369
370 $self->{entry} = new CFClient::UI::Entry
371 connect_changed => sub {
372 $self->update_labels;
300 ], 373 };
374
375 $self->{vbox}->add (
376 $self->{entry},
377 @{$self->{label}},
301 ); 378 );
302 379
303 $self 380 $self
304} 381}
305 382
308 385
309 $self->SUPER::size_allocate ($w, $h); 386 $self->SUPER::size_allocate ($w, $h);
310 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10); 387 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
311} 388}
312 389
313sub update_labels {
314 my ($self) = @_;
315
316 my $command = $self->{command};
317 my $search_abbrev = qr/^\Q$self->{search}/;
318 my $search_full = qr/\Q$self->{search}/;
319
320 my @found;
321
322 for (@$command) {
323 if ($_->[0] =~ $search_abbrev) {
324 push @found, [$_->[0], $_];
325 } elsif ($_[1] =~ $search_full) {
326 push @found, [$_->[1], $_];
327 }
328 }
329
330 @found = sort { $a->[0] cmp $b->[0] } @found;
331
332 $self->{children}[0]->set_text ("$self->{search}_");
333
334 for (0..5) {
335 $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : "");
336 $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : "");
337 }
338
339 $self->{select} = $found[0][1]
340 if @found;
341
342 if (@found > 6) {
343 $self->{children}[6]->set_text ("...");
344 }
345
346 $self->check_size;
347}
348
349sub key_down { 390sub key_down {
350 my ($self, $ev) = @_; 391 my ($self, $ev) = @_;
351 392
352 if ($ev->{sym} == 8) { 393 if ($ev->{sym} == 13) {
353 substr $self->{search}, -1, 1, "";
354 $self->update_labels;
355 } elsif ($ev->{sym} == 13) {
356 if (exists $self->{select}) { 394 if (exists $self->{select}) {
357 $self->emit (execute => $self->{select}); 395 $self->emit (execute => $self->{select});
358 $self->emit ("close"); 396 $self->emit ("close");
359 } 397 }
360 } elsif ($ev->{sym} == 27) { 398 } elsif ($ev->{sym} == 27) {
399 $self->{entry}->set_text ("");
361 $self->emit ("close"); 400 $self->emit ("close");
362 return; 401 return;
363 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) { 402 } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
364 $self->{search} .= chr $ev->{unicode}; 403 ++$self->{select_offset}
404 if $self->{select_offset} < $#{ $self->{last_match} || [] };
365 $self->update_labels; 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);
366 } 413 }
414}
367 415
368 length $self->{search} 416sub update_labels {
417 my ($self) = @_;
418
419 my $text = $self->{entry}->get_text;
420
421 length $text
369 or $self->emit ("close"); 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;
455 $self->{last_match} = \@match;
456
457 $self->{select_offset} = 0;
458 }
459
460 my @labels = @{ $self->{label} };
461 my @matches = @{ $self->{last_match} || [] };
462
463 if ($self->{select_offset}) {
464 splice @matches, 0, $self->{select_offset}, ();
465
466 my $label = shift @labels;
467 $label->set_text ("...");
468 $label->set_tooltip ("Use Cursor-Up to view previous matches");
469 }
470
471 for my $label (@labels) {
472 $label->{fg} = [1, 1, 1, 1];
473 $label->{bg} = [0, 0, 0, 0];
474 }
475
476 if (@matches) {
477 $self->{select} = "$matches[0][0]$arg";
478
479 $labels[0]->{fg} = [0, 0, 0, 1];
480 $labels[0]->{bg} = [1, 1, 1, 0.8];
481 } else {
482 $self->{select} = "$cmd$arg";
483 }
484
485 for my $match (@matches) {
486 my $label = shift @labels;
487
488 if (@labels) {
489 $label->set_text ("$match->[0]$arg");
490 $label->set_tooltip ($match->[1]);
491 } else {
492 $label->set_text ("...");
493 $label->set_tooltip ("Use Cursor-Down to view more matches");
494 last;
495 }
496 }
497
498 for my $label (@labels) {
499 $label->set_text ("");
500 $label->set_tooltip ("");
501 }
502
503 $self->update;
504 ###
505}
506
507sub _draw {
508 my ($self) = @_;
509
510 # hack
511 local $CFClient::UI::FOCUS = $self->{entry};
512
513 $self->SUPER::_draw;
370} 514}
371 515
3721 5161

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines