ClutterEffect

ClutterEffect — Base class for actor effects

Functions

Types and Values

Object Hierarchy

    GObject
    ╰── GInitiallyUnowned
        ╰── ClutterActorMeta
            ╰── ClutterEffect
                ╰── ClutterOffscreenEffect

Description

The ClutterEffect class provides a default type and API for creating effects for generic actors.

Effects are a ClutterActorMeta 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 ClutterActorClass.paint()_ virtual function.

Implementing a ClutterEffect

Creating a sub-class of ClutterEffect requires overriding the ClutterEffectClass.paint() method. The implementation of the function should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void effect_paint (ClutterEffect *effect, ClutterEffectPaintFlags flags)
{
  // Set up initialisation of the paint such as binding a
  // CoglOffscreen or other operations

  // 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.
  ClutterActor *actor =
    clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));

  clutter_actor_continue_paint (actor);

  // perform any cleanup of state, such as popping the CoglOffscreen
}

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.

A simple ClutterEffect implementation

The example below creates two rectangles: one will be painted "behind" the actor, while another will be painted "on top" of the actor.

The ClutterActorMetaClass.set_actor() implementation will create the two materials used for the two different rectangles; the ClutterEffectClass.paint() implementation will paint the first material using cogl_rectangle(), before continuing and then it will paint paint the second material after.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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);

  // Clear the previous state //
  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;
    }

  // Maintain a pointer to the actor
  self->actor = actor;

  // If we've been detached by the actor then we should just bail out here
  if (self->actor == NULL)
    return;

  // Create a red material
  self->rect_1 = cogl_material_new ();
  cogl_material_set_color4f (self->rect_1, 1.0, 0.0, 0.0, 1.0);

  // Create a green material
  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);

  // Paint the first rectangle in the upper left quadrant
  cogl_set_source (self->rect_1);
  cogl_rectangle (0, 0, width / 2, height / 2);

  // Continue to the rest of the paint sequence
  clutter_actor_continue_paint (self->actor);

  // Paint the second rectangle in the lower right quadrant
  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;
}

ClutterEffect is available since Clutter 1.4

Functions

clutter_effect_queue_repaint ()

void
clutter_effect_queue_repaint (ClutterEffect *effect);

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.

Parameters

effect

A ClutterEffect which needs redrawing

 

Since: 1.8


clutter_actor_add_effect ()

void
clutter_actor_add_effect (ClutterActor *self,
                          ClutterEffect *effect);

Adds effect to the list of ClutterEffects applied to self

The ClutterActor will hold a reference on the effect until either clutter_actor_remove_effect() or clutter_actor_clear_effects() is called.

Parameters

self

a ClutterActor

 

effect

a ClutterEffect

 

Since: 1.4


clutter_actor_add_effect_with_name ()

void
clutter_actor_add_effect_with_name (ClutterActor *self,
                                    const gchar *name,
                                    ClutterEffect *effect);

A convenience function for setting the name of a ClutterEffect while adding it to the list of effectss applied to self

This function is the logical equivalent of:

1
2
clutter_actor_meta_set_name (CLUTTER_ACTOR_META (effect), name);
clutter_actor_add_effect (self, effect);

Parameters

self

a ClutterActor

 

name

the name to set on the effect

 

effect

a ClutterEffect

 

Since: 1.4


clutter_actor_remove_effect ()

void
clutter_actor_remove_effect (ClutterActor *self,
                             ClutterEffect *effect);

Removes effect from the list of effects applied to self

The reference held by self on the ClutterEffect will be released

Parameters

self

a ClutterActor

 

effect

a ClutterEffect

 

Since: 1.4


clutter_actor_remove_effect_by_name ()

void
clutter_actor_remove_effect_by_name (ClutterActor *self,
                                     const gchar *name);

Removes the ClutterEffect with the given name from the list of effects applied to self

Parameters

self

a ClutterActor

 

name

the name of the effect to remove

 

Since: 1.4


clutter_actor_get_effects ()

GList *
clutter_actor_get_effects (ClutterActor *self);

Retrieves the ClutterEffects applied on self , if any

Parameters

self

a ClutterActor

 

Returns

a list of ClutterEffects, or NULL. The elements of the returned list are owned by Clutter and they should not be freed. You should free the returned list using g_list_free() when done.

[transfer container][element-type Clutter.Effect]

Since: 1.4


clutter_actor_get_effect ()

ClutterEffect *
clutter_actor_get_effect (ClutterActor *self,
                          const gchar *name);

Retrieves the ClutterEffect with the given name in the list of effects applied to self

Parameters

self

a ClutterActor

 

name

the name of the effect to retrieve

 

Returns

a ClutterEffect for the given name, or NULL. The returned ClutterEffect is owned by the actor and it should not be unreferenced directly.

[transfer none]

Since: 1.4


clutter_actor_clear_effects ()

void
clutter_actor_clear_effects (ClutterActor *self);

Clears the list of effects applied to self

Parameters

self

a ClutterActor

 

Since: 1.4


clutter_actor_has_effects ()

gboolean
clutter_actor_has_effects (ClutterActor *self);

Returns whether the actor has any effects applied.

Parameters

self

A ClutterActor

 

Returns

TRUE if the actor has any effects, FALSE otherwise

Since: 1.10

Types and Values

ClutterEffect

typedef struct _ClutterEffect ClutterEffect;

The ClutterEffect structure contains only private data and should be accessed using the provided API

Since: 1.4


struct ClutterEffectClass

struct ClutterEffectClass {
  gboolean (* pre_paint)        (ClutterEffect           *effect);
  void     (* post_paint)       (ClutterEffect           *effect);

  gboolean (* get_paint_volume) (ClutterEffect           *effect,
                                 ClutterPaintVolume      *volume);

  void     (* paint)            (ClutterEffect           *effect,
                                 ClutterEffectPaintFlags  flags);
  void     (* pick)             (ClutterEffect           *effect,
                                 ClutterEffectPaintFlags  flags);
};

The ClutterEffectClass structure contains only private data

Members

pre_paint ()

virtual function

 

post_paint ()

virtual function

 

get_paint_volume ()

virtual function

 

paint ()

virtual function

 

pick ()

virtual function

 

Since: 1.4