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.107 by root, Sat Apr 14 06:51:45 2007 UTC vs.
Revision 1.117 by root, Fri Aug 10 04:02:13 2007 UTC

20 my $class = shift; 20 my $class = shift;
21 21
22 my $self = $class->SUPER::new ( 22 my $self = $class->SUPER::new (
23 z => -1, 23 z => -1,
24 can_focus => 1, 24 can_focus => 1,
25 list => glGenList,
26 tilesize => 32, 25 tilesize => 32,
27
28 smooth_matrix => [
29 0.05, 0.13, 0.05,
30 0.13, 0.30, 0.13,
31 0.05, 0.13, 0.05,
32 ],
33
34 @_ 26 @_
35 ); 27 );
36 28
37 $self->{completer} = new CFPlus::MapWidget::Command:: 29 $self->{completer} = new CFPlus::MapWidget::Command::
38 command => $self->{command}, 30 command => $self->{command},
191 183
192 $x -= CFPlus::floor $::MAP->w * 0.5; 184 $x -= CFPlus::floor $::MAP->w * 0.5;
193 $y -= CFPlus::floor $::MAP->h * 0.5; 185 $y -= CFPlus::floor $::MAP->h * 0.5;
194 186
195 if ($::CONN) { 187 if ($::CONN) {
196 if ($::IN_BUILD_MODE) {
197 $::CONN->buildat ($::IN_BUILD_MODE, $x, $y);
198 } else {
199 $::CONN->lookat ($x, $y) 188 $::CONN->lookat ($x, $y)
200 }
201 } 189 }
202 190
203 } elsif ($ev->{button} == 2) { 191 } elsif ($ev->{button} == 2) {
204 $self->grab_focus; 192 $self->grab_focus;
205 return unless $::CONN; 193 return unless $::CONN;
308 $self->{need_update} = 1; 296 $self->{need_update} = 1;
309 $self->SUPER::update; 297 $self->SUPER::update;
310} 298}
311 299
312my %DIR = ( 300my %DIR = (
301 ( "," . CFPlus::SDLK_KP5 ), [0, "stay fire"],
313 CFPlus::SDLK_KP8, [1, "north"], 302 ( "," . CFPlus::SDLK_KP8 ), [1, "north"],
314 CFPlus::SDLK_KP9, [2, "northeast"], 303 ( "," . CFPlus::SDLK_KP9 ), [2, "northeast"],
315 CFPlus::SDLK_KP6, [3, "east"], 304 ( "," . CFPlus::SDLK_KP6 ), [3, "east"],
316 CFPlus::SDLK_KP3, [4, "southeast"], 305 ( "," . CFPlus::SDLK_KP3 ), [4, "southeast"],
317 CFPlus::SDLK_KP2, [5, "south"], 306 ( "," . CFPlus::SDLK_KP2 ), [5, "south"],
318 CFPlus::SDLK_KP1, [6, "southwest"], 307 ( "," . CFPlus::SDLK_KP1 ), [6, "southwest"],
319 CFPlus::SDLK_KP4, [7, "west"], 308 ( "," . CFPlus::SDLK_KP4 ), [7, "west"],
320 CFPlus::SDLK_KP7, [8, "northwest"], 309 ( "," . CFPlus::SDLK_KP7 ), [8, "northwest"],
321 310
322 CFPlus::SDLK_UP, [1, "north"], 311 ( "," . CFPlus::SDLK_UP ), [1, "north"],
312 ("1," . CFPlus::SDLK_UP ), [2, "northeast"],
323 CFPlus::SDLK_RIGHT, [3, "east"], 313 ( "," . CFPlus::SDLK_RIGHT), [3, "east"],
314 ("1," . CFPlus::SDLK_RIGHT), [4, "southeast"],
324 CFPlus::SDLK_DOWN, [5, "south"], 315 ( "," . CFPlus::SDLK_DOWN ), [5, "south"],
316 ("1," . CFPlus::SDLK_DOWN ), [6, "southwest"],
325 CFPlus::SDLK_LEFT, [7, "west"], 317 ( "," . CFPlus::SDLK_LEFT ), [7, "west"],
318 ("1," . CFPlus::SDLK_LEFT ), [8, "northwest"],
326); 319);
327 320
328sub invoke_key_down { 321sub invoke_key_down {
329 my ($self, $ev) = @_; 322 my ($self, $ev) = @_;
330 323
333 my $uni = $ev->{unicode}; 326 my $uni = $ev->{unicode};
334 327
335 $mod &= CFPlus::KMOD_CTRL | CFPlus::KMOD_ALT | CFPlus::KMOD_SHIFT; 328 $mod &= CFPlus::KMOD_CTRL | CFPlus::KMOD_ALT | CFPlus::KMOD_SHIFT;
336 329
337 if ($sym == 9) { 330 if ($sym == 9) {
338 ($mod & CFPlus::KMOD_SHIFT ? $::CONSOLE->{window} : $::PL_WINDOW)->toggle_visibility; 331 ($mod & CFPlus::KMOD_SHIFT ? $::MESSAGE_WINDOW : $::PL_WINDOW)->toggle_visibility;
339 } elsif ($sym == CFPlus::SDLK_F1 && !$mod) { 332 } elsif ($sym == CFPlus::SDLK_F1 && !$mod) {
340 $::HELP_WINDOW->toggle_visibility; 333 $::HELP_WINDOW->toggle_visibility;
341 } elsif ($sym == CFPlus::SDLK_F2 && !$mod) { 334 } elsif ($sym == CFPlus::SDLK_F2 && !$mod) {
342 ::toggle_player_page ($::STATS_PAGE); 335 ::toggle_player_page ($::STATS_PAGE);
343 } elsif ($sym == CFPlus::SDLK_F3 && !$mod) { 336 } elsif ($sym == CFPlus::SDLK_F3 && !$mod) {
349 } elsif ($sym == CFPlus::SDLK_F9 && !$mod) { 342 } elsif ($sym == CFPlus::SDLK_F9 && !$mod) {
350 $::SETUP_DIALOG->toggle_visibility; 343 $::SETUP_DIALOG->toggle_visibility;
351 } elsif (!$::CONN) { 344 } elsif (!$::CONN) {
352 return 0; # bindings further down need a valid connection 345 return 0; # bindings further down need a valid connection
353 346
354 } elsif ($sym == CFPlus::SDLK_KP5 && !($mod & ~CFPlus::KMOD_SHIFT)) {
355 $::CONN->user_send ("stay fire");
356 } elsif ($uni == ord ",") { 347 } elsif ($uni == ord ",") {
357 $::CONN->user_send ("take"); 348 $::CONN->user_send ("take");
358 } elsif ($uni == ord " ") { 349 } elsif ($uni == ord " ") {
359 $::CONN->user_send ("apply"); 350 $::CONN->user_send ("apply");
360 } elsif ($uni == 13) { 351 } elsif ($uni == 13) {
375 $self->{completer}->set_prefix ("$::CFG->{say_command} "); 366 $self->{completer}->set_prefix ("$::CFG->{say_command} ");
376 $self->{completer}->show; 367 $self->{completer}->show;
377 } elsif ($uni == ord "'") { 368 } elsif ($uni == ord "'") {
378 $self->{completer}->set_prefix (""); 369 $self->{completer}->set_prefix ("");
379 $self->{completer}->show; 370 $self->{completer}->show;
380 } elsif (exists $DIR{$sym}) { 371 } elsif (my $dir = $DIR{(!!($mod & CFPlus::KMOD_ALT)) . ",$sym"}) {
381 if ($mod & CFPlus::KMOD_SHIFT) { 372 if ($mod & CFPlus::KMOD_SHIFT) {
382 $self->{shft}++; 373 $self->{shft}++;
383 if ($DIR{$sym}[0] != $self->{fire_dir}) { 374 if ($dir->[0] != $self->{fire_dir}) {
384 $::CONN->user_send ("fire $DIR{$sym}[0]"); 375 $::CONN->user_send ("fire $dir->[0]");
385 } 376 }
386 $self->{fire_dir} = $DIR{$sym}[0]; 377 $self->{fire_dir} = $DIR{$sym}[0];
387 } elsif ($mod & CFPlus::KMOD_CTRL) { 378 } elsif ($mod & CFPlus::KMOD_CTRL) {
388 $self->{ctrl}++; 379 $self->{ctrl}++;
389 $::CONN->user_send ("run $DIR{$sym}[0]"); 380 $::CONN->user_send ("run $dir->[0]");
390 } else { 381 } else {
391 $::CONN->user_send ("$DIR{$sym}[1]"); 382 $::CONN->user_send ("$dir->[1]");
392 } 383 }
393 } elsif ((ord 'a') <= $uni && $uni <= (ord 'z')) { 384 } elsif ((ord 'a') <= $uni && $uni <= (ord 'z')) {
394 $self->{completer}->inject_key_down ($ev); 385 $self->{completer}->inject_key_down ($ev);
395 $self->{completer}->show; 386 $self->{completer}->show;
396 } else { 387 } else {
431 } 422 }
432 423
433 $res 424 $res
434} 425}
435 426
427sub invoke_visibility_change {
428 my ($self) = @_;
429
430 $self->refresh_hook;
431
432 0
433}
434
436sub set_magicmap { 435sub set_magicmap {
437 my ($self, $w, $h, $x, $y, $data) = @_; 436 my ($self, $w, $h, $x, $y, $data) = @_;
438 437
439 $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w; 438 $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w;
440 $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h; 439 $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h;
442 $self->{magicmap} = [$x, $y, $w, $h, $data]; 441 $self->{magicmap} = [$x, $y, $w, $h, $data];
443 442
444 $self->update; 443 $self->update;
445} 444}
446 445
446sub refresh_hook {
447 my ($self) = @_;
448
449 if ($::MAP && $::CONN) {
450 if (delete $self->{need_update}) {
451 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
452
453 my $sw = $self->{sw} = 1 + CFPlus::ceil $self->{w} / $tilesize;
454 my $sh = $self->{sh} = 1 + CFPlus::ceil $self->{h} / $tilesize;
455
456 my $sx = CFPlus::ceil $::CFG->{map_shift_x} / $tilesize;
457 my $sy = CFPlus::ceil $::CFG->{map_shift_y} / $tilesize;
458
459 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
460 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
461
462 my $dx = $self->{dx} = CFPlus::ceil 0.5 * ($::MAP->w - $sw) - $sx;
463 my $dy = $self->{dy} = CFPlus::ceil 0.5 * ($::MAP->h - $sh) - $sy;
464
465 if ($::CFG->{fow_enable}) {
466 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
467
468 $self->{fow_texture} = new CFPlus::Texture
469 w => $w,
470 h => $h,
471 data => $data,
472 internalformat => GL_ALPHA,
473 format => GL_ALPHA;
474 } else {
475 delete $self->{fow_texture};
476 }
477
478 glNewList ($self->{list} ||= glGenList);
479
480 glPushMatrix;
481 glTranslate $sx0, $sy0;
482 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
483
484 $::MAP->draw ($dx, $dy, $sw, $sh, $self->{tilesize});
485
486 glScale $self->{tilesize}, $self->{tilesize};
487
488 if (my $tex = $self->{fow_texture}) {
489 glPushMatrix;
490 glScale 1/3, 1/3;
491 glEnable GL_TEXTURE_2D;
492 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
493
494 glColor +($::CFG->{fow_intensity}) x 3, 0.9;
495 $self->{fow_texture}->draw_quad_alpha (0, 0);
496
497 glDisable GL_TEXTURE_2D;
498 glPopMatrix;
499 }
500
501 if ($self->{magicmap}) {
502 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
503
504 $x += $::MAP->ox + $self->{dx};
505 $y += $::MAP->oy + $self->{dy};
506
507 glTranslate - $x - 1, - $y - 1;
508 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
509 $::MAP->draw_magicmap ($x, $y, $w, $h, $data);
510 }
511
512 glPopMatrix;
513 glEndList;
514 }
515 } else {
516 glDeleteList delete $self->{list}
517 if $self->{list};
518 }
519}
520
447sub draw { 521sub draw {
448 my ($self) = @_; 522 my ($self) = @_;
449 523
450 return unless $::MAP; 524 $self->{root}->on_post_alloc (prepare => sub { $self->refresh_hook });
525
526 return unless $self->{list};
451 527
452 my $focused = $CFPlus::UI::FOCUS == $self 528 my $focused = $CFPlus::UI::FOCUS == $self
453 || $CFPlus::UI::FOCUS == $self->{completer}{entry}; 529 || $CFPlus::UI::FOCUS == $self->{completer}{entry};
454 530
455 return 531 return
456 unless $focused || !$::FAST; 532 unless $focused || !$::FAST;
457
458 if (delete $self->{need_update}) {
459 my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
460
461 my $sw = $self->{sw} = 1 + CFPlus::ceil $self->{w} / $tilesize;
462 my $sh = $self->{sh} = 1 + CFPlus::ceil $self->{h} / $tilesize;
463
464 my $sx = CFPlus::ceil $::CFG->{map_shift_x} / $tilesize;
465 my $sy = CFPlus::ceil $::CFG->{map_shift_y} / $tilesize;
466
467 my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
468 my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
469
470 my $dx = $self->{dx} = CFPlus::ceil 0.5 * ($::MAP->w - $sw) - $sx;
471 my $dy = $self->{dy} = CFPlus::ceil 0.5 * ($::MAP->h - $sh) - $sy;
472
473 if ($::CFG->{fow_enable}) {
474 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
475
476 if ($::CFG->{fow_smooth} && $CFPlus::OpenGL::GL_VERSION >= 1.2) { # smooth fog of war
477 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
478 glConvolutionFilter2D (
479 GL_CONVOLUTION_2D,
480 GL_ALPHA,
481 3, 3,
482 GL_ALPHA, GL_FLOAT,
483 (pack "f*", @{ $self->{smooth_matrix} }),
484 );
485 glEnable GL_CONVOLUTION_2D;
486 }
487
488 $self->{fow_texture} = new CFPlus::Texture
489 w => $w,
490 h => $h,
491 data => $data,
492 internalformat => GL_ALPHA,
493 format => GL_ALPHA;
494
495 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
496 } else {
497 delete $self->{fow_texture};
498 }
499
500 glNewList $self->{list};
501
502 glPushMatrix;
503 glTranslate $sx0, $sy0;
504 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
505
506 $::MAP->draw ($dx, $dy, $sw, $sh, $self->{tilesize});
507
508 glScale $self->{tilesize}, $self->{tilesize};
509
510 if (my $tex = $self->{fow_texture}) {
511 glEnable GL_TEXTURE_2D;
512 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
513
514 glColor +($::CFG->{fow_intensity}) x 3, 0.9;
515 $self->{fow_texture}->draw_quad_alpha (0, 0);
516
517 glDisable GL_TEXTURE_2D;
518 }
519
520 if ($self->{magicmap}) {
521 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
522
523 $x += $::MAP->ox + $self->{dx};
524 $y += $::MAP->oy + $self->{dy};
525
526 glTranslate - $x - 1, - $y - 1;
527 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
528 $::MAP->draw_magicmap ($x, $y, $w, $h, $data);
529 }
530
531 glPopMatrix;
532 glEndList;
533 }
534 533
535 glCallList $self->{list}; 534 glCallList $self->{list};
536 535
537 # TNT2 emulates logops in software (or worse :) 536 # TNT2 emulates logops in software (or worse :)
538 unless ($focused) { 537 unless ($focused) {
566 565
567use Time::HiRes qw(time); 566use Time::HiRes qw(time);
568use CFPlus::OpenGL; 567use CFPlus::OpenGL;
569 568
570sub size_request { 569sub size_request {
571 ($::HEIGHT * 0.25, $::HEIGHT * 0.25) 570 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
572} 571}
573 572
574sub invoke_size_allocate { 573sub refresh_hook {
575 my ($self, $w, $h) = @_; 574 my ($self) = @_;
576 575
577 $self->update; 576 if ($::MAP && $self->{texture_atime} < time) {
578
579 1
580}
581
582sub update {
583 my ($self) = @_;
584
585 delete $self->{texture_atime};
586 $self->SUPER::update;
587}
588
589sub _draw {
590 my ($self) = @_;
591
592 $::MAP or return;
593
594 my ($w, $h) = @$self{qw(w h)}; 577 my ($w, $h) = @$self{qw(w h)};
595 578
596 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 579 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
597 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 580 my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
598 581
582 my $ox = 0.5 * ($w - $sw);
583 my $oy = 0.5 * ($h - $sh);
584
599 my $sx = int $::CFG->{map_shift_x} / $::MAPWIDGET->{tilesize}; 585 my $sx = int $::CFG->{map_shift_x} / $::MAPWIDGET->{tilesize};
600 my $sy = int $::CFG->{map_shift_y} / $::MAPWIDGET->{tilesize}; 586 my $sy = int $::CFG->{map_shift_y} / $::MAPWIDGET->{tilesize};
601 587
602 my $ox = 0.5 * ($w - $sw); 588 #TODO: map scale is completely borked
603 my $oy = 0.5 * ($h - $sh);
604 589
605 glEnable GL_BLEND; 590 my $x0 = int $ox - $sx + 0.5;
606 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 591 my $y0 = int $oy - $sy + 0.5;
607 glEnable GL_TEXTURE_2D;
608 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
609 592
610 if ($self->{texture_atime} < time) { 593 $self->{sw} = $sw;
594 $self->{sh} = $sh;
595
596 $self->{x0} = $x0;
597 $self->{y0} = $y0;
598
611 $self->{texture_atime} = time + 1/3; 599 $self->{texture_atime} = time + 1/3;
612 600
613 $self->{texture} = 601 $self->{texture} =
614 new CFPlus::Texture 602 new CFPlus::Texture
615 w => $w, 603 w => $w,
616 h => $h, 604 h => $h,
617 data => $::MAP->mapmap (-$ox, -$oy, $w, $h), 605 data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
618 type => $CFPlus::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; 606 type => $CFPlus::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
619 } 607 }
608}
609
610sub invoke_visibility_change {
611 my ($self) = @_;
612
613 $self->refresh_hook;
614
615 0
616}
617
618sub invoke_size_allocate {
619 my ($self, $w, $h) = @_;
620
621 $self->update;
622
623 1
624}
625
626sub update {
627 my ($self) = @_;
628
629 delete $self->{texture_atime};
630 $self->SUPER::update;
631}
632
633sub _draw {
634 my ($self) = @_;
635
636 $self->{root}->on_post_alloc (texture => sub { $self->refresh_hook });
637
638 $self->{texture} or return;
639
640 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
620 644
621 $self->{texture}->draw_quad (0, 0); 645 $self->{texture}->draw_quad (0, 0);
622 646
623 glDisable GL_TEXTURE_2D; 647 glDisable GL_TEXTURE_2D;
624 648
625 glTranslate 0.375, 0.375; 649 glTranslate 0.375, 0.375;
626
627 #TODO: map scale is completely borked
628
629 my $x0 = int $ox - $sx + 0.5;
630 my $y0 = int $oy - $sy + 0.5;
631 650
632 glColor 1, 1, 0, 1; 651 glColor 1, 1, 0, 1;
633 glBegin GL_LINE_LOOP; 652 glBegin GL_LINE_LOOP;
634 glVertex $x0 , $y0 ; 653 glVertex $self->{x0} , $self->{y0} ;
635 glVertex $x0 , $y0 + $sh; 654 glVertex $self->{x0} , $self->{y0} + $self->{sh};
636 glVertex $x0 + $sw, $y0 + $sh; 655 glVertex $self->{x0} + $self->{sw}, $self->{y0} + $self->{sh};
637 glVertex $x0 + $sw, $y0 ; 656 glVertex $self->{x0} + $self->{sw}, $self->{y0} ;
638 glEnd; 657 glEnd;
639 658
640 glDisable GL_BLEND; 659 glDisable GL_BLEND;
641} 660}
642 661

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines