cookbook: Looping animation examples

Added code examples for creating a looped animation with
each of the animation approaches (implicit, ClutterAnimation,
ClutterState).
This commit is contained in:
Elliot Smith 2010-11-10 16:11:27 +00:00
parent e80035331b
commit 0d8c730558
4 changed files with 242 additions and 0 deletions

View File

@ -4,6 +4,9 @@ NULL =
noinst_PROGRAMS = \
animations-complex \
animations-looping-animator \
animations-looping-implicit \
animations-looping-state \
animations-moving-animator \
animations-moving-implicit \
animations-moving-state \
@ -55,6 +58,9 @@ AM_CFLAGS = \
AM_LDFLAGS = $(CLUTTER_LIBS) -export-dynamic
animations_complex_SOURCES = animations-complex.c
animations_looping_animator_SOURCES = animations-looping-animator.c
animations_looping_implicit_SOURCES = animations-looping-implicit.c
animations_looping_state_SOURCES = animations-looping-state.c
animations_moving_animator_SOURCES = animations-moving-animator.c
animations_moving_implicit_SOURCES = animations-moving-implicit.c
animations_moving_state_SOURCES = animations-moving-state.c

View File

@ -0,0 +1,66 @@
#include <stdlib.h>
#include <clutter/clutter.h>
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor red_color = { 0xff, 0x00, 0x00, 0xff };
static gboolean
key_pressed_cb (ClutterActor *actor,
ClutterEvent *event,
gpointer user_data)
{
ClutterTimeline *timeline = CLUTTER_TIMELINE (user_data);
if (!clutter_timeline_is_playing (timeline))
clutter_timeline_start (timeline);
return TRUE;
}
int
main (int argc,
char *argv[])
{
ClutterActor *stage;
ClutterActor *actor;
ClutterTimeline *timeline;
ClutterAnimator *animator;
clutter_init (&argc, &argv);
stage = clutter_stage_get_default ();
clutter_actor_set_size (stage, 300, 200);
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
actor = clutter_rectangle_new_with_color (&red_color);
clutter_actor_set_size (actor, 100, 100);
clutter_actor_set_position (actor, 150, 50);
timeline = clutter_timeline_new (2000);
clutter_timeline_set_loop (timeline, TRUE);
animator = clutter_animator_new ();
clutter_animator_set_timeline (animator, timeline);
clutter_animator_set (animator,
actor, "x", CLUTTER_LINEAR, 0.0, 150.0,
actor, "x", CLUTTER_LINEAR, 0.5, 50.0,
actor, "x", CLUTTER_LINEAR, 1.0, 150.0,
NULL);
clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor);
g_signal_connect (stage,
"key-press-event",
G_CALLBACK (key_pressed_cb),
timeline);
clutter_actor_show (stage);
clutter_main ();
g_object_unref (animator);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,87 @@
#include <stdlib.h>
#include <clutter/clutter.h>
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor red_color = { 0xff, 0x00, 0x00, 0xff };
typedef struct
{
ClutterActor *actor;
ClutterTimeline *timeline;
} State;
static void
invert_timeline_cb (ClutterTimeline *timeline,
gpointer user_data)
{
ClutterTimelineDirection direction = clutter_timeline_get_direction (timeline);
if (direction == CLUTTER_TIMELINE_FORWARD)
direction = CLUTTER_TIMELINE_BACKWARD;
else
direction = CLUTTER_TIMELINE_FORWARD;
clutter_timeline_set_direction (timeline, direction);
}
static gboolean
key_pressed_cb (ClutterActor *actor,
ClutterEvent *event,
gpointer user_data)
{
State *state = (State *) user_data;
/* only start animating if actor isn't animating already */
if (clutter_actor_get_animation (state->actor) == NULL)
clutter_actor_animate_with_timeline (state->actor,
CLUTTER_LINEAR,
state->timeline,
"x", 50.0,
NULL);
return TRUE;
}
int
main (int argc,
char *argv[])
{
State *state = g_new0 (State, 1);
ClutterActor *stage;
clutter_init (&argc, &argv);
stage = clutter_stage_get_default ();
clutter_actor_set_size (stage, 300, 200);
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
state->actor = clutter_rectangle_new_with_color (&red_color);
clutter_actor_set_size (state->actor, 100, 100);
clutter_actor_set_position (state->actor, 150, 50);
state->timeline = clutter_timeline_new (1000);
clutter_timeline_set_loop (state->timeline, TRUE);
g_signal_connect (stage,
"key-press-event",
G_CALLBACK (key_pressed_cb),
state);
g_signal_connect (state->timeline,
"completed",
G_CALLBACK (invert_timeline_cb),
NULL);
clutter_container_add_actor (CLUTTER_CONTAINER (stage), state->actor);
clutter_actor_show (stage);
clutter_main ();
g_object_unref (state->timeline);
g_free (state);
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,83 @@
#include <stdlib.h>
#include <clutter/clutter.h>
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
static const ClutterColor red_color = { 0xff, 0x00, 0x00, 0xff };
static void
next_state (ClutterState *transitions,
gpointer user_data)
{
const gchar *state = clutter_state_get_state (transitions);
if (g_strcmp0 (state, "right") == 0)
clutter_state_set_state (transitions, "left");
else
clutter_state_set_state (transitions, "right");
}
static gboolean
key_pressed_cb (ClutterActor *actor,
ClutterEvent *event,
gpointer user_data)
{
ClutterState *transitions = CLUTTER_STATE (user_data);
if (!clutter_timeline_is_playing (clutter_state_get_timeline (transitions)))
next_state (transitions, NULL);
return TRUE;
}
int
main (int argc,
char *argv[])
{
ClutterActor *stage;
ClutterActor *actor;
ClutterState *transitions;
clutter_init (&argc, &argv);
stage = clutter_stage_get_default ();
clutter_actor_set_size (stage, 300, 200);
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
actor = clutter_rectangle_new_with_color (&red_color);
clutter_actor_set_position (actor, 150, 50);
clutter_actor_set_size (actor, 100, 100);
transitions = clutter_state_new ();
clutter_state_set_duration (transitions, NULL, NULL, 1000);
clutter_state_set (transitions, NULL, "right",
actor, "x", CLUTTER_LINEAR, 150.0,
NULL);
clutter_state_set (transitions, NULL, "left",
actor, "x", CLUTTER_LINEAR, 50.0,
NULL);
clutter_state_warp_to_state (transitions, "right");
g_signal_connect (stage,
"key-press-event",
G_CALLBACK (key_pressed_cb),
transitions);
g_signal_connect (transitions,
"completed",
G_CALLBACK (next_state),
NULL);
clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor);
clutter_actor_show (stage);
clutter_main ();
g_object_unref (transitions);
return EXIT_SUCCESS;
}