cookbook: Simplified and clarified example code

Modified the "animation reuse" sample code to provide
a simpler example to explain in the recipe.

Also modified variable names to mirror the names used
for the previous "complex animation" example and added
some more comments, to further simplify and support the
recipe.
This commit is contained in:
Elliot Smith 2010-09-09 12:02:02 +01:00
parent d7a3e35f46
commit 82ab00930b
3 changed files with 56 additions and 37 deletions

View File

@ -6,11 +6,8 @@
{ {
"type" : "ClutterAnimator", "type" : "ClutterAnimator",
"id" : "bounce", "id" : "animator",
"timeline" : { "duration" : 2000,
"type" : "ClutterTimeline",
"duration" : 2500
},
"properties" : [ "properties" : [
{ {
@ -18,21 +15,8 @@
"name" : "x", "name" : "x",
"ease-in" : true, "ease-in" : true,
"keys" : [ "keys" : [
[ 0.2, "easeOutCubic", 100.0 ], [ 0.0, "linear", 0.0 ],
[ 0.6, "easeOutCubic", 150.0 ], [ 1.0, "easeOutCubic", 150.0 ]
[ 0.8, "linear", 350.0 ],
[ 1.0, "linear", 500.0 ]
]
},
{
"object" : "rig",
"name" : "y",
"ease-in" : true,
"keys" : [
[ 0.2, "easeOutCubic", 200.0 ],
[ 0.6, "easeOutBounce", 200.0 ],
[ 0.8, "linear", 200.0 ],
[ 1.0, "linear", 0.0 ]
] ]
}, },
{ {
@ -40,10 +24,8 @@
"name" : "scale-x", "name" : "scale-x",
"ease-in" : true, "ease-in" : true,
"keys" : [ "keys" : [
[ 0.2, "easeOutElastic", 2.0 ], [ 0.5, "linear", 1.0 ],
[ 0.6, "easeOutBounce", 4.0 ], [ 1.0, "easeOutBack", 2.0 ]
[ 0.8, "linear", 4.0 ],
[ 1.0, "linear", 1.0 ]
] ]
}, },
{ {
@ -51,10 +33,8 @@
"name" : "scale-y", "name" : "scale-y",
"ease-in" : true, "ease-in" : true,
"keys" : [ "keys" : [
[ 0.2, "easeOutElastic", 2.0 ], [ 0.5, "linear", 1.0 ],
[ 0.6, "easeOutBounce", 4.0 ], [ 1.0, "easeOutBack", 2.0 ]
[ 0.8, "linear", 4.0 ],
[ 1.0, "linear", 1.0 ]
] ]
} }
] ]

View File

@ -2,8 +2,8 @@
{ {
"type" : "ClutterStage", "type" : "ClutterStage",
"id" : "stage", "id" : "stage",
"width" : 550, "width" : 300,
"height" : 400, "height" : 200,
"color" : "#333355ff", "color" : "#333355ff",
"signals" : [ "signals" : [
@ -11,13 +11,13 @@
], ],
"children" : [ "children" : [
{ {
"type" : "ClutterRectangle", "type" : "ClutterRectangle",
"id" : "rect1", "id" : "rect1",
"color" : "red", "color" : "white",
"width" : 50, "width" : 50,
"height" : 50, "height" : 50,
"y" : 50,
"reactive" : true, "reactive" : true,
"signals" : [ "signals" : [
{ "name" : "button-press-event", "handler" : "foo_button_pressed_cb" } { "name" : "button-press-event", "handler" : "foo_button_pressed_cb" }
@ -30,6 +30,7 @@
"color" : "blue", "color" : "blue",
"width" : 50, "width" : 50,
"height" : 50, "height" : 50,
"y" : 50,
"reactive" : true, "reactive" : true,
"signals" : [ "signals" : [
{ "name" : "button-press-event", "handler" : "foo_button_pressed_cb" } { "name" : "button-press-event", "handler" : "foo_button_pressed_cb" }
@ -42,12 +43,38 @@
"color" : "green", "color" : "green",
"width" : 50, "width" : 50,
"height" : 50, "height" : 50,
"y" : 50,
"reactive" : true,
"signals" : [
{ "name" : "button-press-event", "handler" : "foo_button_pressed_cb" }
]
},
{
"type" : "ClutterRectangle",
"id" : "rect4",
"color" : "red",
"width" : 50,
"height" : 50,
"y" : 50,
"reactive" : true,
"signals" : [
{ "name" : "button-press-event", "handler" : "foo_button_pressed_cb" }
]
},
{
"type" : "ClutterRectangle",
"id" : "rect5",
"color" : "grey",
"width" : 50,
"height" : 50,
"y" : 50,
"reactive" : true, "reactive" : true,
"signals" : [ "signals" : [
{ "name" : "button-press-event", "handler" : "foo_button_pressed_cb" } { "name" : "button-press-event", "handler" : "foo_button_pressed_cb" }
] ]
} }
] ]
} }
] ]

View File

@ -32,18 +32,20 @@ foo_button_pressed_cb (ClutterActor *actor,
ClutterScript *script; ClutterScript *script;
ClutterActor *rig; ClutterActor *rig;
ClutterAnimator *bounce; ClutterAnimator *animator;
/* load the rig and its animator from a JSON file */
script = clutter_script_new (); script = clutter_script_new ();
/* use a function defined statically in this source file to load the JSON */
load_script_from_file (script, ANIMATION_FILE); load_script_from_file (script, ANIMATION_FILE);
clutter_script_get_objects (script, clutter_script_get_objects (script,
"rig", &rig, "rig", &rig,
"bounce", &bounce, "animator", &animator,
NULL); NULL);
/* remove the button press handler */ /* remove the button press handler from the rectangle */
g_signal_handlers_disconnect_matched (actor, g_signal_handlers_disconnect_matched (actor,
G_SIGNAL_MATCH_FUNC, G_SIGNAL_MATCH_FUNC,
0, 0,
@ -55,11 +57,21 @@ foo_button_pressed_cb (ClutterActor *actor,
/* add a callback to clean up the script when the rig is destroyed */ /* add a callback to clean up the script when the rig is destroyed */
g_object_set_data_full (G_OBJECT (rig), "script", script, g_object_unref); g_object_set_data_full (G_OBJECT (rig), "script", script, g_object_unref);
/* add the rig to the stage */
clutter_container_add_actor (CLUTTER_CONTAINER (stage), rig); clutter_container_add_actor (CLUTTER_CONTAINER (stage), rig);
/* place the rig at the same coordinates on the stage as the rectangle */
clutter_actor_set_position (rig,
clutter_actor_get_x (actor),
clutter_actor_get_y (actor));
/* put the rectangle into the top-left corner of the rig */
clutter_actor_reparent (actor, rig); clutter_actor_reparent (actor, rig);
clutter_animator_start (bounce); clutter_actor_set_position (actor, 0, 0);
/* animate the rig */
clutter_animator_start (animator);
return TRUE; return TRUE;
} }