From 8a149521cef9f2693d0c32e3cc4bf85a62dee026 Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Wed, 15 Sep 2010 16:05:15 +0100 Subject: [PATCH] cookbook: Simple example to demonstrate bind constraint A simple example showing how to scale an actor to the stage. Demonstrates ClutterBindConstraint and ClutterAlignConstraint in a fashion suitable for a short recipe. --- doc/cookbook/examples/Makefile.am | 2 + .../examples/layouts-bind-constraint-stage.c | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 doc/cookbook/examples/layouts-bind-constraint-stage.c diff --git a/doc/cookbook/examples/Makefile.am b/doc/cookbook/examples/Makefile.am index 26d9e0534..f2434f3a5 100644 --- a/doc/cookbook/examples/Makefile.am +++ b/doc/cookbook/examples/Makefile.am @@ -11,6 +11,7 @@ noinst_PROGRAMS = \ textures-split-go \ textures-sub-texture \ layouts-bind-constraint-overlay \ + layouts-bind-constraint-stage \ layouts-stacking \ layouts-stacking-diff-sized-actors \ events-mouse-scroll \ @@ -54,6 +55,7 @@ textures_reflection_SOURCES = textures-reflection.c textures_split_go_SOURCES = textures-split-go.c textures_sub_texture_SOURCES = textures-sub-texture.c layouts_bind_constraint_overlay_SOURCES = layouts-bind-constraint-overlay.c +layouts_bind_constraint_stage_SOURCES = layouts-bind-constraint-stage.c layouts_stacking_SOURCES = layouts-stacking.c layouts_stacking_diff_sized_actors_SOURCES = layouts-stacking-diff-sized-actors.c events_mouse_scroll_SOURCES = events-mouse-scroll.c diff --git a/doc/cookbook/examples/layouts-bind-constraint-stage.c b/doc/cookbook/examples/layouts-bind-constraint-stage.c new file mode 100644 index 000000000..a5b687553 --- /dev/null +++ b/doc/cookbook/examples/layouts-bind-constraint-stage.c @@ -0,0 +1,55 @@ +#include +#include + +static const ClutterColor stage_color = { 0x33, 0x33, 0x55, 0xff }; +static const ClutterColor rectangle_color = { 0xaa, 0x99, 0x00, 0xff }; + +int +main (int argc, char *argv[]) +{ + /* the stage is the "source" for constraints on the texture */ + ClutterActor *stage; + + /* the "target" actor which will be bound by the constraints */ + ClutterActor *texture; + + ClutterConstraint *width_binding; + ClutterConstraint *height_binding; + + clutter_init (&argc, &argv); + + stage = clutter_stage_new (); + clutter_actor_set_size (stage, 400, 400); + clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); + g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); + + /* make the stage resizable */ + clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE); + + texture = clutter_texture_new (); + clutter_actor_set_opacity (texture, 50); + clutter_texture_set_repeat (CLUTTER_TEXTURE (texture), TRUE, TRUE); + clutter_texture_set_from_file (CLUTTER_TEXTURE (texture), "smiley.png", NULL); + + /* the texture's width will be 100px less than the stage's */ + width_binding = clutter_bind_constraint_new (stage, CLUTTER_BIND_WIDTH, -100); + + /* the texture's height will be 100px less than the stage's */ + height_binding = clutter_bind_constraint_new (stage, CLUTTER_BIND_HEIGHT, -100); + + /* add the constraints to the texture */ + clutter_actor_add_constraint (texture, width_binding); + clutter_actor_add_constraint (texture, height_binding); + + /* add some alignment constraints */ + clutter_actor_add_constraint (texture, clutter_align_constraint_new (stage, CLUTTER_ALIGN_X_AXIS, 0.5)); + clutter_actor_add_constraint (texture, clutter_align_constraint_new (stage, CLUTTER_ALIGN_Y_AXIS, 0.5)); + + clutter_container_add_actor (CLUTTER_CONTAINER (stage), texture); + + clutter_actor_show (stage); + + clutter_main (); + + return EXIT_SUCCESS; +}