summaryrefslogtreecommitdiff
path: root/clutter/clutter-path-constraint.c
blob: 20b6789bae92d8c06bfc3fda1a36d6b5fd06f094 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2010  Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 *   Emmanuele Bassi <ebassi@linux.intel.com>
 */

/**
 * SECTION:clutter-path-constraint
 * @Title: ClutterPathConstraint
 * @Short_Description: A constraint that follows a path
 *
 * #ClutterPathConstraint is a simple constraint that modifies the allocation
 * of the #ClutterActor to which it has been applied using a #ClutterPath.
 *
 * By setting the #ClutterPathConstraint:offset property it is possible to
 * control how far along the path the #ClutterActor should be.
 *
 * ClutterPathConstraint is available since Clutter 1.6.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "clutter-path-constraint.h"

#include "clutter-debug.h"
#include "clutter-marshal.h"
#include "clutter-private.h"

#define CLUTTER_PATH_CONSTRAINT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_PATH_CONSTRAINT, ClutterPathConstraintClass))
#define CLUTTER_IS_PATH_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_PATH_CONSTRAINT))
#define CLUTTER_PATH_CONSTRAINT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_PATH_CONSTRAINT, ClutterPathConstraintClass))

struct _ClutterPathConstraint
{
  ClutterConstraint parent_instance;

  ClutterPath *path;

  gfloat offset;

  ClutterActor *actor;

  guint current_node;
};

struct _ClutterPathConstraintClass
{
  ClutterConstraintClass parent_class;
};

enum
{
  PROP_0,

  PROP_PATH,
  PROP_OFFSET,

  LAST_PROPERTY
};

enum
{
  NODE_REACHED,

  LAST_SIGNAL
};

G_DEFINE_TYPE (ClutterPathConstraint, clutter_path_constraint, CLUTTER_TYPE_CONSTRAINT);

static GParamSpec *path_properties[LAST_PROPERTY] = { NULL, };
static guint path_signals[LAST_SIGNAL] = { 0, };

static void
clutter_path_constraint_update_allocation (ClutterConstraint *constraint,
                                           ClutterActor      *actor,
                                           ClutterActorBox   *allocation)
{
  ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (constraint);
  gfloat width, height;
  ClutterKnot position;
  guint knot_id;

  if (self->path == NULL)
    return;

  knot_id = clutter_path_get_position (self->path, self->offset, &position);
  clutter_actor_box_get_size (allocation, &width, &height);
  allocation->x1 = position.x;
  allocation->y1 = position.y;
  allocation->x2 = allocation->x1 + width;
  allocation->y2 = allocation->y1 + height;

  if (knot_id != self->current_node)
    {
      self->current_node = knot_id;
      g_signal_emit (self, path_signals[NODE_REACHED], 0,
                     self->actor,
                     self->current_node);
    }
}

static void
clutter_path_constraint_set_actor (ClutterActorMeta *meta,
                                   ClutterActor     *new_actor)
{
  ClutterPathConstraint *path = CLUTTER_PATH_CONSTRAINT (meta);
  ClutterActorMetaClass *parent;

  /* store the pointer to the actor, for later use */
  path->actor = new_actor;

  parent = CLUTTER_ACTOR_META_CLASS (clutter_path_constraint_parent_class);
  parent->set_actor (meta, new_actor);
}

static void
clutter_path_constraint_dispose (GObject *gobject)
{
  ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);

  if (self->path != NULL)
    {
      g_object_unref (self->path);
      self->path = NULL;
    }

  G_OBJECT_CLASS (clutter_path_constraint_parent_class)->dispose (gobject);
}

