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.136 by root, Wed Apr 19 20:46:44 2006 UTC vs.
Revision 1.158 by root, Mon Apr 24 02:41:48 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_(.*)$/) {
134 $self->{z} = $z if defined $z; 168 $self->{z} = $z if defined $z;
135 169
136 $self->update; 170 $self->update;
137} 171}
138 172
139sub needs_redraw { 173sub set_size {
140 0 174 my ($self, $w, $h) = @_;
175
176 $self->{user_w} = $w;
177 $self->{user_h} = $h;
178
179 $self->check_size;
141} 180}
142 181
143sub size_request { 182sub size_request {
144 require Carp; 183 require Carp;
145 Carp::confess "size_request is abtract"; 184 Carp::confess "size_request is abstract";
146} 185}
147 186
148sub configure { 187sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 188 my ($self, $x, $y, $w, $h) = @_;
150 189
190 if ($self->{aspect}) {
191 my $w2 = List::Util::min $w, int $h * $self->{aspect};
192 my $h2 = List::Util::min $h, int $w / $self->{aspect};
193
194 # use alignment to adjust x, y
195
196 $x += int +($w - $w2) * 0.5;
197 $y += int +($h - $h2) * 0.5;
198
199 ($w, $h) = ($w2, $h2);
200 }
201
202 if ($self->{x} != $x || $self->{y} != $y) {
151 $self->{x} = $x; 203 $self->{x} = $x;
152 $self->{y} = $y; 204 $self->{y} = $y;
205 $self->update;
206 }
153 207
154 return unless $self->{w} != $w || $self->{h} != $h; 208 if ($self->{w} != $w || $self->{h} != $h) {
155
156 $self->{w} = $w; 209 $self->{w} = $w;
157 $self->{h} = $h; 210 $self->{h} = $h;
158 211
159 $self->size_allocate ($w, $h); 212 $self->size_allocate ($w, $h);
160 $self->update; 213 $self->update;
214 }
161} 215}
162 216
163sub size_allocate { 217sub size_allocate {
164 # nothing to be done 218 # nothing to be done
219}
220
221sub set_max_size {
222 my ($self, $w, $h) = @_;
223
224 delete $self->{max_w}; $self->{max_w} = $w if $w;
225 delete $self->{max_h}; $self->{max_h} = $h if $h;
165} 226}
166 227
167# return top left coordinates 228# return top left coordinates
168sub _topleft { 229sub _topleft {
169 my ($self, $x, $y) = @_; 230 my ($self, $x, $y) = @_;
263} 324}
264 325
265sub find_widget { 326sub find_widget {
266 my ($self, $x, $y) = @_; 327 my ($self, $x, $y) = @_;
267 328
329 return () unless $self->{can_events};
330
268 return $self 331 return $self
269 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 332 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
270 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 333 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
271 334
272 () 335 ()
279} 342}
280 343
281sub check_size { 344sub check_size {
282 my ($self) = @_; 345 my ($self) = @_;
283 346
284 my ($w, $h) = $self->size_request; 347 $self->{parent}
348 or return 1;
285 349
350 my ($w, $h) = $self->{user_w} && $self->{user_h}
351 ? @$self{qw(user_w user_h)}
352 : $self->size_request;
353
286 if ($w != $self->{req_w} || $h != $self->{req_h}) { 354 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w; 355 $self->{req_w} = $w;
288 $self->{req_h} = $h; 356 $self->{req_h} = $h;
289 357
290 $self->{parent}->check_size 358 $self->{parent}->check_size
291 if $self->{parent}; 359 or $self->size_allocate (
360 (List::Util::max $self->{w}, $w),
361 (List::Util::max $self->{h}, $h),
362 );
363
364 1
365 } else {
366 0
292 } 367 }
293} 368}
294 369
295sub update { 370sub update {
296 my ($self) = @_; 371 my ($self) = @_;
324package CFClient::UI::DrawBG; 399package CFClient::UI::DrawBG;
325 400
326our @ISA = CFClient::UI::Base::; 401our @ISA = CFClient::UI::Base::;
327 402
328use strict; 403use strict;
329use SDL::OpenGL; 404use CFClient::OpenGL;
330 405
331sub new { 406sub new {
332 my $class = shift; 407 my $class = shift;
333 408
334 # range [value, low, high, page] 409 # range [value, low, high, page]
363 438
364package CFClient::UI::Empty; 439package CFClient::UI::Empty;
365 440
366our @ISA = CFClient::UI::Base::; 441our @ISA = CFClient::UI::Base::;
367 442
443sub new {
444 my ($class, %arg) = @_;
445 $class->SUPER::new (can_events => 0, %arg);
446}
447
368sub size_request { 448sub size_request {
369 (0, 0) 449 (0, 0)
370} 450}
371 451
372sub draw { } 452sub draw { }
380sub new { 460sub new {
381 my ($class, %arg) = @_; 461 my ($class, %arg) = @_;
382 462
383 my $children = delete $arg{children} || []; 463 my $children = delete $arg{children} || [];
384 464
385 my $self = $class->SUPER::new (children => [], %arg); 465 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
386 $self->add ($_) for @$children; 466 $self->add ($_) for @$children;
387 467
388 $self 468 $self
389} 469}
390 470
409 delete $child->{parent}; 489 delete $child->{parent};
410 490
411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 491 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
412 492
413 $self->check_size; 493 $self->check_size;
494 $self->update;
414} 495}
415 496
416sub find_widget { 497sub find_widget {
417 my ($self, $x, $y) = @_; 498 my ($self, $x, $y) = @_;
418 499
482 563
483package CFClient::UI::Window; 564package CFClient::UI::Window;
484 565
485our @ISA = CFClient::UI::Bin::; 566our @ISA = CFClient::UI::Bin::;
486 567
487use SDL::OpenGL; 568use CFClient::OpenGL;
488 569
489sub new { 570sub new {
490 my ($class, %arg) = @_; 571 my ($class, %arg) = @_;
491 572
492 my $self = $class->SUPER::new (%arg); 573 my $self = $class->SUPER::new (%arg);
546sub new { die } 627sub new { die }
547 628
548sub size_request { 629sub size_request {
549 my ($self) = @_; 630 my ($self) = @_;
550 631
551 @$self{qw(child_w child_h)} = $self->child->size_request; 632 @$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)}); 633 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553 634
554 @$self{qw(child_w child_h)} 635 @$self{qw(child_w child_h)}
555} 636}
556 637
565 646
566package CFClient::UI::Frame; 647package CFClient::UI::Frame;
567 648
568our @ISA = CFClient::UI::Bin::; 649our @ISA = CFClient::UI::Bin::;
569 650
570use SDL::OpenGL; 651use 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 652
618sub new { 653sub new {
619 my $class = shift; 654 my $class = shift;
620 655
621 # TODO: user_x, user_y, overwrite moveto?
622
623 $class->SUPER::new ( 656 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 657 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 658 border_bg => [1, 1, 1, 1],
626 border => 0.8, 659 border => 0.8,
627 @_ 660 @_
628 ) 661 );
662
663 $self
664}
665
666sub _draw {
667 my ($self) = @_;
668
669 my ($w, $h) = ($self->{w}, $self->{h});
670
671 glEnable GL_BLEND;
672 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675
676# glBegin GL_QUADS;
677# glColor 0, 0, 0, 0;
678# glVertex 0 , 0;
679# glVertex 0 , $h;
680# glVertex $w, $h;
681# glVertex $w, 0;
682# glEnd;
683
684
685 $self->child->draw;
686 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D;
688}
689
690#############################################################################
691
692package CFClient::UI::FancyFrame;
693
694our @ISA = CFClient::UI::Bin::;
695
696use CFClient::OpenGL;
697
698my @tex =
699 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
700 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
701
702sub new {
703 my $class = shift;
704
705 # TODO: user_x, user_y, overwrite moveto?
706
707 my $self = $class->SUPER::new (
708 bg => [1, 1, 1, 1],
709 border_bg => [1, 1, 1, 1],
710 border => 0.8,
711 can_events => 1,
712 @_
713 );
714
715 $self->{title} &&= new CFClient::UI::Label
716 align => 0,
717 valign => 1,
718 text => $self->{title},
719 fontsize => 1;
720
721 $self
629} 722}
630 723
631sub border { 724sub border {
632 int $_[0]{border} * $::FONTSIZE 725 int $_[0]{border} * $::FONTSIZE
633} 726}
634 727
635sub size_request { 728sub size_request {
636 my ($self) = @_; 729 my ($self) = @_;
637
638 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
639 730
640 my ($w, $h) = $self->SUPER::size_request; 731 my ($w, $h) = $self->SUPER::size_request;
641 732
642 ( 733 (
643 $w + $self->border * 2, 734 $w + $self->border * 2,
649 my ($self, $w, $h) = @_; 740 my ($self, $w, $h) = @_;
650 741
651 $h -= List::Util::max 0, $self->border * 2; 742 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 743 $w -= List::Util::max 0, $self->border * 2;
653 744
745 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
746 if $self->{title};
747
654 $self->child->configure ($self->border, $self->border, $w, $h); 748 $self->child->configure ($self->border, $self->border, $w, $h);
655} 749}
656 750
657sub button_down { 751sub button_down {
658 my ($self, $ev, $x, $y) = @_; 752 my ($self, $ev, $x, $y) = @_;
660 my $border = $self->border; 754 my $border = $self->border;
661 755
662 if ($x < $self->{w} && $x >= $self->{w} - $border 756 if ($x < $self->{w} && $x >= $self->{w} - $border
663 && $y < $self->{h} && $y >= $self->{h} - $border) { 757 && $y < $self->{h} && $y >= $self->{h} - $border) {
664 758
665 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 759 my ($ox, $oy) = ($ev->{x}, $ev->{y});
666 my ($bw, $bh) = ($self->{w}, $self->{h}); 760 my ($bw, $bh) = ($self->{w}, $self->{h});
667 761
668 $self->{motion} = sub { 762 $self->{motion} = sub {
669 my ($ev, $x, $y) = @_; 763 my ($ev, $x, $y) = @_;
670 764
671 ($x, $y) = ($ev->motion_x, $ev->motion_y); 765 ($x, $y) = ($ev->{x}, $ev->{y});
672 766
673 $self->{user_w} = $bw + $x - $ox; 767 $self->{user_w} = $bw + $x - $ox;
674 $self->{user_h} = $bh + $y - $oy; 768 $self->{user_h} = $bh + $y - $oy;
675 $self->check_size; 769 $self->check_size;
676 }; 770 };
677 771
678 } elsif ($x >= 0 && $x < $self->{w} 772 } elsif ($x >= 0 && $x < $self->{w}
679 && $y >= 0 && $y < $border) { 773 && $y >= 0 && $y < $border) {
680 774
681 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 775 my ($ox, $oy) = ($ev->{x}, $ev->{y});
682 my ($bx, $by) = ($self->{x}, $self->{y}); 776 my ($bx, $by) = ($self->{x}, $self->{y});
683 777
684 $self->{motion} = sub { 778 $self->{motion} = sub {
685 my ($ev, $x, $y) = @_; 779 my ($ev, $x, $y) = @_;
686 780
687 ($x, $y) = ($ev->motion_x, $ev->motion_y); 781 ($x, $y) = ($ev->{x}, $ev->{y});
688 782
689 $self->move ($bx + $x - $ox, $by + $y - $oy); 783 $self->move ($bx + $x - $ox, $by + $y - $oy);
690 $self->update; 784 $self->update;
691 }; 785 };
692 } 786 }
737 $bg->draw_quad ($border, $border, $cw, $ch); 831 $bg->draw_quad ($border, $border, $cw, $ch);
738 832
739 glDisable GL_TEXTURE_2D; 833 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 834 glDisable GL_BLEND;
741 835
836 $self->{title}->draw if $self->{title};
742 $self->child->draw; 837 $self->child->draw;
743} 838}
744 839
745############################################################################# 840#############################################################################
746 841
748 843
749our @ISA = CFClient::UI::Base::; 844our @ISA = CFClient::UI::Base::;
750 845
751use List::Util qw(max sum); 846use List::Util qw(max sum);
752 847
753use SDL::OpenGL; 848use CFClient::OpenGL;
754 849
755sub new { 850sub new {
756 my $class = shift; 851 my $class = shift;
757 852
758 $class->SUPER::new ( 853 $class->SUPER::new (
788 or next; 883 or next;
789 884
790 for my $x (0 .. $#$row) { 885 for my $x (0 .. $#$row) {
791 my $widget = $row->[$x] 886 my $widget = $row->[$x]
792 or next; 887 or next;
793 my ($w, $h) = $widget->size_request; 888 my ($w, $h) = @$widget{qw(req_w req_h)};
794 889
795 $w[$x] = max $w[$x], $w; 890 $w[$x] = max $w[$x], $w;
796 $h[$y] = max $h[$y], $h; 891 $h[$y] = max $h[$y], $h;
797 } 892 }
798 } 893 }
903 998
904 ($h, $w) = ($w, $h); 999 ($h, $w) = ($w, $h);
905 1000
906 my $children = $self->{children}; 1001 my $children = $self->{children};
907 1002
908 my @h = map +($_->size_request)[0], @$children; 1003 my @h = map $_->{req_w}, @$children;
909 1004
910 my $req_h = List::Util::sum @h; 1005 my $req_h = List::Util::sum @h;
911 1006
912 if ($req_h > $h) { 1007 if ($req_h > $h) {
913 # ah well, not enough space 1008 # ah well, not enough space
961sub size_allocate { 1056sub size_allocate {
962 my ($self, $w, $h) = @_; 1057 my ($self, $w, $h) = @_;
963 1058
964 my $children = $self->{children}; 1059 my $children = $self->{children};
965 1060
966 my @h = map +($_->size_request)[1], @$children; 1061 my @h = map $_->{req_h}, @$children;
967 1062
968 my $req_h = List::Util::sum @h; 1063 my $req_h = List::Util::sum @h;
969 1064
970 if ($req_h > $h) { 1065 if ($req_h > $h) {
971 # ah well, not enough space 1066 # ah well, not enough space
999 1094
1000package CFClient::UI::Label; 1095package CFClient::UI::Label;
1001 1096
1002our @ISA = CFClient::UI::Base::; 1097our @ISA = CFClient::UI::Base::;
1003 1098
1004use SDL::OpenGL; 1099use CFClient::OpenGL;
1005 1100
1006sub new { 1101sub new {
1007 my ($class, %arg) = @_; 1102 my ($class, %arg) = @_;
1008 1103
1009 my $self = $class->SUPER::new ( 1104 my $self = $class->SUPER::new (
1010 fg => [1, 1, 1], 1105 fg => [1, 1, 1],
1106 #font => default_font
1011 fontsize => 1, 1107 fontsize => 1,
1012 text => "", 1108 text => "",
1013 align => -1, 1109 align => -1,
1014 valign => -1, 1110 valign => -1,
1015 padding => 2, 1111 padding => 2,
1016 layout => new CFClient::Layout, 1112 layout => new CFClient::Layout,
1113 can_events => 0,
1017 %arg 1114 %arg
1018 ); 1115 );
1019 1116
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1117 if (exists $self->{template}) {
1118 my $layout = new CFClient::Layout;
1119 $layout->set_text (delete $self->{template});
1120 $self->{template} = $layout;
1121 }
1021 1122
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1123 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1124 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1125
1025 $self 1126 $self
1039 my ($self, $text) = @_; 1140 my ($self, $text) = @_;
1040 1141
1041 $self->{layout}->set_text ($text); 1142 $self->{layout}->set_text ($text);
1042 1143
1043 delete $self->{texture}; 1144 delete $self->{texture};
1145 $self->check_size;
1044 $self->update; 1146 $self->update;
1045} 1147}
1046 1148
1047sub set_markup { 1149sub set_markup {
1048 my ($self, $markup) = @_; 1150 my ($self, $markup) = @_;
1049 1151
1050 $self->{layout}->set_markup ($markup); 1152 $self->{layout}->set_markup ($markup);
1051 1153
1052 delete $self->{texture}; 1154 delete $self->{texture};
1155 $self->check_size;
1053 $self->update; 1156 $self->update;
1054} 1157}
1055 1158
1056sub size_request { 1159sub size_request {
1057 my ($self) = @_; 1160 my ($self) = @_;
1058 1161
1059 $self->{layout}->set_width; 1162 $self->{layout}->set_font ($self->{font}) if $self->{font};
1163 $self->{layout}->set_width ($self->{max_w} || -1);
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1164 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1165
1062 my ($w, $h) = $self->{layout}->size; 1166 my ($w, $h) = $self->{layout}->size;
1167
1168 if (exists $self->{template}) {
1169 $self->{template}->set_font ($self->{font}) if $self->{font};
1170 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1171
1172 my ($w2, $h2) = $self->{template}->size;
1173
1174 $w = List::Util::max $w, $w2;
1175 $h = List::Util::max $h, $h2;
1176 }
1063 1177
1064 ( 1178 (
1065 $w + $self->{padding} * 2, 1179 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1180 $h + $self->{padding} * 2,
1067 ) 1181 )
1071 my ($self, $w, $h) = @_; 1185 my ($self, $w, $h) = @_;
1072 1186
1073 delete $self->{texture}; 1187 delete $self->{texture};
1074} 1188}
1075 1189
1190sub set_fontsize {
1191 my ($self, $fontsize) = @_;
1192
1193 $self->{fontsize} = $fontsize;
1194 delete $self->{texture};
1195 $self->check_size;
1196 $self->update;
1197}
1198
1076sub _draw { 1199sub _draw {
1077 my ($self) = @_; 1200 my ($self) = @_;
1078 1201
1079 my $tex = $self->{texture} ||= do { 1202 my $tex = $self->{texture} ||= do {
1203 $self->{layout}->set_font ($self->{font}) if $self->{font};
1080 $self->{layout}->set_width ($self->{w}); 1204 $self->{layout}->set_width ($self->{w});
1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1205 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1082 new_from_layout CFClient::Texture $self->{layout} 1206 new_from_layout CFClient::Texture $self->{layout}
1083 }; 1207 };
1084 1208
1111 1235
1112package CFClient::UI::EntryBase; 1236package CFClient::UI::EntryBase;
1113 1237
1114our @ISA = CFClient::UI::Label::; 1238our @ISA = CFClient::UI::Label::;
1115 1239
1116use SDL::OpenGL; 1240use CFClient::OpenGL;
1117 1241
1118sub new { 1242sub new {
1119 my $class = shift; 1243 my $class = shift;
1120 1244
1121 $class->SUPER::new ( 1245 $class->SUPER::new (
1124 active_bg => [1, 1, 1, 0.5], 1248 active_bg => [1, 1, 1, 0.5],
1125 active_fg => [0, 0, 0], 1249 active_fg => [0, 0, 0],
1126 can_hover => 1, 1250 can_hover => 1,
1127 can_focus => 1, 1251 can_focus => 1,
1128 valign => 0, 1252 valign => 0,
1253 can_events => 1,
1129 @_ 1254 @_
1130 ) 1255 )
1131} 1256}
1132 1257
1133sub _set_text { 1258sub _set_text {
1161} 1286}
1162 1287
1163sub size_allocate { 1288sub size_allocate {
1164 my ($self, $w, $h) = @_; 1289 my ($self, $w, $h) = @_;
1165 1290
1166 $self->_set_text ($self->{text}); 1291 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1167} 1292}
1168 1293
1169sub set_text { 1294sub set_text {
1170 my ($self, $text) = @_; 1295 my ($self, $text) = @_;
1171 1296
1175} 1300}
1176 1301
1177sub key_down { 1302sub key_down {
1178 my ($self, $ev) = @_; 1303 my ($self, $ev) = @_;
1179 1304
1180 my $mod = $ev->key_mod; 1305 my $mod = $ev->{mod};
1181 my $sym = $ev->key_sym; 1306 my $sym = $ev->{sym};
1182
1183 my $uni = $ev->key_unicode; 1307 my $uni = $ev->{unicode};
1184 1308
1185 my $text = $self->get_text; 1309 my $text = $self->get_text;
1186 1310
1187 if ($sym == 8) { 1311 if ($sym == 8) {
1188 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1312 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1279 1403
1280package CFClient::UI::Entry; 1404package CFClient::UI::Entry;
1281 1405
1282our @ISA = CFClient::UI::EntryBase::; 1406our @ISA = CFClient::UI::EntryBase::;
1283 1407
1284use SDL::OpenGL; 1408use CFClient::OpenGL;
1285 1409
1286sub key_down { 1410sub key_down {
1287 my ($self, $ev) = @_; 1411 my ($self, $ev) = @_;
1288 1412
1289 my $sym = $ev->key_sym; 1413 my $sym = $ev->{sym};
1290 1414
1291 if ($sym == 13) { 1415 if ($sym == 13) {
1292 $self->emit (activate => $self->get_text); 1416 $self->emit (activate => $self->get_text);
1293 $self->update; 1417 $self->update;
1294 1418
1302 1426
1303package CFClient::UI::Button; 1427package CFClient::UI::Button;
1304 1428
1305our @ISA = CFClient::UI::Label::; 1429our @ISA = CFClient::UI::Label::;
1306 1430
1307use SDL::OpenGL; 1431use CFClient::OpenGL;
1308 1432
1309my @tex = 1433my @tex =
1310 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1434 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1311 qw(b1_button_active.png); 1435 qw(b1_button_active.png);
1312 1436
1313sub new { 1437sub new {
1314 my $class = shift; 1438 my $class = shift;
1315 1439
1319 bg => [1, 1, 1, 0.2], 1443 bg => [1, 1, 1, 0.2],
1320 active_fg => [0, 0, 1], 1444 active_fg => [0, 0, 1],
1321 can_hover => 1, 1445 can_hover => 1,
1322 align => 0, 1446 align => 0,
1323 valign => 0, 1447 valign => 0,
1448 can_events => 1,
1324 @_ 1449 @_
1325 ) 1450 )
1326} 1451}
1327 1452
1328sub button_up { 1453sub button_up {
1362package CFClient::UI::CheckBox; 1487package CFClient::UI::CheckBox;
1363 1488
1364our @ISA = CFClient::UI::DrawBG::; 1489our @ISA = CFClient::UI::DrawBG::;
1365 1490
1366my @tex = 1491my @tex =
1367 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1492 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1368 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1493 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1369 1494
1370use SDL::OpenGL; 1495use CFClient::OpenGL;
1371 1496
1372sub new { 1497sub new {
1373 my $class = shift; 1498 my $class = shift;
1374 1499
1375 $class->SUPER::new ( 1500 $class->SUPER::new (
1421 glDisable GL_BLEND; 1546 glDisable GL_BLEND;
1422} 1547}
1423 1548
1424############################################################################# 1549#############################################################################
1425 1550
1426package CFClient::UI::VGauge; 1551package CFClient::UI::Image;
1427 1552
1428our @ISA = CFClient::UI::Base::; 1553our @ISA = CFClient::UI::Base::;
1429 1554
1430use SDL::OpenGL; 1555use CFClient::OpenGL;
1556use Carp qw/confess/;
1431 1557
1432my %tex = ( 1558our %loaded_images;
1433 food => [
1434 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1435 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1436 ],
1437 grace => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1440 ],
1441 hp => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1444 ],
1445 mana => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1448 ],
1449);
1450 1559
1451# eg. VGauge->new (gauge => 'food'), default gauge: food
1452sub new { 1560sub new {
1453 my $class = shift; 1561 my $class = shift;
1454 1562
1455 my $self = $class->SUPER::new (gauge => 'food', @_); 1563 my $self = $class->SUPER::new (can_events => 0, @_);
1564
1565 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1566
1567 $loaded_images{$self->{image}} ||=
1568 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1569
1570 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1571
1572 Scalar::Util::weaken $loaded_images{$self->{image}};
1573
1574 $self->{aspect} = $tex->{w} / $tex->{h};
1456 1575
1457 $self 1576 $self
1458} 1577}
1459 1578
1460sub size_request { 1579sub size_request {
1461 my ($self) = @_; 1580 my ($self) = @_;
1462 1581
1463 my $tex = $tex{$self->{gauge}}[0]; 1582 ($self->{tex}->{w}, $self->{tex}->{h})
1464
1465 @$tex{qw(w h)}
1466}
1467
1468sub set_max {
1469 my ($self, $max) = @_;
1470
1471 $self->{max_val} = $max;
1472}
1473
1474sub set_value {
1475 my ($self, $val, $max) = @_;
1476
1477 $self->set_max ($max)
1478 if defined $max;
1479
1480 $max = $self->{max_val};
1481 $self->{val} = $val;
1482
1483 $self->update;
1484} 1583}
1485 1584
1486sub _draw { 1585sub _draw {
1487 my ($self) = @_; 1586 my ($self) = @_;
1488 1587
1489 my $tex = $tex{$self->{gauge}}; 1588 my $tex = $self->{tex};
1490 1589
1491 my ($w, $h) = ($self->{w}, $self->{h}); 1590 my ($w, $h) = ($self->{w}, $self->{h});
1492 1591
1493 my $ycut = $self->{val} / ($self->{max_val} || 1); 1592 if ($self->{rot90}) {
1494 $ycut = 1 if $self->{val} > $self->{max_val}; 1593 glRotate 90, 0, 0, 1;
1594 glTranslate 0, -$self->{w}, 0;
1495 1595
1496 my $t1 = $tex->[0]; 1596 ($w, $h) = ($h, $w);
1497 my $t2 = $tex->[1]; 1597 }
1498 1598
1499 glEnable GL_BLEND; 1599 glEnable GL_BLEND;
1500 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1600 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1501 glEnable GL_TEXTURE_2D; 1601 glEnable GL_TEXTURE_2D;
1502 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1602 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1503 1603
1604 $tex->draw_quad (0, 0, $w, $h);
1605
1606 glDisable GL_BLEND;
1607 glDisable GL_TEXTURE_2D;
1608}
1609
1610#############################################################################
1611
1612package CFClient::UI::VGauge;
1613
1614our @ISA = CFClient::UI::Base::;
1615
1616use List::Util qw(min max);
1617
1618use CFClient::OpenGL;
1619
1620my %tex = (
1621 food => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1624 ],
1625 grace => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1628 ],
1629 hp => [
1630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1631 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1632 ],
1633 mana => [
1634 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1635 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1636 ],
1637);
1638
1639# eg. VGauge->new (gauge => 'food'), default gauge: food
1640sub new {
1641 my $class = shift;
1642
1643 my $self = $class->SUPER::new (
1644 type => 'food',
1645 @_
1646 );
1647
1648 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1649
1650 $self
1651}
1652
1653sub size_request {
1654 my ($self) = @_;
1655
1656 #my $tex = $tex{$self->{type}}[0];
1657 #@$tex{qw(w h)}
1658 (0, 0)
1659}
1660
1661sub set_max {
1662 my ($self, $max) = @_;
1663
1664 $self->{max_val} = $max;
1665}
1666
1667sub set_value {
1668 my ($self, $val, $max) = @_;
1669
1670 $self->set_max ($max)
1671 if defined $max;
1672
1673 $max = $self->{max_val};
1674 $self->{val} = $val;
1675
1676 $self->update;
1677}
1678
1679sub _draw {
1680 my ($self) = @_;
1681
1682 my $tex = $tex{$self->{type}};
1683 my ($t1, $t2, $t3) = @$tex;
1684
1685 my ($w, $h) = ($self->{w}, $self->{h});
1686
1687 if ($self->{vertical}) {
1688 glRotate 90, 0, 0, 1;
1689 glTranslate 0, -$self->{w}, 0;
1690
1691 ($w, $h) = ($h, $w);
1692 }
1693
1694 my $ycut = $self->{val} / ($self->{max_val} || 1);
1695
1696 my $ycut1 = max 0, min 1, $ycut;
1697 my $ycut2 = max 0, min 1, $ycut - 1;
1698
1504 my $h1 = $self->{h} - $ycut * $self->{h}; 1699 my $h1 = $self->{h} * (1 - $ycut1);
1505 my $h2 = $ycut * $self->{h}; 1700 my $h2 = $self->{h} * (1 - $ycut2);
1506 1701
1507 my $yp = 0; 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;
1508 1706
1509 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1707 glBindTexture GL_TEXTURE_2D, $t1->{name};
1510 glBegin GL_QUADS; 1708 glBegin GL_QUADS;
1511 glTexCoord 0 , 0; glVertex 0 , $yp; 1709 glTexCoord 0 , 0; glVertex 0 , 0;
1512 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1710 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1513 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1711 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1514 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1712 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1515 glEnd; 1713 glEnd;
1516 1714
1517 $yp += $h1; 1715 my $ycut1 = List::Util::min 1, $ycut;
1518
1519 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1716 glBindTexture GL_TEXTURE_2D, $t2->{name};
1520 glBegin GL_QUADS; 1717 glBegin GL_QUADS;
1521 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1718 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1522 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1719 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1523 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1524 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1720 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1721 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1525 glEnd; 1722 glEnd;
1723
1724 if ($t3) {
1725 glBindTexture GL_TEXTURE_2D, $t3->{name};
1726 glBegin GL_QUADS;
1727 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1728 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1729 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1730 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1731 glEnd;
1732 }
1526 1733
1527 glDisable GL_BLEND; 1734 glDisable GL_BLEND;
1528 glDisable GL_TEXTURE_2D; 1735 glDisable GL_TEXTURE_2D;
1529} 1736}
1530 1737
1531############################################################################# 1738#############################################################################
1532 1739
1740package CFClient::UI::Gauge;
1741
1742our @ISA = CFClient::UI::VBox::;
1743
1744sub new {
1745 my ($class, %arg) = @_;
1746
1747 my $self = $class->SUPER::new (
1748 tooltip => $arg{type},
1749 %arg,
1750 );
1751
1752 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1753 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1754 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1755
1756 $self
1757}
1758
1759sub set_fontsize {
1760 my ($self, $fsize) = @_;
1761
1762 $self->{value}->set_fontsize ($fsize);
1763 $self->{max} ->set_fontsize ($fsize);
1764}
1765
1766sub set_value {
1767 my ($self, $val, $max) = @_;
1768
1769 $self->set_max ($max)
1770 if defined $max;
1771
1772 $self->{gauge}->set_value ($val, $max);
1773 $self->{value}->set_text ($val);
1774}
1775
1776sub set_max {
1777 my ($self, $max) = @_;
1778
1779 $self->{gauge}->set_max ($max);
1780 $self->{max}->set_text ($max);
1781}
1782
1783#############################################################################
1784
1533package CFClient::UI::Slider; 1785package CFClient::UI::Slider;
1534 1786
1535use strict; 1787use strict;
1536 1788
1537use SDL::OpenGL; 1789use CFClient::OpenGL;
1538 1790
1539our @ISA = CFClient::UI::DrawBG::; 1791our @ISA = CFClient::UI::DrawBG::;
1540 1792
1541my @tex = 1793my @tex =
1542 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1794 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1667 1919
1668package CFClient::UI::TextView; 1920package CFClient::UI::TextView;
1669 1921
1670our @ISA = CFClient::UI::HBox::; 1922our @ISA = CFClient::UI::HBox::;
1671 1923
1672use SDL::OpenGL; 1924use CFClient::OpenGL;
1673 1925
1674sub new { 1926sub new {
1675 my $class = shift; 1927 my $class = shift;
1676 1928
1677 my $self = $class->SUPER::new ( 1929 my $self = $class->SUPER::new (
1678 fontsize => 1, 1930 fontsize => 1,
1931 #font => default_font
1679 @_, 1932 @_,
1680 1933
1681 layout => (new CFClient::Layout), 1934 layout => (new CFClient::Layout),
1682 par => [], 1935 par => [],
1683 height => 0, 1936 height => 0,
1723sub size_allocate { 1976sub size_allocate {
1724 my ($self, $w, $h) = @_; 1977 my ($self, $w, $h) = @_;
1725 1978
1726 $self->SUPER::size_allocate ($w, $h); 1979 $self->SUPER::size_allocate ($w, $h);
1727 1980
1981 $self->{layout}->set_font ($self->{font}) if $self->{font};
1728 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1982 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1729 $self->{layout}->set_width ($self->{children}[0]{w}); 1983 $self->{layout}->set_width ($self->{children}[0]{w});
1730 1984
1731 $self->reflow; 1985 $self->reflow;
1732} 1986}
1784 my $y1 = $top + $self->{h}; 2038 my $y1 = $top + $self->{h};
1785 2039
1786 my $y = 0; 2040 my $y = 0;
1787 2041
1788 my $layout = $self->{layout}; 2042 my $layout = $self->{layout};
2043
2044 $layout->set_font ($self->{font}) if $self->{font};
1789 2045
1790 for my $par (@{$self->{par}}) { 2046 for my $par (@{$self->{par}}) {
1791 my $h = $par->[0]; 2047 my $h = $par->[0];
1792 2048
1793 if ($y0 < $y + $h && $y < $y1) { 2049 if ($y0 < $y + $h && $y < $y1) {
1823 2079
1824############################################################################# 2080#############################################################################
1825 2081
1826package CFClient::UI::Animator; 2082package CFClient::UI::Animator;
1827 2083
1828use SDL::OpenGL; 2084use CFClient::OpenGL;
1829 2085
1830our @ISA = CFClient::UI::Bin::; 2086our @ISA = CFClient::UI::Bin::;
1831 2087
1832sub moveto { 2088sub moveto {
1833 my ($self, $x, $y) = @_; 2089 my ($self, $x, $y) = @_;
1873 my $class = shift; 2129 my $class = shift;
1874 2130
1875 my $self = $class->SUPER::new ( 2131 my $self = $class->SUPER::new (
1876 state => 0, 2132 state => 0,
1877 connect_activate => \&toggle_flopper, 2133 connect_activate => \&toggle_flopper,
2134 can_events => 1,
1878 @_ 2135 @_
1879 ); 2136 );
1880 2137
1881 if ($self->{state}) { 2138 if ($self->{state}) {
1882 $self->{state} = 0; 2139 $self->{state} = 0;
1902 $self->emit (changed => $self->{state}); 2159 $self->emit (changed => $self->{state});
1903} 2160}
1904 2161
1905############################################################################# 2162#############################################################################
1906 2163
2164package CFClient::UI::Tooltip;
2165
2166our @ISA = CFClient::UI::Bin::;
2167
2168use CFClient::OpenGL;
2169
2170sub new {
2171 my $class = shift;
2172
2173 $class->SUPER::new (
2174 @_,
2175 can_events => 0,
2176 )
2177}
2178
2179sub set_text {
2180 my ($self, $text) = @_;
2181
2182 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2183 $self->{label}->set_text ($text);
2184 $self->add ($self->{label});
2185}
2186
2187sub size_request {
2188 my ($self) = @_;
2189
2190 $self->child->set_max_size ($::WIDTH * 0.2);
2191
2192 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2193
2194 ($w + 4, $h + 4)
2195}
2196
2197sub _draw {
2198 my ($self) = @_;
2199
2200 glPushMatrix;
2201 glTranslate 0.375, 0.375;
2202
2203 my ($w, $h) = @$self{qw(w h)};
2204
2205 glColor 1, 0.8, 0.4;
2206 glBegin GL_QUADS;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glColor 0, 0, 0;
2214 glBegin GL_LINE_LOOP;
2215 glVertex 0 , 0;
2216 glVertex 0 , $h;
2217 glVertex $w, $h;
2218 glVertex $w, 0;
2219 glEnd;
2220
2221 glPopMatrix;
2222
2223 glTranslate 2, 2;
2224 $self->SUPER::_draw;
2225}
2226
2227#############################################################################
2228
1907package CFClient::UI::Root; 2229package CFClient::UI::Root;
1908 2230
1909our @ISA = CFClient::UI::Container::; 2231our @ISA = CFClient::UI::Container::;
1910 2232
1911use SDL::OpenGL; 2233use CFClient::OpenGL;
1912 2234
1913sub check_size { 2235sub check_size {
1914 my ($self) = @_; 2236 my ($self) = @_;
1915 2237
1916 $self->configure (0, 0, $::WITH, $::HEIGHT); 2238 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
1917} 2239}
1918 2240
1919sub size_request { 2241sub size_request {
1920 ($::WIDTH, $::HEIGHT) 2242 ($::WIDTH, $::HEIGHT)
1921} 2243}
1923sub configure { 2245sub configure {
1924 my ($self, $x, $y, $w, $h) = @_; 2246 my ($self, $x, $y, $w, $h) = @_;
1925 2247
1926 $self->SUPER::configure ($x, $y, $w, $h); 2248 $self->SUPER::configure ($x, $y, $w, $h);
1927 2249
1928 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1929 for @{$self->{children}}; 2250 for my $child (@{$self->{children}}) {
2251 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2252
2253 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2254 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2255 $child->configure ($X, $Y, $W,$H);
2256 }
1930} 2257}
1931 2258
1932sub _topleft { 2259sub _topleft {
1933 my ($self, $x, $y) = @_; 2260 my ($self, $x, $y) = @_;
1934 2261
1942 ::refresh (); 2269 ::refresh ();
1943} 2270}
1944 2271
1945sub add { 2272sub add {
1946 my ($self, $child) = @_; 2273 my ($self, $child) = @_;
2274
2275 # integerize window positions
2276 $child->{x} = int $child->{x};
2277 $child->{y} = int $child->{y};
1947 2278
1948 $self->SUPER::add ($child); 2279 $self->SUPER::add ($child);
1949} 2280}
1950 2281
1951sub on_refresh { 2282sub on_refresh {
1977############################################################################# 2308#############################################################################
1978 2309
1979package CFClient::UI; 2310package CFClient::UI;
1980 2311
1981$ROOT = new CFClient::UI::Root; 2312$ROOT = new CFClient::UI::Root;
2313$TOOLTIP = new CFClient::UI::Tooltip;
1982 2314
19831 23151
1984 2316

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines