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.245 by elmex, Sat May 27 21:15:57 2006 UTC vs.
Revision 1.254 by root, Mon May 29 01:53:23 2006 UTC

40 if (!$GRAB) { 40 if (!$GRAB) {
41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
42 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
43 43
44 if ($TOOLTIP->{owner} != $widget) { 44 if ($TOOLTIP->{owner} != $widget) {
45 $TOOLTIP->hide;
46
45 $TOOLTIP->{owner} = $widget; 47 $TOOLTIP->{owner} = $widget;
46 48
47 my $tip = $widget->{tooltip}; 49 my $tip = $widget->{tooltip};
48 50
49 $tip = $tip->($widget) if CODE:: eq ref $tip; 51 $tip = $tip->($widget) if CODE:: eq ref $tip;
50 52
51 $TOOLTIP->set_tooltip_from ($widget); 53 $TOOLTIP->set_tooltip_from ($widget);
52 $TOOLTIP->show; 54 $TOOLTIP->show;
53
54 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
55
56 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
57 if $x + $TOOLTIP->{w} > $::WIDTH;
58
59 $TOOLTIP->move ($x, $y);
60 $TOOLTIP->check_size;
61 $TOOLTIP->update;
62 } 55 }
63 56
64 return; 57 return;
65 } 58 }
66 } 59 }
175 for my $widget (values %WIDGET) { 168 for my $widget (values %WIDGET) {
176 if ($widget->{is_toplevel}) { 169 if ($widget->{is_toplevel}) {
177 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 170 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
178 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 171 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
179 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 172 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
180 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
181 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 173 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 174 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
183 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 175 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
184 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
185 } 176 }
186 } 177 }
187 178
188 reconfigure_widgets; 179 reconfigure_widgets;
189} 180}
214 } 205 }
215 206
216 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 207 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
217 208
218 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 209 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
219 $self->{user_x} = $layout->{x} * $::WIDTH; 210 $self->{req_x} = $layout->{x} * $::WIDTH;
220 $self->{user_y} = $layout->{y} * $::HEIGHT; 211 $self->{req_y} = $layout->{y} * $::HEIGHT;
221 $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; 212 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
222 $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; 213 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
223 } 214 }
224 215
225 $self 216 $self
226}
227
228sub toggle_visibility {
229 my ($self) = @_;
230
231 if ($self->{visible}) {
232 $self->hide;
233 } else {
234 $self->show;
235 }
236} 217}
237 218
238sub destroy { 219sub destroy {
239 my ($self) = @_; 220 my ($self) = @_;
240 221
242 %$self = (); 223 %$self = ();
243} 224}
244 225
245sub show { 226sub show {
246 my ($self) = @_; 227 my ($self) = @_;
228
247 return if $self->{parent}; 229 return if $self->{parent};
248 230
249 $CFClient::UI::ROOT->add ($self); 231 $CFClient::UI::ROOT->add ($self);
250} 232}
251 233
252sub show_centered { 234sub center {
253 my ($self) = @_; 235 my ($self) = @_;
254 return if $self->{parent};
255
256 $self->show;
257 236
258 $CFClient::UI::ROOT->on_post_alloc ( 237 $CFClient::UI::ROOT->on_post_alloc (
259 "centered $self" => sub { 238 "center_$self" => sub {
260 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 239 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
261 }, 240 },
262 ); 241 );
242
243 $self->update;
244}
245
246sub set_visible {
247 my ($self) = @_;
248
249 return if $self->{visible};
250
251 $self->{root} = $self->{parent}{root};
252 $self->{visible} = $self->{parent}{visible} + 1;
253
254 $self->emit (visibility_change => 1);
255
256 $self->realloc if !exists $self->{req_w};
257
258 $_->set_visible for $self->children;
263} 259}
264 260
265sub set_invisible { 261sub set_invisible {
266 my ($self) = @_; 262 my ($self) = @_;
267 263
268 return unless $self->{visible}; 264 return unless $self->{visible};
269 265
270 # broken show/hide model 266 $_->set_invisible for $self->children;
271 267
272 delete $self->{root}; 268 delete $self->{root};
273 delete $self->{visible}; 269 delete $self->{visible};
274 270
275 undef $GRAB if $GRAB == $self; 271 undef $GRAB if $GRAB == $self;
276 undef $HOVER if $HOVER == $self; 272 undef $HOVER if $HOVER == $self;
277 273
278 CFClient::UI::check_tooltip 274 CFClient::UI::check_tooltip
279 if $CFClient::UI::TOOLTIP->{owner} == $self; 275 if $TOOLTIP->{owner} == $self;
280 276
281 $self->focus_out; 277 $self->focus_out;
282 278
283 $self->emit (visibility_change => 0); 279 $self->emit (visibility_change => 0);
280}
281
282sub set_visibility {
283 my ($self, $visible) = @_;
284
285 return if $self->{visible} == $visible;
286
287 $visible ? $self->hide
288 : $self->show;
289}
290
291sub toggle_visibility {
292 my ($self) = @_;
293
294 $self->{visible}
295 ? $self->hide
296 : $self->show;
284} 297}
285 298
286sub hide { 299sub hide {
287 my ($self) = @_; 300 my ($self) = @_;
288 301
303} 316}
304 317
305sub set_size { 318sub set_size {
306 my ($self, $w, $h) = @_; 319 my ($self, $w, $h) = @_;
307 320
308 $self->{user_w} = $w; 321 $self->{def_w} = $w;
309 $self->{user_h} = $h; 322 $self->{def_h} = $h;
310 323
311 $self->check_size; 324 $self->realloc;
312} 325}
313 326
314sub size_request { 327sub size_request {
315 require Carp; 328 require Carp;
316 Carp::confess "size_request is abstract"; 329 Carp::confess "size_request is abstract";
336 $self->{y} = $y; 349 $self->{y} = $y;
337 $self->update; 350 $self->update;
338 } 351 }
339 352
340 if ($self->{w} != $w || $self->{h} != $h) { 353 if ($self->{w} != $w || $self->{h} != $h) {
354 return unless $self->{visible};
355
341 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 356 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
342 } 357 }
343} 358}
344 359
345sub size_allocate { 360sub size_allocate {
346 # nothing to be done 361 # nothing to be done
347}
348
349sub reconfigure {
350 my ($self) = @_;
351
352 $self->check_size (1);
353 $self->update;
354} 362}
355 363
356sub children { 364sub children {
357} 365}
358 366
436sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 444sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
437sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 445sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
438sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 446sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
439sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 447sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
440sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 448sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
449
450sub find_widget {
451 my ($self, $x, $y) = @_;
452
453 return () unless $self->{can_events};
454
455 return $self
456 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
457 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
458
459 ()
460}
461
462sub set_parent {
463 my ($self, $parent) = @_;
464
465 Scalar::Util::weaken ($self->{parent} = $parent);
466
467 $self->set_visible if $parent->{visible};
468}
469
470sub connect {
471 my ($self, $signal, $cb) = @_;
472
473 push @{ $self->{signal_cb}{$signal} }, $cb;
474}
475
476sub _emit {
477 my ($self, $signal, @args) = @_;
478
479 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
480}
481
482sub emit {
483 my ($self, $signal, @args) = @_;
484
485 $self->_emit ($signal, @args)
486 || $self->$signal (@args);
487}
488
489sub visibility_change {
490 #my ($self, $visible) = @_;
491}
492
493sub realloc {
494 my ($self) = @_;
495
496 if ($self->{visible}) {
497 return if $self->{root}{realloc}{$self};
498
499 $self->{root}{realloc}{$self} = $self;
500 $self->{root}->update;
501 } else {
502 delete $self->{req_w};
503 }
504}
505
506sub update {
507 my ($self) = @_;
508
509 $self->{parent}->update
510 if $self->{parent};
511}
441 512
442sub draw { 513sub draw {
443 my ($self) = @_; 514 my ($self) = @_;
444 515
445 return unless $self->{h} && $self->{w}; 516 return unless $self->{h} && $self->{w};
483 my ($self) = @_; 554 my ($self) = @_;
484 555
485 warn "no draw defined for $self\n"; 556 warn "no draw defined for $self\n";
486} 557}
487 558
488sub find_widget {
489 my ($self, $x, $y) = @_;
490
491 return () unless $self->{can_events};
492
493 return $self
494 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
495 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
496
497 ()
498}
499
500sub set_parent {
501 my ($self, $parent) = @_;
502
503 Scalar::Util::weaken ($self->{parent} = $parent);
504
505 if ($parent->{visible} || 1) {
506 $self->{root} = $parent->{root};
507 $self->{visible} = $parent->{visible} + 1;
508
509 $self->emit (visibility_change => 1)
510 unless $self->{parent}{visible};
511 }
512
513 # TODO: req_w _does_change after ->reconfigure
514 $self->check_size
515 unless exists $self->{req_w};
516
517 $self->show;
518}
519
520sub check_size {
521 my ($self, $forced) = @_;
522
523 $self->{force_alloc} = 1 if $forced;
524 $CFClient::UI::ROOT->{check_size}{$self} = $self;
525}
526
527sub update {
528 my ($self) = @_;
529
530 $self->{parent}->update
531 if $self->{parent};
532}
533
534sub connect {
535 my ($self, $signal, $cb) = @_;
536
537 push @{ $self->{signal_cb}{$signal} }, $cb;
538}
539
540sub _emit {
541 my ($self, $signal, @args) = @_;
542
543 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
544}
545
546sub emit {
547 my ($self, $signal, @args) = @_;
548
549 $self->_emit ($signal, @args)
550 || $self->$signal (@args);
551}
552
553sub visibility_change {
554 #my ($self, $visible) = @_;
555}
556
557sub DESTROY { 559sub DESTROY {
558 my ($self) = @_; 560 my ($self) = @_;
559 561
560 delete $WIDGET{$self+0}; 562 delete $WIDGET{$self+0};
561 #$self->deactivate; 563 #$self->deactivate;
656 $self->{children} = [ 658 $self->{children} = [
657 sort { $a->{z} <=> $b->{z} } 659 sort { $a->{z} <=> $b->{z} }
658 @{$self->{children}}, @widgets 660 @{$self->{children}}, @widgets
659 ]; 661 ];
660 662
661 $self->check_size (1); 663 $self->realloc;
662 $self->update;
663} 664}
664 665
665sub children { 666sub children {
666 @{ $_[0]{children} } 667 @{ $_[0]{children} }
667} 668}
672 delete $child->{parent}; 673 delete $child->{parent};
673 $child->hide; 674 $child->hide;
674 675
675 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 676 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
676 677
677 $self->check_size (1); 678 $self->realloc;
678 $self->update;
679} 679}
680 680
681sub clear { 681sub clear {
682 my ($self) = @_; 682 my ($self) = @_;
683 683
687 for (@$children) { 687 for (@$children) {
688 delete $_->{parent}; 688 delete $_->{parent};
689 $_->hide; 689 $_->hide;
690 } 690 }
691 691
692 $self->check_size; 692 $self->realloc;
693 $self->update;
694} 693}
695 694
696sub find_widget { 695sub find_widget {
697 my ($self, $x, $y) = @_; 696 my ($self, $x, $y) = @_;
698 697
751sub size_request { 750sub size_request {
752 $_[0]{children}[0]->size_request 751 $_[0]{children}[0]->size_request
753} 752}
754 753
755sub size_allocate { 754sub size_allocate {
756 my ($self, $w, $h) = @_; 755 my ($self, $w, $h, $changed) = @_;
757 756
758 $self->{children}[0]->configure (0, 0, $w, $h); 757 $self->{children}[0]->configure (0, 0, $w, $h);
759} 758}
760 759
761############################################################################# 760#############################################################################
778 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 777 $ROOT->on_post_alloc ($self => sub { $self->render_child });
779 $self->SUPER::update; 778 $self->SUPER::update;
780} 779}
781 780
782sub size_allocate { 781sub size_allocate {
783 my ($self, $w, $h) = @_; 782 my ($self, $w, $h, $changed) = @_;
784 783
785 $self->SUPER::size_allocate ($w, $h); 784 $self->SUPER::size_allocate ($w, $h, $changed);
786 $self->update; 785 $self->update
786 if $changed;
787} 787}
788 788
789sub _render { 789sub _render {
790 $_[0]{children}[0]->draw; 790 $_[0]{children}[0]->draw;
791} 791}
835} 835}
836 836
837sub size_request { 837sub size_request {
838 my ($self) = @_; 838 my ($self) = @_;
839 839
840 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 840 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
841 841
842 @$self{qw(child_w child_h)} 842 $w = 10 if $self->{scroll_x};
843 $h = 10 if $self->{scroll_y};
844
845 ($w, $h)
843} 846}
844 847
845sub size_allocate { 848sub size_allocate {
846 my ($self, $w, $h) = @_; 849 my ($self, $w, $h, $changed) = @_;
847 850
848 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 851 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
849 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 852 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
850 853
851 $self->child->configure (0, 0, $w, $h); 854 $self->child->configure (0, 0, $w, $h);
938 my $child = $self->{vp}->child; 941 my $child = $self->{vp}->child;
939 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 942 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
940} 943}
941 944
942sub size_allocate { 945sub size_allocate {
943 my ($self, $w, $h) = @_; 946 my ($self, $w, $h, $changed) = @_;
944 947
945 $self->SUPER::size_allocate ($w, $h); 948 $self->SUPER::size_allocate ($w, $h, $changed);
946 949
947 my $child = $self->{vp}->child; 950 my $child = $self->{vp}->child;
948 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 951 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
949} 952}
950 953
1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1006 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1004 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1007 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1005 1008
1006sub new { 1009sub new {
1007 my $class = shift; 1010 my $class = shift;
1008
1009 # TODO: user_x, user_y, overwrite moveto?
1010 1011
1011 my $self = $class->SUPER::new ( 1012 my $self = $class->SUPER::new (
1012 bg => [1, 1, 1, 1], 1013 bg => [1, 1, 1, 1],
1013 border_bg => [1, 1, 1, 1], 1014 border_bg => [1, 1, 1, 1],
1014 border => 0.6, 1015 border => 0.6,
1040 $h + $self->border * 2, 1041 $h + $self->border * 2,
1041 ) 1042 )
1042} 1043}
1043 1044
1044sub size_allocate { 1045sub size_allocate {
1045 my ($self, $w, $h) = @_; 1046 my ($self, $w, $h, $changed) = @_;
1047
1048 return unless $changed;
1046 1049
1047 $h -= List::Util::max 0, $self->border * 2; 1050 $h -= List::Util::max 0, $self->border * 2;
1048 $w -= List::Util::max 0, $self->border * 2; 1051 $w -= List::Util::max 0, $self->border * 2;
1049 1052
1050 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1053 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1076 my $dx = $ev->{x} - $ox; 1079 my $dx = $ev->{x} - $ox;
1077 my $dy = $ev->{y} - $oy; 1080 my $dy = $ev->{y} - $oy;
1078 1081
1079 $self->{user_x} = $wx + $dx * $mx; 1082 $self->{user_x} = $wx + $dx * $mx;
1080 $self->{user_y} = $wy + $dy * $my; 1083 $self->{user_y} = $wy + $dy * $my;
1081 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1084 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1082 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1085 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1083 $self->move ($self->{user_x}, $self->{user_y}); 1086 $self->move ($self->{user_x}, $self->{user_y});
1084 $self->check_size; 1087 $self->realloc;
1085 }; 1088 };
1086 1089
1087 } elsif ($lr ^ $td) { 1090 } elsif ($lr ^ $td) {
1088 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1091 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1089 my ($bx, $by) = ($self->{x}, $self->{y}); 1092 my ($bx, $by) = ($self->{x}, $self->{y});
1093 1096
1094 ($x, $y) = ($ev->{x}, $ev->{y}); 1097 ($x, $y) = ($ev->{x}, $ev->{y});
1095 1098
1096 $self->{user_x} = $bx + $x - $ox; 1099 $self->{user_x} = $bx + $x - $ox;
1097 $self->{user_y} = $by + $y - $oy; 1100 $self->{user_y} = $by + $y - $oy;
1101
1098 $self->move ($self->{user_x}, $self->{user_y}); 1102 $self->move ($self->{user_x}, $self->{user_y});
1099 $self->update; 1103 $self->realloc;
1100 }; 1104 };
1101 } 1105 }
1102} 1106}
1103 1107
1104sub button_up { 1108sub button_up {
1179 my ($self, $x, $y, $child) = @_; 1183 my ($self, $x, $y, $child) = @_;
1180 1184
1181 $child->set_parent ($self); 1185 $child->set_parent ($self);
1182 $self->{children}[$y][$x] = $child; 1186 $self->{children}[$y][$x] = $child;
1183 1187
1184 $self->check_size (1); 1188 $self->realloc;
1185} 1189}
1186 1190
1187# TODO: move to container class maybe? send children a signal on removal? 1191# TODO: move to container class maybe? send children a signal on removal?
1188sub clear { 1192sub clear {
1189 my ($self) = @_; 1193 my ($self) = @_;
1194 for (@children) { 1198 for (@children) {
1195 delete $_->{parent}; 1199 delete $_->{parent};
1196 $_->hide; 1200 $_->hide;
1197 } 1201 }
1198 1202
1199 $self->check_size (1); 1203 $self->realloc;
1200 $self->update;
1201} 1204}
1202 1205
1203sub get_wh { 1206sub get_wh {
1204 my ($self) = @_; 1207 my ($self) = @_;
1205 1208
1232 (sum @$hs), 1235 (sum @$hs),
1233 ) 1236 )
1234} 1237}
1235 1238
1236sub size_allocate { 1239sub size_allocate {
1237 my ($self, $w, $h) = @_; 1240 my ($self, $w, $h, $changed) = @_;
1238 1241
1239 my ($ws, $hs) = $self->get_wh; 1242 my ($ws, $hs) = $self->get_wh;
1240 1243
1241 my $req_w = (sum @$ws) || 1; 1244 my $req_w = (sum @$ws) || 1;
1242 my $req_h = (sum @$hs) || 1; 1245 my $req_h = (sum @$hs) || 1;
1301 } 1304 }
1302} 1305}
1303 1306
1304############################################################################# 1307#############################################################################
1305 1308
1309package CFClient::UI::Box;
1310
1311our @ISA = CFClient::UI::Container::;
1312
1313sub size_request {
1314 my ($self) = @_;
1315
1316 $self->{vertical}
1317 ? (
1318 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1319 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1320 )
1321 : (
1322 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1323 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1324 )
1325}
1326
1327sub size_allocate {
1328 my ($self, $w, $h, $changed) = @_;
1329
1330 my $space = $self->{vertical} ? $h : $w;
1331 my $children = $self->{children};
1332
1333 my @req;
1334
1335 if ($self->{homogeneous}) {
1336 @req = ($space / (@$children || 1)) x @$children;
1337 } else {
1338 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1339 my $req = List::Util::sum @req;
1340
1341 if ($req > $space) {
1342 # ah well, not enough space
1343 $_ *= $space / $req for @req;
1344 } else {
1345 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1346
1347 $space = ($space - $req) / $expand; # remaining space to give away
1348
1349 $req[$_] += $space * $children->[$_]{expand}
1350 for 0 .. $#$children;
1351 }
1352 }
1353
1354 CFClient::UI::harmonize \@req;
1355
1356 my $pos = 0;
1357 for (0 .. $#$children) {
1358 my $alloc = $req[$_];
1359 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1360
1361 $pos += $alloc;
1362 }
1363
1364 1
1365}
1366
1367#############################################################################
1368
1306package CFClient::UI::HBox; 1369package CFClient::UI::HBox;
1307 1370
1308# TODO: wrap into common Box base class
1309
1310our @ISA = CFClient::UI::Container::; 1371our @ISA = CFClient::UI::Box::;
1311 1372
1312sub size_request { 1373sub new {
1313 my ($self) = @_; 1374 my $class = shift;
1314 1375
1315 my @alloc = map [$_->size_request], @{$self->{children}}; 1376 $class->SUPER::new (
1316 1377 vertical => 0,
1317 ( 1378 @_,
1318 (List::Util::sum map $_->[0], @alloc),
1319 (List::Util::max map $_->[1], @alloc),
1320 ) 1379 )
1321} 1380}
1322 1381
1323sub size_allocate {
1324 my ($self, $w, $h) = @_;
1325
1326 ($h, $w) = ($w, $h);
1327
1328 my $children = $self->{children};
1329
1330 my @h = map $_->{req_w}, @$children;
1331
1332 my $req_h = List::Util::sum @h;
1333
1334 if ($req_h > $h) {
1335 # ah well, not enough space
1336 $_ *= $h / $req_h for @h;
1337 } else {
1338 my $exp = List::Util::sum map $_->{expand}, @$children;
1339 $exp ||= 1;
1340
1341 for (0 .. $#$children) {
1342 my $child = $children->[$_];
1343
1344 my $alloc_h = $h[$_];
1345 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1346 $h[$_] = $alloc_h;
1347 }
1348 }
1349
1350 CFClient::UI::harmonize \@h;
1351
1352 my $y = 0;
1353 for (0 .. $#$children) {
1354 my $child = $children->[$_];
1355 my $h = $h[$_];
1356 $child->configure ($y, 0, $h, $w);
1357
1358 $y += $h;
1359 }
1360
1361 1
1362}
1363
1364############################################################################# 1382#############################################################################
1365 1383
1366package CFClient::UI::VBox; 1384package CFClient::UI::VBox;
1367 1385
1368# TODO: wrap into common Box base class
1369
1370our @ISA = CFClient::UI::Container::; 1386our @ISA = CFClient::UI::Box::;
1371 1387
1372sub size_request { 1388sub new {
1373 my ($self) = @_; 1389 my $class = shift;
1374 1390
1375 my @alloc = map [$_->size_request], @{$self->{children}}; 1391 $class->SUPER::new (
1376 1392 vertical => 1,
1377 ( 1393 @_,
1378 (List::Util::max map $_->[0], @alloc),
1379 (List::Util::sum map $_->[1], @alloc),
1380 ) 1394 )
1381}
1382
1383sub size_allocate {
1384 my ($self, $w, $h) = @_;
1385
1386 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1387
1388 my $children = $self->{children};
1389
1390 my @h = map $_->{req_h}, @$children;
1391
1392 my $req_h = List::Util::sum @h;
1393
1394 if ($req_h > $h) {
1395 # ah well, not enough space
1396 $_ *= $h / $req_h for @h;
1397 } else {
1398 my $exp = List::Util::sum map $_->{expand}, @$children;
1399 $exp ||= 1;
1400
1401 for (0 .. $#$children) {
1402 my $child = $children->[$_];
1403
1404 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1405 }
1406 }
1407
1408 CFClient::UI::harmonize \@h;
1409
1410 my $y = 0;
1411 for (0 .. $#$children) {
1412 my $child = $children->[$_];
1413 my $h = $h[$_];
1414 $child->configure (0, $y, $w, $h);
1415
1416 $y += $h;
1417 }
1418
1419 1
1420} 1395}
1421 1396
1422############################################################################# 1397#############################################################################
1423 1398
1424package CFClient::UI::Label; 1399package CFClient::UI::Label;
1487 $self->{text} = "T$text"; 1462 $self->{text} = "T$text";
1488 1463
1489 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1464 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1490 $self->{layout}->set_text ($text); 1465 $self->{layout}->set_text ($text);
1491 1466
1467 $self->realloc;
1492 $self->update; 1468 $self->update;
1493 $self->check_size;
1494} 1469}
1495 1470
1496sub set_markup { 1471sub set_markup {
1497 my ($self, $markup) = @_; 1472 my ($self, $markup) = @_;
1498 1473
1502 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1477 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1503 1478
1504 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1479 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1505 $self->{layout}->set_markup ($markup); 1480 $self->{layout}->set_markup ($markup);
1506 1481
1482 $self->realloc;
1507 $self->update; 1483 $self->update;
1508 $self->check_size;
1509} 1484}
1510 1485
1511sub size_request { 1486sub size_request {
1512 my ($self) = @_; 1487 my ($self) = @_;
1513 1488
1534 $h + $self->{padding} * 2, 1509 $h + $self->{padding} * 2,
1535 ) 1510 )
1536} 1511}
1537 1512
1538sub size_allocate { 1513sub size_allocate {
1539 my ($self, $w, $h) = @_; 1514 my ($self, $w, $h, $changed) = @_;
1540 1515
1541 delete $self->{texture}; 1516 delete $self->{texture}
1517 if $changed;
1542} 1518}
1543 1519
1544sub set_fontsize { 1520sub set_fontsize {
1545 my ($self, $fontsize) = @_; 1521 my ($self, $fontsize) = @_;
1546 1522
1547 $self->{fontsize} = $fontsize; 1523 $self->{fontsize} = $fontsize;
1548 delete $self->{texture}; 1524 delete $self->{texture};
1549 1525
1550 $self->update; 1526 $self->realloc;
1551 $self->check_size;
1552} 1527}
1553 1528
1554sub _draw { 1529sub _draw {
1555 my ($self) = @_; 1530 my ($self) = @_;
1556 1531
1636sub set_text { 1611sub set_text {
1637 my ($self, $text) = @_; 1612 my ($self, $text) = @_;
1638 1613
1639 $self->{cursor} = length $text; 1614 $self->{cursor} = length $text;
1640 $self->_set_text ($text); 1615 $self->_set_text ($text);
1641 $self->update; 1616
1642 $self->check_size; 1617 $self->realloc;
1643} 1618}
1644 1619
1645sub get_text { 1620sub get_text {
1646 $_[0]{text} 1621 $_[0]{text}
1647} 1622}
1680 } elsif ($uni) { 1655 } elsif ($uni) {
1681 substr $text, $self->{cursor}++, 0, chr $uni; 1656 substr $text, $self->{cursor}++, 0, chr $uni;
1682 } 1657 }
1683 1658
1684 $self->_set_text ($text); 1659 $self->_set_text ($text);
1685 $self->update; 1660
1686 $self->check_size; 1661 $self->realloc;
1687} 1662}
1688 1663
1689sub focus_in { 1664sub focus_in {
1690 my ($self) = @_; 1665 my ($self) = @_;
1691 1666
2198 $self->update; 2173 $self->update;
2199 2174
2200 $self 2175 $self
2201} 2176}
2202 2177
2178sub changed { }
2179
2203sub set_range { 2180sub set_range {
2204 my ($self, $range) = @_; 2181 my ($self, $range) = @_;
2205 2182
2206 ($range, $self->{range}) = ($self->{range}, $range); 2183 ($range, $self->{range}) = ($self->{range}, $range);
2207 2184
2396 $self->{fontsize} = $fontsize; 2373 $self->{fontsize} = $fontsize;
2397 $self->reflow; 2374 $self->reflow;
2398} 2375}
2399 2376
2400sub size_allocate { 2377sub size_allocate {
2401 my ($self, $w, $h) = @_; 2378 my ($self, $w, $h, $changed) = @_;
2402 2379
2403 $self->SUPER::size_allocate ($w, $h); 2380 $self->SUPER::size_allocate ($w, $h, $changed);
2381
2382 return unless $changed;
2404 2383
2405 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2384 $self->{layout}->set_font ($self->{font}) if $self->{font};
2406 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2385 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2407 $self->{layout}->set_width ($self->{children}[0]{w}); 2386 $self->{layout}->set_width ($self->{children}[0]{w});
2408 2387
2650 2629
2651 ($w + 4, $h + 4) 2630 ($w + 4, $h + 4)
2652} 2631}
2653 2632
2654sub size_allocate { 2633sub size_allocate {
2655 my ($self, $w, $h) = @_; 2634 my ($self, $w, $h, $changed) = @_;
2656 2635
2636 return unless $changed;
2637
2657 $self->SUPER::size_allocate ($w - 4, $h - 4); 2638 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2639}
2640
2641sub visibility_change {
2642 my ($self, $visible) = @_;
2643
2644 return unless $visible;
2645
2646 $self->{root}->on_post_alloc ("move_$self" => sub {
2647 my $widget = $self->{owner}
2648 or return;
2649
2650 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2651
2652 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2653 if $x + $self->{w} > $::WIDTH;
2654
2655 $self->move ($x, $y);
2656 });
2658} 2657}
2659 2658
2660sub _draw { 2659sub _draw {
2661 my ($self) = @_; 2660 my ($self) = @_;
2662 2661
2679 glVertex $w, $h; 2678 glVertex $w, $h;
2680 glVertex $w, 0; 2679 glVertex $w, 0;
2681 glEnd; 2680 glEnd;
2682 2681
2683 glTranslate 2 - 0.375, 2 - 0.375; 2682 glTranslate 2 - 0.375, 2 - 0.375;
2683
2684 $self->SUPER::_draw; 2684 $self->SUPER::_draw;
2685} 2685}
2686 2686
2687############################################################################# 2687#############################################################################
2688 2688
2802 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2802 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2803 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2803 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2804 2804
2805 $row++; 2805 $row++;
2806 } 2806 }
2807}
2808
2809sub size_request {
2810 my ($self) = @_;
2811 ($self->{req_w}, $self->{req_h});
2812} 2807}
2813 2808
2814############################################################################# 2809#############################################################################
2815 2810
2816package CFClient::UI::Menu; 2811package CFClient::UI::Menu;
3002use CFClient::OpenGL; 2997use CFClient::OpenGL;
3003 2998
3004sub new { 2999sub new {
3005 my $class = shift; 3000 my $class = shift;
3006 3001
3007 $class->SUPER::new ( 3002 my $self = $class->SUPER::new (
3008 visible => 1, 3003 visible => 1,
3009 @_, 3004 @_,
3010 ) 3005 );
3006
3007 Scalar::Util::weaken ($self->{root} = $self);
3008
3009 $self
3011} 3010}
3012 3011
3013sub configure { 3012sub configure {
3014 my ($self, $x, $y, $w, $h) = @_; 3013 my ($self, $x, $y, $w, $h) = @_;
3015 3014
3016 $self->{w} = $w; 3015 $self->{w} = $w;
3017 $self->{h} = $h; 3016 $self->{h} = $h;
3018} 3017}
3019 3018
3020sub check_size { 3019sub reconfigure {
3021 my ($self) = @_; 3020 my ($self) = @_;
3022 3021
3022 $self->SUPER::reconfigure;
3023
3023 $self->size_allocate ($self->{w}, $self->{h}) 3024 $self->size_allocate ($self->{w}, $self->{h}, 1)
3024 if $self->{w}; 3025 if $self->{w};
3025} 3026}
3026 3027
3027sub size_request { 3028sub size_request {
3028 my ($self) = @_; 3029 my ($self) = @_;
3029 3030
3030 ($self->{w}, $self->{h}) 3031 ($self->{w}, $self->{h})
3031} 3032}
3032 3033
3033sub size_allocate { 3034sub size_allocate {
3034 my ($self, $w, $h) = @_; 3035 my ($self, $w, $h, $changed) = @_;
3035 3036
3036 for my $child ($self->children) { 3037 for my $child ($self->children) {
3037 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3038 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3038 3039
3039 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3040 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3040 if exists $child->{req_x}; 3041 if exists $child->{req_x};
3041 3042
3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3043 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3043 if exists $child->{req_y}; 3044 if exists $child->{req_y};
3044 3045
3045 $X = $self->{user_x} if exists $self->{user_x}; 3046 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3046 $Y = $self->{user_y} if exists $self->{user_y};
3047 3047
3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3050 3050
3051 $child->configure ($X, $Y, $W, $H); 3051 $child->configure ($X, $Y, $W, $H);
3065} 3065}
3066 3066
3067sub update { 3067sub update {
3068 my ($self) = @_; 3068 my ($self) = @_;
3069 3069
3070 $self->check_size;
3071 $::WANT_REFRESH++; 3070 $::WANT_REFRESH++;
3072} 3071}
3073 3072
3074sub add { 3073sub add {
3075 my ($self, @children) = @_; 3074 my ($self, @children) = @_;
3076
3077 for (my @widgets = @children; my $w = pop @widgets; ) {
3078 push @widgets, $w->children;
3079 $w->{root} = $self;
3080 $w->{visible} = $self->{visible} + 1;
3081 }
3082 3075
3083 for my $child (@children) { 3076 for my $child (@children) {
3084 $child->{is_toplevel} = 1; 3077 $child->{is_toplevel} = 1;
3085 3078
3086 # integerise window positions 3079 # integerise window positions
3121 while ($self->{refresh_hook}) { 3114 while ($self->{refresh_hook}) {
3122 $_->() 3115 $_->()
3123 for values %{delete $self->{refresh_hook}}; 3116 for values %{delete $self->{refresh_hook}};
3124 } 3117 }
3125 3118
3126 if ($self->{check_size}) { 3119 if ($self->{realloc}) {
3127 my @queue; 3120 my @queue;
3128 3121
3129 for (;;) { 3122 while () {
3130 if ($self->{check_size}) { 3123 if ($self->{realloc}) {
3131 #TODO use array-of-depth approach 3124 #TODO use array-of-depth approach
3132 3125
3133 @queue = sort { $a->{visible} <=> $b->{visible} } 3126 @queue = sort { $a->{visible} <=> $b->{visible} }
3134 @queue, values %{delete $self->{check_size}}; 3127 @queue, values %{delete $self->{realloc}};
3135 } 3128 }
3136 3129
3137 my $widget = pop @queue || last; 3130 my $widget = pop @queue || last;
3138 3131
3139 defined $widget->{visible} or last; # do not resize invisible widgets 3132 $widget->{visible} or last; # do not resize invisible widgets
3140 3133
3141 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3134 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3142 ? @$widget{qw(user_w user_h)} 3135 ? @$widget{qw(def_w def_h)}
3143 : $widget->size_request; 3136 : $widget->size_request;
3144 3137
3145 if (delete $widget->{force_alloc}
3146 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3147 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3138 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3148 3139
3149 $widget->{req_w} = $w; 3140 $widget->{req_w} = $w;
3150 $widget->{req_h} = $h; 3141 $widget->{req_h} = $h;
3151 3142
3152 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3143 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3153 3144
3154 $widget->{parent}->check_size 3145 push @queue, $widget->{parent}
3155 if $widget->{parent}; 3146 if $widget->{parent};
3156 }
3157 } 3147 }
3158 } 3148 }
3159 3149
3160 while ($self->{size_alloc}) { 3150 while (my $size_alloc = delete $self->{size_alloc}) {
3161 for (values %{delete $self->{size_alloc}}) { 3151 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3162 my ($widget, $w, $h) = @$_; 3152 values %$size_alloc;
3153
3154 while () {
3155 my ($widget, $w, $h) = @{ pop @queue or last };
3163 3156
3164 $w = 0 if $w < 0; 3157 $w = 0 if $w < 0;
3165 $h = 0 if $h < 0; 3158 $h = 0 if $h < 0;
3166 3159
3160 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3161
3167 $widget->{w} = $w; 3162 $widget->{w} = $w;
3168 $widget->{h} = $h; 3163 $widget->{h} = $h;
3164
3169 $widget->emit (size_allocate => $w, $h); 3165 $widget->emit (size_allocate => $w, $h, $changed);
3170 } 3166 }
3171 } 3167 }
3172 3168
3173 while ($self->{post_alloc_hook}) { 3169 while ($self->{post_alloc_hook}) {
3174 $_->() 3170 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines