ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.55 by root, Mon Apr 10 19:34:04 2006 UTC vs.
Revision 1.67 by root, Tue Apr 11 14:36:59 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use CFClient;
11 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13 13
14our $TOPLEVEL; 14our $TOPLEVEL;
15our $BUTTON_STATE; 15our $BUTTON_STATE;
34 $GRAB->update if $GRAB; 34 $GRAB->update if $GRAB;
35 } 35 }
36 36
37 $BUTTON_STATE |= 1 << ($ev->button - 1); 37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38 38
39 $GRAB->button_down ($ev) if $GRAB; 39 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
40} 40}
41 41
42sub feed_sdl_button_up_event { 42sub feed_sdl_button_up_event {
43 my ($ev) = @_; 43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45 45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47 47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49 51
50 if (!$BUTTON_STATE) { 52 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 53 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 54 $grab->update if $grab;
53 $GRAB->update if $GRAB; 55 $GRAB->update if $GRAB;
65 67
66 $hover->update if $hover; 68 $hover->update if $hover;
67 $HOVER->update if $HOVER; 69 $HOVER->update if $HOVER;
68 } 70 }
69 71
70 $HOVER->mouse_motion ($ev) if $HOVER; 72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71} 73}
72 74
73sub new { 75sub new {
74 my $class = shift; 76 my $class = shift;
75 77
76 bless { @_ }, $class 78 bless {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
77} 84}
78 85
79sub move { 86sub move {
80 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
81 $self->{x} = $x; 88 $self->{x} = $x;
95sub size_allocate { 102sub size_allocate {
96 my ($self, $w, $h) = @_; 103 my ($self, $w, $h) = @_;
97 104
98 $self->{w} = $w; 105 $self->{w} = $w;
99 $self->{h} = $h; 106 $self->{h} = $h;
107}
108
109# translate global koordinates to local coordinate system
110sub translate {
111 my ($self, $x, $y) = @_;
112
113 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
100} 114}
101 115
102sub focus_in { 116sub focus_in {
103 my ($self) = @_; 117 my ($self) = @_;
104 118
201 #$self->deactivate; 215 #$self->deactivate;
202} 216}
203 217
204############################################################################# 218#############################################################################
205 219
220package CFClient::Widget::Empty;
221
222our @ISA = CFClient::Widget::;
223
224sub size_request {
225 (0, 0)
226}
227
228sub draw { }
229
230#############################################################################
231
206package Crossfire::Client::Widget::Container; 232package CFClient::Widget::Container;
207 233
208our @ISA = Crossfire::Client::Widget::; 234our @ISA = CFClient::Widget::;
209 235
210sub new { 236sub new {
211 my ($class, @widgets) = @_; 237 my ($class, %arg) = @_;
212 238
239 my $children = delete $arg{children} || [];
240
213 my $self = $class->SUPER::new (children => []); 241 my $self = $class->SUPER::new (children => [], %arg);
214 $self->add ($_) for @widgets; 242 $self->add ($_) for @$children;
215 243
216 $self 244 $self
217} 245}
218 246
219sub add { 247sub add {
220 my ($self, $chld, $expand) = @_; 248 my ($self, $chld, $expand) = @_;
221 249
222 $chld->{expand} = $expand; 250 $chld->{expand} = $expand;
223 $chld->set_parent ($self); 251 $chld->set_parent ($self);
224 252
225 @{$self->{children}} = 253 $self->{children} = [
226 sort { $a->{z} <=> $b->{z} } 254 sort { $a->{z} <=> $b->{z} }
227 @{$self->{children}}, $chld; 255 @{$self->{children}}, $chld
256 ];
228 257
229 $self->size_allocate ($self->{w}, $self->{h}) 258 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state" 259 if $self->{w}; #TODO: check for "realised state"
231} 260}
232 261
260 $_->draw for @{$self->{children}}; 289 $_->draw for @{$self->{children}};
261} 290}
262 291
263############################################################################# 292#############################################################################
264 293
265package Crossfire::Client::Widget::Bin; 294package CFClient::Widget::Bin;
266 295
267our @ISA = Crossfire::Client::Widget::Container::; 296our @ISA = CFClient::Widget::Container::;
297
298sub new {
299 my ($class, %arg) = @_;
300
301 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::;
302
303 $class->SUPER::new (children => [$child], %arg)
304}
305
306sub add {
307 my ($self, $widget) = @_;
308
309 $self->{children} = [];
310
311 $self->SUPER::add ($widget);
312}
313
314sub remove {
315 my ($self, $widget) = @_;
316
317 $self->SUPER::remove ($widget);
318
319 $self->{children} = [new CFClient::Widget::Empty]
320 unless @{$self->{children}};
321}
268 322
269sub child { $_[0]->{children}[0] } 323sub child { $_[0]->{children}[0] }
270 324
271sub size_request { 325sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 326 $_[0]{children}[0]->size_request if $_[0]{children}[0];
280 if $self->{children}[0] 334 if $self->{children}[0]
281} 335}
282 336
283############################################################################# 337#############################################################################
284 338
285package Crossfire::Client::Widget::Window; 339package CFClient::Widget::Window;
286 340
287our @ISA = Crossfire::Client::Widget::Bin::; 341our @ISA = CFClient::Widget::Bin::;
288 342
289use SDL::OpenGL; 343use SDL::OpenGL;
290 344
291sub new { 345sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_; 346 my ($class, %arg) = @_;
293 347
294 my $self = $class->SUPER::new; 348 my $self = $class->SUPER::new (%arg);
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297} 349}
298 350
299sub update { 351sub update {
300 my ($self) = @_; 352 my ($self) = @_;
301 353
354 # we want to do this delayed...
302 $self->render_chld; 355 $self->render_chld;
303 $self->SUPER::update; 356 $self->SUPER::update;
304} 357}
305 358
306sub render_chld { 359sub render_chld {
307 my ($self) = @_; 360 my ($self) = @_;
308 361
309 $self->{texture} = 362 $self->{texture} =
310 Crossfire::Client::Texture->new_from_opengl ( 363 CFClient::Texture->new_from_opengl (
311 $self->{w}, $self->{h}, sub { $self->child->draw } 364 $self->{w}, $self->{h}, sub { $self->child->draw }
312 ); 365 );
313} 366}
314 367
315sub size_allocate { 368sub size_allocate {
332 or return; 385 or return;
333 386
334 glEnable GL_BLEND; 387 glEnable GL_BLEND;
335 glEnable GL_TEXTURE_2D; 388 glEnable GL_TEXTURE_2D;
336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 389 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
337 glBindTexture GL_TEXTURE_2D, $tex->{name};
338 390
339 glBegin GL_QUADS; 391 $tex->draw_quad (0, 0, $w, $h);
340 glTexCoord 0, 0; glVertex 0, 0;
341 glTexCoord 0, 1; glVertex 0, $h;
342 glTexCoord 1, 1; glVertex $w, $h;
343 glTexCoord 1, 0; glVertex $w, 0;
344 glEnd;
345 392
346 glDisable GL_BLEND; 393 glDisable GL_BLEND;
347 glDisable GL_TEXTURE_2D; 394 glDisable GL_TEXTURE_2D;
348} 395}
349 396
350############################################################################# 397#############################################################################
351 398
352package Crossfire::Client::Widget::Frame; 399package CFClient::Widget::Frame;
353 400
354our @ISA = Crossfire::Client::Widget::Bin::; 401our @ISA = CFClient::Widget::Bin::;
355 402
356use SDL::OpenGL; 403use SDL::OpenGL;
357 404
358sub size_request { 405sub size_request {
359 my ($self) = @_; 406 my ($self) = @_;
382 429
383 my ($w, $h) = $chld->size_request; 430 my ($w, $h) = $chld->size_request;
384 431
385 glBegin GL_QUADS; 432 glBegin GL_QUADS;
386 glColor 0, 0, 0; 433 glColor 0, 0, 0;
387 glTexCoord 0, 0; glVertex 0 , 0; 434 glVertex 0 , 0;
388 glTexCoord 0, 1; glVertex 0 , $h + 4; 435 glVertex 0 , $h + 4;
389 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 436 glVertex $w + 4 , $h + 4;
390 glTexCoord 1, 0; glVertex $w + 4 , 0; 437 glVertex $w + 4 , 0;
391 glEnd; 438 glEnd;
392 439
393 $chld->draw; 440 $chld->draw;
394} 441}
395 442
396############################################################################# 443#############################################################################
397 444
398package Crossfire::Client::Widget::FancyFrame; 445package CFClient::Widget::FancyFrame;
399 446
400our @ISA = Crossfire::Client::Widget::Bin::; 447our @ISA = CFClient::Widget::Bin::;
401 448
402use SDL::OpenGL; 449use SDL::OpenGL;
403 450
404my @tex = 451my @tex =
405 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 452 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
406 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 453 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
407 454
408sub size_request { 455sub size_request {
409 my ($self) = @_; 456 my ($self) = @_;
410 457
429 $w -= $tex[3]->{width}; 476 $w -= $tex[3]->{width};
430 477
431 $h = $h < 0 ? 0 : $h; 478 $h = $h < 0 ? 0 : $h;
432 $w = $w < 0 ? 0 : $w; 479 $w = $w < 0 ? 0 : $w;
433 480
481 my $child = $self->child;
482
434 $self->child->size_allocate ($w, $h); 483 $child->size_allocate ($w, $h);
435 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 484 $child->move ($tex[3]->{width}, $tex[1]->{height});
436} 485}
437 486
438sub _draw { 487sub _draw {
439 my ($self) = @_; 488 my ($self) = @_;
440 489
445 glEnable GL_TEXTURE_2D; 494 glEnable GL_TEXTURE_2D;
446 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 495 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
448 497
449 my $top = $tex[1]; 498 my $top = $tex[1];
450 glBindTexture GL_TEXTURE_2D, $top->{name}; 499 $top->draw_quad (0, 0, $w, $top->{height});
451
452 glBegin GL_QUADS;
453 glTexCoord 0, 0; glVertex 0 , 0;
454 glTexCoord 0, 1; glVertex 0 , $top->{height};
455 glTexCoord 1, 1; glVertex $w , $top->{height};
456 glTexCoord 1, 0; glVertex $w , 0;
457 glEnd;
458 500
459 my $left = $tex[3]; 501 my $left = $tex[3];
460 glBindTexture GL_TEXTURE_2D, $left->{name}; 502 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
461
462 glBegin GL_QUADS;
463 glTexCoord 0, 0; glVertex 0 , $top->{height};
464 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
465 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
466 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
467 glEnd;
468 503
469 my $right = $tex[2]; 504 my $right = $tex[2];
470 glBindTexture GL_TEXTURE_2D, $right->{name}; 505 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
471
472 glBegin GL_QUADS;
473 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
474 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
475 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
476 glTexCoord 1, 0; glVertex $w , $top->{height};
477 glEnd;
478 506
479 my $bottom = $tex[4]; 507 my $bottom = $tex[4];
480 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 508 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
481
482 glBegin GL_QUADS;
483 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
484 glTexCoord 0, 1; glVertex 0 , $h;
485 glTexCoord 1, 1; glVertex $w , $h;
486 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
487 glEnd;
488 509
489 my $bg = $tex[0]; 510 my $bg = $tex[0];
490 glBindTexture GL_TEXTURE_2D, $bg->{name}; 511 glBindTexture GL_TEXTURE_2D, $bg->{name};
491 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 512 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
492 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 513 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
493 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 514 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
494 515
495 my $rep_x = $cw / $bg->{width}; 516 my $rep_x = $cw / $bg->{width};
496 my $rep_y = $ch / $bg->{height}; 517 my $rep_y = $ch / $bg->{height};
497 518
498 glBegin GL_QUADS; 519 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
499 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
500 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
501 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
502 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
503 glEnd;
504 520
505 glDisable GL_BLEND; 521 glDisable GL_BLEND;
506 glDisable GL_TEXTURE_2D; 522 glDisable GL_TEXTURE_2D;
507 523
508 $self->child->draw; 524 $self->child->draw;
509 525
510} 526}
511 527
512############################################################################# 528#############################################################################
513 529
514package Crossfire::Client::Widget::Table; 530package CFClient::Widget::Table;
515 531
516our @ISA = Crossfire::Client::Widget::Bin::; 532our @ISA = CFClient::Widget::Bin::;
517 533
518use SDL::OpenGL; 534use SDL::OpenGL;
519 535
520sub add { 536sub add {
521 my ($self, $x, $y, $chld) = @_; 537 my ($self, $x, $y, $chld) = @_;
596 } 612 }
597} 613}
598 614
599############################################################################# 615#############################################################################
600 616
601package Crossfire::Client::Widget::VBox; 617package CFClient::Widget::VBox;
602 618
603our @ISA = Crossfire::Client::Widget::Container::; 619our @ISA = CFClient::Widget::Container::;
604 620
605use SDL::OpenGL; 621use SDL::OpenGL;
606 622
607sub size_request { 623sub size_request {
608 my ($self) = @_; 624 my ($self) = @_;
656 } 672 }
657} 673}
658 674
659############################################################################# 675#############################################################################
660 676
661package Crossfire::Client::Widget::Label; 677package CFClient::Widget::Label;
662 678
663our @ISA = Crossfire::Client::Widget::; 679our @ISA = CFClient::Widget::;
664 680
665use SDL::OpenGL; 681use SDL::OpenGL;
666 682
667sub new { 683sub new {
668 my ($class, $x, $y, $z, $height, $text) = @_; 684 my ($class, %arg) = @_;
669
670 $height ||= $::FONTSIZE;
671 685
672 # TODO: color, and make height, xyz etc. optional 686 # TODO: color, and make height, xyz etc. optional
673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 687 my $self = $class->SUPER::new (
688 color => [1, 1, 1],
689 height => $::FONTSIZE,
690 text => "",
691 layout => new CFClient::Layout,
692 %arg
693 );
674 694
675 $self->set_text ($text); 695 $self->set_text ($self->{text});
676 696
677 $self 697 $self
678} 698}
679 699
680sub set_text { 700sub set_text {
681 my ($self, $text) = @_; 701 my ($self, $text) = @_;
682 702
683 $self->{text} = $text; 703 $self->{text} = $text;
684 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 704 $self->{layout}->set_markup ($text);
685 705
686 $self->update; 706 delete $self->{texture};
687} 707}
688 708
689sub get_text { 709sub get_text {
690 my ($self, $text) = @_; 710 my ($self, $text) = @_;
691 711
693} 713}
694 714
695sub size_request { 715sub size_request {
696 my ($self) = @_; 716 my ($self) = @_;
697 717
718 $self->{layout}->set_width;
719 $self->{layout}->set_height ($self->{height});
720 $self->{layout}->size
698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 721# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 ( 722# (
700 $self->{texture}{width}, 723# $self->{texture}{width},
701 $self->{texture}{height}, 724# $self->{texture}{height},
702 ) 725# )
703 } else { 726# } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 727# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
705 728#
706 ($w, $h) 729# ($w, $h)
707 } 730# }
731}
708 732
733sub size_allocate {
734 my ($self, $w, $h) = @_;
735
736 $self->SUPER::size_allocate ($w, $h);
737 delete $self->{texture};
709} 738}
710 739
711sub _draw { 740sub _draw {
712 my ($self) = @_; 741 my ($self) = @_;
713 742
714 my $tex = $self->{texture}; 743 my $tex = $self->{texture} ||= do {
744 $self->{layout}->set_width ($self->{w});
745 new_from_layout CFClient::Texture $self->{layout};
746 };
715 747
716 glEnable GL_BLEND; 748 glEnable GL_BLEND;
717 glEnable GL_TEXTURE_2D; 749 glEnable GL_TEXTURE_2D;
718 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 750 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
719 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 751 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
720 glBindTexture GL_TEXTURE_2D, $tex->{name};
721 752
722 glColor 1, 0, 0, 1; # TODO color 753 glColor @{$self->{color}};
723 754
724 glBegin GL_QUADS; 755 $tex->draw_quad (0, 0);
725 glTexCoord 0, 0; glVertex 0 , 0;
726 glTexCoord 0, 1; glVertex 0 , $tex->{height};
727 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
728 glTexCoord 1, 0; glVertex $tex->{width}, 0;
729 glEnd;
730 756
731 glDisable GL_BLEND; 757 glDisable GL_BLEND;
732 glDisable GL_TEXTURE_2D; 758 glDisable GL_TEXTURE_2D;
733} 759}
734 760
735############################################################################# 761#############################################################################
736 762
737package Crossfire::Client::Widget::Entry; 763package CFClient::Widget::Entry;
738 764
739our @ISA = Crossfire::Client::Widget::Label::; 765our @ISA = CFClient::Widget::Label::;
740 766
741use SDL; 767use SDL;
742use SDL::OpenGL; 768use SDL::OpenGL;
743 769
744sub key_down { 770sub key_down {
753 779
754 if ($sym == SDLK_BACKSPACE) { 780 if ($sym == SDLK_BACKSPACE) {
755 substr $text, -1, 1, ''; 781 substr $text, -1, 1, '';
756 } elsif ($uni) { 782 } elsif ($uni) {
757 $text .= chr $uni; 783 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
759 } 784 }
760 785
761 $self->set_text ($text); 786 $self->set_text ($text);
762} 787}
763 788
764sub button_down { 789sub button_down {
765 my ($self, $ev) = @_; 790 my ($self, $ev) = @_;
766 791
767 $self->focus_in; 792 $self->focus_in;
793}
794
795sub mouse_motion {
796 my ($self, $ev, $x, $y) = @_;
797 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
768} 798}
769 799
770sub _draw { 800sub _draw {
771 my ($self) = @_; 801 my ($self) = @_;
772 802
786 $self->SUPER::_draw; 816 $self->SUPER::_draw;
787} 817}
788 818
789############################################################################# 819#############################################################################
790 820
791package Crossfire::Client::Widget::MapWidget; 821package CFClient::Widget::MapWidget;
792 822
793use strict; 823use strict;
794 824
795use List::Util qw(min max); 825use List::Util qw(min max);
796 826
797use SDL; 827use SDL;
798use SDL::OpenGL; 828use SDL::OpenGL;
799use SDL::OpenGL::Constants; 829use SDL::OpenGL::Constants;
800 830
801our @ISA = Crossfire::Client::Widget::; 831our @ISA = CFClient::Widget::;
832
833sub new {
834 my $class = shift;
835
836 $class->SUPER::new (
837 z => -1,
838 list => (glGenLists 1),
839 @_
840 )
841}
802 842
803sub key_down { 843sub key_down {
804 print "MAPKEYDOWN\n"; 844 print "MAPKEYDOWN\n";
805} 845}
806 846
812 1 + int $::WIDTH / 32, 852 1 + int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32, 853 1 + int $::HEIGHT / 32,
814 ) 854 )
815} 855}
816 856
857sub update {
858 my ($self) = @_;
859
860 $self->{need_update} = 1;
861}
862
817sub _draw { 863sub _draw {
818 my ($self) = @_; 864 my ($self) = @_;
819 865
866 if (delete $self->{need_update}) {
867 glNewList $self->{list}, GL_COMPILE;
868
820 my $mx = $::CONN->{mapx}; 869 my $mx = $::CONN->{mapx};
821 my $my = $::CONN->{mapy}; 870 my $my = $::CONN->{mapy};
822 871
823 my $map = $::CONN->{map}; 872 my $map = $::CONN->{map};
824 873
825 my ($xofs, $yofs); 874 my ($xofs, $yofs);
826 875
827 my $sw = 1 + int $::WIDTH / 32; 876 my $sw = 1 + int $::WIDTH / 32;
828 my $sh = 1 + int $::HEIGHT / 32; 877 my $sh = 1 + int $::HEIGHT / 32;
829 878
830 if ($::CONN->{mapw} > $sw) { 879 if ($::CONN->{mapw} > $sw) {
831 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 880 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
832 } else { 881 } else {
833 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 882 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
834 } 883 }
835 884
836 if ($::CONN->{maph} > $sh) { 885 if ($::CONN->{maph} > $sh) {
837 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 886 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
838 } else { 887 } else {
839 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 888 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
840 } 889 }
841 890
842 glEnable GL_TEXTURE_2D; 891 glEnable GL_TEXTURE_2D;
843 glEnable GL_BLEND; 892 glEnable GL_BLEND;
844 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 893 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
845 894
846 my $sw4 = ($sw + 3) & ~3; 895 my $sw4 = ($sw + 3) & ~3;
847 my $lighting = "\x00" x ($sw4 * $sh); 896 my $darkness = "\x00" x ($sw4 * $sh);
848 897
849 for my $x (0 .. $sw - 1) { 898 for my $x (0 .. $sw - 1) {
899 my $row = $map->[$x + $xofs];
850 for my $y (0 .. $sh - 1) { 900 for my $y (0 .. $sh - 1) {
851 901
852 my $cell = $map->[$x + $xofs][$y + $yofs] 902 my $cell = $row->[$y + $yofs]
853 or next; 903 or next;
854 904
855 my $darkness = $cell->[0] * (1 / 255); 905 my $dark = $cell->[0];
856 if ($darkness < 0) { 906 if ($dark < 0) {
857 $darkness = $cell->[1] ? 0.1 : 0; 907 substr $darkness, $y * $sw4 + $x, 1, chr 224;
908 } else {
909 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
858 } 910 }
859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
860 911
861 for my $num (grep $_, @$cell[1,2,3]) { 912 for my $num (grep $_, @$cell[1,2,3]) {
862 my $tex = $::CONN->{face}[$num]{texture} || next; 913 my $tex = $::CONN->{face}[$num]{texture} || next;
863 914
864 glBindTexture GL_TEXTURE_2D, $tex->{name};
865
866 my $w = $tex->{width}; 915 my $w = $tex->{width};
867 my $h = $tex->{height}; 916 my $h = $tex->{height};
868 917
869 my $px = ($x + 1) * 32 - $w; 918 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
870 my $py = ($y + 1) * 32 - $h;
871
872 glBegin GL_QUADS;
873 glTexCoord 0, 0; glVertex $px , $py;
874 glTexCoord 0, 1; glVertex $px , $py + $h;
875 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
876 glTexCoord 1, 0; glVertex $px + $w, $py;
877 glEnd; 919 }
878 } 920 }
879 } 921 }
880 }
881 922
882# if (1) { # higher quality darkness 923# if (1) { # higher quality darkness
883# $lighting =~ s/(.)/$1$1$1/gs; 924# $lighting =~ s/(.)/$1$1$1/gs;
884# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 925# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
885# 926#
887# 928#
888# $lighting = $pb->get_pixels; 929# $lighting = $pb->get_pixels;
889# $lighting =~ s/(.)../$1/gs; 930# $lighting =~ s/(.)../$1/gs;
890# } 931# }
891 932
892 $lighting = new Crossfire::Client::Texture
893 width => $sw4,
894 height => $sh,
895 data => $lighting,
896 internalformat => GL_ALPHA,
897 format => GL_ALPHA;
898
899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 933 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
900 glColor 0.45, 0.45, 0.45, 1;
901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 934 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
904 glBegin GL_QUADS;
905 glTexCoord 0, 0; glVertex 0 , 0;
906 glTexCoord 0, 1; glVertex 0 , $sh * 32;
907 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
908 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
909 glEnd;
910 935
936 $darkness = new CFClient::Texture
937 width => $sw4,
938 height => $sh,
939 data => $darkness,
940 internalformat => GL_ALPHA,
941 format => GL_ALPHA;
942
943 glColor 0.45, 0.45, 0.45, 1;
944 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
945
911 glDisable GL_TEXTURE_2D; 946 glDisable GL_TEXTURE_2D;
912 glDisable GL_BLEND; 947 glDisable GL_BLEND;
948
949 glEndList;
950 }
951
952 glCallList $self->{list};
913} 953}
914 954
915my %DIR = ( 955my %DIR = (
916 SDLK_KP8, [1, "north"], 956 SDLK_KP8, [1, "north"],
917 SDLK_KP9, [2, "northeast"], 957 SDLK_KP9, [2, "northeast"],
963 } 1003 }
964} 1004}
965 1005
966############################################################################# 1006#############################################################################
967 1007
968package Crossfire::Client::Widget::Animator; 1008package CFClient::Widget::Animator;
969 1009
970use SDL::OpenGL; 1010use SDL::OpenGL;
971 1011
972our @ISA = Crossfire::Client::Widget::Bin::; 1012our @ISA = CFClient::Widget::Bin::;
973 1013
974sub moveto { 1014sub moveto {
975 my ($self, $x, $y) = @_; 1015 my ($self, $x, $y) = @_;
976 1016
977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1017 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
978 $self->{speed} = 2; 1018 $self->{speed} = 0.001;
979 $self->{time} = 1; 1019 $self->{time} = 1;
980 1020
981 ::animation_start $self; 1021 ::animation_start $self;
982} 1022}
983 1023
1005 glPopMatrix; 1045 glPopMatrix;
1006} 1046}
1007 1047
1008############################################################################# 1048#############################################################################
1009 1049
1010package Crossfire::Client::Widget::Toplevel; 1050package CFClient::Widget::Toplevel;
1011 1051
1012our @ISA = Crossfire::Client::Widget::Container::; 1052our @ISA = CFClient::Widget::Container::;
1013 1053
1014sub size_request { 1054sub size_request {
1015 ($::WIDTH, $::HEIGHT) 1055 ($::WIDTH, $::HEIGHT)
1016} 1056}
1017 1057
1022 1062
1023 $_->size_allocate ($_->size_request) 1063 $_->size_allocate ($_->size_request)
1024 for @{$self->{children}}; 1064 for @{$self->{children}};
1025} 1065}
1026 1066
1067sub translate {
1068 my ($self, $x, $y) = @_;
1069
1070 ($x, $y)
1071}
1072
1027sub update { 1073sub update {
1028 my ($self) = @_; 1074 my ($self) = @_;
1029 1075
1030 $self->size_allocate ($self->size_request); 1076 $self->size_allocate ($self->size_request);
1031 ::refresh (); 1077 ::refresh ();
1045 $self->_draw; 1091 $self->_draw;
1046} 1092}
1047 1093
1048############################################################################# 1094#############################################################################
1049 1095
1050package Crossfire::Client::Widget; 1096package CFClient::Widget;
1051 1097
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1098$TOPLEVEL = new CFClient::Widget::Toplevel;
1053 1099
10541 11001
1055 1101

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines