first step at rewriting the button code - nothing works, but it compiles
[swfdec.git] / libswfdec / swfdec_button_movie.c
blob742ea24f6028e238a4f4ded444b25a1c3e1c682f
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_as_strings.h"
26 #include "swfdec_audio_event.h"
27 #include "swfdec_debug.h"
28 #include "swfdec_event.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 #if 0
41 /* first index is 1 for menubutton, second index is previous state,
42 * last index is current state
43 * MSB in index is mouse OUT = 0, IN = 1
44 * LSB in index is button UP = 0, DOWN = 1
46 static const SwfdecButtonCondition event_table[2][4][4] = {
47 { { -1, -1, SWFDEC_BUTTON_IDLE_TO_OVER_UP, -1 },
48 { SWFDEC_BUTTON_OUT_DOWN_TO_IDLE, -1, -1, SWFDEC_BUTTON_OUT_DOWN_TO_OVER_DOWN },
49 { SWFDEC_BUTTON_OVER_UP_TO_IDLE, -1, -1, SWFDEC_BUTTON_OVER_UP_TO_OVER_DOWN },
50 { -1, SWFDEC_BUTTON_OVER_DOWN_TO_OUT_DOWN, SWFDEC_BUTTON_OVER_DOWN_TO_OVER_UP, -1 } },
51 { { -1, -1, SWFDEC_BUTTON_IDLE_TO_OVER_UP, -1 },
52 { -2, -1, -1, SWFDEC_BUTTON_IDLE_TO_OVER_DOWN },
53 { SWFDEC_BUTTON_OVER_UP_TO_IDLE, -1, -1, SWFDEC_BUTTON_OVER_UP_TO_OVER_DOWN },
54 { -1, SWFDEC_BUTTON_OVER_DOWN_TO_IDLE, SWFDEC_BUTTON_OVER_DOWN_TO_OVER_UP, -1 } }
56 static const int sound_table[2][4][4] = {
57 { { -1, -1, 1, -1 },
58 { 0, -1, -1, -1 },
59 { 0, -1, -1, 2 },
60 { -1, -1, 3, -1 } },
61 { { -1, -1, 1, -1 },
62 { -1, -1, -1, 1 },
63 { 0, -1, -1, 2 },
64 { -1, 0, 3, -1 } }
67 static const char *
68 swfdec_button_condition_get_name (SwfdecButtonCondition condition)
70 /* FIXME: check if these events are based on conditions or if they're independant of button type */
71 switch (condition) {
72 case SWFDEC_BUTTON_IDLE_TO_OVER_UP:
73 return SWFDEC_AS_STR_onRollOver;
74 case SWFDEC_BUTTON_OVER_UP_TO_IDLE:
75 return SWFDEC_AS_STR_onRollOut;
76 case SWFDEC_BUTTON_OVER_UP_TO_OVER_DOWN:
77 return SWFDEC_AS_STR_onPress;
78 case SWFDEC_BUTTON_OVER_DOWN_TO_OVER_UP:
79 return SWFDEC_AS_STR_onRelease;
80 case SWFDEC_BUTTON_OVER_DOWN_TO_OUT_DOWN:
81 return SWFDEC_AS_STR_onDragOut;
82 case SWFDEC_BUTTON_OUT_DOWN_TO_OVER_DOWN:
83 return SWFDEC_AS_STR_onDragOver;
84 case SWFDEC_BUTTON_OUT_DOWN_TO_IDLE:
85 return SWFDEC_AS_STR_onReleaseOutside;
86 case SWFDEC_BUTTON_IDLE_TO_OVER_DOWN:
87 return SWFDEC_AS_STR_onDragOver;
88 case SWFDEC_BUTTON_OVER_DOWN_TO_IDLE:
89 return SWFDEC_AS_STR_onDragOut;
90 default:
91 g_assert_not_reached ();
92 return NULL;
96 static void
97 swfdec_button_movie_execute (SwfdecButtonMovie *movie,
98 SwfdecButtonCondition condition)
100 const char *name;
102 if (movie->button->menubutton) {
103 g_assert ((condition & ((1 << SWFDEC_BUTTON_OVER_DOWN_TO_OUT_DOWN) \
104 | (1 << SWFDEC_BUTTON_OUT_DOWN_TO_OVER_DOWN) \
105 | (1 << SWFDEC_BUTTON_OUT_DOWN_TO_IDLE))) == 0);
106 } else {
107 g_assert ((condition & ((1 << SWFDEC_BUTTON_IDLE_TO_OVER_DOWN) \
108 | (1 << SWFDEC_BUTTON_OVER_DOWN_TO_IDLE))) == 0);
110 if (movie->button->events)
111 swfdec_event_list_execute (movie->button->events,
112 SWFDEC_AS_OBJECT (SWFDEC_MOVIE (movie)->parent),
113 SWFDEC_SECURITY (SWFDEC_MOVIE (movie)->resource), condition, 0);
114 name = swfdec_button_condition_get_name (condition);
115 swfdec_as_object_call (SWFDEC_AS_OBJECT (movie), name, 0, NULL, NULL);
118 #define CONTENT_IN_FRAME(content, frame) \
119 ((content)->sequence->start <= frame && \
120 (content)->sequence->end > frame)
121 static void
122 swfdec_button_movie_change_state (SwfdecButtonMovie *movie, SwfdecButtonState state)
124 SwfdecMovie *mov = SWFDEC_MOVIE (movie);
125 GList *walk;
127 g_assert (movie->state != state);
128 SWFDEC_LOG ("changing state on button movie %p from %d to %d", movie,
129 movie->state, state);
130 /* do this in 2 loops - otherwise going down DOWN=>UP would
131 * remove children that are only in these 2 states (due to how
132 * adding them is handled)
134 for (walk = movie->button->records; walk; walk = walk->next) {
135 SwfdecContent *content = walk->data;
136 if (CONTENT_IN_FRAME (content, (guint) movie->state) &&
137 !CONTENT_IN_FRAME (content, (guint) state)) {
138 SwfdecMovie *child = swfdec_movie_find (mov, content->depth);
139 if (child)
140 swfdec_movie_remove (child);
143 for (walk = movie->button->records; walk; walk = walk->next) {
144 SwfdecContent *content = walk->data;
145 if (!CONTENT_IN_FRAME (content, (guint) movie->state) &&
146 CONTENT_IN_FRAME (content, (guint) state)) {
147 SwfdecMovie *child = swfdec_movie_find (mov, content->depth);
148 if (child) {
149 g_assert_not_reached ();
151 swfdec_movie_new_for_content (mov, content);
154 movie->state = state;
157 static void
158 swfdec_button_movie_change_mouse (SwfdecButtonMovie *movie, gboolean mouse_in, int button)
160 int event;
161 int sound;
163 if (movie->mouse_in == mouse_in &&
164 movie->mouse_button == button)
165 return;
166 SWFDEC_LOG ("changing mouse state %s: %s %s (%u) => %s %s (%u)",
167 movie->button->menubutton ? "MENU" : "BUTTON",
168 movie->mouse_in ? "IN" : "OUT", movie->mouse_button ? "DOWN" : "UP",
169 (movie->mouse_in ? 2 : 0) + movie->mouse_button,
170 mouse_in ? "IN" : "OUT", button ? "DOWN" : "UP",
171 (mouse_in ? 2 : 0) + button);
172 event = event_table[movie->button->menubutton ? 1 : 0]
173 [(movie->mouse_in ? 2 : 0) + movie->mouse_button]
174 [(mouse_in ? 2 : 0) + button];
176 #ifndef G_DISABLE_ASSERT
177 if (event == -1) {
178 g_error ("Unhandled event for %s: %u => %u",
179 movie->button->menubutton ? "menu" : "button",
180 (movie->mouse_in ? 2 : 0) + movie->mouse_button,
181 (mouse_in ? 2 : 0) + button);
183 #endif
184 if (event >= 0) {
185 SWFDEC_LOG ("emitting event for condition %u", event);
186 swfdec_button_movie_execute (movie, event);
188 sound = sound_table[movie->button->menubutton ? 1 : 0]
189 [(movie->mouse_in ? 2 : 0) + movie->mouse_button]
190 [(mouse_in ? 2 : 0) + button];
191 if (sound >= 0 && movie->button->sounds[sound]) {
192 SwfdecAudio *audio;
193 SWFDEC_LOG ("playing button sound %d", sound);
194 audio = swfdec_audio_event_new_from_chunk (
195 SWFDEC_PLAYER (SWFDEC_AS_OBJECT (movie)->context),
196 movie->button->sounds[sound]);
197 if (audio)
198 g_object_unref (audio);
200 movie->mouse_in = mouse_in;
201 movie->mouse_button = button;
203 #endif
205 static void
206 swfdec_button_movie_init_movie (SwfdecMovie *mov)
208 //SwfdecButtonMovie *movie = SWFDEC_BUTTON_MOVIE (mov);
210 //swfdec_button_movie_change_state (movie, SWFDEC_BUTTON_UP);
213 #if 0
214 static gboolean G_GNUC_UNUSED
215 swfdec_button_movie_mouse_in (SwfdecMovie *movie, double x, double y)
217 GList *walk;
218 double tmpx, tmpy;
219 SwfdecButton *button = SWFDEC_BUTTON_MOVIE (movie)->button;
220 SwfdecContent *content;
222 for (walk = button->records; walk; walk = walk->next) {
223 cairo_matrix_t inverse;
224 content = walk->data;
225 if (content->end <= SWFDEC_BUTTON_HIT)
226 continue;
227 tmpx = x;
228 tmpy = y;
229 swfdec_matrix_ensure_invertible (&content->transform, &inverse);
230 cairo_matrix_transform_point (&inverse, &tmpx, &tmpy);
232 SWFDEC_LOG ("Checking button contents at %g %g (transformed from %g %g)", tmpx, tmpy, x, y);
233 if (swfdec_graphic_mouse_in (content->graphic, tmpx, tmpy))
234 return TRUE;
235 SWFDEC_LOG (" missed");
237 return FALSE;
240 static SwfdecButtonState
241 swfdec_button_movie_get_state (SwfdecButtonMovie *movie, gboolean mouse_in, int button)
243 if (button) {
244 if (mouse_in)
245 return SWFDEC_BUTTON_DOWN;
246 else if (movie->button->menubutton)
247 return SWFDEC_BUTTON_UP;
248 else
249 return SWFDEC_BUTTON_OVER;
250 } else {
251 if (mouse_in)
252 return SWFDEC_BUTTON_OVER;
253 else
254 return SWFDEC_BUTTON_UP;
258 static void G_GNUC_UNUSED
259 swfdec_button_movie_mouse_change (SwfdecMovie *mov, double x, double y,
260 gboolean mouse_in, int button)
262 SwfdecButtonMovie *movie = SWFDEC_BUTTON_MOVIE (mov);
263 SwfdecButtonState new_state = swfdec_button_movie_get_state (movie, mouse_in, button);
265 if (new_state != movie->state) {
266 swfdec_button_movie_change_state (movie, new_state);
268 swfdec_button_movie_change_mouse (movie, mouse_in, button);
270 #endif
272 static void
273 swfdec_button_movie_class_init (SwfdecButtonMovieClass * g_class)
275 SwfdecMovieClass *movie_class = SWFDEC_MOVIE_CLASS (g_class);
277 movie_class->init_movie = swfdec_button_movie_init_movie;
278 movie_class->update_extents = swfdec_button_movie_update_extents;
281 static void
282 swfdec_button_movie_init (SwfdecButtonMovie *movie)
284 movie->state = SWFDEC_BUTTON_INIT;