1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Copyright (C) 2010 Intel Corporation.
7 *
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.
12 *
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.
17 *
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/>.
20 *
21 * Author:
22 * Emmanuele Bassi <ebassi@linux.intel.com>
23 */
24
25 /**
26 * SECTION:clutter-path-constraint
27 * @Title: ClutterPathConstraint
28 * @Short_Description: A constraint that follows a path
29 *
30 * #ClutterPathConstraint is a simple constraint that modifies the allocation
31 * of the #ClutterActor to which it has been applied using a #ClutterPath.
32 *
33 * By setting the #ClutterPathConstraint:offset property it is possible to
34 * control how far along the path the #ClutterActor should be.
35 *
36 * ClutterPathConstraint is available since Clutter 1.6.
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include "clutter-build-config.h"
41 #endif
42
43 #include "clutter-path-constraint.h"
44
45 #include "clutter-debug.h"
46 #include "clutter-marshal.h"
47 #include "clutter-private.h"
48
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))
52
53 struct _ClutterPathConstraint
54 {
55 ClutterConstraint parent_instance;
56
57 ClutterPath *path;
58
59 gfloat offset;
60
61 ClutterActor *actor;
62
63 guint current_node;
64 };
65
66 struct _ClutterPathConstraintClass
67 {
68 ClutterConstraintClass parent_class;
69 };
70
71 enum
72 {
73 PROP_0,
74
75 PROP_PATH,
76 PROP_OFFSET,
77
78 LAST_PROPERTY
79 };
80
81 enum
82 {
83 NODE_REACHED,
84
85 LAST_SIGNAL
86 };
87
88 G_DEFINE_TYPE (ClutterPathConstraint, clutter_path_constraint, CLUTTER_TYPE_CONSTRAINT);
89
90 static GParamSpec *path_properties[LAST_PROPERTY] = { NULL, };
91 static guint path_signals[LAST_SIGNAL] = { 0, };
92
93 static void
clutter_path_constraint_update_allocation(ClutterConstraint * constraint,ClutterActor * actor,ClutterActorBox * allocation)94 clutter_path_constraint_update_allocation (ClutterConstraint *constraint,
95 ClutterActor *actor,
96 ClutterActorBox *allocation)
97 {
98 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (constraint);
99 gfloat width, height;
100 ClutterKnot position;
101 guint knot_id;
102
103 if (self->path == NULL)
104 return;
105
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;
112
113 if (knot_id != self->current_node)
114 {
115 self->current_node = knot_id;
116 g_signal_emit (self, path_signals[NODE_REACHED], 0,
117 self->actor,
118 self->current_node);
119 }
120 }
121
122 static void
clutter_path_constraint_set_actor(ClutterActorMeta * meta,ClutterActor * new_actor)123 clutter_path_constraint_set_actor (ClutterActorMeta *meta,
124 ClutterActor *new_actor)
125 {
126 ClutterPathConstraint *path = CLUTTER_PATH_CONSTRAINT (meta);
127 ClutterActorMetaClass *parent;
128
129 /* store the pointer to the actor, for later use */
130 path->actor = new_actor;
131
132 parent = CLUTTER_ACTOR_META_CLASS (clutter_path_constraint_parent_class);
133 parent->set_actor (meta, new_actor);
134 }
135
136 static void
clutter_path_constraint_dispose(GObject * gobject)137 clutter_path_constraint_dispose (GObject *gobject)
138 {
139 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
140
141 if (self->path != NULL)
142 {
143 g_object_unref (self->path);
144 self->path = NULL;
145 }
146
147 G_OBJECT_CLASS (clutter_path_constraint_parent_class)->dispose (gobject);
148 }
149
150 static void
clutter_path_constraint_set_property(GObject * gobject,guint prop_id,const GValue * value,GParamSpec * pspec)151 clutter_path_constraint_set_property (GObject *gobject,
152 guint prop_id,
153 const GValue *value,
154 GParamSpec *pspec)
155 {
156 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
157
158 switch (prop_id)
159 {
160 case PROP_PATH:
161 clutter_path_constraint_set_path (self, g_value_get_object (value));
162 break;
163
164 case PROP_OFFSET:
165 clutter_path_constraint_set_offset (self, g_value_get_float (value));
166 break;
167
168 default:
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
170 }
171 }
172
173 static void
clutter_path_constraint_get_property(GObject * gobject,guint prop_id,GValue * value,GParamSpec * pspec)174 clutter_path_constraint_get_property (GObject *gobject,
175 guint prop_id,
176 GValue *value,
177 GParamSpec *pspec)
178 {
179 ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
180
181 switch (prop_id)
182 {
183 case PROP_PATH:
184 g_value_set_object (value, self->path);
185 break;
186
187 case PROP_OFFSET:
188 g_value_set_float (value, self->offset);
189 break;
190
191 default:
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
193 }
194 }
195
196 static void
clutter_path_constraint_class_init(ClutterPathConstraintClass * klass)197 clutter_path_constraint_class_init (ClutterPathConstraintClass *klass)
198 {
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);
202
203 /**
204 * ClutterPathConstraint:path:
205 *
206 * The #ClutterPath used to constrain the position of an actor.
207 *
208 * Since: 1.6
209 */
210 path_properties[PROP_PATH] =
211 g_param_spec_object ("path",
212 P_("Path"),
213 P_("The path used to constrain an actor"),
214 CLUTTER_TYPE_PATH,
215 CLUTTER_PARAM_READWRITE);
216
217 /**
218 * ClutterPathConstraint:offset:
219 *
220 * The offset along the #ClutterPathConstraint:path, between -1.0 and 2.0.
221 *
222 * Since: 1.6
223 */
224 path_properties[PROP_OFFSET] =
225 g_param_spec_float ("offset",
226 P_("Offset"),
227 P_("The offset along the path, between -1.0 and 2.0"),
228 -1.0, 2.0,
229 0.0,
230 CLUTTER_PARAM_READWRITE);
231
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,
236 LAST_PROPERTY,
237 path_properties);
238
239 meta_class->set_actor = clutter_path_constraint_set_actor;
240
241 constraint_class->update_allocation = clutter_path_constraint_update_allocation;
242
243 /**
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
248 *
249 * The ::node-reached signal is emitted each time a
250 * #ClutterPathConstraint:offset value results in the actor
251 * passing a #ClutterPathNode
252 *
253 * Since: 1.6
254 */
255 path_signals[NODE_REACHED] =
256 g_signal_new (I_("node-reached"),
257 G_TYPE_FROM_CLASS (klass),
258 G_SIGNAL_RUN_LAST,
259 0,
260 NULL, NULL,
261 _clutter_marshal_VOID__OBJECT_UINT,
262 G_TYPE_NONE, 2,
263 CLUTTER_TYPE_ACTOR,
264 G_TYPE_UINT);
265 }
266
267 static void
clutter_path_constraint_init(ClutterPathConstraint * self)268 clutter_path_constraint_init (ClutterPathConstraint *self)
269 {
270 self->offset = 0.0f;
271 self->current_node = G_MAXUINT;
272 }
273
274 /**
275 * clutter_path_constraint_new:
276 * @path: (allow-none): a #ClutterPath, or %NULL
277 * @offset: the offset along the #ClutterPath
278 *
279 * Creates a new #ClutterPathConstraint with the given @path and @offset
280 *
281 * Return value: the newly created #ClutterPathConstraint
282 *
283 * Since: 1.6
284 */
285 ClutterConstraint *
clutter_path_constraint_new(ClutterPath * path,gfloat offset)286 clutter_path_constraint_new (ClutterPath *path,
287 gfloat offset)
288 {
289 g_return_val_if_fail (path == NULL || CLUTTER_IS_PATH (path), NULL);
290
291 return g_object_new (CLUTTER_TYPE_PATH_CONSTRAINT,
292 "path", path,
293 "offset", offset,
294 NULL);
295 }
296
297 /**
298 * clutter_path_constraint_set_path:
299 * @constraint: a #ClutterPathConstraint
300 * @path: (allow-none): a #ClutterPath
301 *
302 * Sets the @path to be followed by the #ClutterPathConstraint.
303 *
304 * The @constraint will take ownership of the #ClutterPath passed to this
305 * function.
306 *
307 * Since: 1.6
308 */
309 void
clutter_path_constraint_set_path(ClutterPathConstraint * constraint,ClutterPath * path)310 clutter_path_constraint_set_path (ClutterPathConstraint *constraint,
311 ClutterPath *path)
312 {
313 g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));
314 g_return_if_fail (path == NULL || CLUTTER_IS_PATH (path));
315
316 if (constraint->path == path)
317 return;
318
319 if (constraint->path != NULL)
320 {
321 g_object_unref (constraint->path);
322 constraint->path = NULL;
323 }
324
325 if (path != NULL)
326 constraint->path = g_object_ref_sink (path);
327
328 if (constraint->actor != NULL)
329 clutter_actor_queue_relayout (constraint->actor);
330
331 g_object_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_PATH]);
332 }
333
334 /**
335 * clutter_path_constraint_get_path:
336 * @constraint: a #ClutterPathConstraint
337 *
338 * Retrieves a pointer to the #ClutterPath used by @constraint.
339 *
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
343 *
344 * Since: 1.6
345 */
346 ClutterPath *
clutter_path_constraint_get_path(ClutterPathConstraint * constraint)347 clutter_path_constraint_get_path (ClutterPathConstraint *constraint)
348 {
349 g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), NULL);
350
351 return constraint->path;
352 }
353
354 /**
355 * clutter_path_constraint_set_offset:
356 * @constraint: a #ClutterPathConstraint
357 * @offset: the offset along the path
358 *
359 * Sets the offset along the #ClutterPath used by @constraint.
360 *
361 * Since: 1.6
362 */
363 void
clutter_path_constraint_set_offset(ClutterPathConstraint * constraint,gfloat offset)364 clutter_path_constraint_set_offset (ClutterPathConstraint *constraint,
365 gfloat offset)
366 {
367 g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));
368
369 if (constraint->offset == offset)
370 return;
371
372 constraint->offset = offset;
373
374 if (constraint->actor != NULL)
375 clutter_actor_queue_relayout (constraint->actor);
376
377 g_object_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_OFFSET]);
378 }
379
380 /**
381 * clutter_path_constraint_get_offset:
382 * @constraint: a #ClutterPathConstraint
383 *
384 * Retrieves the offset along the #ClutterPath used by @constraint.
385 *
386 * Return value: the offset
387 *
388 * Since: 1.6
389 */
390 gfloat
clutter_path_constraint_get_offset(ClutterPathConstraint * constraint)391 clutter_path_constraint_get_offset (ClutterPathConstraint *constraint)
392 {
393 g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), 0.0);
394
395 return constraint->offset;
396 }
397