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.134 by root, Wed Apr 19 06:06:45 2006 UTC vs.
Revision 1.152 by root, Sun Apr 23 01:05:03 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23 $TOOLTIP->set_text ($widget->{tooltip});
24 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
25 $TOOLTIP->show;
26 }
27
28 return;
29 }
30 }
31 }
32
33 $TOOLTIP->hide;
34 delete $TOOLTIP->{owner};
35}
14 36
15# class methods for events 37# class methods for events
16sub feed_sdl_key_down_event { 38sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 39 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 40}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 43 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 44}
23 45
24sub feed_sdl_button_down_event { 46sub feed_sdl_button_down_event {
25 my ($ev) = @_; 47 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 48 my ($x, $y) = ($ev->{x}, $ev->{y});
27 49
28 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 51 my $widget = $ROOT->find_widget ($x, $y);
30 52
31 $GRAB = $widget; 53 $GRAB = $widget;
32 $GRAB->update if $GRAB; 54 $GRAB->update if $GRAB;
33 }
34 55
56 check_tooltip;
57 }
58
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 59 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 60
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 61 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 62}
39 63
40sub feed_sdl_button_up_event { 64sub feed_sdl_button_up_event {
41 my ($ev) = @_; 65 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 66 my ($x, $y) = ($ev->{x}, $ev->{y});
43 67
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 68 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 69
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 70 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 71
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 72 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 73
50 if (!$BUTTON_STATE) { 74 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 75 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 76 $grab->update if $grab;
53 $GRAB->update if $GRAB; 77 $GRAB->update if $GRAB;
78
79 check_tooltip;
54 } 80 }
55} 81}
56 82
57sub feed_sdl_motion_event { 83sub feed_sdl_motion_event {
58 my ($ev) = @_; 84 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 85 my ($x, $y) = ($ev->{x}, $ev->{y});
60 86
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 87 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 88
63 if ($widget != $HOVER) { 89 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
65 91
66 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
94
95 check_tooltip;
68 } 96 }
69 97
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 98 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 99}
72 100
87 115
88package CFClient::UI::Base; 116package CFClient::UI::Base;
89 117
90use strict; 118use strict;
91 119
92use SDL::OpenGL; 120use CFClient::OpenGL;
93 121
94sub new { 122sub new {
95 my $class = shift; 123 my $class = shift;
96 124
97 my $self = bless { 125 my $self = bless {
98 x => 0, 126 x => 0,
99 y => 0, 127 y => 0,
100 z => 0, 128 z => 0,
129 can_events => 1,
101 @_ 130 @_
102 }, $class; 131 }, $class;
103 132
104 for (keys %$self) { 133 for (keys %$self) {
105 if (/^connect_(.*)$/) { 134 if (/^connect_(.*)$/) {
140 0 169 0
141} 170}
142 171
143sub size_request { 172sub size_request {
144 require Carp; 173 require Carp;
145 Carp::confess "size_request is abtract"; 174 Carp::confess "size_request is abstract";
146} 175}
147 176
148sub configure { 177sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 178 my ($self, $x, $y, $w, $h) = @_;
150 179
180 if ($self->{aspect}) {
181 my $w2 = List::Util::min $w, int $h * $self->{aspect};
182 my $h2 = List::Util::min $h, int $w / $self->{aspect};
183
184 # use alignment to adjust x, y
185
186 $x += int +($w - $w2) * 0.5;
187 $y += int +($h - $h2) * 0.5;
188
189 ($w, $h) = ($w2, $h2);
190 }
191
192 if ($self->{x} != $x || $self->{y} != $y) {
151 $self->{x} = $x; 193 $self->{x} = $x;
152 $self->{y} = $y; 194 $self->{y} = $y;
195 $self->update;
196 }
153 197
154 return unless $self->{w} != $w || $self->{h} != $h; 198 if ($self->{w} != $w || $self->{h} != $h) {
155
156 $self->{w} = $w; 199 $self->{w} = $w;
157 $self->{h} = $h; 200 $self->{h} = $h;
158 201
159 $self->size_allocate ($w, $h); 202 $self->size_allocate ($w, $h);
160 $self->update; 203 $self->update;
204 }
161} 205}
162 206
163sub size_allocate { 207sub size_allocate {
164 # nothing to be done 208 # nothing to be done
165} 209}
263} 307}
264 308
265sub find_widget { 309sub find_widget {
266 my ($self, $x, $y) = @_; 310 my ($self, $x, $y) = @_;
267 311
312 return () unless $self->{can_events};
313
268 return $self 314 return $self
269 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 315 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
270 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 316 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
271 317
272 () 318 ()
279} 325}
280 326
281sub check_size { 327sub check_size {
282 my ($self) = @_; 328 my ($self) = @_;
283 329
330 return unless $self->{parent};
331
284 my ($w, $h) = $self->size_request; 332 my ($w, $h) = $self->size_request;
285 333
286 if ($w != $self->{req_w} || $h != $self->{req_h}) { 334 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w; 335 $self->{req_w} = $w;
288 $self->{req_h} = $h; 336 $self->{req_h} = $h;
289 337
290 $self->{parent}->check_size 338 $self->{parent}->check_size;
291 if $self->{parent};
292 } 339 }
293} 340}
294 341
295sub update { 342sub update {
296 my ($self) = @_; 343 my ($self) = @_;
324package CFClient::UI::DrawBG; 371package CFClient::UI::DrawBG;
325 372
326our @ISA = CFClient::UI::Base::; 373our @ISA = CFClient::UI::Base::;
327 374
328use strict; 375use strict;
329use SDL::OpenGL; 376use CFClient::OpenGL;
330 377
331sub new { 378sub new {
332 my $class = shift; 379 my $class = shift;
333 380
334 # range [value, low, high, page] 381 # range [value, low, high, page]
363 410
364package CFClient::UI::Empty; 411package CFClient::UI::Empty;
365 412
366our @ISA = CFClient::UI::Base::; 413our @ISA = CFClient::UI::Base::;
367 414
415sub new {
416 my ($class, %arg) = @_;
417 $class->SUPER::new (can_events => 0, %arg);
418}
419
368sub size_request { 420sub size_request {
369 (0, 0) 421 (0, 0)
370} 422}
371 423
372sub draw { } 424sub draw { }
380sub new { 432sub new {
381 my ($class, %arg) = @_; 433 my ($class, %arg) = @_;
382 434
383 my $children = delete $arg{children} || []; 435 my $children = delete $arg{children} || [];
384 436
385 my $self = $class->SUPER::new (children => [], %arg); 437 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
386 $self->add ($_) for @$children; 438 $self->add ($_) for @$children;
387 439
388 $self 440 $self
389} 441}
390 442
409 delete $child->{parent}; 461 delete $child->{parent};
410 462
411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 463 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
412 464
413 $self->check_size; 465 $self->check_size;
466 $self->update;
414} 467}
415 468
416sub find_widget { 469sub find_widget {
417 my ($self, $x, $y) = @_; 470 my ($self, $x, $y) = @_;
418 471
482 535
483package CFClient::UI::Window; 536package CFClient::UI::Window;
484 537
485our @ISA = CFClient::UI::Bin::; 538our @ISA = CFClient::UI::Bin::;
486 539
487use SDL::OpenGL; 540use CFClient::OpenGL;
488 541
489sub new { 542sub new {
490 my ($class, %arg) = @_; 543 my ($class, %arg) = @_;
491 544
492 my $self = $class->SUPER::new (%arg); 545 my $self = $class->SUPER::new (%arg);
546sub new { die } 599sub new { die }
547 600
548sub size_request { 601sub size_request {
549 my ($self) = @_; 602 my ($self) = @_;
550 603
551 @$self{qw(child_w child_h)} = $self->child->size_request; 604 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
552 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 605 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553 606
554 @$self{qw(child_w child_h)} 607 @$self{qw(child_w child_h)}
555} 608}
556 609
565 618
566package CFClient::UI::Frame; 619package CFClient::UI::Frame;
567 620
568our @ISA = CFClient::UI::Bin::; 621our @ISA = CFClient::UI::Bin::;
569 622
570use SDL::OpenGL; 623use CFClient::OpenGL;
571
572sub size_request {
573 my ($self) = @_;
574 my $chld = $self->child
575 or return (0, 0);
576
577 $chld->move (2, 2);
578
579 map { $_ + 4 } $chld->size_request;
580}
581
582sub size_allocate {
583 my ($self, $x, $y, $w, $h) = @_;
584
585 $self->child->configure (2, 2, $w - 4, $h - 4);
586}
587
588sub _draw {
589 my ($self) = @_;
590
591 my $chld = $self->child;
592
593 my ($w, $h) = $chld->size_request;
594
595 glBegin GL_QUADS;
596 glColor 0, 0, 0;
597 glVertex 0 , 0;
598 glVertex 0 , $h + 4;
599 glVertex $w + 4 , $h + 4;
600 glVertex $w + 4 , 0;
601 glEnd;
602
603 $chld->draw;
604}
605
606#############################################################################
607
608package CFClient::UI::FancyFrame;
609
610our @ISA = CFClient::UI::Bin::;
611
612use SDL::OpenGL;
613
614my @tex =
615 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
616 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
617 624
618sub new { 625sub new {
619 my $class = shift; 626 my $class = shift;
620 627
621 # TODO: user_x, user_y, overwrite moveto?
622
623 $class->SUPER::new ( 628 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 629 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 630 border_bg => [1, 1, 1, 1],
626 border => 0.8, 631 border => 0.8,
627 @_ 632 @_
628 ) 633 );
634
635 $self
636}
637
638sub set_size {
639 my ($self, $w, $h) = @_;
640 $self->{req_w} = $w;
641 $self->{req_h} = $h;
642 $self->check_size;
643}
644
645sub size_request {
646 my ($self) = @_;
647 ($self->{req_w}, $self->{req_h})
648}
649
650sub size_allocate {
651 my ($self, $w, $h) = @_;
652 $self->{w} = $w;
653 $self->{h} = $h;
654 $self->child->configure (0, 0, $w, $h);
655}
656
657sub _draw {
658 my ($self) = @_;
659
660 my ($w, $h) = ($self->{w}, $self->{h});
661
662 glEnable GL_BLEND;
663 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
664 glEnable GL_TEXTURE_2D;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
666
667# glBegin GL_QUADS;
668# glColor 0, 0, 0, 0;
669# glVertex 0 , 0;
670# glVertex 0 , $h;
671# glVertex $w, $h;
672# glVertex $w, 0;
673# glEnd;
674
675
676 $self->child->draw;
677 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D;
679}
680
681#############################################################################
682
683package CFClient::UI::FancyFrame;
684
685our @ISA = CFClient::UI::Bin::;
686
687use CFClient::OpenGL;
688
689my @tex =
690 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
691 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
692
693sub new {
694 my $class = shift;
695
696 # TODO: user_x, user_y, overwrite moveto?
697
698 my $self = $class->SUPER::new (
699 bg => [1, 1, 1, 1],
700 border_bg => [1, 1, 1, 1],
701 border => 0.8,
702 can_events => 1,
703 @_
704 );
705
706 $self->{title} &&= new CFClient::UI::Label
707 align => 0,
708 valign => 1,
709 text => $self->{title},
710 fontsize => 1;
711
712 $self
629} 713}
630 714
631sub border { 715sub border {
632 int $_[0]{border} * $::FONTSIZE 716 int $_[0]{border} * $::FONTSIZE
633} 717}
649 my ($self, $w, $h) = @_; 733 my ($self, $w, $h) = @_;
650 734
651 $h -= List::Util::max 0, $self->border * 2; 735 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 736 $w -= List::Util::max 0, $self->border * 2;
653 737
738 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
739 if $self->{title};
740
654 $self->child->configure ($self->border, $self->border, $w, $h); 741 $self->child->configure ($self->border, $self->border, $w, $h);
655} 742}
656 743
657sub button_down { 744sub button_down {
658 my ($self, $ev, $x, $y) = @_; 745 my ($self, $ev, $x, $y) = @_;
660 my $border = $self->border; 747 my $border = $self->border;
661 748
662 if ($x < $self->{w} && $x >= $self->{w} - $border 749 if ($x < $self->{w} && $x >= $self->{w} - $border
663 && $y < $self->{h} && $y >= $self->{h} - $border) { 750 && $y < $self->{h} && $y >= $self->{h} - $border) {
664 751
665 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 752 my ($ox, $oy) = ($ev->{x}, $ev->{y});
666 my ($bw, $bh) = ($self->{w}, $self->{h}); 753 my ($bw, $bh) = ($self->{w}, $self->{h});
667 754
668 $self->{motion} = sub { 755 $self->{motion} = sub {
669 my ($ev, $x, $y) = @_; 756 my ($ev, $x, $y) = @_;
670 757
671 ($x, $y) = ($ev->motion_x, $ev->motion_y); 758 ($x, $y) = ($ev->{x}, $ev->{y});
672 759
673 $self->{user_w} = $bw + $x - $ox; 760 $self->{user_w} = $bw + $x - $ox;
674 $self->{user_h} = $bh + $y - $oy; 761 $self->{user_h} = $bh + $y - $oy;
675 $self->check_size; 762 $self->check_size;
676 }; 763 };
677 764
678 } elsif ($x >= 0 && $x < $self->{w} 765 } elsif ($x >= 0 && $x < $self->{w}
679 && $y >= 0 && $y < $border) { 766 && $y >= 0 && $y < $border) {
680 767
681 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 768 my ($ox, $oy) = ($ev->{x}, $ev->{y});
682 my ($bx, $by) = ($self->{x}, $self->{y}); 769 my ($bx, $by) = ($self->{x}, $self->{y});
683 770
684 $self->{motion} = sub { 771 $self->{motion} = sub {
685 my ($ev, $x, $y) = @_; 772 my ($ev, $x, $y) = @_;
686 773
687 ($x, $y) = ($ev->motion_x, $ev->motion_y); 774 ($x, $y) = ($ev->{x}, $ev->{y});
688 775
689 $self->move ($bx + $x - $ox, $by + $y - $oy); 776 $self->move ($bx + $x - $ox, $by + $y - $oy);
690 $self->update; 777 $self->update;
691 }; 778 };
692 } 779 }
737 $bg->draw_quad ($border, $border, $cw, $ch); 824 $bg->draw_quad ($border, $border, $cw, $ch);
738 825
739 glDisable GL_TEXTURE_2D; 826 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 827 glDisable GL_BLEND;
741 828
829 $self->{title}->draw if $self->{title};
742 $self->child->draw; 830 $self->child->draw;
743} 831}
744 832
745############################################################################# 833#############################################################################
746 834
748 836
749our @ISA = CFClient::UI::Base::; 837our @ISA = CFClient::UI::Base::;
750 838
751use List::Util qw(max sum); 839use List::Util qw(max sum);
752 840
753use SDL::OpenGL; 841use CFClient::OpenGL;
754 842
755sub new { 843sub new {
756 my $class = shift; 844 my $class = shift;
757 845
758 $class->SUPER::new ( 846 $class->SUPER::new (
788 or next; 876 or next;
789 877
790 for my $x (0 .. $#$row) { 878 for my $x (0 .. $#$row) {
791 my $widget = $row->[$x] 879 my $widget = $row->[$x]
792 or next; 880 or next;
793 my ($w, $h) = $widget->size_request; 881 my ($w, $h) = @$widget{qw(req_w req_h)};
794 882
795 $w[$x] = max $w[$x], $w; 883 $w[$x] = max $w[$x], $w;
796 $h[$y] = max $h[$y], $h; 884 $h[$y] = max $h[$y], $h;
797 } 885 }
798 } 886 }
903 991
904 ($h, $w) = ($w, $h); 992 ($h, $w) = ($w, $h);
905 993
906 my $children = $self->{children}; 994 my $children = $self->{children};
907 995
908 my @h = map +($_->size_request)[0], @$children; 996 my @h = map $_->{req_w}, @$children;
909 997
910 my $req_h = List::Util::sum @h; 998 my $req_h = List::Util::sum @h;
911 999
912 if ($req_h > $h) { 1000 if ($req_h > $h) {
913 # ah well, not enough space 1001 # ah well, not enough space
961sub size_allocate { 1049sub size_allocate {
962 my ($self, $w, $h) = @_; 1050 my ($self, $w, $h) = @_;
963 1051
964 my $children = $self->{children}; 1052 my $children = $self->{children};
965 1053
966 my @h = map +($_->size_request)[1], @$children; 1054 my @h = map $_->{req_h}, @$children;
967 1055
968 my $req_h = List::Util::sum @h; 1056 my $req_h = List::Util::sum @h;
969 1057
970 if ($req_h > $h) { 1058 if ($req_h > $h) {
971 # ah well, not enough space 1059 # ah well, not enough space
999 1087
1000package CFClient::UI::Label; 1088package CFClient::UI::Label;
1001 1089
1002our @ISA = CFClient::UI::Base::; 1090our @ISA = CFClient::UI::Base::;
1003 1091
1004use SDL::OpenGL; 1092use CFClient::OpenGL;
1005 1093
1006sub new { 1094sub new {
1007 my ($class, %arg) = @_; 1095 my ($class, %arg) = @_;
1008 1096
1009 my $self = $class->SUPER::new ( 1097 my $self = $class->SUPER::new (
1012 text => "", 1100 text => "",
1013 align => -1, 1101 align => -1,
1014 valign => -1, 1102 valign => -1,
1015 padding => 2, 1103 padding => 2,
1016 layout => new CFClient::Layout, 1104 layout => new CFClient::Layout,
1105 can_events => 0,
1017 %arg 1106 %arg
1018 ); 1107 );
1019 1108
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1109 if (exists $self->{template}) {
1110 my $layout = new CFClient::Layout;
1111 $layout->set_text (delete $self->{template});
1112 $self->{template} = $layout;
1113 }
1021 1114
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1115 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1116 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1117
1025 $self 1118 $self
1039 my ($self, $text) = @_; 1132 my ($self, $text) = @_;
1040 1133
1041 $self->{layout}->set_text ($text); 1134 $self->{layout}->set_text ($text);
1042 1135
1043 delete $self->{texture}; 1136 delete $self->{texture};
1137 $self->check_size;
1044 $self->update; 1138 $self->update;
1045} 1139}
1046 1140
1047sub set_markup { 1141sub set_markup {
1048 my ($self, $markup) = @_; 1142 my ($self, $markup) = @_;
1049 1143
1050 $self->{layout}->set_markup ($markup); 1144 $self->{layout}->set_markup ($markup);
1051 1145
1052 delete $self->{texture}; 1146 delete $self->{texture};
1147 $self->check_size;
1053 $self->update; 1148 $self->update;
1054} 1149}
1055 1150
1056sub size_request { 1151sub size_request {
1057 my ($self) = @_; 1152 my ($self) = @_;
1058 1153
1059 $self->{layout}->set_width; 1154 $self->{layout}->set_width;
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1155 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1156
1062 my ($w, $h) = $self->{layout}->size; 1157 my ($w, $h) = $self->{layout}->size;
1158
1159 if (exists $self->{template}) {
1160 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1161
1162 my ($w2, $h2) = $self->{template}->size;
1163
1164 $w = List::Util::max $w, $w2;
1165 $h = List::Util::max $h, $h2;
1166 }
1063 1167
1064 ( 1168 (
1065 $w + $self->{padding} * 2, 1169 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1170 $h + $self->{padding} * 2,
1067 ) 1171 )
1069 1173
1070sub size_allocate { 1174sub size_allocate {
1071 my ($self, $w, $h) = @_; 1175 my ($self, $w, $h) = @_;
1072 1176
1073 delete $self->{texture}; 1177 delete $self->{texture};
1178}
1179
1180sub set_fontsize {
1181 my ($self, $fontsize) = @_;
1182
1183 $self->{fontsize} = $fontsize;
1184 delete $self->{texture};
1185 $self->check_size;
1186 $self->update;
1074} 1187}
1075 1188
1076sub _draw { 1189sub _draw {
1077 my ($self) = @_; 1190 my ($self) = @_;
1078 1191
1111 1224
1112package CFClient::UI::EntryBase; 1225package CFClient::UI::EntryBase;
1113 1226
1114our @ISA = CFClient::UI::Label::; 1227our @ISA = CFClient::UI::Label::;
1115 1228
1116use SDL; 1229use CFClient::OpenGL;
1117use SDL::OpenGL;
1118 1230
1119sub new { 1231sub new {
1120 my $class = shift; 1232 my $class = shift;
1121 1233
1122 $class->SUPER::new ( 1234 $class->SUPER::new (
1125 active_bg => [1, 1, 1, 0.5], 1237 active_bg => [1, 1, 1, 0.5],
1126 active_fg => [0, 0, 0], 1238 active_fg => [0, 0, 0],
1127 can_hover => 1, 1239 can_hover => 1,
1128 can_focus => 1, 1240 can_focus => 1,
1129 valign => 0, 1241 valign => 0,
1242 can_events => 1,
1130 @_ 1243 @_
1131 ) 1244 )
1132} 1245}
1133 1246
1134sub _set_text { 1247sub _set_text {
1162} 1275}
1163 1276
1164sub size_allocate { 1277sub size_allocate {
1165 my ($self, $w, $h) = @_; 1278 my ($self, $w, $h) = @_;
1166 1279
1167 $self->_set_text ($self->{text}); 1280 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1168} 1281}
1169 1282
1170sub set_text { 1283sub set_text {
1171 my ($self, $text) = @_; 1284 my ($self, $text) = @_;
1172 1285
1176} 1289}
1177 1290
1178sub key_down { 1291sub key_down {
1179 my ($self, $ev) = @_; 1292 my ($self, $ev) = @_;
1180 1293
1181 my $mod = $ev->key_mod; 1294 my $mod = $ev->{mod};
1182 my $sym = $ev->key_sym; 1295 my $sym = $ev->{sym};
1183
1184 my $uni = $ev->key_unicode; 1296 my $uni = $ev->{unicode};
1185 1297
1186 my $text = $self->get_text; 1298 my $text = $self->get_text;
1187 1299
1188 if ($sym == SDLK_BACKSPACE) { 1300 if ($sym == 8) {
1189 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1301 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1190 } elsif ($sym == SDLK_DELETE) { 1302 } elsif ($sym == 127) {
1191 substr $text, $self->{cursor}, 1, ""; 1303 substr $text, $self->{cursor}, 1, "";
1192 } elsif ($sym == SDLK_LEFT) { 1304 } elsif ($sym == CFClient::SDLK_LEFT) {
1193 --$self->{cursor} if $self->{cursor}; 1305 --$self->{cursor} if $self->{cursor};
1194 } elsif ($sym == SDLK_RIGHT) { 1306 } elsif ($sym == CFClient::SDLK_RIGHT) {
1195 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1307 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1196 } elsif ($sym == SDLK_HOME) { 1308 } elsif ($sym == CFClient::SDLK_HOME) {
1197 $self->{cursor} = 0; 1309 $self->{cursor} = 0;
1198 } elsif ($sym == SDLK_END) { 1310 } elsif ($sym == CFClient::SDLK_END) {
1199 $self->{cursor} = length $text; 1311 $self->{cursor} = length $text;
1200 } elsif ($sym == SDLK_ESCAPE) { 1312 } elsif ($sym == 27) {
1201 $self->emit ('escape'); 1313 $self->emit ('escape');
1202 } elsif ($uni) { 1314 } elsif ($uni) {
1203 substr $text, $self->{cursor}++, 0, chr $uni; 1315 substr $text, $self->{cursor}++, 0, chr $uni;
1204 } 1316 }
1205 1317
1280 1392
1281package CFClient::UI::Entry; 1393package CFClient::UI::Entry;
1282 1394
1283our @ISA = CFClient::UI::EntryBase::; 1395our @ISA = CFClient::UI::EntryBase::;
1284 1396
1285use SDL; 1397use CFClient::OpenGL;
1286use SDL::OpenGL;
1287 1398
1288sub key_down { 1399sub key_down {
1289 my ($self, $ev) = @_; 1400 my ($self, $ev) = @_;
1290 1401
1291 my $sym = $ev->key_sym; 1402 my $sym = $ev->{sym};
1292 1403
1293 if ($sym == SDLK_RETURN) { 1404 if ($sym == 13) {
1294 $self->emit (activate => $self->get_text); 1405 $self->emit (activate => $self->get_text);
1295 $self->update; 1406 $self->update;
1296 1407
1297 } else { 1408 } else {
1298 $self->SUPER::key_down ($ev); 1409 $self->SUPER::key_down ($ev);
1304 1415
1305package CFClient::UI::Button; 1416package CFClient::UI::Button;
1306 1417
1307our @ISA = CFClient::UI::Label::; 1418our @ISA = CFClient::UI::Label::;
1308 1419
1309use SDL; 1420use CFClient::OpenGL;
1310use SDL::OpenGL;
1311 1421
1312my @tex = 1422my @tex =
1313 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1423 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1314 qw(b1_button_active.png); 1424 qw(b1_button_active.png);
1315 1425
1316sub new { 1426sub new {
1317 my $class = shift; 1427 my $class = shift;
1318 1428
1322 bg => [1, 1, 1, 0.2], 1432 bg => [1, 1, 1, 0.2],
1323 active_fg => [0, 0, 1], 1433 active_fg => [0, 0, 1],
1324 can_hover => 1, 1434 can_hover => 1,
1325 align => 0, 1435 align => 0,
1326 valign => 0, 1436 valign => 0,
1437 can_events => 1,
1327 @_ 1438 @_
1328 ) 1439 )
1329} 1440}
1330 1441
1331sub button_up { 1442sub button_up {
1365package CFClient::UI::CheckBox; 1476package CFClient::UI::CheckBox;
1366 1477
1367our @ISA = CFClient::UI::DrawBG::; 1478our @ISA = CFClient::UI::DrawBG::;
1368 1479
1369my @tex = 1480my @tex =
1370 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1371 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1482 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1372 1483
1373use SDL; 1484use CFClient::OpenGL;
1374use SDL::OpenGL;
1375 1485
1376sub new { 1486sub new {
1377 my $class = shift; 1487 my $class = shift;
1378 1488
1379 $class->SUPER::new ( 1489 $class->SUPER::new (
1425 glDisable GL_BLEND; 1535 glDisable GL_BLEND;
1426} 1536}
1427 1537
1428############################################################################# 1538#############################################################################
1429 1539
1430package CFClient::UI::VGauge; 1540package CFClient::UI::Image;
1431 1541
1432our @ISA = CFClient::UI::Base::; 1542our @ISA = CFClient::UI::Base::;
1433 1543
1434use SDL::OpenGL; 1544use CFClient::OpenGL;
1545use Carp qw/confess/;
1435 1546
1436my %tex = ( 1547our %loaded_images;
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454 1548
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1549sub new {
1457 my $class = shift; 1550 my $class = shift;
1458 1551
1459 my $self = $class->SUPER::new (gauge => 'food', @_); 1552 my $self = $class->SUPER::new (can_events => 0, @_);
1553
1554 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1555
1556 $loaded_images{$self->{image}} ||=
1557 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1558
1559 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1560
1561 Scalar::Util::weaken $loaded_images{$self->{image}};
1562
1563 $self->{aspect} = $tex->{w} / $tex->{h};
1460 1564
1461 $self 1565 $self
1462} 1566}
1463 1567
1464sub size_request { 1568sub size_request {
1465 my ($self) = @_; 1569 my ($self) = @_;
1466 1570
1467 my $tex = $tex{$self->{gauge}}[0]; 1571 ($self->{tex}->{w}, $self->{tex}->{h})
1468
1469 @$tex{qw(w h)}
1470}
1471
1472sub set_max {
1473 my ($self, $max) = @_;
1474
1475 $self->{max_val} = $max;
1476}
1477
1478sub set_value {
1479 my ($self, $val, $max) = @_;
1480
1481 $self->set_max ($max)
1482 if defined $max;
1483
1484 $max = $self->{max_val};
1485 $self->{val} = $val;
1486
1487 $self->update;
1488} 1572}
1489 1573
1490sub _draw { 1574sub _draw {
1491 my ($self) = @_; 1575 my ($self) = @_;
1492 1576
1493 my $tex = $tex{$self->{gauge}}; 1577 my $tex = $self->{tex};
1494 1578
1495 my ($w, $h) = ($self->{w}, $self->{h}); 1579 my ($w, $h) = ($self->{w}, $self->{h});
1496 1580
1497 my $ycut = $self->{val} / ($self->{max_val} || 1); 1581 if ($self->{rot90}) {
1498 $ycut = 1 if $self->{val} > $self->{max_val}; 1582 glRotate 90, 0, 0, 1;
1583 glTranslate 0, -$self->{w}, 0;
1499 1584
1500 my $t1 = $tex->[0]; 1585 ($w, $h) = ($h, $w);
1501 my $t2 = $tex->[1]; 1586 }
1502 1587
1503 glEnable GL_BLEND; 1588 glEnable GL_BLEND;
1504 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1589 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1505 glEnable GL_TEXTURE_2D; 1590 glEnable GL_TEXTURE_2D;
1506 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1507 1592
1593 $tex->draw_quad (0, 0, $w, $h);
1594
1595 glDisable GL_BLEND;
1596 glDisable GL_TEXTURE_2D;
1597}
1598
1599#############################################################################
1600
1601package CFClient::UI::VGauge;
1602
1603our @ISA = CFClient::UI::Base::;
1604
1605use CFClient::OpenGL;
1606
1607my %tex = (
1608 food => [
1609 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1610 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1611 ],
1612 grace => [
1613 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1614 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1615 ],
1616 hp => [
1617 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1618 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1619 ],
1620 mana => [
1621 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1622 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1623 ],
1624);
1625
1626# eg. VGauge->new (gauge => 'food'), default gauge: food
1627sub new {
1628 my $class = shift;
1629
1630 my $self = $class->SUPER::new (
1631 type => 'food',
1632 @_
1633 );
1634
1635 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1636
1637 $self
1638}
1639
1640sub size_request {
1641 my ($self) = @_;
1642
1643 #my $tex = $tex{$self->{type}}[0];
1644 #@$tex{qw(w h)}
1645 (0, 0)
1646}
1647
1648sub set_max {
1649 my ($self, $max) = @_;
1650
1651 $self->{max_val} = $max;
1652}
1653
1654sub set_value {
1655 my ($self, $val, $max) = @_;
1656
1657 $self->set_max ($max)
1658 if defined $max;
1659
1660 $max = $self->{max_val};
1661 $self->{val} = $val;
1662
1663 $self->update;
1664}
1665
1666sub _draw {
1667 my ($self) = @_;
1668
1669 my $tex = $tex{$self->{type}};
1670
1671 my ($w, $h) = ($self->{w}, $self->{h});
1672
1673 if ($self->{vertical}) {
1674 glRotate 90, 0, 0, 1;
1675 glTranslate 0, -$self->{w}, 0;
1676
1677 ($w, $h) = ($h, $w);
1678 }
1679
1680 my $ycut = $self->{val} / ($self->{max_val} || 1);
1681 $ycut = 1 if $self->{val} > $self->{max_val};
1682
1683 my $t1 = $tex->[0];
1684 my $t2 = $tex->[1];
1685
1686 glEnable GL_BLEND;
1687 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1688 glEnable GL_TEXTURE_2D;
1689 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1690
1508 my $h1 = $self->{h} - $ycut * $self->{h}; 1691 my $h1 = $self->{h} - $ycut * $self->{h};
1509 my $h2 = $ycut * $self->{h}; 1692 my $h2 = $ycut * $self->{h};
1510 1693
1511 my $yp = 0;
1512
1513 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1694 glBindTexture GL_TEXTURE_2D, $t1->{name};
1514 glBegin GL_QUADS; 1695 glBegin GL_QUADS;
1515 glTexCoord 0 , 0; glVertex 0 , $yp; 1696 glTexCoord 0 , 0; glVertex 0 , 0;
1516 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1697 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1517 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1698 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1518 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1699 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1519 glEnd; 1700 glEnd;
1520
1521 $yp += $h1;
1522 1701
1523 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1702 glBindTexture GL_TEXTURE_2D, $t2->{name};
1524 glBegin GL_QUADS; 1703 glBegin GL_QUADS;
1525 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1704 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1526 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1705 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1527 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1706 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1528 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1707 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1529 glEnd; 1708 glEnd;
1530 1709
1531 glDisable GL_BLEND; 1710 glDisable GL_BLEND;
1532 glDisable GL_TEXTURE_2D; 1711 glDisable GL_TEXTURE_2D;
1533} 1712}
1534 1713
1535############################################################################# 1714#############################################################################
1536 1715
1716package CFClient::UI::Gauge;
1717
1718our @ISA = CFClient::UI::VBox::;
1719
1720sub new {
1721 my ($class, %arg) = @_;
1722
1723 my $self = $class->SUPER::new (
1724 tooltip => $arg{type},
1725 %arg,
1726 );
1727
1728 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1729 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1730 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1731
1732 $self
1733}
1734
1735sub set_fontsize {
1736 my ($self, $fsize) = @_;
1737
1738 $self->{value}->set_fontsize ($fsize);
1739 $self->{max} ->set_fontsize ($fsize);
1740}
1741
1742sub set_value {
1743 my ($self, $val, $max) = @_;
1744
1745 $self->set_max ($max)
1746 if defined $max;
1747
1748 $self->{gauge}->set_value ($val, $max);
1749 $self->{value}->set_text ($val);
1750}
1751
1752sub set_max {
1753 my ($self, $max) = @_;
1754
1755 $self->{gauge}->set_max ($max);
1756 $self->{max}->set_text ($max);
1757}
1758
1759#############################################################################
1760
1537package CFClient::UI::Slider; 1761package CFClient::UI::Slider;
1538 1762
1539use strict; 1763use strict;
1540 1764
1541use SDL::OpenGL; 1765use CFClient::OpenGL;
1542 1766
1543our @ISA = CFClient::UI::DrawBG::; 1767our @ISA = CFClient::UI::DrawBG::;
1544 1768
1545my @tex = 1769my @tex =
1546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1770 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1671 1895
1672package CFClient::UI::TextView; 1896package CFClient::UI::TextView;
1673 1897
1674our @ISA = CFClient::UI::HBox::; 1898our @ISA = CFClient::UI::HBox::;
1675 1899
1676use SDL::OpenGL; 1900use CFClient::OpenGL;
1677 1901
1678sub new { 1902sub new {
1679 my $class = shift; 1903 my $class = shift;
1680 1904
1681 my $self = $class->SUPER::new ( 1905 my $self = $class->SUPER::new (
1825 2049
1826} 2050}
1827 2051
1828############################################################################# 2052#############################################################################
1829 2053
1830package CFClient::UI::MapWidget;
1831
1832use strict;
1833
1834use List::Util qw(min max);
1835
1836use SDL;
1837use SDL::OpenGL;
1838
1839our @ISA = CFClient::UI::Base::;
1840
1841sub new {
1842 my $class = shift;
1843
1844 $class->SUPER::new (
1845 z => -1,
1846 can_focus => 1,
1847 list => (glGenLists 1),
1848 @_
1849 )
1850}
1851
1852sub key_down {
1853 print "MAPKEYDOWN\n";
1854}
1855
1856sub key_up {
1857}
1858
1859sub button_down {
1860 my ($self, $ev, $x, $y) = @_;
1861
1862 $self->focus_in;
1863
1864 if ($ev->button == 2) {
1865 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1866 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1867
1868 $self->{motion} = sub {
1869 my ($ev, $x, $y) = @_;
1870
1871 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1872
1873 $::CFG->{map_shift_x} = $bw + $x - $ox;
1874 $::CFG->{map_shift_y} = $bh + $y - $oy;
1875
1876 $self->update;
1877 };
1878 }
1879}
1880
1881sub button_up {
1882 my ($self, $ev, $x, $y) = @_;
1883
1884 delete $self->{motion};
1885}
1886
1887sub mouse_motion {
1888 my ($self, $ev, $x, $y) = @_;
1889
1890 $self->{motion}->($ev, $x, $y) if $self->{motion};
1891}
1892
1893sub size_request {
1894 (
1895 1 + 32 * int $::WIDTH / 32,
1896 1 + 32 * int $::HEIGHT / 32,
1897 )
1898}
1899
1900sub update {
1901 my ($self) = @_;
1902
1903 $self->{need_update} = 1;
1904 $self->SUPER::update;
1905}
1906
1907sub draw {
1908 my ($self) = @_;
1909
1910 if (delete $self->{need_update}) {
1911 glNewList $self->{list}, GL_COMPILE;
1912
1913 if ($::MAP) {
1914 my $sw = int $::WIDTH / 32;
1915 my $sh = int $::HEIGHT / 32;
1916
1917 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1918 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1919
1920 glTranslate $sx0 - 32, $sy0 - 32, 0;
1921
1922 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1923
1924 if ($::CFG->{fow_enable}) {
1925 if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war
1926 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
1927 glConvolutionFilter2D (
1928 GL_CONVOLUTION_2D,
1929 GL_ALPHA,
1930 3, 3,
1931 GL_ALPHA, GL_FLOAT,
1932 pack "f*",
1933 0.1, 0.1, 0.1,
1934 0.1, 0.2, 0.1,
1935 0.1, 0.1, 0.1,
1936 );
1937 glEnable GL_CONVOLUTION_2D;
1938 }
1939
1940 $self->{fow_texture} = new CFClient::Texture
1941 w => $w,
1942 h => $h,
1943 data => $data,
1944 internalformat => GL_ALPHA,
1945 format => GL_ALPHA;
1946
1947 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1948
1949 glEnable GL_BLEND;
1950 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1951 glEnable GL_TEXTURE_2D;
1952 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1953
1954 glColor +($::CFG->{fow_intensity}) x 3, 1;
1955 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1956
1957 glDisable GL_TEXTURE_2D;
1958 glDisable GL_BLEND;
1959 }
1960
1961 # HACK BEGIN
1962 {
1963 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1964 my ($w, $h) = (250, 250);
1965
1966 glEnable GL_BLEND;
1967 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1968 glEnable GL_TEXTURE_2D;
1969 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1970
1971 $self->{mapmap_texture} =
1972 new CFClient::Texture
1973 w => $w,
1974 h => $h,
1975 data => $::MAP->mapmap ($w, $h),
1976 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
1977
1978 $self->{mapmap_texture}->draw_quad (100, 100);
1979
1980 glDisable GL_TEXTURE_2D;
1981 glDisable GL_BLEND;
1982 }
1983 # HACK END
1984 }
1985
1986 glEndList;
1987 }
1988
1989 glPushMatrix;
1990 glCallList $self->{list};
1991 glPopMatrix;
1992
1993 if ($FOCUS != $self) {
1994 glColor 64/255, 64/255, 64/255;
1995 glLogicOp GL_AND;
1996 glEnable GL_COLOR_LOGIC_OP;
1997 glBegin GL_QUADS;
1998 glVertex 0, 0;
1999 glVertex 0, $::HEIGHT;
2000 glVertex $::WIDTH, $::HEIGHT;
2001 glVertex $::WIDTH, 0;
2002 glEnd;
2003 glDisable GL_COLOR_LOGIC_OP;
2004 }
2005}
2006
2007my %DIR = (
2008 SDLK_KP8, [1, "north"],
2009 SDLK_KP9, [2, "northeast"],
2010 SDLK_KP6, [3, "east"],
2011 SDLK_KP3, [4, "southeast"],
2012 SDLK_KP2, [5, "south"],
2013 SDLK_KP1, [6, "southwest"],
2014 SDLK_KP4, [7, "west"],
2015 SDLK_KP7, [8, "northwest"],
2016
2017 SDLK_UP, [1, "north"],
2018 SDLK_RIGHT, [3, "east"],
2019 SDLK_DOWN, [5, "south"],
2020 SDLK_LEFT, [7, "west"],
2021);
2022
2023sub key_down {
2024 my ($self, $ev) = @_;
2025
2026 my $mod = $ev->key_mod;
2027 my $sym = $ev->key_sym;
2028
2029 if ($sym == SDLK_KP5) {
2030 $::CONN->user_send ("stay fire");
2031 } elsif ($sym == SDLK_a) {
2032 $::CONN->user_send ("apply");
2033 } elsif ($sym == SDLK_QUOTE) {
2034 $self->emit ('activate_console');
2035 } elsif ($sym == SDLK_SLASH) {
2036 $self->emit ('activate_console' => '/');
2037 } elsif (exists $DIR{$sym}) {
2038 if ($mod & KMOD_SHIFT) {
2039 $self->{shft}++;
2040 $::CONN->user_send ("fire $DIR{$sym}[0]");
2041 } elsif ($mod & KMOD_CTRL) {
2042 $self->{ctrl}++;
2043 $::CONN->user_send ("run $DIR{$sym}[0]");
2044 } else {
2045 $::CONN->user_send ("$DIR{$sym}[1]");
2046 }
2047 }
2048}
2049
2050sub key_up {
2051 my ($self, $ev) = @_;
2052
2053 my $mod = $ev->key_mod;
2054 my $sym = $ev->key_sym;
2055
2056 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2057 $::CONN->user_send ("fire_stop");
2058 }
2059 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2060 $::CONN->user_send ("run_stop");
2061 }
2062}
2063
2064#############################################################################
2065
2066package CFClient::UI::Animator; 2054package CFClient::UI::Animator;
2067 2055
2068use SDL::OpenGL; 2056use CFClient::OpenGL;
2069 2057
2070our @ISA = CFClient::UI::Bin::; 2058our @ISA = CFClient::UI::Bin::;
2071 2059
2072sub moveto { 2060sub moveto {
2073 my ($self, $x, $y) = @_; 2061 my ($self, $x, $y) = @_;
2113 my $class = shift; 2101 my $class = shift;
2114 2102
2115 my $self = $class->SUPER::new ( 2103 my $self = $class->SUPER::new (
2116 state => 0, 2104 state => 0,
2117 connect_activate => \&toggle_flopper, 2105 connect_activate => \&toggle_flopper,
2106 can_events => 1,
2118 @_ 2107 @_
2119 ); 2108 );
2120 2109
2121 if ($self->{state}) { 2110 if ($self->{state}) {
2122 $self->{state} = 0; 2111 $self->{state} = 0;
2146 2135
2147package CFClient::UI::Root; 2136package CFClient::UI::Root;
2148 2137
2149our @ISA = CFClient::UI::Container::; 2138our @ISA = CFClient::UI::Container::;
2150 2139
2151use SDL::OpenGL; 2140use CFClient::OpenGL;
2152 2141
2153sub check_size { 2142sub check_size {
2154 my ($self) = @_; 2143 my ($self) = @_;
2155 2144
2156 $self->configure (0, 0, $::WITH, $::HEIGHT); 2145 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2157} 2146}
2158 2147
2159sub size_request { 2148sub size_request {
2160 ($::WIDTH, $::HEIGHT) 2149 ($::WIDTH, $::HEIGHT)
2161} 2150}
2163sub configure { 2152sub configure {
2164 my ($self, $x, $y, $w, $h) = @_; 2153 my ($self, $x, $y, $w, $h) = @_;
2165 2154
2166 $self->SUPER::configure ($x, $y, $w, $h); 2155 $self->SUPER::configure ($x, $y, $w, $h);
2167 2156
2168 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2169 for @{$self->{children}}; 2157 for my $child (@{$self->{children}}) {
2158 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2159
2160 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2161 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2162 $child->configure ($X, $Y, $W,$H);
2163 }
2170} 2164}
2171 2165
2172sub _topleft { 2166sub _topleft {
2173 my ($self, $x, $y) = @_; 2167 my ($self, $x, $y) = @_;
2174 2168
2182 ::refresh (); 2176 ::refresh ();
2183} 2177}
2184 2178
2185sub add { 2179sub add {
2186 my ($self, $child) = @_; 2180 my ($self, $child) = @_;
2181
2182 # integerize window positions
2183 $child->{x} = int $child->{x};
2184 $child->{y} = int $child->{y};
2187 2185
2188 $self->SUPER::add ($child); 2186 $self->SUPER::add ($child);
2189} 2187}
2190 2188
2191sub on_refresh { 2189sub on_refresh {
2217############################################################################# 2215#############################################################################
2218 2216
2219package CFClient::UI; 2217package CFClient::UI;
2220 2218
2221$ROOT = new CFClient::UI::Root; 2219$ROOT = new CFClient::UI::Root;
2220$TOOLTIP = new CFClient::UI::Label fontsize => 0.8, can_events => 0;
2222 2221
22231 22221
2224 2223

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines