… | |
… | |
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); |
|
|
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 | } |
198 | |
224 | |
199 | $self |
225 | $self |
200 | } |
226 | } |
201 | |
227 | |
202 | sub destroy { |
228 | sub destroy { |
… | |
… | |
229 | sub set_invisible { |
255 | sub set_invisible { |
230 | my ($self) = @_; |
256 | my ($self) = @_; |
231 | |
257 | |
232 | # broken show/hide model |
258 | # broken show/hide model |
233 | |
259 | |
|
|
260 | delete $self->{root}; |
234 | delete $self->{visible}; |
261 | delete $self->{visible}; |
235 | |
262 | |
236 | undef $GRAB if $GRAB == $self; |
263 | undef $GRAB if $GRAB == $self; |
237 | undef $HOVER if $HOVER == $self; |
264 | undef $HOVER if $HOVER == $self; |
238 | |
265 | |
… | |
… | |
460 | my ($self, $parent) = @_; |
487 | my ($self, $parent) = @_; |
461 | |
488 | |
462 | Scalar::Util::weaken ($self->{parent} = $parent); |
489 | Scalar::Util::weaken ($self->{parent} = $parent); |
463 | |
490 | |
464 | $self->{root} = $parent->{root}; |
491 | $self->{root} = $parent->{root}; |
465 | $self->{visible} = $parent->{visible}; |
492 | $self->{visible} = $parent->{visible} + 1; |
466 | |
493 | |
467 | # TODO: req_w _does_change after ->reconfigure |
494 | # TODO: req_w _does_change after ->reconfigure |
468 | $self->check_size |
495 | $self->check_size |
469 | unless exists $self->{req_w}; |
496 | unless exists $self->{req_w}; |
470 | |
497 | |
… | |
… | |
857 | my $class = shift; |
884 | my $class = shift; |
858 | |
885 | |
859 | my $self; |
886 | my $self; |
860 | |
887 | |
861 | my $slider = new CFClient::UI::Slider |
888 | my $slider = new CFClient::UI::Slider |
862 | vertical => 1, |
889 | vertical => 1, |
863 | range => [0, 0, 1, 0.01], # HACK fix |
890 | range => [0, 0, 1, 0.01], # HACK fix |
864 | connect_changed => sub { |
891 | on_changed => sub { |
865 | $self->{vp}->set_offset (0, $_[1]); |
892 | $self->{vp}->set_offset (0, $_[1]); |
866 | }, |
893 | }, |
867 | ; |
894 | ; |
868 | |
895 | |
869 | $self = $class->SUPER::new ( |
896 | $self = $class->SUPER::new ( |
… | |
… | |
1024 | my ($ev, $x, $y) = @_; |
1051 | my ($ev, $x, $y) = @_; |
1025 | |
1052 | |
1026 | my $dx = $ev->{x} - $ox; |
1053 | my $dx = $ev->{x} - $ox; |
1027 | my $dy = $ev->{y} - $oy; |
1054 | my $dy = $ev->{y} - $oy; |
1028 | |
1055 | |
|
|
1056 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1057 | $self->{user_y} = $wy + $dy * $my; |
1029 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1058 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1030 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1059 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1031 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
1060 | $self->move ($self->{user_x}, $self->{user_y}); |
1032 | $self->check_size; |
1061 | $self->check_size; |
1033 | }; |
1062 | }; |
1034 | |
1063 | |
1035 | } elsif ($lr ^ $td) { |
1064 | } elsif ($lr ^ $td) { |
1036 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1065 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
… | |
… | |
1039 | $self->{motion} = sub { |
1068 | $self->{motion} = sub { |
1040 | my ($ev, $x, $y) = @_; |
1069 | my ($ev, $x, $y) = @_; |
1041 | |
1070 | |
1042 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1071 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1043 | |
1072 | |
1044 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
1073 | $self->{user_x} = $bx + $x - $ox; |
|
|
1074 | $self->{user_y} = $by + $y - $oy; |
|
|
1075 | $self->move ($self->{user_x}, $self->{user_y}); |
1045 | $self->update; |
1076 | $self->update; |
1046 | }; |
1077 | }; |
1047 | } |
1078 | } |
1048 | } |
1079 | } |
1049 | |
1080 | |
… | |
… | |
2543 | |
2574 | |
2544 | sub new { |
2575 | sub new { |
2545 | my $class = shift; |
2576 | my $class = shift; |
2546 | |
2577 | |
2547 | my $self = $class->SUPER::new ( |
2578 | my $self = $class->SUPER::new ( |
2548 | state => 0, |
2579 | state => 0, |
2549 | connect_activate => \&toggle_flopper, |
2580 | on_activate => \&toggle_flopper, |
2550 | @_ |
2581 | @_ |
2551 | ); |
2582 | ); |
2552 | |
2583 | |
2553 | if ($self->{state}) { |
2584 | if ($self->{state}) { |
2554 | $self->{state} = 0; |
2585 | $self->{state} = 0; |
… | |
… | |
2964 | |
2995 | |
2965 | sub new { |
2996 | sub new { |
2966 | my $class = shift; |
2997 | my $class = shift; |
2967 | |
2998 | |
2968 | $class->SUPER::new ( |
2999 | $class->SUPER::new ( |
|
|
3000 | visible => 1, |
2969 | @_, |
3001 | @_, |
2970 | ) |
3002 | ) |
2971 | } |
3003 | } |
2972 | |
3004 | |
2973 | sub configure { |
3005 | sub configure { |
… | |
… | |
3031 | sub add { |
3063 | sub add { |
3032 | my ($self, @children) = @_; |
3064 | my ($self, @children) = @_; |
3033 | |
3065 | |
3034 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3066 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3035 | push @widgets, $w->children; |
3067 | push @widgets, $w->children; |
3036 | $w->{root} = $self; |
3068 | $w->{root} = $self; |
3037 | $w->{visible} = 1; |
3069 | $w->{visible} = $self->{visible} + 1; |
3038 | } |
3070 | } |
3039 | |
3071 | |
3040 | for my $child (@children) { |
3072 | for my $child (@children) { |
3041 | $child->{is_toplevel} = 1; |
3073 | $child->{is_toplevel} = 1; |
3042 | |
3074 | |
… | |
… | |
3079 | $_->() |
3111 | $_->() |
3080 | for values %{delete $self->{refresh_hook}}; |
3112 | for values %{delete $self->{refresh_hook}}; |
3081 | } |
3113 | } |
3082 | |
3114 | |
3083 | if ($self->{check_size}) { |
3115 | if ($self->{check_size}) { |
3084 | my @queue = ([], []); |
3116 | my @queue; |
3085 | |
3117 | |
3086 | for (;;) { |
3118 | for (;;) { |
3087 | if ($self->{check_size}) { |
3119 | if ($self->{check_size}) { |
3088 | # heuristic: check containers last |
3120 | #TODO use array-of-depth approach |
3089 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
3121 | |
|
|
3122 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3090 | for values %{delete $self->{check_size}} |
3123 | @queue, values %{delete $self->{check_size}}; |
3091 | } |
3124 | } |
3092 | |
3125 | |
3093 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
3126 | my $widget = pop @queue || last; |
|
|
3127 | |
|
|
3128 | defined $widget->{visible} or last; # do not resize invisible widgets |
3094 | |
3129 | |
3095 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3130 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3096 | ? @$widget{qw(user_w user_h)} |
3131 | ? @$widget{qw(user_w user_h)} |
3097 | : $widget->size_request; |
3132 | : $widget->size_request; |
3098 | |
3133 | |