Rename ActorClone to Clone/2

Step two: rename the object and its methods.

While we're at it, adhere more strictly to the coding style
practises; rename :clone-source to :source; add a setter method
for the :source property; take a reference on the source actor
to avoid it disappearing while we're still accessing it.
This commit is contained in:
Emmanuele Bassi 2009-01-26 11:03:12 +00:00
parent 9197646d25
commit 86e4e89bf1
3 changed files with 175 additions and 132 deletions

View File

@ -26,7 +26,7 @@
* @short_description: An actor that displays a scaled clone of another * @short_description: An actor that displays a scaled clone of another
* actor. * actor.
* *
* #ClutterActorClone is a #ClutterActor which draws with the paint * #ClutterClone is a #ClutterActor which draws with the paint
* function of another actor, scaled to fit its own allocation. * function of another actor, scaled to fit its own allocation.
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -41,34 +41,40 @@
#include "cogl/cogl.h" #include "cogl/cogl.h"
G_DEFINE_TYPE (ClutterActorClone, clutter_actor_clone, CLUTTER_TYPE_ACTOR); G_DEFINE_TYPE (ClutterClone, clutter_clone, CLUTTER_TYPE_ACTOR);
enum enum
{ {
PROP_0, PROP_0,
PROP_CLONE_SOURCE PROP_SOURCE
}; };
#define CLUTTER_ACTOR_CLONE_GET_PRIVATE(obj) \ #define CLUTTER_CLONE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_CLONE, ClutterClonePrivate))
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
CLUTTER_TYPE_ACTOR_CLONE, \
ClutterActorClonePrivate))
struct _ClutterActorClonePrivate struct _ClutterClonePrivate
{ {
ClutterActor *clone_source; ClutterActor *clone_source;
}; };
static void static void
clutter_actor_clone_get_preferred_width (ClutterActor *self, clutter_clone_get_preferred_width (ClutterActor *self,
ClutterUnit for_height, ClutterUnit for_height,
ClutterUnit *min_width_p, ClutterUnit *min_width_p,
ClutterUnit *natural_width_p) ClutterUnit *natural_width_p)
{ {
ClutterActorClonePrivate *priv = CLUTTER_ACTOR_CLONE (self)->priv; ClutterClonePrivate *priv = CLUTTER_CLONE (self)->priv;
ClutterActor *clone_source = priv->clone_source; ClutterActor *clone_source = priv->clone_source;
if (G_UNLIKELY (clone_source == NULL))
{
if (min_width_p)
*min_width_p = 0;
if (natural_width_p)
*natural_width_p = 0;
}
else
clutter_actor_get_preferred_width (clone_source, clutter_actor_get_preferred_width (clone_source,
for_height, for_height,
min_width_p, min_width_p,
@ -76,14 +82,23 @@ clutter_actor_clone_get_preferred_width (ClutterActor *self,
} }
static void static void
clutter_actor_clone_get_preferred_height (ClutterActor *self, clutter_clone_get_preferred_height (ClutterActor *self,
ClutterUnit for_width, ClutterUnit for_width,
ClutterUnit *min_height_p, ClutterUnit *min_height_p,
ClutterUnit *natural_height_p) ClutterUnit *natural_height_p)
{ {
ClutterActorClonePrivate *priv = CLUTTER_ACTOR_CLONE (self)->priv; ClutterClonePrivate *priv = CLUTTER_CLONE (self)->priv;
ClutterActor *clone_source = priv->clone_source; ClutterActor *clone_source = priv->clone_source;
if (G_UNLIKELY (clone_source == NULL))
{
if (min_height_p)
*min_height_p = 0;
if (natural_height_p)
*natural_height_p = 0;
}
else
clutter_actor_get_preferred_height (clone_source, clutter_actor_get_preferred_height (clone_source,
for_width, for_width,
min_height_p, min_height_p,
@ -91,14 +106,16 @@ clutter_actor_clone_get_preferred_height (ClutterActor *self,
} }
static void static void
clutter_actor_clone_paint (ClutterActor *self) clutter_clone_paint (ClutterActor *self)
{ {
ClutterActorClone *clone = CLUTTER_ACTOR_CLONE (self); ClutterClone *clone = CLUTTER_CLONE (self);
ClutterActorClonePrivate *priv = clone->priv; ClutterClonePrivate *priv = clone->priv;
ClutterGeometry geom; ClutterGeometry geom;
ClutterGeometry clone_source_geom; ClutterGeometry clone_source_geom;
float x_scale; gfloat x_scale, y_scale;
float y_scale;
if (G_UNLIKELY (priv->clone_source == NULL))
return;
CLUTTER_NOTE (PAINT, CLUTTER_NOTE (PAINT,
"painting clone actor '%s'", "painting clone actor '%s'",
@ -112,10 +129,11 @@ clutter_actor_clone_paint (ClutterActor *self)
/* We need to scale what the clone-source actor paints to fill our own /* We need to scale what the clone-source actor paints to fill our own
* allocation... */ * allocation... */
x_scale = (float)geom.width / clone_source_geom.width; x_scale = (gfloat) geom.width / clone_source_geom.width;
y_scale = (float)geom.height / clone_source_geom.height; y_scale = (gfloat) geom.height / clone_source_geom.height;
cogl_scale (COGL_FIXED_FROM_FLOAT (x_scale), COGL_FIXED_FROM_FLOAT (y_scale)); cogl_scale (COGL_FIXED_FROM_FLOAT (x_scale),
COGL_FIXED_FROM_FLOAT (y_scale));
/* The final bits of magic: /* The final bits of magic:
* - We need to make sure that when the clone-source actor's paint method * - We need to make sure that when the clone-source actor's paint method
@ -135,140 +153,166 @@ clutter_actor_clone_paint (ClutterActor *self)
} }
static void static void
clutter_actor_clone_set_property (GObject *object, clutter_clone_set_property (GObject *gobject,
guint prop_id, guint prop_id,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
ClutterActorClone *clone = CLUTTER_ACTOR_CLONE(object); ClutterClone *clone = CLUTTER_CLONE (gobject);
switch (prop_id) switch (prop_id)
{ {
case PROP_CLONE_SOURCE: case PROP_SOURCE:
clone->priv->clone_source = g_value_get_object (value); clutter_clone_set_source (clone, g_value_get_object (value));
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break; break;
} }
} }
static void static void
clutter_actor_clone_get_property (GObject *object, clutter_clone_get_property (GObject *gobject,
guint prop_id, guint prop_id,
GValue *value, GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
ClutterActorClonePrivate *priv = CLUTTER_ACTOR_CLONE(object)->priv; ClutterClonePrivate *priv = CLUTTER_CLONE (gobject)->priv;
switch (prop_id) switch (prop_id)
{ {
case PROP_CLONE_SOURCE: case PROP_SOURCE:
g_value_set_object (value, priv->clone_source); g_value_set_object (value, priv->clone_source);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break; break;
} }
} }
static void static void
clutter_actor_clone_finalize (GObject *object) clutter_clone_dispose (GObject *gobject)
{ {
G_OBJECT_CLASS (clutter_actor_clone_parent_class)->finalize (object); ClutterClonePrivate *priv = CLUTTER_CLONE (gobject)->priv;
if (priv->clone_source)
{
g_object_unref (priv->clone_source);
priv->clone_source = NULL;
}
G_OBJECT_CLASS (clutter_clone_parent_class)->dispose (gobject);
} }
static void static void
clutter_actor_clone_dispose (GObject *object) clutter_clone_class_init (ClutterCloneClass *klass)
{
G_OBJECT_CLASS (clutter_actor_clone_parent_class)->dispose (object);
}
static void
clutter_actor_clone_class_init (ClutterActorCloneClass *klass)
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass); ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);
GParamSpec *pspec = NULL;
actor_class->paint = clutter_actor_clone_paint; g_type_class_add_private (gobject_class, sizeof (ClutterClonePrivate));
actor_class->get_preferred_width =
clutter_actor_clone_get_preferred_width;
actor_class->get_preferred_height =
clutter_actor_clone_get_preferred_height;
gobject_class->finalize = clutter_actor_clone_finalize; actor_class->paint = clutter_clone_paint;
gobject_class->dispose = clutter_actor_clone_dispose; actor_class->get_preferred_width = clutter_clone_get_preferred_width;
gobject_class->set_property = clutter_actor_clone_set_property; actor_class->get_preferred_height = clutter_clone_get_preferred_height;
gobject_class->get_property = clutter_actor_clone_get_property;
gobject_class->dispose = clutter_clone_dispose;
gobject_class->set_property = clutter_clone_set_property;
gobject_class->get_property = clutter_clone_get_property;
/** /**
* ClutterActorClone:clone-source * ClutterClone:source:
* *
* This specifies the source actor being cloned * This property specifies the source actor being cloned.
* *
* Since: 1.0 * Since: 1.0
*/ */
g_object_class_install_property pspec = g_param_spec_object ("source",
(gobject_class, "Source",
PROP_CLONE_SOURCE,
g_param_spec_object ("clone-source",
"Clone Source",
"Specifies the actor to be cloned", "Specifies the actor to be cloned",
CLUTTER_TYPE_ACTOR, CLUTTER_TYPE_ACTOR,
G_PARAM_CONSTRUCT_ONLY G_PARAM_CONSTRUCT_ONLY |
|G_PARAM_WRITABLE CLUTTER_PARAM_READWRITE);
|G_PARAM_READABLE)); g_object_class_install_property (gobject_class, PROP_SOURCE, pspec);
g_type_class_add_private (gobject_class, sizeof (ClutterActorClonePrivate));
} }
static void static void
clutter_actor_clone_init (ClutterActorClone *self) clutter_clone_init (ClutterClone *self)
{ {
ClutterActorClonePrivate *priv; ClutterClonePrivate *priv;
self->priv = priv = CLUTTER_ACTOR_CLONE_GET_PRIVATE (self); self->priv = priv = CLUTTER_CLONE_GET_PRIVATE (self);
priv->clone_source = NULL; priv->clone_source = NULL;
} }
/** /**
* clutter_actor_clone_new: * clutter_clone_new:
* @source: a #ClutterActor, or %NULL
* *
* Creates a new #ClutterActor which clones clone_source. * Creates a new #ClutterActor which clones @source/
* *
* Return value: a new #ClutterActor * Return value: the newly created #ClutterClone
* *
* Since: 1.0 * Since: 1.0
*/ */
ClutterActor * ClutterActor *
clutter_actor_clone_new (ClutterActor *clone_source) clutter_clone_new (ClutterActor *source)
{ {
return g_object_new (CLUTTER_TYPE_ACTOR_CLONE, return g_object_new (CLUTTER_TYPE_CLONE, "source", source, NULL);
"clone-source", clone_source,
NULL);
} }
/** /**
* clutter_actor_clone_get_clone_source: * clutter_clone_set_source:
* @clone: a #ClutterClone
* @source: a #ClutterActor, or %NULL
* *
* @clone: a #ClutterActorClone * Sets @source as the source actor to be cloned by @clone.
* *
* Retrieves the source #ClutterActor being clone by @clone * Since: 1.0
*/
void
clutter_clone_set_source (ClutterClone *clone,
ClutterActor *source)
{
ClutterClonePrivate *priv;
g_return_if_fail (CLUTTER_IS_CLONE (clone));
g_return_if_fail (source == NULL || CLUTTER_IS_ACTOR (source));
priv = clone->priv;
if (priv->clone_source)
{
g_object_unref (priv->clone_source);
priv->clone_source = NULL;
}
if (source)
priv->clone_source = g_object_ref (source);
g_object_notify (G_OBJECT (clone), "source");
clutter_actor_queue_relayout (CLUTTER_ACTOR (clone));
}
/**
* clutter_clone_get_source:
* @clone: a #ClutterClone
*
* Retrieves the source #ClutterActor being cloned by @clone
* *
* Return value: the actor source for the clone * Return value: the actor source for the clone
* *
* Since: 1.0 * Since: 1.0
*/ */
ClutterActor * ClutterActor *
clutter_actor_clone_get_clone_source (ClutterActorClone *clone) clutter_clone_get_clone_source (ClutterClone *clone)
{ {
ClutterActorClonePrivate *priv; g_return_val_if_fail (CLUTTER_IS_CLONE (clone), NULL);
g_return_val_if_fail (CLUTTER_IS_ACTOR_CLONE (clone), NULL); return clone->priv->clone_source;
priv = clone->priv;
return priv->clone_source;
} }

View File

@ -25,35 +25,33 @@
#error "Only <clutter/clutter.h> can be included directly." #error "Only <clutter/clutter.h> can be included directly."
#endif #endif
#ifndef __CLUTTER_ACTOR_CLONE_H__ #ifndef __CLUTTER_CLONE_H__
#define __CLUTTER_ACTOR_CLONE_H__ #define __CLUTTER_CLONE_H__
#include <glib-object.h>
#include <clutter/clutter-actor.h> #include <clutter/clutter-actor.h>
#include <clutter/clutter-color.h>
G_BEGIN_DECLS G_BEGIN_DECLS
#define CLUTTER_TYPE_ACTOR_CLONE (clutter_actor_clone_get_type()) #define CLUTTER_TYPE_CLONE (clutter_clone_get_type())
#define CLUTTER_ACTOR_CLONE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_ACTOR_CLONE, ClutterActorClone)) #define CLUTTER_CLONE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_CLONE, ClutterClone))
#define CLUTTER_ACTOR_CLONE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_ACTOR_CLONE, ClutterActorCloneClass)) #define CLUTTER_CLONE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_CLONE, ClutterCloneClass))
#define CLUTTER_IS_ACTOR_CLONE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_ACTOR_CLONE)) #define CLUTTER_IS_CLONE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_CLONE))
#define CLUTTER_IS_ACTOR_CLONE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_ACTOR_CLONE)) #define CLUTTER_IS_CLONE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_CLONE))
#define CLUTTER_ACTOR_CLONE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_ACTOR_CLONE, ClutterActorCloneClass)) #define CLUTTER_CLONE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_CLONE, ClutterCloneClass))
typedef struct _ClutterActorClone ClutterActorClone; typedef struct _ClutterClone ClutterClone;
typedef struct _ClutterActorCloneClass ClutterActorCloneClass; typedef struct _ClutterCloneClass ClutterCloneClass;
typedef struct _ClutterActorClonePrivate ClutterActorClonePrivate; typedef struct _ClutterClonePrivate ClutterClonePrivate;
struct _ClutterActorClone struct _ClutterClone
{ {
/*< private >*/ /*< private >*/
ClutterActor parent; ClutterActor parent_instance;
ClutterActorClonePrivate *priv; ClutterClonePrivate *priv;
}; };
struct _ClutterActorCloneClass struct _ClutterCloneClass
{ {
/*< private >*/ /*< private >*/
ClutterActorClass parent_class; ClutterActorClass parent_class;
@ -65,12 +63,13 @@ struct _ClutterActorCloneClass
void (*_clutter_actor_clone4) (void); void (*_clutter_actor_clone4) (void);
}; };
GType clutter_actor_clone_get_type (void) G_GNUC_CONST; GType clutter_clone_get_type (void) G_GNUC_CONST;
ClutterActor *clutter_actor_clone_new (ClutterActor *clone_source); ClutterActor *clutter_clone_new (ClutterActor *source);
void clutter_clone_set_source (ClutterClone *clone,
ClutterActor *clutter_actor_clone_get_clone_source (ClutterActorClone *clone); ClutterActor *source);
ClutterActor *clutter_clone_get_source (ClutterClone *clone);
G_END_DECLS G_END_DECLS
#endif /* __CLUTTER_ACTOR_CLONE_H__ */ #endif /* __CLUTTER_CLONE_H__ */

View File

@ -55,7 +55,7 @@ input_cb (ClutterActor *stage,
e = clutter_stage_get_actor_at_pos (CLUTTER_STAGE (stage), x, y); e = clutter_stage_get_actor_at_pos (CLUTTER_STAGE (stage), x, y);
if (e && (CLUTTER_IS_TEXTURE (e) || CLUTTER_IS_CLONE_TEXTURE (e))) if (e && (CLUTTER_IS_TEXTURE (e) || CLUTTER_IS_CLONE (e)))
{ {
clutter_actor_hide (e); clutter_actor_hide (e);
return TRUE; return TRUE;
@ -212,7 +212,7 @@ test_actor_clone_main (int argc, char *argv[])
gint x, y, w, h; gint x, y, w, h;
/* Create a texture from file, then clone in to same resources */ /* Create a texture from file, then clone in to same resources */
oh->hand[i] = clutter_actor_clone_new (real_hand); oh->hand[i] = clutter_clone_new (real_hand);
clutter_actor_set_size (oh->hand[i], 200, 213); clutter_actor_set_size (oh->hand[i], 200, 213);
/* Place around a circle */ /* Place around a circle */