static void
clutter_path_constraint_set_property (GObject      *gobject,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);

  switch (prop_id)
    {
    case PROP_PATH:
      clutter_path_constraint_set_path (self, g_value_get_object (value));
      break;

    case PROP_OFFSET:
      clutter_path_constraint_set_offset (self, g_value_get_float (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
clutter_path_constraint_get_property (GObject    *gobject,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);

  switch (prop_id)
    {
    case PROP_PATH:
      g_value_set_object (value, self->path);
      break;

    case PROP_OFFSET:
      g_value_set_float (value, self->offset);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
clutter_path_constraint_class_init (ClutterPathConstraintClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
  ClutterConstraintClass *constraint_class = CLUTTER_CONSTRAINT_CLASS (klass);

  /**
   * ClutterPathConstraint:path:
   *
   * The #ClutterPath used to constrain the position of an actor.
   *
   * Since: 1.6
   */
  path_properties[PROP_PATH] =
    g_param_spec_object ("path",
                         P_("Path"),
                         P_("The path used to constrain an actor"),
                         CLUTTER_TYPE_PATH,
                         CLUTTER_PARAM_READWRITE);

  /**
   * ClutterPathConstraint:offset:
   *
   * The offset along the #ClutterPathConstraint:path, between -1.0 and 2.0.
   *
   * Since: 1.6
   */
  path_properties[PROP_OFFSET] =
    g_param_spec_float ("offset",
                        P_("Offset"),
                        P_("The offset along the path, between -1.0 and 2.0"),
                        -1.0, 2.0,
                        0.0,
                        CLUTTER_PARAM_READWRITE);

  gobject_class->set_property = clutter_path_constraint_set_property;
  gobject_class->get_property = clutter_path_constraint_get_property;
  gobject_class->dispose = clutter_path_constraint_dispose;
  g_object_class_install_properties (gobject_class,
                                     LAST_PROPERTY,
                                     path_properties);

  meta_class->set_actor = clutter_path_constraint_set_actor;

  constraint_class->update_allocation = clutter_path_constraint_update_allocation;

  /**
   * ClutterPathConstraint::node-reached:
   * @constraint: the #ClutterPathConstraint that emitted the signal
   * @actor: the #ClutterActor using the @constraint
   * @index: the index of the node that has been reached
   *
   * The ::node-reached signal is emitted each time a
   * #ClutterPathConstraint:offset value results in the actor
   * passing a #ClutterPathNode
   *
   * Since: 1.6
   */
  path_signals[NODE_REACHED] =
    g_signal_new (I_("node-reached"),
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL,
                  _clutter_marshal_VOID__OBJECT_UINT,
                  G_TYPE_NONE, 2,
                  CLUTTER_TYPE_ACTOR,
                  G_TYPE_UINT);
}

static void
clutter_path_constraint_init (ClutterPathConstraint *self)
{
  self->offset = 0.0f;
  self->current_node = G_MAXUINT;
}

/**
 * clutter_path_constraint_new:
 * @path: (allow-none): a #ClutterPath, or %NULL
 * @offset: the offset along the #ClutterPath
 *
 * Creates a new #ClutterPathConstraint with the given @path and @offset
 *
 * Return value: the newly created #ClutterPathConstraint
 *
 * Since: 1.6
 */
ClutterConstraint *
clutter_path_constraint_new (ClutterPath *path,
                             gfloat       offset)
{
  g_return_val_if_fail (path == NULL || CLUTTER_IS_PATH (path), NULL);

  return g_object_new (CLUTTER_TYPE_PATH_CONSTRAINT,
                       "path", path,
                       "offset", offset,
                       NULL);
}

/**
 * clutter_path_constraint_set_path:
 * @constraint: a #ClutterPathConstraint
 * @path: (allow-none): a #ClutterPath
 *
 * Sets the @path to be followed by the #ClutterPathConstraint.
 *
 * The @constraint will take ownership of the #ClutterPath passed to this
 * function.
 *
 * Since: 1.6
 */
void
clutter_path_constraint_set_path (ClutterPathConstraint *constraint,
                                  ClutterPath           *path)
{
  g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));
  g_return_if_fail (path == NULL || CLUTTER_IS_PATH (path));

  if (constraint->path == path)
    return;

  if (constraint->path != NULL)
    {
      g_object_unref (constraint->path);
      constraint->path = NULL;
    }

  if (path != NULL)
    constraint->path = g_object_ref_sink (path);

  if (constraint->actor != NULL)
    clutter_actor_queue_relayout (constraint->actor);

  g_object_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_PATH]);
}

/**
 * clutter_path_constraint_get_path:
 * @constraint: a #ClutterPathConstraint
 *
 * Retrieves a pointer to the #ClutterPath used by @constraint.
 *
 * Return value: (transfer none): the #ClutterPath used by the
 *   #ClutterPathConstraint, or %NULL. The returned #ClutterPath is owned
 *   by the constraint and it should not be unreferenced
 *
 * Since: 1.6
 */
ClutterPath *
clutter_path_constraint_get_path (ClutterPathConstraint *constraint)
{
  g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), NULL);

  return constraint->path;
}

/**
 * clutter_path_constraint_set_offset:
 * @constraint: a #ClutterPathConstraint
 * @offset: the offset along the path
 *
 * Sets the offset along the #ClutterPath used by @constraint.
 *
 * Since: 1.6
 */
void
clutter_path_constraint_set_offset (ClutterPathConstraint *constraint,
                                    gfloat                 offset)
{
  g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));

  if (constraint->offset == offset)
    return;

  constraint->offset = offset;

  if (constraint->actor != NULL)
    clutter_actor_queue_relayout (constraint->actor);

  g_object_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_OFFSET]);
}

/**
 * clutter_path_constraint_get_offset:
 * @constraint: a #ClutterPathConstraint
 *
 * Retrieves the offset along the #ClutterPath used by @constraint.
 *
 * Return value: the offset
 *
 * Since: 1.6
 */
gfloat
clutter_path_constraint_get_offset (ClutterPathConstraint *constraint)
{
  g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), 0.0);

  return constraint->offset;
}