Next: , Previous: ClutterDropAction, Up: Top


32 ClutterEffect

Base class for actor effects

32.1 Overview

The <clutter-effect> class provides a default type and API for creating effects for generic actors.

Effects are a <clutter-actor-meta> sub-class that modify the way an actor is painted in a way that is not part of the actor's implementation.

Effects should be the preferred way to affect the paint sequence of an actor without sub-classing the actor itself and overriding the <"paint"> virtual function.

32.2 Implementing a ClutterEffect

Creating a sub-class of <clutter-effect> requires overriding the ‘paint’ method. The implementation of the function should look something like this:

     
     void effect_paint (ClutterEffect *effect, ClutterEffectPaintFlags flags)
     {
       /&#x002A; Set up initialisation of the paint such as binding a
          CoglOffscreen or other operations &#x002A;/
     
       /&#x002A; Chain to the next item in the paint sequence. This will either call
          ‘paint’ on the next effect or just paint the actor if this is
          the last effect. &#x002A;/
       ClutterActor *actor =
         clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
       clutter_actor_continue_paint (actor);
     
       /&#x002A; perform any cleanup of state, such as popping the
          CoglOffscreen &#x002A;/
     }
     

The effect can optionally avoid calling clutter-actor-continue-paint to skip any further stages of the paint sequence. This is useful for example if the effect contains a cached image of the actor. In that case it can optimise painting by avoiding the actor paint and instead painting the cached image. The ‘CLUTTER_EFFECT_PAINT_ACTOR_DIRTY’ flag is useful in this case. Clutter will set this flag when a redraw has been queued on the actor since it was last painted. The effect can use this information to decide if the cached image is still valid.

The ‘paint’ virtual was added in Clutter 1.8. Prior to that there were two separate functions as follows.

The pre-paint function was used to set up the <clutter-effect> right before the <clutter-actor>'s paint sequence. This function can fail, and return ‘#f’; in that case, no post-paint invocation will follow.

The post-paint function was called after the <clutter-actor>'s paint sequence.

With these two functions it is not possible to skip the rest of the paint sequence. The default implementation of the ‘paint’ virtual calls pre-paint, clutter-actor-continue-paint and then post-paint so that existing actors that aren't using the paint virtual will continue to work. New actors using the paint virtual do not need to implement pre or post paint.

The example below creates two rectangles: one will be painted "behind" the actor, while another will be painted "on top" of the actor. The set-actor implementation will create the two materials used for the two different rectangles; the paint function will paint the first material using cogl-rectangle, before continuing and then it will paint paint the second material after.

     
      typedef struct {
        ClutterEffect parent_instance;
     
        CoglHandle rect_1;
        CoglHandle rect_2;
      } MyEffect;
     
      typedef struct _ClutterEffectClass MyEffectClass;
     
      G_DEFINE_TYPE (MyEffect, my_effect, CLUTTER_TYPE_EFFECT);
     
      static void
      my_effect_set_actor (ClutterActorMeta *meta,
                           ClutterActor     *actor)
      {
        MyEffect *self = MY_EFFECT (meta);
     
        /&#x002A; Clear the previous state &#x002A;/
        if (self->rect_1)
          {
            cogl_handle_unref (self->rect_1);
            self->rect_1 = NULL;
          }
     
        if (self->rect_2)
          {
            cogl_handle_unref (self->rect_2);
            self->rect_2 = NULL;
          }
     
        /&#x002A; Maintain a pointer to the actor &#x002A;
        self->actor = actor;
     
        /&#x002A; If we've been detached by the actor then we should
         &#x002A; just bail out here
         &#x002A;/
        if (self->actor == NULL)
          return;
     
        /&#x002A; Create a red material &#x002A;/
        self->rect_1 = cogl_material_new ();
        cogl_material_set_color4f (self->rect_1, 1.0, 0.0, 0.0, 1.0);
     
        /&#x002A; Create a green material &#x002A;/
        self->rect_2 = cogl_material_new ();
        cogl_material_set_color4f (self->rect_2, 0.0, 1.0, 0.0, 1.0);
      }
     
      static gboolean
      my_effect_paint (ClutterEffect *effect)
      {
        MyEffect *self = MY_EFFECT (effect);
        gfloat width, height;
     
        clutter_actor_get_size (self->actor, &width, &height);
     
        /&#x002A; Paint the first rectangle in the upper left quadrant &#x002A;/
        cogl_set_source (self->rect_1);
        cogl_rectangle (0, 0, width / 2, height / 2);
     
        /&#x002A; Continue to the rest of the paint sequence &#x002A;/
        clutter_actor_continue_paint (self->actor);
     
        /&#x002A; Paint the second rectangle in the lower right quadrant &#x002A;/
        cogl_set_source (self->rect_2);
        cogl_rectangle (width / 2, height / 2, width, height);
      }
     
      static void
      my_effect_class_init (MyEffectClass *klass)
      {
        ClutterActorMetaClas *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
     
        meta_class->set_actor = my_effect_set_actor;
     
        klass->paint = my_effect_paint;
      }
     

<clutter-effect> is available since Clutter 1.4

32.3 Usage

— Function: clutter-effect-queue-repaint (self <clutter-effect>)
— Method: queue-repaint

Queues a repaint of the effect. The effect can detect when the ‘paint’ method is called as a result of this function because it will not have the ‘CLUTTER_EFFECT_PAINT_ACTOR_DIRTY’ flag set. In that case the effect is free to assume that the actor has not changed its appearance since the last time it was painted so it doesn't need to call clutter-actor-continue-paint if it can draw a cached image. This is mostly intended for effects that are using a ‘CoglOffscreen’ to redirect the actor (such as ‘ClutterOffscreenEffect’). In that case the effect can save a bit of rendering time by painting the cached texture without causing the entire actor to be painted.

This function can be used by effects that have their own animatable parameters. For example, an effect which adds a varying degree of a red tint to an actor by redirecting it through a CoglOffscreen might have a property to specify the level of tint. When this value changes, the underlying actor doesn't need to be redrawn so the effect can call clutter-effect-queue-repaint to make sure the effect is repainted.

Note however that modifying the position of the parent of an actor may change the appearance of the actor because its transformation matrix would change. In this case a redraw wouldn't be queued on the actor itself so the ‘CLUTTER_EFFECT_PAINT_ACTOR_DIRTY’ would still not be set. The effect can detect this case by keeping track of the last modelview matrix that was used to render the actor and veryifying that it remains the same in the next paint.

Any other effects that are layered on top of the passed in effect will still be passed the ‘CLUTTER_EFFECT_PAINT_ACTOR_DIRTY’ flag. If anything queues a redraw on the actor without specifying an effect or with an effect that is lower in the chain of effects than this one then that will override this call. In that case this effect will instead be called with the ‘CLUTTER_EFFECT_PAINT_ACTOR_DIRTY’ flag set.

effect
A <clutter-effect> which needs redrawing

Since 1.8