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.137 by root, Wed Apr 19 21:38:05 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 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 (length $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_markup ($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}
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;
60
61 check_tooltip;
33 } 62 }
34 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;
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) = @_;
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_(.*)$/) {
108 } 142 }
109 143
110 $self 144 $self
111} 145}
112 146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
113sub show { 154sub show {
114 my ($self) = @_; 155 my ($self) = @_;
115 156
116 return if $self->{parent}; 157 return if $self->{parent};
117 158
134 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
135 176
136 $self->update; 177 $self->update;
137} 178}
138 179
139sub needs_redraw { 180sub set_size {
140 0 181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
141} 187}
142 188
143sub size_request { 189sub size_request {
144 require Carp; 190 require Carp;
145 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
146} 192}
147 193
148sub configure { 194sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
150 196
197 if ($self->{aspect}) {
198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
200
201 # use alignment to adjust x, y
202
203 $x += int +($w - $w2) * 0.5;
204 $y += int +($h - $h2) * 0.5;
205
206 ($w, $h) = ($w2, $h2);
207 }
208
209 if ($self->{x} != $x || $self->{y} != $y) {
151 $self->{x} = $x; 210 $self->{x} = $x;
152 $self->{y} = $y; 211 $self->{y} = $y;
212 $self->update;
213 }
153 214
154 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
155
156 $self->{w} = $w; 216 $self->{w} = $w;
157 $self->{h} = $h; 217 $self->{h} = $h;
158 218
159 $self->size_allocate ($w, $h); 219 $self->size_allocate ($w, $h);
160 $self->update; 220 $self->update;
221 }
161} 222}
162 223
163sub size_allocate { 224sub size_allocate {
164 # nothing to be done 225 # nothing to be done
226}
227
228sub set_max_size {
229 my ($self, $w, $h) = @_;
230
231 delete $self->{max_w}; $self->{max_w} = $w if $w;
232 delete $self->{max_h}; $self->{max_h} = $h if $h;
165} 233}
166 234
167# return top left coordinates 235# return top left coordinates
168sub _topleft { 236sub _topleft {
169 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
252 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
253 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
254 glEnd; 322 glEnd;
255 glDisable GL_BLEND; 323 glDisable GL_BLEND;
256 } 324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
257} 339}
258 340
259sub _draw { 341sub _draw {
260 my ($self) = @_; 342 my ($self) = @_;
261 343
262 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
263} 345}
264 346
265sub find_widget { 347sub find_widget {
266 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
267 351
268 return $self 352 return $self
269 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
270 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
271 355
279} 363}
280 364
281sub check_size { 365sub check_size {
282 my ($self) = @_; 366 my ($self) = @_;
283 367
284 my ($w, $h) = $self->size_request; 368 $self->{parent}
369 or return 1;
285 370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
286 if ($w != $self->{req_w} || $h != $self->{req_h}) { 375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w; 376 $self->{req_w} = $w;
288 $self->{req_h} = $h; 377 $self->{req_h} = $h;
289 378
290 $self->{parent}->check_size 379 $self->{parent}->check_size
291 if $self->{parent}; 380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
292 } 388 }
293} 389}
294 390
295sub update { 391sub update {
296 my ($self) = @_; 392 my ($self) = @_;
324package CFClient::UI::DrawBG; 420package CFClient::UI::DrawBG;
325 421
326our @ISA = CFClient::UI::Base::; 422our @ISA = CFClient::UI::Base::;
327 423
328use strict; 424use strict;
329use SDL::OpenGL; 425use CFClient::OpenGL;
330 426
331sub new { 427sub new {
332 my $class = shift; 428 my $class = shift;
333 429
334 # range [value, low, high, page] 430 # range [value, low, high, page]
363 459
364package CFClient::UI::Empty; 460package CFClient::UI::Empty;
365 461
366our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
367 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
368sub size_request { 469sub size_request {
369 (0, 0) 470 (0, 0)
370} 471}
371 472
372sub draw { } 473sub draw { }
380sub new { 481sub new {
381 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
382 483
383 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
384 485
385 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
386 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
387 488
388 $self 489 $self
389} 490}
390 491
409 delete $child->{parent}; 510 delete $child->{parent};
410 511
411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
412 513
413 $self->check_size; 514 $self->check_size;
515 $self->update;
516}
517
518sub clear {
519 my ($self) = @_;
520
521 delete $_->{parent}
522 for @{ delete $self->{children} };
523
524 $self->{children} = [];
414} 525}
415 526
416sub find_widget { 527sub find_widget {
417 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
418 529
482 593
483package CFClient::UI::Window; 594package CFClient::UI::Window;
484 595
485our @ISA = CFClient::UI::Bin::; 596our @ISA = CFClient::UI::Bin::;
486 597
487use SDL::OpenGL; 598use CFClient::OpenGL;
488 599
489sub new { 600sub new {
490 my ($class, %arg) = @_; 601 my ($class, %arg) = @_;
491 602
492 my $self = $class->SUPER::new (%arg); 603 my $self = $class->SUPER::new (%arg);
546sub new { die } 657sub new { die }
547 658
548sub size_request { 659sub size_request {
549 my ($self) = @_; 660 my ($self) = @_;
550 661
551 @$self{qw(child_w child_h)} = $self->child->size_request; 662 @$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)}); 663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553 664
554 @$self{qw(child_w child_h)} 665 @$self{qw(child_w child_h)}
555} 666}
556 667
565 676
566package CFClient::UI::Frame; 677package CFClient::UI::Frame;
567 678
568our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
569 680
570use SDL::OpenGL; 681use 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 682
618sub new { 683sub new {
619 my $class = shift; 684 my $class = shift;
620 685
621 # TODO: user_x, user_y, overwrite moveto?
622
623 $class->SUPER::new ( 686 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
626 border => 0.8, 689 border => 0.8,
627 @_ 690 @_
628 ) 691 );
692
693 $self
694}
695
696sub _draw {
697 my ($self) = @_;
698
699 my ($w, $h) = ($self->{w}, $self->{h});
700
701 glEnable GL_BLEND;
702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
703 glEnable GL_TEXTURE_2D;
704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
705
706# glBegin GL_QUADS;
707# glColor 0, 0, 0, 0;
708# glVertex 0 , 0;
709# glVertex 0 , $h;
710# glVertex $w, $h;
711# glVertex $w, 0;
712# glEnd;
713
714
715 $self->child->draw;
716 glDisable GL_BLEND;
717 glDisable GL_TEXTURE_2D;
718}
719
720#############################################################################
721
722package CFClient::UI::FancyFrame;
723
724our @ISA = CFClient::UI::Bin::;
725
726use CFClient::OpenGL;
727
728my @tex =
729 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
730 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
731
732sub new {
733 my $class = shift;
734
735 # TODO: user_x, user_y, overwrite moveto?
736
737 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1],
740 border => 0.8,
741 can_events => 1,
742 @_
743 );
744
745 $self->{title} &&= new CFClient::UI::Label
746 align => 0,
747 valign => 1,
748 text => $self->{title},
749 fontsize => 1;
750
751 $self
629} 752}
630 753
631sub border { 754sub border {
632 int $_[0]{border} * $::FONTSIZE 755 int $_[0]{border} * $::FONTSIZE
633} 756}
634 757
635sub size_request { 758sub size_request {
636 my ($self) = @_; 759 my ($self) = @_;
637
638 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
639 760
640 my ($w, $h) = $self->SUPER::size_request; 761 my ($w, $h) = $self->SUPER::size_request;
641 762
642 ( 763 (
643 $w + $self->border * 2, 764 $w + $self->border * 2,
649 my ($self, $w, $h) = @_; 770 my ($self, $w, $h) = @_;
650 771
651 $h -= List::Util::max 0, $self->border * 2; 772 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 773 $w -= List::Util::max 0, $self->border * 2;
653 774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
654 $self->child->configure ($self->border, $self->border, $w, $h); 778 $self->child->configure ($self->border, $self->border, $w, $h);
655} 779}
656 780
657sub button_down { 781sub button_down {
658 my ($self, $ev, $x, $y) = @_; 782 my ($self, $ev, $x, $y) = @_;
660 my $border = $self->border; 784 my $border = $self->border;
661 785
662 if ($x < $self->{w} && $x >= $self->{w} - $border 786 if ($x < $self->{w} && $x >= $self->{w} - $border
663 && $y < $self->{h} && $y >= $self->{h} - $border) { 787 && $y < $self->{h} && $y >= $self->{h} - $border) {
664 788
665 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
666 my ($bw, $bh) = ($self->{w}, $self->{h}); 790 my ($bw, $bh) = ($self->{w}, $self->{h});
667 791
668 $self->{motion} = sub { 792 $self->{motion} = sub {
669 my ($ev, $x, $y) = @_; 793 my ($ev, $x, $y) = @_;
670 794
671 ($x, $y) = ($ev->motion_x, $ev->motion_y); 795 ($x, $y) = ($ev->{x}, $ev->{y});
672 796
673 $self->{user_w} = $bw + $x - $ox; 797 $self->{user_w} = $bw + $x - $ox;
674 $self->{user_h} = $bh + $y - $oy; 798 $self->{user_h} = $bh + $y - $oy;
675 $self->check_size; 799 $self->check_size;
676 }; 800 };
677 801
678 } elsif ($x >= 0 && $x < $self->{w} 802 } elsif ($x >= 0 && $x < $self->{w}
679 && $y >= 0 && $y < $border) { 803 && $y >= 0 && $y < $border) {
680 804
681 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
682 my ($bx, $by) = ($self->{x}, $self->{y}); 806 my ($bx, $by) = ($self->{x}, $self->{y});
683 807
684 $self->{motion} = sub { 808 $self->{motion} = sub {
685 my ($ev, $x, $y) = @_; 809 my ($ev, $x, $y) = @_;
686 810
687 ($x, $y) = ($ev->motion_x, $ev->motion_y); 811 ($x, $y) = ($ev->{x}, $ev->{y});
688 812
689 $self->move ($bx + $x - $ox, $by + $y - $oy); 813 $self->move ($bx + $x - $ox, $by + $y - $oy);
690 $self->update; 814 $self->update;
691 }; 815 };
692 } 816 }
737 $bg->draw_quad ($border, $border, $cw, $ch); 861 $bg->draw_quad ($border, $border, $cw, $ch);
738 862
739 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 864 glDisable GL_BLEND;
741 865
866 $self->{title}->draw if $self->{title};
742 $self->child->draw; 867 $self->child->draw;
743} 868}
744 869
745############################################################################# 870#############################################################################
746 871
748 873
749our @ISA = CFClient::UI::Base::; 874our @ISA = CFClient::UI::Base::;
750 875
751use List::Util qw(max sum); 876use List::Util qw(max sum);
752 877
753use SDL::OpenGL; 878use CFClient::OpenGL;
754 879
755sub new { 880sub new {
756 my $class = shift; 881 my $class = shift;
757 882
758 $class->SUPER::new ( 883 $class->SUPER::new (
788 or next; 913 or next;
789 914
790 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
791 my $widget = $row->[$x] 916 my $widget = $row->[$x]
792 or next; 917 or next;
793 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
794 919
795 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
796 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
797 } 922 }
798 } 923 }
903 1028
904 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
905 1030
906 my $children = $self->{children}; 1031 my $children = $self->{children};
907 1032
908 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
909 1034
910 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
911 1036
912 if ($req_h > $h) { 1037 if ($req_h > $h) {
913 # ah well, not enough space 1038 # ah well, not enough space
961sub size_allocate { 1086sub size_allocate {
962 my ($self, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
963 1088
964 my $children = $self->{children}; 1089 my $children = $self->{children};
965 1090
966 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
967 1092
968 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
969 1094
970 if ($req_h > $h) { 1095 if ($req_h > $h) {
971 # ah well, not enough space 1096 # ah well, not enough space
999 1124
1000package CFClient::UI::Label; 1125package CFClient::UI::Label;
1001 1126
1002our @ISA = CFClient::UI::Base::; 1127our @ISA = CFClient::UI::Base::;
1003 1128
1004use SDL::OpenGL; 1129use CFClient::OpenGL;
1005 1130
1006sub new { 1131sub new {
1007 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
1008 1133
1009 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
1010 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
1011 fontsize => 1, 1137 fontsize => 1,
1012 text => "", 1138 text => "",
1013 align => -1, 1139 align => -1,
1014 valign => -1, 1140 valign => -1,
1015 padding => 2, 1141 padding => 2,
1016 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
1017 %arg 1144 %arg
1018 ); 1145 );
1019 1146
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1147 if (exists $self->{template}) {
1148 my $layout = new CFClient::Layout;
1149 $layout->set_text (delete $self->{template});
1150 $self->{template} = $layout;
1151 }
1021 1152
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1153 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1155
1025 $self 1156 $self
1026} 1157}
1027 1158
1028sub escape_text { 1159sub escape {
1029 local $_ = $_[1]; 1160 local $_ = $_[1];
1030 1161
1031 s/&/&amp;/g; 1162 s/&/&amp;/g;
1032 s/>/&gt;/g; 1163 s/>/&gt;/g;
1033 s/</&lt;/g; 1164 s/</&lt;/g;
1039 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
1040 1171
1041 $self->{layout}->set_text ($text); 1172 $self->{layout}->set_text ($text);
1042 1173
1043 delete $self->{texture}; 1174 delete $self->{texture};
1175 $self->check_size;
1044 $self->update; 1176 $self->update;
1045} 1177}
1046 1178
1047sub set_markup { 1179sub set_markup {
1048 my ($self, $markup) = @_; 1180 my ($self, $markup) = @_;
1049 1181
1050 $self->{layout}->set_markup ($markup); 1182 $self->{layout}->set_markup ($markup);
1051 1183
1052 delete $self->{texture}; 1184 delete $self->{texture};
1185 $self->check_size;
1053 $self->update; 1186 $self->update;
1054} 1187}
1055 1188
1056sub size_request { 1189sub size_request {
1057 my ($self) = @_; 1190 my ($self) = @_;
1058 1191
1059 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1195
1062 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1197
1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1201
1202 my ($w2, $h2) = $self->{template}->size;
1203
1204 $w = List::Util::max $w, $w2;
1205 $h = List::Util::max $h, $h2;
1206 }
1063 1207
1064 ( 1208 (
1065 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
1067 ) 1211 )
1071 my ($self, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
1072 1216
1073 delete $self->{texture}; 1217 delete $self->{texture};
1074} 1218}
1075 1219
1220sub set_fontsize {
1221 my ($self, $fontsize) = @_;
1222
1223 $self->{fontsize} = $fontsize;
1224 delete $self->{texture};
1225 $self->check_size;
1226 $self->update;
1227}
1228
1076sub _draw { 1229sub _draw {
1077 my ($self) = @_; 1230 my ($self) = @_;
1078 1231
1079 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1080 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1082 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1083 }; 1237 };
1084 1238
1111 1265
1112package CFClient::UI::EntryBase; 1266package CFClient::UI::EntryBase;
1113 1267
1114our @ISA = CFClient::UI::Label::; 1268our @ISA = CFClient::UI::Label::;
1115 1269
1116use SDL::OpenGL; 1270use CFClient::OpenGL;
1117 1271
1118sub new { 1272sub new {
1119 my $class = shift; 1273 my $class = shift;
1120 1274
1121 $class->SUPER::new ( 1275 $class->SUPER::new (
1124 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1125 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1126 can_hover => 1, 1280 can_hover => 1,
1127 can_focus => 1, 1281 can_focus => 1,
1128 valign => 0, 1282 valign => 0,
1283 can_events => 1,
1129 @_ 1284 @_
1130 ) 1285 )
1131} 1286}
1132 1287
1133sub _set_text { 1288sub _set_text {
1161} 1316}
1162 1317
1163sub size_allocate { 1318sub size_allocate {
1164 my ($self, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1165 1320
1166 $self->_set_text ($self->{text}); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1167} 1322}
1168 1323
1169sub set_text { 1324sub set_text {
1170 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1171 1326
1278 1433
1279package CFClient::UI::Entry; 1434package CFClient::UI::Entry;
1280 1435
1281our @ISA = CFClient::UI::EntryBase::; 1436our @ISA = CFClient::UI::EntryBase::;
1282 1437
1283use SDL::OpenGL; 1438use CFClient::OpenGL;
1284 1439
1285sub key_down { 1440sub key_down {
1286 my ($self, $ev) = @_; 1441 my ($self, $ev) = @_;
1287 1442
1288 my $sym = $ev->{sym}; 1443 my $sym = $ev->{sym};
1301 1456
1302package CFClient::UI::Button; 1457package CFClient::UI::Button;
1303 1458
1304our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1305 1460
1306use SDL::OpenGL; 1461use CFClient::OpenGL;
1307 1462
1308my @tex = 1463my @tex =
1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1310 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1311 1466
1312sub new { 1467sub new {
1313 my $class = shift; 1468 my $class = shift;
1314 1469
1318 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1319 active_fg => [0, 0, 1], 1474 active_fg => [0, 0, 1],
1320 can_hover => 1, 1475 can_hover => 1,
1321 align => 0, 1476 align => 0,
1322 valign => 0, 1477 valign => 0,
1478 can_events => 1,
1323 @_ 1479 @_
1324 ) 1480 )
1325} 1481}
1326 1482
1327sub button_up { 1483sub button_up {
1361package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1362 1518
1363our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1364 1520
1365my @tex = 1521my @tex =
1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1367 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1368 1524
1369use SDL::OpenGL; 1525use CFClient::OpenGL;
1370 1526
1371sub new { 1527sub new {
1372 my $class = shift; 1528 my $class = shift;
1373 1529
1374 $class->SUPER::new ( 1530 $class->SUPER::new (
1420 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1421} 1577}
1422 1578
1423############################################################################# 1579#############################################################################
1424 1580
1425package CFClient::UI::VGauge; 1581package CFClient::UI::Image;
1426 1582
1427our @ISA = CFClient::UI::Base::; 1583our @ISA = CFClient::UI::Base::;
1428 1584
1429use SDL::OpenGL; 1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1430 1587
1431my %tex = ( 1588our %loaded_images;
1432 food => [
1433 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1434 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1435 ],
1436 grace => [
1437 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1438 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1439 ],
1440 hp => [
1441 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1442 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1443 ],
1444 mana => [
1445 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1446 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1447 ],
1448);
1449 1589
1450# eg. VGauge->new (gauge => 'food'), default gauge: food
1451sub new { 1590sub new {
1452 my $class = shift; 1591 my $class = shift;
1453 1592
1454 my $self = $class->SUPER::new (gauge => 'food', @_); 1593 my $self = $class->SUPER::new (can_events => 0, @_);
1594
1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1596
1597 $loaded_images{$self->{image}} ||=
1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1599
1600 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1601
1602 Scalar::Util::weaken $loaded_images{$self->{image}};
1603
1604 $self->{aspect} = $tex->{w} / $tex->{h};
1455 1605
1456 $self 1606 $self
1457} 1607}
1458 1608
1459sub size_request { 1609sub size_request {
1460 my ($self) = @_; 1610 my ($self) = @_;
1461 1611
1462 my $tex = $tex{$self->{gauge}}[0]; 1612 ($self->{tex}->{w}, $self->{tex}->{h})
1463
1464 @$tex{qw(w h)}
1465}
1466
1467sub set_max {
1468 my ($self, $max) = @_;
1469
1470 $self->{max_val} = $max;
1471}
1472
1473sub set_value {
1474 my ($self, $val, $max) = @_;
1475
1476 $self->set_max ($max)
1477 if defined $max;
1478
1479 $max = $self->{max_val};
1480 $self->{val} = $val;
1481
1482 $self->update;
1483} 1613}
1484 1614
1485sub _draw { 1615sub _draw {
1486 my ($self) = @_; 1616 my ($self) = @_;
1487 1617
1488 my $tex = $tex{$self->{gauge}}; 1618 my $tex = $self->{tex};
1489 1619
1490 my ($w, $h) = ($self->{w}, $self->{h}); 1620 my ($w, $h) = ($self->{w}, $self->{h});
1491 1621
1492 my $ycut = $self->{val} / ($self->{max_val} || 1); 1622 if ($self->{rot90}) {
1493 $ycut = 1 if $self->{val} > $self->{max_val}; 1623 glRotate 90, 0, 0, 1;
1624 glTranslate 0, -$self->{w}, 0;
1494 1625
1495 my $t1 = $tex->[0]; 1626 ($w, $h) = ($h, $w);
1496 my $t2 = $tex->[1]; 1627 }
1497 1628
1498 glEnable GL_BLEND; 1629 glEnable GL_BLEND;
1499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1630 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1500 glEnable GL_TEXTURE_2D; 1631 glEnable GL_TEXTURE_2D;
1501 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1632 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1502 1633
1634 $tex->draw_quad (0, 0, $w, $h);
1635
1636 glDisable GL_BLEND;
1637 glDisable GL_TEXTURE_2D;
1638}
1639
1640#############################################################################
1641
1642package CFClient::UI::VGauge;
1643
1644our @ISA = CFClient::UI::Base::;
1645
1646use List::Util qw(min max);
1647
1648use CFClient::OpenGL;
1649
1650my %tex = (
1651 food => [
1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1654 ],
1655 grace => [
1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1658 ],
1659 hp => [
1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1662 ],
1663 mana => [
1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1666 ],
1667);
1668
1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1670sub new {
1671 my $class = shift;
1672
1673 my $self = $class->SUPER::new (
1674 type => 'food',
1675 @_
1676 );
1677
1678 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1679
1680 $self
1681}
1682
1683sub size_request {
1684 my ($self) = @_;
1685
1686 #my $tex = $tex{$self->{type}}[0];
1687 #@$tex{qw(w h)}
1688 (0, 0)
1689}
1690
1691sub set_max {
1692 my ($self, $max) = @_;
1693
1694 $self->{max_val} = $max;
1695}
1696
1697sub set_value {
1698 my ($self, $val, $max) = @_;
1699
1700 $self->set_max ($max)
1701 if defined $max;
1702
1703 $max = $self->{max_val};
1704 $self->{val} = $val;
1705
1706 $self->update;
1707}
1708
1709sub _draw {
1710 my ($self) = @_;
1711
1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1714
1715 my ($w, $h) = ($self->{w}, $self->{h});
1716
1717 if ($self->{vertical}) {
1718 glRotate 90, 0, 0, 1;
1719 glTranslate 0, -$self->{w}, 0;
1720
1721 ($w, $h) = ($h, $w);
1722 }
1723
1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1725
1726 my $ycut1 = max 0, min 1, $ycut;
1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1503 my $h1 = $self->{h} - $ycut * $self->{h}; 1729 my $h1 = $self->{h} * (1 - $ycut1);
1504 my $h2 = $ycut * $self->{h}; 1730 my $h2 = $self->{h} * (1 - $ycut2);
1505 1731
1506 my $yp = 0; 1732 glEnable GL_BLEND;
1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1734 glEnable GL_TEXTURE_2D;
1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1507 1736
1508 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1509 glBegin GL_QUADS; 1738 glBegin GL_QUADS;
1510 glTexCoord 0 , 0; glVertex 0 , $yp; 1739 glTexCoord 0 , 0; glVertex 0 , 0;
1511 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1512 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1513 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1514 glEnd; 1743 glEnd;
1515 1744
1516 $yp += $h1; 1745 my $ycut1 = List::Util::min 1, $ycut;
1517
1518 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1519 glBegin GL_QUADS; 1747 glBegin GL_QUADS;
1520 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1521 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1522 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1523 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1524 glEnd; 1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1525 1763
1526 glDisable GL_BLEND; 1764 glDisable GL_BLEND;
1527 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1528} 1766}
1529 1767
1530############################################################################# 1768#############################################################################
1531 1769
1770package CFClient::UI::Gauge;
1771
1772our @ISA = CFClient::UI::VBox::;
1773
1774sub new {
1775 my ($class, %arg) = @_;
1776
1777 my $self = $class->SUPER::new (
1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1782 );
1783
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1787
1788 $self
1789}
1790
1791sub set_fontsize {
1792 my ($self, $fsize) = @_;
1793
1794 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize);
1796}
1797
1798sub set_value {
1799 my ($self, $val, $max) = @_;
1800
1801 $self->set_max ($max)
1802 if defined $max;
1803
1804 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1813}
1814
1815#############################################################################
1816
1532package CFClient::UI::Slider; 1817package CFClient::UI::Slider;
1533 1818
1534use strict; 1819use strict;
1535 1820
1536use SDL::OpenGL; 1821use CFClient::OpenGL;
1537 1822
1538our @ISA = CFClient::UI::DrawBG::; 1823our @ISA = CFClient::UI::DrawBG::;
1539 1824
1540my @tex = 1825my @tex =
1541 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1666 1951
1667package CFClient::UI::TextView; 1952package CFClient::UI::TextView;
1668 1953
1669our @ISA = CFClient::UI::HBox::; 1954our @ISA = CFClient::UI::HBox::;
1670 1955
1671use SDL::OpenGL; 1956use CFClient::OpenGL;
1672 1957
1673sub new { 1958sub new {
1674 my $class = shift; 1959 my $class = shift;
1675 1960
1676 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1677 fontsize => 1, 1962 fontsize => 1,
1963 #font => default_font
1678 @_, 1964 @_,
1679 1965
1680 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1681 par => [], 1967 par => [],
1682 height => 0, 1968 height => 0,
1722sub size_allocate { 2008sub size_allocate {
1723 my ($self, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1724 2010
1725 $self->SUPER::size_allocate ($w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1726 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1727 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1728 $self->{layout}->set_width ($self->{children}[0]{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1729 2016
1730 $self->reflow; 2017 $self->reflow;
1731} 2018}
1783 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1784 2071
1785 my $y = 0; 2072 my $y = 0;
1786 2073
1787 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1788 2077
1789 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1790 my $h = $par->[0]; 2079 my $h = $par->[0];
1791 2080
1792 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
1822 2111
1823############################################################################# 2112#############################################################################
1824 2113
1825package CFClient::UI::Animator; 2114package CFClient::UI::Animator;
1826 2115
1827use SDL::OpenGL; 2116use CFClient::OpenGL;
1828 2117
1829our @ISA = CFClient::UI::Bin::; 2118our @ISA = CFClient::UI::Bin::;
1830 2119
1831sub moveto { 2120sub moveto {
1832 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
1872 my $class = shift; 2161 my $class = shift;
1873 2162
1874 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
1875 state => 0, 2164 state => 0,
1876 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
1877 @_ 2167 @_
1878 ); 2168 );
1879 2169
1880 if ($self->{state}) { 2170 if ($self->{state}) {
1881 $self->{state} = 0; 2171 $self->{state} = 0;
1901 $self->emit (changed => $self->{state}); 2191 $self->emit (changed => $self->{state});
1902} 2192}
1903 2193
1904############################################################################# 2194#############################################################################
1905 2195
2196package CFClient::UI::Tooltip;
2197
2198our @ISA = CFClient::UI::Bin::;
2199
2200use CFClient::OpenGL;
2201
2202sub new {
2203 my $class = shift;
2204
2205 $class->SUPER::new (
2206 @_,
2207 can_events => 0,
2208 )
2209}
2210
2211sub set_markup {
2212 my ($self, $text) = @_;
2213
2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2215 $self->{label}->set_markup ($text);
2216 $self->add ($self->{label});
2217}
2218
2219sub size_request {
2220 my ($self) = @_;
2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2225
2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2233}
2234
2235sub _draw {
2236 my ($self) = @_;
2237
2238 glPushMatrix;
2239 glTranslate 0.375, 0.375;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 glColor 1, 0.8, 0.4;
2244 glBegin GL_QUADS;
2245 glVertex 0 , 0;
2246 glVertex 0 , $h;
2247 glVertex $w, $h;
2248 glVertex $w, 0;
2249 glEnd;
2250
2251 glColor 0, 0, 0;
2252 glBegin GL_LINE_LOOP;
2253 glVertex 0 , 0;
2254 glVertex 0 , $h;
2255 glVertex $w, $h;
2256 glVertex $w, 0;
2257 glEnd;
2258
2259 glPopMatrix;
2260
2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
2264
2265#############################################################################
2266
2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
1906package CFClient::UI::Root; 2305package CFClient::UI::Root;
1907 2306
1908our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
1909 2308
1910use SDL::OpenGL; 2309use CFClient::OpenGL;
1911 2310
1912sub check_size { 2311sub check_size {
1913 my ($self) = @_; 2312 my ($self) = @_;
1914 2313
1915 $self->configure (0, 0, $::WITH, $::HEIGHT); 2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
1916} 2315}
1917 2316
1918sub size_request { 2317sub size_request {
1919 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
1920} 2319}
1922sub configure { 2321sub configure {
1923 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
1924 2323
1925 $self->SUPER::configure ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
1926 2325
1927 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1928 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2328
2329 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H);
2332 }
1929} 2333}
1930 2334
1931sub _topleft { 2335sub _topleft {
1932 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
1933 2337
1941 ::refresh (); 2345 ::refresh ();
1942} 2346}
1943 2347
1944sub add { 2348sub add {
1945 my ($self, $child) = @_; 2349 my ($self, $child) = @_;
2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
1946 2354
1947 $self->SUPER::add ($child); 2355 $self->SUPER::add ($child);
1948} 2356}
1949 2357
1950sub on_refresh { 2358sub on_refresh {
1976############################################################################# 2384#############################################################################
1977 2385
1978package CFClient::UI; 2386package CFClient::UI;
1979 2387
1980$ROOT = new CFClient::UI::Root; 2388$ROOT = new CFClient::UI::Root;
2389$TOOLTIP = new CFClient::UI::Tooltip;
1981 2390
19821 23911
1983 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines