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.156 by elmex, Sun Apr 23 02:50:42 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}
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_(.*)$/) {
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
165} 214}
263} 312}
264 313
265sub find_widget { 314sub find_widget {
266 my ($self, $x, $y) = @_; 315 my ($self, $x, $y) = @_;
267 316
317 return () unless $self->{can_events};
318
268 return $self 319 return $self
269 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 320 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
270 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 321 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
271 322
272 () 323 ()
279} 330}
280 331
281sub check_size { 332sub check_size {
282 my ($self) = @_; 333 my ($self) = @_;
283 334
335 return unless $self->{parent};
336
284 my ($w, $h) = $self->size_request; 337 my ($w, $h) = $self->size_request;
285 338
286 if ($w != $self->{req_w} || $h != $self->{req_h}) { 339 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w; 340 $self->{req_w} = $w;
288 $self->{req_h} = $h; 341 $self->{req_h} = $h;
289 342
290 $self->{parent}->check_size 343 $self->{parent}->check_size;
291 if $self->{parent};
292 } 344 }
293} 345}
294 346
295sub update { 347sub update {
296 my ($self) = @_; 348 my ($self) = @_;
324package CFClient::UI::DrawBG; 376package CFClient::UI::DrawBG;
325 377
326our @ISA = CFClient::UI::Base::; 378our @ISA = CFClient::UI::Base::;
327 379
328use strict; 380use strict;
329use SDL::OpenGL; 381use CFClient::OpenGL;
330 382
331sub new { 383sub new {
332 my $class = shift; 384 my $class = shift;
333 385
334 # range [value, low, high, page] 386 # range [value, low, high, page]
363 415
364package CFClient::UI::Empty; 416package CFClient::UI::Empty;
365 417
366our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
367 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
368sub size_request { 425sub size_request {
369 (0, 0) 426 (0, 0)
370} 427}
371 428
372sub draw { } 429sub draw { }
380sub new { 437sub new {
381 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
382 439
383 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
384 441
385 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
386 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
387 444
388 $self 445 $self
389} 446}
390 447
409 delete $child->{parent}; 466 delete $child->{parent};
410 467
411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 468 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
412 469
413 $self->check_size; 470 $self->check_size;
471 $self->update;
414} 472}
415 473
416sub find_widget { 474sub find_widget {
417 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
418 476
482 540
483package CFClient::UI::Window; 541package CFClient::UI::Window;
484 542
485our @ISA = CFClient::UI::Bin::; 543our @ISA = CFClient::UI::Bin::;
486 544
487use SDL::OpenGL; 545use CFClient::OpenGL;
488 546
489sub new { 547sub new {
490 my ($class, %arg) = @_; 548 my ($class, %arg) = @_;
491 549
492 my $self = $class->SUPER::new (%arg); 550 my $self = $class->SUPER::new (%arg);
546sub new { die } 604sub new { die }
547 605
548sub size_request { 606sub size_request {
549 my ($self) = @_; 607 my ($self) = @_;
550 608
551 @$self{qw(child_w child_h)} = $self->child->size_request; 609 @$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)}); 610 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553 611
554 @$self{qw(child_w child_h)} 612 @$self{qw(child_w child_h)}
555} 613}
556 614
565 623
566package CFClient::UI::Frame; 624package CFClient::UI::Frame;
567 625
568our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
569 627
570use SDL::OpenGL; 628use 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 629
618sub new { 630sub new {
619 my $class = shift; 631 my $class = shift;
620 632
621 # TODO: user_x, user_y, overwrite moveto?
622
623 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
626 border => 0.8, 636 border => 0.8,
627 @_ 637 @_
628 ) 638 );
639
640 $self
641}
642
643sub set_size {
644 my ($self, $w, $h) = @_;
645 $self->{req_w} = $w;
646 $self->{req_h} = $h;
647 $self->check_size;
648}
649
650sub size_request {
651 my ($self) = @_;
652 ($self->{req_w}, $self->{req_h})
653}
654
655sub size_allocate {
656 my ($self, $w, $h) = @_;
657 $self->{w} = $w;
658 $self->{h} = $h;
659 $self->child->configure (0, 0, $w, $h);
660}
661
662sub _draw {
663 my ($self) = @_;
664
665 my ($w, $h) = ($self->{w}, $self->{h});
666
667 glEnable GL_BLEND;
668 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
669 glEnable GL_TEXTURE_2D;
670 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
671
672# glBegin GL_QUADS;
673# glColor 0, 0, 0, 0;
674# glVertex 0 , 0;
675# glVertex 0 , $h;
676# glVertex $w, $h;
677# glVertex $w, 0;
678# glEnd;
679
680
681 $self->child->draw;
682 glDisable GL_BLEND;
683 glDisable GL_TEXTURE_2D;
684}
685
686#############################################################################
687
688package CFClient::UI::FancyFrame;
689
690our @ISA = CFClient::UI::Bin::;
691
692use CFClient::OpenGL;
693
694my @tex =
695 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
696 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
697
698sub new {
699 my $class = shift;
700
701 # TODO: user_x, user_y, overwrite moveto?
702
703 my $self = $class->SUPER::new (
704 bg => [1, 1, 1, 1],
705 border_bg => [1, 1, 1, 1],
706 border => 0.8,
707 can_events => 1,
708 @_
709 );
710
711 $self->{title} &&= new CFClient::UI::Label
712 align => 0,
713 valign => 1,
714 text => $self->{title},
715 fontsize => 1;
716
717 $self
629} 718}
630 719
631sub border { 720sub border {
632 int $_[0]{border} * $::FONTSIZE 721 int $_[0]{border} * $::FONTSIZE
633} 722}
649 my ($self, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
650 739
651 $h -= List::Util::max 0, $self->border * 2; 740 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 741 $w -= List::Util::max 0, $self->border * 2;
653 742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
745
654 $self->child->configure ($self->border, $self->border, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
655} 747}
656 748
657sub button_down { 749sub button_down {
658 my ($self, $ev, $x, $y) = @_; 750 my ($self, $ev, $x, $y) = @_;
660 my $border = $self->border; 752 my $border = $self->border;
661 753
662 if ($x < $self->{w} && $x >= $self->{w} - $border 754 if ($x < $self->{w} && $x >= $self->{w} - $border
663 && $y < $self->{h} && $y >= $self->{h} - $border) { 755 && $y < $self->{h} && $y >= $self->{h} - $border) {
664 756
665 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 757 my ($ox, $oy) = ($ev->{x}, $ev->{y});
666 my ($bw, $bh) = ($self->{w}, $self->{h}); 758 my ($bw, $bh) = ($self->{w}, $self->{h});
667 759
668 $self->{motion} = sub { 760 $self->{motion} = sub {
669 my ($ev, $x, $y) = @_; 761 my ($ev, $x, $y) = @_;
670 762
671 ($x, $y) = ($ev->motion_x, $ev->motion_y); 763 ($x, $y) = ($ev->{x}, $ev->{y});
672 764
673 $self->{user_w} = $bw + $x - $ox; 765 $self->{user_w} = $bw + $x - $ox;
674 $self->{user_h} = $bh + $y - $oy; 766 $self->{user_h} = $bh + $y - $oy;
675 $self->check_size; 767 $self->check_size;
676 }; 768 };
677 769
678 } elsif ($x >= 0 && $x < $self->{w} 770 } elsif ($x >= 0 && $x < $self->{w}
679 && $y >= 0 && $y < $border) { 771 && $y >= 0 && $y < $border) {
680 772
681 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 773 my ($ox, $oy) = ($ev->{x}, $ev->{y});
682 my ($bx, $by) = ($self->{x}, $self->{y}); 774 my ($bx, $by) = ($self->{x}, $self->{y});
683 775
684 $self->{motion} = sub { 776 $self->{motion} = sub {
685 my ($ev, $x, $y) = @_; 777 my ($ev, $x, $y) = @_;
686 778
687 ($x, $y) = ($ev->motion_x, $ev->motion_y); 779 ($x, $y) = ($ev->{x}, $ev->{y});
688 780
689 $self->move ($bx + $x - $ox, $by + $y - $oy); 781 $self->move ($bx + $x - $ox, $by + $y - $oy);
690 $self->update; 782 $self->update;
691 }; 783 };
692 } 784 }
737 $bg->draw_quad ($border, $border, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
738 830
739 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 832 glDisable GL_BLEND;
741 833
834 $self->{title}->draw if $self->{title};
742 $self->child->draw; 835 $self->child->draw;
743} 836}
744 837
745############################################################################# 838#############################################################################
746 839
748 841
749our @ISA = CFClient::UI::Base::; 842our @ISA = CFClient::UI::Base::;
750 843
751use List::Util qw(max sum); 844use List::Util qw(max sum);
752 845
753use SDL::OpenGL; 846use CFClient::OpenGL;
754 847
755sub new { 848sub new {
756 my $class = shift; 849 my $class = shift;
757 850
758 $class->SUPER::new ( 851 $class->SUPER::new (
788 or next; 881 or next;
789 882
790 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
791 my $widget = $row->[$x] 884 my $widget = $row->[$x]
792 or next; 885 or next;
793 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
794 887
795 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
796 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
797 } 890 }
798 } 891 }
903 996
904 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
905 998
906 my $children = $self->{children}; 999 my $children = $self->{children};
907 1000
908 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
909 1002
910 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
911 1004
912 if ($req_h > $h) { 1005 if ($req_h > $h) {
913 # ah well, not enough space 1006 # ah well, not enough space
961sub size_allocate { 1054sub size_allocate {
962 my ($self, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
963 1056
964 my $children = $self->{children}; 1057 my $children = $self->{children};
965 1058
966 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
967 1060
968 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
969 1062
970 if ($req_h > $h) { 1063 if ($req_h > $h) {
971 # ah well, not enough space 1064 # ah well, not enough space
999 1092
1000package CFClient::UI::Label; 1093package CFClient::UI::Label;
1001 1094
1002our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
1003 1096
1004use SDL::OpenGL; 1097use CFClient::OpenGL;
1005 1098
1006sub new { 1099sub new {
1007 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
1008 1101
1009 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
1012 text => "", 1105 text => "",
1013 align => -1, 1106 align => -1,
1014 valign => -1, 1107 valign => -1,
1015 padding => 2, 1108 padding => 2,
1016 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
1017 %arg 1111 %arg
1018 ); 1112 );
1019 1113
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1114 if (exists $self->{template}) {
1115 my $layout = new CFClient::Layout;
1116 $layout->set_text (delete $self->{template});
1117 $self->{template} = $layout;
1118 }
1021 1119
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1120 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1121 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1122
1025 $self 1123 $self
1039 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
1040 1138
1041 $self->{layout}->set_text ($text); 1139 $self->{layout}->set_text ($text);
1042 1140
1043 delete $self->{texture}; 1141 delete $self->{texture};
1142 $self->check_size;
1044 $self->update; 1143 $self->update;
1045} 1144}
1046 1145
1047sub set_markup { 1146sub set_markup {
1048 my ($self, $markup) = @_; 1147 my ($self, $markup) = @_;
1049 1148
1050 $self->{layout}->set_markup ($markup); 1149 $self->{layout}->set_markup ($markup);
1051 1150
1052 delete $self->{texture}; 1151 delete $self->{texture};
1152 $self->check_size;
1053 $self->update; 1153 $self->update;
1054} 1154}
1055 1155
1056sub size_request { 1156sub size_request {
1057 my ($self) = @_; 1157 my ($self) = @_;
1058 1158
1059 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1161
1062 my ($w, $h) = $self->{layout}->size; 1162 my ($w, $h) = $self->{layout}->size;
1163
1164 if (exists $self->{template}) {
1165 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1166
1167 my ($w2, $h2) = $self->{template}->size;
1168
1169 $w = List::Util::max $w, $w2;
1170 $h = List::Util::max $h, $h2;
1171 }
1063 1172
1064 ( 1173 (
1065 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
1067 ) 1176 )
1069 1178
1070sub size_allocate { 1179sub size_allocate {
1071 my ($self, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
1072 1181
1073 delete $self->{texture}; 1182 delete $self->{texture};
1183}
1184
1185sub set_fontsize {
1186 my ($self, $fontsize) = @_;
1187
1188 $self->{fontsize} = $fontsize;
1189 delete $self->{texture};
1190 $self->check_size;
1191 $self->update;
1074} 1192}
1075 1193
1076sub _draw { 1194sub _draw {
1077 my ($self) = @_; 1195 my ($self) = @_;
1078 1196
1111 1229
1112package CFClient::UI::EntryBase; 1230package CFClient::UI::EntryBase;
1113 1231
1114our @ISA = CFClient::UI::Label::; 1232our @ISA = CFClient::UI::Label::;
1115 1233
1116use SDL::OpenGL; 1234use CFClient::OpenGL;
1117 1235
1118sub new { 1236sub new {
1119 my $class = shift; 1237 my $class = shift;
1120 1238
1121 $class->SUPER::new ( 1239 $class->SUPER::new (
1124 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1125 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1126 can_hover => 1, 1244 can_hover => 1,
1127 can_focus => 1, 1245 can_focus => 1,
1128 valign => 0, 1246 valign => 0,
1247 can_events => 1,
1129 @_ 1248 @_
1130 ) 1249 )
1131} 1250}
1132 1251
1133sub _set_text { 1252sub _set_text {
1161} 1280}
1162 1281
1163sub size_allocate { 1282sub size_allocate {
1164 my ($self, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1165 1284
1166 $self->_set_text ($self->{text}); 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1167} 1286}
1168 1287
1169sub set_text { 1288sub set_text {
1170 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1171 1290
1278 1397
1279package CFClient::UI::Entry; 1398package CFClient::UI::Entry;
1280 1399
1281our @ISA = CFClient::UI::EntryBase::; 1400our @ISA = CFClient::UI::EntryBase::;
1282 1401
1283use SDL::OpenGL; 1402use CFClient::OpenGL;
1284 1403
1285sub key_down { 1404sub key_down {
1286 my ($self, $ev) = @_; 1405 my ($self, $ev) = @_;
1287 1406
1288 my $sym = $ev->{sym}; 1407 my $sym = $ev->{sym};
1301 1420
1302package CFClient::UI::Button; 1421package CFClient::UI::Button;
1303 1422
1304our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1305 1424
1306use SDL::OpenGL; 1425use CFClient::OpenGL;
1307 1426
1308my @tex = 1427my @tex =
1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1310 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1311 1430
1312sub new { 1431sub new {
1313 my $class = shift; 1432 my $class = shift;
1314 1433
1318 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1319 active_fg => [0, 0, 1], 1438 active_fg => [0, 0, 1],
1320 can_hover => 1, 1439 can_hover => 1,
1321 align => 0, 1440 align => 0,
1322 valign => 0, 1441 valign => 0,
1442 can_events => 1,
1323 @_ 1443 @_
1324 ) 1444 )
1325} 1445}
1326 1446
1327sub button_up { 1447sub button_up {
1361package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1362 1482
1363our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1364 1484
1365my @tex = 1485my @tex =
1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1367 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1368 1488
1369use SDL::OpenGL; 1489use CFClient::OpenGL;
1370 1490
1371sub new { 1491sub new {
1372 my $class = shift; 1492 my $class = shift;
1373 1493
1374 $class->SUPER::new ( 1494 $class->SUPER::new (
1420 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1421} 1541}
1422 1542
1423############################################################################# 1543#############################################################################
1424 1544
1425package CFClient::UI::VGauge; 1545package CFClient::UI::Image;
1426 1546
1427our @ISA = CFClient::UI::Base::; 1547our @ISA = CFClient::UI::Base::;
1428 1548
1429use SDL::OpenGL; 1549use CFClient::OpenGL;
1550use Carp qw/confess/;
1430 1551
1431my %tex = ( 1552our %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 1553
1450# eg. VGauge->new (gauge => 'food'), default gauge: food
1451sub new { 1554sub new {
1452 my $class = shift; 1555 my $class = shift;
1453 1556
1454 my $self = $class->SUPER::new (gauge => 'food', @_); 1557 my $self = $class->SUPER::new (can_events => 0, @_);
1558
1559 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1560
1561 $loaded_images{$self->{image}} ||=
1562 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1563
1564 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1565
1566 Scalar::Util::weaken $loaded_images{$self->{image}};
1567
1568 $self->{aspect} = $tex->{w} / $tex->{h};
1455 1569
1456 $self 1570 $self
1457} 1571}
1458 1572
1459sub size_request { 1573sub size_request {
1460 my ($self) = @_; 1574 my ($self) = @_;
1461 1575
1462 my $tex = $tex{$self->{gauge}}[0]; 1576 ($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} 1577}
1484 1578
1485sub _draw { 1579sub _draw {
1486 my ($self) = @_; 1580 my ($self) = @_;
1487 1581
1488 my $tex = $tex{$self->{gauge}}; 1582 my $tex = $self->{tex};
1489 1583
1490 my ($w, $h) = ($self->{w}, $self->{h}); 1584 my ($w, $h) = ($self->{w}, $self->{h});
1491 1585
1492 my $ycut = $self->{val} / ($self->{max_val} || 1); 1586 if ($self->{rot90}) {
1493 $ycut = 1 if $self->{val} > $self->{max_val}; 1587 glRotate 90, 0, 0, 1;
1588 glTranslate 0, -$self->{w}, 0;
1494 1589
1495 my $t1 = $tex->[0]; 1590 ($w, $h) = ($h, $w);
1496 my $t2 = $tex->[1]; 1591 }
1497 1592
1498 glEnable GL_BLEND; 1593 glEnable GL_BLEND;
1499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1594 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1500 glEnable GL_TEXTURE_2D; 1595 glEnable GL_TEXTURE_2D;
1501 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1502 1597
1598 $tex->draw_quad (0, 0, $w, $h);
1599
1600 glDisable GL_BLEND;
1601 glDisable GL_TEXTURE_2D;
1602}
1603
1604#############################################################################
1605
1606package CFClient::UI::VGauge;
1607
1608our @ISA = CFClient::UI::Base::;
1609
1610use CFClient::OpenGL;
1611
1612my %tex = (
1613 food => [
1614 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1615 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1616 ],
1617 grace => [
1618 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1619 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1620 ],
1621 hp => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1624 ],
1625 mana => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1628 ],
1629);
1630
1631# eg. VGauge->new (gauge => 'food'), default gauge: food
1632sub new {
1633 my $class = shift;
1634
1635 my $self = $class->SUPER::new (
1636 type => 'food',
1637 @_
1638 );
1639
1640 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1641
1642 $self
1643}
1644
1645sub size_request {
1646 my ($self) = @_;
1647
1648 #my $tex = $tex{$self->{type}}[0];
1649 #@$tex{qw(w h)}
1650 (0, 0)
1651}
1652
1653sub set_max {
1654 my ($self, $max) = @_;
1655
1656 $self->{max_val} = $max;
1657}
1658
1659sub set_value {
1660 my ($self, $val, $max) = @_;
1661
1662 $self->set_max ($max)
1663 if defined $max;
1664
1665 $max = $self->{max_val};
1666 $self->{val} = $val;
1667
1668 $self->update;
1669}
1670
1671sub _draw {
1672 my ($self) = @_;
1673
1674 my $tex = $tex{$self->{type}};
1675
1676 my ($w, $h) = ($self->{w}, $self->{h});
1677
1678 if ($self->{vertical}) {
1679 glRotate 90, 0, 0, 1;
1680 glTranslate 0, -$self->{w}, 0;
1681
1682 ($w, $h) = ($h, $w);
1683 }
1684
1685 my $ycut = $self->{val} / ($self->{max_val} || 1);
1686 $ycut = 1 if $self->{val} > $self->{max_val};
1687
1688 my $t1 = $tex->[0];
1689 my $t2 = $tex->[1];
1690
1691 glEnable GL_BLEND;
1692 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1693 glEnable GL_TEXTURE_2D;
1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1695
1503 my $h1 = $self->{h} - $ycut * $self->{h}; 1696 my $h1 = $self->{h} - $ycut * $self->{h};
1504 my $h2 = $ycut * $self->{h}; 1697 my $h2 = $ycut * $self->{h};
1505 1698
1506 my $yp = 0;
1507
1508 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1699 glBindTexture GL_TEXTURE_2D, $t1->{name};
1509 glBegin GL_QUADS; 1700 glBegin GL_QUADS;
1510 glTexCoord 0 , 0; glVertex 0 , $yp; 1701 glTexCoord 0 , 0; glVertex 0 , 0;
1511 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1512 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1513 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1704 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1514 glEnd; 1705 glEnd;
1515
1516 $yp += $h1;
1517 1706
1518 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1707 glBindTexture GL_TEXTURE_2D, $t2->{name};
1519 glBegin GL_QUADS; 1708 glBegin GL_QUADS;
1520 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1521 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1522 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1523 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1524 glEnd; 1713 glEnd;
1525 1714
1526 glDisable GL_BLEND; 1715 glDisable GL_BLEND;
1527 glDisable GL_TEXTURE_2D; 1716 glDisable GL_TEXTURE_2D;
1528} 1717}
1529 1718
1530############################################################################# 1719#############################################################################
1531 1720
1721package CFClient::UI::Gauge;
1722
1723our @ISA = CFClient::UI::VBox::;
1724
1725sub new {
1726 my ($class, %arg) = @_;
1727
1728 my $self = $class->SUPER::new (
1729 tooltip => $arg{type},
1730 %arg,
1731 );
1732
1733 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1736
1737 $self
1738}
1739
1740sub set_fontsize {
1741 my ($self, $fsize) = @_;
1742
1743 $self->{value}->set_fontsize ($fsize);
1744 $self->{max} ->set_fontsize ($fsize);
1745}
1746
1747sub set_value {
1748 my ($self, $val, $max) = @_;
1749
1750 $self->set_max ($max)
1751 if defined $max;
1752
1753 $self->{gauge}->set_value ($val, $max);
1754 $self->{value}->set_text ($val);
1755}
1756
1757sub set_max {
1758 my ($self, $max) = @_;
1759
1760 $self->{gauge}->set_max ($max);
1761 $self->{max}->set_text ($max);
1762}
1763
1764#############################################################################
1765
1532package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1533 1767
1534use strict; 1768use strict;
1535 1769
1536use SDL::OpenGL; 1770use CFClient::OpenGL;
1537 1771
1538our @ISA = CFClient::UI::DrawBG::; 1772our @ISA = CFClient::UI::DrawBG::;
1539 1773
1540my @tex = 1774my @tex =
1541 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1775 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1666 1900
1667package CFClient::UI::TextView; 1901package CFClient::UI::TextView;
1668 1902
1669our @ISA = CFClient::UI::HBox::; 1903our @ISA = CFClient::UI::HBox::;
1670 1904
1671use SDL::OpenGL; 1905use CFClient::OpenGL;
1672 1906
1673sub new { 1907sub new {
1674 my $class = shift; 1908 my $class = shift;
1675 1909
1676 my $self = $class->SUPER::new ( 1910 my $self = $class->SUPER::new (
1822 2056
1823############################################################################# 2057#############################################################################
1824 2058
1825package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
1826 2060
1827use SDL::OpenGL; 2061use CFClient::OpenGL;
1828 2062
1829our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
1830 2064
1831sub moveto { 2065sub moveto {
1832 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
1872 my $class = shift; 2106 my $class = shift;
1873 2107
1874 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1875 state => 0, 2109 state => 0,
1876 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1877 @_ 2112 @_
1878 ); 2113 );
1879 2114
1880 if ($self->{state}) { 2115 if ($self->{state}) {
1881 $self->{state} = 0; 2116 $self->{state} = 0;
1901 $self->emit (changed => $self->{state}); 2136 $self->emit (changed => $self->{state});
1902} 2137}
1903 2138
1904############################################################################# 2139#############################################################################
1905 2140
2141package CFClient::UI::Tooltip;
2142
2143our @ISA = CFClient::UI::Bin::;
2144
2145use CFClient::OpenGL;
2146
2147sub new {
2148 my $class = shift;
2149
2150 $class->SUPER::new (
2151 @_,
2152 can_events => 0,
2153 )
2154}
2155
2156sub set_text {
2157 my ($self, $text) = @_;
2158
2159 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2160 $self->{label}->set_text ($text);
2161 $self->add ($self->{label});
2162}
2163
2164sub size_request {
2165 my ($self) = @_;
2166
2167 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2168
2169 $w = List::Util::min $::WIDTH * 0.2, $w;
2170 $h = List::Util::max $::HEIGHT * 0.2, $h;
2171
2172 ($w + 4, $h + 4)
2173}
2174
2175sub _draw {
2176 my ($self) = @_;
2177
2178 glPushMatrix;
2179 glTranslate 0.375, 0.375;
2180
2181 my ($w, $h) = @$self{qw(w h)};
2182
2183 glColor 1, 0.8, 0.4;
2184 glBegin GL_QUADS;
2185 glVertex 0 , 0;
2186 glVertex 0 , $h;
2187 glVertex $w, $h;
2188 glVertex $w, 0;
2189 glEnd;
2190
2191 glColor 0, 0, 0;
2192 glBegin GL_LINE_LOOP;
2193 glVertex 0 , 0;
2194 glVertex 0 , $h;
2195 glVertex $w, $h;
2196 glVertex $w, 0;
2197 glEnd;
2198
2199 glPopMatrix;
2200
2201 glTranslate 2, 2;
2202 $self->SUPER::_draw;
2203}
2204
2205#############################################################################
2206
1906package CFClient::UI::Root; 2207package CFClient::UI::Root;
1907 2208
1908our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
1909 2210
1910use SDL::OpenGL; 2211use CFClient::OpenGL;
1911 2212
1912sub check_size { 2213sub check_size {
1913 my ($self) = @_; 2214 my ($self) = @_;
1914 2215
1915 $self->configure (0, 0, $::WITH, $::HEIGHT); 2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
1916} 2217}
1917 2218
1918sub size_request { 2219sub size_request {
1919 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
1920} 2221}
1922sub configure { 2223sub configure {
1923 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
1924 2225
1925 $self->SUPER::configure ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
1926 2227
1927 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1928 for @{$self->{children}}; 2228 for my $child (@{$self->{children}}) {
2229 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2230
2231 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2232 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2233 $child->configure ($X, $Y, $W,$H);
2234 }
1929} 2235}
1930 2236
1931sub _topleft { 2237sub _topleft {
1932 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
1933 2239
1941 ::refresh (); 2247 ::refresh ();
1942} 2248}
1943 2249
1944sub add { 2250sub add {
1945 my ($self, $child) = @_; 2251 my ($self, $child) = @_;
2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
1946 2256
1947 $self->SUPER::add ($child); 2257 $self->SUPER::add ($child);
1948} 2258}
1949 2259
1950sub on_refresh { 2260sub on_refresh {
1976############################################################################# 2286#############################################################################
1977 2287
1978package CFClient::UI; 2288package CFClient::UI;
1979 2289
1980$ROOT = new CFClient::UI::Root; 2290$ROOT = new CFClient::UI::Root;
2291$TOOLTIP = new CFClient::UI::Tooltip;
1981 2292
19821 22931
1983 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines