diff --git a/tests/interactive/test-actor-clone.c b/tests/interactive/test-actor-clone.c index a2fa85f10..10548553c 100644 --- a/tests/interactive/test-actor-clone.c +++ b/tests/interactive/test-actor-clone.c @@ -36,7 +36,24 @@ static GOptionEntry super_oh_entries[] = { { NULL } }; -/* input handler */ +static gboolean +on_button_press_event (ClutterActor *actor, + ClutterEvent *event, + SuperOH *oh) +{ + gfloat x, y; + + clutter_event_get_coords (event, &x, &y); + + g_print ("*** button press event (button:%d) at %.2f, %.2f ***\n", + clutter_event_get_button (event), + x, y); + + clutter_actor_hide (actor); + + return TRUE; +} + static gboolean input_cb (ClutterActor *stage, ClutterEvent *event, @@ -44,31 +61,7 @@ input_cb (ClutterActor *stage, { SuperOH *oh = data; - if (event->type == CLUTTER_BUTTON_PRESS) - { - ClutterButtonEvent *button_event; - ClutterActor *e; - gfloat x, y; - - clutter_event_get_coords (event, &x, &y); - - button_event = (ClutterButtonEvent *) event; - g_print ("*** button press event (button:%d) at %.2f, %.2f ***\n", - button_event->button, - x, y); - - e = clutter_stage_get_actor_at_pos (CLUTTER_STAGE (stage), - CLUTTER_PICK_ALL, - x, y); - - /* only allow hiding the clones */ - if (e && CLUTTER_IS_CLONE (e)) - { - clutter_actor_hide (e); - return TRUE; - } - } - else if (event->type == CLUTTER_KEY_RELEASE) + if (event->type == CLUTTER_KEY_RELEASE) { g_print ("*** key press event (key:%c) ***\n", clutter_event_get_key_symbol (event)); @@ -76,6 +69,7 @@ input_cb (ClutterActor *stage, if (clutter_event_get_key_symbol (event) == CLUTTER_q) { clutter_main_quit (); + return TRUE; } else if (clutter_event_get_key_symbol (event) == CLUTTER_r) @@ -85,8 +79,6 @@ input_cb (ClutterActor *stage, for (i = 0; i < n_hands; i++) clutter_actor_show (oh->hand[i]); - clutter_actor_show (oh->real_hand); - return TRUE; } } @@ -94,7 +86,6 @@ input_cb (ClutterActor *stage, return FALSE; } - /* Timeline handler */ static void frame_cb (ClutterTimeline *timeline, @@ -116,10 +107,6 @@ frame_cb (ClutterTimeline *timeline, for (i = 0; i < n_hands; i++) { - gdouble scale_x, scale_y; - - clutter_actor_get_scale (oh->hand[i], &scale_x, &scale_y); - /* Rotate each hand around there centers - to get this we need * to take into account any scaling. */ @@ -235,8 +222,11 @@ test_actor_clone_main (int argc, char *argv[]) /* Create a texture from file, then clone in to same resources */ oh->hand[i] = clutter_clone_new (real_hand); + clutter_actor_set_size (oh->hand[i], 200, 213); + clutter_actor_set_reactive (oh->hand[i], TRUE); + /* Place around a circle */ w = clutter_actor_get_width (oh->hand[0]); h = clutter_actor_get_height (oh->hand[0]); @@ -259,6 +249,10 @@ test_actor_clone_main (int argc, char *argv[]) /* Add to our group group */ clutter_container_add_actor (CLUTTER_CONTAINER (oh->group), oh->hand[i]); + g_signal_connect (oh->hand[i], "button-press-event", + G_CALLBACK (on_button_press_event), + oh); + if (i % 2) clutter_behaviour_apply (oh->scaler_1, oh->hand[i]); else @@ -271,9 +265,6 @@ test_actor_clone_main (int argc, char *argv[]) /* Show everying */ clutter_actor_show (stage); - g_signal_connect (stage, "button-press-event", - G_CALLBACK (input_cb), - oh); g_signal_connect (stage, "key-release-event", G_CALLBACK (input_cb), oh); diff --git a/tests/interactive/test-actors.c b/tests/interactive/test-actors.c index 6af92caf4..ef2c56fd9 100644 --- a/tests/interactive/test-actors.c +++ b/tests/interactive/test-actors.c @@ -36,7 +36,24 @@ static GOptionEntry super_oh_entries[] = { { NULL } }; -/* input handler */ +static gboolean +on_button_press_event (ClutterActor *actor, + ClutterEvent *event, + SuperOH *oh) +{ + gfloat x, y; + + clutter_event_get_coords (event, &x, &y); + + g_print ("*** button press event (button:%d) at %.2f, %.2f ***\n", + clutter_event_get_button (event), + x, y); + + clutter_actor_hide (actor); + + return TRUE; +} + static gboolean input_cb (ClutterActor *stage, ClutterEvent *event, @@ -44,31 +61,7 @@ input_cb (ClutterActor *stage, { SuperOH *oh = data; - if (event->type == CLUTTER_BUTTON_PRESS) - { - ClutterButtonEvent *button_event; - ClutterActor *e; - gfloat x, y; - - clutter_event_get_coords (event, &x, &y); - - button_event = (ClutterButtonEvent *) event; - g_print ("*** button press event (button:%d) at %.2f, %.2f ***\n", - button_event->button, - x, y); - - e = clutter_stage_get_actor_at_pos (CLUTTER_STAGE (stage), - CLUTTER_PICK_ALL, - x, y); - - /* only allow hiding the clones */ - if (e && (CLUTTER_IS_TEXTURE (e) || CLUTTER_IS_CLONE (e))) - { - clutter_actor_hide (e); - return TRUE; - } - } - else if (event->type == CLUTTER_KEY_RELEASE) + if (event->type == CLUTTER_KEY_RELEASE) { g_print ("*** key press event (key:%c) ***\n", clutter_event_get_key_symbol (event)); @@ -76,6 +69,7 @@ input_cb (ClutterActor *stage, if (clutter_event_get_key_symbol (event) == CLUTTER_q) { clutter_main_quit (); + return TRUE; } else if (clutter_event_get_key_symbol (event) == CLUTTER_r) @@ -92,7 +86,6 @@ input_cb (ClutterActor *stage, return FALSE; } - /* Timeline handler */ static void frame_cb (ClutterTimeline *timeline, @@ -209,6 +202,8 @@ test_actors_main (int argc, char *argv[]) else oh->hand[i] = clutter_clone_new (real_hand); + clutter_actor_set_reactive (oh->hand[i], TRUE); + clutter_actor_set_size (oh->hand[i], 200, 213); /* Place around a circle */ @@ -233,6 +228,10 @@ test_actors_main (int argc, char *argv[]) /* Add to our group group */ clutter_container_add_actor (CLUTTER_CONTAINER (oh->group), oh->hand[i]); + g_signal_connect (oh->hand[i], "button-press-event", + G_CALLBACK (on_button_press_event), + oh); + if (i % 2) clutter_behaviour_apply (oh->scaler_1, oh->hand[i]); else @@ -245,9 +244,6 @@ test_actors_main (int argc, char *argv[]) /* Show everying */ clutter_actor_show (stage); - g_signal_connect (stage, "button-press-event", - G_CALLBACK (input_cb), - oh); g_signal_connect (stage, "key-release-event", G_CALLBACK (input_cb), oh); diff --git a/tests/interactive/test-paint-wrapper.c b/tests/interactive/test-paint-wrapper.c index 182106cc4..704ce7800 100644 --- a/tests/interactive/test-paint-wrapper.c +++ b/tests/interactive/test-paint-wrapper.c @@ -10,17 +10,24 @@ #include #include -#define TRAILS 0 #define NHANDS 6 -#define RADIUS ((CLUTTER_STAGE_WIDTH()+CLUTTER_STAGE_HEIGHT())/NHANDS) typedef struct SuperOH { - ClutterActor **hand, *bgtex; - ClutterActor *group; + ClutterActor **hand, *bgtex; + ClutterActor *real_hand; + ClutterActor *group; + ClutterActor *stage; - gboolean *paint_guards; + gint stage_width; + gint stage_height; + gfloat radius; + ClutterBehaviour *scaler_1; + ClutterBehaviour *scaler_2; + ClutterTimeline *timeline; + + gboolean *paint_guards; } SuperOH; static gint n_hands = NHANDS; @@ -35,39 +42,32 @@ static GOptionEntry super_oh_entries[] = { { NULL } }; -static gint -get_radius (void) +static gboolean +on_button_press_event (ClutterActor *actor, + ClutterEvent *event, + SuperOH *oh) { - return (CLUTTER_STAGE_HEIGHT() + CLUTTER_STAGE_HEIGHT()) / n_hands ; + gfloat x, y; + + clutter_event_get_coords (event, &x, &y); + + g_print ("*** button press event (button:%d) at %.2f, %.2f ***\n", + clutter_event_get_button (event), + x, y); + + clutter_actor_hide (actor); + + return TRUE; } -/* input handler */ static gboolean -input_cb (ClutterStage *stage, +input_cb (ClutterActor *stage, ClutterEvent *event, gpointer data) { - if (event->type == CLUTTER_BUTTON_PRESS) - { - ClutterButtonEvent *button_event; - ClutterActor *e; - gfloat x, y; + SuperOH *oh = data; - clutter_event_get_coords (event, &x, &y); - - button_event = (ClutterButtonEvent *) event; - g_print ("*** button press event (button:%d) ***\n", - button_event->button); - - e = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, x, y); - - if (e && (CLUTTER_IS_TEXTURE (e) || CLUTTER_IS_CLONE (e))) - { - clutter_actor_hide (e); - return TRUE; - } - } - else if (event->type == CLUTTER_KEY_RELEASE) + if (event->type == CLUTTER_KEY_RELEASE) { g_print ("*** key press event (key:%c) ***\n", clutter_event_get_key_symbol (event)); @@ -75,6 +75,16 @@ input_cb (ClutterStage *stage, if (clutter_event_get_key_symbol (event) == CLUTTER_q) { clutter_main_quit (); + + return TRUE; + } + else if (clutter_event_get_key_symbol (event) == CLUTTER_r) + { + gint i; + + for (i = 0; i < n_hands; i++) + clutter_actor_show (oh->hand[i]); + return TRUE; } } @@ -82,52 +92,57 @@ input_cb (ClutterStage *stage, return FALSE; } - /* Timeline handler */ static void frame_cb (ClutterTimeline *timeline, gint msecs, gpointer data) { - SuperOH *oh = (SuperOH *)data; - gint i; - float rotation = clutter_timeline_get_progress (timeline) * 360.0f; + SuperOH *oh = data; + gint i; + float rotation = clutter_timeline_get_progress (timeline) * 360.0f; /* Rotate everything clockwise about stage center*/ - clutter_actor_set_rotation (CLUTTER_ACTOR (oh->group), + clutter_actor_set_rotation (oh->group, CLUTTER_Z_AXIS, rotation, - CLUTTER_STAGE_WIDTH () / 2, - CLUTTER_STAGE_HEIGHT () / 2, + oh->stage_width / 2, + oh->stage_height / 2, 0); for (i = 0; i < n_hands; i++) { - gdouble scale_x, scale_y; - - clutter_actor_get_scale (oh->hand[i], &scale_x, &scale_y); - /* Rotate each hand around there centers - to get this we need * to take into account any scaling. - * - * FIXME: scaling causes drift so disabled for now. Need rotation - * unit based functions to fix. */ - clutter_actor_set_rotation (oh->hand[i], CLUTTER_Z_AXIS, - - 6.0 * rotation, 0, 0, 0); + clutter_actor_set_rotation (oh->hand[i], + CLUTTER_Z_AXIS, + -6.0 * rotation, + 0, 0, 0); } } +static gdouble +my_sine_wave (ClutterAlpha *alpha, + gpointer dummy G_GNUC_UNUSED) +{ + ClutterTimeline *timeline = clutter_alpha_get_timeline (alpha); + gdouble progress = clutter_timeline_get_progress (timeline); + + return sin (progress * G_PI); +} + static void hand_pre_paint (ClutterActor *actor, gpointer user_data) { - SuperOH *oh = (SuperOH *) user_data; + SuperOH *oh = user_data; gfloat w, h; int actor_num; - for (actor_num = 0; oh->hand[actor_num] != actor; actor_num++); + for (actor_num = 0; oh->hand[actor_num] != actor; actor_num++) + ; g_assert (oh->paint_guards[actor_num] == FALSE); @@ -143,11 +158,12 @@ static void hand_post_paint (ClutterActor *actor, gpointer user_data) { - SuperOH *oh = (SuperOH *) user_data; + SuperOH *oh = user_data; gfloat w, h; int actor_num; - for (actor_num = 0; oh->hand[actor_num] != actor; actor_num++); + for (actor_num = 0; oh->hand[actor_num] != actor; actor_num++) + ; g_assert (oh->paint_guards[actor_num] == TRUE); @@ -159,27 +175,16 @@ hand_post_paint (ClutterActor *actor, oh->paint_guards[actor_num] = FALSE; } -static gdouble -my_sine_wave (ClutterAlpha *alpha, - gpointer dummy G_GNUC_UNUSED) -{ - ClutterTimeline *timeline = clutter_alpha_get_timeline (alpha); - gdouble progress = clutter_timeline_get_progress (timeline); - - return sin (progress * G_PI); -} - G_MODULE_EXPORT int test_paint_wrapper_main (int argc, char *argv[]) { - ClutterTimeline *timeline; - ClutterAlpha *alpha; - ClutterBehaviour *scaler_1, *scaler_2; - ClutterActor *stage; - ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff }; - SuperOH *oh; - gint i; - GError *error; + ClutterAlpha *alpha; + ClutterActor *stage; + ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff }; + SuperOH *oh; + gint i; + GError *error; + ClutterActor *real_hand; error = NULL; @@ -194,57 +199,83 @@ test_paint_wrapper_main (int argc, char *argv[]) error->message); g_error_free (error); - exit (1); + return EXIT_FAILURE; } stage = clutter_stage_get_default (); clutter_actor_set_size (stage, 800, 600); - clutter_stage_set_title (CLUTTER_STAGE (stage), "Actors Test"); - clutter_stage_set_color (CLUTTER_STAGE (stage), - &stage_color); + clutter_stage_set_title (CLUTTER_STAGE (stage), "Paint Test"); + clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); oh = g_new(SuperOH, 1); + oh->stage = stage; /* Create a timeline to manage animation */ - timeline = clutter_timeline_new (6000); - clutter_timeline_set_loop (timeline, TRUE); + oh->timeline = clutter_timeline_new (6000); + clutter_timeline_set_loop (oh->timeline, TRUE); /* fire a callback for frame change */ - g_signal_connect (timeline, "new-frame", G_CALLBACK (frame_cb), oh); + g_signal_connect (oh->timeline, "new-frame", G_CALLBACK (frame_cb), oh); /* Set up some behaviours to handle scaling */ - alpha = clutter_alpha_new_with_func (timeline, my_sine_wave, NULL, NULL); + alpha = clutter_alpha_new_with_func (oh->timeline, my_sine_wave, NULL, NULL); - scaler_1 = clutter_behaviour_scale_new (alpha, - 0.5, 0.5, - 1.0, 1.0); + oh->scaler_1 = clutter_behaviour_scale_new (alpha, 0.5, 0.5, 1.0, 1.0); + oh->scaler_2 = clutter_behaviour_scale_new (alpha, 1.0, 1.0, 0.5, 0.5); - scaler_2 = clutter_behaviour_scale_new (alpha, - 1.0, 1.0, - 0.5, 0.5); + real_hand = clutter_texture_new_from_file ("redhand.png", &error); + if (real_hand == NULL) + { + g_error ("image load failed: %s", error->message); + return EXIT_FAILURE; + } /* create a new group to hold multiple actors in a group */ oh->group = clutter_group_new(); oh->hand = g_new (ClutterActor*, n_hands); + + oh->stage_width = clutter_actor_get_width (stage); + oh->stage_height = clutter_actor_get_height (stage); + oh->radius = (oh->stage_width + oh->stage_height) + / n_hands; + for (i = 0; i < n_hands; i++) { gint x, y, w, h; - gint radius = get_radius (); - /* Create a texture from file, then clone in to same resources */ if (i == 0) - { - if ((oh->hand[i] = clutter_texture_new_from_file ("redhand.png", - &error)) == NULL) - { - g_error ("image load failed: %s", error->message); - exit (1); - } - } + oh->hand[i] = real_hand; else - oh->hand[i] = clutter_clone_new (oh->hand[0]); + oh->hand[i] = clutter_clone_new (real_hand); + + clutter_actor_set_reactive (oh->hand[i], TRUE); + + clutter_actor_set_size (oh->hand[i], 200, 213); + + /* Place around a circle */ + w = clutter_actor_get_width (oh->hand[i]); + h = clutter_actor_get_height (oh->hand[i]); + + x = oh->stage_width / 2 + + oh->radius + * cos (i * G_PI / (n_hands / 2)) + - w / 2; + + y = oh->stage_height / 2 + + oh->radius + * sin (i * G_PI / (n_hands / 2)) + - h / 2; + + clutter_actor_set_position (oh->hand[i], x, y); + + clutter_actor_move_anchor_point_from_gravity (oh->hand[i], + CLUTTER_GRAVITY_CENTER); + + g_signal_connect (oh->hand[i], "button-press-event", + G_CALLBACK (on_button_press_event), + oh); /* paint something before each hand */ g_signal_connect (oh->hand[i], @@ -256,34 +287,13 @@ test_paint_wrapper_main (int argc, char *argv[]) "paint", G_CALLBACK (hand_post_paint), oh); - /* Place around a circle */ - w = clutter_actor_get_width (oh->hand[0]); - h = clutter_actor_get_height (oh->hand[0]); - - x = CLUTTER_STAGE_WIDTH () / 2 - + radius - * cos (i * M_PI / (n_hands / 2)) - - w / 2; - - y = CLUTTER_STAGE_HEIGHT () / 2 - + radius - * sin (i * M_PI / (n_hands / 2)) - - h / 2; - - clutter_actor_set_position (oh->hand[i], x, y); - - clutter_actor_move_anchor_point_from_gravity (oh->hand[i], - CLUTTER_GRAVITY_CENTER); - /* Add to our group group */ clutter_container_add_actor (CLUTTER_CONTAINER (oh->group), oh->hand[i]); -#if 1 /* FIXME: disabled as causes drift? - see comment above */ if (i % 2) - clutter_behaviour_apply (scaler_1, oh->hand[i]); + clutter_behaviour_apply (oh->scaler_1, oh->hand[i]); else - clutter_behaviour_apply (scaler_2, oh->hand[i]); -#endif + clutter_behaviour_apply (oh->scaler_2, oh->hand[i]); } oh->paint_guards = g_malloc0 (sizeof (gboolean) * n_hands); @@ -295,21 +305,20 @@ test_paint_wrapper_main (int argc, char *argv[]) /* Show everying ( and map window ) */ clutter_actor_show (stage); - - g_signal_connect (stage, "button-press-event", - G_CALLBACK (input_cb), - oh); g_signal_connect (stage, "key-release-event", G_CALLBACK (input_cb), oh); /* and start it */ - clutter_timeline_start (timeline); + clutter_timeline_start (oh->timeline); clutter_main (); - g_free (oh->hand); + g_object_unref (oh->scaler_1); + g_object_unref (oh->scaler_2); + g_object_unref (oh->timeline); g_free (oh->paint_guards); + g_free (oh->hand); g_free (oh); return 0;