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.244 by root, Sat May 27 21:06:09 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} 217}
227 218
232 %$self = (); 223 %$self = ();
233} 224}
234 225
235sub show { 226sub show {
236 my ($self) = @_; 227 my ($self) = @_;
228
237 return if $self->{parent}; 229 return if $self->{parent};
238 230
239 $CFClient::UI::ROOT->add ($self); 231 $CFClient::UI::ROOT->add ($self);
240} 232}
241 233
242sub show_centered { 234sub center {
243 my ($self) = @_; 235 my ($self) = @_;
244 return if $self->{parent};
245
246 $self->show;
247 236
248 $CFClient::UI::ROOT->on_post_alloc ( 237 $CFClient::UI::ROOT->on_post_alloc (
249 "centered $self" => sub { 238 "center_$self" => sub {
250 $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);
251 }, 240 },
252 ); 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;
253} 259}
254 260
255sub set_invisible { 261sub set_invisible {
256 my ($self) = @_; 262 my ($self) = @_;
257 263
258 return unless $self->{visible}; 264 return unless $self->{visible};
259 265
260 # broken show/hide model 266 $_->set_invisible for $self->children;
261 267
262 delete $self->{root}; 268 delete $self->{root};
263 delete $self->{visible}; 269 delete $self->{visible};
264 270
265 undef $GRAB if $GRAB == $self; 271 undef $GRAB if $GRAB == $self;
266 undef $HOVER if $HOVER == $self; 272 undef $HOVER if $HOVER == $self;
267 273
268 CFClient::UI::check_tooltip 274 CFClient::UI::check_tooltip
269 if $CFClient::UI::TOOLTIP->{owner} == $self; 275 if $TOOLTIP->{owner} == $self;
270 276
271 $self->focus_out; 277 $self->focus_out;
272 278
273 $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;
274} 297}
275 298
276sub hide { 299sub hide {
277 my ($self) = @_; 300 my ($self) = @_;
278 301
293} 316}
294 317
295sub set_size { 318sub set_size {
296 my ($self, $w, $h) = @_; 319 my ($self, $w, $h) = @_;
297 320
298 $self->{user_w} = $w; 321 $self->{def_w} = $w;
299 $self->{user_h} = $h; 322 $self->{def_h} = $h;
300 323
301 $self->check_size; 324 $self->realloc;
302} 325}
303 326
304sub size_request { 327sub size_request {
305 require Carp; 328 require Carp;
306 Carp::confess "size_request is abstract"; 329 Carp::confess "size_request is abstract";
326 $self->{y} = $y; 349 $self->{y} = $y;
327 $self->update; 350 $self->update;
328 } 351 }
329 352
330 if ($self->{w} != $w || $self->{h} != $h) { 353 if ($self->{w} != $w || $self->{h} != $h) {
354 return unless $self->{visible};
355
331 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 356 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
332 } 357 }
333} 358}
334 359
335sub size_allocate { 360sub size_allocate {
336 # nothing to be done 361 # nothing to be done
337}
338
339sub reconfigure {
340 my ($self) = @_;
341
342 $self->check_size (1);
343 $self->update;
344} 362}
345 363
346sub children { 364sub children {
347} 365}
348 366
426sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 444sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
427sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 445sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
428sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 446sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
429sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 447sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
430sub 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}
431 512
432sub draw { 513sub draw {
433 my ($self) = @_; 514 my ($self) = @_;
434 515
435 return unless $self->{h} && $self->{w}; 516 return unless $self->{h} && $self->{w};
473 my ($self) = @_; 554 my ($self) = @_;
474 555
475 warn "no draw defined for $self\n"; 556 warn "no draw defined for $self\n";
476} 557}
477 558
478sub find_widget {
479 my ($self, $x, $y) = @_;
480
481 return () unless $self->{can_events};
482
483 return $self
484 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
485 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
486
487 ()
488}
489
490sub set_parent {
491 my ($self, $parent) = @_;
492
493 Scalar::Util::weaken ($self->{parent} = $parent);
494
495 if ($parent->{visible} || 1) {
496 $self->{root} = $parent->{root};
497 $self->{visible} = $parent->{visible} + 1;
498
499 $self->emit (visibility_change => 1)
500 unless $self->{parent}{visible};
501 }
502
503 # TODO: req_w _does_change after ->reconfigure
504 $self->check_size
505 unless exists $self->{req_w};
506
507 $self->show;
508}
509
510sub check_size {
511 my ($self, $forced) = @_;
512
513 $self->{force_alloc} = 1 if $forced;
514 $CFClient::UI::ROOT->{check_size}{$self} = $self;
515}
516
517sub update {
518 my ($self) = @_;
519
520 $self->{parent}->update
521 if $self->{parent};
522}
523
524sub connect {
525 my ($self, $signal, $cb) = @_;
526
527 push @{ $self->{signal_cb}{$signal} }, $cb;
528}
529
530sub _emit {
531 my ($self, $signal, @args) = @_;
532
533 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
534}
535
536sub emit {
537 my ($self, $signal, @args) = @_;
538
539 $self->_emit ($signal, @args)
540 || $self->$signal (@args);
541}
542
543sub visibility_change {
544 #my ($self, $visible) = @_;
545}
546
547sub DESTROY { 559sub DESTROY {
548 my ($self) = @_; 560 my ($self) = @_;
549 561
550 delete $WIDGET{$self+0}; 562 delete $WIDGET{$self+0};
551 #$self->deactivate; 563 #$self->deactivate;
646 $self->{children} = [ 658 $self->{children} = [
647 sort { $a->{z} <=> $b->{z} } 659 sort { $a->{z} <=> $b->{z} }
648 @{$self->{children}}, @widgets 660 @{$self->{children}}, @widgets
649 ]; 661 ];
650 662
651 $self->check_size (1); 663 $self->realloc;
652 $self->update;
653} 664}
654 665
655sub children { 666sub children {
656 @{ $_[0]{children} } 667 @{ $_[0]{children} }
657} 668}
662 delete $child->{parent}; 673 delete $child->{parent};
663 $child->hide; 674 $child->hide;
664 675
665 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 676 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
666 677
667 $self->check_size (1); 678 $self->realloc;
668 $self->update;
669} 679}
670 680
671sub clear { 681sub clear {
672 my ($self) = @_; 682 my ($self) = @_;
673 683
677 for (@$children) { 687 for (@$children) {
678 delete $_->{parent}; 688 delete $_->{parent};
679 $_->hide; 689 $_->hide;
680 } 690 }
681 691
682 $self->check_size; 692 $self->realloc;
683 $self->update;
684} 693}
685 694
686sub find_widget { 695sub find_widget {
687 my ($self, $x, $y) = @_; 696 my ($self, $x, $y) = @_;
688 697
741sub size_request { 750sub size_request {
742 $_[0]{children}[0]->size_request 751 $_[0]{children}[0]->size_request
743} 752}
744 753
745sub size_allocate { 754sub size_allocate {
746 my ($self, $w, $h) = @_; 755 my ($self, $w, $h, $changed) = @_;
747 756
748 $self->{children}[0]->configure (0, 0, $w, $h); 757 $self->{children}[0]->configure (0, 0, $w, $h);
749} 758}
750 759
751############################################################################# 760#############################################################################
768 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 777 $ROOT->on_post_alloc ($self => sub { $self->render_child });
769 $self->SUPER::update; 778 $self->SUPER::update;
770} 779}
771 780
772sub size_allocate { 781sub size_allocate {
773 my ($self, $w, $h) = @_; 782 my ($self, $w, $h, $changed) = @_;
774 783
775 $self->SUPER::size_allocate ($w, $h); 784 $self->SUPER::size_allocate ($w, $h, $changed);
776 $self->update; 785 $self->update
786 if $changed;
777} 787}
778 788
779sub _render { 789sub _render {
780 $_[0]{children}[0]->draw; 790 $_[0]{children}[0]->draw;
781} 791}
825} 835}
826 836
827sub size_request { 837sub size_request {
828 my ($self) = @_; 838 my ($self) = @_;
829 839
830 @$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)};
831 841
832 @$self{qw(child_w child_h)} 842 $w = 10 if $self->{scroll_x};
843 $h = 10 if $self->{scroll_y};
844
845 ($w, $h)
833} 846}
834 847
835sub size_allocate { 848sub size_allocate {
836 my ($self, $w, $h) = @_; 849 my ($self, $w, $h, $changed) = @_;
837 850
838 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 851 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
839 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 852 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
840 853
841 $self->child->configure (0, 0, $w, $h); 854 $self->child->configure (0, 0, $w, $h);
928 my $child = $self->{vp}->child; 941 my $child = $self->{vp}->child;
929 $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]);
930} 943}
931 944
932sub size_allocate { 945sub size_allocate {
933 my ($self, $w, $h) = @_; 946 my ($self, $w, $h, $changed) = @_;
934 947
935 $self->SUPER::size_allocate ($w, $h); 948 $self->SUPER::size_allocate ($w, $h, $changed);
936 949
937 my $child = $self->{vp}->child; 950 my $child = $self->{vp}->child;
938 $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]);
939} 952}
940 953
993 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1006 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
994 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);
995 1008
996sub new { 1009sub new {
997 my $class = shift; 1010 my $class = shift;
998
999 # TODO: user_x, user_y, overwrite moveto?
1000 1011
1001 my $self = $class->SUPER::new ( 1012 my $self = $class->SUPER::new (
1002 bg => [1, 1, 1, 1], 1013 bg => [1, 1, 1, 1],
1003 border_bg => [1, 1, 1, 1], 1014 border_bg => [1, 1, 1, 1],
1004 border => 0.6, 1015 border => 0.6,
1030 $h + $self->border * 2, 1041 $h + $self->border * 2,
1031 ) 1042 )
1032} 1043}
1033 1044
1034sub size_allocate { 1045sub size_allocate {
1035 my ($self, $w, $h) = @_; 1046 my ($self, $w, $h, $changed) = @_;
1047
1048 return unless $changed;
1036 1049
1037 $h -= List::Util::max 0, $self->border * 2; 1050 $h -= List::Util::max 0, $self->border * 2;
1038 $w -= List::Util::max 0, $self->border * 2; 1051 $w -= List::Util::max 0, $self->border * 2;
1039 1052
1040 $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)
1066 my $dx = $ev->{x} - $ox; 1079 my $dx = $ev->{x} - $ox;
1067 my $dy = $ev->{y} - $oy; 1080 my $dy = $ev->{y} - $oy;
1068 1081
1069 $self->{user_x} = $wx + $dx * $mx; 1082 $self->{user_x} = $wx + $dx * $mx;
1070 $self->{user_y} = $wy + $dy * $my; 1083 $self->{user_y} = $wy + $dy * $my;
1071 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1084 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1072 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1085 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1073 $self->move ($self->{user_x}, $self->{user_y}); 1086 $self->move ($self->{user_x}, $self->{user_y});
1074 $self->check_size; 1087 $self->realloc;
1075 }; 1088 };
1076 1089
1077 } elsif ($lr ^ $td) { 1090 } elsif ($lr ^ $td) {
1078 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1091 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1079 my ($bx, $by) = ($self->{x}, $self->{y}); 1092 my ($bx, $by) = ($self->{x}, $self->{y});
1083 1096
1084 ($x, $y) = ($ev->{x}, $ev->{y}); 1097 ($x, $y) = ($ev->{x}, $ev->{y});
1085 1098
1086 $self->{user_x} = $bx + $x - $ox; 1099 $self->{user_x} = $bx + $x - $ox;
1087 $self->{user_y} = $by + $y - $oy; 1100 $self->{user_y} = $by + $y - $oy;
1101
1088 $self->move ($self->{user_x}, $self->{user_y}); 1102 $self->move ($self->{user_x}, $self->{user_y});
1089 $self->update; 1103 $self->realloc;
1090 }; 1104 };
1091 } 1105 }
1092} 1106}
1093 1107
1094sub button_up { 1108sub button_up {
1169 my ($self, $x, $y, $child) = @_; 1183 my ($self, $x, $y, $child) = @_;
1170 1184
1171 $child->set_parent ($self); 1185 $child->set_parent ($self);
1172 $self->{children}[$y][$x] = $child; 1186 $self->{children}[$y][$x] = $child;
1173 1187
1174 $self->check_size (1); 1188 $self->realloc;
1175} 1189}
1176 1190
1177# 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?
1178sub clear { 1192sub clear {
1179 my ($self) = @_; 1193 my ($self) = @_;
1184 for (@children) { 1198 for (@children) {
1185 delete $_->{parent}; 1199 delete $_->{parent};
1186 $_->hide; 1200 $_->hide;
1187 } 1201 }
1188 1202
1189 $self->check_size (1); 1203 $self->realloc;
1190 $self->update;
1191} 1204}
1192 1205
1193sub get_wh { 1206sub get_wh {
1194 my ($self) = @_; 1207 my ($self) = @_;
1195 1208
1222 (sum @$hs), 1235 (sum @$hs),
1223 ) 1236 )
1224} 1237}
1225 1238
1226sub size_allocate { 1239sub size_allocate {
1227 my ($self, $w, $h) = @_; 1240 my ($self, $w, $h, $changed) = @_;
1228 1241
1229 my ($ws, $hs) = $self->get_wh; 1242 my ($ws, $hs) = $self->get_wh;
1230 1243
1231 my $req_w = (sum @$ws) || 1; 1244 my $req_w = (sum @$ws) || 1;
1232 my $req_h = (sum @$hs) || 1; 1245 my $req_h = (sum @$hs) || 1;
1291 } 1304 }
1292} 1305}
1293 1306
1294############################################################################# 1307#############################################################################
1295 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
1296package CFClient::UI::HBox; 1369package CFClient::UI::HBox;
1297 1370
1298# TODO: wrap into common Box base class
1299
1300our @ISA = CFClient::UI::Container::; 1371our @ISA = CFClient::UI::Box::;
1301 1372
1302sub size_request { 1373sub new {
1303 my ($self) = @_; 1374 my $class = shift;
1304 1375
1305 my @alloc = map [$_->size_request], @{$self->{children}}; 1376 $class->SUPER::new (
1306 1377 vertical => 0,
1307 ( 1378 @_,
1308 (List::Util::sum map $_->[0], @alloc),
1309 (List::Util::max map $_->[1], @alloc),
1310 ) 1379 )
1311} 1380}
1312 1381
1313sub size_allocate {
1314 my ($self, $w, $h) = @_;
1315
1316 ($h, $w) = ($w, $h);
1317
1318 my $children = $self->{children};
1319
1320 my @h = map $_->{req_w}, @$children;
1321
1322 my $req_h = List::Util::sum @h;
1323
1324 if ($req_h > $h) {
1325 # ah well, not enough space
1326 $_ *= $h / $req_h for @h;
1327 } else {
1328 my $exp = List::Util::sum map $_->{expand}, @$children;
1329 $exp ||= 1;
1330
1331 for (0 .. $#$children) {
1332 my $child = $children->[$_];
1333
1334 my $alloc_h = $h[$_];
1335 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1336 $h[$_] = $alloc_h;
1337 }
1338 }
1339
1340 CFClient::UI::harmonize \@h;
1341
1342 my $y = 0;
1343 for (0 .. $#$children) {
1344 my $child = $children->[$_];
1345 my $h = $h[$_];
1346 $child->configure ($y, 0, $h, $w);
1347
1348 $y += $h;
1349 }
1350
1351 1
1352}
1353
1354############################################################################# 1382#############################################################################
1355 1383
1356package CFClient::UI::VBox; 1384package CFClient::UI::VBox;
1357 1385
1358# TODO: wrap into common Box base class
1359
1360our @ISA = CFClient::UI::Container::; 1386our @ISA = CFClient::UI::Box::;
1361 1387
1362sub size_request { 1388sub new {
1363 my ($self) = @_; 1389 my $class = shift;
1364 1390
1365 my @alloc = map [$_->size_request], @{$self->{children}}; 1391 $class->SUPER::new (
1366 1392 vertical => 1,
1367 ( 1393 @_,
1368 (List::Util::max map $_->[0], @alloc),
1369 (List::Util::sum map $_->[1], @alloc),
1370 ) 1394 )
1371}
1372
1373sub size_allocate {
1374 my ($self, $w, $h) = @_;
1375
1376 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1377
1378 my $children = $self->{children};
1379
1380 my @h = map $_->{req_h}, @$children;
1381
1382 my $req_h = List::Util::sum @h;
1383
1384 if ($req_h > $h) {
1385 # ah well, not enough space
1386 $_ *= $h / $req_h for @h;
1387 } else {
1388 my $exp = List::Util::sum map $_->{expand}, @$children;
1389 $exp ||= 1;
1390
1391 for (0 .. $#$children) {
1392 my $child = $children->[$_];
1393
1394 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1395 }
1396 }
1397
1398 CFClient::UI::harmonize \@h;
1399
1400 my $y = 0;
1401 for (0 .. $#$children) {
1402 my $child = $children->[$_];
1403 my $h = $h[$_];
1404 $child->configure (0, $y, $w, $h);
1405
1406 $y += $h;
1407 }
1408
1409 1
1410} 1395}
1411 1396
1412############################################################################# 1397#############################################################################
1413 1398
1414package CFClient::UI::Label; 1399package CFClient::UI::Label;
1477 $self->{text} = "T$text"; 1462 $self->{text} = "T$text";
1478 1463
1479 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1464 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1480 $self->{layout}->set_text ($text); 1465 $self->{layout}->set_text ($text);
1481 1466
1467 $self->realloc;
1482 $self->update; 1468 $self->update;
1483 $self->check_size;
1484} 1469}
1485 1470
1486sub set_markup { 1471sub set_markup {
1487 my ($self, $markup) = @_; 1472 my ($self, $markup) = @_;
1488 1473
1492 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1477 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1493 1478
1494 $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;
1495 $self->{layout}->set_markup ($markup); 1480 $self->{layout}->set_markup ($markup);
1496 1481
1482 $self->realloc;
1497 $self->update; 1483 $self->update;
1498 $self->check_size;
1499} 1484}
1500 1485
1501sub size_request { 1486sub size_request {
1502 my ($self) = @_; 1487 my ($self) = @_;
1503 1488
1524 $h + $self->{padding} * 2, 1509 $h + $self->{padding} * 2,
1525 ) 1510 )
1526} 1511}
1527 1512
1528sub size_allocate { 1513sub size_allocate {
1529 my ($self, $w, $h) = @_; 1514 my ($self, $w, $h, $changed) = @_;
1530 1515
1531 delete $self->{texture}; 1516 delete $self->{texture}
1517 if $changed;
1532} 1518}
1533 1519
1534sub set_fontsize { 1520sub set_fontsize {
1535 my ($self, $fontsize) = @_; 1521 my ($self, $fontsize) = @_;
1536 1522
1537 $self->{fontsize} = $fontsize; 1523 $self->{fontsize} = $fontsize;
1538 delete $self->{texture}; 1524 delete $self->{texture};
1539 1525
1540 $self->update; 1526 $self->realloc;
1541 $self->check_size;
1542} 1527}
1543 1528
1544sub _draw { 1529sub _draw {
1545 my ($self) = @_; 1530 my ($self) = @_;
1546 1531
1626sub set_text { 1611sub set_text {
1627 my ($self, $text) = @_; 1612 my ($self, $text) = @_;
1628 1613
1629 $self->{cursor} = length $text; 1614 $self->{cursor} = length $text;
1630 $self->_set_text ($text); 1615 $self->_set_text ($text);
1631 $self->update; 1616
1632 $self->check_size; 1617 $self->realloc;
1633} 1618}
1634 1619
1635sub get_text { 1620sub get_text {
1636 $_[0]{text} 1621 $_[0]{text}
1637} 1622}
1670 } elsif ($uni) { 1655 } elsif ($uni) {
1671 substr $text, $self->{cursor}++, 0, chr $uni; 1656 substr $text, $self->{cursor}++, 0, chr $uni;
1672 } 1657 }
1673 1658
1674 $self->_set_text ($text); 1659 $self->_set_text ($text);
1675 $self->update; 1660
1676 $self->check_size; 1661 $self->realloc;
1677} 1662}
1678 1663
1679sub focus_in { 1664sub focus_in {
1680 my ($self) = @_; 1665 my ($self) = @_;
1681 1666
2188 $self->update; 2173 $self->update;
2189 2174
2190 $self 2175 $self
2191} 2176}
2192 2177
2178sub changed { }
2179
2193sub set_range { 2180sub set_range {
2194 my ($self, $range) = @_; 2181 my ($self, $range) = @_;
2195 2182
2196 ($range, $self->{range}) = ($self->{range}, $range); 2183 ($range, $self->{range}) = ($self->{range}, $range);
2197 2184
2386 $self->{fontsize} = $fontsize; 2373 $self->{fontsize} = $fontsize;
2387 $self->reflow; 2374 $self->reflow;
2388} 2375}
2389 2376
2390sub size_allocate { 2377sub size_allocate {
2391 my ($self, $w, $h) = @_; 2378 my ($self, $w, $h, $changed) = @_;
2392 2379
2393 $self->SUPER::size_allocate ($w, $h); 2380 $self->SUPER::size_allocate ($w, $h, $changed);
2381
2382 return unless $changed;
2394 2383
2395 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2384 $self->{layout}->set_font ($self->{font}) if $self->{font};
2396 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2385 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2397 $self->{layout}->set_width ($self->{children}[0]{w}); 2386 $self->{layout}->set_width ($self->{children}[0]{w});
2398 2387
2592 state => 0, 2581 state => 0,
2593 on_activate => \&toggle_flopper, 2582 on_activate => \&toggle_flopper,
2594 @_ 2583 @_
2595 ); 2584 );
2596 2585
2597 if ($self->{state}) {
2598 $self->{state} = 0;
2599 $self->toggle_flopper;
2600 }
2601
2602 $self 2586 $self
2603} 2587}
2604 2588
2605sub toggle_flopper { 2589sub toggle_flopper {
2606 my ($self) = @_; 2590 my ($self) = @_;
2607 2591
2608 # TODO: use animation 2592 $self->{other}->toggle_visibility;
2609 if ($self->{state} = !$self->{state}) {
2610 $CFClient::UI::ROOT->add ($self->{other});
2611 $self->{other}->move ($self->coord2global (0, $self->{h}));
2612 $self->_emit ("open");
2613 } else {
2614 $CFClient::UI::ROOT->remove ($self->{other});
2615 $self->_emit ("close");
2616 }
2617
2618 $self->_emit (changed => $self->{state});
2619} 2593}
2620 2594
2621############################################################################# 2595#############################################################################
2622 2596
2623package CFClient::UI::Tooltip; 2597package CFClient::UI::Tooltip;
2655 2629
2656 ($w + 4, $h + 4) 2630 ($w + 4, $h + 4)
2657} 2631}
2658 2632
2659sub size_allocate { 2633sub size_allocate {
2660 my ($self, $w, $h) = @_; 2634 my ($self, $w, $h, $changed) = @_;
2661 2635
2636 return unless $changed;
2637
2662 $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 });
2663} 2657}
2664 2658
2665sub _draw { 2659sub _draw {
2666 my ($self) = @_; 2660 my ($self) = @_;
2667 2661
2684 glVertex $w, $h; 2678 glVertex $w, $h;
2685 glVertex $w, 0; 2679 glVertex $w, 0;
2686 glEnd; 2680 glEnd;
2687 2681
2688 glTranslate 2 - 0.375, 2 - 0.375; 2682 glTranslate 2 - 0.375, 2 - 0.375;
2683
2689 $self->SUPER::_draw; 2684 $self->SUPER::_draw;
2690} 2685}
2691 2686
2692############################################################################# 2687#############################################################################
2693 2688
2807 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2802 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2808 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2803 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2809 2804
2810 $row++; 2805 $row++;
2811 } 2806 }
2812}
2813
2814sub size_request {
2815 my ($self) = @_;
2816 ($self->{req_w}, $self->{req_h});
2817} 2807}
2818 2808
2819############################################################################# 2809#############################################################################
2820 2810
2821package CFClient::UI::Menu; 2811package CFClient::UI::Menu;
3007use CFClient::OpenGL; 2997use CFClient::OpenGL;
3008 2998
3009sub new { 2999sub new {
3010 my $class = shift; 3000 my $class = shift;
3011 3001
3012 $class->SUPER::new ( 3002 my $self = $class->SUPER::new (
3013 visible => 1, 3003 visible => 1,
3014 @_, 3004 @_,
3015 ) 3005 );
3006
3007 Scalar::Util::weaken ($self->{root} = $self);
3008
3009 $self
3016} 3010}
3017 3011
3018sub configure { 3012sub configure {
3019 my ($self, $x, $y, $w, $h) = @_; 3013 my ($self, $x, $y, $w, $h) = @_;
3020 3014
3021 $self->{w} = $w; 3015 $self->{w} = $w;
3022 $self->{h} = $h; 3016 $self->{h} = $h;
3023} 3017}
3024 3018
3025sub check_size { 3019sub reconfigure {
3026 my ($self) = @_; 3020 my ($self) = @_;
3027 3021
3022 $self->SUPER::reconfigure;
3023
3028 $self->size_allocate ($self->{w}, $self->{h}) 3024 $self->size_allocate ($self->{w}, $self->{h}, 1)
3029 if $self->{w}; 3025 if $self->{w};
3030} 3026}
3031 3027
3032sub size_request { 3028sub size_request {
3033 my ($self) = @_; 3029 my ($self) = @_;
3034 3030
3035 ($self->{w}, $self->{h}) 3031 ($self->{w}, $self->{h})
3036} 3032}
3037 3033
3038sub size_allocate { 3034sub size_allocate {
3039 my ($self, $w, $h) = @_; 3035 my ($self, $w, $h, $changed) = @_;
3040 3036
3041 for my $child ($self->children) { 3037 for my $child ($self->children) {
3042 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)};
3043 3039
3044 $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
3045 if exists $child->{req_x}; 3041 if exists $child->{req_x};
3046 3042
3047 $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
3048 if exists $child->{req_y}; 3044 if exists $child->{req_y};
3049 3045
3050 $X = $self->{user_x} if exists $self->{user_x}; 3046 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3051 $Y = $self->{user_y} if exists $self->{user_y};
3052 3047
3053 $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;
3054 $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;
3055 3050
3056 $child->configure ($X, $Y, $W, $H); 3051 $child->configure ($X, $Y, $W, $H);
3070} 3065}
3071 3066
3072sub update { 3067sub update {
3073 my ($self) = @_; 3068 my ($self) = @_;
3074 3069
3075 $self->check_size;
3076 $::WANT_REFRESH++; 3070 $::WANT_REFRESH++;
3077} 3071}
3078 3072
3079sub add { 3073sub add {
3080 my ($self, @children) = @_; 3074 my ($self, @children) = @_;
3081
3082 for (my @widgets = @children; my $w = pop @widgets; ) {
3083 push @widgets, $w->children;
3084 $w->{root} = $self;
3085 $w->{visible} = $self->{visible} + 1;
3086 }
3087 3075
3088 for my $child (@children) { 3076 for my $child (@children) {
3089 $child->{is_toplevel} = 1; 3077 $child->{is_toplevel} = 1;
3090 3078
3091 # integerise window positions 3079 # integerise window positions
3126 while ($self->{refresh_hook}) { 3114 while ($self->{refresh_hook}) {
3127 $_->() 3115 $_->()
3128 for values %{delete $self->{refresh_hook}}; 3116 for values %{delete $self->{refresh_hook}};
3129 } 3117 }
3130 3118
3131 if ($self->{check_size}) { 3119 if ($self->{realloc}) {
3132 my @queue; 3120 my @queue;
3133 3121
3134 for (;;) { 3122 while () {
3135 if ($self->{check_size}) { 3123 if ($self->{realloc}) {
3136 #TODO use array-of-depth approach 3124 #TODO use array-of-depth approach
3137 3125
3138 @queue = sort { $a->{visible} <=> $b->{visible} } 3126 @queue = sort { $a->{visible} <=> $b->{visible} }
3139 @queue, values %{delete $self->{check_size}}; 3127 @queue, values %{delete $self->{realloc}};
3140 } 3128 }
3141 3129
3142 my $widget = pop @queue || last; 3130 my $widget = pop @queue || last;
3143 3131
3144 defined $widget->{visible} or last; # do not resize invisible widgets 3132 $widget->{visible} or last; # do not resize invisible widgets
3145 3133
3146 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3134 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3147 ? @$widget{qw(user_w user_h)} 3135 ? @$widget{qw(def_w def_h)}
3148 : $widget->size_request; 3136 : $widget->size_request;
3149 3137
3150 if (delete $widget->{force_alloc}
3151 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3152 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#
3153 3139
3154 $widget->{req_w} = $w; 3140 $widget->{req_w} = $w;
3155 $widget->{req_h} = $h; 3141 $widget->{req_h} = $h;
3156 3142
3157 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3143 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3158 3144
3159 $widget->{parent}->check_size 3145 push @queue, $widget->{parent}
3160 if $widget->{parent}; 3146 if $widget->{parent};
3161 }
3162 } 3147 }
3163 } 3148 }
3164 3149
3165 while ($self->{size_alloc}) { 3150 while (my $size_alloc = delete $self->{size_alloc}) {
3166 for (values %{delete $self->{size_alloc}}) { 3151 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3167 my ($widget, $w, $h) = @$_; 3152 values %$size_alloc;
3153
3154 while () {
3155 my ($widget, $w, $h) = @{ pop @queue or last };
3168 3156
3169 $w = 0 if $w < 0; 3157 $w = 0 if $w < 0;
3170 $h = 0 if $h < 0; 3158 $h = 0 if $h < 0;
3171 3159
3160 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3161
3172 $widget->{w} = $w; 3162 $widget->{w} = $w;
3173 $widget->{h} = $h; 3163 $widget->{h} = $h;
3164
3174 $widget->emit (size_allocate => $w, $h); 3165 $widget->emit (size_allocate => $w, $h, $changed);
3175 } 3166 }
3176 } 3167 }
3177 3168
3178 while ($self->{post_alloc_hook}) { 3169 while ($self->{post_alloc_hook}) {
3179 $_->() 3170 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines