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.157 by root, Sun Apr 23 21:47:32 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
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_(.*)$/) {
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
152 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 186 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 187 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154 188
155 # use alignment to adjust x, y 189 # use alignment to adjust x, y
156 190
157 $x += ($w - $w2) * 0.5; 191 $x += int +($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5; 192 $y += int +($h - $h2) * 0.5;
159 193
160 ($w, $h) = ($w2, $h2); 194 ($w, $h) = ($w2, $h2);
161 } 195 }
162 196
163 if ($self->{x} != $x || $self->{y} != $y) { 197 if ($self->{x} != $x || $self->{y} != $y) {
175 } 209 }
176} 210}
177 211
178sub size_allocate { 212sub size_allocate {
179 # nothing to be done 213 # nothing to be done
214}
215
216sub set_max_size {
217 my ($self, $w, $h) = @_;
218
219 delete $self->{max_w}; $self->{max_w} = $w if $w;
220 delete $self->{max_h}; $self->{max_h} = $h if $h;
180} 221}
181 222
182# return top left coordinates 223# return top left coordinates
183sub _topleft { 224sub _topleft {
184 my ($self, $x, $y) = @_; 225 my ($self, $x, $y) = @_;
278} 319}
279 320
280sub find_widget { 321sub find_widget {
281 my ($self, $x, $y) = @_; 322 my ($self, $x, $y) = @_;
282 323
324 return () unless $self->{can_events};
325
283 return $self 326 return $self
284 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 327 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
285 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 328 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
286 329
287 () 330 ()
294} 337}
295 338
296sub check_size { 339sub check_size {
297 my ($self) = @_; 340 my ($self) = @_;
298 341
342 return unless $self->{parent};
343
299 my ($w, $h) = $self->size_request; 344 my ($w, $h) = $self->size_request;
300 345
301 if ($w != $self->{req_w} || $h != $self->{req_h}) { 346 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w; 347 $self->{req_w} = $w;
303 $self->{req_h} = $h; 348 $self->{req_h} = $h;
304 349
305 $self->{parent}->check_size 350 $self->{parent}->check_size;
306 if $self->{parent};
307 } 351 }
308} 352}
309 353
310sub update { 354sub update {
311 my ($self) = @_; 355 my ($self) = @_;
378 422
379package CFClient::UI::Empty; 423package CFClient::UI::Empty;
380 424
381our @ISA = CFClient::UI::Base::; 425our @ISA = CFClient::UI::Base::;
382 426
427sub new {
428 my ($class, %arg) = @_;
429 $class->SUPER::new (can_events => 0, %arg);
430}
431
383sub size_request { 432sub size_request {
384 (0, 0) 433 (0, 0)
385} 434}
386 435
387sub draw { } 436sub draw { }
395sub new { 444sub new {
396 my ($class, %arg) = @_; 445 my ($class, %arg) = @_;
397 446
398 my $children = delete $arg{children} || []; 447 my $children = delete $arg{children} || [];
399 448
400 my $self = $class->SUPER::new (children => [], %arg); 449 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
401 $self->add ($_) for @$children; 450 $self->add ($_) for @$children;
402 451
403 $self 452 $self
404} 453}
405 454
424 delete $child->{parent}; 473 delete $child->{parent};
425 474
426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 475 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
427 476
428 $self->check_size; 477 $self->check_size;
478 $self->update;
429} 479}
430 480
431sub find_widget { 481sub find_widget {
432 my ($self, $x, $y) = @_; 482 my ($self, $x, $y) = @_;
433 483
561sub new { die } 611sub new { die }
562 612
563sub size_request { 613sub size_request {
564 my ($self) = @_; 614 my ($self) = @_;
565 615
566 @$self{qw(child_w child_h)} = $self->child->size_request; 616 @$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)}); 617 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568 618
569 @$self{qw(child_w child_h)} 619 @$self{qw(child_w child_h)}
570} 620}
571 621
581package CFClient::UI::Frame; 631package CFClient::UI::Frame;
582 632
583our @ISA = CFClient::UI::Bin::; 633our @ISA = CFClient::UI::Bin::;
584 634
585use CFClient::OpenGL; 635use 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 636
633sub new { 637sub new {
634 my $class = shift; 638 my $class = shift;
635
636 # TODO: user_x, user_y, overwrite moveto?
637 639
638 my $self = $class->SUPER::new ( 640 my $self = $class->SUPER::new (
639 bg => [1, 1, 1, 1], 641 bg => [1, 1, 1, 1],
640 border_bg => [1, 1, 1, 1], 642 border_bg => [1, 1, 1, 1],
641 border => 0.8, 643 border => 0.8,
642 @_ 644 @_
643 ); 645 );
644 646
647 $self
648}
649
650sub set_size {
651 my ($self, $w, $h) = @_;
652 $self->{req_w} = $w;
653 $self->{req_h} = $h;
654 $self->check_size;
655}
656
657sub size_request {
658 my ($self) = @_;
659 ($self->{req_w}, $self->{req_h})
660}
661
662sub size_allocate {
663 my ($self, $w, $h) = @_;
664 $self->{w} = $w;
665 $self->{h} = $h;
666 $self->child->configure (0, 0, $w, $h);
667}
668
669sub _draw {
670 my ($self) = @_;
671
672 my ($w, $h) = ($self->{w}, $self->{h});
673
674 glEnable GL_BLEND;
675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
676 glEnable GL_TEXTURE_2D;
677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
678
679# glBegin GL_QUADS;
680# glColor 0, 0, 0, 0;
681# glVertex 0 , 0;
682# glVertex 0 , $h;
683# glVertex $w, $h;
684# glVertex $w, 0;
685# glEnd;
686
687
688 $self->child->draw;
689 glDisable GL_BLEND;
690 glDisable GL_TEXTURE_2D;
691}
692
693#############################################################################
694
695package CFClient::UI::FancyFrame;
696
697our @ISA = CFClient::UI::Bin::;
698
699use CFClient::OpenGL;
700
701my @tex =
702 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
703 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
704
705sub new {
706 my $class = shift;
707
708 # TODO: user_x, user_y, overwrite moveto?
709
710 my $self = $class->SUPER::new (
711 bg => [1, 1, 1, 1],
712 border_bg => [1, 1, 1, 1],
713 border => 0.8,
714 can_events => 1,
715 @_
716 );
717
645 $self->{title} &&= new CFClient::UI::Label 718 $self->{title} &&= new CFClient::UI::Label
646 align => 0, 719 align => 0,
647 valign => 1, 720 valign => 1,
648 text => $self->{title}, 721 text => $self->{title},
649 fontsize => 1; 722 fontsize => 1;
815 or next; 888 or next;
816 889
817 for my $x (0 .. $#$row) { 890 for my $x (0 .. $#$row) {
818 my $widget = $row->[$x] 891 my $widget = $row->[$x]
819 or next; 892 or next;
820 my ($w, $h) = $widget->size_request; 893 my ($w, $h) = @$widget{qw(req_w req_h)};
821 894
822 $w[$x] = max $w[$x], $w; 895 $w[$x] = max $w[$x], $w;
823 $h[$y] = max $h[$y], $h; 896 $h[$y] = max $h[$y], $h;
824 } 897 }
825 } 898 }
930 1003
931 ($h, $w) = ($w, $h); 1004 ($h, $w) = ($w, $h);
932 1005
933 my $children = $self->{children}; 1006 my $children = $self->{children};
934 1007
935 my @h = map +($_->size_request)[0], @$children; 1008 my @h = map $_->{req_w}, @$children;
936 1009
937 my $req_h = List::Util::sum @h; 1010 my $req_h = List::Util::sum @h;
938 1011
939 if ($req_h > $h) { 1012 if ($req_h > $h) {
940 # ah well, not enough space 1013 # ah well, not enough space
988sub size_allocate { 1061sub size_allocate {
989 my ($self, $w, $h) = @_; 1062 my ($self, $w, $h) = @_;
990 1063
991 my $children = $self->{children}; 1064 my $children = $self->{children};
992 1065
993 my @h = map +($_->size_request)[1], @$children; 1066 my @h = map $_->{req_h}, @$children;
994 1067
995 my $req_h = List::Util::sum @h; 1068 my $req_h = List::Util::sum @h;
996 1069
997 if ($req_h > $h) { 1070 if ($req_h > $h) {
998 # ah well, not enough space 1071 # ah well, not enough space
1033sub new { 1106sub new {
1034 my ($class, %arg) = @_; 1107 my ($class, %arg) = @_;
1035 1108
1036 my $self = $class->SUPER::new ( 1109 my $self = $class->SUPER::new (
1037 fg => [1, 1, 1], 1110 fg => [1, 1, 1],
1111 #font => default_font
1038 fontsize => 1, 1112 fontsize => 1,
1039 text => "", 1113 text => "",
1040 align => -1, 1114 align => -1,
1041 valign => -1, 1115 valign => -1,
1042 padding => 2, 1116 padding => 2,
1043 layout => new CFClient::Layout, 1117 layout => new CFClient::Layout,
1118 can_events => 0,
1044 %arg 1119 %arg
1045 ); 1120 );
1046 1121
1047 if (exists $self->{template}) { 1122 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout; 1123 my $layout = new CFClient::Layout;
1070 my ($self, $text) = @_; 1145 my ($self, $text) = @_;
1071 1146
1072 $self->{layout}->set_text ($text); 1147 $self->{layout}->set_text ($text);
1073 1148
1074 delete $self->{texture}; 1149 delete $self->{texture};
1150 $self->check_size;
1075 $self->update; 1151 $self->update;
1076} 1152}
1077 1153
1078sub set_markup { 1154sub set_markup {
1079 my ($self, $markup) = @_; 1155 my ($self, $markup) = @_;
1080 1156
1081 $self->{layout}->set_markup ($markup); 1157 $self->{layout}->set_markup ($markup);
1082 1158
1083 delete $self->{texture}; 1159 delete $self->{texture};
1160 $self->check_size;
1084 $self->update; 1161 $self->update;
1085} 1162}
1086 1163
1087sub size_request { 1164sub size_request {
1088 my ($self) = @_; 1165 my ($self) = @_;
1089 1166
1090 $self->{layout}->set_width; 1167 $self->{layout}->set_font ($self->{font}) if $self->{font};
1168 $self->{layout}->set_width ($self->{max_w} || -1);
1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1169 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1092 1170
1093 my ($w, $h) = $self->{layout}->size; 1171 my ($w, $h) = $self->{layout}->size;
1094 1172
1095 if (exists $self->{template}) { 1173 if (exists $self->{template}) {
1174 $self->{template}->set_font ($self->{font}) if $self->{font};
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1175 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097 1176
1098 my ($w2, $h2) = $self->{template}->size; 1177 my ($w2, $h2) = $self->{template}->size;
1099 1178
1100 $w = List::Util::max $w, $w2; 1179 $w = List::Util::max $w, $w2;
1111 my ($self, $w, $h) = @_; 1190 my ($self, $w, $h) = @_;
1112 1191
1113 delete $self->{texture}; 1192 delete $self->{texture};
1114} 1193}
1115 1194
1195sub set_fontsize {
1196 my ($self, $fontsize) = @_;
1197
1198 $self->{fontsize} = $fontsize;
1199 delete $self->{texture};
1200 $self->check_size;
1201 $self->update;
1202}
1203
1116sub _draw { 1204sub _draw {
1117 my ($self) = @_; 1205 my ($self) = @_;
1118 1206
1119 my $tex = $self->{texture} ||= do { 1207 my $tex = $self->{texture} ||= do {
1208 $self->{layout}->set_font ($self->{font}) if $self->{font};
1120 $self->{layout}->set_width ($self->{w}); 1209 $self->{layout}->set_width ($self->{w});
1121 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1210 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1122 new_from_layout CFClient::Texture $self->{layout} 1211 new_from_layout CFClient::Texture $self->{layout}
1123 }; 1212 };
1124 1213
1164 active_bg => [1, 1, 1, 0.5], 1253 active_bg => [1, 1, 1, 0.5],
1165 active_fg => [0, 0, 0], 1254 active_fg => [0, 0, 0],
1166 can_hover => 1, 1255 can_hover => 1,
1167 can_focus => 1, 1256 can_focus => 1,
1168 valign => 0, 1257 valign => 0,
1258 can_events => 1,
1169 @_ 1259 @_
1170 ) 1260 )
1171} 1261}
1172 1262
1173sub _set_text { 1263sub _set_text {
1201} 1291}
1202 1292
1203sub size_allocate { 1293sub size_allocate {
1204 my ($self, $w, $h) = @_; 1294 my ($self, $w, $h) = @_;
1205 1295
1206 $self->_set_text ($self->{text}); 1296 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1207} 1297}
1208 1298
1209sub set_text { 1299sub set_text {
1210 my ($self, $text) = @_; 1300 my ($self, $text) = @_;
1211 1301
1344our @ISA = CFClient::UI::Label::; 1434our @ISA = CFClient::UI::Label::;
1345 1435
1346use CFClient::OpenGL; 1436use CFClient::OpenGL;
1347 1437
1348my @tex = 1438my @tex =
1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1439 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1350 qw(b1_button_active.png); 1440 qw(b1_button_active.png);
1351 1441
1352sub new { 1442sub new {
1353 my $class = shift; 1443 my $class = shift;
1354 1444
1358 bg => [1, 1, 1, 0.2], 1448 bg => [1, 1, 1, 0.2],
1359 active_fg => [0, 0, 1], 1449 active_fg => [0, 0, 1],
1360 can_hover => 1, 1450 can_hover => 1,
1361 align => 0, 1451 align => 0,
1362 valign => 0, 1452 valign => 0,
1453 can_events => 1,
1363 @_ 1454 @_
1364 ) 1455 )
1365} 1456}
1366 1457
1367sub button_up { 1458sub button_up {
1401package CFClient::UI::CheckBox; 1492package CFClient::UI::CheckBox;
1402 1493
1403our @ISA = CFClient::UI::DrawBG::; 1494our @ISA = CFClient::UI::DrawBG::;
1404 1495
1405my @tex = 1496my @tex =
1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1497 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1407 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1498 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1408 1499
1409use CFClient::OpenGL; 1500use CFClient::OpenGL;
1410 1501
1411sub new { 1502sub new {
1460 glDisable GL_BLEND; 1551 glDisable GL_BLEND;
1461} 1552}
1462 1553
1463############################################################################# 1554#############################################################################
1464 1555
1556package CFClient::UI::Image;
1557
1558our @ISA = CFClient::UI::Base::;
1559
1560use CFClient::OpenGL;
1561use Carp qw/confess/;
1562
1563our %loaded_images;
1564
1565sub new {
1566 my $class = shift;
1567
1568 my $self = $class->SUPER::new (can_events => 0, @_);
1569
1570 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1571
1572 $loaded_images{$self->{image}} ||=
1573 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1574
1575 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1576
1577 Scalar::Util::weaken $loaded_images{$self->{image}};
1578
1579 $self->{aspect} = $tex->{w} / $tex->{h};
1580
1581 $self
1582}
1583
1584sub size_request {
1585 my ($self) = @_;
1586
1587 ($self->{tex}->{w}, $self->{tex}->{h})
1588}
1589
1590sub _draw {
1591 my ($self) = @_;
1592
1593 my $tex = $self->{tex};
1594
1595 my ($w, $h) = ($self->{w}, $self->{h});
1596
1597 if ($self->{rot90}) {
1598 glRotate 90, 0, 0, 1;
1599 glTranslate 0, -$self->{w}, 0;
1600
1601 ($w, $h) = ($h, $w);
1602 }
1603
1604 glEnable GL_BLEND;
1605 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1606 glEnable GL_TEXTURE_2D;
1607 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1608
1609 $tex->draw_quad (0, 0, $w, $h);
1610
1611 glDisable GL_BLEND;
1612 glDisable GL_TEXTURE_2D;
1613}
1614
1615#############################################################################
1616
1465package CFClient::UI::VGauge; 1617package CFClient::UI::VGauge;
1466 1618
1467our @ISA = CFClient::UI::Base::; 1619our @ISA = CFClient::UI::Base::;
1468 1620
1469use CFClient::OpenGL; 1621use CFClient::OpenGL;
1470 1622
1471my %tex = ( 1623my %tex = (
1472 food => [ 1624 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1625 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1626 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ], 1627 ],
1476 grace => [ 1628 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1630 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1479 ], 1631 ],
1480 hp => [ 1632 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1634 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ], 1635 ],
1484 mana => [ 1636 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1638 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1487 ], 1639 ],
1488); 1640);
1489 1641
1490# eg. VGauge->new (gauge => 'food'), default gauge: food 1642# eg. VGauge->new (gauge => 'food'), default gauge: food
1580package CFClient::UI::Gauge; 1732package CFClient::UI::Gauge;
1581 1733
1582our @ISA = CFClient::UI::VBox::; 1734our @ISA = CFClient::UI::VBox::;
1583 1735
1584sub new { 1736sub new {
1585 my ($class, %arg) = shift; 1737 my ($class, %arg) = @_;
1586 1738
1587 my $self = $class->SUPER::new ( 1739 my $self = $class->SUPER::new (
1588 @_, 1740 tooltip => $arg{type},
1741 %arg,
1589 ); 1742 );
1590 1743
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1744 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1592 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1745 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1593 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1746 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1594 1747
1595 $self 1748 $self
1749}
1750
1751sub set_fontsize {
1752 my ($self, $fsize) = @_;
1753
1754 $self->{value}->set_fontsize ($fsize);
1755 $self->{max} ->set_fontsize ($fsize);
1596} 1756}
1597 1757
1598sub set_value { 1758sub set_value {
1599 my ($self, $val, $max) = @_; 1759 my ($self, $val, $max) = @_;
1600 1760
1758sub new { 1918sub new {
1759 my $class = shift; 1919 my $class = shift;
1760 1920
1761 my $self = $class->SUPER::new ( 1921 my $self = $class->SUPER::new (
1762 fontsize => 1, 1922 fontsize => 1,
1923 #font => default_font
1763 @_, 1924 @_,
1764 1925
1765 layout => (new CFClient::Layout), 1926 layout => (new CFClient::Layout),
1766 par => [], 1927 par => [],
1767 height => 0, 1928 height => 0,
1807sub size_allocate { 1968sub size_allocate {
1808 my ($self, $w, $h) = @_; 1969 my ($self, $w, $h) = @_;
1809 1970
1810 $self->SUPER::size_allocate ($w, $h); 1971 $self->SUPER::size_allocate ($w, $h);
1811 1972
1973 $self->{layout}->set_font ($self->{font}) if $self->{font};
1812 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1974 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1813 $self->{layout}->set_width ($self->{children}[0]{w}); 1975 $self->{layout}->set_width ($self->{children}[0]{w});
1814 1976
1815 $self->reflow; 1977 $self->reflow;
1816} 1978}
1868 my $y1 = $top + $self->{h}; 2030 my $y1 = $top + $self->{h};
1869 2031
1870 my $y = 0; 2032 my $y = 0;
1871 2033
1872 my $layout = $self->{layout}; 2034 my $layout = $self->{layout};
2035
2036 $layout->set_font ($self->{font}) if $self->{font};
1873 2037
1874 for my $par (@{$self->{par}}) { 2038 for my $par (@{$self->{par}}) {
1875 my $h = $par->[0]; 2039 my $h = $par->[0];
1876 2040
1877 if ($y0 < $y + $h && $y < $y1) { 2041 if ($y0 < $y + $h && $y < $y1) {
1957 my $class = shift; 2121 my $class = shift;
1958 2122
1959 my $self = $class->SUPER::new ( 2123 my $self = $class->SUPER::new (
1960 state => 0, 2124 state => 0,
1961 connect_activate => \&toggle_flopper, 2125 connect_activate => \&toggle_flopper,
2126 can_events => 1,
1962 @_ 2127 @_
1963 ); 2128 );
1964 2129
1965 if ($self->{state}) { 2130 if ($self->{state}) {
1966 $self->{state} = 0; 2131 $self->{state} = 0;
1986 $self->emit (changed => $self->{state}); 2151 $self->emit (changed => $self->{state});
1987} 2152}
1988 2153
1989############################################################################# 2154#############################################################################
1990 2155
2156package CFClient::UI::Tooltip;
2157
2158our @ISA = CFClient::UI::Bin::;
2159
2160use CFClient::OpenGL;
2161
2162sub new {
2163 my $class = shift;
2164
2165 $class->SUPER::new (
2166 @_,
2167 can_events => 0,
2168 )
2169}
2170
2171sub set_text {
2172 my ($self, $text) = @_;
2173
2174 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2175 $self->{label}->set_text ($text);
2176 $self->add ($self->{label});
2177}
2178
2179sub size_request {
2180 my ($self) = @_;
2181
2182 $self->child->set_max_size ($::WIDTH * 0.2);
2183
2184 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2185
2186 ($w + 4, $h + 4)
2187}
2188
2189sub _draw {
2190 my ($self) = @_;
2191
2192 glPushMatrix;
2193 glTranslate 0.375, 0.375;
2194
2195 my ($w, $h) = @$self{qw(w h)};
2196
2197 glColor 1, 0.8, 0.4;
2198 glBegin GL_QUADS;
2199 glVertex 0 , 0;
2200 glVertex 0 , $h;
2201 glVertex $w, $h;
2202 glVertex $w, 0;
2203 glEnd;
2204
2205 glColor 0, 0, 0;
2206 glBegin GL_LINE_LOOP;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glPopMatrix;
2214
2215 glTranslate 2, 2;
2216 $self->SUPER::_draw;
2217}
2218
2219#############################################################################
2220
1991package CFClient::UI::Root; 2221package CFClient::UI::Root;
1992 2222
1993our @ISA = CFClient::UI::Container::; 2223our @ISA = CFClient::UI::Container::;
1994 2224
1995use CFClient::OpenGL; 2225use CFClient::OpenGL;
1996 2226
1997sub check_size { 2227sub check_size {
1998 my ($self) = @_; 2228 my ($self) = @_;
1999 2229
2000 $self->configure (0, 0, $::WITH, $::HEIGHT); 2230 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2001} 2231}
2002 2232
2003sub size_request { 2233sub size_request {
2004 ($::WIDTH, $::HEIGHT) 2234 ($::WIDTH, $::HEIGHT)
2005} 2235}
2007sub configure { 2237sub configure {
2008 my ($self, $x, $y, $w, $h) = @_; 2238 my ($self, $x, $y, $w, $h) = @_;
2009 2239
2010 $self->SUPER::configure ($x, $y, $w, $h); 2240 $self->SUPER::configure ($x, $y, $w, $h);
2011 2241
2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2013 for @{$self->{children}}; 2242 for my $child (@{$self->{children}}) {
2243 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2244
2245 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2246 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2247 $child->configure ($X, $Y, $W,$H);
2248 }
2014} 2249}
2015 2250
2016sub _topleft { 2251sub _topleft {
2017 my ($self, $x, $y) = @_; 2252 my ($self, $x, $y) = @_;
2018 2253
2026 ::refresh (); 2261 ::refresh ();
2027} 2262}
2028 2263
2029sub add { 2264sub add {
2030 my ($self, $child) = @_; 2265 my ($self, $child) = @_;
2266
2267 # integerize window positions
2268 $child->{x} = int $child->{x};
2269 $child->{y} = int $child->{y};
2031 2270
2032 $self->SUPER::add ($child); 2271 $self->SUPER::add ($child);
2033} 2272}
2034 2273
2035sub on_refresh { 2274sub on_refresh {
2061############################################################################# 2300#############################################################################
2062 2301
2063package CFClient::UI; 2302package CFClient::UI;
2064 2303
2065$ROOT = new CFClient::UI::Root; 2304$ROOT = new CFClient::UI::Root;
2305$TOOLTIP = new CFClient::UI::Tooltip;
2066 2306
20671 23071
2068 2308

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines