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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines