mirror of
https://github.com/brl/mutter.git
synced 2024-11-26 10:00:45 -05:00
cookbook: Example of animated movement with ClutterAnimator
Added an example showing how to randomly animate the simultaneous movement on the x axis of three actors, using ClutterAnimator.
This commit is contained in:
parent
f1b2f1e8c5
commit
6307876552
@ -4,6 +4,7 @@ NULL =
|
||||
|
||||
noinst_PROGRAMS = \
|
||||
animations-complex \
|
||||
animations-moving-animator \
|
||||
animations-moving-implicit \
|
||||
animations-moving-state \
|
||||
animations-reuse \
|
||||
@ -51,6 +52,7 @@ AM_CFLAGS = \
|
||||
AM_LDFLAGS = $(CLUTTER_LIBS) -export-dynamic
|
||||
|
||||
animations_complex_SOURCES = animations-complex.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
|
||||
animations_reuse_SOURCES = animations-reuse.c
|
||||
|
145
doc/cookbook/examples/animations-moving-animator.c
Normal file
145
doc/cookbook/examples/animations-moving-animator.c
Normal file
@ -0,0 +1,145 @@
|
||||
#include <stdlib.h>
|
||||
#include <clutter/clutter.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ClutterActor *stage;
|
||||
ClutterActor *group;
|
||||
ClutterAnimator *animator;
|
||||
} State;
|
||||
|
||||
static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff };
|
||||
static const ClutterColor red_color = { 0xff, 0x00, 0x00, 0xff };
|
||||
static const ClutterColor green_color = { 0x00, 0xff, 0x00, 0xff };
|
||||
static const ClutterColor blue_color = { 0x00, 0x00, 0xff, 0xff };
|
||||
|
||||
/* TODO remove this function when bug fixed */
|
||||
static void
|
||||
remove_keys (gpointer data,
|
||||
gpointer user_data)
|
||||
{
|
||||
ClutterAnimatorKey *key = (ClutterAnimatorKey *) data;
|
||||
State *state = (State *) user_data;
|
||||
|
||||
clutter_animator_remove_key (state->animator,
|
||||
clutter_animator_key_get_object (key),
|
||||
clutter_animator_key_get_property_name (key),
|
||||
clutter_animator_key_get_progress (key));
|
||||
}
|
||||
|
||||
/* add keys to the animator such that the actor is moved
|
||||
* to a random x position
|
||||
*/
|
||||
static void
|
||||
add_keys_for_actor (ClutterActor *actor,
|
||||
gpointer user_data)
|
||||
{
|
||||
ClutterAnimator *animator = CLUTTER_ANIMATOR (user_data);
|
||||
|
||||
gfloat x, end_x;
|
||||
|
||||
x = clutter_actor_get_x (actor);
|
||||
|
||||
end_x = 50.0;
|
||||
if (x == 50.0)
|
||||
end_x = 225.0 + (100.0 * rand () / (RAND_MAX + 1.0));
|
||||
|
||||
clutter_animator_set (animator,
|
||||
actor, "x", CLUTTER_LINEAR, 0.0, x,
|
||||
actor, "x", CLUTTER_EASE_OUT_CUBIC, 1.0, end_x,
|
||||
NULL);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
move_actors (ClutterActor *actor,
|
||||
ClutterEvent *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
State *state = (State *) user_data;
|
||||
GList *keys;
|
||||
|
||||
/* do nothing if the animator is already running */
|
||||
if (clutter_timeline_is_playing (clutter_animator_get_timeline (state->animator)))
|
||||
return TRUE;
|
||||
|
||||
/* TODO remove when bug fixed
|
||||
* remove all keys from the animator;
|
||||
* when this bug - http://bugzilla.clutter-project.org/show_bug.cgi?id=2335
|
||||
* - is fixed, can just use NULL for the second argument, like this:
|
||||
* clutter_animator_remove_key (state->animator, NULL, NULL, -1);
|
||||
*/
|
||||
keys = clutter_animator_get_keys (state->animator, NULL, NULL, -1);
|
||||
g_list_foreach (keys, remove_keys, state);
|
||||
|
||||
/* add keys for all actors in the group */
|
||||
clutter_container_foreach (CLUTTER_CONTAINER (state->group),
|
||||
add_keys_for_actor,
|
||||
state->animator);
|
||||
|
||||
/* start the animation */
|
||||
clutter_animator_start (state->animator);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
/* seed random number generator */
|
||||
srand ((unsigned int) time (NULL));
|
||||
|
||||
State *state = g_new0 (State, 1);
|
||||
|
||||
ClutterActor *red;
|
||||
ClutterActor *green;
|
||||
ClutterActor *blue;
|
||||
|
||||
clutter_init (&argc, &argv);
|
||||
|
||||
state->animator = clutter_animator_new ();
|
||||
clutter_animator_set_duration (state->animator, 500);
|
||||
|
||||
state->stage = clutter_stage_get_default ();
|
||||
clutter_actor_set_size (state->stage, 400, 350);
|
||||
clutter_stage_set_color (CLUTTER_STAGE (state->stage), &stage_color);
|
||||
g_signal_connect (state->stage,
|
||||
"destroy",
|
||||
G_CALLBACK (clutter_main_quit),
|
||||
NULL);
|
||||
|
||||
state->group = clutter_group_new ();
|
||||
|
||||
red = clutter_rectangle_new_with_color (&red_color);
|
||||
clutter_actor_set_size (red, 50, 50);
|
||||
clutter_actor_set_position (red, 50, 50);
|
||||
|
||||
green = clutter_rectangle_new_with_color (&green_color);
|
||||
clutter_actor_set_size (green, 50, 50);
|
||||
clutter_actor_set_position (green, 50, 150);
|
||||
|
||||
blue = clutter_rectangle_new_with_color (&blue_color);
|
||||
clutter_actor_set_size (blue, 50, 50);
|
||||
clutter_actor_set_position (blue, 50, 250);
|
||||
|
||||
g_signal_connect (state->stage,
|
||||
"key-press-event",
|
||||
G_CALLBACK (move_actors),
|
||||
state);
|
||||
|
||||
clutter_container_add (CLUTTER_CONTAINER (state->group),
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
NULL);
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (state->stage), state->group);
|
||||
|
||||
clutter_actor_show (state->stage);
|
||||
|
||||
clutter_main ();
|
||||
|
||||
g_object_unref (state->animator);
|
||||
g_free (state);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user