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.157 by root, Sun Apr 23 21:47:32 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
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
99
100 check_tooltip;
68 } 101 }
69 102
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 104}
72 105
87 120
88package CFClient::UI::Base; 121package CFClient::UI::Base;
89 122
90use strict; 123use strict;
91 124
92use SDL::OpenGL; 125use CFClient::OpenGL;
93 126
94sub new { 127sub new {
95 my $class = shift; 128 my $class = shift;
96 129
97 my $self = bless { 130 my $self = bless {
98 x => 0, 131 x => 0,
99 y => 0, 132 y => 0,
100 z => 0, 133 z => 0,
134 can_events => 1,
101 @_ 135 @_
102 }, $class; 136 }, $class;
103 137
104 for (keys %$self) { 138 for (keys %$self) {
105 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
140 0 174 0
141} 175}
142 176
143sub size_request { 177sub size_request {
144 require Carp; 178 require Carp;
145 Carp::confess "size_request is abtract"; 179 Carp::confess "size_request is abstract";
146} 180}
147 181
148sub configure { 182sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 183 my ($self, $x, $y, $w, $h) = @_;
150 184
185 if ($self->{aspect}) {
186 my $w2 = List::Util::min $w, int $h * $self->{aspect};
187 my $h2 = List::Util::min $h, int $w / $self->{aspect};
188
189 # use alignment to adjust x, y
190
191 $x += int +($w - $w2) * 0.5;
192 $y += int +($h - $h2) * 0.5;
193
194 ($w, $h) = ($w2, $h2);
195 }
196
197 if ($self->{x} != $x || $self->{y} != $y) {
151 $self->{x} = $x; 198 $self->{x} = $x;
152 $self->{y} = $y; 199 $self->{y} = $y;
200 $self->update;
201 }
153 202
154 return unless $self->{w} != $w || $self->{h} != $h; 203 if ($self->{w} != $w || $self->{h} != $h) {
155
156 $self->{w} = $w; 204 $self->{w} = $w;
157 $self->{h} = $h; 205 $self->{h} = $h;
158 206
159 $self->size_allocate ($w, $h); 207 $self->size_allocate ($w, $h);
160 $self->update; 208 $self->update;
209 }
161} 210}
162 211
163sub size_allocate { 212sub size_allocate {
164 # nothing to be done 213 # nothing to be done
214}
215
216sub set_max_size {
217 my ($self, $w, $h) = @_;
218
219 delete $self->{max_w}; $self->{max_w} = $w if $w;
220 delete $self->{max_h}; $self->{max_h} = $h if $h;
165} 221}
166 222
167# return top left coordinates 223# return top left coordinates
168sub _topleft { 224sub _topleft {
169 my ($self, $x, $y) = @_; 225 my ($self, $x, $y) = @_;
263} 319}
264 320
265sub find_widget { 321sub find_widget {
266 my ($self, $x, $y) = @_; 322 my ($self, $x, $y) = @_;
267 323
324 return () unless $self->{can_events};
325
268 return $self 326 return $self
269 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 327 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
270 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 328 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
271 329
272 () 330 ()
279} 337}
280 338
281sub check_size { 339sub check_size {
282 my ($self) = @_; 340 my ($self) = @_;
283 341
342 return unless $self->{parent};
343
284 my ($w, $h) = $self->size_request; 344 my ($w, $h) = $self->size_request;
285 345
286 if ($w != $self->{req_w} || $h != $self->{req_h}) { 346 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w; 347 $self->{req_w} = $w;
288 $self->{req_h} = $h; 348 $self->{req_h} = $h;
289 349
290 $self->{parent}->check_size 350 $self->{parent}->check_size;
291 if $self->{parent};
292 } 351 }
293} 352}
294 353
295sub update { 354sub update {
296 my ($self) = @_; 355 my ($self) = @_;
324package CFClient::UI::DrawBG; 383package CFClient::UI::DrawBG;
325 384
326our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
327 386
328use strict; 387use strict;
329use SDL::OpenGL; 388use CFClient::OpenGL;
330 389
331sub new { 390sub new {
332 my $class = shift; 391 my $class = shift;
333 392
334 # range [value, low, high, page] 393 # range [value, low, high, page]
363 422
364package CFClient::UI::Empty; 423package CFClient::UI::Empty;
365 424
366our @ISA = CFClient::UI::Base::; 425our @ISA = CFClient::UI::Base::;
367 426
427sub new {
428 my ($class, %arg) = @_;
429 $class->SUPER::new (can_events => 0, %arg);
430}
431
368sub size_request { 432sub size_request {
369 (0, 0) 433 (0, 0)
370} 434}
371 435
372sub draw { } 436sub draw { }
380sub new { 444sub new {
381 my ($class, %arg) = @_; 445 my ($class, %arg) = @_;
382 446
383 my $children = delete $arg{children} || []; 447 my $children = delete $arg{children} || [];
384 448
385 my $self = $class->SUPER::new (children => [], %arg); 449 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
386 $self->add ($_) for @$children; 450 $self->add ($_) for @$children;
387 451
388 $self 452 $self
389} 453}
390 454
409 delete $child->{parent}; 473 delete $child->{parent};
410 474
411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 475 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
412 476
413 $self->check_size; 477 $self->check_size;
478 $self->update;
414} 479}
415 480
416sub find_widget { 481sub find_widget {
417 my ($self, $x, $y) = @_; 482 my ($self, $x, $y) = @_;
418 483
482 547
483package CFClient::UI::Window; 548package CFClient::UI::Window;
484 549
485our @ISA = CFClient::UI::Bin::; 550our @ISA = CFClient::UI::Bin::;
486 551
487use SDL::OpenGL; 552use CFClient::OpenGL;
488 553
489sub new { 554sub new {
490 my ($class, %arg) = @_; 555 my ($class, %arg) = @_;
491 556
492 my $self = $class->SUPER::new (%arg); 557 my $self = $class->SUPER::new (%arg);
546sub new { die } 611sub new { die }
547 612
548sub size_request { 613sub size_request {
549 my ($self) = @_; 614 my ($self) = @_;
550 615
551 @$self{qw(child_w child_h)} = $self->child->size_request; 616 @$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)}); 617 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553 618
554 @$self{qw(child_w child_h)} 619 @$self{qw(child_w child_h)}
555} 620}
556 621
565 630
566package CFClient::UI::Frame; 631package CFClient::UI::Frame;
567 632
568our @ISA = CFClient::UI::Bin::; 633our @ISA = CFClient::UI::Bin::;
569 634
570use SDL::OpenGL; 635use 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 636
618sub new { 637sub new {
619 my $class = shift; 638 my $class = shift;
620 639
621 # TODO: user_x, user_y, overwrite moveto?
622
623 $class->SUPER::new ( 640 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 641 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 642 border_bg => [1, 1, 1, 1],
626 border => 0.8, 643 border => 0.8,
627 @_ 644 @_
628 ) 645 );
646
647 $self
648}
649
650sub set_size {
651 my ($self, $w, $h) = @_;
652 $self->{req_w} = $w;
653 $self->{req_h} = $h;
654 $self->check_size;
655}
656
657sub size_request {
658 my ($self) = @_;
659 ($self->{req_w}, $self->{req_h})
660}
661
662sub size_allocate {
663 my ($self, $w, $h) = @_;
664 $self->{w} = $w;
665 $self->{h} = $h;
666 $self->child->configure (0, 0, $w, $h);
667}
668
669sub _draw {
670 my ($self) = @_;
671
672 my ($w, $h) = ($self->{w}, $self->{h});
673
674 glEnable GL_BLEND;
675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
676 glEnable GL_TEXTURE_2D;
677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
678
679# glBegin GL_QUADS;
680# glColor 0, 0, 0, 0;
681# glVertex 0 , 0;
682# glVertex 0 , $h;
683# glVertex $w, $h;
684# glVertex $w, 0;
685# glEnd;
686
687
688 $self->child->draw;
689 glDisable GL_BLEND;
690 glDisable GL_TEXTURE_2D;
691}
692
693#############################################################################
694
695package CFClient::UI::FancyFrame;
696
697our @ISA = CFClient::UI::Bin::;
698
699use CFClient::OpenGL;
700
701my @tex =
702 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
703 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
704
705sub new {
706 my $class = shift;
707
708 # TODO: user_x, user_y, overwrite moveto?
709
710 my $self = $class->SUPER::new (
711 bg => [1, 1, 1, 1],
712 border_bg => [1, 1, 1, 1],
713 border => 0.8,
714 can_events => 1,
715 @_
716 );
717
718 $self->{title} &&= new CFClient::UI::Label
719 align => 0,
720 valign => 1,
721 text => $self->{title},
722 fontsize => 1;
723
724 $self
629} 725}
630 726
631sub border { 727sub border {
632 int $_[0]{border} * $::FONTSIZE 728 int $_[0]{border} * $::FONTSIZE
633} 729}
649 my ($self, $w, $h) = @_; 745 my ($self, $w, $h) = @_;
650 746
651 $h -= List::Util::max 0, $self->border * 2; 747 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 748 $w -= List::Util::max 0, $self->border * 2;
653 749
750 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
751 if $self->{title};
752
654 $self->child->configure ($self->border, $self->border, $w, $h); 753 $self->child->configure ($self->border, $self->border, $w, $h);
655} 754}
656 755
657sub button_down { 756sub button_down {
658 my ($self, $ev, $x, $y) = @_; 757 my ($self, $ev, $x, $y) = @_;
660 my $border = $self->border; 759 my $border = $self->border;
661 760
662 if ($x < $self->{w} && $x >= $self->{w} - $border 761 if ($x < $self->{w} && $x >= $self->{w} - $border
663 && $y < $self->{h} && $y >= $self->{h} - $border) { 762 && $y < $self->{h} && $y >= $self->{h} - $border) {
664 763
665 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 764 my ($ox, $oy) = ($ev->{x}, $ev->{y});
666 my ($bw, $bh) = ($self->{w}, $self->{h}); 765 my ($bw, $bh) = ($self->{w}, $self->{h});
667 766
668 $self->{motion} = sub { 767 $self->{motion} = sub {
669 my ($ev, $x, $y) = @_; 768 my ($ev, $x, $y) = @_;
670 769
671 ($x, $y) = ($ev->motion_x, $ev->motion_y); 770 ($x, $y) = ($ev->{x}, $ev->{y});
672 771
673 $self->{user_w} = $bw + $x - $ox; 772 $self->{user_w} = $bw + $x - $ox;
674 $self->{user_h} = $bh + $y - $oy; 773 $self->{user_h} = $bh + $y - $oy;
675 $self->check_size; 774 $self->check_size;
676 }; 775 };
677 776
678 } elsif ($x >= 0 && $x < $self->{w} 777 } elsif ($x >= 0 && $x < $self->{w}
679 && $y >= 0 && $y < $border) { 778 && $y >= 0 && $y < $border) {
680 779
681 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 780 my ($ox, $oy) = ($ev->{x}, $ev->{y});
682 my ($bx, $by) = ($self->{x}, $self->{y}); 781 my ($bx, $by) = ($self->{x}, $self->{y});
683 782
684 $self->{motion} = sub { 783 $self->{motion} = sub {
685 my ($ev, $x, $y) = @_; 784 my ($ev, $x, $y) = @_;
686 785
687 ($x, $y) = ($ev->motion_x, $ev->motion_y); 786 ($x, $y) = ($ev->{x}, $ev->{y});
688 787
689 $self->move ($bx + $x - $ox, $by + $y - $oy); 788 $self->move ($bx + $x - $ox, $by + $y - $oy);
690 $self->update; 789 $self->update;
691 }; 790 };
692 } 791 }
737 $bg->draw_quad ($border, $border, $cw, $ch); 836 $bg->draw_quad ($border, $border, $cw, $ch);
738 837
739 glDisable GL_TEXTURE_2D; 838 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 839 glDisable GL_BLEND;
741 840
841 $self->{title}->draw if $self->{title};
742 $self->child->draw; 842 $self->child->draw;
743} 843}
744 844
745############################################################################# 845#############################################################################
746 846
748 848
749our @ISA = CFClient::UI::Base::; 849our @ISA = CFClient::UI::Base::;
750 850
751use List::Util qw(max sum); 851use List::Util qw(max sum);
752 852
753use SDL::OpenGL; 853use CFClient::OpenGL;
754 854
755sub new { 855sub new {
756 my $class = shift; 856 my $class = shift;
757 857
758 $class->SUPER::new ( 858 $class->SUPER::new (
788 or next; 888 or next;
789 889
790 for my $x (0 .. $#$row) { 890 for my $x (0 .. $#$row) {
791 my $widget = $row->[$x] 891 my $widget = $row->[$x]
792 or next; 892 or next;
793 my ($w, $h) = $widget->size_request; 893 my ($w, $h) = @$widget{qw(req_w req_h)};
794 894
795 $w[$x] = max $w[$x], $w; 895 $w[$x] = max $w[$x], $w;
796 $h[$y] = max $h[$y], $h; 896 $h[$y] = max $h[$y], $h;
797 } 897 }
798 } 898 }
903 1003
904 ($h, $w) = ($w, $h); 1004 ($h, $w) = ($w, $h);
905 1005
906 my $children = $self->{children}; 1006 my $children = $self->{children};
907 1007
908 my @h = map +($_->size_request)[0], @$children; 1008 my @h = map $_->{req_w}, @$children;
909 1009
910 my $req_h = List::Util::sum @h; 1010 my $req_h = List::Util::sum @h;
911 1011
912 if ($req_h > $h) { 1012 if ($req_h > $h) {
913 # ah well, not enough space 1013 # ah well, not enough space
961sub size_allocate { 1061sub size_allocate {
962 my ($self, $w, $h) = @_; 1062 my ($self, $w, $h) = @_;
963 1063
964 my $children = $self->{children}; 1064 my $children = $self->{children};
965 1065
966 my @h = map +($_->size_request)[1], @$children; 1066 my @h = map $_->{req_h}, @$children;
967 1067
968 my $req_h = List::Util::sum @h; 1068 my $req_h = List::Util::sum @h;
969 1069
970 if ($req_h > $h) { 1070 if ($req_h > $h) {
971 # ah well, not enough space 1071 # ah well, not enough space
999 1099
1000package CFClient::UI::Label; 1100package CFClient::UI::Label;
1001 1101
1002our @ISA = CFClient::UI::Base::; 1102our @ISA = CFClient::UI::Base::;
1003 1103
1004use SDL::OpenGL; 1104use CFClient::OpenGL;
1005 1105
1006sub new { 1106sub new {
1007 my ($class, %arg) = @_; 1107 my ($class, %arg) = @_;
1008 1108
1009 my $self = $class->SUPER::new ( 1109 my $self = $class->SUPER::new (
1010 fg => [1, 1, 1], 1110 fg => [1, 1, 1],
1111 #font => default_font
1011 fontsize => 1, 1112 fontsize => 1,
1012 text => "", 1113 text => "",
1013 align => -1, 1114 align => -1,
1014 valign => -1, 1115 valign => -1,
1015 padding => 2, 1116 padding => 2,
1016 layout => new CFClient::Layout, 1117 layout => new CFClient::Layout,
1118 can_events => 0,
1017 %arg 1119 %arg
1018 ); 1120 );
1019 1121
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1122 if (exists $self->{template}) {
1123 my $layout = new CFClient::Layout;
1124 $layout->set_text (delete $self->{template});
1125 $self->{template} = $layout;
1126 }
1021 1127
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1128 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1129 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1130
1025 $self 1131 $self
1039 my ($self, $text) = @_; 1145 my ($self, $text) = @_;
1040 1146
1041 $self->{layout}->set_text ($text); 1147 $self->{layout}->set_text ($text);
1042 1148
1043 delete $self->{texture}; 1149 delete $self->{texture};
1150 $self->check_size;
1044 $self->update; 1151 $self->update;
1045} 1152}
1046 1153
1047sub set_markup { 1154sub set_markup {
1048 my ($self, $markup) = @_; 1155 my ($self, $markup) = @_;
1049 1156
1050 $self->{layout}->set_markup ($markup); 1157 $self->{layout}->set_markup ($markup);
1051 1158
1052 delete $self->{texture}; 1159 delete $self->{texture};
1160 $self->check_size;
1053 $self->update; 1161 $self->update;
1054} 1162}
1055 1163
1056sub size_request { 1164sub size_request {
1057 my ($self) = @_; 1165 my ($self) = @_;
1058 1166
1059 $self->{layout}->set_width; 1167 $self->{layout}->set_font ($self->{font}) if $self->{font};
1168 $self->{layout}->set_width ($self->{max_w} || -1);
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1169 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1170
1062 my ($w, $h) = $self->{layout}->size; 1171 my ($w, $h) = $self->{layout}->size;
1172
1173 if (exists $self->{template}) {
1174 $self->{template}->set_font ($self->{font}) if $self->{font};
1175 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1176
1177 my ($w2, $h2) = $self->{template}->size;
1178
1179 $w = List::Util::max $w, $w2;
1180 $h = List::Util::max $h, $h2;
1181 }
1063 1182
1064 ( 1183 (
1065 $w + $self->{padding} * 2, 1184 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1185 $h + $self->{padding} * 2,
1067 ) 1186 )
1071 my ($self, $w, $h) = @_; 1190 my ($self, $w, $h) = @_;
1072 1191
1073 delete $self->{texture}; 1192 delete $self->{texture};
1074} 1193}
1075 1194
1195sub set_fontsize {
1196 my ($self, $fontsize) = @_;
1197
1198 $self->{fontsize} = $fontsize;
1199 delete $self->{texture};
1200 $self->check_size;
1201 $self->update;
1202}
1203
1076sub _draw { 1204sub _draw {
1077 my ($self) = @_; 1205 my ($self) = @_;
1078 1206
1079 my $tex = $self->{texture} ||= do { 1207 my $tex = $self->{texture} ||= do {
1208 $self->{layout}->set_font ($self->{font}) if $self->{font};
1080 $self->{layout}->set_width ($self->{w}); 1209 $self->{layout}->set_width ($self->{w});
1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1210 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1082 new_from_layout CFClient::Texture $self->{layout} 1211 new_from_layout CFClient::Texture $self->{layout}
1083 }; 1212 };
1084 1213
1111 1240
1112package CFClient::UI::EntryBase; 1241package CFClient::UI::EntryBase;
1113 1242
1114our @ISA = CFClient::UI::Label::; 1243our @ISA = CFClient::UI::Label::;
1115 1244
1116use SDL; 1245use CFClient::OpenGL;
1117use SDL::OpenGL;
1118 1246
1119sub new { 1247sub new {
1120 my $class = shift; 1248 my $class = shift;
1121 1249
1122 $class->SUPER::new ( 1250 $class->SUPER::new (
1125 active_bg => [1, 1, 1, 0.5], 1253 active_bg => [1, 1, 1, 0.5],
1126 active_fg => [0, 0, 0], 1254 active_fg => [0, 0, 0],
1127 can_hover => 1, 1255 can_hover => 1,
1128 can_focus => 1, 1256 can_focus => 1,
1129 valign => 0, 1257 valign => 0,
1258 can_events => 1,
1130 @_ 1259 @_
1131 ) 1260 )
1132} 1261}
1133 1262
1134sub _set_text { 1263sub _set_text {
1162} 1291}
1163 1292
1164sub size_allocate { 1293sub size_allocate {
1165 my ($self, $w, $h) = @_; 1294 my ($self, $w, $h) = @_;
1166 1295
1167 $self->_set_text ($self->{text}); 1296 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1168} 1297}
1169 1298
1170sub set_text { 1299sub set_text {
1171 my ($self, $text) = @_; 1300 my ($self, $text) = @_;
1172 1301
1176} 1305}
1177 1306
1178sub key_down { 1307sub key_down {
1179 my ($self, $ev) = @_; 1308 my ($self, $ev) = @_;
1180 1309
1181 my $mod = $ev->key_mod; 1310 my $mod = $ev->{mod};
1182 my $sym = $ev->key_sym; 1311 my $sym = $ev->{sym};
1183
1184 my $uni = $ev->key_unicode; 1312 my $uni = $ev->{unicode};
1185 1313
1186 my $text = $self->get_text; 1314 my $text = $self->get_text;
1187 1315
1188 if ($sym == SDLK_BACKSPACE) { 1316 if ($sym == 8) {
1189 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1317 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1190 } elsif ($sym == SDLK_DELETE) { 1318 } elsif ($sym == 127) {
1191 substr $text, $self->{cursor}, 1, ""; 1319 substr $text, $self->{cursor}, 1, "";
1192 } elsif ($sym == SDLK_LEFT) { 1320 } elsif ($sym == CFClient::SDLK_LEFT) {
1193 --$self->{cursor} if $self->{cursor}; 1321 --$self->{cursor} if $self->{cursor};
1194 } elsif ($sym == SDLK_RIGHT) { 1322 } elsif ($sym == CFClient::SDLK_RIGHT) {
1195 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1323 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1196 } elsif ($sym == SDLK_HOME) { 1324 } elsif ($sym == CFClient::SDLK_HOME) {
1197 $self->{cursor} = 0; 1325 $self->{cursor} = 0;
1198 } elsif ($sym == SDLK_END) { 1326 } elsif ($sym == CFClient::SDLK_END) {
1199 $self->{cursor} = length $text; 1327 $self->{cursor} = length $text;
1200 } elsif ($sym == SDLK_ESCAPE) { 1328 } elsif ($sym == 27) {
1201 $self->emit ('escape'); 1329 $self->emit ('escape');
1202 } elsif ($uni) { 1330 } elsif ($uni) {
1203 substr $text, $self->{cursor}++, 0, chr $uni; 1331 substr $text, $self->{cursor}++, 0, chr $uni;
1204 } 1332 }
1205 1333
1280 1408
1281package CFClient::UI::Entry; 1409package CFClient::UI::Entry;
1282 1410
1283our @ISA = CFClient::UI::EntryBase::; 1411our @ISA = CFClient::UI::EntryBase::;
1284 1412
1285use SDL; 1413use CFClient::OpenGL;
1286use SDL::OpenGL;
1287 1414
1288sub key_down { 1415sub key_down {
1289 my ($self, $ev) = @_; 1416 my ($self, $ev) = @_;
1290 1417
1291 my $sym = $ev->key_sym; 1418 my $sym = $ev->{sym};
1292 1419
1293 if ($sym == SDLK_RETURN) { 1420 if ($sym == 13) {
1294 $self->emit (activate => $self->get_text); 1421 $self->emit (activate => $self->get_text);
1295 $self->update; 1422 $self->update;
1296 1423
1297 } else { 1424 } else {
1298 $self->SUPER::key_down ($ev); 1425 $self->SUPER::key_down ($ev);
1304 1431
1305package CFClient::UI::Button; 1432package CFClient::UI::Button;
1306 1433
1307our @ISA = CFClient::UI::Label::; 1434our @ISA = CFClient::UI::Label::;
1308 1435
1309use SDL; 1436use CFClient::OpenGL;
1310use SDL::OpenGL;
1311 1437
1312my @tex = 1438my @tex =
1313 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1439 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1314 qw(b1_button_active.png); 1440 qw(b1_button_active.png);
1315 1441
1316sub new { 1442sub new {
1317 my $class = shift; 1443 my $class = shift;
1318 1444
1322 bg => [1, 1, 1, 0.2], 1448 bg => [1, 1, 1, 0.2],
1323 active_fg => [0, 0, 1], 1449 active_fg => [0, 0, 1],
1324 can_hover => 1, 1450 can_hover => 1,
1325 align => 0, 1451 align => 0,
1326 valign => 0, 1452 valign => 0,
1453 can_events => 1,
1327 @_ 1454 @_
1328 ) 1455 )
1329} 1456}
1330 1457
1331sub button_up { 1458sub button_up {
1365package CFClient::UI::CheckBox; 1492package CFClient::UI::CheckBox;
1366 1493
1367our @ISA = CFClient::UI::DrawBG::; 1494our @ISA = CFClient::UI::DrawBG::;
1368 1495
1369my @tex = 1496my @tex =
1370 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1497 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1371 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1498 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1372 1499
1373use SDL; 1500use CFClient::OpenGL;
1374use SDL::OpenGL;
1375 1501
1376sub new { 1502sub new {
1377 my $class = shift; 1503 my $class = shift;
1378 1504
1379 $class->SUPER::new ( 1505 $class->SUPER::new (
1425 glDisable GL_BLEND; 1551 glDisable GL_BLEND;
1426} 1552}
1427 1553
1428############################################################################# 1554#############################################################################
1429 1555
1430package CFClient::UI::VGauge; 1556package CFClient::UI::Image;
1431 1557
1432our @ISA = CFClient::UI::Base::; 1558our @ISA = CFClient::UI::Base::;
1433 1559
1434use SDL::OpenGL; 1560use CFClient::OpenGL;
1561use Carp qw/confess/;
1435 1562
1436my %tex = ( 1563our %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 1564
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1565sub new {
1457 my $class = shift; 1566 my $class = shift;
1458 1567
1459 my $self = $class->SUPER::new (gauge => 'food', @_); 1568 my $self = $class->SUPER::new (can_events => 0, @_);
1569
1570 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1571
1572 $loaded_images{$self->{image}} ||=
1573 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1574
1575 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1576
1577 Scalar::Util::weaken $loaded_images{$self->{image}};
1578
1579 $self->{aspect} = $tex->{w} / $tex->{h};
1460 1580
1461 $self 1581 $self
1462} 1582}
1463 1583
1464sub size_request { 1584sub size_request {
1465 my ($self) = @_; 1585 my ($self) = @_;
1466 1586
1467 my $tex = $tex{$self->{gauge}}[0]; 1587 ($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} 1588}
1489 1589
1490sub _draw { 1590sub _draw {
1491 my ($self) = @_; 1591 my ($self) = @_;
1492 1592
1493 my $tex = $tex{$self->{gauge}}; 1593 my $tex = $self->{tex};
1494 1594
1495 my ($w, $h) = ($self->{w}, $self->{h}); 1595 my ($w, $h) = ($self->{w}, $self->{h});
1496 1596
1497 my $ycut = $self->{val} / ($self->{max_val} || 1); 1597 if ($self->{rot90}) {
1498 $ycut = 1 if $self->{val} > $self->{max_val}; 1598 glRotate 90, 0, 0, 1;
1599 glTranslate 0, -$self->{w}, 0;
1499 1600
1500 my $t1 = $tex->[0]; 1601 ($w, $h) = ($h, $w);
1501 my $t2 = $tex->[1]; 1602 }
1502 1603
1503 glEnable GL_BLEND; 1604 glEnable GL_BLEND;
1504 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1605 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1505 glEnable GL_TEXTURE_2D; 1606 glEnable GL_TEXTURE_2D;
1506 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1607 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1507 1608
1609 $tex->draw_quad (0, 0, $w, $h);
1610
1611 glDisable GL_BLEND;
1612 glDisable GL_TEXTURE_2D;
1613}
1614
1615#############################################################################
1616
1617package CFClient::UI::VGauge;
1618
1619our @ISA = CFClient::UI::Base::;
1620
1621use CFClient::OpenGL;
1622
1623my %tex = (
1624 food => [
1625 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1626 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1627 ],
1628 grace => [
1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1630 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1631 ],
1632 hp => [
1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1634 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1635 ],
1636 mana => [
1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1638 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1639 ],
1640);
1641
1642# eg. VGauge->new (gauge => 'food'), default gauge: food
1643sub new {
1644 my $class = shift;
1645
1646 my $self = $class->SUPER::new (
1647 type => 'food',
1648 @_
1649 );
1650
1651 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1652
1653 $self
1654}
1655
1656sub size_request {
1657 my ($self) = @_;
1658
1659 #my $tex = $tex{$self->{type}}[0];
1660 #@$tex{qw(w h)}
1661 (0, 0)
1662}
1663
1664sub set_max {
1665 my ($self, $max) = @_;
1666
1667 $self->{max_val} = $max;
1668}
1669
1670sub set_value {
1671 my ($self, $val, $max) = @_;
1672
1673 $self->set_max ($max)
1674 if defined $max;
1675
1676 $max = $self->{max_val};
1677 $self->{val} = $val;
1678
1679 $self->update;
1680}
1681
1682sub _draw {
1683 my ($self) = @_;
1684
1685 my $tex = $tex{$self->{type}};
1686
1687 my ($w, $h) = ($self->{w}, $self->{h});
1688
1689 if ($self->{vertical}) {
1690 glRotate 90, 0, 0, 1;
1691 glTranslate 0, -$self->{w}, 0;
1692
1693 ($w, $h) = ($h, $w);
1694 }
1695
1696 my $ycut = $self->{val} / ($self->{max_val} || 1);
1697 $ycut = 1 if $self->{val} > $self->{max_val};
1698
1699 my $t1 = $tex->[0];
1700 my $t2 = $tex->[1];
1701
1702 glEnable GL_BLEND;
1703 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1704 glEnable GL_TEXTURE_2D;
1705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1706
1508 my $h1 = $self->{h} - $ycut * $self->{h}; 1707 my $h1 = $self->{h} - $ycut * $self->{h};
1509 my $h2 = $ycut * $self->{h}; 1708 my $h2 = $ycut * $self->{h};
1510 1709
1511 my $yp = 0;
1512
1513 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1710 glBindTexture GL_TEXTURE_2D, $t1->{name};
1514 glBegin GL_QUADS; 1711 glBegin GL_QUADS;
1515 glTexCoord 0 , 0; glVertex 0 , $yp; 1712 glTexCoord 0 , 0; glVertex 0 , 0;
1516 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1713 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1517 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1714 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1518 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1715 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1519 glEnd; 1716 glEnd;
1520
1521 $yp += $h1;
1522 1717
1523 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1718 glBindTexture GL_TEXTURE_2D, $t2->{name};
1524 glBegin GL_QUADS; 1719 glBegin GL_QUADS;
1525 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1720 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1526 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1721 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1527 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1722 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1528 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1723 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1529 glEnd; 1724 glEnd;
1530 1725
1531 glDisable GL_BLEND; 1726 glDisable GL_BLEND;
1532 glDisable GL_TEXTURE_2D; 1727 glDisable GL_TEXTURE_2D;
1533} 1728}
1534 1729
1535############################################################################# 1730#############################################################################
1536 1731
1732package CFClient::UI::Gauge;
1733
1734our @ISA = CFClient::UI::VBox::;
1735
1736sub new {
1737 my ($class, %arg) = @_;
1738
1739 my $self = $class->SUPER::new (
1740 tooltip => $arg{type},
1741 %arg,
1742 );
1743
1744 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1745 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1746 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1747
1748 $self
1749}
1750
1751sub set_fontsize {
1752 my ($self, $fsize) = @_;
1753
1754 $self->{value}->set_fontsize ($fsize);
1755 $self->{max} ->set_fontsize ($fsize);
1756}
1757
1758sub set_value {
1759 my ($self, $val, $max) = @_;
1760
1761 $self->set_max ($max)
1762 if defined $max;
1763
1764 $self->{gauge}->set_value ($val, $max);
1765 $self->{value}->set_text ($val);
1766}
1767
1768sub set_max {
1769 my ($self, $max) = @_;
1770
1771 $self->{gauge}->set_max ($max);
1772 $self->{max}->set_text ($max);
1773}
1774
1775#############################################################################
1776
1537package CFClient::UI::Slider; 1777package CFClient::UI::Slider;
1538 1778
1539use strict; 1779use strict;
1540 1780
1541use SDL::OpenGL; 1781use CFClient::OpenGL;
1542 1782
1543our @ISA = CFClient::UI::DrawBG::; 1783our @ISA = CFClient::UI::DrawBG::;
1544 1784
1545my @tex = 1785my @tex =
1546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1786 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1671 1911
1672package CFClient::UI::TextView; 1912package CFClient::UI::TextView;
1673 1913
1674our @ISA = CFClient::UI::HBox::; 1914our @ISA = CFClient::UI::HBox::;
1675 1915
1676use SDL::OpenGL; 1916use CFClient::OpenGL;
1677 1917
1678sub new { 1918sub new {
1679 my $class = shift; 1919 my $class = shift;
1680 1920
1681 my $self = $class->SUPER::new ( 1921 my $self = $class->SUPER::new (
1682 fontsize => 1, 1922 fontsize => 1,
1923 #font => default_font
1683 @_, 1924 @_,
1684 1925
1685 layout => (new CFClient::Layout), 1926 layout => (new CFClient::Layout),
1686 par => [], 1927 par => [],
1687 height => 0, 1928 height => 0,
1727sub size_allocate { 1968sub size_allocate {
1728 my ($self, $w, $h) = @_; 1969 my ($self, $w, $h) = @_;
1729 1970
1730 $self->SUPER::size_allocate ($w, $h); 1971 $self->SUPER::size_allocate ($w, $h);
1731 1972
1973 $self->{layout}->set_font ($self->{font}) if $self->{font};
1732 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1974 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1733 $self->{layout}->set_width ($self->{children}[0]{w}); 1975 $self->{layout}->set_width ($self->{children}[0]{w});
1734 1976
1735 $self->reflow; 1977 $self->reflow;
1736} 1978}
1788 my $y1 = $top + $self->{h}; 2030 my $y1 = $top + $self->{h};
1789 2031
1790 my $y = 0; 2032 my $y = 0;
1791 2033
1792 my $layout = $self->{layout}; 2034 my $layout = $self->{layout};
2035
2036 $layout->set_font ($self->{font}) if $self->{font};
1793 2037
1794 for my $par (@{$self->{par}}) { 2038 for my $par (@{$self->{par}}) {
1795 my $h = $par->[0]; 2039 my $h = $par->[0];
1796 2040
1797 if ($y0 < $y + $h && $y < $y1) { 2041 if ($y0 < $y + $h && $y < $y1) {
1825 2069
1826} 2070}
1827 2071
1828############################################################################# 2072#############################################################################
1829 2073
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; 2074package CFClient::UI::Animator;
2067 2075
2068use SDL::OpenGL; 2076use CFClient::OpenGL;
2069 2077
2070our @ISA = CFClient::UI::Bin::; 2078our @ISA = CFClient::UI::Bin::;
2071 2079
2072sub moveto { 2080sub moveto {
2073 my ($self, $x, $y) = @_; 2081 my ($self, $x, $y) = @_;
2113 my $class = shift; 2121 my $class = shift;
2114 2122
2115 my $self = $class->SUPER::new ( 2123 my $self = $class->SUPER::new (
2116 state => 0, 2124 state => 0,
2117 connect_activate => \&toggle_flopper, 2125 connect_activate => \&toggle_flopper,
2126 can_events => 1,
2118 @_ 2127 @_
2119 ); 2128 );
2120 2129
2121 if ($self->{state}) { 2130 if ($self->{state}) {
2122 $self->{state} = 0; 2131 $self->{state} = 0;
2142 $self->emit (changed => $self->{state}); 2151 $self->emit (changed => $self->{state});
2143} 2152}
2144 2153
2145############################################################################# 2154#############################################################################
2146 2155
2156package CFClient::UI::Tooltip;
2157
2158our @ISA = CFClient::UI::Bin::;
2159
2160use CFClient::OpenGL;
2161
2162sub new {
2163 my $class = shift;
2164
2165 $class->SUPER::new (
2166 @_,
2167 can_events => 0,
2168 )
2169}
2170
2171sub set_text {
2172 my ($self, $text) = @_;
2173
2174 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2175 $self->{label}->set_text ($text);
2176 $self->add ($self->{label});
2177}
2178
2179sub size_request {
2180 my ($self) = @_;
2181
2182 $self->child->set_max_size ($::WIDTH * 0.2);
2183
2184 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2185
2186 ($w + 4, $h + 4)
2187}
2188
2189sub _draw {
2190 my ($self) = @_;
2191
2192 glPushMatrix;
2193 glTranslate 0.375, 0.375;
2194
2195 my ($w, $h) = @$self{qw(w h)};
2196
2197 glColor 1, 0.8, 0.4;
2198 glBegin GL_QUADS;
2199 glVertex 0 , 0;
2200 glVertex 0 , $h;
2201 glVertex $w, $h;
2202 glVertex $w, 0;
2203 glEnd;
2204
2205 glColor 0, 0, 0;
2206 glBegin GL_LINE_LOOP;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glPopMatrix;
2214
2215 glTranslate 2, 2;
2216 $self->SUPER::_draw;
2217}
2218
2219#############################################################################
2220
2147package CFClient::UI::Root; 2221package CFClient::UI::Root;
2148 2222
2149our @ISA = CFClient::UI::Container::; 2223our @ISA = CFClient::UI::Container::;
2150 2224
2151use SDL::OpenGL; 2225use CFClient::OpenGL;
2152 2226
2153sub check_size { 2227sub check_size {
2154 my ($self) = @_; 2228 my ($self) = @_;
2155 2229
2156 $self->configure (0, 0, $::WITH, $::HEIGHT); 2230 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2157} 2231}
2158 2232
2159sub size_request { 2233sub size_request {
2160 ($::WIDTH, $::HEIGHT) 2234 ($::WIDTH, $::HEIGHT)
2161} 2235}
2163sub configure { 2237sub configure {
2164 my ($self, $x, $y, $w, $h) = @_; 2238 my ($self, $x, $y, $w, $h) = @_;
2165 2239
2166 $self->SUPER::configure ($x, $y, $w, $h); 2240 $self->SUPER::configure ($x, $y, $w, $h);
2167 2241
2168 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2169 for @{$self->{children}}; 2242 for my $child (@{$self->{children}}) {
2243 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2244
2245 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2246 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2247 $child->configure ($X, $Y, $W,$H);
2248 }
2170} 2249}
2171 2250
2172sub _topleft { 2251sub _topleft {
2173 my ($self, $x, $y) = @_; 2252 my ($self, $x, $y) = @_;
2174 2253
2182 ::refresh (); 2261 ::refresh ();
2183} 2262}
2184 2263
2185sub add { 2264sub add {
2186 my ($self, $child) = @_; 2265 my ($self, $child) = @_;
2266
2267 # integerize window positions
2268 $child->{x} = int $child->{x};
2269 $child->{y} = int $child->{y};
2187 2270
2188 $self->SUPER::add ($child); 2271 $self->SUPER::add ($child);
2189} 2272}
2190 2273
2191sub on_refresh { 2274sub on_refresh {
2217############################################################################# 2300#############################################################################
2218 2301
2219package CFClient::UI; 2302package CFClient::UI;
2220 2303
2221$ROOT = new CFClient::UI::Root; 2304$ROOT = new CFClient::UI::Root;
2305$TOOLTIP = new CFClient::UI::Tooltip;
2222 2306
22231 23071
2224 2308

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines