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.245 by elmex, Sat May 27 21:15:57 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}) {
187 can_events => 1, 206 can_events => 1,
188 @_ 207 @_
189 }, $class; 208 }, $class;
190 209
191 for (keys %$self) { 210 for (keys %$self) {
192 if (/^connect_(.*)$/) { 211 if (/^on_(.*)$/) {
193 $self->connect ($1 => delete $self->{$_}); 212 $self->connect ($1 => delete $self->{$_});
194 } 213 }
195 } 214 }
196 215
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 216 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198 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 }
224
199 $self 225 $self
226}
227
228sub toggle_visibility {
229 my ($self) = @_;
230
231 if ($self->{visible}) {
232 $self->hide;
233 } else {
234 $self->show;
235 }
200} 236}
201 237
202sub destroy { 238sub destroy {
203 my ($self) = @_; 239 my ($self) = @_;
204 240
227} 263}
228 264
229sub set_invisible { 265sub set_invisible {
230 my ($self) = @_; 266 my ($self) = @_;
231 267
268 return unless $self->{visible};
269
232 # broken show/hide model 270 # broken show/hide model
233 271
272 delete $self->{root};
234 delete $self->{visible}; 273 delete $self->{visible};
235 274
236 undef $GRAB if $GRAB == $self; 275 undef $GRAB if $GRAB == $self;
237 undef $HOVER if $HOVER == $self; 276 undef $HOVER if $HOVER == $self;
238 277
239 CFClient::UI::check_tooltip 278 CFClient::UI::check_tooltip
240 if $CFClient::UI::TOOLTIP->{owner} == $self; 279 if $CFClient::UI::TOOLTIP->{owner} == $self;
241 280
242 $self->focus_out; 281 $self->focus_out;
282
283 $self->emit (visibility_change => 0);
243} 284}
244 285
245sub hide { 286sub hide {
246 my ($self) = @_; 287 my ($self) = @_;
247 288
459sub set_parent { 500sub set_parent {
460 my ($self, $parent) = @_; 501 my ($self, $parent) = @_;
461 502
462 Scalar::Util::weaken ($self->{parent} = $parent); 503 Scalar::Util::weaken ($self->{parent} = $parent);
463 504
505 if ($parent->{visible} || 1) {
464 $self->{root} = $parent->{root}; 506 $self->{root} = $parent->{root};
465 $self->{visible} = $parent->{visible}; 507 $self->{visible} = $parent->{visible} + 1;
508
509 $self->emit (visibility_change => 1)
510 unless $self->{parent}{visible};
511 }
466 512
467 # TODO: req_w _does_change after ->reconfigure 513 # TODO: req_w _does_change after ->reconfigure
468 $self->check_size 514 $self->check_size
469 unless exists $self->{req_w}; 515 unless exists $self->{req_w};
470 516
500sub emit { 546sub emit {
501 my ($self, $signal, @args) = @_; 547 my ($self, $signal, @args) = @_;
502 548
503 $self->_emit ($signal, @args) 549 $self->_emit ($signal, @args)
504 || $self->$signal (@args); 550 || $self->$signal (@args);
551}
552
553sub visibility_change {
554 #my ($self, $visible) = @_;
505} 555}
506 556
507sub DESTROY { 557sub DESTROY {
508 my ($self) = @_; 558 my ($self) = @_;
509 559
857 my $class = shift; 907 my $class = shift;
858 908
859 my $self; 909 my $self;
860 910
861 my $slider = new CFClient::UI::Slider 911 my $slider = new CFClient::UI::Slider
862 vertical => 1, 912 vertical => 1,
863 range => [0, 0, 1, 0.01], # HACK fix 913 range => [0, 0, 1, 0.01], # HACK fix
864 connect_changed => sub { 914 on_changed => sub {
865 $self->{vp}->set_offset (0, $_[1]); 915 $self->{vp}->set_offset (0, $_[1]);
866 }, 916 },
867 ; 917 ;
868 918
869 $self = $class->SUPER::new ( 919 $self = $class->SUPER::new (
1024 my ($ev, $x, $y) = @_; 1074 my ($ev, $x, $y) = @_;
1025 1075
1026 my $dx = $ev->{x} - $ox; 1076 my $dx = $ev->{x} - $ox;
1027 my $dy = $ev->{y} - $oy; 1077 my $dy = $ev->{y} - $oy;
1028 1078
1079 $self->{user_x} = $wx + $dx * $mx;
1080 $self->{user_y} = $wy + $dy * $my;
1029 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1081 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
1030 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1082 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
1031 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1083 $self->move ($self->{user_x}, $self->{user_y});
1032 $self->check_size; 1084 $self->check_size;
1033 }; 1085 };
1034 1086
1035 } elsif ($lr ^ $td) { 1087 } elsif ($lr ^ $td) {
1036 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1088 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1039 $self->{motion} = sub { 1091 $self->{motion} = sub {
1040 my ($ev, $x, $y) = @_; 1092 my ($ev, $x, $y) = @_;
1041 1093
1042 ($x, $y) = ($ev->{x}, $ev->{y}); 1094 ($x, $y) = ($ev->{x}, $ev->{y});
1043 1095
1044 $self->move ($bx + $x - $ox, $by + $y - $oy); 1096 $self->{user_x} = $bx + $x - $ox;
1097 $self->{user_y} = $by + $y - $oy;
1098 $self->move ($self->{user_x}, $self->{user_y});
1045 $self->update; 1099 $self->update;
1046 }; 1100 };
1047 } 1101 }
1048} 1102}
1049 1103
2543 2597
2544sub new { 2598sub new {
2545 my $class = shift; 2599 my $class = shift;
2546 2600
2547 my $self = $class->SUPER::new ( 2601 my $self = $class->SUPER::new (
2548 state => 0, 2602 state => 0,
2549 connect_activate => \&toggle_flopper, 2603 on_activate => \&toggle_flopper,
2550 @_ 2604 @_
2551 ); 2605 );
2552 2606
2553 if ($self->{state}) {
2554 $self->{state} = 0;
2555 $self->toggle_flopper;
2556 }
2557
2558 $self 2607 $self
2559} 2608}
2560 2609
2561sub toggle_flopper { 2610sub toggle_flopper {
2562 my ($self) = @_; 2611 my ($self) = @_;
2563 2612
2564 # TODO: use animation 2613 $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} 2614}
2576 2615
2577############################################################################# 2616#############################################################################
2578 2617
2579package CFClient::UI::Tooltip; 2618package CFClient::UI::Tooltip;
2964 3003
2965sub new { 3004sub new {
2966 my $class = shift; 3005 my $class = shift;
2967 3006
2968 $class->SUPER::new ( 3007 $class->SUPER::new (
3008 visible => 1,
2969 @_, 3009 @_,
2970 ) 3010 )
2971} 3011}
2972 3012
2973sub configure { 3013sub configure {
3000 if exists $child->{req_x}; 3040 if exists $child->{req_x};
3001 3041
3002 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3042 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3003 if exists $child->{req_y}; 3043 if exists $child->{req_y};
3004 3044
3045 $X = $self->{user_x} if exists $self->{user_x};
3046 $Y = $self->{user_y} if exists $self->{user_y};
3047
3005 $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;
3006 $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;
3007 3050
3008 $child->configure ($X, $Y, $W, $H); 3051 $child->configure ($X, $Y, $W, $H);
3009 } 3052 }
3031sub add { 3074sub add {
3032 my ($self, @children) = @_; 3075 my ($self, @children) = @_;
3033 3076
3034 for (my @widgets = @children; my $w = pop @widgets; ) { 3077 for (my @widgets = @children; my $w = pop @widgets; ) {
3035 push @widgets, $w->children; 3078 push @widgets, $w->children;
3036 $w->{root} = $self; 3079 $w->{root} = $self;
3037 $w->{visible} = 1; 3080 $w->{visible} = $self->{visible} + 1;
3038 } 3081 }
3039 3082
3040 for my $child (@children) { 3083 for my $child (@children) {
3041 $child->{is_toplevel} = 1; 3084 $child->{is_toplevel} = 1;
3042 3085
3079 $_->() 3122 $_->()
3080 for values %{delete $self->{refresh_hook}}; 3123 for values %{delete $self->{refresh_hook}};
3081 } 3124 }
3082 3125
3083 if ($self->{check_size}) { 3126 if ($self->{check_size}) {
3084 my @queue = ([], []); 3127 my @queue;
3085 3128
3086 for (;;) { 3129 for (;;) {
3087 if ($self->{check_size}) { 3130 if ($self->{check_size}) {
3088 # heuristic: check containers last 3131 #TODO use array-of-depth approach
3089 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3132
3133 @queue = sort { $a->{visible} <=> $b->{visible} }
3090 for values %{delete $self->{check_size}} 3134 @queue, values %{delete $self->{check_size}};
3091 } 3135 }
3092 3136
3093 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3137 my $widget = pop @queue || last;
3138
3139 defined $widget->{visible} or last; # do not resize invisible widgets
3094 3140
3095 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3141 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3096 ? @$widget{qw(user_w user_h)} 3142 ? @$widget{qw(user_w user_h)}
3097 : $widget->size_request; 3143 : $widget->size_request;
3098 3144

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines