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.140 by root, Thu Apr 20 04:20:52 2006 UTC vs.
Revision 1.155 by root, Sun Apr 23 02:22:54 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
151 $self->{x} = $x;
152 $self->{y} = $y;
153
154 if ($self->{aspect}) { 185 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect}; 186 my $w2 = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect}; 187 my $h2 = List::Util::min $h, int $w / $self->{aspect};
157 }
158 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) {
198 $self->{x} = $x;
199 $self->{y} = $y;
200 $self->update;
201 }
202
159 return unless $self->{w} != $w || $self->{h} != $h; 203 if ($self->{w} != $w || $self->{h} != $h) {
160
161 $self->{w} = $w; 204 $self->{w} = $w;
162 $self->{h} = $h; 205 $self->{h} = $h;
163 206
164 $self->size_allocate ($w, $h); 207 $self->size_allocate ($w, $h);
165 $self->update; 208 $self->update;
209 }
166} 210}
167 211
168sub size_allocate { 212sub size_allocate {
169 # nothing to be done 213 # nothing to be done
170} 214}
268} 312}
269 313
270sub find_widget { 314sub find_widget {
271 my ($self, $x, $y) = @_; 315 my ($self, $x, $y) = @_;
272 316
317 return () unless $self->{can_events};
318
273 return $self 319 return $self
274 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 320 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
275 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 321 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
276 322
277 () 323 ()
284} 330}
285 331
286sub check_size { 332sub check_size {
287 my ($self) = @_; 333 my ($self) = @_;
288 334
335 return unless $self->{parent};
336
289 my ($w, $h) = $self->size_request; 337 my ($w, $h) = $self->size_request;
290 338
291 if ($w != $self->{req_w} || $h != $self->{req_h}) { 339 if ($w != $self->{req_w} || $h != $self->{req_h}) {
292 $self->{req_w} = $w; 340 $self->{req_w} = $w;
293 $self->{req_h} = $h; 341 $self->{req_h} = $h;
294 342
295 $self->{parent}->check_size 343 $self->{parent}->check_size;
296 if $self->{parent};
297 } 344 }
298} 345}
299 346
300sub update { 347sub update {
301 my ($self) = @_; 348 my ($self) = @_;
368 415
369package CFClient::UI::Empty; 416package CFClient::UI::Empty;
370 417
371our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
372 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
373sub size_request { 425sub size_request {
374 (0, 0) 426 (0, 0)
375} 427}
376 428
377sub draw { } 429sub draw { }
385sub new { 437sub new {
386 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
387 439
388 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
389 441
390 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
391 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
392 444
393 $self 445 $self
394} 446}
395 447
414 delete $child->{parent}; 466 delete $child->{parent};
415 467
416 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 468 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
417 469
418 $self->check_size; 470 $self->check_size;
471 $self->update;
419} 472}
420 473
421sub find_widget { 474sub find_widget {
422 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
423 476
551sub new { die } 604sub new { die }
552 605
553sub size_request { 606sub size_request {
554 my ($self) = @_; 607 my ($self) = @_;
555 608
556 @$self{qw(child_w child_h)} = $self->child->size_request; 609 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
557 $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)});
558 611
559 @$self{qw(child_w child_h)} 612 @$self{qw(child_w child_h)}
560} 613}
561 614
571package CFClient::UI::Frame; 624package CFClient::UI::Frame;
572 625
573our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
574 627
575use CFClient::OpenGL; 628use CFClient::OpenGL;
576
577sub size_request {
578 my ($self) = @_;
579 my $chld = $self->child
580 or return (0, 0);
581
582 $chld->move (2, 2);
583
584 map { $_ + 4 } $chld->size_request;
585}
586
587sub size_allocate {
588 my ($self, $x, $y, $w, $h) = @_;
589
590 $self->child->configure (2, 2, $w - 4, $h - 4);
591}
592
593sub _draw {
594 my ($self) = @_;
595
596 my $chld = $self->child;
597
598 my ($w, $h) = $chld->size_request;
599
600 glBegin GL_QUADS;
601 glColor 0, 0, 0;
602 glVertex 0 , 0;
603 glVertex 0 , $h + 4;
604 glVertex $w + 4 , $h + 4;
605 glVertex $w + 4 , 0;
606 glEnd;
607
608 $chld->draw;
609}
610
611#############################################################################
612
613package CFClient::UI::FancyFrame;
614
615our @ISA = CFClient::UI::Bin::;
616
617use CFClient::OpenGL;
618
619my @tex =
620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
622 629
623sub new { 630sub new {
624 my $class = shift; 631 my $class = shift;
625 632
626 # TODO: user_x, user_y, overwrite moveto?
627
628 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
629 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
630 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
631 border => 0.8, 636 border => 0.8,
632 @_ 637 @_
633 ) 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
634} 718}
635 719
636sub border { 720sub border {
637 int $_[0]{border} * $::FONTSIZE 721 int $_[0]{border} * $::FONTSIZE
638} 722}
653sub size_allocate { 737sub size_allocate {
654 my ($self, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
655 739
656 $h -= List::Util::max 0, $self->border * 2; 740 $h -= List::Util::max 0, $self->border * 2;
657 $w -= List::Util::max 0, $self->border * 2; 741 $w -= List::Util::max 0, $self->border * 2;
742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
658 745
659 $self->child->configure ($self->border, $self->border, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
660} 747}
661 748
662sub button_down { 749sub button_down {
742 $bg->draw_quad ($border, $border, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
743 830
744 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
745 glDisable GL_BLEND; 832 glDisable GL_BLEND;
746 833
834 $self->{title}->draw if $self->{title};
747 $self->child->draw; 835 $self->child->draw;
748} 836}
749 837
750############################################################################# 838#############################################################################
751 839
793 or next; 881 or next;
794 882
795 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
796 my $widget = $row->[$x] 884 my $widget = $row->[$x]
797 or next; 885 or next;
798 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
799 887
800 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
801 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
802 } 890 }
803 } 891 }
908 996
909 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
910 998
911 my $children = $self->{children}; 999 my $children = $self->{children};
912 1000
913 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
914 1002
915 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
916 1004
917 if ($req_h > $h) { 1005 if ($req_h > $h) {
918 # ah well, not enough space 1006 # ah well, not enough space
966sub size_allocate { 1054sub size_allocate {
967 my ($self, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
968 1056
969 my $children = $self->{children}; 1057 my $children = $self->{children};
970 1058
971 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
972 1060
973 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
974 1062
975 if ($req_h > $h) { 1063 if ($req_h > $h) {
976 # ah well, not enough space 1064 # ah well, not enough space
1017 text => "", 1105 text => "",
1018 align => -1, 1106 align => -1,
1019 valign => -1, 1107 valign => -1,
1020 padding => 2, 1108 padding => 2,
1021 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
1022 %arg 1111 %arg
1023 ); 1112 );
1024 1113
1025 $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 }
1026 1119
1027 $self->set_text (delete $self->{text}) if exists $self->{text}; 1120 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1121 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1029 1122
1030 $self 1123 $self
1044 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
1045 1138
1046 $self->{layout}->set_text ($text); 1139 $self->{layout}->set_text ($text);
1047 1140
1048 delete $self->{texture}; 1141 delete $self->{texture};
1142 $self->check_size;
1049 $self->update; 1143 $self->update;
1050} 1144}
1051 1145
1052sub set_markup { 1146sub set_markup {
1053 my ($self, $markup) = @_; 1147 my ($self, $markup) = @_;
1054 1148
1055 $self->{layout}->set_markup ($markup); 1149 $self->{layout}->set_markup ($markup);
1056 1150
1057 delete $self->{texture}; 1151 delete $self->{texture};
1152 $self->check_size;
1058 $self->update; 1153 $self->update;
1059} 1154}
1060 1155
1061sub size_request { 1156sub size_request {
1062 my ($self) = @_; 1157 my ($self) = @_;
1063 1158
1064 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066 1161
1067 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 }
1068 1172
1069 ( 1173 (
1070 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
1072 ) 1176 )
1074 1178
1075sub size_allocate { 1179sub size_allocate {
1076 my ($self, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
1077 1181
1078 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;
1079} 1192}
1080 1193
1081sub _draw { 1194sub _draw {
1082 my ($self) = @_; 1195 my ($self) = @_;
1083 1196
1129 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1130 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1131 can_hover => 1, 1244 can_hover => 1,
1132 can_focus => 1, 1245 can_focus => 1,
1133 valign => 0, 1246 valign => 0,
1247 can_events => 1,
1134 @_ 1248 @_
1135 ) 1249 )
1136} 1250}
1137 1251
1138sub _set_text { 1252sub _set_text {
1166} 1280}
1167 1281
1168sub size_allocate { 1282sub size_allocate {
1169 my ($self, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1170 1284
1171 $self->_set_text ($self->{text}); 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1172} 1286}
1173 1287
1174sub set_text { 1288sub set_text {
1175 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1176 1290
1309our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1310 1424
1311use CFClient::OpenGL; 1425use CFClient::OpenGL;
1312 1426
1313my @tex = 1427my @tex =
1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1315 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1316 1430
1317sub new { 1431sub new {
1318 my $class = shift; 1432 my $class = shift;
1319 1433
1323 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1324 active_fg => [0, 0, 1], 1438 active_fg => [0, 0, 1],
1325 can_hover => 1, 1439 can_hover => 1,
1326 align => 0, 1440 align => 0,
1327 valign => 0, 1441 valign => 0,
1442 can_events => 1,
1328 @_ 1443 @_
1329 ) 1444 )
1330} 1445}
1331 1446
1332sub button_up { 1447sub button_up {
1366package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1367 1482
1368our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1369 1484
1370my @tex = 1485my @tex =
1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1372 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1373 1488
1374use CFClient::OpenGL; 1489use CFClient::OpenGL;
1375 1490
1376sub new { 1491sub new {
1425 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1426} 1541}
1427 1542
1428############################################################################# 1543#############################################################################
1429 1544
1430package CFClient::UI::VGauge; 1545package CFClient::UI::Image;
1431 1546
1432our @ISA = CFClient::UI::Base::; 1547our @ISA = CFClient::UI::Base::;
1433 1548
1434use CFClient::OpenGL; 1549use CFClient::OpenGL;
1550use Carp qw/confess/;
1435 1551
1436my %tex = ( 1552our %loaded_images;
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454 1553
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1554sub new {
1457 my $class = shift; 1555 my $class = shift;
1458 1556
1459 my $self = $class->SUPER::new ( 1557 my $self = $class->SUPER::new (can_events => 0, @_);
1460 gauge => 'food',
1461 @_
1462 );
1463 1558
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h}; 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};
1465 1569
1466 $self 1570 $self
1467} 1571}
1468 1572
1469sub size_request { 1573sub size_request {
1470 my ($self) = @_; 1574 my ($self) = @_;
1471 1575
1472 my $tex = $tex{$self->{gauge}}[0]; 1576 ($self->{tex}->{w}, $self->{tex}->{h})
1473
1474 @$tex{qw(w h)}
1475}
1476
1477sub set_max {
1478 my ($self, $max) = @_;
1479
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493} 1577}
1494 1578
1495sub _draw { 1579sub _draw {
1496 my ($self) = @_; 1580 my ($self) = @_;
1497 1581
1498 my $tex = $tex{$self->{gauge}}; 1582 my $tex = $self->{tex};
1499 1583
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1584 my ($w, $h) = ($self->{w}, $self->{h});
1501 1585
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1586 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1587 glRotate 90, 0, 0, 1;
1588 glTranslate 0, -$self->{w}, 0;
1504 1589
1505 my $t1 = $tex->[0]; 1590 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1591 }
1507 1592
1508 glEnable GL_BLEND; 1593 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1594 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1595 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 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
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1696 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h}; 1697 my $h2 = $ycut * $self->{h};
1515 1698
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1699 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS; 1700 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp; 1701 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1704 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd; 1705 glEnd;
1525
1526 $yp += $h1;
1527 1706
1528 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1707 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS; 1708 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1534 glEnd; 1713 glEnd;
1535 1714
1536 glDisable GL_BLEND; 1715 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1716 glDisable GL_TEXTURE_2D;
1717}
1718
1719#############################################################################
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");
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
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);
1538} 1762}
1539 1763
1540############################################################################# 1764#############################################################################
1541 1765
1542package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1882 my $class = shift; 2106 my $class = shift;
1883 2107
1884 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1885 state => 0, 2109 state => 0,
1886 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1887 @_ 2112 @_
1888 ); 2113 );
1889 2114
1890 if ($self->{state}) { 2115 if ($self->{state}) {
1891 $self->{state} = 0; 2116 $self->{state} = 0;
1911 $self->emit (changed => $self->{state}); 2136 $self->emit (changed => $self->{state});
1912} 2137}
1913 2138
1914############################################################################# 2139#############################################################################
1915 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
1916package CFClient::UI::Root; 2207package CFClient::UI::Root;
1917 2208
1918our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
1919 2210
1920use CFClient::OpenGL; 2211use CFClient::OpenGL;
1921 2212
1922sub check_size { 2213sub check_size {
1923 my ($self) = @_; 2214 my ($self) = @_;
1924 2215
1925 $self->configure (0, 0, $::WITH, $::HEIGHT); 2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
1926} 2217}
1927 2218
1928sub size_request { 2219sub size_request {
1929 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
1930} 2221}
1932sub configure { 2223sub configure {
1933 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
1934 2225
1935 $self->SUPER::configure ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
1936 2227
1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1938 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 }
1939} 2235}
1940 2236
1941sub _topleft { 2237sub _topleft {
1942 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
1943 2239
1951 ::refresh (); 2247 ::refresh ();
1952} 2248}
1953 2249
1954sub add { 2250sub add {
1955 my ($self, $child) = @_; 2251 my ($self, $child) = @_;
2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
1956 2256
1957 $self->SUPER::add ($child); 2257 $self->SUPER::add ($child);
1958} 2258}
1959 2259
1960sub on_refresh { 2260sub on_refresh {
1986############################################################################# 2286#############################################################################
1987 2287
1988package CFClient::UI; 2288package CFClient::UI;
1989 2289
1990$ROOT = new CFClient::UI::Root; 2290$ROOT = new CFClient::UI::Root;
2291$TOOLTIP = new CFClient::UI::Tooltip;
1991 2292
19921 22931
1993 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines