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.158 by root, Mon Apr 24 02:41:48 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
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_(.*)$/) {
134 $self->{z} = $z if defined $z; 168 $self->{z} = $z if defined $z;
135 169
136 $self->update; 170 $self->update;
137} 171}
138 172
139sub needs_redraw { 173sub set_size {
140 0 174 my ($self, $w, $h) = @_;
175
176 $self->{user_w} = $w;
177 $self->{user_h} = $h;
178
179 $self->check_size;
141} 180}
142 181
143sub size_request { 182sub size_request {
144 require Carp; 183 require Carp;
145 Carp::confess "size_request is abtract"; 184 Carp::confess "size_request is abstract";
146} 185}
147 186
148sub configure { 187sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 188 my ($self, $x, $y, $w, $h) = @_;
150 189
152 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 191 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 192 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154 193
155 # use alignment to adjust x, y 194 # use alignment to adjust x, y
156 195
157 $x += ($w - $w2) * 0.5; 196 $x += int +($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5; 197 $y += int +($h - $h2) * 0.5;
159 198
160 ($w, $h) = ($w2, $h2); 199 ($w, $h) = ($w2, $h2);
161 } 200 }
162 201
163 if ($self->{x} != $x || $self->{y} != $y) { 202 if ($self->{x} != $x || $self->{y} != $y) {
175 } 214 }
176} 215}
177 216
178sub size_allocate { 217sub size_allocate {
179 # nothing to be done 218 # nothing to be done
219}
220
221sub set_max_size {
222 my ($self, $w, $h) = @_;
223
224 delete $self->{max_w}; $self->{max_w} = $w if $w;
225 delete $self->{max_h}; $self->{max_h} = $h if $h;
180} 226}
181 227
182# return top left coordinates 228# return top left coordinates
183sub _topleft { 229sub _topleft {
184 my ($self, $x, $y) = @_; 230 my ($self, $x, $y) = @_;
278} 324}
279 325
280sub find_widget { 326sub find_widget {
281 my ($self, $x, $y) = @_; 327 my ($self, $x, $y) = @_;
282 328
329 return () unless $self->{can_events};
330
283 return $self 331 return $self
284 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 332 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
285 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 333 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
286 334
287 () 335 ()
294} 342}
295 343
296sub check_size { 344sub check_size {
297 my ($self) = @_; 345 my ($self) = @_;
298 346
299 my ($w, $h) = $self->size_request; 347 $self->{parent}
348 or return 1;
300 349
350 my ($w, $h) = $self->{user_w} && $self->{user_h}
351 ? @$self{qw(user_w user_h)}
352 : $self->size_request;
353
301 if ($w != $self->{req_w} || $h != $self->{req_h}) { 354 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w; 355 $self->{req_w} = $w;
303 $self->{req_h} = $h; 356 $self->{req_h} = $h;
304 357
305 $self->{parent}->check_size 358 $self->{parent}->check_size
306 if $self->{parent}; 359 or $self->size_allocate (
360 (List::Util::max $self->{w}, $w),
361 (List::Util::max $self->{h}, $h),
362 );
363
364 1
365 } else {
366 0
307 } 367 }
308} 368}
309 369
310sub update { 370sub update {
311 my ($self) = @_; 371 my ($self) = @_;
378 438
379package CFClient::UI::Empty; 439package CFClient::UI::Empty;
380 440
381our @ISA = CFClient::UI::Base::; 441our @ISA = CFClient::UI::Base::;
382 442
443sub new {
444 my ($class, %arg) = @_;
445 $class->SUPER::new (can_events => 0, %arg);
446}
447
383sub size_request { 448sub size_request {
384 (0, 0) 449 (0, 0)
385} 450}
386 451
387sub draw { } 452sub draw { }
395sub new { 460sub new {
396 my ($class, %arg) = @_; 461 my ($class, %arg) = @_;
397 462
398 my $children = delete $arg{children} || []; 463 my $children = delete $arg{children} || [];
399 464
400 my $self = $class->SUPER::new (children => [], %arg); 465 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
401 $self->add ($_) for @$children; 466 $self->add ($_) for @$children;
402 467
403 $self 468 $self
404} 469}
405 470
424 delete $child->{parent}; 489 delete $child->{parent};
425 490
426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 491 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
427 492
428 $self->check_size; 493 $self->check_size;
494 $self->update;
429} 495}
430 496
431sub find_widget { 497sub find_widget {
432 my ($self, $x, $y) = @_; 498 my ($self, $x, $y) = @_;
433 499
561sub new { die } 627sub new { die }
562 628
563sub size_request { 629sub size_request {
564 my ($self) = @_; 630 my ($self) = @_;
565 631
566 @$self{qw(child_w child_h)} = $self->child->size_request; 632 @$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)}); 633 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568 634
569 @$self{qw(child_w child_h)} 635 @$self{qw(child_w child_h)}
570} 636}
571 637
581package CFClient::UI::Frame; 647package CFClient::UI::Frame;
582 648
583our @ISA = CFClient::UI::Bin::; 649our @ISA = CFClient::UI::Bin::;
584 650
585use CFClient::OpenGL; 651use 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 652
633sub new { 653sub new {
634 my $class = shift; 654 my $class = shift;
635
636 # TODO: user_x, user_y, overwrite moveto?
637 655
638 my $self = $class->SUPER::new ( 656 my $self = $class->SUPER::new (
639 bg => [1, 1, 1, 1], 657 bg => [1, 1, 1, 1],
640 border_bg => [1, 1, 1, 1], 658 border_bg => [1, 1, 1, 1],
641 border => 0.8, 659 border => 0.8,
642 @_ 660 @_
643 ); 661 );
644 662
663 $self
664}
665
666sub _draw {
667 my ($self) = @_;
668
669 my ($w, $h) = ($self->{w}, $self->{h});
670
671 glEnable GL_BLEND;
672 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675
676# glBegin GL_QUADS;
677# glColor 0, 0, 0, 0;
678# glVertex 0 , 0;
679# glVertex 0 , $h;
680# glVertex $w, $h;
681# glVertex $w, 0;
682# glEnd;
683
684
685 $self->child->draw;
686 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D;
688}
689
690#############################################################################
691
692package CFClient::UI::FancyFrame;
693
694our @ISA = CFClient::UI::Bin::;
695
696use CFClient::OpenGL;
697
698my @tex =
699 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
700 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
701
702sub new {
703 my $class = shift;
704
705 # TODO: user_x, user_y, overwrite moveto?
706
707 my $self = $class->SUPER::new (
708 bg => [1, 1, 1, 1],
709 border_bg => [1, 1, 1, 1],
710 border => 0.8,
711 can_events => 1,
712 @_
713 );
714
645 $self->{title} &&= new CFClient::UI::Label 715 $self->{title} &&= new CFClient::UI::Label
646 align => 0, 716 align => 0,
647 valign => 1, 717 valign => 1,
648 text => $self->{title}, 718 text => $self->{title},
649 fontsize => 1; 719 fontsize => 1;
656} 726}
657 727
658sub size_request { 728sub size_request {
659 my ($self) = @_; 729 my ($self) = @_;
660 730
661 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
662
663 my ($w, $h) = $self->SUPER::size_request; 731 my ($w, $h) = $self->SUPER::size_request;
664 732
665 ( 733 (
666 $w + $self->border * 2, 734 $w + $self->border * 2,
667 $h + $self->border * 2, 735 $h + $self->border * 2,
815 or next; 883 or next;
816 884
817 for my $x (0 .. $#$row) { 885 for my $x (0 .. $#$row) {
818 my $widget = $row->[$x] 886 my $widget = $row->[$x]
819 or next; 887 or next;
820 my ($w, $h) = $widget->size_request; 888 my ($w, $h) = @$widget{qw(req_w req_h)};
821 889
822 $w[$x] = max $w[$x], $w; 890 $w[$x] = max $w[$x], $w;
823 $h[$y] = max $h[$y], $h; 891 $h[$y] = max $h[$y], $h;
824 } 892 }
825 } 893 }
930 998
931 ($h, $w) = ($w, $h); 999 ($h, $w) = ($w, $h);
932 1000
933 my $children = $self->{children}; 1001 my $children = $self->{children};
934 1002
935 my @h = map +($_->size_request)[0], @$children; 1003 my @h = map $_->{req_w}, @$children;
936 1004
937 my $req_h = List::Util::sum @h; 1005 my $req_h = List::Util::sum @h;
938 1006
939 if ($req_h > $h) { 1007 if ($req_h > $h) {
940 # ah well, not enough space 1008 # ah well, not enough space
988sub size_allocate { 1056sub size_allocate {
989 my ($self, $w, $h) = @_; 1057 my ($self, $w, $h) = @_;
990 1058
991 my $children = $self->{children}; 1059 my $children = $self->{children};
992 1060
993 my @h = map +($_->size_request)[1], @$children; 1061 my @h = map $_->{req_h}, @$children;
994 1062
995 my $req_h = List::Util::sum @h; 1063 my $req_h = List::Util::sum @h;
996 1064
997 if ($req_h > $h) { 1065 if ($req_h > $h) {
998 # ah well, not enough space 1066 # ah well, not enough space
1033sub new { 1101sub new {
1034 my ($class, %arg) = @_; 1102 my ($class, %arg) = @_;
1035 1103
1036 my $self = $class->SUPER::new ( 1104 my $self = $class->SUPER::new (
1037 fg => [1, 1, 1], 1105 fg => [1, 1, 1],
1106 #font => default_font
1038 fontsize => 1, 1107 fontsize => 1,
1039 text => "", 1108 text => "",
1040 align => -1, 1109 align => -1,
1041 valign => -1, 1110 valign => -1,
1042 padding => 2, 1111 padding => 2,
1043 layout => new CFClient::Layout, 1112 layout => new CFClient::Layout,
1113 can_events => 0,
1044 %arg 1114 %arg
1045 ); 1115 );
1046 1116
1047 if (exists $self->{template}) { 1117 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout; 1118 my $layout = new CFClient::Layout;
1070 my ($self, $text) = @_; 1140 my ($self, $text) = @_;
1071 1141
1072 $self->{layout}->set_text ($text); 1142 $self->{layout}->set_text ($text);
1073 1143
1074 delete $self->{texture}; 1144 delete $self->{texture};
1145 $self->check_size;
1075 $self->update; 1146 $self->update;
1076} 1147}
1077 1148
1078sub set_markup { 1149sub set_markup {
1079 my ($self, $markup) = @_; 1150 my ($self, $markup) = @_;
1080 1151
1081 $self->{layout}->set_markup ($markup); 1152 $self->{layout}->set_markup ($markup);
1082 1153
1083 delete $self->{texture}; 1154 delete $self->{texture};
1155 $self->check_size;
1084 $self->update; 1156 $self->update;
1085} 1157}
1086 1158
1087sub size_request { 1159sub size_request {
1088 my ($self) = @_; 1160 my ($self) = @_;
1089 1161
1090 $self->{layout}->set_width; 1162 $self->{layout}->set_font ($self->{font}) if $self->{font};
1163 $self->{layout}->set_width ($self->{max_w} || -1);
1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1164 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1092 1165
1093 my ($w, $h) = $self->{layout}->size; 1166 my ($w, $h) = $self->{layout}->size;
1094 1167
1095 if (exists $self->{template}) { 1168 if (exists $self->{template}) {
1169 $self->{template}->set_font ($self->{font}) if $self->{font};
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1170 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097 1171
1098 my ($w2, $h2) = $self->{template}->size; 1172 my ($w2, $h2) = $self->{template}->size;
1099 1173
1100 $w = List::Util::max $w, $w2; 1174 $w = List::Util::max $w, $w2;
1111 my ($self, $w, $h) = @_; 1185 my ($self, $w, $h) = @_;
1112 1186
1113 delete $self->{texture}; 1187 delete $self->{texture};
1114} 1188}
1115 1189
1190sub set_fontsize {
1191 my ($self, $fontsize) = @_;
1192
1193 $self->{fontsize} = $fontsize;
1194 delete $self->{texture};
1195 $self->check_size;
1196 $self->update;
1197}
1198
1116sub _draw { 1199sub _draw {
1117 my ($self) = @_; 1200 my ($self) = @_;
1118 1201
1119 my $tex = $self->{texture} ||= do { 1202 my $tex = $self->{texture} ||= do {
1203 $self->{layout}->set_font ($self->{font}) if $self->{font};
1120 $self->{layout}->set_width ($self->{w}); 1204 $self->{layout}->set_width ($self->{w});
1121 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1205 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1122 new_from_layout CFClient::Texture $self->{layout} 1206 new_from_layout CFClient::Texture $self->{layout}
1123 }; 1207 };
1124 1208
1164 active_bg => [1, 1, 1, 0.5], 1248 active_bg => [1, 1, 1, 0.5],
1165 active_fg => [0, 0, 0], 1249 active_fg => [0, 0, 0],
1166 can_hover => 1, 1250 can_hover => 1,
1167 can_focus => 1, 1251 can_focus => 1,
1168 valign => 0, 1252 valign => 0,
1253 can_events => 1,
1169 @_ 1254 @_
1170 ) 1255 )
1171} 1256}
1172 1257
1173sub _set_text { 1258sub _set_text {
1201} 1286}
1202 1287
1203sub size_allocate { 1288sub size_allocate {
1204 my ($self, $w, $h) = @_; 1289 my ($self, $w, $h) = @_;
1205 1290
1206 $self->_set_text ($self->{text}); 1291 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1207} 1292}
1208 1293
1209sub set_text { 1294sub set_text {
1210 my ($self, $text) = @_; 1295 my ($self, $text) = @_;
1211 1296
1344our @ISA = CFClient::UI::Label::; 1429our @ISA = CFClient::UI::Label::;
1345 1430
1346use CFClient::OpenGL; 1431use CFClient::OpenGL;
1347 1432
1348my @tex = 1433my @tex =
1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1434 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1350 qw(b1_button_active.png); 1435 qw(b1_button_active.png);
1351 1436
1352sub new { 1437sub new {
1353 my $class = shift; 1438 my $class = shift;
1354 1439
1358 bg => [1, 1, 1, 0.2], 1443 bg => [1, 1, 1, 0.2],
1359 active_fg => [0, 0, 1], 1444 active_fg => [0, 0, 1],
1360 can_hover => 1, 1445 can_hover => 1,
1361 align => 0, 1446 align => 0,
1362 valign => 0, 1447 valign => 0,
1448 can_events => 1,
1363 @_ 1449 @_
1364 ) 1450 )
1365} 1451}
1366 1452
1367sub button_up { 1453sub button_up {
1401package CFClient::UI::CheckBox; 1487package CFClient::UI::CheckBox;
1402 1488
1403our @ISA = CFClient::UI::DrawBG::; 1489our @ISA = CFClient::UI::DrawBG::;
1404 1490
1405my @tex = 1491my @tex =
1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1492 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1407 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1493 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1408 1494
1409use CFClient::OpenGL; 1495use CFClient::OpenGL;
1410 1496
1411sub new { 1497sub new {
1460 glDisable GL_BLEND; 1546 glDisable GL_BLEND;
1461} 1547}
1462 1548
1463############################################################################# 1549#############################################################################
1464 1550
1551package CFClient::UI::Image;
1552
1553our @ISA = CFClient::UI::Base::;
1554
1555use CFClient::OpenGL;
1556use Carp qw/confess/;
1557
1558our %loaded_images;
1559
1560sub new {
1561 my $class = shift;
1562
1563 my $self = $class->SUPER::new (can_events => 0, @_);
1564
1565 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1566
1567 $loaded_images{$self->{image}} ||=
1568 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1569
1570 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1571
1572 Scalar::Util::weaken $loaded_images{$self->{image}};
1573
1574 $self->{aspect} = $tex->{w} / $tex->{h};
1575
1576 $self
1577}
1578
1579sub size_request {
1580 my ($self) = @_;
1581
1582 ($self->{tex}->{w}, $self->{tex}->{h})
1583}
1584
1585sub _draw {
1586 my ($self) = @_;
1587
1588 my $tex = $self->{tex};
1589
1590 my ($w, $h) = ($self->{w}, $self->{h});
1591
1592 if ($self->{rot90}) {
1593 glRotate 90, 0, 0, 1;
1594 glTranslate 0, -$self->{w}, 0;
1595
1596 ($w, $h) = ($h, $w);
1597 }
1598
1599 glEnable GL_BLEND;
1600 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1601 glEnable GL_TEXTURE_2D;
1602 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1603
1604 $tex->draw_quad (0, 0, $w, $h);
1605
1606 glDisable GL_BLEND;
1607 glDisable GL_TEXTURE_2D;
1608}
1609
1610#############################################################################
1611
1465package CFClient::UI::VGauge; 1612package CFClient::UI::VGauge;
1466 1613
1467our @ISA = CFClient::UI::Base::; 1614our @ISA = CFClient::UI::Base::;
1615
1616use List::Util qw(min max);
1468 1617
1469use CFClient::OpenGL; 1618use CFClient::OpenGL;
1470 1619
1471my %tex = ( 1620my %tex = (
1472 food => [ 1621 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1623 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ], 1624 ],
1476 grace => [ 1625 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1627 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1479 ], 1628 ],
1480 hp => [ 1629 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1631 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ], 1632 ],
1484 mana => [ 1633 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1634 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1635 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1487 ], 1636 ],
1488); 1637);
1489 1638
1490# eg. VGauge->new (gauge => 'food'), default gauge: food 1639# eg. VGauge->new (gauge => 'food'), default gauge: food
1491sub new { 1640sub new {
1529 1678
1530sub _draw { 1679sub _draw {
1531 my ($self) = @_; 1680 my ($self) = @_;
1532 1681
1533 my $tex = $tex{$self->{type}}; 1682 my $tex = $tex{$self->{type}};
1683 my ($t1, $t2, $t3) = @$tex;
1534 1684
1535 my ($w, $h) = ($self->{w}, $self->{h}); 1685 my ($w, $h) = ($self->{w}, $self->{h});
1536 1686
1537 if ($self->{vertical}) { 1687 if ($self->{vertical}) {
1538 glRotate 90, 0, 0, 1; 1688 glRotate 90, 0, 0, 1;
1540 1690
1541 ($w, $h) = ($h, $w); 1691 ($w, $h) = ($h, $w);
1542 } 1692 }
1543 1693
1544 my $ycut = $self->{val} / ($self->{max_val} || 1); 1694 my $ycut = $self->{val} / ($self->{max_val} || 1);
1545 $ycut = 1 if $self->{val} > $self->{max_val};
1546 1695
1547 my $t1 = $tex->[0]; 1696 my $ycut1 = max 0, min 1, $ycut;
1548 my $t2 = $tex->[1]; 1697 my $ycut2 = max 0, min 1, $ycut - 1;
1698
1699 my $h1 = $self->{h} * (1 - $ycut1);
1700 my $h2 = $self->{h} * (1 - $ycut2);
1549 1701
1550 glEnable GL_BLEND; 1702 glEnable GL_BLEND;
1551 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1703 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1552 glEnable GL_TEXTURE_2D; 1704 glEnable GL_TEXTURE_2D;
1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1554 1706
1555 my $h1 = $self->{h} - $ycut * $self->{h};
1556 my $h2 = $ycut * $self->{h};
1557
1558 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1707 glBindTexture GL_TEXTURE_2D, $t1->{name};
1559 glBegin GL_QUADS; 1708 glBegin GL_QUADS;
1560 glTexCoord 0 , 0; glVertex 0 , 0; 1709 glTexCoord 0 , 0; glVertex 0 , 0;
1561 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1710 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1562 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1711 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1563 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1712 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1564 glEnd; 1713 glEnd;
1565 1714
1715 my $ycut1 = List::Util::min 1, $ycut;
1566 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1716 glBindTexture GL_TEXTURE_2D, $t2->{name};
1567 glBegin GL_QUADS; 1717 glBegin GL_QUADS;
1568 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1718 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1569 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1719 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1570 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1720 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1571 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1721 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1572 glEnd; 1722 glEnd;
1723
1724 if ($t3) {
1725 glBindTexture GL_TEXTURE_2D, $t3->{name};
1726 glBegin GL_QUADS;
1727 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1728 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1729 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1730 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1731 glEnd;
1732 }
1573 1733
1574 glDisable GL_BLEND; 1734 glDisable GL_BLEND;
1575 glDisable GL_TEXTURE_2D; 1735 glDisable GL_TEXTURE_2D;
1576} 1736}
1577 1737
1580package CFClient::UI::Gauge; 1740package CFClient::UI::Gauge;
1581 1741
1582our @ISA = CFClient::UI::VBox::; 1742our @ISA = CFClient::UI::VBox::;
1583 1743
1584sub new { 1744sub new {
1585 my ($class, %arg) = shift; 1745 my ($class, %arg) = @_;
1586 1746
1587 my $self = $class->SUPER::new ( 1747 my $self = $class->SUPER::new (
1588 @_, 1748 tooltip => $arg{type},
1749 %arg,
1589 ); 1750 );
1590 1751
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1752 $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); 1753 $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"); 1754 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1594 1755
1595 $self 1756 $self
1757}
1758
1759sub set_fontsize {
1760 my ($self, $fsize) = @_;
1761
1762 $self->{value}->set_fontsize ($fsize);
1763 $self->{max} ->set_fontsize ($fsize);
1596} 1764}
1597 1765
1598sub set_value { 1766sub set_value {
1599 my ($self, $val, $max) = @_; 1767 my ($self, $val, $max) = @_;
1600 1768
1758sub new { 1926sub new {
1759 my $class = shift; 1927 my $class = shift;
1760 1928
1761 my $self = $class->SUPER::new ( 1929 my $self = $class->SUPER::new (
1762 fontsize => 1, 1930 fontsize => 1,
1931 #font => default_font
1763 @_, 1932 @_,
1764 1933
1765 layout => (new CFClient::Layout), 1934 layout => (new CFClient::Layout),
1766 par => [], 1935 par => [],
1767 height => 0, 1936 height => 0,
1807sub size_allocate { 1976sub size_allocate {
1808 my ($self, $w, $h) = @_; 1977 my ($self, $w, $h) = @_;
1809 1978
1810 $self->SUPER::size_allocate ($w, $h); 1979 $self->SUPER::size_allocate ($w, $h);
1811 1980
1981 $self->{layout}->set_font ($self->{font}) if $self->{font};
1812 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1982 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1813 $self->{layout}->set_width ($self->{children}[0]{w}); 1983 $self->{layout}->set_width ($self->{children}[0]{w});
1814 1984
1815 $self->reflow; 1985 $self->reflow;
1816} 1986}
1868 my $y1 = $top + $self->{h}; 2038 my $y1 = $top + $self->{h};
1869 2039
1870 my $y = 0; 2040 my $y = 0;
1871 2041
1872 my $layout = $self->{layout}; 2042 my $layout = $self->{layout};
2043
2044 $layout->set_font ($self->{font}) if $self->{font};
1873 2045
1874 for my $par (@{$self->{par}}) { 2046 for my $par (@{$self->{par}}) {
1875 my $h = $par->[0]; 2047 my $h = $par->[0];
1876 2048
1877 if ($y0 < $y + $h && $y < $y1) { 2049 if ($y0 < $y + $h && $y < $y1) {
1957 my $class = shift; 2129 my $class = shift;
1958 2130
1959 my $self = $class->SUPER::new ( 2131 my $self = $class->SUPER::new (
1960 state => 0, 2132 state => 0,
1961 connect_activate => \&toggle_flopper, 2133 connect_activate => \&toggle_flopper,
2134 can_events => 1,
1962 @_ 2135 @_
1963 ); 2136 );
1964 2137
1965 if ($self->{state}) { 2138 if ($self->{state}) {
1966 $self->{state} = 0; 2139 $self->{state} = 0;
1986 $self->emit (changed => $self->{state}); 2159 $self->emit (changed => $self->{state});
1987} 2160}
1988 2161
1989############################################################################# 2162#############################################################################
1990 2163
2164package CFClient::UI::Tooltip;
2165
2166our @ISA = CFClient::UI::Bin::;
2167
2168use CFClient::OpenGL;
2169
2170sub new {
2171 my $class = shift;
2172
2173 $class->SUPER::new (
2174 @_,
2175 can_events => 0,
2176 )
2177}
2178
2179sub set_text {
2180 my ($self, $text) = @_;
2181
2182 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2183 $self->{label}->set_text ($text);
2184 $self->add ($self->{label});
2185}
2186
2187sub size_request {
2188 my ($self) = @_;
2189
2190 $self->child->set_max_size ($::WIDTH * 0.2);
2191
2192 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2193
2194 ($w + 4, $h + 4)
2195}
2196
2197sub _draw {
2198 my ($self) = @_;
2199
2200 glPushMatrix;
2201 glTranslate 0.375, 0.375;
2202
2203 my ($w, $h) = @$self{qw(w h)};
2204
2205 glColor 1, 0.8, 0.4;
2206 glBegin GL_QUADS;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glColor 0, 0, 0;
2214 glBegin GL_LINE_LOOP;
2215 glVertex 0 , 0;
2216 glVertex 0 , $h;
2217 glVertex $w, $h;
2218 glVertex $w, 0;
2219 glEnd;
2220
2221 glPopMatrix;
2222
2223 glTranslate 2, 2;
2224 $self->SUPER::_draw;
2225}
2226
2227#############################################################################
2228
1991package CFClient::UI::Root; 2229package CFClient::UI::Root;
1992 2230
1993our @ISA = CFClient::UI::Container::; 2231our @ISA = CFClient::UI::Container::;
1994 2232
1995use CFClient::OpenGL; 2233use CFClient::OpenGL;
1996 2234
1997sub check_size { 2235sub check_size {
1998 my ($self) = @_; 2236 my ($self) = @_;
1999 2237
2000 $self->configure (0, 0, $::WITH, $::HEIGHT); 2238 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2001} 2239}
2002 2240
2003sub size_request { 2241sub size_request {
2004 ($::WIDTH, $::HEIGHT) 2242 ($::WIDTH, $::HEIGHT)
2005} 2243}
2007sub configure { 2245sub configure {
2008 my ($self, $x, $y, $w, $h) = @_; 2246 my ($self, $x, $y, $w, $h) = @_;
2009 2247
2010 $self->SUPER::configure ($x, $y, $w, $h); 2248 $self->SUPER::configure ($x, $y, $w, $h);
2011 2249
2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2013 for @{$self->{children}}; 2250 for my $child (@{$self->{children}}) {
2251 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2252
2253 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2254 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2255 $child->configure ($X, $Y, $W,$H);
2256 }
2014} 2257}
2015 2258
2016sub _topleft { 2259sub _topleft {
2017 my ($self, $x, $y) = @_; 2260 my ($self, $x, $y) = @_;
2018 2261
2026 ::refresh (); 2269 ::refresh ();
2027} 2270}
2028 2271
2029sub add { 2272sub add {
2030 my ($self, $child) = @_; 2273 my ($self, $child) = @_;
2274
2275 # integerize window positions
2276 $child->{x} = int $child->{x};
2277 $child->{y} = int $child->{y};
2031 2278
2032 $self->SUPER::add ($child); 2279 $self->SUPER::add ($child);
2033} 2280}
2034 2281
2035sub on_refresh { 2282sub on_refresh {
2061############################################################################# 2308#############################################################################
2062 2309
2063package CFClient::UI; 2310package CFClient::UI;
2064 2311
2065$ROOT = new CFClient::UI::Root; 2312$ROOT = new CFClient::UI::Root;
2313$TOOLTIP = new CFClient::UI::Tooltip;
2066 2314
20671 23151
2068 2316

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines