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.
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
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
)
33 swfdec_button_movie_update_extents (SwfdecMovie
*movie
,
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] = {
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);
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)
86 swfdec_button_movie_change_state (SwfdecButtonMovie
*movie
, SwfdecButtonState state
)
88 SwfdecMovie
*mov
= SWFDEC_MOVIE (movie
);
91 g_assert (movie
->state
!= state
);
92 SWFDEC_LOG ("changing state on button movie %p from %d to %d", movie
,
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
);
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
);
113 g_assert_not_reached ();
115 swfdec_movie_new (mov
, content
);
118 movie
->state
= state
;
122 swfdec_button_movie_change_mouse (SwfdecButtonMovie
*movie
, gboolean mouse_in
, int button
)
124 SwfdecButtonCondition event
;
127 if (movie
->mouse_in
== mouse_in
&&
128 movie
->mouse_button
== button
)
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);
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
;
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
);
167 swfdec_button_movie_mouse_in (SwfdecMovie
*movie
, double x
, double y
)
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
)
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
))
187 SWFDEC_LOG (" missed");
192 static SwfdecButtonState
193 swfdec_button_movie_get_state (SwfdecButtonMovie
*movie
, gboolean mouse_in
, int button
)
197 return SWFDEC_BUTTON_DOWN
;
198 else if (movie
->button
->menubutton
)
199 return SWFDEC_BUTTON_UP
;
201 return SWFDEC_BUTTON_OVER
;
204 return SWFDEC_BUTTON_OVER
;
206 return SWFDEC_BUTTON_UP
;
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
);
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
;
235 swfdec_button_movie_init (SwfdecButtonMovie
*movie
)
237 movie
->state
= SWFDEC_BUTTON_INIT
;