make swfdec_bits_get_matrix and swfdec-tranform_to_matrix optionally return the inver...
[swfdec.git] / libswfdec / swfdec_button_movie.c
blob576b96341966a3caaccf068aa742ba4482e81c3f
1 /* Swfdec
2 * Copyright (C) 2006 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfdec_button_movie.h"
25 #include "swfdec_audio_event.h"
26 #include "swfdec_debug.h"
27 #include "swfdec_event.h"
28 #include "swfdec_root_movie.h"
30 G_DEFINE_TYPE (SwfdecButtonMovie, swfdec_button_movie, SWFDEC_TYPE_MOVIE)
32 static void
33 swfdec_button_movie_update_extents (SwfdecMovie *movie,
34 SwfdecRect *extents)
36 swfdec_rect_union (extents, extents,
37 &SWFDEC_GRAPHIC (SWFDEC_BUTTON_MOVIE (movie)->button)->extents);
40 /* first index is 1 for menubutton, second index is previous state,
41 * last index is current state
42 * MSB in index is mouse OUT = 0, IN = 1
43 * LSB in index is button UP = 0, DOWN = 1
45 static const SwfdecButtonCondition event_table[2][4][4] = {
46 { { -1, -1, SWFDEC_BUTTON_IDLE_TO_OVER_UP, -1 },
47 { SWFDEC_BUTTON_OUT_DOWN_TO_IDLE, -1, -1, SWFDEC_BUTTON_OUT_DOWN_TO_OVER_DOWN },
48 { SWFDEC_BUTTON_OVER_UP_TO_IDLE, -1, -1, SWFDEC_BUTTON_OVER_UP_TO_OVER_DOWN },
49 { -1, SWFDEC_BUTTON_OVER_DOWN_TO_OUT_DOWN, SWFDEC_BUTTON_OVER_DOWN_TO_OVER_UP, -1 } },
50 { { -1, -1, SWFDEC_BUTTON_IDLE_TO_OVER_UP, -1 },
51 { 0, -1, -1, SWFDEC_BUTTON_IDLE_TO_OVER_DOWN },
52 { SWFDEC_BUTTON_OVER_UP_TO_IDLE, -1, -1, SWFDEC_BUTTON_OVER_UP_TO_OVER_DOWN },
53 { -1, SWFDEC_BUTTON_OVER_DOWN_TO_IDLE, SWFDEC_BUTTON_OVER_DOWN_TO_OVER_UP, -1 } }
55 static const int sound_table[2][4][4] = {
56 { { -1, -1, 1, -1 },
57 { 0, -1, -1, -1 },
58 { 0, -1, -1, 2 },
59 { -1, -1, 3, -1 } },
60 { { -1, -1, 1, -1 },
61 { -1, -1, -1, 1 },
62 { 0, -1, -1, 2 },
63 { -1, 0, 3, -1 } }
66 static void
67 swfdec_button_movie_execute (SwfdecButtonMovie *movie,
68 SwfdecButtonCondition condition)
70 if (movie->button->menubutton) {
71 g_assert ((condition & (SWFDEC_BUTTON_OVER_DOWN_TO_OUT_DOWN \
72 | SWFDEC_BUTTON_OUT_DOWN_TO_OVER_DOWN \
73 | SWFDEC_BUTTON_OUT_DOWN_TO_IDLE)) == 0);
74 } else {
75 g_assert ((condition & (SWFDEC_BUTTON_IDLE_TO_OVER_DOWN \
76 | SWFDEC_BUTTON_OVER_DOWN_TO_IDLE)) == 0);
78 if (movie->button->events)
79 swfdec_event_list_execute (movie->button->events, SWFDEC_MOVIE (movie)->parent, condition, 0);
82 #define CONTENT_IN_FRAME(content, frame) \
83 ((content)->sequence->start <= frame && \
84 (content)->sequence->end > frame)
85 static void
86 swfdec_button_movie_change_state (SwfdecButtonMovie *movie, SwfdecButtonState state)
88 SwfdecMovie *mov = SWFDEC_MOVIE (movie);
89 GList *walk;
91 g_assert (movie->state != state);
92 SWFDEC_LOG ("changing state on button movie %p from %d to %d", movie,
93 movie->state, state);
94 /* do this in 2 loops - otherwise going down DOWN=>UP would
95 * remove children that are only in these 2 states (due to how
96 * adding them is handled)
98 for (walk = movie->button->records; walk; walk = walk->next) {
99 SwfdecContent *content = walk->data;
100 if (CONTENT_IN_FRAME (content, (guint) movie->state) &&
101 !CONTENT_IN_FRAME (content, (guint) state)) {
102 SwfdecMovie *child = swfdec_movie_find (mov, content->depth);
103 if (child)
104 swfdec_movie_remove (child);
107 for (walk = movie->button->records; walk; walk = walk->next) {
108 SwfdecContent *content = walk->data;
109 if (!CONTENT_IN_FRAME (content, (guint) movie->state) &&
110 CONTENT_IN_FRAME (content, (guint) state)) {
111 SwfdecMovie *child = swfdec_movie_find (mov, content->depth);
112 if (child) {
113 g_assert_not_reached ();
115 swfdec_movie_new (mov, content);
118 movie->state = state;
121 static void
122 swfdec_button_movie_change_mouse (SwfdecButtonMovie *movie, gboolean mouse_in, int button)
124 SwfdecButtonCondition event;
125 int sound;
127 if (movie->mouse_in == mouse_in &&
128 movie->mouse_button == button)
129 return;
130 SWFDEC_LOG ("changing mouse state %s: %s %s (%u) => %s %s (%u)\n",
131 movie->button->menubutton ? "MENU" : "BUTTON",
132 movie->mouse_in ? "IN" : "OUT", movie->mouse_button ? "DOWN" : "UP",
133 (movie->mouse_in ? 2 : 0) + movie->mouse_button,
134 mouse_in ? "IN" : "OUT", button ? "DOWN" : "UP",
135 (mouse_in ? 2 : 0) + button);
136 event = event_table[movie->button->menubutton ? 1 : 0]
137 [(movie->mouse_in ? 2 : 0) + movie->mouse_button]
138 [(mouse_in ? 2 : 0) + button];
140 g_assert (event != (guint) -1);
141 if (event != 0) {
142 SWFDEC_LOG ("emitting event for condition %u", event);
143 swfdec_button_movie_execute (movie, event);
145 sound = sound_table[movie->button->menubutton ? 1 : 0]
146 [(movie->mouse_in ? 2 : 0) + movie->mouse_button]
147 [(mouse_in ? 2 : 0) + button];
148 if (sound >= 0 && movie->button->sounds[sound]) {
149 SWFDEC_LOG ("playing button sound %d", sound);
150 swfdec_audio_event_new (
151 SWFDEC_ROOT_MOVIE (SWFDEC_MOVIE (movie)->root)->player,
152 movie->button->sounds[sound]);
154 movie->mouse_in = mouse_in;
155 movie->mouse_button = button;
158 static void
159 swfdec_button_movie_init_movie (SwfdecMovie *mov)
161 SwfdecButtonMovie *movie = SWFDEC_BUTTON_MOVIE (mov);
163 swfdec_button_movie_change_state (movie, SWFDEC_BUTTON_UP);
166 static gboolean
167 swfdec_button_movie_mouse_in (SwfdecMovie *movie, double x, double y)
169 GList *walk;
170 double tmpx, tmpy;
171 SwfdecButton *button = SWFDEC_BUTTON_MOVIE (movie)->button;
172 SwfdecContent *content;
174 for (walk = button->records; walk; walk = walk->next) {
175 cairo_matrix_t matrix, inverse;
176 content = walk->data;
177 if (content->end <= SWFDEC_BUTTON_HIT)
178 continue;
179 tmpx = x;
180 tmpy = y;
181 swfdec_transform_to_matrix (&matrix, &inverse, &content->transform);
182 cairo_matrix_transform_point (&inverse, &tmpx, &tmpy);
184 SWFDEC_LOG ("Checking button contents at %g %g (transformed from %g %g)", tmpx, tmpy, x, y);
185 if (swfdec_graphic_mouse_in (content->graphic, tmpx, tmpy))
186 return TRUE;
187 SWFDEC_LOG (" missed");
189 return FALSE;
192 static SwfdecButtonState
193 swfdec_button_movie_get_state (SwfdecButtonMovie *movie, gboolean mouse_in, int button)
195 if (button) {
196 if (mouse_in)
197 return SWFDEC_BUTTON_DOWN;
198 else if (movie->button->menubutton)
199 return SWFDEC_BUTTON_UP;
200 else
201 return SWFDEC_BUTTON_OVER;
202 } else {
203 if (mouse_in)
204 return SWFDEC_BUTTON_OVER;
205 else
206 return SWFDEC_BUTTON_UP;
210 static void
211 swfdec_button_movie_mouse_change (SwfdecMovie *mov, double x, double y,
212 gboolean mouse_in, int button)
214 SwfdecButtonMovie *movie = SWFDEC_BUTTON_MOVIE (mov);
215 SwfdecButtonState new_state = swfdec_button_movie_get_state (movie, mouse_in, button);
217 if (new_state != movie->state) {
218 swfdec_button_movie_change_state (movie, new_state);
220 swfdec_button_movie_change_mouse (movie, mouse_in, button);
223 static void
224 swfdec_button_movie_class_init (SwfdecButtonMovieClass * g_class)
226 SwfdecMovieClass *movie_class = SWFDEC_MOVIE_CLASS (g_class);
228 movie_class->init_movie = swfdec_button_movie_init_movie;
229 movie_class->update_extents = swfdec_button_movie_update_extents;
230 movie_class->mouse_in = swfdec_button_movie_mouse_in;
231 movie_class->mouse_change = swfdec_button_movie_mouse_change;
234 static void
235 swfdec_button_movie_init (SwfdecButtonMovie *movie)
237 movie->state = SWFDEC_BUTTON_INIT;