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