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.148 by elmex, Sat Apr 22 12:14:45 2006 UTC vs.
Revision 1.171 by root, Mon Apr 24 11:54:26 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->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
45 if $FOCUS;
18} 46}
19 47
20sub feed_sdl_key_up_event { 48sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS; 49 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
50 if $FOCUS;
22} 51}
23 52
24sub feed_sdl_button_down_event { 53sub feed_sdl_button_down_event {
25 my ($ev) = @_; 54 my ($ev) = @_;
26 my ($x, $y) = ($ev->{x}, $ev->{y}); 55 my ($x, $y) = ($ev->{x}, $ev->{y});
28 if (!$BUTTON_STATE) { 57 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 58 my $widget = $ROOT->find_widget ($x, $y);
30 59
31 $GRAB = $widget; 60 $GRAB = $widget;
32 $GRAB->update if $GRAB; 61 $GRAB->update if $GRAB;
62
63 check_tooltip;
33 } 64 }
34 65
35 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 66 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 67
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 68 if ($GRAB) {
69 ($x, $y) = $GRAB->coord2local ($x, $y);
70 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
71 }
38} 72}
39 73
40sub feed_sdl_button_up_event { 74sub feed_sdl_button_up_event {
41 my ($ev) = @_; 75 my ($ev) = @_;
42 my ($x, $y) = ($ev->{x}, $ev->{y}); 76 my ($x, $y) = ($ev->{x}, $ev->{y});
43 77
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 78 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 79
46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 80 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 81
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 82 if ($GRAB) {
83 ($x, $y) = $GRAB->coord2local ($x, $y);
84 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
85 }
49 86
50 if (!$BUTTON_STATE) { 87 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 88 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 89 $grab->update if $grab;
53 $GRAB->update if $GRAB; 90 $GRAB->update if $GRAB;
91
92 check_tooltip;
54 } 93 }
55} 94}
56 95
57sub feed_sdl_motion_event { 96sub feed_sdl_motion_event {
58 my ($ev) = @_; 97 my ($ev) = @_;
63 if ($widget != $HOVER) { 102 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 103 my $hover = $HOVER; $HOVER = $widget;
65 104
66 $hover->update if $hover && $hover->{can_hover}; 105 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 106 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 107
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 108 check_tooltip;
109 }
110
111 if ($HOVER) {
112 ($x, $y) = $HOVER->coord2local ($x, $y);
113 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
114 }
71} 115}
72 116
73# convert position array to integers 117# convert position array to integers
74sub harmonize { 118sub harmonize {
75 my ($vals) = @_; 119 my ($vals) = @_;
93 137
94sub new { 138sub new {
95 my $class = shift; 139 my $class = shift;
96 140
97 my $self = bless { 141 my $self = bless {
98 x => 0, 142 x => 0,
99 y => 0, 143 y => 0,
100 z => 0, 144 z => 0,
145 can_events => 1,
101 @_ 146 @_
102 }, $class; 147 }, $class;
103 148
104 for (keys %$self) { 149 for (keys %$self) {
105 if (/^connect_(.*)$/) { 150 if (/^connect_(.*)$/) {
108 } 153 }
109 154
110 $self 155 $self
111} 156}
112 157
158sub destroy {
159 my ($self) = @_;
160
161 $self->hide;
162 %$self = ();
163}
164
113sub show { 165sub show {
114 my ($self) = @_; 166 my ($self) = @_;
115 167
116 return if $self->{parent}; 168 return if $self->{parent};
117 169
119} 171}
120 172
121sub hide { 173sub hide {
122 my ($self) = @_; 174 my ($self) = @_;
123 175
124 return unless $self->{parent}; 176 undef $GRAB if $GRAB == $self;
177 undef $HOVER if $HOVER == $self;
125 178
126 $self->{parent}->remove ($self); 179 $self->{parent}->remove ($self)
180 if $self->{parent};
127} 181}
128 182
129sub move { 183sub move {
130 my ($self, $x, $y, $z) = @_; 184 my ($self, $x, $y, $z) = @_;
131 185
134 $self->{z} = $z if defined $z; 188 $self->{z} = $z if defined $z;
135 189
136 $self->update; 190 $self->update;
137} 191}
138 192
139sub needs_redraw { 193sub set_size {
140 0 194 my ($self, $w, $h) = @_;
195
196 $self->{user_w} = $w;
197 $self->{user_h} = $h;
198
199 $self->check_size;
141} 200}
142 201
143sub size_request { 202sub size_request {
144 require Carp; 203 require Carp;
145 Carp::confess "size_request is abstract"; 204 Carp::confess "size_request is abstract";
177 236
178sub size_allocate { 237sub size_allocate {
179 # nothing to be done 238 # nothing to be done
180} 239}
181 240
241sub set_max_size {
242 my ($self, $w, $h) = @_;
243
244 delete $self->{max_w}; $self->{max_w} = $w if $w;
245 delete $self->{max_h}; $self->{max_h} = $h if $h;
246}
247
182# return top left coordinates 248# return top left coordinates
183sub _topleft { 249sub _topleft {
184 my ($self, $x, $y) = @_; 250 my ($self, $x, $y) = @_;
251
252 $self->{parent}
253 or Carp::confess "no parent widget in _topleft\n";#d#
185 254
186 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 255 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
187} 256}
188 257
189# translate global coordinates to local coordinate system 258# translate global coordinates to local coordinate system
267 glVertex $x + $self->{w}, $y + $self->{h}; 336 glVertex $x + $self->{w}, $y + $self->{h};
268 glVertex $x , $y + $self->{h}; 337 glVertex $x , $y + $self->{h};
269 glEnd; 338 glEnd;
270 glDisable GL_BLEND; 339 glDisable GL_BLEND;
271 } 340 }
341
342 if ($ENV{PCLIENT_DEBUG}) {
343 glPushMatrix;
344 glColor 1, 1, 0, 1;
345 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
346 glBegin GL_LINE_LOOP;
347 glVertex 0 , 0;
348 glVertex $self->{w}, 0;
349 glVertex $self->{w}, $self->{h};
350 glVertex 0 , $self->{h};
351 glEnd;
352 glPopMatrix;
353 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
354 }
272} 355}
273 356
274sub _draw { 357sub _draw {
275 my ($self) = @_; 358 my ($self) = @_;
276 359
277 warn "no draw defined for $self\n"; 360 warn "no draw defined for $self\n";
278} 361}
279 362
280sub find_widget { 363sub find_widget {
281 my ($self, $x, $y) = @_; 364 my ($self, $x, $y) = @_;
365
366 return () unless $self->{can_events};
282 367
283 return $self 368 return $self
284 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 369 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
285 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 370 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
286 371
294} 379}
295 380
296sub check_size { 381sub check_size {
297 my ($self) = @_; 382 my ($self) = @_;
298 383
299 my ($w, $h) = $self->size_request; 384 $self->{parent}
385 or return 1;
300 386
387 my ($w, $h) = $self->{user_w} && $self->{user_h}
388 ? @$self{qw(user_w user_h)}
389 : $self->size_request;
390
301 if ($w != $self->{req_w} || $h != $self->{req_h}) { 391 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w; 392 $self->{req_w} = $w;
303 $self->{req_h} = $h; 393 $self->{req_h} = $h;
304 394
305 $self->{parent}->check_size 395 $self->{parent}->check_size
306 if $self->{parent}; 396 or $self->size_allocate (
397 (List::Util::max $self->{w}, $w),
398 (List::Util::max $self->{h}, $h),
399 );
400
401 1
402 } else {
403 0
307 } 404 }
308} 405}
309 406
310sub update { 407sub update {
311 my ($self) = @_; 408 my ($self) = @_;
321} 418}
322 419
323sub emit { 420sub emit {
324 my ($self, $signal, @args) = @_; 421 my ($self, $signal, @args) = @_;
325 422
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 423 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
327 $cb->($self, @args);
328 }
329} 424}
330 425
331sub DESTROY { 426sub DESTROY {
332 my ($self) = @_; 427 my ($self) = @_;
333 428
378 473
379package CFClient::UI::Empty; 474package CFClient::UI::Empty;
380 475
381our @ISA = CFClient::UI::Base::; 476our @ISA = CFClient::UI::Base::;
382 477
478sub new {
479 my ($class, %arg) = @_;
480 $class->SUPER::new (can_events => 0, %arg);
481}
482
383sub size_request { 483sub size_request {
384 (0, 0) 484 (0, 0)
385} 485}
386 486
387sub draw { } 487sub draw { }
395sub new { 495sub new {
396 my ($class, %arg) = @_; 496 my ($class, %arg) = @_;
397 497
398 my $children = delete $arg{children} || []; 498 my $children = delete $arg{children} || [];
399 499
400 my $self = $class->SUPER::new (children => [], %arg); 500 my $self = $class->SUPER::new (
501 children => [],
502 can_events => 0,
503 %arg,
504 );
401 $self->add ($_) for @$children; 505 $self->add ($_) for @$children;
402 506
403 $self 507 $self
404} 508}
405 509
420 524
421sub remove { 525sub remove {
422 my ($self, $child) = @_; 526 my ($self, $child) = @_;
423 527
424 delete $child->{parent}; 528 delete $child->{parent};
529 $child->hide;
425 530
426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 531 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
427 532
428 $self->check_size; 533 $self->check_size;
534 $self->update;
535}
536
537sub clear {
538 my ($self) = @_;
539
540 my $children = delete $self->{children};
541 $self->{children} = [];
542
543 for (@$children) {
544 delete $_->{parent};
545 $_->hide;
546 }
429} 547}
430 548
431sub find_widget { 549sub find_widget {
432 my ($self, $x, $y) = @_; 550 my ($self, $x, $y) = @_;
433 551
561sub new { die } 679sub new { die }
562 680
563sub size_request { 681sub size_request {
564 my ($self) = @_; 682 my ($self) = @_;
565 683
566 @$self{qw(child_w child_h)} = $self->child->size_request; 684 @$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)}); 685 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568 686
569 @$self{qw(child_w child_h)} 687 @$self{qw(child_w child_h)}
570} 688}
571 689
593 border => 0.8, 711 border => 0.8,
594 @_ 712 @_
595 ); 713 );
596 714
597 $self 715 $self
598}
599
600sub set_size {
601 my ($self, $w, $h) = @_;
602 $self->{req_w} = $w;
603 $self->{req_h} = $h;
604 $self->check_size;
605}
606
607sub size_request {
608 my ($self) = @_;
609 ($self->{req_w}, $self->{req_h})
610}
611
612sub size_allocate {
613 my ($self, $w, $h) = @_;
614 $self->{w} = $w;
615 $self->{h} = $h;
616 $self->child->configure (0, 0, $w, $h);
617} 716}
618 717
619sub _draw { 718sub _draw {
620 my ($self) = @_; 719 my ($self) = @_;
621 720
656 my $class = shift; 755 my $class = shift;
657 756
658 # TODO: user_x, user_y, overwrite moveto? 757 # TODO: user_x, user_y, overwrite moveto?
659 758
660 my $self = $class->SUPER::new ( 759 my $self = $class->SUPER::new (
661 bg => [1, 1, 1, 1], 760 bg => [1, 1, 1, 1],
662 border_bg => [1, 1, 1, 1], 761 border_bg => [1, 1, 1, 1],
663 border => 0.8, 762 border => 0.8,
763 can_events => 1,
664 @_ 764 @_
665 ); 765 );
666 766
667 $self->{title} &&= new CFClient::UI::Label 767 $self->{title} &&= new CFClient::UI::Label
668 align => 0, 768 align => 0,
678} 778}
679 779
680sub size_request { 780sub size_request {
681 my ($self) = @_; 781 my ($self) = @_;
682 782
683 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
684
685 my ($w, $h) = $self->SUPER::size_request; 783 my ($w, $h) = $self->SUPER::size_request;
686 784
687 ( 785 (
688 $w + $self->border * 2, 786 $w + $self->border * 2,
689 $h + $self->border * 2, 787 $h + $self->border * 2,
821 919
822# TODO: move to container class maybe? send childs a signal on removal? 920# TODO: move to container class maybe? send childs a signal on removal?
823sub clear { 921sub clear {
824 my ($self) = @_; 922 my ($self) = @_;
825 923
826 delete $self->{children}; 924 my $children = delete $self->{children};
925
926 for (grep $_, map @$_, grep $_, @$children) {
927 delete $_->{parent};
928 $_->hide;
929 }
930
827 $self->update; 931 $self->update;
828} 932}
829 933
830sub get_wh { 934sub get_wh {
831 my ($self) = @_; 935 my ($self) = @_;
837 or next; 941 or next;
838 942
839 for my $x (0 .. $#$row) { 943 for my $x (0 .. $#$row) {
840 my $widget = $row->[$x] 944 my $widget = $row->[$x]
841 or next; 945 or next;
842 my ($w, $h) = $widget->size_request; 946 my ($w, $h) = @$widget{qw(req_w req_h)};
843 947
844 $w[$x] = max $w[$x], $w; 948 $w[$x] = max $w[$x], $w;
845 $h[$y] = max $h[$y], $h; 949 $h[$y] = max $h[$y], $h;
846 } 950 }
847 } 951 }
952 1056
953 ($h, $w) = ($w, $h); 1057 ($h, $w) = ($w, $h);
954 1058
955 my $children = $self->{children}; 1059 my $children = $self->{children};
956 1060
957 my @h = map +($_->size_request)[0], @$children; 1061 my @h = map $_->{req_w}, @$children;
958 1062
959 my $req_h = List::Util::sum @h; 1063 my $req_h = List::Util::sum @h;
960 1064
961 if ($req_h > $h) { 1065 if ($req_h > $h) {
962 # ah well, not enough space 1066 # ah well, not enough space
1010sub size_allocate { 1114sub size_allocate {
1011 my ($self, $w, $h) = @_; 1115 my ($self, $w, $h) = @_;
1012 1116
1013 my $children = $self->{children}; 1117 my $children = $self->{children};
1014 1118
1015 my @h = map +($_->size_request)[1], @$children; 1119 my @h = map $_->{req_h}, @$children;
1016 1120
1017 my $req_h = List::Util::sum @h; 1121 my $req_h = List::Util::sum @h;
1018 1122
1019 if ($req_h > $h) { 1123 if ($req_h > $h) {
1020 # ah well, not enough space 1124 # ah well, not enough space
1054 1158
1055sub new { 1159sub new {
1056 my ($class, %arg) = @_; 1160 my ($class, %arg) = @_;
1057 1161
1058 my $self = $class->SUPER::new ( 1162 my $self = $class->SUPER::new (
1059 fg => [1, 1, 1], 1163 fg => [1, 1, 1],
1164 #font => default_font
1060 fontsize => 1, 1165 fontsize => 1,
1061 text => "", 1166 text => "",
1062 align => -1, 1167 align => -1,
1063 valign => -1, 1168 valign => -1,
1064 padding => 2, 1169 padding => 2,
1065 layout => new CFClient::Layout, 1170 layout => new CFClient::Layout,
1171 can_events => 0,
1066 %arg 1172 %arg
1067 ); 1173 );
1068 1174
1069 if (exists $self->{template}) { 1175 if (exists $self->{template}) {
1070 my $layout = new CFClient::Layout; 1176 my $layout = new CFClient::Layout;
1076 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1182 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1077 1183
1078 $self 1184 $self
1079} 1185}
1080 1186
1081sub escape_text { 1187sub escape {
1082 local $_ = $_[1]; 1188 local $_ = $_[1];
1083 1189
1084 s/&/&amp;/g; 1190 s/&/&amp;/g;
1085 s/>/&gt;/g; 1191 s/>/&gt;/g;
1086 s/</&lt;/g; 1192 s/</&lt;/g;
1092 my ($self, $text) = @_; 1198 my ($self, $text) = @_;
1093 1199
1094 $self->{layout}->set_text ($text); 1200 $self->{layout}->set_text ($text);
1095 1201
1096 delete $self->{texture}; 1202 delete $self->{texture};
1203 $self->check_size;
1097 $self->update; 1204 $self->update;
1098} 1205}
1099 1206
1100sub set_markup { 1207sub set_markup {
1101 my ($self, $markup) = @_; 1208 my ($self, $markup) = @_;
1102 1209
1103 $self->{layout}->set_markup ($markup); 1210 $self->{layout}->set_markup ($markup);
1104 1211
1105 delete $self->{texture}; 1212 delete $self->{texture};
1213 $self->check_size;
1106 $self->update; 1214 $self->update;
1107} 1215}
1108 1216
1109sub size_request { 1217sub size_request {
1110 my ($self) = @_; 1218 my ($self) = @_;
1111 1219
1112 $self->{layout}->set_width; 1220 $self->{layout}->set_font ($self->{font}) if $self->{font};
1221 $self->{layout}->set_width ($self->{max_w} || -1);
1113 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1222 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1114 1223
1115 my ($w, $h) = $self->{layout}->size; 1224 my ($w, $h) = $self->{layout}->size;
1116 1225
1117 if (exists $self->{template}) { 1226 if (exists $self->{template}) {
1227 $self->{template}->set_font ($self->{font}) if $self->{font};
1118 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1228 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1119 1229
1120 my ($w2, $h2) = $self->{template}->size; 1230 my ($w2, $h2) = $self->{template}->size;
1121 1231
1122 $w = List::Util::max $w, $w2; 1232 $w = List::Util::max $w, $w2;
1137 1247
1138sub set_fontsize { 1248sub set_fontsize {
1139 my ($self, $fontsize) = @_; 1249 my ($self, $fontsize) = @_;
1140 1250
1141 $self->{fontsize} = $fontsize; 1251 $self->{fontsize} = $fontsize;
1252 delete $self->{texture};
1142 $self->check_size; 1253 $self->check_size;
1254 $self->update;
1143} 1255}
1144 1256
1145sub _draw { 1257sub _draw {
1146 my ($self) = @_; 1258 my ($self) = @_;
1147 1259
1148 my $tex = $self->{texture} ||= do { 1260 my $tex = $self->{texture} ||= do {
1261 $self->{layout}->set_font ($self->{font}) if $self->{font};
1149 $self->{layout}->set_width ($self->{w}); 1262 $self->{layout}->set_width ($self->{w});
1150 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1263 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1151 new_from_layout CFClient::Texture $self->{layout} 1264 new_from_layout CFClient::Texture $self->{layout}
1152 }; 1265 };
1153 1266
1186 1299
1187sub new { 1300sub new {
1188 my $class = shift; 1301 my $class = shift;
1189 1302
1190 $class->SUPER::new ( 1303 $class->SUPER::new (
1191 fg => [1, 1, 1], 1304 fg => [1, 1, 1],
1192 bg => [0, 0, 0, 0.2], 1305 bg => [0, 0, 0, 0.2],
1193 active_bg => [1, 1, 1, 0.5], 1306 active_bg => [1, 1, 1, 0.5],
1194 active_fg => [0, 0, 0], 1307 active_fg => [0, 0, 0],
1195 can_hover => 1, 1308 can_hover => 1,
1196 can_focus => 1, 1309 can_focus => 1,
1197 valign => 0, 1310 valign => 0,
1311 can_events => 1,
1198 @_ 1312 @_
1199 ) 1313 )
1200} 1314}
1201 1315
1202sub _set_text { 1316sub _set_text {
1230} 1344}
1231 1345
1232sub size_allocate { 1346sub size_allocate {
1233 my ($self, $w, $h) = @_; 1347 my ($self, $w, $h) = @_;
1234 1348
1235 $self->_set_text ($self->{text}); 1349 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1236} 1350}
1237 1351
1238sub set_text { 1352sub set_text {
1239 my ($self, $text) = @_; 1353 my ($self, $text) = @_;
1240 1354
1355 my ($self, $ev) = @_; 1469 my ($self, $ev) = @_;
1356 1470
1357 my $sym = $ev->{sym}; 1471 my $sym = $ev->{sym};
1358 1472
1359 if ($sym == 13) { 1473 if ($sym == 13) {
1474 unshift @{$self->{history}},
1475 my $txt = $self->get_text;
1476 $self->{history_pointer} = -1;
1477 $self->{history_saveback} = '';
1360 $self->emit (activate => $self->get_text); 1478 $self->emit (activate => $txt);
1361 $self->update; 1479 $self->update;
1480
1481 } elsif ($sym == CFClient::SDLK_UP) {
1482 if ($self->{history_pointer} < 0) {
1483 $self->{history_saveback} = $self->get_text;
1484 }
1485 if (@{$self->{history} || []} > 0) {
1486 $self->{history_pointer}++;
1487 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1488 $self->{history_pointer} = @{$self->{history} || []} - 1;
1489 }
1490 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1491 }
1492
1493 } elsif ($sym == CFClient::SDLK_DOWN) {
1494 $self->{history_pointer}--;
1495 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1496
1497 if ($self->{history_pointer} >= 0) {
1498 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1499 } else {
1500 $self->set_text ($self->{history_saveback});
1501 }
1362 1502
1363 } else { 1503 } else {
1364 $self->SUPER::key_down ($ev); 1504 $self->SUPER::key_down ($ev);
1365 } 1505 }
1366 1506
1380 1520
1381sub new { 1521sub new {
1382 my $class = shift; 1522 my $class = shift;
1383 1523
1384 $class->SUPER::new ( 1524 $class->SUPER::new (
1385 padding => 4, 1525 padding => 4,
1386 fg => [1, 1, 1], 1526 fg => [1, 1, 1],
1387 bg => [1, 1, 1, 0.2], 1527 bg => [1, 1, 1, 0.2],
1388 active_fg => [0, 0, 1], 1528 active_fg => [0, 0, 1],
1389 can_hover => 1, 1529 can_hover => 1,
1390 align => 0, 1530 align => 0,
1391 valign => 0, 1531 valign => 0,
1532 can_events => 1,
1392 @_ 1533 @_
1393 ) 1534 )
1394} 1535}
1395 1536
1396sub button_up { 1537sub button_up {
1501our %loaded_images; 1642our %loaded_images;
1502 1643
1503sub new { 1644sub new {
1504 my $class = shift; 1645 my $class = shift;
1505 1646
1506 my $self = $class->SUPER::new (@_); 1647 my $self = $class->SUPER::new (can_events => 0, @_);
1507 1648
1508 $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; 1649 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1509 1650
1510 $loaded_images{$self->{image}} ||= 1651 $loaded_images{$self->{image}} ||=
1511 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; 1652 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1553############################################################################# 1694#############################################################################
1554 1695
1555package CFClient::UI::VGauge; 1696package CFClient::UI::VGauge;
1556 1697
1557our @ISA = CFClient::UI::Base::; 1698our @ISA = CFClient::UI::Base::;
1699
1700use List::Util qw(min max);
1558 1701
1559use CFClient::OpenGL; 1702use CFClient::OpenGL;
1560 1703
1561my %tex = ( 1704my %tex = (
1562 food => [ 1705 food => [
1563 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1706 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1564 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1707 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1565 ], 1708 ],
1566 grace => [ 1709 grace => [
1567 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1710 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1568 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1711 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1569 ], 1712 ],
1570 hp => [ 1713 hp => [
1571 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1714 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1572 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1715 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1573 ], 1716 ],
1574 mana => [ 1717 mana => [
1575 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1718 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1576 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1719 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1577 ], 1720 ],
1578); 1721);
1579 1722
1580# eg. VGauge->new (gauge => 'food'), default gauge: food 1723# eg. VGauge->new (gauge => 'food'), default gauge: food
1581sub new { 1724sub new {
1619 1762
1620sub _draw { 1763sub _draw {
1621 my ($self) = @_; 1764 my ($self) = @_;
1622 1765
1623 my $tex = $tex{$self->{type}}; 1766 my $tex = $tex{$self->{type}};
1767 my ($t1, $t2, $t3) = @$tex;
1624 1768
1625 my ($w, $h) = ($self->{w}, $self->{h}); 1769 my ($w, $h) = ($self->{w}, $self->{h});
1626 1770
1627 if ($self->{vertical}) { 1771 if ($self->{vertical}) {
1628 glRotate 90, 0, 0, 1; 1772 glRotate 90, 0, 0, 1;
1630 1774
1631 ($w, $h) = ($h, $w); 1775 ($w, $h) = ($h, $w);
1632 } 1776 }
1633 1777
1634 my $ycut = $self->{val} / ($self->{max_val} || 1); 1778 my $ycut = $self->{val} / ($self->{max_val} || 1);
1635 $ycut = 1 if $self->{val} > $self->{max_val};
1636 1779
1637 my $t1 = $tex->[0]; 1780 my $ycut1 = max 0, min 1, $ycut;
1638 my $t2 = $tex->[1]; 1781 my $ycut2 = max 0, min 1, $ycut - 1;
1782
1783 my $h1 = $self->{h} * (1 - $ycut1);
1784 my $h2 = $self->{h} * (1 - $ycut2);
1639 1785
1640 glEnable GL_BLEND; 1786 glEnable GL_BLEND;
1641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1787 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1642 glEnable GL_TEXTURE_2D; 1788 glEnable GL_TEXTURE_2D;
1643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1789 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1644 1790
1645 my $h1 = $self->{h} - $ycut * $self->{h};
1646 my $h2 = $ycut * $self->{h};
1647
1648 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1791 glBindTexture GL_TEXTURE_2D, $t1->{name};
1649 glBegin GL_QUADS; 1792 glBegin GL_QUADS;
1650 glTexCoord 0 , 0; glVertex 0 , 0; 1793 glTexCoord 0 , 0; glVertex 0 , 0;
1651 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1794 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1652 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1795 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1653 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1796 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1654 glEnd; 1797 glEnd;
1655 1798
1799 my $ycut1 = List::Util::min 1, $ycut;
1656 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1800 glBindTexture GL_TEXTURE_2D, $t2->{name};
1657 glBegin GL_QUADS; 1801 glBegin GL_QUADS;
1658 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1802 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1659 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1803 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1660 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1804 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1661 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1805 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1662 glEnd; 1806 glEnd;
1807
1808 if ($t3) {
1809 glBindTexture GL_TEXTURE_2D, $t3->{name};
1810 glBegin GL_QUADS;
1811 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1812 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1813 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1814 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1815 glEnd;
1816 }
1663 1817
1664 glDisable GL_BLEND; 1818 glDisable GL_BLEND;
1665 glDisable GL_TEXTURE_2D; 1819 glDisable GL_TEXTURE_2D;
1666} 1820}
1667 1821
1670package CFClient::UI::Gauge; 1824package CFClient::UI::Gauge;
1671 1825
1672our @ISA = CFClient::UI::VBox::; 1826our @ISA = CFClient::UI::VBox::;
1673 1827
1674sub new { 1828sub new {
1675 my ($class, %arg) = shift; 1829 my ($class, %arg) = @_;
1676 1830
1677 my $self = $class->SUPER::new ( 1831 my $self = $class->SUPER::new (
1678 @_, 1832 tooltip => $arg{type},
1833 can_hover => 1,
1834 can_events => 1,
1835 %arg,
1679 ); 1836 );
1680 1837
1681 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1838 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1682 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1839 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1683 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1840 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1684 1841
1685 $self 1842 $self
1686} 1843}
1687 1844
1688sub set_fontsize { 1845sub set_fontsize {
1854 2011
1855sub new { 2012sub new {
1856 my $class = shift; 2013 my $class = shift;
1857 2014
1858 my $self = $class->SUPER::new ( 2015 my $self = $class->SUPER::new (
1859 fontsize => 1, 2016 fontsize => 1,
2017 can_events => 0,
2018 #font => default_font
1860 @_, 2019 @_,
1861 2020
1862 layout => (new CFClient::Layout), 2021 layout => (new CFClient::Layout),
1863 par => [], 2022 par => [],
1864 height => 0, 2023 height => 0,
1865 children => [ 2024 children => [
1866 (new CFClient::UI::Empty expand => 1), 2025 (new CFClient::UI::Empty expand => 1),
1867 (new CFClient::UI::Slider vertical => 1), 2026 (new CFClient::UI::Slider vertical => 1),
1868 ], 2027 ],
1869 ); 2028 );
1870 2029
1904sub size_allocate { 2063sub size_allocate {
1905 my ($self, $w, $h) = @_; 2064 my ($self, $w, $h) = @_;
1906 2065
1907 $self->SUPER::size_allocate ($w, $h); 2066 $self->SUPER::size_allocate ($w, $h);
1908 2067
2068 $self->{layout}->set_font ($self->{font}) if $self->{font};
1909 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2069 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1910 $self->{layout}->set_width ($self->{children}[0]{w}); 2070 $self->{layout}->set_width ($self->{children}[0]{w});
1911 2071
1912 $self->reflow; 2072 $self->reflow;
1913} 2073}
1965 my $y1 = $top + $self->{h}; 2125 my $y1 = $top + $self->{h};
1966 2126
1967 my $y = 0; 2127 my $y = 0;
1968 2128
1969 my $layout = $self->{layout}; 2129 my $layout = $self->{layout};
2130
2131 $layout->set_font ($self->{font}) if $self->{font};
1970 2132
1971 for my $par (@{$self->{par}}) { 2133 for my $par (@{$self->{par}}) {
1972 my $h = $par->[0]; 2134 my $h = $par->[0];
1973 2135
1974 if ($y0 < $y + $h && $y < $y1) { 2136 if ($y0 < $y + $h && $y < $y1) {
2052 2214
2053sub new { 2215sub new {
2054 my $class = shift; 2216 my $class = shift;
2055 2217
2056 my $self = $class->SUPER::new ( 2218 my $self = $class->SUPER::new (
2057 state => 0, 2219 state => 0,
2058 connect_activate => \&toggle_flopper, 2220 connect_activate => \&toggle_flopper,
2059 @_ 2221 @_
2060 ); 2222 );
2061 2223
2062 if ($self->{state}) { 2224 if ($self->{state}) {
2083 $self->emit (changed => $self->{state}); 2245 $self->emit (changed => $self->{state});
2084} 2246}
2085 2247
2086############################################################################# 2248#############################################################################
2087 2249
2250package CFClient::UI::Tooltip;
2251
2252our @ISA = CFClient::UI::Bin::;
2253
2254use CFClient::OpenGL;
2255
2256sub new {
2257 my $class = shift;
2258
2259 $class->SUPER::new (
2260 @_,
2261 can_events => 0,
2262 )
2263}
2264
2265sub set_markup {
2266 my ($self, $text) = @_;
2267
2268 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2269 $self->{label}->set_markup ($text);
2270 $self->add ($self->{label});
2271}
2272
2273sub size_request {
2274 my ($self) = @_;
2275
2276 $self->child->set_max_size ($::WIDTH * 0.3);
2277
2278 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2279
2280 ($w + 4, $h + 4)
2281}
2282
2283sub size_allocate {
2284 my ($self, $w, $h) = @_;
2285
2286 $self->SUPER::size_allocate ($w - 4, $h - 4);
2287}
2288
2289sub _draw {
2290 my ($self) = @_;
2291
2292 glPushMatrix;
2293 glTranslate 0.375, 0.375;
2294
2295 my ($w, $h) = @$self{qw(w h)};
2296
2297 glColor 1, 0.8, 0.4;
2298 glBegin GL_QUADS;
2299 glVertex 0 , 0;
2300 glVertex 0 , $h;
2301 glVertex $w, $h;
2302 glVertex $w, 0;
2303 glEnd;
2304
2305 glColor 0, 0, 0;
2306 glBegin GL_LINE_LOOP;
2307 glVertex 0 , 0;
2308 glVertex 0 , $h;
2309 glVertex $w, $h;
2310 glVertex $w, 0;
2311 glEnd;
2312
2313 glPopMatrix;
2314
2315 glTranslate 2, 2;
2316 $self->SUPER::_draw;
2317}
2318
2319#############################################################################
2320
2321package CFClient::UI::Face;
2322
2323our @ISA = CFClient::UI::Base::;
2324
2325use CFClient::OpenGL;
2326
2327sub new {
2328 my $class = shift;
2329
2330 $class->SUPER::new (
2331 aspect => 1,
2332 @_,
2333 )
2334}
2335
2336sub size_request {
2337 (32, 8)
2338}
2339
2340sub draw {
2341 my ($self) = @_;
2342
2343 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2344
2345 if ($tex) {
2346 glEnable GL_BLEND;
2347 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2348 glEnable GL_TEXTURE_2D;
2349 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2350 glColor 1, 1, 1, 1;
2351 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2352 glDisable GL_TEXTURE_2D;
2353 glDisable GL_BLEND;
2354 }
2355}
2356
2357#############################################################################
2358
2088package CFClient::UI::Root; 2359package CFClient::UI::Root;
2089 2360
2090our @ISA = CFClient::UI::Container::; 2361our @ISA = CFClient::UI::Container::;
2091 2362
2092use CFClient::OpenGL; 2363use CFClient::OpenGL;
2093 2364
2094sub check_size { 2365sub check_size {
2095 my ($self) = @_; 2366 my ($self) = @_;
2096 2367
2097 $self->configure (0, 0, $::WITH, $::HEIGHT); 2368 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2098} 2369}
2099 2370
2100sub size_request { 2371sub size_request {
2101 ($::WIDTH, $::HEIGHT) 2372 ($::WIDTH, $::HEIGHT)
2102} 2373}
2104sub configure { 2375sub configure {
2105 my ($self, $x, $y, $w, $h) = @_; 2376 my ($self, $x, $y, $w, $h) = @_;
2106 2377
2107 $self->SUPER::configure ($x, $y, $w, $h); 2378 $self->SUPER::configure ($x, $y, $w, $h);
2108 2379
2109 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2110 for @{$self->{children}}; 2380 for my $child (@{$self->{children}}) {
2381 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2382
2383 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2384 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2385 $child->configure ($X, $Y, $W,$H);
2386 }
2111} 2387}
2112 2388
2113sub _topleft { 2389sub _topleft {
2114 my ($self, $x, $y) = @_; 2390 my ($self, $x, $y) = @_;
2115 2391
2162############################################################################# 2438#############################################################################
2163 2439
2164package CFClient::UI; 2440package CFClient::UI;
2165 2441
2166$ROOT = new CFClient::UI::Root; 2442$ROOT = new CFClient::UI::Root;
2443$TOOLTIP = new CFClient::UI::Tooltip;
2167 2444
21681 24451
2169 2446

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines