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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines