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.241 by root, Fri May 26 20:50:35 2006 UTC vs.
Revision 1.252 by root, Sun May 28 23:21:28 2006 UTC

9use CFClient; 9use CFClient;
10use CFClient::Texture; 10use CFClient::Texture;
11 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13 13
14our $LAYOUT;
14our $ROOT; 15our $ROOT;
15our $TOOLTIP; 16our $TOOLTIP;
16our $BUTTON_STATE; 17our $BUTTON_STATE;
17 18
18our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20
21sub get_layout {
22 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = {
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 };
29 }
30
31 return $LAYOUT;
32}
33
34sub set_layout {
35 my ($layout) = @_;
36 $LAYOUT = $layout;
37}
19 38
20sub check_tooltip { 39sub check_tooltip {
21 if (!$GRAB) { 40 if (!$GRAB) {
22 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
23 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
28 my $tip = $widget->{tooltip}; 47 my $tip = $widget->{tooltip};
29 48
30 $tip = $tip->($widget) if CODE:: eq ref $tip; 49 $tip = $tip->($widget) if CODE:: eq ref $tip;
31 50
32 $TOOLTIP->set_tooltip_from ($widget); 51 $TOOLTIP->set_tooltip_from ($widget);
33 $TOOLTIP->show;
34 52
35 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 53 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
36 54
37 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) 55 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
38 if $x + $TOOLTIP->{w} > $::WIDTH; 56 if $x + $TOOLTIP->{w} > $::WIDTH;
39 57
40 $TOOLTIP->move ($x, $y); 58 $TOOLTIP->move ($x, $y);
41 $TOOLTIP->check_size;
42 $TOOLTIP->update; 59 $TOOLTIP->show;
43 } 60 }
44 61
45 return; 62 return;
46 } 63 }
47 } 64 }
156 for my $widget (values %WIDGET) { 173 for my $widget (values %WIDGET) {
157 if ($widget->{is_toplevel}) { 174 if ($widget->{is_toplevel}) {
158 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 175 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
159 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 176 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
160 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 177 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
162 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 178 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
163 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 179 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
164 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 180 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
165 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
166 } 181 }
167 } 182 }
168 183
169 reconfigure_widgets; 184 reconfigure_widgets;
170} 185}
187 can_events => 1, 202 can_events => 1,
188 @_ 203 @_
189 }, $class; 204 }, $class;
190 205
191 for (keys %$self) { 206 for (keys %$self) {
192 if (/^connect_(.*)$/) { 207 if (/^on_(.*)$/) {
193 $self->connect ($1 => delete $self->{$_}); 208 $self->connect ($1 => delete $self->{$_});
194 } 209 }
195 } 210 }
196 211
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 212 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198 213
214 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
215 $self->{req_x} = $layout->{x} * $::WIDTH;
216 $self->{req_y} = $layout->{y} * $::HEIGHT;
217 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
218 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
219 }
220
199 $self 221 $self
200} 222}
201 223
202sub destroy { 224sub destroy {
203 my ($self) = @_; 225 my ($self) = @_;
206 %$self = (); 228 %$self = ();
207} 229}
208 230
209sub show { 231sub show {
210 my ($self) = @_; 232 my ($self) = @_;
233
211 return if $self->{parent}; 234 return if $self->{parent};
212 235
213 $CFClient::UI::ROOT->add ($self); 236 $CFClient::UI::ROOT->add ($self);
214} 237}
215 238
216sub show_centered { 239sub center {
217 my ($self) = @_; 240 my ($self) = @_;
218 return if $self->{parent};
219
220 $self->show;
221 241
222 $CFClient::UI::ROOT->on_post_alloc ( 242 $CFClient::UI::ROOT->on_post_alloc (
223 "centered $self" => sub { 243 "center_$self" => sub {
224 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 244 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
225 }, 245 },
226 ); 246 );
247
248 $self->update;
249}
250
251sub set_visible {
252 my ($self) = @_;
253
254 return if $self->{visible};
255
256 $self->{root} = $self->{parent}{root};
257 $self->{visible} = $self->{parent}{visible} + 1;
258
259 $self->emit (visibility_change => 1);
260
261 $self->realloc if !exists $self->{req_w};
262
263 $_->set_visible for $self->children;
227} 264}
228 265
229sub set_invisible { 266sub set_invisible {
230 my ($self) = @_; 267 my ($self) = @_;
231 268
232 # broken show/hide model 269 return unless $self->{visible};
270
271 $_->set_invisible for $self->children;
233 272
234 delete $self->{root}; 273 delete $self->{root};
235 delete $self->{visible}; 274 delete $self->{visible};
236 275
237 undef $GRAB if $GRAB == $self; 276 undef $GRAB if $GRAB == $self;
238 undef $HOVER if $HOVER == $self; 277 undef $HOVER if $HOVER == $self;
239 278
240 CFClient::UI::check_tooltip 279 CFClient::UI::check_tooltip
241 if $CFClient::UI::TOOLTIP->{owner} == $self; 280 if $TOOLTIP->{owner} == $self;
242 281
243 $self->focus_out; 282 $self->focus_out;
283
284 $self->emit (visibility_change => 0);
285}
286
287sub set_visibility {
288 my ($self, $visible) = @_;
289
290 return if $self->{visible} == $visible;
291
292 $visible ? $self->hide
293 : $self->show;
294}
295
296sub toggle_visibility {
297 my ($self) = @_;
298
299 $self->{visible}
300 ? $self->hide
301 : $self->show;
244} 302}
245 303
246sub hide { 304sub hide {
247 my ($self) = @_; 305 my ($self) = @_;
248 306
263} 321}
264 322
265sub set_size { 323sub set_size {
266 my ($self, $w, $h) = @_; 324 my ($self, $w, $h) = @_;
267 325
268 $self->{user_w} = $w; 326 $self->{def_w} = $w;
269 $self->{user_h} = $h; 327 $self->{def_h} = $h;
270 328
271 $self->check_size; 329 $self->realloc;
272} 330}
273 331
274sub size_request { 332sub size_request {
275 require Carp; 333 require Carp;
276 Carp::confess "size_request is abstract"; 334 Carp::confess "size_request is abstract";
296 $self->{y} = $y; 354 $self->{y} = $y;
297 $self->update; 355 $self->update;
298 } 356 }
299 357
300 if ($self->{w} != $w || $self->{h} != $h) { 358 if ($self->{w} != $w || $self->{h} != $h) {
359 return unless $self->{visible};
360
301 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 361 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
302 } 362 }
303} 363}
304 364
305sub size_allocate { 365sub size_allocate {
306 # nothing to be done 366 # nothing to be done
307}
308
309sub reconfigure {
310 my ($self) = @_;
311
312 $self->check_size (1);
313 $self->update;
314} 367}
315 368
316sub children { 369sub children {
317} 370}
318 371
396sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 449sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
397sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 450sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
398sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 451sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
399sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 452sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
400sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 453sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
454
455sub find_widget {
456 my ($self, $x, $y) = @_;
457
458 return () unless $self->{can_events};
459
460 return $self
461 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
462 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
463
464 ()
465}
466
467sub set_parent {
468 my ($self, $parent) = @_;
469
470 Scalar::Util::weaken ($self->{parent} = $parent);
471
472 $self->set_visible if $parent->{visible};
473}
474
475sub connect {
476 my ($self, $signal, $cb) = @_;
477
478 push @{ $self->{signal_cb}{$signal} }, $cb;
479}
480
481sub _emit {
482 my ($self, $signal, @args) = @_;
483
484 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
485}
486
487sub emit {
488 my ($self, $signal, @args) = @_;
489
490 $self->_emit ($signal, @args)
491 || $self->$signal (@args);
492}
493
494sub visibility_change {
495 #my ($self, $visible) = @_;
496}
497
498sub realloc {
499 my ($self) = @_;
500
501 if ($self->{visible}) {
502 return if $self->{root}{realloc}{$self};
503
504 $self->{root}{realloc}{$self} = $self;
505 $self->{root}->update;
506 } else {
507 delete $self->{req_w};
508 }
509}
510
511sub update {
512 my ($self) = @_;
513
514 $self->{parent}->update
515 if $self->{parent};
516}
401 517
402sub draw { 518sub draw {
403 my ($self) = @_; 519 my ($self) = @_;
404 520
405 return unless $self->{h} && $self->{w}; 521 return unless $self->{h} && $self->{w};
443 my ($self) = @_; 559 my ($self) = @_;
444 560
445 warn "no draw defined for $self\n"; 561 warn "no draw defined for $self\n";
446} 562}
447 563
448sub find_widget {
449 my ($self, $x, $y) = @_;
450
451 return () unless $self->{can_events};
452
453 return $self
454 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
455 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
456
457 ()
458}
459
460sub set_parent {
461 my ($self, $parent) = @_;
462
463 Scalar::Util::weaken ($self->{parent} = $parent);
464
465 $self->{root} = $parent->{root};
466 $self->{visible} = $parent->{visible} + 1;
467
468 # TODO: req_w _does_change after ->reconfigure
469 $self->check_size
470 unless exists $self->{req_w};
471
472 $self->show;
473}
474
475sub check_size {
476 my ($self, $forced) = @_;
477
478 $self->{force_alloc} = 1 if $forced;
479 $CFClient::UI::ROOT->{check_size}{$self} = $self;
480}
481
482sub update {
483 my ($self) = @_;
484
485 $self->{parent}->update
486 if $self->{parent};
487}
488
489sub connect {
490 my ($self, $signal, $cb) = @_;
491
492 push @{ $self->{signal_cb}{$signal} }, $cb;
493}
494
495sub _emit {
496 my ($self, $signal, @args) = @_;
497
498 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
499}
500
501sub emit {
502 my ($self, $signal, @args) = @_;
503
504 $self->_emit ($signal, @args)
505 || $self->$signal (@args);
506}
507
508sub DESTROY { 564sub DESTROY {
509 my ($self) = @_; 565 my ($self) = @_;
510 566
511 delete $WIDGET{$self+0}; 567 delete $WIDGET{$self+0};
512 #$self->deactivate; 568 #$self->deactivate;
607 $self->{children} = [ 663 $self->{children} = [
608 sort { $a->{z} <=> $b->{z} } 664 sort { $a->{z} <=> $b->{z} }
609 @{$self->{children}}, @widgets 665 @{$self->{children}}, @widgets
610 ]; 666 ];
611 667
612 $self->check_size (1); 668 $self->realloc;
613 $self->update;
614} 669}
615 670
616sub children { 671sub children {
617 @{ $_[0]{children} } 672 @{ $_[0]{children} }
618} 673}
623 delete $child->{parent}; 678 delete $child->{parent};
624 $child->hide; 679 $child->hide;
625 680
626 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 681 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
627 682
628 $self->check_size (1); 683 $self->realloc;
629 $self->update;
630} 684}
631 685
632sub clear { 686sub clear {
633 my ($self) = @_; 687 my ($self) = @_;
634 688
638 for (@$children) { 692 for (@$children) {
639 delete $_->{parent}; 693 delete $_->{parent};
640 $_->hide; 694 $_->hide;
641 } 695 }
642 696
643 $self->check_size; 697 $self->realloc;
644 $self->update;
645} 698}
646 699
647sub find_widget { 700sub find_widget {
648 my ($self, $x, $y) = @_; 701 my ($self, $x, $y) = @_;
649 702
702sub size_request { 755sub size_request {
703 $_[0]{children}[0]->size_request 756 $_[0]{children}[0]->size_request
704} 757}
705 758
706sub size_allocate { 759sub size_allocate {
707 my ($self, $w, $h) = @_; 760 my ($self, $w, $h, $changed) = @_;
708 761
709 $self->{children}[0]->configure (0, 0, $w, $h); 762 $self->{children}[0]->configure (0, 0, $w, $h);
710} 763}
711 764
712############################################################################# 765#############################################################################
729 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 782 $ROOT->on_post_alloc ($self => sub { $self->render_child });
730 $self->SUPER::update; 783 $self->SUPER::update;
731} 784}
732 785
733sub size_allocate { 786sub size_allocate {
734 my ($self, $w, $h) = @_; 787 my ($self, $w, $h, $changed) = @_;
735 788
736 $self->SUPER::size_allocate ($w, $h); 789 $self->SUPER::size_allocate ($w, $h, $changed);
737 $self->update; 790 $self->update
791 if $changed;
738} 792}
739 793
740sub _render { 794sub _render {
741 $_[0]{children}[0]->draw; 795 $_[0]{children}[0]->draw;
742} 796}
786} 840}
787 841
788sub size_request { 842sub size_request {
789 my ($self) = @_; 843 my ($self) = @_;
790 844
791 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 845 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
792 846
793 @$self{qw(child_w child_h)} 847 $w = 10 if $self->{scroll_x};
848 $h = 10 if $self->{scroll_y};
849
850 ($w, $h)
794} 851}
795 852
796sub size_allocate { 853sub size_allocate {
797 my ($self, $w, $h) = @_; 854 my ($self, $w, $h, $changed) = @_;
798 855
799 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 856 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
800 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 857 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
801 858
802 $self->child->configure (0, 0, $w, $h); 859 $self->child->configure (0, 0, $w, $h);
858 my $class = shift; 915 my $class = shift;
859 916
860 my $self; 917 my $self;
861 918
862 my $slider = new CFClient::UI::Slider 919 my $slider = new CFClient::UI::Slider
863 vertical => 1, 920 vertical => 1,
864 range => [0, 0, 1, 0.01], # HACK fix 921 range => [0, 0, 1, 0.01], # HACK fix
865 connect_changed => sub { 922 on_changed => sub {
866 $self->{vp}->set_offset (0, $_[1]); 923 $self->{vp}->set_offset (0, $_[1]);
867 }, 924 },
868 ; 925 ;
869 926
870 $self = $class->SUPER::new ( 927 $self = $class->SUPER::new (
889 my $child = $self->{vp}->child; 946 my $child = $self->{vp}->child;
890 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 947 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
891} 948}
892 949
893sub size_allocate { 950sub size_allocate {
894 my ($self, $w, $h) = @_; 951 my ($self, $w, $h, $changed) = @_;
895 952
896 $self->SUPER::size_allocate ($w, $h); 953 $self->SUPER::size_allocate ($w, $h, $changed);
897 954
898 my $child = $self->{vp}->child; 955 my $child = $self->{vp}->child;
899 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 956 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
900} 957}
901 958
954 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1011 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
955 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1012 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
956 1013
957sub new { 1014sub new {
958 my $class = shift; 1015 my $class = shift;
959
960 # TODO: user_x, user_y, overwrite moveto?
961 1016
962 my $self = $class->SUPER::new ( 1017 my $self = $class->SUPER::new (
963 bg => [1, 1, 1, 1], 1018 bg => [1, 1, 1, 1],
964 border_bg => [1, 1, 1, 1], 1019 border_bg => [1, 1, 1, 1],
965 border => 0.6, 1020 border => 0.6,
991 $h + $self->border * 2, 1046 $h + $self->border * 2,
992 ) 1047 )
993} 1048}
994 1049
995sub size_allocate { 1050sub size_allocate {
996 my ($self, $w, $h) = @_; 1051 my ($self, $w, $h, $changed) = @_;
1052
1053 return unless $changed;
997 1054
998 $h -= List::Util::max 0, $self->border * 2; 1055 $h -= List::Util::max 0, $self->border * 2;
999 $w -= List::Util::max 0, $self->border * 2; 1056 $w -= List::Util::max 0, $self->border * 2;
1000 1057
1001 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1058 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1025 my ($ev, $x, $y) = @_; 1082 my ($ev, $x, $y) = @_;
1026 1083
1027 my $dx = $ev->{x} - $ox; 1084 my $dx = $ev->{x} - $ox;
1028 my $dy = $ev->{y} - $oy; 1085 my $dy = $ev->{y} - $oy;
1029 1086
1087 $self->{user_x} = $wx + $dx * $mx;
1088 $self->{user_y} = $wy + $dy * $my;
1030 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1089 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1031 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1090 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1032 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1091 $self->move ($self->{user_x}, $self->{user_y});
1033 $self->check_size; 1092 $self->realloc;
1034 }; 1093 };
1035 1094
1036 } elsif ($lr ^ $td) { 1095 } elsif ($lr ^ $td) {
1037 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1096 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1038 my ($bx, $by) = ($self->{x}, $self->{y}); 1097 my ($bx, $by) = ($self->{x}, $self->{y});
1040 $self->{motion} = sub { 1099 $self->{motion} = sub {
1041 my ($ev, $x, $y) = @_; 1100 my ($ev, $x, $y) = @_;
1042 1101
1043 ($x, $y) = ($ev->{x}, $ev->{y}); 1102 ($x, $y) = ($ev->{x}, $ev->{y});
1044 1103
1045 $self->move ($bx + $x - $ox, $by + $y - $oy); 1104 $self->{user_x} = $bx + $x - $ox;
1105 $self->{user_y} = $by + $y - $oy;
1106
1107 $self->move ($self->{user_x}, $self->{user_y});
1046 $self->update; 1108 $self->realloc;
1047 }; 1109 };
1048 } 1110 }
1049} 1111}
1050 1112
1051sub button_up { 1113sub button_up {
1126 my ($self, $x, $y, $child) = @_; 1188 my ($self, $x, $y, $child) = @_;
1127 1189
1128 $child->set_parent ($self); 1190 $child->set_parent ($self);
1129 $self->{children}[$y][$x] = $child; 1191 $self->{children}[$y][$x] = $child;
1130 1192
1131 $self->check_size (1); 1193 $self->realloc;
1132} 1194}
1133 1195
1134# TODO: move to container class maybe? send children a signal on removal? 1196# TODO: move to container class maybe? send children a signal on removal?
1135sub clear { 1197sub clear {
1136 my ($self) = @_; 1198 my ($self) = @_;
1141 for (@children) { 1203 for (@children) {
1142 delete $_->{parent}; 1204 delete $_->{parent};
1143 $_->hide; 1205 $_->hide;
1144 } 1206 }
1145 1207
1146 $self->check_size (1); 1208 $self->realloc;
1147 $self->update;
1148} 1209}
1149 1210
1150sub get_wh { 1211sub get_wh {
1151 my ($self) = @_; 1212 my ($self) = @_;
1152 1213
1179 (sum @$hs), 1240 (sum @$hs),
1180 ) 1241 )
1181} 1242}
1182 1243
1183sub size_allocate { 1244sub size_allocate {
1184 my ($self, $w, $h) = @_; 1245 my ($self, $w, $h, $changed) = @_;
1185 1246
1186 my ($ws, $hs) = $self->get_wh; 1247 my ($ws, $hs) = $self->get_wh;
1187 1248
1188 my $req_w = (sum @$ws) || 1; 1249 my $req_w = (sum @$ws) || 1;
1189 my $req_h = (sum @$hs) || 1; 1250 my $req_h = (sum @$hs) || 1;
1248 } 1309 }
1249} 1310}
1250 1311
1251############################################################################# 1312#############################################################################
1252 1313
1314package CFClient::UI::Box;
1315
1316our @ISA = CFClient::UI::Container::;
1317
1318sub size_request {
1319 my ($self) = @_;
1320
1321 $self->{vertical}
1322 ? (
1323 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1324 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1325 )
1326 : (
1327 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1328 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1329 )
1330}
1331
1332sub size_allocate {
1333 my ($self, $w, $h, $changed) = @_;
1334
1335 my $space = $self->{vertical} ? $h : $w;
1336 my $children = $self->{children};
1337
1338 my @req;
1339
1340 if ($self->{homogeneous}) {
1341 @req = ($space / (@$children || 1)) x @$children;
1342 } else {
1343 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1344 my $req = List::Util::sum @req;
1345
1346 if ($req > $space) {
1347 # ah well, not enough space
1348 $_ *= $space / $req for @req;
1349 } else {
1350 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1351
1352 $space = ($space - $req) / $expand; # remaining space to give away
1353
1354 $req[$_] += $space * $children->[$_]{expand}
1355 for 0 .. $#$children;
1356 }
1357 }
1358
1359 CFClient::UI::harmonize \@req;
1360
1361 my $pos = 0;
1362 for (0 .. $#$children) {
1363 my $alloc = $req[$_];
1364 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1365
1366 $pos += $alloc;
1367 }
1368
1369 1
1370}
1371
1372#############################################################################
1373
1253package CFClient::UI::HBox; 1374package CFClient::UI::HBox;
1254 1375
1255# TODO: wrap into common Box base class
1256
1257our @ISA = CFClient::UI::Container::; 1376our @ISA = CFClient::UI::Box::;
1258 1377
1259sub size_request { 1378sub new {
1260 my ($self) = @_; 1379 my $class = shift;
1261 1380
1262 my @alloc = map [$_->size_request], @{$self->{children}}; 1381 $class->SUPER::new (
1263 1382 vertical => 0,
1264 ( 1383 @_,
1265 (List::Util::sum map $_->[0], @alloc),
1266 (List::Util::max map $_->[1], @alloc),
1267 ) 1384 )
1268} 1385}
1269 1386
1270sub size_allocate {
1271 my ($self, $w, $h) = @_;
1272
1273 ($h, $w) = ($w, $h);
1274
1275 my $children = $self->{children};
1276
1277 my @h = map $_->{req_w}, @$children;
1278
1279 my $req_h = List::Util::sum @h;
1280
1281 if ($req_h > $h) {
1282 # ah well, not enough space
1283 $_ *= $h / $req_h for @h;
1284 } else {
1285 my $exp = List::Util::sum map $_->{expand}, @$children;
1286 $exp ||= 1;
1287
1288 for (0 .. $#$children) {
1289 my $child = $children->[$_];
1290
1291 my $alloc_h = $h[$_];
1292 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1293 $h[$_] = $alloc_h;
1294 }
1295 }
1296
1297 CFClient::UI::harmonize \@h;
1298
1299 my $y = 0;
1300 for (0 .. $#$children) {
1301 my $child = $children->[$_];
1302 my $h = $h[$_];
1303 $child->configure ($y, 0, $h, $w);
1304
1305 $y += $h;
1306 }
1307
1308 1
1309}
1310
1311############################################################################# 1387#############################################################################
1312 1388
1313package CFClient::UI::VBox; 1389package CFClient::UI::VBox;
1314 1390
1315# TODO: wrap into common Box base class
1316
1317our @ISA = CFClient::UI::Container::; 1391our @ISA = CFClient::UI::Box::;
1318 1392
1319sub size_request { 1393sub new {
1320 my ($self) = @_; 1394 my $class = shift;
1321 1395
1322 my @alloc = map [$_->size_request], @{$self->{children}}; 1396 $class->SUPER::new (
1323 1397 vertical => 1,
1324 ( 1398 @_,
1325 (List::Util::max map $_->[0], @alloc),
1326 (List::Util::sum map $_->[1], @alloc),
1327 ) 1399 )
1328}
1329
1330sub size_allocate {
1331 my ($self, $w, $h) = @_;
1332
1333 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1334
1335 my $children = $self->{children};
1336
1337 my @h = map $_->{req_h}, @$children;
1338
1339 my $req_h = List::Util::sum @h;
1340
1341 if ($req_h > $h) {
1342 # ah well, not enough space
1343 $_ *= $h / $req_h for @h;
1344 } else {
1345 my $exp = List::Util::sum map $_->{expand}, @$children;
1346 $exp ||= 1;
1347
1348 for (0 .. $#$children) {
1349 my $child = $children->[$_];
1350
1351 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1352 }
1353 }
1354
1355 CFClient::UI::harmonize \@h;
1356
1357 my $y = 0;
1358 for (0 .. $#$children) {
1359 my $child = $children->[$_];
1360 my $h = $h[$_];
1361 $child->configure (0, $y, $w, $h);
1362
1363 $y += $h;
1364 }
1365
1366 1
1367} 1400}
1368 1401
1369############################################################################# 1402#############################################################################
1370 1403
1371package CFClient::UI::Label; 1404package CFClient::UI::Label;
1434 $self->{text} = "T$text"; 1467 $self->{text} = "T$text";
1435 1468
1436 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1469 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1437 $self->{layout}->set_text ($text); 1470 $self->{layout}->set_text ($text);
1438 1471
1472 $self->realloc;
1439 $self->update; 1473 $self->update;
1440 $self->check_size;
1441} 1474}
1442 1475
1443sub set_markup { 1476sub set_markup {
1444 my ($self, $markup) = @_; 1477 my ($self, $markup) = @_;
1445 1478
1449 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1482 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1450 1483
1451 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1484 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1452 $self->{layout}->set_markup ($markup); 1485 $self->{layout}->set_markup ($markup);
1453 1486
1487 $self->realloc;
1454 $self->update; 1488 $self->update;
1455 $self->check_size;
1456} 1489}
1457 1490
1458sub size_request { 1491sub size_request {
1459 my ($self) = @_; 1492 my ($self) = @_;
1460 1493
1481 $h + $self->{padding} * 2, 1514 $h + $self->{padding} * 2,
1482 ) 1515 )
1483} 1516}
1484 1517
1485sub size_allocate { 1518sub size_allocate {
1486 my ($self, $w, $h) = @_; 1519 my ($self, $w, $h, $changed) = @_;
1487 1520
1488 delete $self->{texture}; 1521 delete $self->{texture}
1522 if $changed;
1489} 1523}
1490 1524
1491sub set_fontsize { 1525sub set_fontsize {
1492 my ($self, $fontsize) = @_; 1526 my ($self, $fontsize) = @_;
1493 1527
1494 $self->{fontsize} = $fontsize; 1528 $self->{fontsize} = $fontsize;
1495 delete $self->{texture}; 1529 delete $self->{texture};
1496 1530
1497 $self->update; 1531 $self->realloc;
1498 $self->check_size;
1499} 1532}
1500 1533
1501sub _draw { 1534sub _draw {
1502 my ($self) = @_; 1535 my ($self) = @_;
1503 1536
1583sub set_text { 1616sub set_text {
1584 my ($self, $text) = @_; 1617 my ($self, $text) = @_;
1585 1618
1586 $self->{cursor} = length $text; 1619 $self->{cursor} = length $text;
1587 $self->_set_text ($text); 1620 $self->_set_text ($text);
1588 $self->update; 1621
1589 $self->check_size; 1622 $self->realloc;
1590} 1623}
1591 1624
1592sub get_text { 1625sub get_text {
1593 $_[0]{text} 1626 $_[0]{text}
1594} 1627}
1627 } elsif ($uni) { 1660 } elsif ($uni) {
1628 substr $text, $self->{cursor}++, 0, chr $uni; 1661 substr $text, $self->{cursor}++, 0, chr $uni;
1629 } 1662 }
1630 1663
1631 $self->_set_text ($text); 1664 $self->_set_text ($text);
1632 $self->update; 1665
1633 $self->check_size; 1666 $self->realloc;
1634} 1667}
1635 1668
1636sub focus_in { 1669sub focus_in {
1637 my ($self) = @_; 1670 my ($self) = @_;
1638 1671
2145 $self->update; 2178 $self->update;
2146 2179
2147 $self 2180 $self
2148} 2181}
2149 2182
2183sub changed { }
2184
2150sub set_range { 2185sub set_range {
2151 my ($self, $range) = @_; 2186 my ($self, $range) = @_;
2152 2187
2153 ($range, $self->{range}) = ($self->{range}, $range); 2188 ($range, $self->{range}) = ($self->{range}, $range);
2154 2189
2343 $self->{fontsize} = $fontsize; 2378 $self->{fontsize} = $fontsize;
2344 $self->reflow; 2379 $self->reflow;
2345} 2380}
2346 2381
2347sub size_allocate { 2382sub size_allocate {
2348 my ($self, $w, $h) = @_; 2383 my ($self, $w, $h, $changed) = @_;
2349 2384
2350 $self->SUPER::size_allocate ($w, $h); 2385 $self->SUPER::size_allocate ($w, $h, $changed);
2386
2387 return unless $changed;
2351 2388
2352 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2389 $self->{layout}->set_font ($self->{font}) if $self->{font};
2353 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2390 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2354 $self->{layout}->set_width ($self->{children}[0]{w}); 2391 $self->{layout}->set_width ($self->{children}[0]{w});
2355 2392
2544 2581
2545sub new { 2582sub new {
2546 my $class = shift; 2583 my $class = shift;
2547 2584
2548 my $self = $class->SUPER::new ( 2585 my $self = $class->SUPER::new (
2549 state => 0, 2586 state => 0,
2550 connect_activate => \&toggle_flopper, 2587 on_activate => \&toggle_flopper,
2551 @_ 2588 @_
2552 ); 2589 );
2553 2590
2554 if ($self->{state}) {
2555 $self->{state} = 0;
2556 $self->toggle_flopper;
2557 }
2558
2559 $self 2591 $self
2560} 2592}
2561 2593
2562sub toggle_flopper { 2594sub toggle_flopper {
2563 my ($self) = @_; 2595 my ($self) = @_;
2564 2596
2565 # TODO: use animation 2597 $self->{other}->toggle_visibility;
2566 if ($self->{state} = !$self->{state}) {
2567 $CFClient::UI::ROOT->add ($self->{other});
2568 $self->{other}->move ($self->coord2global (0, $self->{h}));
2569 $self->_emit ("open");
2570 } else {
2571 $CFClient::UI::ROOT->remove ($self->{other});
2572 $self->_emit ("close");
2573 }
2574
2575 $self->_emit (changed => $self->{state});
2576} 2598}
2577 2599
2578############################################################################# 2600#############################################################################
2579 2601
2580package CFClient::UI::Tooltip; 2602package CFClient::UI::Tooltip;
2612 2634
2613 ($w + 4, $h + 4) 2635 ($w + 4, $h + 4)
2614} 2636}
2615 2637
2616sub size_allocate { 2638sub size_allocate {
2617 my ($self, $w, $h) = @_; 2639 my ($self, $w, $h, $changed) = @_;
2618 2640
2641 return unless $changed;
2642
2619 $self->SUPER::size_allocate ($w - 4, $h - 4); 2643 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2620} 2644}
2621 2645
2622sub _draw { 2646sub _draw {
2623 my ($self) = @_; 2647 my ($self) = @_;
2624 2648
2641 glVertex $w, $h; 2665 glVertex $w, $h;
2642 glVertex $w, 0; 2666 glVertex $w, 0;
2643 glEnd; 2667 glEnd;
2644 2668
2645 glTranslate 2 - 0.375, 2 - 0.375; 2669 glTranslate 2 - 0.375, 2 - 0.375;
2670
2646 $self->SUPER::_draw; 2671 $self->SUPER::_draw;
2647} 2672}
2648 2673
2649############################################################################# 2674#############################################################################
2650 2675
2764 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2789 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2765 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2790 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2766 2791
2767 $row++; 2792 $row++;
2768 } 2793 }
2769}
2770
2771sub size_request {
2772 my ($self) = @_;
2773 ($self->{req_w}, $self->{req_h});
2774} 2794}
2775 2795
2776############################################################################# 2796#############################################################################
2777 2797
2778package CFClient::UI::Menu; 2798package CFClient::UI::Menu;
2964use CFClient::OpenGL; 2984use CFClient::OpenGL;
2965 2985
2966sub new { 2986sub new {
2967 my $class = shift; 2987 my $class = shift;
2968 2988
2969 $class->SUPER::new ( 2989 my $self = $class->SUPER::new (
2970 visible => 1, 2990 visible => 1,
2971 @_, 2991 @_,
2972 ) 2992 );
2993
2994 Scalar::Util::weaken ($self->{root} = $self);
2995
2996 $self
2973} 2997}
2974 2998
2975sub configure { 2999sub configure {
2976 my ($self, $x, $y, $w, $h) = @_; 3000 my ($self, $x, $y, $w, $h) = @_;
2977 3001
2978 $self->{w} = $w; 3002 $self->{w} = $w;
2979 $self->{h} = $h; 3003 $self->{h} = $h;
2980} 3004}
2981 3005
2982sub check_size { 3006sub reconfigure {
2983 my ($self) = @_; 3007 my ($self) = @_;
2984 3008
3009 $self->SUPER::reconfigure;
3010
2985 $self->size_allocate ($self->{w}, $self->{h}) 3011 $self->size_allocate ($self->{w}, $self->{h}, 1)
2986 if $self->{w}; 3012 if $self->{w};
2987} 3013}
2988 3014
2989sub size_request { 3015sub size_request {
2990 my ($self) = @_; 3016 my ($self) = @_;
2991 3017
2992 ($self->{w}, $self->{h}) 3018 ($self->{w}, $self->{h})
2993} 3019}
2994 3020
2995sub size_allocate { 3021sub size_allocate {
2996 my ($self, $w, $h) = @_; 3022 my ($self, $w, $h, $changed) = @_;
2997 3023
2998 for my $child ($self->children) { 3024 for my $child ($self->children) {
2999 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3025 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3000 3026
3001 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3027 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3002 if exists $child->{req_x}; 3028 if exists $child->{req_x};
3003 3029
3004 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3030 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3005 if exists $child->{req_y}; 3031 if exists $child->{req_y};
3006 3032
3033 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3034
3007 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3035 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3008 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3036 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3009 3037
3010 $child->configure ($X, $Y, $W, $H); 3038 $child->configure ($X, $Y, $W, $H);
3011 } 3039 }
3024} 3052}
3025 3053
3026sub update { 3054sub update {
3027 my ($self) = @_; 3055 my ($self) = @_;
3028 3056
3029 $self->check_size;
3030 $::WANT_REFRESH++; 3057 $::WANT_REFRESH++;
3031} 3058}
3032 3059
3033sub add { 3060sub add {
3034 my ($self, @children) = @_; 3061 my ($self, @children) = @_;
3035
3036 for (my @widgets = @children; my $w = pop @widgets; ) {
3037 push @widgets, $w->children;
3038 $w->{root} = $self;
3039 $w->{visible} = $self->{visible} + 1;
3040 }
3041 3062
3042 for my $child (@children) { 3063 for my $child (@children) {
3043 $child->{is_toplevel} = 1; 3064 $child->{is_toplevel} = 1;
3044 3065
3045 # integerise window positions 3066 # integerise window positions
3080 while ($self->{refresh_hook}) { 3101 while ($self->{refresh_hook}) {
3081 $_->() 3102 $_->()
3082 for values %{delete $self->{refresh_hook}}; 3103 for values %{delete $self->{refresh_hook}};
3083 } 3104 }
3084 3105
3085 if ($self->{check_size}) { 3106 if ($self->{realloc}) {
3086 my @queue = ([], []); 3107 my @queue;
3087 3108
3088 for (;;) { 3109 while () {
3089 if ($self->{check_size}) { 3110 if ($self->{realloc}) {
3090 # heuristic: check containers last 3111 #TODO use array-of-depth approach
3091 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3112
3092 for values %{delete $self->{check_size}} 3113 @queue = sort { $a->{visible} <=> $b->{visible} }
3114 @queue, values %{delete $self->{realloc}};
3093 } 3115 }
3094 3116
3095 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3117 my $widget = pop @queue || last;
3096 3118
3119 $widget->{visible} or last; # do not resize invisible widgets
3120
3097 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3121 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3098 ? @$widget{qw(user_w user_h)} 3122 ? @$widget{qw(def_w def_h)}
3099 : $widget->size_request; 3123 : $widget->size_request;
3100 3124
3101 if (delete $widget->{force_alloc}
3102 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3103 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3125 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3104 3126
3105 $widget->{req_w} = $w; 3127 $widget->{req_w} = $w;
3106 $widget->{req_h} = $h; 3128 $widget->{req_h} = $h;
3107 3129
3108 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3130 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3109 3131
3110 $widget->{parent}->check_size 3132 push @queue, $widget->{parent}
3111 if $widget->{parent}; 3133 if $widget->{parent};
3112 }
3113 } 3134 }
3114 } 3135 }
3115 3136
3116 while ($self->{size_alloc}) { 3137 while (my $size_alloc = delete $self->{size_alloc}) {
3117 for (values %{delete $self->{size_alloc}}) { 3138 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3118 my ($widget, $w, $h) = @$_; 3139 values %$size_alloc;
3140
3141 while () {
3142 my ($widget, $w, $h) = @{ pop @queue or last };
3119 3143
3120 $w = 0 if $w < 0; 3144 $w = 0 if $w < 0;
3121 $h = 0 if $h < 0; 3145 $h = 0 if $h < 0;
3122 3146
3147 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3148
3123 $widget->{w} = $w; 3149 $widget->{w} = $w;
3124 $widget->{h} = $h; 3150 $widget->{h} = $h;
3151
3125 $widget->emit (size_allocate => $w, $h); 3152 $widget->emit (size_allocate => $w, $h, $changed);
3126 } 3153 }
3127 } 3154 }
3128 3155
3129 while ($self->{post_alloc_hook}) { 3156 while ($self->{post_alloc_hook}) {
3130 $_->() 3157 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines