## Creating You Own Behaviours

### Matthew Allum

`<mallum`openedhand.com`>`

Clutter comes with a number of fairly generic prebuilt behaviour classes which provide a basis for transitions, animations and other visual effects. However even with the ability to combine a number of these behaviours sometimes they are not enough and a custom behaviour is needed to create a spcific animation.

In order to implement a new ClutterBehaviour subclass the usual machinery for subclassing a GObject should be used. The new subclass then just overides the ClutterBehaviour::`alpha_notify()` method. This method is passed an alpha value which is then used to compute modifications to any actors the behaviour is applied to.

Example 23. Implementing the alpha-notify virtual function

This example demonstrates a behaviour that produces a vertical 'wipe' like affect by modifying the actors clip region

    static void
    clutter_behaviour_foo_alpha_notify (ClutterBehaviour *behaviour,
                                        gdouble           factor)
    {
      ClutterActor *actor
      gint i, n;

      n = clutter_behaviour_get_n_actors (behaviour);

      /* Change clip height of each applied actor. Note that it is
       * usually better to use clutter_behaviour_actors_foreach()
       * to avoid iterating multiple times
       */
      for (i = 0; i < n; i++)
        {
          gfloat clip_height;

          actor = clutter_behaviour_get_nth_actor (behaviour, i);

          clip_height = clutter_actor_get_height (actor)
                      - (clutter_actor_get_height (actor) * factor);

          clutter_actor_set_clip (actor,
                                  0,
                                  0,
                                  clutter_actor_get_width (actor),
                                  clip_height);
       }
    }

If the new behaviour is meant to set an initial state on the actors to which its applied to, then the ClutterBehaviour::applied signal class handler should be overridden.