4 * An OpenGL based 'interactive canvas' library.
6 * Copyright (C) 2010 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Emmanuele Bassi <ebassi@linux.intel.com>
26 * SECTION:clutter-path-constraint
27 * @Title: ClutterPathConstraint
28 * @Short_Description: A constraint that follows a path
30 * #ClutterPathConstraint is a simple constraint that modifies the allocation
31 * of the #ClutterActor to which it has been applied using a #ClutterPath.
33 * By setting the #ClutterPathConstraint:offset property it is possible to
34 * control how far along the path the #ClutterActor should be.
36 * ClutterPathConstraint is available since Clutter 1.6.
43 #include "clutter-path-constraint.h"
45 #include "clutter-debug.h"
46 #include "clutter-marshal.h"
47 #include "clutter-private.h"
49 #define CLUTTER_PATH_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_PATH_CONSTRAINT, ClutterPathConstraintClass))
50 #define CLUTTER_IS_PATH_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_PATH_CONSTRAINT))
51 #define CLUTTER_PATH_CONSTRAINT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_PATH_CONSTRAINT, ClutterPathConstraintClass))
53 struct _ClutterPathConstraint
55 ClutterConstraint parent_instance;
66 struct _ClutterPathConstraintClass
68 ClutterConstraintClass parent_class;
88 G_DEFINE_TYPE (ClutterPathConstraint, clutter_path_constraint, CLUTTER_TYPE_CONSTRAINT);
90 static GParamSpec *path_properties[LAST_PROPERTY] = { NULL, };
91 static guint path_signals[LAST_SIGNAL] = { 0, };
94 clutter_path_constraint_update_allocation (ClutterConstraint *constraint,
96 ClutterActorBox *allocation)
98 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (constraint);
100 ClutterKnot position;
103 if (self->path == NULL)
106 knot_id = clutter_path_get_position (self->path, self->offset, &position);
107 clutter_actor_box_get_size (allocation, &width, &height);
108 allocation->x1 = position.x;
109 allocation->y1 = position.y;
110 allocation->x2 = allocation->x1 + width;
111 allocation->y2 = allocation->y1 + height;
113 if (knot_id != self->current_node)
115 self->current_node = knot_id;
116 g_signal_emit (self, path_signals[NODE_REACHED], 0,
123 clutter_path_constraint_set_actor (ClutterActorMeta *meta,
124 ClutterActor *new_actor)
126 ClutterPathConstraint *path = CLUTTER_PATH_CONSTRAINT (meta);
127 ClutterActorMetaClass *parent;
129 /* store the pointer to the actor, for later use */
130 path->actor = new_actor;
132 parent = CLUTTER_ACTOR_META_CLASS (clutter_path_constraint_parent_class);
133 parent->set_actor (meta, new_actor);
137 clutter_path_constraint_dispose (GObject *gobject)
139 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
141 if (self->path != NULL)
143 g_object_unref (self->path);
147 G_OBJECT_CLASS (clutter_path_constraint_parent_class)->dispose (gobject);
151 clutter_path_constraint_set_property (GObject *gobject,
156 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
161 clutter_path_constraint_set_path (self, g_value_get_object (value));
165 clutter_path_constraint_set_offset (self, g_value_get_float (value));
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
174 clutter_path_constraint_get_property (GObject *gobject,
179 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
184 g_value_set_object (value, self->path);
188 g_value_set_float (value, self->offset);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
197 clutter_path_constraint_class_init (ClutterPathConstraintClass *klass)
199 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
200 ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
201 ClutterConstraintClass *constraint_class = CLUTTER_CONSTRAINT_CLASS (klass);
204 * ClutterPathConstraint:path:
206 * The #ClutterPath used to constrain the position of an actor.
210 path_properties[PROP_PATH] =
211 g_param_spec_object ("path",
213 P_("The path used to constrain an actor"),
215 CLUTTER_PARAM_READWRITE);
218 * ClutterPathConstraint:offset:
220 * The offset along the #ClutterPathConstraint:path, between -1.0 and 2.0.
224 path_properties[PROP_OFFSET] =
225 g_param_spec_float ("offset",
227 P_("The offset along the path, between -1.0 and 2.0"),
230 CLUTTER_PARAM_READWRITE);
232 gobject_class->set_property = clutter_path_constraint_set_property;
233 gobject_class->get_property = clutter_path_constraint_get_property;
234 gobject_class->dispose = clutter_path_constraint_dispose;
235 g_object_class_install_properties (gobject_class,
239 meta_class->set_actor = clutter_path_constraint_set_actor;
241 constraint_class->update_allocation = clutter_path_constraint_update_allocation;
244 * ClutterPathConstraint::node-reached:
245 * @constraint: the #ClutterPathConstraint that emitted the signal
246 * @actor: the #ClutterActor using the @constraint
247 * @index: the index of the node that has been reached
249 * The ::node-reached signal is emitted each time a
250 * #ClutterPathConstraint:offset value results in the actor
251 * passing a #ClutterPathNode
255 path_signals[NODE_REACHED] =
256 g_signal_new (I_("node-reached"),
257 G_TYPE_FROM_CLASS (klass),
261 _clutter_marshal_VOID__OBJECT_UINT,
268 clutter_path_constraint_init (ClutterPathConstraint *self)
271 self->current_node = G_MAXUINT;
275 * clutter_path_constraint_new:
276 * @path: (allow-none): a #ClutterPath, or %NULL
277 * @offset: the offset along the #ClutterPath
279 * Creates a new #ClutterPathConstraint with the given @path and @offset
281 * Return value: (transfer full): the newly created #ClutterPathConstraint
286 clutter_path_constraint_new (ClutterPath *path,
289 g_return_val_if_fail (path == NULL || CLUTTER_IS_PATH (path), NULL);
291 return g_object_new (CLUTTER_TYPE_PATH_CONSTRAINT,
298 * clutter_path_constraint_set_path:
299 * @constraint: a #ClutterPathConstraint
300 * @path: (allow-none): a #ClutterPath
302 * Sets the @path to be followed by the #ClutterPathConstraint.
304 * The @constraint will take ownership of the #ClutterPath passed to this
310 clutter_path_constraint_set_path (ClutterPathConstraint *constraint,
313 g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));
314 g_return_if_fail (path == NULL || CLUTTER_IS_PATH (path));
316 if (constraint->path == path)
319 if (constraint->path != NULL)
321 g_object_unref (constraint->path);
322 constraint->path = NULL;
326 constraint->path = g_object_ref_sink (path);
328 if (constraint->actor != NULL)
329 clutter_actor_queue_relayout (constraint->actor);
331 g_object_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_PATH]);
335 * clutter_path_constraint_get_path:
336 * @constraint: a #ClutterPathConstraint
338 * Retrieves a pointer to the #ClutterPath used by @constraint.
340 * Return value: (transfer none): the #ClutterPath used by the
341 * #ClutterPathConstraint, or %NULL. The returned #ClutterPath is owned
342 * by the constraint and it should not be unreferenced
347 clutter_path_constraint_get_path (ClutterPathConstraint *constraint)
349 g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), NULL);
351 return constraint->path;
355 * clutter_path_constraint_set_offset:
356 * @constraint: a #ClutterPathConstraint
357 * @offset: the offset along the path
359 * Sets the offset along the #ClutterPath used by @constraint.
364 clutter_path_constraint_set_offset (ClutterPathConstraint *constraint,
367 g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));
369 if (constraint->offset == offset)
372 constraint->offset = offset;
374 if (constraint->actor != NULL)
375 clutter_actor_queue_relayout (constraint->actor);
377 g_object_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_OFFSET]);
381 * clutter_path_constraint_get_offset:
382 * @constraint: a #ClutterPathConstraint
384 * Retrieves the offset along the #ClutterPath used by @constraint.
386 * Return value: the offset
391 clutter_path_constraint_get_offset (ClutterPathConstraint *constraint)
393 g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), 0.0);
395 return constraint->offset;