constraints.c 3.94 KB
Newer Older
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1 2 3
#include <stdlib.h>
#include <clutter/clutter.h>

4 5
int
main (int argc, char *argv[])
Emmanuele Bassi's avatar
Emmanuele Bassi committed
6 7 8
{
  ClutterActor *stage, *layer_a, *layer_b, *layer_c;

9 10
  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
11 12 13 14 15

  /* the main container */
  stage = clutter_stage_new ();
  clutter_actor_set_name (stage, "stage");
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Snap Constraint");
16
  clutter_actor_set_background_color (stage, CLUTTER_COLOR_Aluminium1);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
17 18 19 20
  clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);

  /* first layer, with a fixed (100, 25) size */
21 22
  layer_a = clutter_actor_new ();
  clutter_actor_set_background_color (layer_a, CLUTTER_COLOR_ScarletRed);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
23 24
  clutter_actor_set_name (layer_a, "layerA");
  clutter_actor_set_size (layer_a, 100.0, 25.0);
25
  clutter_actor_add_child (stage, layer_a);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
26 27

  /* the first layer is anchored to the middle of the stage */
28
  clutter_actor_add_constraint (layer_a, clutter_align_constraint_new (stage, CLUTTER_ALIGN_BOTH, 0.5));
Emmanuele Bassi's avatar
Emmanuele Bassi committed
29 30

  /* second layer, with no implicit size */
31 32
  layer_b = clutter_actor_new ();
  clutter_actor_set_background_color (layer_b, CLUTTER_COLOR_DarkButter);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
33
  clutter_actor_set_name (layer_b, "layerB");
34
  clutter_actor_add_child (stage, layer_b);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

  /* the second layer tracks the X coordinate and the width of
   * the first layer
   */
  clutter_actor_add_constraint (layer_b, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_X, 0.0));
  clutter_actor_add_constraint (layer_b, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_WIDTH, 0.0));

  /* the second layer is snapped between the bottom edge of
   * the first layer, and the bottom edge of the stage; a
   * spacing of 10 pixels in each direction is added for padding
   */
  clutter_actor_add_constraint (layer_b,
                                clutter_snap_constraint_new (layer_a,
                                                             CLUTTER_SNAP_EDGE_TOP,
                                                             CLUTTER_SNAP_EDGE_BOTTOM,
                                                             10.0));

  clutter_actor_add_constraint (layer_b,
                                clutter_snap_constraint_new (stage,
                                                             CLUTTER_SNAP_EDGE_BOTTOM,
                                                             CLUTTER_SNAP_EDGE_BOTTOM,
                                                             -10.0));

  /* the third layer, with no implicit size */
59 60
  layer_c = clutter_actor_new ();
  clutter_actor_set_background_color (layer_c, CLUTTER_COLOR_LightChameleon);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
61
  clutter_actor_set_name (layer_c, "layerC");
62
  clutter_actor_add_child (stage, layer_c);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

  /* as for the second layer, the third layer tracks the X
   * coordinate and width of the first layer
   */
  clutter_actor_add_constraint (layer_c, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_X, 0.0));
  clutter_actor_add_constraint (layer_c, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_WIDTH, 0.0));

  /* the third layer is snapped between the top edge of the stage
   * and the top edge of the first layer; again, a spacing of
   * 10 pixels in each direction is added for padding
   */
  clutter_actor_add_constraint (layer_c,
                                clutter_snap_constraint_new (layer_a,
                                                             CLUTTER_SNAP_EDGE_BOTTOM,
                                                             CLUTTER_SNAP_EDGE_TOP,
                                                             -10.0));
  clutter_actor_add_constraint (layer_c,
                                clutter_snap_constraint_new (stage,
                                                             CLUTTER_SNAP_EDGE_TOP,
                                                             CLUTTER_SNAP_EDGE_TOP,
                                                             10.0));

  clutter_actor_show (stage);

  clutter_main ();

  return EXIT_SUCCESS;
}