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.147 by root, Sat Apr 22 03:50:25 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 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_markup ($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 abstract"; 191 Carp::confess "size_request is abstract";
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) = @_;
267 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
268 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
269 glEnd; 322 glEnd;
270 glDisable GL_BLEND; 323 glDisable GL_BLEND;
271 } 324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
272} 339}
273 340
274sub _draw { 341sub _draw {
275 my ($self) = @_; 342 my ($self) = @_;
276 343
277 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
278} 345}
279 346
280sub find_widget { 347sub find_widget {
281 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
282 351
283 return $self 352 return $self
284 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
285 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
286 355
294} 363}
295 364
296sub check_size { 365sub check_size {
297 my ($self) = @_; 366 my ($self) = @_;
298 367
299 my ($w, $h) = $self->size_request; 368 $self->{parent}
369 or return 1;
300 370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
301 if ($w != $self->{req_w} || $h != $self->{req_h}) { 375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w; 376 $self->{req_w} = $w;
303 $self->{req_h} = $h; 377 $self->{req_h} = $h;
304 378
305 $self->{parent}->check_size 379 $self->{parent}->check_size
306 if $self->{parent}; 380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
307 } 388 }
308} 389}
309 390
310sub update { 391sub update {
311 my ($self) = @_; 392 my ($self) = @_;
378 459
379package CFClient::UI::Empty; 460package CFClient::UI::Empty;
380 461
381our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
382 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
383sub size_request { 469sub size_request {
384 (0, 0) 470 (0, 0)
385} 471}
386 472
387sub draw { } 473sub draw { }
395sub new { 481sub new {
396 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
397 483
398 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
399 485
400 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
401 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
402 488
403 $self 489 $self
404} 490}
405 491
424 delete $child->{parent}; 510 delete $child->{parent};
425 511
426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
427 513
428 $self->check_size; 514 $self->check_size;
515 $self->update;
516}
517
518sub clear {
519 my ($self) = @_;
520
521 delete $_->{parent}
522 for @{ delete $self->{children} };
523
524 $self->{children} = [];
429} 525}
430 526
431sub find_widget { 527sub find_widget {
432 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
433 529
561sub new { die } 657sub new { die }
562 658
563sub size_request { 659sub size_request {
564 my ($self) = @_; 660 my ($self) = @_;
565 661
566 @$self{qw(child_w child_h)} = $self->child->size_request; 662 @$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)}); 663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568 664
569 @$self{qw(child_w child_h)} 665 @$self{qw(child_w child_h)}
570} 666}
571 667
581package CFClient::UI::Frame; 677package CFClient::UI::Frame;
582 678
583our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
584 680
585use CFClient::OpenGL; 681use 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 $_, mipmap => 1 }
631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
632 682
633sub new { 683sub new {
634 my $class = shift; 684 my $class = shift;
635
636 # TODO: user_x, user_y, overwrite moveto?
637 685
638 my $self = $class->SUPER::new ( 686 my $self = $class->SUPER::new (
639 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
640 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
641 border => 0.8, 689 border => 0.8,
642 @_ 690 @_
643 ); 691 );
644 692
693 $self
694}
695
696sub _draw {
697 my ($self) = @_;
698
699 my ($w, $h) = ($self->{w}, $self->{h});
700
701 glEnable GL_BLEND;
702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
703 glEnable GL_TEXTURE_2D;
704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
705
706# glBegin GL_QUADS;
707# glColor 0, 0, 0, 0;
708# glVertex 0 , 0;
709# glVertex 0 , $h;
710# glVertex $w, $h;
711# glVertex $w, 0;
712# glEnd;
713
714
715 $self->child->draw;
716 glDisable GL_BLEND;
717 glDisable GL_TEXTURE_2D;
718}
719
720#############################################################################
721
722package CFClient::UI::FancyFrame;
723
724our @ISA = CFClient::UI::Bin::;
725
726use CFClient::OpenGL;
727
728my @tex =
729 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
730 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
731
732sub new {
733 my $class = shift;
734
735 # TODO: user_x, user_y, overwrite moveto?
736
737 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1],
740 border => 0.8,
741 can_events => 1,
742 @_
743 );
744
645 $self->{title} &&= new CFClient::UI::Label 745 $self->{title} &&= new CFClient::UI::Label
646 align => 0, 746 align => 0,
647 valign => 1, 747 valign => 1,
648 text => $self->{title}, 748 text => $self->{title},
649 fontsize => 1; 749 fontsize => 1;
656} 756}
657 757
658sub size_request { 758sub size_request {
659 my ($self) = @_; 759 my ($self) = @_;
660 760
661 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
662
663 my ($w, $h) = $self->SUPER::size_request; 761 my ($w, $h) = $self->SUPER::size_request;
664 762
665 ( 763 (
666 $w + $self->border * 2, 764 $w + $self->border * 2,
667 $h + $self->border * 2, 765 $h + $self->border * 2,
815 or next; 913 or next;
816 914
817 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
818 my $widget = $row->[$x] 916 my $widget = $row->[$x]
819 or next; 917 or next;
820 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
821 919
822 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
823 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
824 } 922 }
825 } 923 }
930 1028
931 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
932 1030
933 my $children = $self->{children}; 1031 my $children = $self->{children};
934 1032
935 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
936 1034
937 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
938 1036
939 if ($req_h > $h) { 1037 if ($req_h > $h) {
940 # ah well, not enough space 1038 # ah well, not enough space
988sub size_allocate { 1086sub size_allocate {
989 my ($self, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
990 1088
991 my $children = $self->{children}; 1089 my $children = $self->{children};
992 1090
993 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
994 1092
995 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
996 1094
997 if ($req_h > $h) { 1095 if ($req_h > $h) {
998 # ah well, not enough space 1096 # ah well, not enough space
1033sub new { 1131sub new {
1034 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
1035 1133
1036 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
1037 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
1038 fontsize => 1, 1137 fontsize => 1,
1039 text => "", 1138 text => "",
1040 align => -1, 1139 align => -1,
1041 valign => -1, 1140 valign => -1,
1042 padding => 2, 1141 padding => 2,
1043 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
1044 %arg 1144 %arg
1045 ); 1145 );
1046 1146
1047 if (exists $self->{template}) { 1147 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout; 1148 my $layout = new CFClient::Layout;
1054 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1055 1155
1056 $self 1156 $self
1057} 1157}
1058 1158
1059sub escape_text { 1159sub escape {
1060 local $_ = $_[1]; 1160 local $_ = $_[1];
1061 1161
1062 s/&/&amp;/g; 1162 s/&/&amp;/g;
1063 s/>/&gt;/g; 1163 s/>/&gt;/g;
1064 s/</&lt;/g; 1164 s/</&lt;/g;
1070 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
1071 1171
1072 $self->{layout}->set_text ($text); 1172 $self->{layout}->set_text ($text);
1073 1173
1074 delete $self->{texture}; 1174 delete $self->{texture};
1175 $self->check_size;
1075 $self->update; 1176 $self->update;
1076} 1177}
1077 1178
1078sub set_markup { 1179sub set_markup {
1079 my ($self, $markup) = @_; 1180 my ($self, $markup) = @_;
1080 1181
1081 $self->{layout}->set_markup ($markup); 1182 $self->{layout}->set_markup ($markup);
1082 1183
1083 delete $self->{texture}; 1184 delete $self->{texture};
1185 $self->check_size;
1084 $self->update; 1186 $self->update;
1085} 1187}
1086 1188
1087sub size_request { 1189sub size_request {
1088 my ($self) = @_; 1190 my ($self) = @_;
1089 1191
1090 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1092 1195
1093 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1094 1197
1095 if (exists $self->{template}) { 1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097 1201
1098 my ($w2, $h2) = $self->{template}->size; 1202 my ($w2, $h2) = $self->{template}->size;
1099 1203
1100 $w = List::Util::max $w, $w2; 1204 $w = List::Util::max $w, $w2;
1115 1219
1116sub set_fontsize { 1220sub set_fontsize {
1117 my ($self, $fontsize) = @_; 1221 my ($self, $fontsize) = @_;
1118 1222
1119 $self->{fontsize} = $fontsize; 1223 $self->{fontsize} = $fontsize;
1224 delete $self->{texture};
1120 $self->check_size; 1225 $self->check_size;
1226 $self->update;
1121} 1227}
1122 1228
1123sub _draw { 1229sub _draw {
1124 my ($self) = @_; 1230 my ($self) = @_;
1125 1231
1126 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1127 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1128 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1129 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1130 }; 1237 };
1131 1238
1171 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1172 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1173 can_hover => 1, 1280 can_hover => 1,
1174 can_focus => 1, 1281 can_focus => 1,
1175 valign => 0, 1282 valign => 0,
1283 can_events => 1,
1176 @_ 1284 @_
1177 ) 1285 )
1178} 1286}
1179 1287
1180sub _set_text { 1288sub _set_text {
1208} 1316}
1209 1317
1210sub size_allocate { 1318sub size_allocate {
1211 my ($self, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1212 1320
1213 $self->_set_text ($self->{text}); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1214} 1322}
1215 1323
1216sub set_text { 1324sub set_text {
1217 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1218 1326
1365 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1366 active_fg => [0, 0, 1], 1474 active_fg => [0, 0, 1],
1367 can_hover => 1, 1475 can_hover => 1,
1368 align => 0, 1476 align => 0,
1369 valign => 0, 1477 valign => 0,
1478 can_events => 1,
1370 @_ 1479 @_
1371 ) 1480 )
1372} 1481}
1373 1482
1374sub button_up { 1483sub button_up {
1479our %loaded_images; 1588our %loaded_images;
1480 1589
1481sub new { 1590sub new {
1482 my $class = shift; 1591 my $class = shift;
1483 1592
1484 my $self = $class->SUPER::new (@_); 1593 my $self = $class->SUPER::new (can_events => 0, @_);
1485 1594
1486 $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; 1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1487 1596
1488 $loaded_images{$self->{image}} ||= 1597 $loaded_images{$self->{image}} ||=
1489 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; 1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1531############################################################################# 1640#############################################################################
1532 1641
1533package CFClient::UI::VGauge; 1642package CFClient::UI::VGauge;
1534 1643
1535our @ISA = CFClient::UI::Base::; 1644our @ISA = CFClient::UI::Base::;
1645
1646use List::Util qw(min max);
1536 1647
1537use CFClient::OpenGL; 1648use CFClient::OpenGL;
1538 1649
1539my %tex = ( 1650my %tex = (
1540 food => [ 1651 food => [
1541 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1542 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1543 ], 1654 ],
1544 grace => [ 1655 grace => [
1545 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1546 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1547 ], 1658 ],
1548 hp => [ 1659 hp => [
1549 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1550 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1551 ], 1662 ],
1552 mana => [ 1663 mana => [
1553 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1554 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1555 ], 1666 ],
1556); 1667);
1557 1668
1558# eg. VGauge->new (gauge => 'food'), default gauge: food 1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1559sub new { 1670sub new {
1597 1708
1598sub _draw { 1709sub _draw {
1599 my ($self) = @_; 1710 my ($self) = @_;
1600 1711
1601 my $tex = $tex{$self->{type}}; 1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1602 1714
1603 my ($w, $h) = ($self->{w}, $self->{h}); 1715 my ($w, $h) = ($self->{w}, $self->{h});
1604 1716
1605 if ($self->{vertical}) { 1717 if ($self->{vertical}) {
1606 glRotate 90, 0, 0, 1; 1718 glRotate 90, 0, 0, 1;
1608 1720
1609 ($w, $h) = ($h, $w); 1721 ($w, $h) = ($h, $w);
1610 } 1722 }
1611 1723
1612 my $ycut = $self->{val} / ($self->{max_val} || 1); 1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1613 $ycut = 1 if $self->{val} > $self->{max_val};
1614 1725
1615 my $t1 = $tex->[0]; 1726 my $ycut1 = max 0, min 1, $ycut;
1616 my $t2 = $tex->[1]; 1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1729 my $h1 = $self->{h} * (1 - $ycut1);
1730 my $h2 = $self->{h} * (1 - $ycut2);
1617 1731
1618 glEnable GL_BLEND; 1732 glEnable GL_BLEND;
1619 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1620 glEnable GL_TEXTURE_2D; 1734 glEnable GL_TEXTURE_2D;
1621 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1622 1736
1623 my $h1 = $self->{h} - $ycut * $self->{h};
1624 my $h2 = $ycut * $self->{h};
1625
1626 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1627 glBegin GL_QUADS; 1738 glBegin GL_QUADS;
1628 glTexCoord 0 , 0; glVertex 0 , 0; 1739 glTexCoord 0 , 0; glVertex 0 , 0;
1629 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1630 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1631 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1632 glEnd; 1743 glEnd;
1633 1744
1745 my $ycut1 = List::Util::min 1, $ycut;
1634 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1635 glBegin GL_QUADS; 1747 glBegin GL_QUADS;
1636 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1637 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1638 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1639 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1640 glEnd; 1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1641 1763
1642 glDisable GL_BLEND; 1764 glDisable GL_BLEND;
1643 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1644} 1766}
1645 1767
1648package CFClient::UI::Gauge; 1770package CFClient::UI::Gauge;
1649 1771
1650our @ISA = CFClient::UI::VBox::; 1772our @ISA = CFClient::UI::VBox::;
1651 1773
1652sub new { 1774sub new {
1653 my ($class, %arg) = shift; 1775 my ($class, %arg) = @_;
1654 1776
1655 my $self = $class->SUPER::new ( 1777 my $self = $class->SUPER::new (
1656 @_, 1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1657 ); 1782 );
1658 1783
1659 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1660 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1661 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1662 1787
1663 $self 1788 $self
1664} 1789}
1665 1790
1666sub set_fontsize { 1791sub set_fontsize {
1833sub new { 1958sub new {
1834 my $class = shift; 1959 my $class = shift;
1835 1960
1836 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1837 fontsize => 1, 1962 fontsize => 1,
1963 #font => default_font
1838 @_, 1964 @_,
1839 1965
1840 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1841 par => [], 1967 par => [],
1842 height => 0, 1968 height => 0,
1882sub size_allocate { 2008sub size_allocate {
1883 my ($self, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1884 2010
1885 $self->SUPER::size_allocate ($w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1886 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1887 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1888 $self->{layout}->set_width ($self->{children}[0]{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1889 2016
1890 $self->reflow; 2017 $self->reflow;
1891} 2018}
1943 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1944 2071
1945 my $y = 0; 2072 my $y = 0;
1946 2073
1947 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1948 2077
1949 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1950 my $h = $par->[0]; 2079 my $h = $par->[0];
1951 2080
1952 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
2032 my $class = shift; 2161 my $class = shift;
2033 2162
2034 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
2035 state => 0, 2164 state => 0,
2036 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
2037 @_ 2167 @_
2038 ); 2168 );
2039 2169
2040 if ($self->{state}) { 2170 if ($self->{state}) {
2041 $self->{state} = 0; 2171 $self->{state} = 0;
2061 $self->emit (changed => $self->{state}); 2191 $self->emit (changed => $self->{state});
2062} 2192}
2063 2193
2064############################################################################# 2194#############################################################################
2065 2195
2196package CFClient::UI::Tooltip;
2197
2198our @ISA = CFClient::UI::Bin::;
2199
2200use CFClient::OpenGL;
2201
2202sub new {
2203 my $class = shift;
2204
2205 $class->SUPER::new (
2206 @_,
2207 can_events => 0,
2208 )
2209}
2210
2211sub set_markup {
2212 my ($self, $text) = @_;
2213
2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2215 $self->{label}->set_markup ($text);
2216 $self->add ($self->{label});
2217}
2218
2219sub size_request {
2220 my ($self) = @_;
2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2225
2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2233}
2234
2235sub _draw {
2236 my ($self) = @_;
2237
2238 glPushMatrix;
2239 glTranslate 0.375, 0.375;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 glColor 1, 0.8, 0.4;
2244 glBegin GL_QUADS;
2245 glVertex 0 , 0;
2246 glVertex 0 , $h;
2247 glVertex $w, $h;
2248 glVertex $w, 0;
2249 glEnd;
2250
2251 glColor 0, 0, 0;
2252 glBegin GL_LINE_LOOP;
2253 glVertex 0 , 0;
2254 glVertex 0 , $h;
2255 glVertex $w, $h;
2256 glVertex $w, 0;
2257 glEnd;
2258
2259 glPopMatrix;
2260
2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
2264
2265#############################################################################
2266
2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
2066package CFClient::UI::Root; 2305package CFClient::UI::Root;
2067 2306
2068our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
2069 2308
2070use CFClient::OpenGL; 2309use CFClient::OpenGL;
2071 2310
2072sub check_size { 2311sub check_size {
2073 my ($self) = @_; 2312 my ($self) = @_;
2074 2313
2075 $self->configure (0, 0, $::WITH, $::HEIGHT); 2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2076} 2315}
2077 2316
2078sub size_request { 2317sub size_request {
2079 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
2080} 2319}
2082sub configure { 2321sub configure {
2083 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
2084 2323
2085 $self->SUPER::configure ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
2086 2325
2087 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2088 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2328
2329 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H);
2332 }
2089} 2333}
2090 2334
2091sub _topleft { 2335sub _topleft {
2092 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
2093 2337
2140############################################################################# 2384#############################################################################
2141 2385
2142package CFClient::UI; 2386package CFClient::UI;
2143 2387
2144$ROOT = new CFClient::UI::Root; 2388$ROOT = new CFClient::UI::Root;
2389$TOOLTIP = new CFClient::UI::Tooltip;
2145 2390
21461 23911
2147 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines