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.239 by root, Thu May 25 22:29:57 2006 UTC vs.
Revision 1.244 by root, Sat May 27 21:06:09 2006 UTC

5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8 8
9use CFClient; 9use CFClient;
10use CFClient::Texture;
10 11
11our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
12 13
14our $LAYOUT;
13our $ROOT; 15our $ROOT;
14our $TOOLTIP; 16our $TOOLTIP;
15our $BUTTON_STATE; 17our $BUTTON_STATE;
16 18
17our %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}
18 38
19sub check_tooltip { 39sub check_tooltip {
20 if (!$GRAB) { 40 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
186 can_events => 1, 206 can_events => 1,
187 @_ 207 @_
188 }, $class; 208 }, $class;
189 209
190 for (keys %$self) { 210 for (keys %$self) {
191 if (/^connect_(.*)$/) { 211 if (/^on_(.*)$/) {
192 $self->connect ($1 => delete $self->{$_}); 212 $self->connect ($1 => delete $self->{$_});
193 } 213 }
194 } 214 }
195 215
196 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 216 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
217
218 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
219 $self->{user_x} = $layout->{x} * $::WIDTH;
220 $self->{user_y} = $layout->{y} * $::HEIGHT;
221 $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
222 $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
223 }
197 224
198 $self 225 $self
199} 226}
200 227
201sub destroy { 228sub destroy {
226} 253}
227 254
228sub set_invisible { 255sub set_invisible {
229 my ($self) = @_; 256 my ($self) = @_;
230 257
258 return unless $self->{visible};
259
231 # broken show/hide model 260 # broken show/hide model
232 261
262 delete $self->{root};
233 delete $self->{visible}; 263 delete $self->{visible};
234 264
235 undef $GRAB if $GRAB == $self; 265 undef $GRAB if $GRAB == $self;
236 undef $HOVER if $HOVER == $self; 266 undef $HOVER if $HOVER == $self;
237 267
238 CFClient::UI::check_tooltip 268 CFClient::UI::check_tooltip
239 if $CFClient::UI::TOOLTIP->{owner} == $self; 269 if $CFClient::UI::TOOLTIP->{owner} == $self;
240 270
241 $self->focus_out; 271 $self->focus_out;
272
273 $self->emit (visibility_change => 0);
242} 274}
243 275
244sub hide { 276sub hide {
245 my ($self) = @_; 277 my ($self) = @_;
246 278
458sub set_parent { 490sub set_parent {
459 my ($self, $parent) = @_; 491 my ($self, $parent) = @_;
460 492
461 Scalar::Util::weaken ($self->{parent} = $parent); 493 Scalar::Util::weaken ($self->{parent} = $parent);
462 494
495 if ($parent->{visible} || 1) {
463 $self->{root} = $parent->{root}; 496 $self->{root} = $parent->{root};
464 $self->{visible} = $parent->{visible}; 497 $self->{visible} = $parent->{visible} + 1;
498
499 $self->emit (visibility_change => 1)
500 unless $self->{parent}{visible};
501 }
465 502
466 # TODO: req_w _does_change after ->reconfigure 503 # TODO: req_w _does_change after ->reconfigure
467 $self->check_size 504 $self->check_size
468 unless exists $self->{req_w}; 505 unless exists $self->{req_w};
469 506
499sub emit { 536sub emit {
500 my ($self, $signal, @args) = @_; 537 my ($self, $signal, @args) = @_;
501 538
502 $self->_emit ($signal, @args) 539 $self->_emit ($signal, @args)
503 || $self->$signal (@args); 540 || $self->$signal (@args);
541}
542
543sub visibility_change {
544 #my ($self, $visible) = @_;
504} 545}
505 546
506sub DESTROY { 547sub DESTROY {
507 my ($self) = @_; 548 my ($self) = @_;
508 549
856 my $class = shift; 897 my $class = shift;
857 898
858 my $self; 899 my $self;
859 900
860 my $slider = new CFClient::UI::Slider 901 my $slider = new CFClient::UI::Slider
861 vertical => 1, 902 vertical => 1,
862 range => [0, 0, 1, 0.01], # HACK fix 903 range => [0, 0, 1, 0.01], # HACK fix
863 connect_changed => sub { 904 on_changed => sub {
864 $self->{vp}->set_offset (0, $_[1]); 905 $self->{vp}->set_offset (0, $_[1]);
865 }, 906 },
866 ; 907 ;
867 908
868 $self = $class->SUPER::new ( 909 $self = $class->SUPER::new (
1023 my ($ev, $x, $y) = @_; 1064 my ($ev, $x, $y) = @_;
1024 1065
1025 my $dx = $ev->{x} - $ox; 1066 my $dx = $ev->{x} - $ox;
1026 my $dy = $ev->{y} - $oy; 1067 my $dy = $ev->{y} - $oy;
1027 1068
1069 $self->{user_x} = $wx + $dx * $mx;
1070 $self->{user_y} = $wy + $dy * $my;
1028 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1071 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
1029 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1072 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
1030 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1073 $self->move ($self->{user_x}, $self->{user_y});
1031 $self->check_size; 1074 $self->check_size;
1032 }; 1075 };
1033 1076
1034 } elsif ($lr ^ $td) { 1077 } elsif ($lr ^ $td) {
1035 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1078 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1038 $self->{motion} = sub { 1081 $self->{motion} = sub {
1039 my ($ev, $x, $y) = @_; 1082 my ($ev, $x, $y) = @_;
1040 1083
1041 ($x, $y) = ($ev->{x}, $ev->{y}); 1084 ($x, $y) = ($ev->{x}, $ev->{y});
1042 1085
1043 $self->move ($bx + $x - $ox, $by + $y - $oy); 1086 $self->{user_x} = $bx + $x - $ox;
1087 $self->{user_y} = $by + $y - $oy;
1088 $self->move ($self->{user_x}, $self->{user_y});
1044 $self->update; 1089 $self->update;
1045 }; 1090 };
1046 } 1091 }
1047} 1092}
1048 1093
2542 2587
2543sub new { 2588sub new {
2544 my $class = shift; 2589 my $class = shift;
2545 2590
2546 my $self = $class->SUPER::new ( 2591 my $self = $class->SUPER::new (
2547 state => 0, 2592 state => 0,
2548 connect_activate => \&toggle_flopper, 2593 on_activate => \&toggle_flopper,
2549 @_ 2594 @_
2550 ); 2595 );
2551 2596
2552 if ($self->{state}) { 2597 if ($self->{state}) {
2553 $self->{state} = 0; 2598 $self->{state} = 0;
2963 3008
2964sub new { 3009sub new {
2965 my $class = shift; 3010 my $class = shift;
2966 3011
2967 $class->SUPER::new ( 3012 $class->SUPER::new (
3013 visible => 1,
2968 @_, 3014 @_,
2969 ) 3015 )
2970} 3016}
2971 3017
2972sub configure { 3018sub configure {
2999 if exists $child->{req_x}; 3045 if exists $child->{req_x};
3000 3046
3001 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3047 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3002 if exists $child->{req_y}; 3048 if exists $child->{req_y};
3003 3049
3050 $X = $self->{user_x} if exists $self->{user_x};
3051 $Y = $self->{user_y} if exists $self->{user_y};
3052
3004 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3053 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3005 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3054 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3006 3055
3007 $child->configure ($X, $Y, $W, $H); 3056 $child->configure ($X, $Y, $W, $H);
3008 } 3057 }
3030sub add { 3079sub add {
3031 my ($self, @children) = @_; 3080 my ($self, @children) = @_;
3032 3081
3033 for (my @widgets = @children; my $w = pop @widgets; ) { 3082 for (my @widgets = @children; my $w = pop @widgets; ) {
3034 push @widgets, $w->children; 3083 push @widgets, $w->children;
3035 $w->{root} = $self; 3084 $w->{root} = $self;
3036 $w->{visible} = 1; 3085 $w->{visible} = $self->{visible} + 1;
3037 } 3086 }
3038 3087
3039 for my $child (@children) { 3088 for my $child (@children) {
3040 $child->{is_toplevel} = 1; 3089 $child->{is_toplevel} = 1;
3041 3090
3078 $_->() 3127 $_->()
3079 for values %{delete $self->{refresh_hook}}; 3128 for values %{delete $self->{refresh_hook}};
3080 } 3129 }
3081 3130
3082 if ($self->{check_size}) { 3131 if ($self->{check_size}) {
3083 my @queue = ([], []); 3132 my @queue;
3084 3133
3085 for (;;) { 3134 for (;;) {
3086 if ($self->{check_size}) { 3135 if ($self->{check_size}) {
3087 # heuristic: check containers last 3136 #TODO use array-of-depth approach
3088 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3137
3138 @queue = sort { $a->{visible} <=> $b->{visible} }
3089 for values %{delete $self->{check_size}} 3139 @queue, values %{delete $self->{check_size}};
3090 } 3140 }
3091 3141
3092 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3142 my $widget = pop @queue || last;
3143
3144 defined $widget->{visible} or last; # do not resize invisible widgets
3093 3145
3094 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3146 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3095 ? @$widget{qw(user_w user_h)} 3147 ? @$widget{qw(user_w user_h)}
3096 : $widget->size_request; 3148 : $widget->size_request;
3097 3149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines