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.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 abtract"; 191 Carp::confess "size_request is abstract";
146} 192}
147 193
148sub configure { 194sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
150 196
151 $self->{x} = $x;
152 $self->{y} = $y;
153
154 if ($self->{aspect}) { 197 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect}; 198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect}; 199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
157 }
158 200
201 # use alignment to adjust x, y
202
203 $x += int +($w - $w2) * 0.5;
204 $y += int +($h - $h2) * 0.5;
205
206 ($w, $h) = ($w2, $h2);
207 }
208
209 if ($self->{x} != $x || $self->{y} != $y) {
210 $self->{x} = $x;
211 $self->{y} = $y;
212 $self->update;
213 }
214
159 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
160
161 $self->{w} = $w; 216 $self->{w} = $w;
162 $self->{h} = $h; 217 $self->{h} = $h;
163 218
164 $self->size_allocate ($w, $h); 219 $self->size_allocate ($w, $h);
165 $self->update; 220 $self->update;
221 }
166} 222}
167 223
168sub size_allocate { 224sub size_allocate {
169 # 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;
170} 233}
171 234
172# return top left coordinates 235# return top left coordinates
173sub _topleft { 236sub _topleft {
174 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
257 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
258 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
259 glEnd; 322 glEnd;
260 glDisable GL_BLEND; 323 glDisable GL_BLEND;
261 } 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 }
262} 339}
263 340
264sub _draw { 341sub _draw {
265 my ($self) = @_; 342 my ($self) = @_;
266 343
267 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
268} 345}
269 346
270sub find_widget { 347sub find_widget {
271 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
272 351
273 return $self 352 return $self
274 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
275 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
276 355
284} 363}
285 364
286sub check_size { 365sub check_size {
287 my ($self) = @_; 366 my ($self) = @_;
288 367
289 my ($w, $h) = $self->size_request; 368 $self->{parent}
369 or return 1;
290 370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
291 if ($w != $self->{req_w} || $h != $self->{req_h}) { 375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
292 $self->{req_w} = $w; 376 $self->{req_w} = $w;
293 $self->{req_h} = $h; 377 $self->{req_h} = $h;
294 378
295 $self->{parent}->check_size 379 $self->{parent}->check_size
296 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
297 } 388 }
298} 389}
299 390
300sub update { 391sub update {
301 my ($self) = @_; 392 my ($self) = @_;
368 459
369package CFClient::UI::Empty; 460package CFClient::UI::Empty;
370 461
371our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
372 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
373sub size_request { 469sub size_request {
374 (0, 0) 470 (0, 0)
375} 471}
376 472
377sub draw { } 473sub draw { }
385sub new { 481sub new {
386 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
387 483
388 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
389 485
390 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
391 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
392 488
393 $self 489 $self
394} 490}
395 491
414 delete $child->{parent}; 510 delete $child->{parent};
415 511
416 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
417 513
418 $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} = [];
419} 525}
420 526
421sub find_widget { 527sub find_widget {
422 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
423 529
551sub new { die } 657sub new { die }
552 658
553sub size_request { 659sub size_request {
554 my ($self) = @_; 660 my ($self) = @_;
555 661
556 @$self{qw(child_w child_h)} = $self->child->size_request; 662 @$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)}); 663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
558 664
559 @$self{qw(child_w child_h)} 665 @$self{qw(child_w child_h)}
560} 666}
561 667
571package CFClient::UI::Frame; 677package CFClient::UI::Frame;
572 678
573our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
574 680
575use CFClient::OpenGL; 681use 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 682
623sub new { 683sub new {
624 my $class = shift; 684 my $class = shift;
625 685
626 # TODO: user_x, user_y, overwrite moveto?
627
628 $class->SUPER::new ( 686 my $self = $class->SUPER::new (
629 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
630 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
631 border => 0.8, 689 border => 0.8,
632 @_ 690 @_
633 ) 691 );
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
745 $self->{title} &&= new CFClient::UI::Label
746 align => 0,
747 valign => 1,
748 text => $self->{title},
749 fontsize => 1;
750
751 $self
634} 752}
635 753
636sub border { 754sub border {
637 int $_[0]{border} * $::FONTSIZE 755 int $_[0]{border} * $::FONTSIZE
638} 756}
639 757
640sub size_request { 758sub size_request {
641 my ($self) = @_; 759 my ($self) = @_;
642
643 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
644 760
645 my ($w, $h) = $self->SUPER::size_request; 761 my ($w, $h) = $self->SUPER::size_request;
646 762
647 ( 763 (
648 $w + $self->border * 2, 764 $w + $self->border * 2,
653sub size_allocate { 769sub size_allocate {
654 my ($self, $w, $h) = @_; 770 my ($self, $w, $h) = @_;
655 771
656 $h -= List::Util::max 0, $self->border * 2; 772 $h -= List::Util::max 0, $self->border * 2;
657 $w -= List::Util::max 0, $self->border * 2; 773 $w -= List::Util::max 0, $self->border * 2;
774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
658 777
659 $self->child->configure ($self->border, $self->border, $w, $h); 778 $self->child->configure ($self->border, $self->border, $w, $h);
660} 779}
661 780
662sub button_down { 781sub button_down {
742 $bg->draw_quad ($border, $border, $cw, $ch); 861 $bg->draw_quad ($border, $border, $cw, $ch);
743 862
744 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
745 glDisable GL_BLEND; 864 glDisable GL_BLEND;
746 865
866 $self->{title}->draw if $self->{title};
747 $self->child->draw; 867 $self->child->draw;
748} 868}
749 869
750############################################################################# 870#############################################################################
751 871
793 or next; 913 or next;
794 914
795 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
796 my $widget = $row->[$x] 916 my $widget = $row->[$x]
797 or next; 917 or next;
798 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
799 919
800 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
801 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
802 } 922 }
803 } 923 }
908 1028
909 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
910 1030
911 my $children = $self->{children}; 1031 my $children = $self->{children};
912 1032
913 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
914 1034
915 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
916 1036
917 if ($req_h > $h) { 1037 if ($req_h > $h) {
918 # ah well, not enough space 1038 # ah well, not enough space
966sub size_allocate { 1086sub size_allocate {
967 my ($self, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
968 1088
969 my $children = $self->{children}; 1089 my $children = $self->{children};
970 1090
971 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
972 1092
973 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
974 1094
975 if ($req_h > $h) { 1095 if ($req_h > $h) {
976 # ah well, not enough space 1096 # ah well, not enough space
1011sub new { 1131sub new {
1012 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
1013 1133
1014 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
1015 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
1016 fontsize => 1, 1137 fontsize => 1,
1017 text => "", 1138 text => "",
1018 align => -1, 1139 align => -1,
1019 valign => -1, 1140 valign => -1,
1020 padding => 2, 1141 padding => 2,
1021 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
1022 %arg 1144 %arg
1023 ); 1145 );
1024 1146
1025 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1147 if (exists $self->{template}) {
1148 my $layout = new CFClient::Layout;
1149 $layout->set_text (delete $self->{template});
1150 $self->{template} = $layout;
1151 }
1026 1152
1027 $self->set_text (delete $self->{text}) if exists $self->{text}; 1153 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1029 1155
1030 $self 1156 $self
1031} 1157}
1032 1158
1033sub escape_text { 1159sub escape {
1034 local $_ = $_[1]; 1160 local $_ = $_[1];
1035 1161
1036 s/&/&amp;/g; 1162 s/&/&amp;/g;
1037 s/>/&gt;/g; 1163 s/>/&gt;/g;
1038 s/</&lt;/g; 1164 s/</&lt;/g;
1044 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
1045 1171
1046 $self->{layout}->set_text ($text); 1172 $self->{layout}->set_text ($text);
1047 1173
1048 delete $self->{texture}; 1174 delete $self->{texture};
1175 $self->check_size;
1049 $self->update; 1176 $self->update;
1050} 1177}
1051 1178
1052sub set_markup { 1179sub set_markup {
1053 my ($self, $markup) = @_; 1180 my ($self, $markup) = @_;
1054 1181
1055 $self->{layout}->set_markup ($markup); 1182 $self->{layout}->set_markup ($markup);
1056 1183
1057 delete $self->{texture}; 1184 delete $self->{texture};
1185 $self->check_size;
1058 $self->update; 1186 $self->update;
1059} 1187}
1060 1188
1061sub size_request { 1189sub size_request {
1062 my ($self) = @_; 1190 my ($self) = @_;
1063 1191
1064 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066 1195
1067 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1197
1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1201
1202 my ($w2, $h2) = $self->{template}->size;
1203
1204 $w = List::Util::max $w, $w2;
1205 $h = List::Util::max $h, $h2;
1206 }
1068 1207
1069 ( 1208 (
1070 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
1072 ) 1211 )
1076 my ($self, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
1077 1216
1078 delete $self->{texture}; 1217 delete $self->{texture};
1079} 1218}
1080 1219
1220sub set_fontsize {
1221 my ($self, $fontsize) = @_;
1222
1223 $self->{fontsize} = $fontsize;
1224 delete $self->{texture};
1225 $self->check_size;
1226 $self->update;
1227}
1228
1081sub _draw { 1229sub _draw {
1082 my ($self) = @_; 1230 my ($self) = @_;
1083 1231
1084 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1085 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1086 $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);
1087 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1088 }; 1237 };
1089 1238
1129 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1130 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1131 can_hover => 1, 1280 can_hover => 1,
1132 can_focus => 1, 1281 can_focus => 1,
1133 valign => 0, 1282 valign => 0,
1283 can_events => 1,
1134 @_ 1284 @_
1135 ) 1285 )
1136} 1286}
1137 1287
1138sub _set_text { 1288sub _set_text {
1166} 1316}
1167 1317
1168sub size_allocate { 1318sub size_allocate {
1169 my ($self, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1170 1320
1171 $self->_set_text ($self->{text}); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1172} 1322}
1173 1323
1174sub set_text { 1324sub set_text {
1175 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1176 1326
1309our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1310 1460
1311use CFClient::OpenGL; 1461use CFClient::OpenGL;
1312 1462
1313my @tex = 1463my @tex =
1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1315 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1316 1466
1317sub new { 1467sub new {
1318 my $class = shift; 1468 my $class = shift;
1319 1469
1323 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1324 active_fg => [0, 0, 1], 1474 active_fg => [0, 0, 1],
1325 can_hover => 1, 1475 can_hover => 1,
1326 align => 0, 1476 align => 0,
1327 valign => 0, 1477 valign => 0,
1478 can_events => 1,
1328 @_ 1479 @_
1329 ) 1480 )
1330} 1481}
1331 1482
1332sub button_up { 1483sub button_up {
1366package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1367 1518
1368our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1369 1520
1370my @tex = 1521my @tex =
1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1372 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1373 1524
1374use CFClient::OpenGL; 1525use CFClient::OpenGL;
1375 1526
1376sub new { 1527sub new {
1425 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1426} 1577}
1427 1578
1428############################################################################# 1579#############################################################################
1429 1580
1430package CFClient::UI::VGauge; 1581package CFClient::UI::Image;
1431 1582
1432our @ISA = CFClient::UI::Base::; 1583our @ISA = CFClient::UI::Base::;
1433 1584
1434use CFClient::OpenGL; 1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1435 1587
1436my %tex = ( 1588our %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 1589
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1590sub new {
1457 my $class = shift; 1591 my $class = shift;
1458 1592
1459 my $self = $class->SUPER::new ( 1593 my $self = $class->SUPER::new (can_events => 0, @_);
1460 gauge => 'food',
1461 @_
1462 );
1463 1594
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h}; 1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1596
1597 $loaded_images{$self->{image}} ||=
1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1599
1600 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1601
1602 Scalar::Util::weaken $loaded_images{$self->{image}};
1603
1604 $self->{aspect} = $tex->{w} / $tex->{h};
1465 1605
1466 $self 1606 $self
1467} 1607}
1468 1608
1469sub size_request { 1609sub size_request {
1470 my ($self) = @_; 1610 my ($self) = @_;
1471 1611
1472 my $tex = $tex{$self->{gauge}}[0]; 1612 ($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} 1613}
1494 1614
1495sub _draw { 1615sub _draw {
1496 my ($self) = @_; 1616 my ($self) = @_;
1497 1617
1498 my $tex = $tex{$self->{gauge}}; 1618 my $tex = $self->{tex};
1499 1619
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1620 my ($w, $h) = ($self->{w}, $self->{h});
1501 1621
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1622 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1623 glRotate 90, 0, 0, 1;
1624 glTranslate 0, -$self->{w}, 0;
1504 1625
1505 my $t1 = $tex->[0]; 1626 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1627 }
1507 1628
1508 glEnable GL_BLEND; 1629 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1630 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1631 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1632 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 1633
1634 $tex->draw_quad (0, 0, $w, $h);
1635
1636 glDisable GL_BLEND;
1637 glDisable GL_TEXTURE_2D;
1638}
1639
1640#############################################################################
1641
1642package CFClient::UI::VGauge;
1643
1644our @ISA = CFClient::UI::Base::;
1645
1646use List::Util qw(min max);
1647
1648use CFClient::OpenGL;
1649
1650my %tex = (
1651 food => [
1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1654 ],
1655 grace => [
1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1658 ],
1659 hp => [
1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1662 ],
1663 mana => [
1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1666 ],
1667);
1668
1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1670sub new {
1671 my $class = shift;
1672
1673 my $self = $class->SUPER::new (
1674 type => 'food',
1675 @_
1676 );
1677
1678 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1679
1680 $self
1681}
1682
1683sub size_request {
1684 my ($self) = @_;
1685
1686 #my $tex = $tex{$self->{type}}[0];
1687 #@$tex{qw(w h)}
1688 (0, 0)
1689}
1690
1691sub set_max {
1692 my ($self, $max) = @_;
1693
1694 $self->{max_val} = $max;
1695}
1696
1697sub set_value {
1698 my ($self, $val, $max) = @_;
1699
1700 $self->set_max ($max)
1701 if defined $max;
1702
1703 $max = $self->{max_val};
1704 $self->{val} = $val;
1705
1706 $self->update;
1707}
1708
1709sub _draw {
1710 my ($self) = @_;
1711
1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1714
1715 my ($w, $h) = ($self->{w}, $self->{h});
1716
1717 if ($self->{vertical}) {
1718 glRotate 90, 0, 0, 1;
1719 glTranslate 0, -$self->{w}, 0;
1720
1721 ($w, $h) = ($h, $w);
1722 }
1723
1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1725
1726 my $ycut1 = max 0, min 1, $ycut;
1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1729 my $h1 = $self->{h} * (1 - $ycut1);
1514 my $h2 = $ycut * $self->{h}; 1730 my $h2 = $self->{h} * (1 - $ycut2);
1515 1731
1516 my $yp = 0; 1732 glEnable GL_BLEND;
1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1734 glEnable GL_TEXTURE_2D;
1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1517 1736
1518 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS; 1738 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp; 1739 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd; 1743 glEnd;
1525 1744
1526 $yp += $h1; 1745 my $ycut1 = List::Util::min 1, $ycut;
1527
1528 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS; 1747 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1534 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 }
1535 1763
1536 glDisable GL_BLEND; 1764 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1766}
1767
1768#############################################################################
1769
1770package CFClient::UI::Gauge;
1771
1772our @ISA = CFClient::UI::VBox::;
1773
1774sub new {
1775 my ($class, %arg) = @_;
1776
1777 my $self = $class->SUPER::new (
1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1782 );
1783
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1787
1788 $self
1789}
1790
1791sub set_fontsize {
1792 my ($self, $fsize) = @_;
1793
1794 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize);
1796}
1797
1798sub set_value {
1799 my ($self, $val, $max) = @_;
1800
1801 $self->set_max ($max)
1802 if defined $max;
1803
1804 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1538} 1813}
1539 1814
1540############################################################################# 1815#############################################################################
1541 1816
1542package CFClient::UI::Slider; 1817package CFClient::UI::Slider;
1683sub new { 1958sub new {
1684 my $class = shift; 1959 my $class = shift;
1685 1960
1686 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1687 fontsize => 1, 1962 fontsize => 1,
1963 #font => default_font
1688 @_, 1964 @_,
1689 1965
1690 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1691 par => [], 1967 par => [],
1692 height => 0, 1968 height => 0,
1732sub size_allocate { 2008sub size_allocate {
1733 my ($self, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1734 2010
1735 $self->SUPER::size_allocate ($w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1736 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1737 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1738 $self->{layout}->set_width ($self->{children}[0]{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1739 2016
1740 $self->reflow; 2017 $self->reflow;
1741} 2018}
1793 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1794 2071
1795 my $y = 0; 2072 my $y = 0;
1796 2073
1797 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1798 2077
1799 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1800 my $h = $par->[0]; 2079 my $h = $par->[0];
1801 2080
1802 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
1882 my $class = shift; 2161 my $class = shift;
1883 2162
1884 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
1885 state => 0, 2164 state => 0,
1886 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
1887 @_ 2167 @_
1888 ); 2168 );
1889 2169
1890 if ($self->{state}) { 2170 if ($self->{state}) {
1891 $self->{state} = 0; 2171 $self->{state} = 0;
1911 $self->emit (changed => $self->{state}); 2191 $self->emit (changed => $self->{state});
1912} 2192}
1913 2193
1914############################################################################# 2194#############################################################################
1915 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
1916package CFClient::UI::Root; 2305package CFClient::UI::Root;
1917 2306
1918our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
1919 2308
1920use CFClient::OpenGL; 2309use CFClient::OpenGL;
1921 2310
1922sub check_size { 2311sub check_size {
1923 my ($self) = @_; 2312 my ($self) = @_;
1924 2313
1925 $self->configure (0, 0, $::WITH, $::HEIGHT); 2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
1926} 2315}
1927 2316
1928sub size_request { 2317sub size_request {
1929 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
1930} 2319}
1932sub configure { 2321sub configure {
1933 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
1934 2323
1935 $self->SUPER::configure ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
1936 2325
1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1938 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 }
1939} 2333}
1940 2334
1941sub _topleft { 2335sub _topleft {
1942 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
1943 2337
1951 ::refresh (); 2345 ::refresh ();
1952} 2346}
1953 2347
1954sub add { 2348sub add {
1955 my ($self, $child) = @_; 2349 my ($self, $child) = @_;
2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
1956 2354
1957 $self->SUPER::add ($child); 2355 $self->SUPER::add ($child);
1958} 2356}
1959 2357
1960sub on_refresh { 2358sub on_refresh {
1986############################################################################# 2384#############################################################################
1987 2385
1988package CFClient::UI; 2386package CFClient::UI;
1989 2387
1990$ROOT = new CFClient::UI::Root; 2388$ROOT = new CFClient::UI::Root;
2389$TOOLTIP = new CFClient::UI::Tooltip;
1991 2390
19921 23911
1993 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines