Debian Codegraph @

tests/interactive/test-paint-wrapper.c

raw
1 #include <gmodule.h>
2
3 #include <clutter/clutter.h>
4
5 #if defined (_MSC_VER) && !defined (_USE_MATH_DEFINES)
6 #define _USE_MATH_DEFINES
7 #endif
8
9 #include <math.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <glib.h>
13
14 #ifdef CLUTTER_WINDOWING_X11
15 #include "clutter/x11/clutter-x11.h"
16 #endif
17
18 #define NHANDS 6
19
20 typedef struct SuperOH
21 {
22 ClutterActor **hand, *bgtex;
23 ClutterActor *real_hand;
24 ClutterActor *group;
25 ClutterActor *stage;
26
27 gint stage_width;
28 gint stage_height;
29 gfloat radius;
30
31 ClutterBehaviour *scaler_1;
32 ClutterBehaviour *scaler_2;
33 ClutterTimeline *timeline;
34
35 guint frame_id;
36
37 gboolean *paint_guards;
38 } SuperOH;
39
40 static gint n_hands = NHANDS;
41 static gint use_alpha = 255;
42
43 static GOptionEntry super_oh_entries[] = {
44 {
45 "num-hands", 'n',
46 0,
47 G_OPTION_ARG_INT, &n_hands,
48 "Number of hands", "HANDS"
49 },
50 {
51 "use-alpha", 'a',
52 0,
53 G_OPTION_ARG_INT, &use_alpha,
54 "Stage opacity", "VALUE"
55 },
56 { NULL }
57 };
58
59 static gboolean
60 on_button_press_event (ClutterActor *actor,
61 ClutterEvent *event,
62 SuperOH *oh)
63 {
64 gfloat x, y;
65
66 clutter_event_get_coords (event, &x, &y);
67
68 g_print ("*** button press event (button:%d) at %.2f, %.2f ***\n",
69 clutter_event_get_button (event),
70 x, y);
71
72 clutter_actor_hide (actor);
73
74 return TRUE;
75 }
76
77 static gboolean
78 input_cb (ClutterActor *stage,
79 ClutterEvent *event,
80 gpointer data)
81 {
82 SuperOH *oh = data;
83
84 if (event->type == CLUTTER_KEY_RELEASE)
85 {
86 g_print ("*** key press event (key:%c) ***\n",
87 clutter_event_get_key_symbol (event));
88
89 if (clutter_event_get_key_symbol (event) == CLUTTER_KEY_q)
90 {
91 clutter_main_quit ();
92
93 return TRUE;
94 }
95 else if (clutter_event_get_key_symbol (event) == CLUTTER_KEY_r)
96 {
97 gint i;
98
99 for (i = 0; i < n_hands; i++)
100 clutter_actor_show (oh->hand[i]);
101
102 return TRUE;
103 }
104 }
105
106 return FALSE;
107 }
108
109 /* Timeline handler */
110 static void
111 frame_cb (ClutterTimeline *timeline,
112 gint msecs,
113 gpointer data)
114 {
115 SuperOH *oh = data;
116 gint i;
117 float rotation = clutter_timeline_get_progress (timeline) * 360.0f;
118
119 /* Rotate everything clockwise about stage center*/
120
121 clutter_actor_set_rotation (oh->group,
122 CLUTTER_Z_AXIS,
123 rotation,
124 oh->stage_width / 2,
125 oh->stage_height / 2,
126 0);
127
128 for (i = 0; i < n_hands; i++)
129 {
130 /* Rotate each hand around there centers - to get this we need
131 * to take into account any scaling.
132 */
133 clutter_actor_set_rotation (oh->hand[i],
134 CLUTTER_Z_AXIS,
135 -6.0 * rotation,
136 0, 0, 0);
137 }
138 }
139
140 static gdouble
141 my_sine_wave (ClutterAlpha *alpha,
142 gpointer dummy G_GNUC_UNUSED)
143 {
144 ClutterTimeline *timeline = clutter_alpha_get_timeline (alpha);
145 gdouble progress = clutter_timeline_get_progress (timeline);
146
147 return sin (progress * G_PI);
148 }
149
150 static void
151 hand_pre_paint (ClutterActor *actor,
152 gpointer user_data)
153 {
154 SuperOH *oh = user_data;
155 gfloat w, h;
156 int actor_num;
157
158 for (actor_num = 0; oh->hand[actor_num] != actor; actor_num++)
159 ;
160
161 g_assert (oh->paint_guards[actor_num] == FALSE);
162
163 clutter_actor_get_size (actor, &w, &h);
164
165 cogl_set_source_color4ub (255, 0, 0, 128);
166 cogl_rectangle (0, 0, w / 2, h / 2);
167
168 oh->paint_guards[actor_num] = TRUE;
169 }
170
171 static void
172 hand_post_paint (ClutterActor *actor,
173 gpointer user_data)
174 {
175 SuperOH *oh = user_data;
176 gfloat w, h;
177 int actor_num;
178
179 for (actor_num = 0; oh->hand[actor_num] != actor; actor_num++)
180 ;
181
182 g_assert (oh->paint_guards[actor_num] == TRUE);
183
184 clutter_actor_get_size (actor, &w, &h);
185
186 cogl_set_source_color4ub (0, 255, 0, 128);
187 cogl_rectangle (w / 2, h / 2, w, h);
188
189 oh->paint_guards[actor_num] = FALSE;
190 }
191
192 static void
193 stop_and_quit (ClutterActor *actor,
194 SuperOH *oh)
195 {
196 g_signal_handler_disconnect (oh->timeline, oh->frame_id);
197 clutter_timeline_stop (oh->timeline);
198
199 clutter_main_quit ();
200 }
201
202 G_MODULE_EXPORT int
203 test_paint_wrapper_main (int argc, char *argv[])
204 {
205 ClutterAlpha *alpha;
206 ClutterActor *stage;
207 ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff };
208 SuperOH *oh;
209 gint i;
210 GError *error;
211 ClutterActor *real_hand;
212
213 error = NULL;
214
215 #ifdef CLUTTER_WINDOWING_X11
216 clutter_x11_set_use_argb_visual (TRUE);
217 #endif
218
219 if (clutter_init_with_args (&argc, &argv,
220 NULL,
221 super_oh_entries,
222 NULL,
223 &error) != CLUTTER_INIT_SUCCESS)
224 {
225 g_warning ("Unable to initialise Clutter:\n%s",
226 error->message);
227 g_error_free (error);
228
229 return EXIT_FAILURE;
230 }
231
232 oh = g_new(SuperOH, 1);
233
234 stage = clutter_stage_new ();
235 clutter_actor_set_size (stage, 800, 600);
236
237 if (use_alpha != 255)
238 {
239 clutter_stage_set_use_alpha (CLUTTER_STAGE (stage), TRUE);
240 clutter_actor_set_opacity (stage, use_alpha);
241 }
242
243 clutter_stage_set_title (CLUTTER_STAGE (stage), "Paint Test");
244 clutter_actor_set_background_color (stage, &stage_color);
245 g_signal_connect (stage, "destroy", G_CALLBACK (stop_and_quit), oh);
246
247 oh->stage = stage;
248
249 /* Create a timeline to manage animation */
250 oh->timeline = clutter_timeline_new (6000);
251 clutter_timeline_set_repeat_count (oh->timeline, -1);
252
253 /* fire a callback for frame change */
254 oh->frame_id =
255 g_signal_connect (oh->timeline, "new-frame", G_CALLBACK (frame_cb), oh);
256
257 /* Set up some behaviours to handle scaling */
258 alpha = clutter_alpha_new_with_func (oh->timeline, my_sine_wave, NULL, NULL);
259
260 oh->scaler_1 = clutter_behaviour_scale_new (alpha, 0.5, 0.5, 1.0, 1.0);
261 oh->scaler_2 = clutter_behaviour_scale_new (alpha, 1.0, 1.0, 0.5, 0.5);
262
263 real_hand = clutter_texture_new_from_file (TESTS_DATADIR
264 G_DIR_SEPARATOR_S
265 "redhand.png",
266 &error);
267 if (real_hand == NULL)
268 {
269 g_error ("image load failed: %s", error->message);
270 return EXIT_FAILURE;
271 }
272
273 /* create a new group to hold multiple actors in a group */
274 oh->group = clutter_group_new();
275
276 oh->hand = g_new (ClutterActor*, n_hands);
277
278 oh->stage_width = clutter_actor_get_width (stage);
279 oh->stage_height = clutter_actor_get_height (stage);
280 oh->radius = (oh->stage_width + oh->stage_height)
281 / n_hands;
282
283 for (i = 0; i < n_hands; i++)
284 {
285 gint x, y, w, h;
286
287 if (i == 0)
288 oh->hand[i] = real_hand;
289 else
290 oh->hand[i] = clutter_clone_new (real_hand);
291
292 clutter_actor_set_reactive (oh->hand[i], TRUE);
293
294 clutter_actor_set_size (oh->hand[i], 200, 213);
295
296 /* Place around a circle */
297 w = clutter_actor_get_width (oh->hand[i]);
298 h = clutter_actor_get_height (oh->hand[i]);
299
300 x = oh->stage_width / 2
301 + oh->radius
302 * cos (i * G_PI / (n_hands / 2))
303 - w / 2;
304
305 y = oh->stage_height / 2
306 + oh->radius
307 * sin (i * G_PI / (n_hands / 2))
308 - h / 2;
309
310 clutter_actor_set_position (oh->hand[i], x, y);
311
312 clutter_actor_move_anchor_point_from_gravity (oh->hand[i],
313 CLUTTER_GRAVITY_CENTER);
314
315 g_signal_connect (oh->hand[i], "button-press-event",
316 G_CALLBACK (on_button_press_event),
317 oh);
318
319 /* paint something before each hand */
320 g_signal_connect (oh->hand[i],
321 "paint", G_CALLBACK (hand_pre_paint),
322 oh);
323
324 /* paint something after each hand */
325 g_signal_connect_after (oh->hand[i],
326 "paint", G_CALLBACK (hand_post_paint),
327 oh);
328
329 /* Add to our group group */
330 clutter_container_add_actor (CLUTTER_CONTAINER (oh->group), oh->hand[i]);
331
332 if (i % 2)
333 clutter_behaviour_apply (oh->scaler_1, oh->hand[i]);
334 else
335 clutter_behaviour_apply (oh->scaler_2, oh->hand[i]);
336 }
337
338 oh->paint_guards = g_malloc0 (sizeof (gboolean) * n_hands);
339
340 /* Add the group to the stage */
341 clutter_container_add_actor (CLUTTER_CONTAINER (stage),
342 CLUTTER_ACTOR (oh->group));
343
344 /* Show everying ( and map window ) */
345 clutter_actor_show (stage);
346
347 g_signal_connect (stage, "key-release-event",
348 G_CALLBACK (input_cb),
349 oh);
350
351 /* and start it */
352 clutter_timeline_start (oh->timeline);
353
354 clutter_main ();
355
356 g_object_unref (oh->scaler_1);
357 g_object_unref (oh->scaler_2);
358 g_object_unref (oh->timeline);
359 g_free (oh->paint_guards);
360 g_free (oh->hand);
361 g_free (oh);
362
363 return 0;
364 }
365
366 G_MODULE_EXPORT const char *
367 test_paint_wrapper_describe (void)
368 {
369 return "Wrap an actor's paint cycle for pre and post processing.";
370 }