… | |
… | |
7 | |
7 | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $TOPLEVEL; |
12 | our $ROOT; |
13 | our $BUTTON_STATE; |
13 | our $BUTTON_STATE; |
14 | |
14 | |
15 | # class methods for events |
15 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
16 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
… | |
… | |
24 | sub feed_sdl_button_down_event { |
24 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
25 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
27 | |
27 | |
28 | if (!$BUTTON_STATE) { |
28 | if (!$BUTTON_STATE) { |
29 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
29 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
30 | |
31 | $GRAB = $widget; |
31 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
32 | $GRAB->update if $GRAB; |
33 | } |
33 | } |
34 | |
34 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
36 | |
36 | |
37 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
38 | } |
39 | |
39 | |
40 | sub feed_sdl_button_up_event { |
40 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
41 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
43 | |
43 | |
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
45 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
47 | |
47 | |
48 | $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
49 | |
50 | if (!$BUTTON_STATE) { |
50 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
51 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
52 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
53 | $GRAB->update if $GRAB; |
… | |
… | |
56 | |
56 | |
57 | sub feed_sdl_motion_event { |
57 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
58 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
60 | |
60 | |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
62 | |
63 | if ($widget != $HOVER) { |
63 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
64 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
65 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
66 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
68 | } |
69 | |
69 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
71 | } |
71 | } |
72 | |
72 | |
73 | # convert position array to integers |
73 | # convert position array to integers |
74 | sub harmonize { |
74 | sub harmonize { |
75 | my ($vals) = @_; |
75 | my ($vals) = @_; |
… | |
… | |
146 | my ($self, $x, $y, $w, $h) = @_; |
146 | my ($self, $x, $y, $w, $h) = @_; |
147 | |
147 | |
148 | $self->_size_allocate ($x, $y, $w, $h); |
148 | $self->_size_allocate ($x, $y, $w, $h); |
149 | } |
149 | } |
150 | |
150 | |
|
|
151 | # return top left coordinates |
|
|
152 | sub _topleft { |
|
|
153 | my ($self, $x, $y) = @_; |
|
|
154 | |
|
|
155 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
|
|
156 | } |
|
|
157 | |
151 | # translate global coordinates to local coordinate system |
158 | # translate global coordinates to local coordinate system |
152 | sub translate { |
159 | sub coord2local { |
153 | my ($self, $x, $y) = @_; |
160 | my ($self, $x, $y) = @_; |
154 | |
161 | |
155 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
162 | my ($X, $Y) = $self->_topleft; |
|
|
163 | ($x - $X, $y - $Y) |
|
|
164 | } |
|
|
165 | |
|
|
166 | # translate local coordinates to global coordinate system |
|
|
167 | sub coord2global { |
|
|
168 | my ($self, $x, $y) = @_; |
|
|
169 | |
|
|
170 | my ($X, $Y) = $self->_topleft; |
|
|
171 | ($x + $X, $y + $Y) |
156 | } |
172 | } |
157 | |
173 | |
158 | sub focus_in { |
174 | sub focus_in { |
159 | my ($self) = @_; |
175 | my ($self) = @_; |
160 | |
176 | |
… | |
… | |
233 | |
249 | |
234 | () |
250 | () |
235 | } |
251 | } |
236 | |
252 | |
237 | sub set_parent { |
253 | sub set_parent { |
238 | my ($self, $par) = @_; |
254 | my ($self, $parent) = @_; |
239 | |
255 | |
240 | $self->{parent} = $par; |
|
|
241 | Scalar::Util::weaken $self->{parent}; |
256 | Scalar::Util::weaken ($self->{parent} = $parent); |
242 | } |
|
|
243 | |
|
|
244 | sub get_parent { |
|
|
245 | $_[0]->{parent} |
|
|
246 | } |
257 | } |
247 | |
258 | |
248 | sub update { |
259 | sub update { |
249 | my ($self) = @_; |
260 | my ($self) = @_; |
250 | |
261 | |
… | |
… | |
339 | |
350 | |
340 | $self |
351 | $self |
341 | } |
352 | } |
342 | |
353 | |
343 | sub add { |
354 | sub add { |
344 | my ($self, $chld) = @_; |
355 | my ($self, $child) = @_; |
345 | |
356 | |
346 | $chld->set_parent ($self); |
357 | $child->set_parent ($self); |
|
|
358 | |
|
|
359 | use sort 'stable'; |
347 | |
360 | |
348 | $self->{children} = [ |
361 | $self->{children} = [ |
349 | sort { $a->{z} <=> $b->{z} } |
362 | sort { $a->{z} <=> $b->{z} } |
350 | @{$self->{children}}, $chld |
363 | @{$self->{children}}, $child |
351 | ]; |
364 | ]; |
352 | |
365 | |
353 | $self->{w} = $self->{h} = -1; |
366 | $self->{w} = $self->{h} = -1; |
354 | $self->update; |
367 | $self->update; |
355 | } |
368 | } |
… | |
… | |
685 | @_ |
698 | @_ |
686 | ) |
699 | ) |
687 | } |
700 | } |
688 | |
701 | |
689 | sub add { |
702 | sub add { |
690 | my ($self, $x, $y, $chld) = @_; |
703 | my ($self, $x, $y, $child) = @_; |
691 | |
704 | |
|
|
705 | $child->set_parent ($self); |
692 | $self->{children}[$y][$x] = $chld; |
706 | $self->{children}[$y][$x] = $child; |
693 | $chld->set_parent ($self); |
|
|
694 | |
707 | |
695 | $self->{w} = $self->{h} = -1; |
708 | $self->{w} = $self->{h} = -1; |
|
|
709 | $self->update; |
|
|
710 | } |
|
|
711 | |
|
|
712 | # TODO: move to container class maybe? send childs a signal on removal? |
|
|
713 | sub clear { |
|
|
714 | my ($self) = @_; |
|
|
715 | |
|
|
716 | delete $self->{children}; |
696 | $self->update; |
717 | $self->update; |
697 | } |
718 | } |
698 | |
719 | |
699 | sub get_wh { |
720 | sub get_wh { |
700 | my ($self) = @_; |
721 | my ($self) = @_; |
… | |
… | |
1575 | |
1596 | |
1576 | return unless $self->{h} > 0; |
1597 | return unless $self->{h} > 0; |
1577 | |
1598 | |
1578 | delete $self->{texture}; |
1599 | delete $self->{texture}; |
1579 | |
1600 | |
1580 | $TOPLEVEL->on_refresh ($self, sub { |
1601 | $ROOT->on_refresh ($self, sub { |
1581 | if (delete $self->{need_reflow}) { |
1602 | if (delete $self->{need_reflow}) { |
1582 | my $height = 0; |
1603 | my $height = 0; |
1583 | |
1604 | |
1584 | $height += $_->[0] = $self->text_height ($_->[2]) |
1605 | $height += $_->[0] = $self->text_height ($_->[2]) |
1585 | for @{$self->{par}}; |
1606 | for @{$self->{par}}; |
… | |
… | |
1945 | sub toggle_flopper { |
1966 | sub toggle_flopper { |
1946 | my ($self) = @_; |
1967 | my ($self) = @_; |
1947 | |
1968 | |
1948 | # TODO: use animation |
1969 | # TODO: use animation |
1949 | if ($self->{state} = !$self->{state}) { |
1970 | if ($self->{state} = !$self->{state}) { |
1950 | $CFClient::UI::TOPLEVEL->add ($self->{other}); |
1971 | $CFClient::UI::ROOT->add ($self->{other}); |
1951 | $self->{other}->move ( |
1972 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
1952 | ($::WIDTH - $self->{other}{w}) * 0.5, |
1973 | $self->emit ("open"); |
1953 | ($::HEIGHT - $self->{other}{h}) * 0.5, |
|
|
1954 | ); |
|
|
1955 | } else { |
1974 | } else { |
1956 | $CFClient::UI::TOPLEVEL->remove ($self->{other}); |
1975 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
1976 | $self->emit ("close"); |
1957 | } |
1977 | } |
1958 | } |
|
|
1959 | |
1978 | |
1960 | ############################################################################# |
1979 | $self->emit (changed => $self->{state}); |
|
|
1980 | } |
1961 | |
1981 | |
|
|
1982 | ############################################################################# |
|
|
1983 | |
1962 | package CFClient::UI::Toplevel; |
1984 | package CFClient::UI::Root; |
1963 | |
1985 | |
1964 | our @ISA = CFClient::UI::Container::; |
1986 | our @ISA = CFClient::UI::Container::; |
1965 | |
1987 | |
1966 | use SDL::OpenGL; |
1988 | use SDL::OpenGL; |
1967 | |
1989 | |
… | |
… | |
1976 | |
1998 | |
1977 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1999 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1978 | for @{$self->{children}}; |
2000 | for @{$self->{children}}; |
1979 | } |
2001 | } |
1980 | |
2002 | |
1981 | sub translate { |
2003 | sub _topleft { |
1982 | my ($self, $x, $y) = @_; |
2004 | my ($self, $x, $y) = @_; |
1983 | |
2005 | |
1984 | ($x, $y) |
2006 | ($x, $y) |
1985 | } |
2007 | } |
1986 | |
2008 | |
… | |
… | |
2027 | |
2049 | |
2028 | ############################################################################# |
2050 | ############################################################################# |
2029 | |
2051 | |
2030 | package CFClient::UI; |
2052 | package CFClient::UI; |
2031 | |
2053 | |
2032 | $TOPLEVEL = new CFClient::UI::Toplevel; |
2054 | $ROOT = new CFClient::UI::Root; |
2033 | |
2055 | |
2034 | 1 |
2056 | 1 |
2035 | |
2057 | |