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.143 by root, Thu Apr 20 21:28:51 2006 UTC vs.
Revision 1.161 by root, Mon Apr 24 03:43:51 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_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
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
152 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154 200
155 # use alignment to adjust x, y 201 # use alignment to adjust x, y
156 202
157 $x += ($w - $w2) * 0.5; 203 $x += int +($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5; 204 $y += int +($h - $h2) * 0.5;
159 205
160 ($w, $h) = ($w2, $h2); 206 ($w, $h) = ($w2, $h2);
161 } 207 }
162 208
163 if ($self->{x} != $x || $self->{y} != $y) { 209 if ($self->{x} != $x || $self->{y} != $y) {
175 } 221 }
176} 222}
177 223
178sub size_allocate { 224sub size_allocate {
179 # 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;
180} 233}
181 234
182# return top left coordinates 235# return top left coordinates
183sub _topleft { 236sub _topleft {
184 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
278} 331}
279 332
280sub find_widget { 333sub find_widget {
281 my ($self, $x, $y) = @_; 334 my ($self, $x, $y) = @_;
282 335
336 return () unless $self->{can_events};
337
283 return $self 338 return $self
284 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 339 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
285 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 340 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
286 341
287 () 342 ()
294} 349}
295 350
296sub check_size { 351sub check_size {
297 my ($self) = @_; 352 my ($self) = @_;
298 353
299 my ($w, $h) = $self->size_request; 354 $self->{parent}
355 or return 1;
300 356
357 my ($w, $h) = $self->{user_w} && $self->{user_h}
358 ? @$self{qw(user_w user_h)}
359 : $self->size_request;
360
301 if ($w != $self->{req_w} || $h != $self->{req_h}) { 361 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w; 362 $self->{req_w} = $w;
303 $self->{req_h} = $h; 363 $self->{req_h} = $h;
304 364
305 $self->{parent}->check_size 365 $self->{parent}->check_size
306 if $self->{parent}; 366 or $self->size_allocate (
367 (List::Util::max $self->{w}, $w),
368 (List::Util::max $self->{h}, $h),
369 );
370
371 1
372 } else {
373 0
307 } 374 }
308} 375}
309 376
310sub update { 377sub update {
311 my ($self) = @_; 378 my ($self) = @_;
378 445
379package CFClient::UI::Empty; 446package CFClient::UI::Empty;
380 447
381our @ISA = CFClient::UI::Base::; 448our @ISA = CFClient::UI::Base::;
382 449
450sub new {
451 my ($class, %arg) = @_;
452 $class->SUPER::new (can_events => 0, %arg);
453}
454
383sub size_request { 455sub size_request {
384 (0, 0) 456 (0, 0)
385} 457}
386 458
387sub draw { } 459sub draw { }
395sub new { 467sub new {
396 my ($class, %arg) = @_; 468 my ($class, %arg) = @_;
397 469
398 my $children = delete $arg{children} || []; 470 my $children = delete $arg{children} || [];
399 471
400 my $self = $class->SUPER::new (children => [], %arg); 472 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
401 $self->add ($_) for @$children; 473 $self->add ($_) for @$children;
402 474
403 $self 475 $self
404} 476}
405 477
424 delete $child->{parent}; 496 delete $child->{parent};
425 497
426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 498 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
427 499
428 $self->check_size; 500 $self->check_size;
501 $self->update;
429} 502}
430 503
431sub find_widget { 504sub find_widget {
432 my ($self, $x, $y) = @_; 505 my ($self, $x, $y) = @_;
433 506
561sub new { die } 634sub new { die }
562 635
563sub size_request { 636sub size_request {
564 my ($self) = @_; 637 my ($self) = @_;
565 638
566 @$self{qw(child_w child_h)} = $self->child->size_request; 639 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 640 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568 641
569 @$self{qw(child_w child_h)} 642 @$self{qw(child_w child_h)}
570} 643}
571 644
581package CFClient::UI::Frame; 654package CFClient::UI::Frame;
582 655
583our @ISA = CFClient::UI::Bin::; 656our @ISA = CFClient::UI::Bin::;
584 657
585use CFClient::OpenGL; 658use CFClient::OpenGL;
586
587sub size_request {
588 my ($self) = @_;
589 my $chld = $self->child
590 or return (0, 0);
591
592 $chld->move (2, 2);
593
594 map { $_ + 4 } $chld->size_request;
595}
596
597sub size_allocate {
598 my ($self, $x, $y, $w, $h) = @_;
599
600 $self->child->configure (2, 2, $w - 4, $h - 4);
601}
602
603sub _draw {
604 my ($self) = @_;
605
606 my $chld = $self->child;
607
608 my ($w, $h) = $chld->size_request;
609
610 glBegin GL_QUADS;
611 glColor 0, 0, 0;
612 glVertex 0 , 0;
613 glVertex 0 , $h + 4;
614 glVertex $w + 4 , $h + 4;
615 glVertex $w + 4 , 0;
616 glEnd;
617
618 $chld->draw;
619}
620
621#############################################################################
622
623package CFClient::UI::FancyFrame;
624
625our @ISA = CFClient::UI::Bin::;
626
627use CFClient::OpenGL;
628
629my @tex =
630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
632 659
633sub new { 660sub new {
634 my $class = shift; 661 my $class = shift;
635
636 # TODO: user_x, user_y, overwrite moveto?
637 662
638 my $self = $class->SUPER::new ( 663 my $self = $class->SUPER::new (
639 bg => [1, 1, 1, 1], 664 bg => [1, 1, 1, 1],
640 border_bg => [1, 1, 1, 1], 665 border_bg => [1, 1, 1, 1],
641 border => 0.8, 666 border => 0.8,
642 @_ 667 @_
643 ); 668 );
644 669
670 $self
671}
672
673sub _draw {
674 my ($self) = @_;
675
676 my ($w, $h) = ($self->{w}, $self->{h});
677
678 glEnable GL_BLEND;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680 glEnable GL_TEXTURE_2D;
681 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
682
683# glBegin GL_QUADS;
684# glColor 0, 0, 0, 0;
685# glVertex 0 , 0;
686# glVertex 0 , $h;
687# glVertex $w, $h;
688# glVertex $w, 0;
689# glEnd;
690
691
692 $self->child->draw;
693 glDisable GL_BLEND;
694 glDisable GL_TEXTURE_2D;
695}
696
697#############################################################################
698
699package CFClient::UI::FancyFrame;
700
701our @ISA = CFClient::UI::Bin::;
702
703use CFClient::OpenGL;
704
705my @tex =
706 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
707 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
708
709sub new {
710 my $class = shift;
711
712 # TODO: user_x, user_y, overwrite moveto?
713
714 my $self = $class->SUPER::new (
715 bg => [1, 1, 1, 1],
716 border_bg => [1, 1, 1, 1],
717 border => 0.8,
718 can_events => 1,
719 @_
720 );
721
645 $self->{title} &&= new CFClient::UI::Label 722 $self->{title} &&= new CFClient::UI::Label
646 align => 0, 723 align => 0,
647 valign => 1, 724 valign => 1,
648 text => $self->{title}, 725 text => $self->{title},
649 fontsize => 1; 726 fontsize => 1;
656} 733}
657 734
658sub size_request { 735sub size_request {
659 my ($self) = @_; 736 my ($self) = @_;
660 737
661 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
662
663 my ($w, $h) = $self->SUPER::size_request; 738 my ($w, $h) = $self->SUPER::size_request;
664 739
665 ( 740 (
666 $w + $self->border * 2, 741 $w + $self->border * 2,
667 $h + $self->border * 2, 742 $h + $self->border * 2,
815 or next; 890 or next;
816 891
817 for my $x (0 .. $#$row) { 892 for my $x (0 .. $#$row) {
818 my $widget = $row->[$x] 893 my $widget = $row->[$x]
819 or next; 894 or next;
820 my ($w, $h) = $widget->size_request; 895 my ($w, $h) = @$widget{qw(req_w req_h)};
821 896
822 $w[$x] = max $w[$x], $w; 897 $w[$x] = max $w[$x], $w;
823 $h[$y] = max $h[$y], $h; 898 $h[$y] = max $h[$y], $h;
824 } 899 }
825 } 900 }
930 1005
931 ($h, $w) = ($w, $h); 1006 ($h, $w) = ($w, $h);
932 1007
933 my $children = $self->{children}; 1008 my $children = $self->{children};
934 1009
935 my @h = map +($_->size_request)[0], @$children; 1010 my @h = map $_->{req_w}, @$children;
936 1011
937 my $req_h = List::Util::sum @h; 1012 my $req_h = List::Util::sum @h;
938 1013
939 if ($req_h > $h) { 1014 if ($req_h > $h) {
940 # ah well, not enough space 1015 # ah well, not enough space
988sub size_allocate { 1063sub size_allocate {
989 my ($self, $w, $h) = @_; 1064 my ($self, $w, $h) = @_;
990 1065
991 my $children = $self->{children}; 1066 my $children = $self->{children};
992 1067
993 my @h = map +($_->size_request)[1], @$children; 1068 my @h = map $_->{req_h}, @$children;
994 1069
995 my $req_h = List::Util::sum @h; 1070 my $req_h = List::Util::sum @h;
996 1071
997 if ($req_h > $h) { 1072 if ($req_h > $h) {
998 # ah well, not enough space 1073 # ah well, not enough space
1033sub new { 1108sub new {
1034 my ($class, %arg) = @_; 1109 my ($class, %arg) = @_;
1035 1110
1036 my $self = $class->SUPER::new ( 1111 my $self = $class->SUPER::new (
1037 fg => [1, 1, 1], 1112 fg => [1, 1, 1],
1113 #font => default_font
1038 fontsize => 1, 1114 fontsize => 1,
1039 text => "", 1115 text => "",
1040 align => -1, 1116 align => -1,
1041 valign => -1, 1117 valign => -1,
1042 padding => 2, 1118 padding => 2,
1043 layout => new CFClient::Layout, 1119 layout => new CFClient::Layout,
1120 can_events => 0,
1044 %arg 1121 %arg
1045 ); 1122 );
1046 1123
1047 if (exists $self->{template}) { 1124 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout; 1125 my $layout = new CFClient::Layout;
1070 my ($self, $text) = @_; 1147 my ($self, $text) = @_;
1071 1148
1072 $self->{layout}->set_text ($text); 1149 $self->{layout}->set_text ($text);
1073 1150
1074 delete $self->{texture}; 1151 delete $self->{texture};
1152 $self->check_size;
1075 $self->update; 1153 $self->update;
1076} 1154}
1077 1155
1078sub set_markup { 1156sub set_markup {
1079 my ($self, $markup) = @_; 1157 my ($self, $markup) = @_;
1080 1158
1081 $self->{layout}->set_markup ($markup); 1159 $self->{layout}->set_markup ($markup);
1082 1160
1083 delete $self->{texture}; 1161 delete $self->{texture};
1162 $self->check_size;
1084 $self->update; 1163 $self->update;
1085} 1164}
1086 1165
1087sub size_request { 1166sub size_request {
1088 my ($self) = @_; 1167 my ($self) = @_;
1089 1168
1090 $self->{layout}->set_width; 1169 $self->{layout}->set_font ($self->{font}) if $self->{font};
1170 $self->{layout}->set_width ($self->{max_w} || -1);
1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1171 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1092 1172
1093 my ($w, $h) = $self->{layout}->size; 1173 my ($w, $h) = $self->{layout}->size;
1094 1174
1095 if (exists $self->{template}) { 1175 if (exists $self->{template}) {
1176 $self->{template}->set_font ($self->{font}) if $self->{font};
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1177 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097 1178
1098 my ($w2, $h2) = $self->{template}->size; 1179 my ($w2, $h2) = $self->{template}->size;
1099 1180
1100 $w = List::Util::max $w, $w2; 1181 $w = List::Util::max $w, $w2;
1111 my ($self, $w, $h) = @_; 1192 my ($self, $w, $h) = @_;
1112 1193
1113 delete $self->{texture}; 1194 delete $self->{texture};
1114} 1195}
1115 1196
1197sub set_fontsize {
1198 my ($self, $fontsize) = @_;
1199
1200 $self->{fontsize} = $fontsize;
1201 delete $self->{texture};
1202 $self->check_size;
1203 $self->update;
1204}
1205
1116sub _draw { 1206sub _draw {
1117 my ($self) = @_; 1207 my ($self) = @_;
1118 1208
1119 my $tex = $self->{texture} ||= do { 1209 my $tex = $self->{texture} ||= do {
1210 $self->{layout}->set_font ($self->{font}) if $self->{font};
1120 $self->{layout}->set_width ($self->{w}); 1211 $self->{layout}->set_width ($self->{w});
1121 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1212 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1122 new_from_layout CFClient::Texture $self->{layout} 1213 new_from_layout CFClient::Texture $self->{layout}
1123 }; 1214 };
1124 1215
1164 active_bg => [1, 1, 1, 0.5], 1255 active_bg => [1, 1, 1, 0.5],
1165 active_fg => [0, 0, 0], 1256 active_fg => [0, 0, 0],
1166 can_hover => 1, 1257 can_hover => 1,
1167 can_focus => 1, 1258 can_focus => 1,
1168 valign => 0, 1259 valign => 0,
1260 can_events => 1,
1169 @_ 1261 @_
1170 ) 1262 )
1171} 1263}
1172 1264
1173sub _set_text { 1265sub _set_text {
1201} 1293}
1202 1294
1203sub size_allocate { 1295sub size_allocate {
1204 my ($self, $w, $h) = @_; 1296 my ($self, $w, $h) = @_;
1205 1297
1206 $self->_set_text ($self->{text}); 1298 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1207} 1299}
1208 1300
1209sub set_text { 1301sub set_text {
1210 my ($self, $text) = @_; 1302 my ($self, $text) = @_;
1211 1303
1344our @ISA = CFClient::UI::Label::; 1436our @ISA = CFClient::UI::Label::;
1345 1437
1346use CFClient::OpenGL; 1438use CFClient::OpenGL;
1347 1439
1348my @tex = 1440my @tex =
1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1441 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1350 qw(b1_button_active.png); 1442 qw(b1_button_active.png);
1351 1443
1352sub new { 1444sub new {
1353 my $class = shift; 1445 my $class = shift;
1354 1446
1358 bg => [1, 1, 1, 0.2], 1450 bg => [1, 1, 1, 0.2],
1359 active_fg => [0, 0, 1], 1451 active_fg => [0, 0, 1],
1360 can_hover => 1, 1452 can_hover => 1,
1361 align => 0, 1453 align => 0,
1362 valign => 0, 1454 valign => 0,
1455 can_events => 1,
1363 @_ 1456 @_
1364 ) 1457 )
1365} 1458}
1366 1459
1367sub button_up { 1460sub button_up {
1401package CFClient::UI::CheckBox; 1494package CFClient::UI::CheckBox;
1402 1495
1403our @ISA = CFClient::UI::DrawBG::; 1496our @ISA = CFClient::UI::DrawBG::;
1404 1497
1405my @tex = 1498my @tex =
1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1499 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1407 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1500 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1408 1501
1409use CFClient::OpenGL; 1502use CFClient::OpenGL;
1410 1503
1411sub new { 1504sub new {
1460 glDisable GL_BLEND; 1553 glDisable GL_BLEND;
1461} 1554}
1462 1555
1463############################################################################# 1556#############################################################################
1464 1557
1558package CFClient::UI::Image;
1559
1560our @ISA = CFClient::UI::Base::;
1561
1562use CFClient::OpenGL;
1563use Carp qw/confess/;
1564
1565our %loaded_images;
1566
1567sub new {
1568 my $class = shift;
1569
1570 my $self = $class->SUPER::new (can_events => 0, @_);
1571
1572 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1573
1574 $loaded_images{$self->{image}} ||=
1575 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1576
1577 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1578
1579 Scalar::Util::weaken $loaded_images{$self->{image}};
1580
1581 $self->{aspect} = $tex->{w} / $tex->{h};
1582
1583 $self
1584}
1585
1586sub size_request {
1587 my ($self) = @_;
1588
1589 ($self->{tex}->{w}, $self->{tex}->{h})
1590}
1591
1592sub _draw {
1593 my ($self) = @_;
1594
1595 my $tex = $self->{tex};
1596
1597 my ($w, $h) = ($self->{w}, $self->{h});
1598
1599 if ($self->{rot90}) {
1600 glRotate 90, 0, 0, 1;
1601 glTranslate 0, -$self->{w}, 0;
1602
1603 ($w, $h) = ($h, $w);
1604 }
1605
1606 glEnable GL_BLEND;
1607 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1608 glEnable GL_TEXTURE_2D;
1609 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1610
1611 $tex->draw_quad (0, 0, $w, $h);
1612
1613 glDisable GL_BLEND;
1614 glDisable GL_TEXTURE_2D;
1615}
1616
1617#############################################################################
1618
1465package CFClient::UI::VGauge; 1619package CFClient::UI::VGauge;
1466 1620
1467our @ISA = CFClient::UI::Base::; 1621our @ISA = CFClient::UI::Base::;
1622
1623use List::Util qw(min max);
1468 1624
1469use CFClient::OpenGL; 1625use CFClient::OpenGL;
1470 1626
1471my %tex = ( 1627my %tex = (
1472 food => [ 1628 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1630 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ], 1631 ],
1476 grace => [ 1632 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1634 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1479 ], 1635 ],
1480 hp => [ 1636 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1638 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ], 1639 ],
1484 mana => [ 1640 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1641 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1642 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1487 ], 1643 ],
1488); 1644);
1489 1645
1490# eg. VGauge->new (gauge => 'food'), default gauge: food 1646# eg. VGauge->new (gauge => 'food'), default gauge: food
1491sub new { 1647sub new {
1529 1685
1530sub _draw { 1686sub _draw {
1531 my ($self) = @_; 1687 my ($self) = @_;
1532 1688
1533 my $tex = $tex{$self->{type}}; 1689 my $tex = $tex{$self->{type}};
1690 my ($t1, $t2, $t3) = @$tex;
1534 1691
1535 my ($w, $h) = ($self->{w}, $self->{h}); 1692 my ($w, $h) = ($self->{w}, $self->{h});
1536 1693
1537 if ($self->{vertical}) { 1694 if ($self->{vertical}) {
1538 glRotate 90, 0, 0, 1; 1695 glRotate 90, 0, 0, 1;
1540 1697
1541 ($w, $h) = ($h, $w); 1698 ($w, $h) = ($h, $w);
1542 } 1699 }
1543 1700
1544 my $ycut = $self->{val} / ($self->{max_val} || 1); 1701 my $ycut = $self->{val} / ($self->{max_val} || 1);
1545 $ycut = 1 if $self->{val} > $self->{max_val};
1546 1702
1547 my $t1 = $tex->[0]; 1703 my $ycut1 = max 0, min 1, $ycut;
1548 my $t2 = $tex->[1]; 1704 my $ycut2 = max 0, min 1, $ycut - 1;
1705
1706 my $h1 = $self->{h} * (1 - $ycut1);
1707 my $h2 = $self->{h} * (1 - $ycut2);
1549 1708
1550 glEnable GL_BLEND; 1709 glEnable GL_BLEND;
1551 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1552 glEnable GL_TEXTURE_2D; 1711 glEnable GL_TEXTURE_2D;
1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1712 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1554 1713
1555 my $h1 = $self->{h} - $ycut * $self->{h};
1556 my $h2 = $ycut * $self->{h};
1557
1558 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1714 glBindTexture GL_TEXTURE_2D, $t1->{name};
1559 glBegin GL_QUADS; 1715 glBegin GL_QUADS;
1560 glTexCoord 0 , 0; glVertex 0 , 0; 1716 glTexCoord 0 , 0; glVertex 0 , 0;
1561 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1717 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1562 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1718 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1563 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1719 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1564 glEnd; 1720 glEnd;
1565 1721
1722 my $ycut1 = List::Util::min 1, $ycut;
1566 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1723 glBindTexture GL_TEXTURE_2D, $t2->{name};
1567 glBegin GL_QUADS; 1724 glBegin GL_QUADS;
1568 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1725 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1569 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1726 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1570 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1727 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1571 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1728 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1572 glEnd; 1729 glEnd;
1730
1731 if ($t3) {
1732 glBindTexture GL_TEXTURE_2D, $t3->{name};
1733 glBegin GL_QUADS;
1734 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1735 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1736 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1737 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1738 glEnd;
1739 }
1573 1740
1574 glDisable GL_BLEND; 1741 glDisable GL_BLEND;
1575 glDisable GL_TEXTURE_2D; 1742 glDisable GL_TEXTURE_2D;
1576} 1743}
1577 1744
1580package CFClient::UI::Gauge; 1747package CFClient::UI::Gauge;
1581 1748
1582our @ISA = CFClient::UI::VBox::; 1749our @ISA = CFClient::UI::VBox::;
1583 1750
1584sub new { 1751sub new {
1585 my ($class, %arg) = shift; 1752 my ($class, %arg) = @_;
1586 1753
1587 my $self = $class->SUPER::new ( 1754 my $self = $class->SUPER::new (
1588 @_, 1755 tooltip => $arg{type},
1756 can_events => 1,
1757 can_hover => 1,
1758 %arg,
1589 ); 1759 );
1590 1760
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1761 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1592 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1762 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1593 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1763 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1594 1764
1595 $self 1765 $self
1766}
1767
1768sub set_fontsize {
1769 my ($self, $fsize) = @_;
1770
1771 $self->{value}->set_fontsize ($fsize);
1772 $self->{max} ->set_fontsize ($fsize);
1596} 1773}
1597 1774
1598sub set_value { 1775sub set_value {
1599 my ($self, $val, $max) = @_; 1776 my ($self, $val, $max) = @_;
1600 1777
1758sub new { 1935sub new {
1759 my $class = shift; 1936 my $class = shift;
1760 1937
1761 my $self = $class->SUPER::new ( 1938 my $self = $class->SUPER::new (
1762 fontsize => 1, 1939 fontsize => 1,
1940 #font => default_font
1763 @_, 1941 @_,
1764 1942
1765 layout => (new CFClient::Layout), 1943 layout => (new CFClient::Layout),
1766 par => [], 1944 par => [],
1767 height => 0, 1945 height => 0,
1807sub size_allocate { 1985sub size_allocate {
1808 my ($self, $w, $h) = @_; 1986 my ($self, $w, $h) = @_;
1809 1987
1810 $self->SUPER::size_allocate ($w, $h); 1988 $self->SUPER::size_allocate ($w, $h);
1811 1989
1990 $self->{layout}->set_font ($self->{font}) if $self->{font};
1812 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1991 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1813 $self->{layout}->set_width ($self->{children}[0]{w}); 1992 $self->{layout}->set_width ($self->{children}[0]{w});
1814 1993
1815 $self->reflow; 1994 $self->reflow;
1816} 1995}
1868 my $y1 = $top + $self->{h}; 2047 my $y1 = $top + $self->{h};
1869 2048
1870 my $y = 0; 2049 my $y = 0;
1871 2050
1872 my $layout = $self->{layout}; 2051 my $layout = $self->{layout};
2052
2053 $layout->set_font ($self->{font}) if $self->{font};
1873 2054
1874 for my $par (@{$self->{par}}) { 2055 for my $par (@{$self->{par}}) {
1875 my $h = $par->[0]; 2056 my $h = $par->[0];
1876 2057
1877 if ($y0 < $y + $h && $y < $y1) { 2058 if ($y0 < $y + $h && $y < $y1) {
1957 my $class = shift; 2138 my $class = shift;
1958 2139
1959 my $self = $class->SUPER::new ( 2140 my $self = $class->SUPER::new (
1960 state => 0, 2141 state => 0,
1961 connect_activate => \&toggle_flopper, 2142 connect_activate => \&toggle_flopper,
2143 can_events => 1,
1962 @_ 2144 @_
1963 ); 2145 );
1964 2146
1965 if ($self->{state}) { 2147 if ($self->{state}) {
1966 $self->{state} = 0; 2148 $self->{state} = 0;
1986 $self->emit (changed => $self->{state}); 2168 $self->emit (changed => $self->{state});
1987} 2169}
1988 2170
1989############################################################################# 2171#############################################################################
1990 2172
2173package CFClient::UI::Tooltip;
2174
2175our @ISA = CFClient::UI::Bin::;
2176
2177use CFClient::OpenGL;
2178
2179sub new {
2180 my $class = shift;
2181
2182 $class->SUPER::new (
2183 @_,
2184 can_events => 0,
2185 )
2186}
2187
2188sub set_text {
2189 my ($self, $text) = @_;
2190
2191 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2192 $self->{label}->set_text ($text);
2193 $self->add ($self->{label});
2194}
2195
2196sub size_request {
2197 my ($self) = @_;
2198
2199 $self->child->set_max_size ($::WIDTH * 0.3);
2200
2201 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2202
2203 ($w + 4, $h + 4)
2204}
2205
2206sub _draw {
2207 my ($self) = @_;
2208
2209 glPushMatrix;
2210 glTranslate 0.375, 0.375;
2211
2212 my ($w, $h) = @$self{qw(w h)};
2213
2214 glColor 1, 0.8, 0.4;
2215 glBegin GL_QUADS;
2216 glVertex 0 , 0;
2217 glVertex 0 , $h;
2218 glVertex $w, $h;
2219 glVertex $w, 0;
2220 glEnd;
2221
2222 glColor 0, 0, 0;
2223 glBegin GL_LINE_LOOP;
2224 glVertex 0 , 0;
2225 glVertex 0 , $h;
2226 glVertex $w, $h;
2227 glVertex $w, 0;
2228 glEnd;
2229
2230 glPopMatrix;
2231
2232 glTranslate 2, 2;
2233 $self->SUPER::_draw;
2234}
2235
2236#############################################################################
2237
1991package CFClient::UI::Root; 2238package CFClient::UI::Root;
1992 2239
1993our @ISA = CFClient::UI::Container::; 2240our @ISA = CFClient::UI::Container::;
1994 2241
1995use CFClient::OpenGL; 2242use CFClient::OpenGL;
1996 2243
1997sub check_size { 2244sub check_size {
1998 my ($self) = @_; 2245 my ($self) = @_;
1999 2246
2000 $self->configure (0, 0, $::WITH, $::HEIGHT); 2247 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2001} 2248}
2002 2249
2003sub size_request { 2250sub size_request {
2004 ($::WIDTH, $::HEIGHT) 2251 ($::WIDTH, $::HEIGHT)
2005} 2252}
2007sub configure { 2254sub configure {
2008 my ($self, $x, $y, $w, $h) = @_; 2255 my ($self, $x, $y, $w, $h) = @_;
2009 2256
2010 $self->SUPER::configure ($x, $y, $w, $h); 2257 $self->SUPER::configure ($x, $y, $w, $h);
2011 2258
2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2013 for @{$self->{children}}; 2259 for my $child (@{$self->{children}}) {
2260 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2261
2262 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2263 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2264 $child->configure ($X, $Y, $W,$H);
2265 }
2014} 2266}
2015 2267
2016sub _topleft { 2268sub _topleft {
2017 my ($self, $x, $y) = @_; 2269 my ($self, $x, $y) = @_;
2018 2270
2026 ::refresh (); 2278 ::refresh ();
2027} 2279}
2028 2280
2029sub add { 2281sub add {
2030 my ($self, $child) = @_; 2282 my ($self, $child) = @_;
2283
2284 # integerize window positions
2285 $child->{x} = int $child->{x};
2286 $child->{y} = int $child->{y};
2031 2287
2032 $self->SUPER::add ($child); 2288 $self->SUPER::add ($child);
2033} 2289}
2034 2290
2035sub on_refresh { 2291sub on_refresh {
2061############################################################################# 2317#############################################################################
2062 2318
2063package CFClient::UI; 2319package CFClient::UI;
2064 2320
2065$ROOT = new CFClient::UI::Root; 2321$ROOT = new CFClient::UI::Root;
2322$TOOLTIP = new CFClient::UI::Tooltip;
2066 2323
20671 23241
2068 2325

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines