… | |
… | |
9 | use CFClient; |
9 | use CFClient; |
10 | use CFClient::Texture; |
10 | use CFClient::Texture; |
11 | |
11 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
13 | |
|
|
14 | our $LAYOUT; |
14 | our $ROOT; |
15 | our $ROOT; |
15 | our $TOOLTIP; |
16 | our $TOOLTIP; |
16 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
17 | |
18 | |
18 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
|
|
20 | |
|
|
21 | sub 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 | |
|
|
34 | sub set_layout { |
|
|
35 | my ($layout) = @_; |
|
|
36 | $LAYOUT = $layout; |
|
|
37 | } |
19 | |
38 | |
20 | sub check_tooltip { |
39 | sub 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 | |
|
|
228 | sub toggle_visibility { |
|
|
229 | my ($self) = @_; |
|
|
230 | |
|
|
231 | if ($self->{visible}) { |
|
|
232 | $self->hide; |
|
|
233 | } else { |
|
|
234 | $self->show; |
|
|
235 | } |
200 | } |
236 | } |
201 | |
237 | |
202 | sub destroy { |
238 | sub destroy { |
203 | my ($self) = @_; |
239 | my ($self) = @_; |
204 | |
240 | |
… | |
… | |
227 | } |
263 | } |
228 | |
264 | |
229 | sub set_invisible { |
265 | sub 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 | |
234 | delete $self->{root}; |
272 | delete $self->{root}; |
235 | delete $self->{visible}; |
273 | delete $self->{visible}; |
236 | |
274 | |
… | |
… | |
239 | |
277 | |
240 | CFClient::UI::check_tooltip |
278 | CFClient::UI::check_tooltip |
241 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
279 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
242 | |
280 | |
243 | $self->focus_out; |
281 | $self->focus_out; |
|
|
282 | |
|
|
283 | $self->emit (visibility_change => 0); |
244 | } |
284 | } |
245 | |
285 | |
246 | sub hide { |
286 | sub hide { |
247 | my ($self) = @_; |
287 | my ($self) = @_; |
248 | |
288 | |
… | |
… | |
460 | sub set_parent { |
500 | sub set_parent { |
461 | my ($self, $parent) = @_; |
501 | my ($self, $parent) = @_; |
462 | |
502 | |
463 | Scalar::Util::weaken ($self->{parent} = $parent); |
503 | Scalar::Util::weaken ($self->{parent} = $parent); |
464 | |
504 | |
|
|
505 | if ($parent->{visible} || 1) { |
465 | $self->{root} = $parent->{root}; |
506 | $self->{root} = $parent->{root}; |
466 | $self->{visible} = $parent->{visible} + 1; |
507 | $self->{visible} = $parent->{visible} + 1; |
|
|
508 | |
|
|
509 | $self->emit (visibility_change => 1) |
|
|
510 | unless $self->{parent}{visible}; |
|
|
511 | } |
467 | |
512 | |
468 | # TODO: req_w _does_change after ->reconfigure |
513 | # TODO: req_w _does_change after ->reconfigure |
469 | $self->check_size |
514 | $self->check_size |
470 | unless exists $self->{req_w}; |
515 | unless exists $self->{req_w}; |
471 | |
516 | |
… | |
… | |
501 | sub emit { |
546 | sub emit { |
502 | my ($self, $signal, @args) = @_; |
547 | my ($self, $signal, @args) = @_; |
503 | |
548 | |
504 | $self->_emit ($signal, @args) |
549 | $self->_emit ($signal, @args) |
505 | || $self->$signal (@args); |
550 | || $self->$signal (@args); |
|
|
551 | } |
|
|
552 | |
|
|
553 | sub visibility_change { |
|
|
554 | #my ($self, $visible) = @_; |
506 | } |
555 | } |
507 | |
556 | |
508 | sub DESTROY { |
557 | sub DESTROY { |
509 | my ($self) = @_; |
558 | my ($self) = @_; |
510 | |
559 | |
… | |
… | |
858 | my $class = shift; |
907 | my $class = shift; |
859 | |
908 | |
860 | my $self; |
909 | my $self; |
861 | |
910 | |
862 | my $slider = new CFClient::UI::Slider |
911 | my $slider = new CFClient::UI::Slider |
863 | vertical => 1, |
912 | vertical => 1, |
864 | range => [0, 0, 1, 0.01], # HACK fix |
913 | range => [0, 0, 1, 0.01], # HACK fix |
865 | connect_changed => sub { |
914 | on_changed => sub { |
866 | $self->{vp}->set_offset (0, $_[1]); |
915 | $self->{vp}->set_offset (0, $_[1]); |
867 | }, |
916 | }, |
868 | ; |
917 | ; |
869 | |
918 | |
870 | $self = $class->SUPER::new ( |
919 | $self = $class->SUPER::new ( |
… | |
… | |
1025 | my ($ev, $x, $y) = @_; |
1074 | my ($ev, $x, $y) = @_; |
1026 | |
1075 | |
1027 | my $dx = $ev->{x} - $ox; |
1076 | my $dx = $ev->{x} - $ox; |
1028 | my $dy = $ev->{y} - $oy; |
1077 | my $dy = $ev->{y} - $oy; |
1029 | |
1078 | |
|
|
1079 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1080 | $self->{user_y} = $wy + $dy * $my; |
1030 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1081 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1031 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1082 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1032 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
1083 | $self->move ($self->{user_x}, $self->{user_y}); |
1033 | $self->check_size; |
1084 | $self->check_size; |
1034 | }; |
1085 | }; |
1035 | |
1086 | |
1036 | } elsif ($lr ^ $td) { |
1087 | } elsif ($lr ^ $td) { |
1037 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1088 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
… | |
… | |
1040 | $self->{motion} = sub { |
1091 | $self->{motion} = sub { |
1041 | my ($ev, $x, $y) = @_; |
1092 | my ($ev, $x, $y) = @_; |
1042 | |
1093 | |
1043 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1094 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1044 | |
1095 | |
1045 | $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}); |
1046 | $self->update; |
1099 | $self->update; |
1047 | }; |
1100 | }; |
1048 | } |
1101 | } |
1049 | } |
1102 | } |
1050 | |
1103 | |
… | |
… | |
2544 | |
2597 | |
2545 | sub new { |
2598 | sub new { |
2546 | my $class = shift; |
2599 | my $class = shift; |
2547 | |
2600 | |
2548 | my $self = $class->SUPER::new ( |
2601 | my $self = $class->SUPER::new ( |
2549 | state => 0, |
2602 | state => 0, |
2550 | connect_activate => \&toggle_flopper, |
2603 | on_activate => \&toggle_flopper, |
2551 | @_ |
2604 | @_ |
2552 | ); |
2605 | ); |
2553 | |
2606 | |
2554 | if ($self->{state}) { |
|
|
2555 | $self->{state} = 0; |
|
|
2556 | $self->toggle_flopper; |
|
|
2557 | } |
|
|
2558 | |
|
|
2559 | $self |
2607 | $self |
2560 | } |
2608 | } |
2561 | |
2609 | |
2562 | sub toggle_flopper { |
2610 | sub toggle_flopper { |
2563 | my ($self) = @_; |
2611 | my ($self) = @_; |
2564 | |
2612 | |
2565 | # TODO: use animation |
2613 | $self->{other}->toggle_visibility; |
2566 | if ($self->{state} = !$self->{state}) { |
|
|
2567 | $CFClient::UI::ROOT->add ($self->{other}); |
|
|
2568 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
|
|
2569 | $self->_emit ("open"); |
|
|
2570 | } else { |
|
|
2571 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
2572 | $self->_emit ("close"); |
|
|
2573 | } |
|
|
2574 | |
|
|
2575 | $self->_emit (changed => $self->{state}); |
|
|
2576 | } |
2614 | } |
2577 | |
2615 | |
2578 | ############################################################################# |
2616 | ############################################################################# |
2579 | |
2617 | |
2580 | package CFClient::UI::Tooltip; |
2618 | package CFClient::UI::Tooltip; |
… | |
… | |
3002 | if exists $child->{req_x}; |
3040 | if exists $child->{req_x}; |
3003 | |
3041 | |
3004 | $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 |
3005 | if exists $child->{req_y}; |
3043 | if exists $child->{req_y}; |
3006 | |
3044 | |
|
|
3045 | $X = $self->{user_x} if exists $self->{user_x}; |
|
|
3046 | $Y = $self->{user_y} if exists $self->{user_y}; |
|
|
3047 | |
3007 | $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; |
3008 | $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; |
3009 | |
3050 | |
3010 | $child->configure ($X, $Y, $W, $H); |
3051 | $child->configure ($X, $Y, $W, $H); |
3011 | } |
3052 | } |
… | |
… | |
3081 | $_->() |
3122 | $_->() |
3082 | for values %{delete $self->{refresh_hook}}; |
3123 | for values %{delete $self->{refresh_hook}}; |
3083 | } |
3124 | } |
3084 | |
3125 | |
3085 | if ($self->{check_size}) { |
3126 | if ($self->{check_size}) { |
3086 | my @queue = ([], []); |
3127 | my @queue; |
3087 | |
3128 | |
3088 | for (;;) { |
3129 | for (;;) { |
3089 | if ($self->{check_size}) { |
3130 | if ($self->{check_size}) { |
3090 | # heuristic: check containers last |
3131 | #TODO use array-of-depth approach |
3091 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
3132 | |
|
|
3133 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3092 | for values %{delete $self->{check_size}} |
3134 | @queue, values %{delete $self->{check_size}}; |
3093 | } |
3135 | } |
3094 | |
3136 | |
3095 | 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 |
3096 | |
3140 | |
3097 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3141 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3098 | ? @$widget{qw(user_w user_h)} |
3142 | ? @$widget{qw(user_w user_h)} |
3099 | : $widget->size_request; |
3143 | : $widget->size_request; |
3100 | |
3144 | |