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.236 by root, Thu May 25 16:54:29 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) {
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
463 # TODO: req_w _does_change after ->reconfigure 503 # TODO: req_w _does_change after ->reconfigure
464 $self->check_size 504 $self->check_size
465 unless exists $self->{req_w}; 505 unless exists $self->{req_w};
466 506
467 $self->show; 507 $self->show;
496sub emit { 536sub emit {
497 my ($self, $signal, @args) = @_; 537 my ($self, $signal, @args) = @_;
498 538
499 $self->_emit ($signal, @args) 539 $self->_emit ($signal, @args)
500 || $self->$signal (@args); 540 || $self->$signal (@args);
541}
542
543sub visibility_change {
544 #my ($self, $visible) = @_;
501} 545}
502 546
503sub DESTROY { 547sub DESTROY {
504 my ($self) = @_; 548 my ($self) = @_;
505 549
853 my $class = shift; 897 my $class = shift;
854 898
855 my $self; 899 my $self;
856 900
857 my $slider = new CFClient::UI::Slider 901 my $slider = new CFClient::UI::Slider
858 vertical => 1, 902 vertical => 1,
859 range => [0, 0, 1, 0.01], # HACK fix 903 range => [0, 0, 1, 0.01], # HACK fix
860 connect_changed => sub { 904 on_changed => sub {
861 $self->{vp}->set_offset (0, $_[1]); 905 $self->{vp}->set_offset (0, $_[1]);
862 }, 906 },
863 ; 907 ;
864 908
865 $self = $class->SUPER::new ( 909 $self = $class->SUPER::new (
871 $self->{vp}->add ($self->{scrolled}); 915 $self->{vp}->add ($self->{scrolled});
872 $self->add ($self->{vp}); 916 $self->add ($self->{vp});
873 $self->add ($self->{slider}); 917 $self->add ($self->{slider});
874 918
875 $self 919 $self
920}
921
922sub update {
923 my ($self) = @_;
924
925 $self->SUPER::update;
926
927 # todo: overwrite size_allocate of child
928 my $child = $self->{vp}->child;
929 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
876} 930}
877 931
878sub size_allocate { 932sub size_allocate {
879 my ($self, $w, $h) = @_; 933 my ($self, $w, $h) = @_;
880 934
1010 my ($ev, $x, $y) = @_; 1064 my ($ev, $x, $y) = @_;
1011 1065
1012 my $dx = $ev->{x} - $ox; 1066 my $dx = $ev->{x} - $ox;
1013 my $dy = $ev->{y} - $oy; 1067 my $dy = $ev->{y} - $oy;
1014 1068
1069 $self->{user_x} = $wx + $dx * $mx;
1070 $self->{user_y} = $wy + $dy * $my;
1015 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1071 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
1016 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1072 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
1017 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1073 $self->move ($self->{user_x}, $self->{user_y});
1018 $self->check_size; 1074 $self->check_size;
1019 }; 1075 };
1020 1076
1021 } elsif ($lr ^ $td) { 1077 } elsif ($lr ^ $td) {
1022 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1078 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1025 $self->{motion} = sub { 1081 $self->{motion} = sub {
1026 my ($ev, $x, $y) = @_; 1082 my ($ev, $x, $y) = @_;
1027 1083
1028 ($x, $y) = ($ev->{x}, $ev->{y}); 1084 ($x, $y) = ($ev->{x}, $ev->{y});
1029 1085
1030 $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});
1031 $self->update; 1089 $self->update;
1032 }; 1090 };
1033 } 1091 }
1034} 1092}
1035 1093
1168sub size_allocate { 1226sub size_allocate {
1169 my ($self, $w, $h) = @_; 1227 my ($self, $w, $h) = @_;
1170 1228
1171 my ($ws, $hs) = $self->get_wh; 1229 my ($ws, $hs) = $self->get_wh;
1172 1230
1173 my $req_w = sum @$ws; 1231 my $req_w = (sum @$ws) || 1;
1174 my $req_h = sum @$hs; 1232 my $req_h = (sum @$hs) || 1;
1175 1233
1176 # TODO: nicer code && do row_expand 1234 # TODO: nicer code && do row_expand
1177 my @col_expand = @{$self->{col_expand}}; 1235 my @col_expand = @{$self->{col_expand}};
1178 @col_expand = (1) x @$ws unless @col_expand; 1236 @col_expand = (1) x @$ws unless @col_expand;
1179 my $col_expand = (sum @col_expand) || 1; 1237 my $col_expand = (sum @col_expand) || 1;
2133} 2191}
2134 2192
2135sub set_range { 2193sub set_range {
2136 my ($self, $range) = @_; 2194 my ($self, $range) = @_;
2137 2195
2138 $self->{range} = $range; 2196 ($range, $self->{range}) = ($self->{range}, $range);
2139 2197
2140 $self->update; 2198 $self->update
2199 if "@$range" ne "@{$self->{range}}";
2141} 2200}
2142 2201
2143sub set_value { 2202sub set_value {
2144 my ($self, $value) = @_; 2203 my ($self, $value) = @_;
2145 2204
2528 2587
2529sub new { 2588sub new {
2530 my $class = shift; 2589 my $class = shift;
2531 2590
2532 my $self = $class->SUPER::new ( 2591 my $self = $class->SUPER::new (
2533 state => 0, 2592 state => 0,
2534 connect_activate => \&toggle_flopper, 2593 on_activate => \&toggle_flopper,
2535 @_ 2594 @_
2536 ); 2595 );
2537 2596
2538 if ($self->{state}) { 2597 if ($self->{state}) {
2539 $self->{state} = 0; 2598 $self->{state} = 0;
2748 $self->{scrolled}->add (1, $row, $item->{desc_widget}); 2807 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2749 $self->{scrolled}->add (2, $row, $item->{weight_widget}); 2808 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2750 2809
2751 $row++; 2810 $row++;
2752 } 2811 }
2753
2754# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2755} 2812}
2756 2813
2757sub size_request { 2814sub size_request {
2758 my ($self) = @_; 2815 my ($self) = @_;
2759 ($self->{req_w}, $self->{req_h}); 2816 ($self->{req_w}, $self->{req_h});
2951 3008
2952sub new { 3009sub new {
2953 my $class = shift; 3010 my $class = shift;
2954 3011
2955 $class->SUPER::new ( 3012 $class->SUPER::new (
3013 visible => 1,
2956 @_, 3014 @_,
2957 ) 3015 )
2958} 3016}
2959 3017
2960sub configure { 3018sub configure {
2987 if exists $child->{req_x}; 3045 if exists $child->{req_x};
2988 3046
2989 $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
2990 if exists $child->{req_y}; 3048 if exists $child->{req_y};
2991 3049
3050 $X = $self->{user_x} if exists $self->{user_x};
3051 $Y = $self->{user_y} if exists $self->{user_y};
3052
2992 $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;
2993 $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;
2994 3055
2995 $child->configure ($X, $Y, $W, $H); 3056 $child->configure ($X, $Y, $W, $H);
2996 } 3057 }
3018sub add { 3079sub add {
3019 my ($self, @children) = @_; 3080 my ($self, @children) = @_;
3020 3081
3021 for (my @widgets = @children; my $w = pop @widgets; ) { 3082 for (my @widgets = @children; my $w = pop @widgets; ) {
3022 push @widgets, $w->children; 3083 push @widgets, $w->children;
3023 $w->{root} = $self; 3084 $w->{root} = $self;
3024 $w->{visible} = 1; 3085 $w->{visible} = $self->{visible} + 1;
3025 } 3086 }
3026 3087
3027 for my $child (@children) { 3088 for my $child (@children) {
3028 $child->{is_toplevel} = 1; 3089 $child->{is_toplevel} = 1;
3029 3090
3066 $_->() 3127 $_->()
3067 for values %{delete $self->{refresh_hook}}; 3128 for values %{delete $self->{refresh_hook}};
3068 } 3129 }
3069 3130
3070 if ($self->{check_size}) { 3131 if ($self->{check_size}) {
3071 my @queue = ([], []); 3132 my @queue;
3072 3133
3073 for (;;) { 3134 for (;;) {
3074 if ($self->{check_size}) { 3135 if ($self->{check_size}) {
3075 # heuristic: check containers last 3136 #TODO use array-of-depth approach
3076 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3137
3138 @queue = sort { $a->{visible} <=> $b->{visible} }
3077 for values %{delete $self->{check_size}} 3139 @queue, values %{delete $self->{check_size}};
3078 } 3140 }
3079 3141
3080 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
3081 3145
3082 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3146 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3083 ? @$widget{qw(user_w user_h)} 3147 ? @$widget{qw(user_w user_h)}
3084 : $widget->size_request; 3148 : $widget->size_request;
3085 3149

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines