From b47b2f474910ead5b72eae88ebc53c2f667b93d7 Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Fri, 19 Nov 2010 14:29:39 +0000 Subject: [PATCH 1/4] cookbook: Added animated scaling example Added an example showing scaling of an actor on each of the scaling gravity settings (NORTH_WEST, NORTH etc.), with a mark indicating the center being used. Displays the transformed size and position, updated on each paint of the actor. --- doc/cookbook/examples/Makefile.am | 2 + doc/cookbook/examples/animations-scaling.c | 183 +++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 doc/cookbook/examples/animations-scaling.c diff --git a/doc/cookbook/examples/Makefile.am b/doc/cookbook/examples/Makefile.am index c34de68b6..64b3e2942 100644 --- a/doc/cookbook/examples/Makefile.am +++ b/doc/cookbook/examples/Makefile.am @@ -12,6 +12,7 @@ noinst_PROGRAMS = \ animations-moving-state \ animations-reuse \ animations-rotating \ + animations-scaling \ text-shadow \ textures-reflection \ textures-split-go \ @@ -66,6 +67,7 @@ animations_moving_implicit_SOURCES = animations-moving-implicit.c animations_moving_state_SOURCES = animations-moving-state.c animations_reuse_SOURCES = animations-reuse.c animations_rotating_SOURCES = animations-rotating.c +animations_scaling_SOURCES = animations-scaling.c text_shadow_SOURCES = text-shadow.c textures_reflection_SOURCES = textures-reflection.c textures_split_go_SOURCES = textures-split-go.c diff --git a/doc/cookbook/examples/animations-scaling.c b/doc/cookbook/examples/animations-scaling.c new file mode 100644 index 000000000..df723691b --- /dev/null +++ b/doc/cookbook/examples/animations-scaling.c @@ -0,0 +1,183 @@ +#include +#include + +typedef struct +{ + ClutterState *transitions; + ClutterActor *actor; + ClutterActor *props_display; + guint scale_gravity; + gboolean transitions_running; +} State; + +static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff }; +static const ClutterColor red_color = { 0xff, 0x00, 0x00, 0xff }; +static const ClutterColor yellow_color = { 0xff, 0xff, 0x00, 0xff }; + +static void +show_scale_properties_cb (ClutterActor *actor, + gpointer user_data) +{ + State *state = (State *) user_data; + + gfloat transformed_x, transformed_y; + gfloat transformed_width, transformed_height; + gfloat scale_center_x, scale_center_y; + + gchar *message; + + clutter_actor_get_transformed_position (state->actor, + &transformed_x, + &transformed_y); + + clutter_actor_get_transformed_size (state->actor, + &transformed_width, + &transformed_height); + + g_object_get (G_OBJECT (actor), + "scale-center-x", &scale_center_x, + "scale-center-y", &scale_center_y, + NULL); + + /* draw cross on the scale center */ + cogl_set_source_color4ub (255, 255, 0, 255); + + cogl_path_move_to (scale_center_x, scale_center_y); + cogl_path_rel_line_to (10, 10); + cogl_path_rel_line_to (-20, -20); + cogl_path_move_to (scale_center_x, scale_center_y); + cogl_path_rel_line_to (10, -10); + cogl_path_rel_line_to (-20, 20); + + cogl_path_stroke (); + + /* show actor properties */ + message = g_strdup_printf ("Scale center: %.0f, %.0f\n" + "Transformed position: %.2f, %.2f\n" + "Transformed size: %.2f, %.2f", + scale_center_x, scale_center_y, + transformed_x, transformed_y, + transformed_width, transformed_height); + + clutter_text_set_text (CLUTTER_TEXT (state->props_display), message); + + g_free (message); +} + +static void +next_transition_cb (ClutterState *transitions, + gpointer user_data) +{ + State *state = (State *) user_data; + + if (clutter_actor_is_scaled (state->actor)) + clutter_state_set_state (state->transitions, "not-scaled"); + else if (state->scale_gravity > 9) + { + /* gravity is at center, so reset ready for next key press */ + state->scale_gravity = CLUTTER_GRAVITY_NORTH; + + state->transitions_running = FALSE; + } + else + { + g_object_set (G_OBJECT (state->actor), + "scale-gravity", state->scale_gravity, + NULL); + + state->scale_gravity++; + + clutter_state_set_state (state->transitions, "scaled-down"); + } +} + +static gboolean +key_pressed_cb (ClutterActor *actor, + ClutterEvent *event, + gpointer user_data) +{ + State *state = (State *) user_data; + + if (!state->transitions_running) + { + state->transitions_running = TRUE; + next_transition_cb (NULL, state); + } + + 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, 350, 350); + clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); + g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); + + state->scale_gravity = CLUTTER_GRAVITY_NORTH; + state->transitions_running = FALSE; + + state->props_display = clutter_text_new (); + clutter_actor_set_size (state->props_display, 340, 80); + clutter_actor_set_position (state->props_display, 5, 280); + clutter_text_set_color (CLUTTER_TEXT (state->props_display), &yellow_color); + + state->actor = clutter_rectangle_new_with_color (&red_color); + clutter_actor_set_size (state->actor, 200, 200); + clutter_actor_set_position (state->actor, 75, 50); + + g_object_set (G_OBJECT (state->actor), + "scale-gravity", state->scale_gravity, + NULL); + + state->transitions = clutter_state_new (); + clutter_state_set_duration (state->transitions, NULL, NULL, 400); + + clutter_state_set (state->transitions, NULL, "not-scaled", + state->actor, "scale-x", CLUTTER_LINEAR, 1.0, + state->actor, "scale-y", CLUTTER_LINEAR, 1.0, + NULL); + + clutter_state_set (state->transitions, NULL, "scaled-down", + state->actor, "scale-x", CLUTTER_LINEAR, 0.25, + state->actor, "scale-y", CLUTTER_LINEAR, 0.25, + NULL); + + clutter_state_warp_to_state (state->transitions, "not-scaled"); + + g_signal_connect (stage, + "key-press-event", + G_CALLBACK (key_pressed_cb), + state); + + g_signal_connect (state->transitions, + "completed", + G_CALLBACK (next_transition_cb), + state); + + g_signal_connect_after (state->actor, + "paint", + G_CALLBACK (show_scale_properties_cb), + state); + + clutter_container_add (CLUTTER_CONTAINER (stage), + state->actor, + state->props_display, + NULL); + + clutter_actor_show (stage); + + clutter_main (); + + g_object_unref (state->transitions); + g_free (state); + + return EXIT_SUCCESS; +} From c8f112876e69ca2aeb9eb202c504bcf24ecab921 Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Fri, 19 Nov 2010 14:52:44 +0000 Subject: [PATCH 2/4] cookbook: Added "animated scaling" recipe skeleton --- doc/cookbook/animations.xml | 132 ++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/doc/cookbook/animations.xml b/doc/cookbook/animations.xml index ffcaa649d..6c0a674ea 100644 --- a/doc/cookbook/animations.xml +++ b/doc/cookbook/animations.xml @@ -2703,4 +2703,136 @@ timeline_completed_cb (ClutterTimeline *timeline, +
+ Animated scaling + +
+ Problem + + You want to animate scaling of an actor. +
+ +
+ Solution + + Animate the actor's scale-x and + scale-y properties to change the scaling on + the x and y axes respectively. + + For example, to animate an actor to twice its initial scale + with implicit animations: + + + + + + + + Alternatively, ClutterAnimator or + ClutterState can be used to animate an actor's scale + properties. See this + example for details. +
+ +
+ Discussion + + + +
+ Setting the scale center + + +
+ +
+ +
+ Full examples + + + Animated scaling of an actor using each of the + scale gravities. Press any key to start the animation. + + + a code sample should be here... but isn't + + + +
+ +
+ From ee9a4d02bba8bb0cf6ea73c5cdcce49fb29f786e Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Wed, 24 Nov 2010 12:22:12 +0000 Subject: [PATCH 3/4] cookbook: Add example of scaling a texture Added a simple application for scaling a texture while keeping the mouse pointer over the same coordinates on its surface. --- doc/cookbook/examples/Makefile.am | 2 + .../examples/animations-scaling-zoom.c | 155 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 doc/cookbook/examples/animations-scaling-zoom.c diff --git a/doc/cookbook/examples/Makefile.am b/doc/cookbook/examples/Makefile.am index 64b3e2942..7d3ed2ab3 100644 --- a/doc/cookbook/examples/Makefile.am +++ b/doc/cookbook/examples/Makefile.am @@ -13,6 +13,7 @@ noinst_PROGRAMS = \ animations-reuse \ animations-rotating \ animations-scaling \ + animations-scaling-zoom \ text-shadow \ textures-reflection \ textures-split-go \ @@ -68,6 +69,7 @@ animations_moving_state_SOURCES = animations-moving-state.c animations_reuse_SOURCES = animations-reuse.c animations_rotating_SOURCES = animations-rotating.c animations_scaling_SOURCES = animations-scaling.c +animations_scaling_zoom_SOURCES = animations-scaling-zoom.c text_shadow_SOURCES = text-shadow.c textures_reflection_SOURCES = textures-reflection.c textures_split_go_SOURCES = textures-split-go.c diff --git a/doc/cookbook/examples/animations-scaling-zoom.c b/doc/cookbook/examples/animations-scaling-zoom.c new file mode 100644 index 000000000..023b567c7 --- /dev/null +++ b/doc/cookbook/examples/animations-scaling-zoom.c @@ -0,0 +1,155 @@ +/* + * Load an image into a texture, which can then be zoomed in/out + * (double click on button 1, double click on button 3 respectively); + * also resets the texture to the stage center when a key is pressed + * (better would be to prevent drags taking the actor off-stage, + * but the implementation is much more complicated) + */ +#include +#include + +#define STAGE_SIDE 400.0 + +static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff }; + +/* on key press, center the actor on the stage; + * useful if you drag it off-stage accidentally + */ +static gboolean +key_press_cb (ClutterActor *actor, + ClutterEvent *event, + gpointer user_data) +{ + gfloat width, height; + + clutter_actor_get_size (actor, &width, &height); + + clutter_actor_set_anchor_point (actor, width / 2, height / 2); + + clutter_actor_set_position (actor, + STAGE_SIDE / 2, + STAGE_SIDE / 2); + + return TRUE; +} + +/* on double click, zoom in on the clicked point; + * also keeps scale in the range 0.1 to 20 + */ +static gboolean +clicked_cb (ClutterActor *actor, + ClutterEvent *event, + gpointer user_data) +{ + gdouble scale; + gfloat click_x, click_y; + gfloat click_target_x, click_target_y; + guint32 button; + + /* don't do anything unless there was a double click */ + if (clutter_event_get_click_count (event) < 2) + return TRUE; + + /* work out new scale */ + button = clutter_event_get_button (event); + + clutter_actor_get_scale (actor, &scale, NULL); + + if (button == 1) + scale *= 1.2; + else if (button == 3) + scale /= 1.2; + + /* don't do anything if scale is outside bounds */ + if (scale < 0.1 || scale > 20.0) + return TRUE; + + /* get the location of the click on the scaled actor */ + clutter_event_get_coords (event, &click_x, &click_y); + clutter_actor_transform_stage_point (actor, + click_x, click_y, + &click_target_x, &click_target_y); + + /* anchor the actor on the clicked point on its surface */ + clutter_actor_set_anchor_point (actor, click_target_x, click_target_y); + + /* set the actor's position to the click coords: it won't move, + * because the anchor point is already there; but + * the scale will now be centered on these coords (as the + * scale center defaults to the anchor point); so the anchor point + * on the actor won't move from under the pointer + */ + clutter_actor_set_position (actor, click_x, click_y); + + clutter_actor_animate (actor, CLUTTER_LINEAR, 500, + "scale-x", scale, + "scale-y", scale, + NULL); + + return TRUE; +} + +int +main (int argc, + char *argv[]) +{ + ClutterActor *stage; + ClutterActor *texture; + gchar *image_path; + GError *error = NULL; + + if (argc < 2) + { + g_print ("Usage: %s \n", argv[0]); + exit (EXIT_FAILURE); + } + + image_path = argv[1]; + + clutter_init (&argc, &argv); + + stage = clutter_stage_get_default (); + clutter_actor_set_size (stage, STAGE_SIDE, STAGE_SIDE); + clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); + g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); + + texture = clutter_texture_new (); + clutter_actor_set_reactive (texture, TRUE); + clutter_actor_set_width (texture, STAGE_SIDE); + clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (texture), TRUE); + + clutter_actor_add_action (texture, clutter_drag_action_new ()); + + g_object_set (G_OBJECT (texture), + "scale-gravity", CLUTTER_GRAVITY_NORTH_WEST, + NULL); + + clutter_texture_set_from_file (CLUTTER_TEXTURE (texture), image_path, &error); + + if (error != NULL) + { + g_warning ("Error loading %s\n%s", image_path, error->message); + g_error_free (error); + exit (EXIT_FAILURE); + } + + clutter_actor_set_y (texture, (STAGE_SIDE - clutter_actor_get_height (texture)) * 0.5); + + g_signal_connect (texture, + "button-release-event", + G_CALLBACK (clicked_cb), + NULL); + + g_signal_connect_swapped (stage, + "key-press-event", + G_CALLBACK (key_press_cb), + texture); + + clutter_container_add_actor (CLUTTER_CONTAINER (stage), texture); + + clutter_actor_show (stage); + + clutter_main (); + + return EXIT_SUCCESS; +} From c9d0f8b26e8ca6e67b52a1cc2a54d247919d09a9 Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Wed, 24 Nov 2010 12:44:35 +0000 Subject: [PATCH 4/4] cookbook: Add recipe for animated scaling of an actor Recipe explains how to animate scaling a single actor. Also covers scaling vs. resizing, scale center, and scaling within layouts and containers. The first example shows how animations around each scale gravity look, as well as tracking the transformed position and size of the actor and displaying those. The second example is a simple image viewer with zoom in/out using scaling. --- doc/cookbook/animations.xml | 285 +++++++++++++++++++++++++++++++----- 1 file changed, 247 insertions(+), 38 deletions(-) diff --git a/doc/cookbook/animations.xml b/doc/cookbook/animations.xml index 6c0a674ea..2534f29dc 100644 --- a/doc/cookbook/animations.xml +++ b/doc/cookbook/animations.xml @@ -2709,7 +2709,20 @@ timeline_completed_cb (ClutterTimeline *timeline,
Problem - You want to animate scaling of an actor. + You want to animate scaling of an actor. Example use + cases: + + + + To animate zooming in/out of a texture in an + image viewer application. + + + To add an animated "bounce" effect (quick scale up + followed by scale down) to a UI element + to indicate it has received focus. + +
@@ -2717,18 +2730,19 @@ timeline_completed_cb (ClutterTimeline *timeline, Animate the actor's scale-x and scale-y properties to change the scaling on - the x and y axes respectively. + the x and y axes + respectively. - For example, to animate an actor to twice its initial scale + For example, to animate an actor to twice its current scale with implicit animations: Alternatively, ClutterAnimator or ClutterState can be used to animate an actor's scale properties. See this - example for details. + example which uses ClutterState to animate + scaling.
Discussion - + + Scaling can easily change the apparent size + of multiple actors inside a container. For example, say you + wanted to shrink multiple actors inside a container + to half their original size. There are two ways you + could do this: + + + + The hard way would be to resize + each actor individually. You couldn't just resize the container, + as resizing a container doesn't resize its children: usually + they will be clipped so that they are either partially or + wholly hidden. + + + + The easy way would be to set the container's scale + to half its initial value: the actors + in the container would retain their original sizes, but would + appear at half size. + + + + + +
+ +
+ Scaling, layouts and containers + + It is possible to scale actors inside containers. For + example, if you were using a ClutterBox + which has a ClutterBoxLayout layout manager, + you could scale the children of that layout. + + However, you should remain aware that layout managers + don't take account of the scale of their children, only their + size. So if you scale up an actor inside a layout manager, + it may overlap other actors in the layout: the size allocated + by the layout manager doesn't increase as an actor's scale + increases. + + Similarly, scaling an actor down doesn't reduce the space + it will be allocated by a layout. +
Setting the scale center - + If this isn't desirable, you can just retain the scale + center on a scaled actor, and only change it when the actor + is unscaled. +
@@ -2828,6 +3026,17 @@ Note that the scale center is relative to the actor's actual size, not its trans a code sample should be here... but isn't + + + + + + Animated scaling (up and down) of a texture in response + to button presses. Call with the path to an image as the + first argument. + + + a code sample should be here... but isn't