~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to clutter/clutter-animator.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *   Ć˜yvind KolĆ„s <pippin@linux.intel.com>
 
23
 */
 
24
 
 
25
/**
 
26
 * SECTION:clutter-animator
 
27
 * @short_description: Multi-actor tweener
 
28
 * @See_Also: #ClutterAnimatable, #ClutterInterval, #ClutterAlpha,
 
29
 *   #ClutterTimeline
 
30
 *
 
31
 * #ClutterAnimator is an object providing declarative animations for
 
32
 * #GObject properties belonging to one or more #GObject<!-- -->s to
 
33
 * #ClutterIntervals.
 
34
 *
 
35
 * #ClutterAnimator is used to build and describe complex animations
 
36
 * in terms of "key frames". #ClutterAnimator is meant to be used
 
37
 * through the #ClutterScript definition format, but it comes with a
 
38
 * convenience C API.
 
39
 *
 
40
 * <refsect2 id="ClutterAnimator-key-frames">
 
41
 *   <title>Key Frames</title>
 
42
 *   <para>Every animation handled by a #ClutterAnimator can be
 
43
 *   described in terms of "key frames". For each #GObject property
 
44
 *   there can be multiple key frames, each one defined by the end
 
45
 *   value for the property to be computed starting from the current
 
46
 *   value to a specific point in time, using a given easing
 
47
 *   mode.</para>
 
48
 *   <para>The point in time is defined using a value representing
 
49
 *   the progress in the normalized interval of [ 0, 1 ]. This maps
 
50
 *   the value returned by clutter_timeline_get_duration().</para>
 
51
 *   <figure id="easing-modes">
 
52
 *     <title>Key Frames</title>
 
53
 *     <graphic fileref="animator-key-frames.png" format="PNG"/>
 
54
 *   </figure>
 
55
 *   <para>In the image above the duration of the animation is
 
56
 *   represented by the blue line. Each key frame is the white dot,
 
57
 *   along with its progress. The red line represents the computed
 
58
 *   function of time given the easing mode.</para>
 
59
 * </refsect2>
 
60
 *
 
61
 * <refsect2 id="ClutterAnimator-script">
 
62
 *   <title>ClutterAnimator description for #ClutterScript</title>
 
63
 *   <para>#ClutterAnimator defines a custom "properties" property
 
64
 *   which allows describing the key frames for objects.</para>
 
65
 *   <para>The "properties" property has the following syntax:</para>
 
66
 *   <informalexample>
 
67
 *     <programlisting>
 
68
 *  {
 
69
 *    "properties" : [
 
70
 *      {
 
71
 *        "object" : &lt;id of an object&gt;,
 
72
 *        "name" : &lt;name of the property&gt;,
 
73
 *        "ease-in" : &lt;boolean&gt;,
 
74
 *        "interpolation" : &lt;#ClutterInterpolation value&gt;,
 
75
 *        "keys" : [
 
76
 *          [ &lt;progress&gt;, &lt;easing mode&gt;, &lt;final value&gt; ]
 
77
 *        ]
 
78
 *    ]
 
79
 *  }
 
80
 *     </programlisting>
 
81
 *   </informalexample>
 
82
 *   <example id="ClutterAnimator-script-example">
 
83
 *     <title>ClutterAnimator definition</title>
 
84
 *     <para>The following JSON fragment defines a #ClutterAnimator
 
85
 *     with the duration of 1 second and operating on the x and y
 
86
 *     properties of a #ClutterActor named "rect-01", with two frames
 
87
 *     for each property. The first frame will linearly move the actor
 
88
 *     from its current position to the 100, 100 position in 20 percent
 
89
 *     of the duration of the animation; the second will using a cubic
 
90
 *     easing to move the actor to the 200, 200 coordinates.</para>
 
91
 *     <programlisting>
 
92
 *  {
 
93
 *    "type" : "ClutterAnimator",
 
94
 *    "duration" : 1000,
 
95
 *    "properties" : [
 
96
 *      {
 
97
 *        "object" : "rect-01",
 
98
 *        "name" : "x",
 
99
 *        "ease-in" : true,
 
100
 *        "keys" : [
 
101
 *          [ 0.2, "linear",       100.0 ],
 
102
 *          [ 1.0, "easeOutCubic", 200.0 ]
 
103
 *        ]
 
104
 *      },
 
105
 *      {
 
106
 *        "object" : "rect-01",
 
107
 *        "name" : "y",
 
108
 *        "ease-in" : true,
 
109
 *        "keys" : [
 
110
 *          [ 0.2, "linear",       100.0 ],
 
111
 *          [ 1.0, "easeOutCubic", 200.0 ]
 
112
 *        ]
 
113
 *      }
 
114
 *    ]
 
115
 *  }
 
116
 *     </programlisting>
 
117
 *   </example>
 
118
 * </refsect2>
 
119
 *
 
120
 * #ClutterAnimator is available since Clutter 1.2
 
121
 */
 
122
 
 
123
#ifdef HAVE_CONFIG_H
 
124
#include "config.h"
 
125
#endif
 
126
 
 
127
#include <string.h>
 
128
#include <gobject/gvaluecollector.h>
 
129
 
 
130
#include "clutter-animator.h"
 
131
 
 
132
#include "clutter-alpha.h"
 
133
#include "clutter-debug.h"
 
134
#include "clutter-enum-types.h"
 
135
#include "clutter-interval.h"
 
136
#include "clutter-private.h"
 
137
#include "clutter-script-private.h"
 
138
#include "clutter-scriptable.h"
 
139
 
 
140
#define CLUTTER_ANIMATOR_GET_PRIVATE(obj)       (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_ANIMATOR, ClutterAnimatorPrivate))
 
141
 
 
142
/* progress values varying by less than this are considered equal */
 
143
#define PROGRESS_EPSILON  0.00001
 
144
 
 
145
struct _ClutterAnimatorPrivate
 
146
{
 
147
  ClutterTimeline  *timeline;
 
148
  ClutterTimeline  *slave_timeline;
 
149
 
 
150
  GList            *score;
 
151
 
 
152
  GHashTable       *properties;
 
153
};
 
154
 
 
155
struct _ClutterAnimatorKey
 
156
{
 
157
  GObject             *object;
 
158
  const gchar         *property_name;
 
159
  guint                mode;
 
160
 
 
161
  GValue               value;
 
162
 
 
163
  /* normalized progress, between 0.0 and 1.0 */
 
164
  gdouble              progress;
 
165
 
 
166
  /* back-pointer to the animator which owns the key */
 
167
  ClutterAnimator     *animator;
 
168
 
 
169
  /* interpolation mode */
 
170
  ClutterInterpolation interpolation;
 
171
 
 
172
  /* ease from the current object state into the animation when it starts */
 
173
  guint                ease_in : 1;
 
174
 
 
175
  /* This key is already being destroyed and shouldn't
 
176
   * trigger additional weak unrefs
 
177
   */
 
178
  guint                is_inert : 1;
 
179
 
 
180
  gint                 ref_count;
 
181
};
 
182
 
 
183
enum
 
184
{
 
185
  PROP_0,
 
186
 
 
187
  PROP_DURATION,
 
188
  PROP_TIMELINE
 
189
};
 
190
 
 
191
static void clutter_scriptable_init (ClutterScriptableIface *iface);
 
192
 
 
193
G_DEFINE_TYPE_WITH_CODE (ClutterAnimator,
 
194
                         clutter_animator,
 
195
                         G_TYPE_OBJECT,
 
196
                         G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_SCRIPTABLE,
 
197
                                                clutter_scriptable_init));
 
198
/**
 
199
 * clutter_animator_new:
 
200
 *
 
201
 * Creates a new #ClutterAnimator instance
 
202
 *
 
203
 * Return value: a new #ClutterAnimator.
 
204
 *
 
205
 * Since: 1.2
 
206
 */
 
207
ClutterAnimator *
 
208
clutter_animator_new (void)
 
209
{
 
210
  return g_object_new (CLUTTER_TYPE_ANIMATOR, NULL);
 
211
}
 
212
 
 
213
/***/
 
214
 
 
215
typedef struct _PropObjectKey {
 
216
  GObject      *object;
 
217
  const gchar  *property_name;
 
218
  guint         mode;
 
219
  gdouble       progress;
 
220
} PropObjectKey;
 
221
 
 
222
typedef struct _KeyAnimator {
 
223
  PropObjectKey       *key;
 
224
  ClutterInterval     *interval;
 
225
  ClutterAlpha        *alpha;
 
226
 
 
227
  GList               *current;
 
228
 
 
229
  gdouble              start;    /* the progress of current */
 
230
  gdouble              end;      /* until which progress it is valid */
 
231
  ClutterInterpolation interpolation;
 
232
 
 
233
  guint                ease_in : 1;
 
234
} KeyAnimator;
 
235
 
 
236
static PropObjectKey *
 
237
prop_actor_key_new (GObject     *object,
 
238
                    const gchar *property_name)
 
239
{
 
240
  PropObjectKey *key = g_slice_new0 (PropObjectKey);
 
241
 
 
242
  key->object = object;
 
243
  key->property_name = g_intern_string (property_name);
 
244
 
 
245
  return key;
 
246
}
 
247
 
 
248
static void
 
249
prop_actor_key_free (gpointer key)
 
250
{
 
251
  if (key != NULL)
 
252
    g_slice_free (PropObjectKey, key);
 
253
}
 
254
 
 
255
static void
 
256
key_animator_free (gpointer key)
 
257
{
 
258
  if (key != NULL)
 
259
    {
 
260
      KeyAnimator *key_animator = key;
 
261
 
 
262
      g_object_unref (key_animator->interval);
 
263
      g_object_unref (key_animator->alpha);
 
264
 
 
265
      g_slice_free (KeyAnimator, key_animator);
 
266
    }
 
267
}
 
268
 
 
269
static KeyAnimator *
 
270
key_animator_new (ClutterAnimator *animator,
 
271
                  PropObjectKey   *key,
 
272
                  GType            type)
 
273
{
 
274
  KeyAnimator *key_animator = g_slice_new (KeyAnimator);
 
275
  ClutterInterval *interval = g_object_new (CLUTTER_TYPE_INTERVAL,
 
276
                                            "value-type", type,
 
277
                                            NULL);
 
278
 
 
279
  /* we own this interval */
 
280
  g_object_ref_sink (interval);
 
281
 
 
282
  key_animator->interval = interval;
 
283
  key_animator->key = key;
 
284
  key_animator->alpha = clutter_alpha_new ();
 
285
  clutter_alpha_set_timeline (key_animator->alpha,
 
286
                              animator->priv->slave_timeline);
 
287
 
 
288
  /* as well as the alpha */
 
289
  g_object_ref_sink (key_animator->alpha);
 
290
 
 
291
  return key_animator;
 
292
}
 
293
 
 
294
static guint
 
295
prop_actor_hash (gconstpointer value)
 
296
{
 
297
  const PropObjectKey *info = value;
 
298
 
 
299
  return GPOINTER_TO_INT (info->property_name)
 
300
       ^ GPOINTER_TO_INT (info->object);
 
301
}
 
302
 
 
303
static gboolean
 
304
prop_actor_equal (gconstpointer a, gconstpointer b)
 
305
{
 
306
  const PropObjectKey *infoa = a;
 
307
  const PropObjectKey *infob = b;
 
308
 
 
309
  /* property name strings are interned so we can just compare pointers */
 
310
  if (infoa->object == infob->object &&
 
311
      (infoa->property_name == infob->property_name))
 
312
    return TRUE;
 
313
 
 
314
  return FALSE;
 
315
}
 
316
 
 
317
static gint
 
318
sort_actor_prop_progress_func (gconstpointer a,
 
319
                               gconstpointer b)
 
320
{
 
321
  const ClutterAnimatorKey *pa = a;
 
322
  const ClutterAnimatorKey *pb = b;
 
323
 
 
324
  if (pa->object == pb->object)
 
325
    {
 
326
      gint pdiff = pb->property_name - pa->property_name;
 
327
 
 
328
      if (pdiff)
 
329
        return pdiff;
 
330
 
 
331
      if (fabs (pa->progress - pb->progress) < PROGRESS_EPSILON)
 
332
        return 0;
 
333
 
 
334
      if (pa->progress > pb->progress)
 
335
        return 1;
 
336
 
 
337
      return -1;
 
338
    }
 
339
 
 
340
  return pa->object - pb->object;
 
341
}
 
342
 
 
343
static gint
 
344
sort_actor_prop_func (gconstpointer a,
 
345
                      gconstpointer b)
 
346
{
 
347
  const ClutterAnimatorKey *pa = a;
 
348
  const ClutterAnimatorKey *pb = b;
 
349
 
 
350
  if (pa->object == pb->object)
 
351
    return pa->property_name - pb->property_name;
 
352
 
 
353
  return pa->object - pb->object;
 
354
}
 
355
 
 
356
 
 
357
static void
 
358
object_disappeared (gpointer  data,
 
359
                    GObject  *where_the_object_was)
 
360
{
 
361
  clutter_animator_remove_key (data, where_the_object_was, NULL, -1.0);
 
362
}
 
363
 
 
364
static ClutterAnimatorKey *
 
365
clutter_animator_key_new (ClutterAnimator *animator,
 
366
                          GObject         *object,
 
367
                          const gchar     *property_name,
 
368
                          gdouble          progress,
 
369
                          guint            mode)
 
370
{
 
371
  ClutterAnimatorKey *animator_key;
 
372
 
 
373
  animator_key = g_slice_new (ClutterAnimatorKey);
 
374
 
 
375
  animator_key->ref_count = 1;
 
376
  animator_key->animator = animator;
 
377
  animator_key->object = object;
 
378
  animator_key->mode = mode;
 
379
  memset (&(animator_key->value), 0, sizeof (GValue));
 
380
  animator_key->progress = progress;
 
381
  animator_key->property_name = g_intern_string (property_name);
 
382
  animator_key->interpolation = CLUTTER_INTERPOLATION_LINEAR;
 
383
  animator_key->ease_in = FALSE;
 
384
  animator_key->is_inert = FALSE;
 
385
 
 
386
  /* keep a weak reference on the animator, so that we can release the
 
387
   * back-pointer when needed
 
388
   */
 
389
  g_object_weak_ref (object, object_disappeared,
 
390
                     animator_key->animator);
 
391
 
 
392
  return animator_key;
 
393
}
 
394
 
 
395
static gpointer
 
396
clutter_animator_key_copy (gpointer boxed)
 
397
{
 
398
  ClutterAnimatorKey *key = boxed;
 
399
 
 
400
  if (key != NULL)
 
401
    key->ref_count += 1;
 
402
 
 
403
  return key;
 
404
}
 
405
 
 
406
static void
 
407
clutter_animator_key_free (gpointer boxed)
 
408
{
 
409
  ClutterAnimatorKey *key = boxed;
 
410
 
 
411
  if (key == NULL)
 
412
    return;
 
413
 
 
414
  key->ref_count -= 1;
 
415
 
 
416
  if (key->ref_count > 0)
 
417
    return;
 
418
 
 
419
  if (!key->is_inert)
 
420
    g_object_weak_unref (key->object, object_disappeared, key->animator);
 
421
 
 
422
  g_slice_free (ClutterAnimatorKey, key);
 
423
}
 
424
 
 
425
static void
 
426
clutter_animator_finalize (GObject *object)
 
427
{
 
428
  ClutterAnimator *animator = CLUTTER_ANIMATOR (object);
 
429
  ClutterAnimatorPrivate *priv = animator->priv;
 
430
 
 
431
  g_list_foreach (priv->score, (GFunc) clutter_animator_key_free, NULL);
 
432
  g_list_free (priv->score);
 
433
  priv->score = NULL;
 
434
 
 
435
#if 0
 
436
  for (; priv->score;
 
437
       priv->score = g_list_remove (priv->score, priv->score->data))
 
438
    {
 
439
      clutter_animator_key_free (priv->score->data);
 
440
    }
 
441
#endif
 
442
 
 
443
  g_object_unref (priv->timeline);
 
444
  g_object_unref (priv->slave_timeline);
 
445
 
 
446
  G_OBJECT_CLASS (clutter_animator_parent_class)->finalize (object);
 
447
}
 
448
 
 
449
/* XXX: this is copied and slightly modified from glib,
 
450
 * there is only one way to do this. */
 
451
static GList *
 
452
list_find_custom_reverse (GList         *list,
 
453
                          gconstpointer  data,
 
454
                          GCompareFunc   func)
 
455
{
 
456
  while (list)
 
457
    {
 
458
      if (! func (list->data, data))
 
459
        return list;
 
460
 
 
461
      list = list->prev;
 
462
    }
 
463
 
 
464
  return NULL;
 
465
}
 
466
 
 
467
/* Ensures that the interval provided by the animator is correct
 
468
 * for the requested progress value.
 
469
 */
 
470
static void
 
471
animation_animator_ensure_animator (ClutterAnimator *animator,
 
472
                                    KeyAnimator     *key_animator,
 
473
                                    PropObjectKey   *key,
 
474
                                    gdouble          progress)
 
475
{
 
476
 
 
477
  if (progress > key_animator->end)
 
478
    {
 
479
      while (progress > key_animator->end)
 
480
        {
 
481
          ClutterAnimatorKey *initial_key, *next_key;
 
482
          GList *initial, *next;
 
483
 
 
484
          initial = g_list_find_custom (key_animator->current->next,
 
485
                                        key,
 
486
                                        sort_actor_prop_func);
 
487
 
 
488
          if (initial)
 
489
            {
 
490
              initial_key = initial->data;
 
491
 
 
492
              clutter_interval_set_initial_value (key_animator->interval,
 
493
                                                  &initial_key->value);
 
494
              key_animator->current = initial;
 
495
              key_animator->start = initial_key->progress;
 
496
 
 
497
              next = g_list_find_custom (initial->next,
 
498
                                         key,
 
499
                                         sort_actor_prop_func);
 
500
              if (next)
 
501
                {
 
502
                  next_key = next->data;
 
503
 
 
504
                  key_animator->end = next_key->progress;
 
505
                }
 
506
              else
 
507
                {
 
508
                  next_key = initial_key;
 
509
 
 
510
                  key_animator->end = 1.0;
 
511
                }
 
512
 
 
513
              clutter_interval_set_final_value (key_animator->interval,
 
514
                                                &next_key->value);
 
515
 
 
516
              if ((clutter_alpha_get_mode (key_animator->alpha) != next_key->mode))
 
517
                clutter_alpha_set_mode (key_animator->alpha, next_key->mode);
 
518
            }
 
519
          else /* no relevant interval */
 
520
            {
 
521
              ClutterAnimatorKey *current_key = key_animator->current->data;
 
522
              clutter_interval_set_initial_value (key_animator->interval,
 
523
                                                  &current_key->value);
 
524
              clutter_interval_set_final_value (key_animator->interval,
 
525
                                                &current_key->value);
 
526
              break;
 
527
            }
 
528
        }
 
529
    }
 
530
  else if (progress < key_animator->start)
 
531
    {
 
532
      while (progress < key_animator->start)
 
533
        {
 
534
          ClutterAnimatorKey *initial_key, *next_key;
 
535
          GList *initial;
 
536
          GList *old = key_animator->current;
 
537
 
 
538
          initial = list_find_custom_reverse (key_animator->current->prev,
 
539
                                              key,
 
540
                                              sort_actor_prop_func);
 
541
 
 
542
          if (initial)
 
543
            {
 
544
              initial_key = initial->data;
 
545
 
 
546
              clutter_interval_set_initial_value (key_animator->interval,
 
547
                                                  &initial_key->value);
 
548
              key_animator->current = initial;
 
549
              key_animator->end = key_animator->start;
 
550
              key_animator->start = initial_key->progress;
 
551
 
 
552
              if (old)
 
553
                {
 
554
                  next_key = old->data;
 
555
 
 
556
                  key_animator->end = next_key->progress;
 
557
                }
 
558
              else
 
559
                {
 
560
                  next_key = initial_key;
 
561
 
 
562
                  key_animator->end = 1.0;
 
563
                }
 
564
 
 
565
              clutter_interval_set_final_value (key_animator->interval,
 
566
                                                &next_key->value);
 
567
              if ((clutter_alpha_get_mode (key_animator->alpha) != next_key->mode))
 
568
                clutter_alpha_set_mode (key_animator->alpha, next_key->mode);
 
569
            }
 
570
          else
 
571
            break;
 
572
        }
 
573
    }
 
574
}
 
575
 
 
576
/* XXX - this might be useful as an internal function exposed somewhere */
 
577
static gdouble
 
578
cubic_interpolation (const gdouble dx,
 
579
                     const gdouble prev,
 
580
                     const gdouble j,
 
581
                     const gdouble next,
 
582
                     const gdouble nextnext)
 
583
{
 
584
  return (((( - prev + 3 * j - 3 * next + nextnext ) * dx +
 
585
            ( 2 * prev - 5 * j + 4 * next - nextnext ) ) * dx +
 
586
            ( - prev + next ) ) * dx + (j + j) ) / 2.0;
 
587
}
 
588
 
 
589
/* try to get a floating point key value from a key for a property,
 
590
 * failing use the closest key in that direction or the starting point.
 
591
 */
 
592
static gfloat
 
593
list_try_get_rel (GList *list,
 
594
                  gint   count)
 
595
{
 
596
  ClutterAnimatorKey *key;
 
597
  GList *iter = list;
 
598
  GList *best = list;
 
599
 
 
600
  if (count > 0)
 
601
    {
 
602
      while (count -- && iter != NULL)
 
603
        {
 
604
          iter = g_list_find_custom (iter->next, list->data,
 
605
                                     sort_actor_prop_func);
 
606
          if (iter != NULL)
 
607
            best = iter;
 
608
        }
 
609
    }
 
610
  else
 
611
    {
 
612
      while (count ++ < 0 && iter != NULL)
 
613
        {
 
614
          iter = list_find_custom_reverse (iter->prev, list->data,
 
615
                                           sort_actor_prop_func);
 
616
          if (iter != NULL)
 
617
            best = iter;
 
618
        }
 
619
    }
 
620
 
 
621
  if (best != NULL && best->data != NULL)
 
622
    {
 
623
      key = best->data;
 
624
 
 
625
      return g_value_get_float (&(key->value));
 
626
    }
 
627
 
 
628
  return 0;
 
629
}
 
630
 
 
631
static void
 
632
animation_animator_new_frame (ClutterTimeline  *timeline,
 
633
                              gint              msecs,
 
634
                              ClutterAnimator  *animator)
 
635
{
 
636
  gdouble progress;
 
637
  GHashTableIter iter;
 
638
  gpointer key, value;
 
639
 
 
640
  progress  = 1.0 * msecs / clutter_timeline_get_duration (timeline);
 
641
 
 
642
  /* for each property that is managed figure out the GValue to set,
 
643
   * avoid creating new ClutterInterval's for each interval crossed
 
644
   */
 
645
  g_hash_table_iter_init (&iter, animator->priv->properties);
 
646
 
 
647
  key = value = NULL;
 
648
  while (g_hash_table_iter_next (&iter, &key, &value))
 
649
    {
 
650
      PropObjectKey      *prop_actor_key = key;
 
651
      KeyAnimator        *key_animator   = value;
 
652
      ClutterAnimatorKey *start_key;
 
653
      gdouble             sub_progress;
 
654
 
 
655
      animation_animator_ensure_animator (animator, key_animator,
 
656
                                          key,
 
657
                                          progress);
 
658
      start_key = key_animator->current->data;
 
659
 
 
660
      sub_progress = (progress - key_animator->start)
 
661
                   / (key_animator->end - key_animator->start);
 
662
 
 
663
      /* do not change values if we're not active yet (delay) */
 
664
      if (sub_progress >= 0.0 && sub_progress <= 1.0)
 
665
        {
 
666
          GValue tmp_value = { 0, };
 
667
          GType int_type;
 
668
 
 
669
          g_value_init (&tmp_value, G_VALUE_TYPE (&start_key->value));
 
670
 
 
671
          clutter_timeline_advance (animator->priv->slave_timeline,
 
672
                                    sub_progress * 10000);
 
673
 
 
674
          sub_progress = clutter_alpha_get_alpha (key_animator->alpha);
 
675
          int_type = clutter_interval_get_value_type (key_animator->interval);
 
676
 
 
677
          if (key_animator->interpolation == CLUTTER_INTERPOLATION_CUBIC &&
 
678
              int_type == G_TYPE_FLOAT)
 
679
            {
 
680
              gdouble prev, current, next, nextnext;
 
681
              gdouble res;
 
682
 
 
683
              if ((key_animator->ease_in == FALSE ||
 
684
                  (key_animator->ease_in &&
 
685
                   list_find_custom_reverse (key_animator->current->prev,
 
686
                                             key_animator->current->data,
 
687
                                             sort_actor_prop_func))))
 
688
                {
 
689
                  current = g_value_get_float (&start_key->value);
 
690
                  prev = list_try_get_rel (key_animator->current, -1);
 
691
                }
 
692
              else
 
693
                {
 
694
                  /* interpolated and easing in */
 
695
                  clutter_interval_get_initial_value (key_animator->interval,
 
696
                                                      &tmp_value);
 
697
                  prev = current = g_value_get_float (&tmp_value);
 
698
                }
 
699
 
 
700
               next = list_try_get_rel (key_animator->current, 1);
 
701
               nextnext = list_try_get_rel (key_animator->current, 2);
 
702
               res = cubic_interpolation (sub_progress, prev, current, next,
 
703
                                          nextnext);
 
704
 
 
705
               g_value_set_float (&tmp_value, res);
 
706
            }
 
707
          else
 
708
            clutter_interval_compute_value (key_animator->interval,
 
709
                                            sub_progress,
 
710
                                            &tmp_value);
 
711
 
 
712
          g_object_set_property (prop_actor_key->object,
 
713
                                 prop_actor_key->property_name,
 
714
                                 &tmp_value);
 
715
 
 
716
          g_value_unset (&tmp_value);
 
717
        }
 
718
    }
 
719
}
 
720
 
 
721
static void
 
722
animation_animator_started (ClutterTimeline *timeline,
 
723
                            ClutterAnimator *animator)
 
724
{
 
725
  GList *k;
 
726
 
 
727
  /* Ensure that animators exist for all involved properties */
 
728
  for (k = animator->priv->score; k != NULL; k = k->next)
 
729
    {
 
730
      ClutterAnimatorKey *key = k->data;
 
731
      KeyAnimator        *key_animator;
 
732
      PropObjectKey      *prop_actor_key;
 
733
 
 
734
      prop_actor_key = prop_actor_key_new (key->object, key->property_name);
 
735
      key_animator = g_hash_table_lookup (animator->priv->properties,
 
736
                                          prop_actor_key);
 
737
      if (key_animator)
 
738
        {
 
739
          prop_actor_key_free (prop_actor_key);
 
740
        }
 
741
      else
 
742
        {
 
743
          GObjectClass *klass = G_OBJECT_GET_CLASS (key->object);
 
744
          GParamSpec *pspec;
 
745
 
 
746
          pspec = g_object_class_find_property (klass, key->property_name);
 
747
 
 
748
          key_animator = key_animator_new (animator, prop_actor_key,
 
749
                                           G_PARAM_SPEC_VALUE_TYPE (pspec));
 
750
          g_hash_table_insert (animator->priv->properties,
 
751
                               prop_actor_key,
 
752
                               key_animator);
 
753
        }
 
754
    }
 
755
 
 
756
  /* initialize animator with initial list pointers */
 
757
  {
 
758
    GHashTableIter iter;
 
759
    gpointer key, value;
 
760
 
 
761
    g_hash_table_iter_init (&iter, animator->priv->properties);
 
762
    while (g_hash_table_iter_next (&iter, &key, &value))
 
763
      {
 
764
        KeyAnimator *key_animator = value;
 
765
        ClutterAnimatorKey *initial_key, *next_key;
 
766
        GList *initial;
 
767
        GList *next;
 
768
 
 
769
        initial = g_list_find_custom (animator->priv->score,
 
770
                                      key,
 
771
                                      sort_actor_prop_func);
 
772
        g_assert (initial != NULL);
 
773
        initial_key = initial->data;
 
774
        clutter_interval_set_initial_value (key_animator->interval,
 
775
                                            &initial_key->value);
 
776
 
 
777
        key_animator->current       = initial;
 
778
        key_animator->start         = initial_key->progress;
 
779
        key_animator->ease_in       = initial_key->ease_in;
 
780
        key_animator->interpolation = initial_key->interpolation;
 
781
 
 
782
        if (key_animator->ease_in)
 
783
          {
 
784
            GValue tmp_value = { 0, };
 
785
            GType int_type;
 
786
 
 
787
            int_type = clutter_interval_get_value_type (key_animator->interval);
 
788
            g_value_init (&tmp_value, int_type);
 
789
 
 
790
            g_object_get_property (initial_key->object,
 
791
                                   initial_key->property_name,
 
792
                                   &tmp_value);
 
793
 
 
794
            clutter_interval_set_initial_value (key_animator->interval,
 
795
                                                &tmp_value);
 
796
 
 
797
            g_value_unset (&tmp_value);
 
798
          }
 
799
 
 
800
        next = g_list_find_custom (initial->next, key, sort_actor_prop_func);
 
801
        if (next)
 
802
          {
 
803
            next_key = next->data;
 
804
            key_animator->end = next_key->progress;
 
805
          }
 
806
        else
 
807
          {
 
808
            next_key = initial_key;
 
809
            key_animator->end = 1.0;
 
810
          }
 
811
 
 
812
        clutter_interval_set_final_value (key_animator->interval,
 
813
                                          &next_key->value);
 
814
        if ((clutter_alpha_get_mode (key_animator->alpha) != next_key->mode))
 
815
          clutter_alpha_set_mode (key_animator->alpha, next_key->mode);
 
816
      }
 
817
  }
 
818
}
 
819
 
 
820
/**
 
821
 * clutter_animator_compute_value:
 
822
 * @animator: a #ClutterAnimator
 
823
 * @object: a #GObject
 
824
 * @property_name: the name of the property on object to check
 
825
 * @progress: a value between 0.0 and 1.0
 
826
 * @value: an initialized value to store the computed result
 
827
 *
 
828
 * Compute the value for a managed property at a given progress.
 
829
 *
 
830
 * If the property is an ease-in property, the current value of the property
 
831
 * on the object will be used as the starting point for computation.
 
832
 *
 
833
 * Return value: %TRUE if the computation yields has a value, otherwise (when
 
834
 *   an error occurs or the progress is before any of the keys) %FALSE is
 
835
 *   returned and the #GValue is left untouched
 
836
 *
 
837
 * Since: 1.2
 
838
 */
 
839
gboolean
 
840
clutter_animator_compute_value (ClutterAnimator *animator,
 
841
                                GObject         *object,
 
842
                                const gchar     *property_name,
 
843
                                gdouble          progress,
 
844
                                GValue          *value)
 
845
{
 
846
  ClutterAnimatorKey   key;
 
847
  ClutterAnimatorKey  *previous;
 
848
  ClutterAnimatorKey  *next = NULL;
 
849
  GParamSpec          *pspec;
 
850
  GList               *initial_l;
 
851
  GList               *previous_l;
 
852
  GList               *next_l;
 
853
  gboolean             ease_in;
 
854
  ClutterInterpolation interpolation;
 
855
 
 
856
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator), FALSE);
 
857
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
 
858
  g_return_val_if_fail (property_name, FALSE);
 
859
  g_return_val_if_fail (value, FALSE);
 
860
  
 
861
  ease_in = clutter_animator_property_get_ease_in (animator, object,
 
862
                                                   property_name);
 
863
  interpolation = clutter_animator_property_get_interpolation (animator,
 
864
                                                   object, property_name);
 
865
 
 
866
  property_name = g_intern_string (property_name);
 
867
 
 
868
  pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
 
869
                                        property_name);
 
870
 
 
871
  key.object        = object;
 
872
  key.property_name = property_name;
 
873
 
 
874
  initial_l = g_list_find_custom (animator->priv->score, &key,
 
875
                                  sort_actor_prop_func);
 
876
  if (!initial_l)
 
877
    return FALSE;
 
878
 
 
879
  /* first find the interval we belong in, that is the first interval
 
880
   * existing between keys
 
881
   */
 
882
 
 
883
  for (previous_l = initial_l, next_l = previous_l->next ;
 
884
       previous_l->next ;
 
885
       previous_l = previous_l->next, next_l = previous_l->next)
 
886
    {
 
887
       previous = previous_l->data;
 
888
       if (next_l)
 
889
         {
 
890
           next = next_l->data;
 
891
           if (next->object != object ||
 
892
               next->property_name != property_name)
 
893
             {
 
894
               next_l = NULL;
 
895
               next = NULL;
 
896
             }
 
897
         }
 
898
       else
 
899
         {
 
900
           next = NULL;
 
901
         }
 
902
 
 
903
       if (progress < previous->progress)
 
904
         {
 
905
            /* we are before the defined values */
 
906
 
 
907
            /* value has not been set */
 
908
            return FALSE;
 
909
         }
 
910
 
 
911
       if (!next && previous->progress <= progress)
 
912
         {
 
913
            /* we only had one key for this object/property */
 
914
            /* and we are past it, that is our value */
 
915
            g_value_copy (&previous->value, value);
 
916
            return TRUE;
 
917
         }
 
918
       if (next->progress >= progress)
 
919
         {
 
920
            ClutterInterval *interval;
 
921
            ClutterAlpha    *alpha;
 
922
 
 
923
            gdouble sub_progress = (progress - previous->progress)
 
924
                                    / (next->progress - previous->progress);
 
925
            /* this should be our interval */
 
926
            interval = g_object_new (CLUTTER_TYPE_INTERVAL,
 
927
                                     "value-type", pspec->value_type,
 
928
                                     NULL);
 
929
 
 
930
            if (ease_in && previous_l == initial_l)
 
931
              {
 
932
                GValue tmp_value = {0, };
 
933
                g_value_init (&tmp_value, pspec->value_type);
 
934
                g_object_get_property (object, property_name, &tmp_value);
 
935
                clutter_interval_set_initial_value (interval, &tmp_value);
 
936
                g_value_unset (&tmp_value);
 
937
              }
 
938
            else
 
939
              {
 
940
                clutter_interval_set_initial_value (interval, &previous->value);
 
941
              }
 
942
            clutter_interval_set_final_value (interval, &next->value);
 
943
 
 
944
            alpha = clutter_alpha_new ();
 
945
            clutter_alpha_set_timeline (alpha,
 
946
                                        animator->priv->slave_timeline);
 
947
            clutter_alpha_set_mode (alpha, next->mode);
 
948
 
 
949
            clutter_timeline_advance (animator->priv->slave_timeline,
 
950
                                      sub_progress * 10000);
 
951
            sub_progress = clutter_alpha_get_alpha (alpha);
 
952
 
 
953
            if (interpolation == CLUTTER_INTERPOLATION_CUBIC &&
 
954
                pspec->value_type == G_TYPE_FLOAT)
 
955
              {
 
956
                gdouble prev, current, nextv, nextnext;
 
957
                gdouble res;
 
958
 
 
959
                if ((ease_in == FALSE ||
 
960
                    (ease_in &&
 
961
                     list_find_custom_reverse (previous_l->prev,
 
962
                                               previous_l->data,
 
963
                                               sort_actor_prop_func))))
 
964
                  {
 
965
                    current = g_value_get_float (&previous->value);
 
966
                    prev = list_try_get_rel (previous_l, -1);
 
967
                  }
 
968
                else
 
969
                  {
 
970
                    /* interpolated and easing in */
 
971
                    GValue tmp_value = {0, };
 
972
                    g_value_init (&tmp_value, pspec->value_type);
 
973
                    clutter_interval_get_initial_value (interval,
 
974
                                                        &tmp_value);
 
975
                    prev = current = g_value_get_float (&tmp_value);
 
976
                    g_value_unset (&tmp_value);
 
977
                  }
 
978
 
 
979
                 nextv = list_try_get_rel (previous_l, 1);
 
980
                 nextnext = list_try_get_rel (previous_l, 2);
 
981
                 res = cubic_interpolation (sub_progress, prev, current, nextv,
 
982
                                            nextnext);
 
983
                 g_value_set_float (value, res);
 
984
              }
 
985
            else
 
986
              clutter_interval_compute_value (interval,
 
987
                                              sub_progress,
 
988
                                              value);
 
989
 
 
990
            g_object_ref_sink (interval);
 
991
            g_object_unref (interval);
 
992
            g_object_ref_sink (alpha);
 
993
            g_object_unref (alpha);
 
994
 
 
995
            return TRUE;
 
996
         }
 
997
 
 
998
    }
 
999
 
 
1000
  if (!next)
 
1001
    return FALSE;
 
1002
 
 
1003
  /* We're at, or past the end, use the last value */
 
1004
  g_value_copy (&next->value, value);
 
1005
 
 
1006
  return TRUE;
 
1007
}
 
1008
 
 
1009
 
 
1010
/**
 
1011
 * clutter_animator_set_timeline:
 
1012
 * @animator: a #ClutterAnimator
 
1013
 * @timeline: a #ClutterTimeline
 
1014
 *
 
1015
 * Sets an external timeline that will be used for driving the animation
 
1016
 *
 
1017
 * Since: 1.2
 
1018
 */
 
1019
void
 
1020
clutter_animator_set_timeline (ClutterAnimator *animator,
 
1021
                               ClutterTimeline *timeline)
 
1022
{
 
1023
  ClutterAnimatorPrivate *priv;
 
1024
 
 
1025
  g_return_if_fail (CLUTTER_IS_ANIMATOR (animator));
 
1026
 
 
1027
  priv = animator->priv;
 
1028
 
 
1029
  if (priv->timeline != NULL)
 
1030
    {
 
1031
      g_signal_handlers_disconnect_by_func (priv->timeline,
 
1032
                                            animation_animator_new_frame,
 
1033
                                            animator);
 
1034
      g_signal_handlers_disconnect_by_func (priv->timeline,
 
1035
                                            animation_animator_started,
 
1036
                                            animator);
 
1037
      g_object_unref (priv->timeline);
 
1038
    }
 
1039
 
 
1040
  priv->timeline = timeline;
 
1041
  if (timeline != NULL)
 
1042
    {
 
1043
      g_object_ref_sink (priv->timeline);
 
1044
 
 
1045
      g_signal_connect (priv->timeline, "new-frame",
 
1046
                        G_CALLBACK (animation_animator_new_frame),
 
1047
                        animator);
 
1048
      g_signal_connect (priv->timeline, "started",
 
1049
                        G_CALLBACK (animation_animator_started),
 
1050
                        animator);
 
1051
    }
 
1052
}
 
1053
 
 
1054
/**
 
1055
 * clutter_animator_get_timeline:
 
1056
 * @animator: a #ClutterAnimator
 
1057
 *
 
1058
 * Get the timeline hooked up for driving the #ClutterAnimator
 
1059
 *
 
1060
 * Return value: (transfer none): the #ClutterTimeline that drives the animator
 
1061
 *
 
1062
 * Since: 1.2
 
1063
 */
 
1064
ClutterTimeline *
 
1065
clutter_animator_get_timeline (ClutterAnimator *animator)
 
1066
{
 
1067
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator), NULL);
 
1068
  return animator->priv->timeline;
 
1069
}
 
1070
 
 
1071
/**
 
1072
 * clutter_animator_start:
 
1073
 * @animator: a #ClutterAnimator
 
1074
 *
 
1075
 * Start the ClutterAnimator, this is a thin wrapper that rewinds
 
1076
 * and starts the animators current timeline.
 
1077
 *
 
1078
 * Return value: the #ClutterTimeline that drives the animator.
 
1079
 *
 
1080
 * Since: 1.2
 
1081
 */
 
1082
ClutterTimeline *
 
1083
clutter_animator_start (ClutterAnimator *animator)
 
1084
{
 
1085
  ClutterAnimatorPrivate *priv;
 
1086
 
 
1087
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator), NULL);
 
1088
 
 
1089
  priv = animator->priv;
 
1090
 
 
1091
  clutter_timeline_rewind (priv->timeline);
 
1092
  clutter_timeline_start (priv->timeline);
 
1093
 
 
1094
  return priv->timeline;
 
1095
}
 
1096
 
 
1097
/**
 
1098
 * clutter_animator_set_duration:
 
1099
 * @animator: a #ClutterAnimator
 
1100
 * @duration: milliseconds a run of the animator should last.
 
1101
 *
 
1102
 * Runs the timeline of the #ClutterAnimator with a duration in msecs
 
1103
 * as specified.
 
1104
 *
 
1105
 * Since: 1.2
 
1106
 */
 
1107
void
 
1108
clutter_animator_set_duration (ClutterAnimator *animator,
 
1109
                               guint            duration)
 
1110
{
 
1111
  g_return_if_fail (CLUTTER_IS_ANIMATOR (animator));
 
1112
 
 
1113
  clutter_timeline_set_duration (animator->priv->timeline, duration);
 
1114
}
 
1115
 
 
1116
/**
 
1117
 * clutter_animator_get_duration:
 
1118
 * @animator: a #ClutterAnimator
 
1119
 *
 
1120
 * Retrieves the current duration of an animator
 
1121
 *
 
1122
 * Return value: the duration of the animation, in milliseconds
 
1123
 *
 
1124
 * Since: 1.2
 
1125
 */
 
1126
guint
 
1127
clutter_animator_get_duration  (ClutterAnimator *animator)
 
1128
{
 
1129
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator), 0);
 
1130
 
 
1131
  return clutter_timeline_get_duration (animator->priv->timeline);
 
1132
}
 
1133
 
 
1134
/**
 
1135
 * clutter_animator_set:
 
1136
 * @animator: a #ClutterAnimator
 
1137
 * @first_object: a #GObject
 
1138
 * @first_property_name: the property to specify a key for
 
1139
 * @first_mode: the id of the alpha function to use
 
1140
 * @first_progress: at which stage of the animation this value applies; the
 
1141
 *   range is a normalized floating point value between 0 and 1
 
1142
 * @VarArgs: the value first_property_name should have for first_object
 
1143
 *   at first_progress, followed by more (object, property_name, mode,
 
1144
 *   progress, value) tuples, followed by %NULL
 
1145
 *
 
1146
 * Adds multiple keys to a #ClutterAnimator, specifying the value a given
 
1147
 * property should have at a given progress of the animation. The mode
 
1148
 * specified is the mode used when going to this key from the previous key of
 
1149
 * the @property_name
 
1150
 *
 
1151
 * If a given (object, property, progress) tuple already exist the mode and
 
1152
 * value will be replaced with the new values.
 
1153
 *
 
1154
 * Since: 1.2
 
1155
 */
 
1156
void
 
1157
clutter_animator_set (ClutterAnimator *animator,
 
1158
                      gpointer         first_object,
 
1159
                      const gchar     *first_property_name,
 
1160
                      guint            first_mode,
 
1161
                      gdouble          first_progress,
 
1162
                      ...)
 
1163
{
 
1164
  GObject      *object;
 
1165
  const gchar  *property_name;
 
1166
  guint         mode;
 
1167
  gdouble       progress;
 
1168
  va_list       args;
 
1169
 
 
1170
  g_return_if_fail (CLUTTER_IS_ANIMATOR (animator));
 
1171
 
 
1172
  object = first_object;
 
1173
  property_name = first_property_name;
 
1174
  mode = first_mode;
 
1175
  progress = first_progress;
 
1176
 
 
1177
  va_start (args, first_progress);
 
1178
 
 
1179
  while (object != NULL)
 
1180
    {
 
1181
      GParamSpec *pspec;
 
1182
      GObjectClass *klass;
 
1183
      GValue value = { 0, };
 
1184
      gchar *error = NULL;
 
1185
 
 
1186
      g_return_if_fail (object);
 
1187
      g_return_if_fail (property_name);
 
1188
 
 
1189
      klass = G_OBJECT_GET_CLASS (object);
 
1190
      pspec = g_object_class_find_property (klass, property_name);
 
1191
 
 
1192
      if (!pspec)
 
1193
        {
 
1194
          g_warning ("Cannot bind property '%s': object of type '%s' "
 
1195
                     "do not have this property",
 
1196
                     property_name, G_OBJECT_TYPE_NAME (object));
 
1197
          break;
 
1198
        }
 
1199
 
 
1200
#if GLIB_CHECK_VERSION (2, 23, 2)
 
1201
      G_VALUE_COLLECT_INIT (&value, G_PARAM_SPEC_VALUE_TYPE (pspec),
 
1202
                            args, 0,
 
1203
                            &error);
 
1204
#else
 
1205
      g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
 
1206
      G_VALUE_COLLECT (&value, args, 0, &error);
 
1207
#endif /* GLIB_CHECK_VERSION (2, 23, 2) */
 
1208
 
 
1209
      if (error)
 
1210
        {
 
1211
          g_warning ("%s: %s", G_STRLOC, error);
 
1212
          g_free (error);
 
1213
          break;
 
1214
        }
 
1215
 
 
1216
      clutter_animator_set_key (animator,
 
1217
                                object,
 
1218
                                property_name,
 
1219
                                mode,
 
1220
                                progress,
 
1221
                                &value);
 
1222
 
 
1223
      object= va_arg (args, GObject *);
 
1224
      if (object)
 
1225
        {
 
1226
          property_name = va_arg (args, gchar*);
 
1227
          if (!property_name)
 
1228
           {
 
1229
             g_warning ("%s: expected a property name", G_STRLOC);
 
1230
             break;
 
1231
           }
 
1232
          mode = va_arg (args, guint);
 
1233
          progress = va_arg (args, gdouble);
 
1234
        }
 
1235
    }
 
1236
 
 
1237
  va_end (args);
 
1238
}
 
1239
 
 
1240
static inline void
 
1241
clutter_animator_set_key_internal (ClutterAnimator    *animator,
 
1242
                                   ClutterAnimatorKey *key)
 
1243
{
 
1244
  ClutterAnimatorPrivate *priv = animator->priv;
 
1245
  GList *old_item;
 
1246
 
 
1247
  old_item = g_list_find_custom (priv->score, key,
 
1248
                                 sort_actor_prop_progress_func);
 
1249
 
 
1250
  /* replace the key if we already have a similar one */
 
1251
  if (old_item != NULL)
 
1252
    {
 
1253
      ClutterAnimatorKey *old_key = old_item->data;
 
1254
 
 
1255
      clutter_animator_key_free (old_key);
 
1256
 
 
1257
      priv->score = g_list_remove (priv->score, old_key);
 
1258
    }
 
1259
 
 
1260
  priv->score = g_list_insert_sorted (priv->score, key,
 
1261
                                      sort_actor_prop_progress_func);
 
1262
}
 
1263
 
 
1264
/**
 
1265
 * clutter_animator_set_key:
 
1266
 * @animator: a #ClutterAnimator
 
1267
 * @object: a #GObject
 
1268
 * @property_name: the property to specify a key for
 
1269
 * @mode: the id of the alpha function to use
 
1270
 * @progress: the normalized range at which stage of the animation this
 
1271
 *   value applies
 
1272
 * @value: the value property_name should have at progress.
 
1273
 *
 
1274
 * Sets a single key in the #ClutterAnimator for the @property_name of
 
1275
 * @object at @progress.
 
1276
 *
 
1277
 * See also: clutter_animator_set()
 
1278
 *
 
1279
 * Return value: (transfer none): The animator instance
 
1280
 *
 
1281
 * Since: 1.2
 
1282
 */
 
1283
ClutterAnimator *
 
1284
clutter_animator_set_key (ClutterAnimator *animator,
 
1285
                          GObject         *object,
 
1286
                          const gchar     *property_name,
 
1287
                          guint            mode,
 
1288
                          gdouble          progress,
 
1289
                          const GValue    *value)
 
1290
{
 
1291
  ClutterAnimatorKey *animator_key;
 
1292
 
 
1293
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator), NULL);
 
1294
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
 
1295
  g_return_val_if_fail (property_name, NULL);
 
1296
  g_return_val_if_fail (value, NULL);
 
1297
 
 
1298
  property_name = g_intern_string (property_name);
 
1299
 
 
1300
  animator_key = clutter_animator_key_new (animator,
 
1301
                                           object, property_name,
 
1302
                                           progress,
 
1303
                                           mode);
 
1304
 
 
1305
  g_value_init (&animator_key->value, G_VALUE_TYPE (value));
 
1306
  g_value_copy (value, &animator_key->value);
 
1307
 
 
1308
  clutter_animator_set_key_internal (animator, animator_key);
 
1309
 
 
1310
  return animator;
 
1311
}
 
1312
 
 
1313
/**
 
1314
 * clutter_animator_get_keys:
 
1315
 * @animator: a #ClutterAnimator instance
 
1316
 * @object: (allow-none): a #GObject to search for, or %NULL for all objects
 
1317
 * @property_name: (allow-none): a specific property name to query for,
 
1318
 *   or %NULL for all properties
 
1319
 * @progress: a specific progress to search for, or a negative value for all
 
1320
 *   progresses
 
1321
 *
 
1322
 * Returns a list of pointers to opaque structures with accessor functions
 
1323
 * that describe the keys added to an animator.
 
1324
 *
 
1325
 * Return value: (transfer container) (element-type ClutterAnimatorKey): a
 
1326
 *   list of #ClutterAnimatorKey<!-- -->s; the contents of the list are owned
 
1327
 *   by the #ClutterAnimator, but you should free the returned list when done,
 
1328
 *   using g_list_free()
 
1329
 *
 
1330
 * Since: 1.2
 
1331
 */
 
1332
GList *
 
1333
clutter_animator_get_keys (ClutterAnimator *animator,
 
1334
                           GObject         *object,
 
1335
                           const gchar     *property_name,
 
1336
                           gdouble          progress)
 
1337
{
 
1338
  GList *keys = NULL;
 
1339
  GList *k;
 
1340
 
 
1341
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator), NULL);
 
1342
  g_return_val_if_fail (object == NULL || G_IS_OBJECT (object), NULL);
 
1343
 
 
1344
  property_name = g_intern_string (property_name);
 
1345
 
 
1346
  for (k = animator->priv->score; k; k = k->next)
 
1347
    {
 
1348
      ClutterAnimatorKey *key = k->data;
 
1349
 
 
1350
      if ((object == NULL || (object == key->object)) &&
 
1351
          (property_name == NULL || ((property_name == key->property_name))) &&
 
1352
          (progress < 0  || fabs (progress - key->progress) < PROGRESS_EPSILON))
 
1353
        {
 
1354
          keys = g_list_prepend (keys, key);
 
1355
        }
 
1356
    }
 
1357
 
 
1358
  return g_list_reverse (keys);
 
1359
}
 
1360
 
 
1361
/**
 
1362
 * clutter_animator_remove_key:
 
1363
 * @animator: a #ClutterAnimator
 
1364
 * @object: (allow-none): a #GObject to search for, or %NULL for all
 
1365
 * @property_name: (allow-none): a specific property name to query for,
 
1366
 *   or %NULL for all
 
1367
 * @progress: a specific progress to search for or a negative value
 
1368
 *   for all
 
1369
 *
 
1370
 * Removes all keys matching the conditions specificed in the arguments.
 
1371
 *
 
1372
 * Since: 1.2
 
1373
 */
 
1374
void
 
1375
clutter_animator_remove_key (ClutterAnimator *animator,
 
1376
                             GObject         *object,
 
1377
                             const gchar     *property_name,
 
1378
                             gdouble          progress)
 
1379
{
 
1380
  ClutterAnimatorPrivate *priv;
 
1381
  GList *k;
 
1382
 
 
1383
  g_return_if_fail (CLUTTER_IS_ANIMATOR (animator));
 
1384
  g_return_if_fail (object == NULL || G_IS_OBJECT (object));
 
1385
 
 
1386
  property_name = g_intern_string (property_name);
 
1387
 
 
1388
  priv = animator->priv;
 
1389
 
 
1390
  for (k = priv->score; k != NULL; k = k->next)
 
1391
    {
 
1392
      ClutterAnimatorKey *key = k->data;
 
1393
 
 
1394
      if ((object == NULL        || (object == key->object)) &&
 
1395
          (property_name == NULL || ((property_name == key->property_name))) &&
 
1396
          (progress < 0  || fabs (progress - key->progress) < PROGRESS_EPSILON)
 
1397
         )
 
1398
        {
 
1399
          key->is_inert = TRUE;
 
1400
 
 
1401
          clutter_animator_key_free (key);
 
1402
 
 
1403
          /* FIXME: non performant since we reiterate the list many times */
 
1404
          k = priv->score = g_list_remove (priv->score, key);
 
1405
        }
 
1406
    }
 
1407
 
 
1408
  if (object)
 
1409
    {
 
1410
      GHashTableIter iter;
 
1411
      gpointer key, value;
 
1412
 
 
1413
again:
 
1414
      g_hash_table_iter_init (&iter, priv->properties);
 
1415
      while (g_hash_table_iter_next (&iter, &key, &value))
 
1416
        {
 
1417
          PropObjectKey *prop_actor_key = key;
 
1418
          if (prop_actor_key->object == object)
 
1419
            {
 
1420
              g_hash_table_remove (priv->properties, key);
 
1421
              goto again;
 
1422
            }
 
1423
        }
 
1424
    }
 
1425
}
 
1426
 
 
1427
 
 
1428
 
 
1429
 
 
1430
typedef struct _ParseClosure {
 
1431
  ClutterAnimator *animator;
 
1432
  ClutterScript *script;
 
1433
 
 
1434
  GValue *value;
 
1435
 
 
1436
  gboolean result;
 
1437
} ParseClosure;
 
1438
 
 
1439
static ClutterInterpolation
 
1440
resolve_interpolation (JsonNode *node)
 
1441
{
 
1442
  if ((JSON_NODE_TYPE (node) != JSON_NODE_VALUE))
 
1443
    return CLUTTER_INTERPOLATION_LINEAR;
 
1444
 
 
1445
  if (json_node_get_value_type (node) == G_TYPE_INT64)
 
1446
    {
 
1447
      return json_node_get_int (node);
 
1448
    }
 
1449
  else if (json_node_get_value_type (node) == G_TYPE_STRING)
 
1450
    {
 
1451
      const gchar *str = json_node_get_string (node);
 
1452
      gboolean res;
 
1453
      gint enum_value;
 
1454
 
 
1455
      res = clutter_script_enum_from_string (CLUTTER_TYPE_INTERPOLATION,
 
1456
                                             str,
 
1457
                                             &enum_value);
 
1458
      if (res)
 
1459
        return enum_value;
 
1460
    }
 
1461
 
 
1462
  return CLUTTER_INTERPOLATION_LINEAR;
 
1463
}
 
1464
 
 
1465
static void
 
1466
parse_animator_property (JsonArray *array,
 
1467
                         guint      index_,
 
1468
                         JsonNode  *element,
 
1469
                         gpointer   data)
 
1470
{
 
1471
  ParseClosure *clos = data;
 
1472
  JsonObject *object;
 
1473
  JsonArray *keys;
 
1474
  GObject *gobject;
 
1475
  const gchar *id, *pname;
 
1476
  GObjectClass *klass;
 
1477
  GParamSpec *pspec;
 
1478
  GSList *valid_keys = NULL;
 
1479
  GList *k;
 
1480
  ClutterInterpolation interpolation = CLUTTER_INTERPOLATION_LINEAR;
 
1481
  gboolean ease_in = FALSE;
 
1482
 
 
1483
  if (JSON_NODE_TYPE (element) != JSON_NODE_OBJECT)
 
1484
    {
 
1485
      g_warning ("The 'properties' member of a ClutterAnimator description "
 
1486
                 "should be an array of objects, but the element %d of the "
 
1487
                 "array is of type '%s'. The element will be ignored.",
 
1488
                 index_,
 
1489
                 json_node_type_name (element));
 
1490
      return;
 
1491
    }
 
1492
 
 
1493
  object = json_node_get_object (element);
 
1494
 
 
1495
  if (!json_object_has_member (object, "object") ||
 
1496
      !json_object_has_member (object, "name") ||
 
1497
      !json_object_has_member (object, "keys"))
 
1498
    {
 
1499
      g_warning ("The property description at index %d is missing one of "
 
1500
                 "the mandatory fields: object, name and keys",
 
1501
                 index_);
 
1502
      return;
 
1503
    }
 
1504
 
 
1505
  id = json_object_get_string_member (object, "object");
 
1506
  gobject = clutter_script_get_object (clos->script, id);
 
1507
  if (gobject == NULL)
 
1508
    {
 
1509
      g_warning ("No object with id '%s' has been defined.", id);
 
1510
      return;
 
1511
    }
 
1512
 
 
1513
  pname = json_object_get_string_member (object, "name");
 
1514
  klass = G_OBJECT_GET_CLASS (gobject);
 
1515
  pspec = g_object_class_find_property (klass, pname);
 
1516
  if (pspec == NULL)
 
1517
    {
 
1518
      g_warning ("The object of type '%s' and name '%s' has no "
 
1519
                 "property named '%s'",
 
1520
                 G_OBJECT_TYPE_NAME (gobject),
 
1521
                 id,
 
1522
                 pname);
 
1523
      return;
 
1524
    }
 
1525
 
 
1526
  if (json_object_has_member (object, "ease-in"))
 
1527
    ease_in = json_object_get_boolean_member (object, "ease-in");
 
1528
 
 
1529
  if (json_object_has_member (object, "interpolation"))
 
1530
    {
 
1531
      JsonNode *node = json_object_get_member (object, "interpolation");
 
1532
 
 
1533
      interpolation = resolve_interpolation (node);
 
1534
    }
 
1535
 
 
1536
  keys = json_object_get_array_member (object, "keys");
 
1537
  if (keys == NULL)
 
1538
    {
 
1539
      g_warning ("The property description at index %d has an invalid "
 
1540
                 "key field of type '%s' when an array was expected.",
 
1541
                 index_,
 
1542
                 json_node_type_name (json_object_get_member (object, "keys")));
 
1543
      return;
 
1544
    }
 
1545
 
 
1546
  if (G_IS_VALUE (clos->value))
 
1547
    valid_keys = g_slist_reverse (g_value_get_pointer (clos->value));
 
1548
  else
 
1549
    g_value_init (clos->value, G_TYPE_POINTER);
 
1550
 
 
1551
  for (k = json_array_get_elements (keys);
 
1552
       k != NULL;
 
1553
       k = k->next)
 
1554
    {
 
1555
      JsonNode *node = k->data;
 
1556
      JsonArray *key = json_node_get_array (node);
 
1557
      ClutterAnimatorKey *animator_key;
 
1558
      gdouble progress;
 
1559
      gulong mode;
 
1560
      gboolean res;
 
1561
 
 
1562
      progress = json_array_get_double_element (key, 0);
 
1563
      mode = clutter_script_resolve_animation_mode (json_array_get_element (key, 1));
 
1564
 
 
1565
      animator_key = clutter_animator_key_new (clos->animator,
 
1566
                                               gobject,
 
1567
                                               pname,
 
1568
                                               progress,
 
1569
                                               mode);
 
1570
 
 
1571
      res = clutter_script_parse_node (clos->script,
 
1572
                                       &(animator_key->value),
 
1573
                                       pname,
 
1574
                                       json_array_get_element (key, 2),
 
1575
                                       pspec);
 
1576
      if (!res)
 
1577
        {
 
1578
          g_warning ("Unable to parse the key value for the "
 
1579
                     "property '%s' (progress: %.2f) at index %d",
 
1580
                     pname,
 
1581
                     progress,
 
1582
                     index_);
 
1583
          continue;
 
1584
        }
 
1585
 
 
1586
      animator_key->ease_in = ease_in;
 
1587
      animator_key->interpolation = interpolation;
 
1588
 
 
1589
      valid_keys = g_slist_prepend (valid_keys, animator_key);
 
1590
    }
 
1591
 
 
1592
  g_value_set_pointer (clos->value, g_slist_reverse (valid_keys));
 
1593
 
 
1594
  clos->result = TRUE;
 
1595
}
 
1596
 
 
1597
static gboolean
 
1598
clutter_animator_parse_custom_node (ClutterScriptable *scriptable,
 
1599
                                    ClutterScript     *script,
 
1600
                                    GValue            *value,
 
1601
                                    const gchar       *name,
 
1602
                                    JsonNode          *node)
 
1603
{
 
1604
  ParseClosure parse_closure;
 
1605
 
 
1606
  if (strcmp (name, "properties") != 0)
 
1607
    return FALSE;
 
1608
 
 
1609
  if (JSON_NODE_TYPE (node) != JSON_NODE_ARRAY)
 
1610
    return FALSE;
 
1611
 
 
1612
  parse_closure.animator = CLUTTER_ANIMATOR (scriptable);
 
1613
  parse_closure.script = script;
 
1614
  parse_closure.value = value;
 
1615
  parse_closure.result = FALSE;
 
1616
 
 
1617
  json_array_foreach_element (json_node_get_array (node),
 
1618
                              parse_animator_property,
 
1619
                              &parse_closure);
 
1620
 
 
1621
  /* we return TRUE if we had at least one key parsed */
 
1622
 
 
1623
  return parse_closure.result;
 
1624
}
 
1625
 
 
1626
static void
 
1627
clutter_animator_set_custom_property (ClutterScriptable *scriptable,
 
1628
                                      ClutterScript     *script,
 
1629
                                      const gchar       *name,
 
1630
                                      const GValue      *value)
 
1631
{
 
1632
  if (strcmp (name, "properties") == 0)
 
1633
    {
 
1634
      ClutterAnimator *animator = CLUTTER_ANIMATOR (scriptable);
 
1635
      GSList *keys = g_value_get_pointer (value);
 
1636
      GSList *k;
 
1637
 
 
1638
      for (k = keys; k != NULL; k = k->next)
 
1639
        clutter_animator_set_key_internal (animator, k->data);
 
1640
 
 
1641
      g_slist_free (keys);
 
1642
    }
 
1643
  else
 
1644
    g_object_set_property (G_OBJECT (scriptable), name, value);
 
1645
}
 
1646
 
 
1647
static void
 
1648
clutter_scriptable_init (ClutterScriptableIface *iface)
 
1649
{
 
1650
  iface->parse_custom_node = clutter_animator_parse_custom_node;
 
1651
  iface->set_custom_property = clutter_animator_set_custom_property;
 
1652
}
 
1653
 
 
1654
static void
 
1655
clutter_animator_set_property (GObject      *gobject,
 
1656
                               guint         prop_id,
 
1657
                               const GValue *value,
 
1658
                               GParamSpec   *pspec)
 
1659
{
 
1660
  ClutterAnimator *self = CLUTTER_ANIMATOR (gobject);
 
1661
 
 
1662
  switch (prop_id)
 
1663
    {
 
1664
    case PROP_DURATION:
 
1665
      clutter_animator_set_duration (self, g_value_get_uint (value));
 
1666
      break;
 
1667
 
 
1668
    case PROP_TIMELINE:
 
1669
      clutter_animator_set_timeline (self, g_value_get_object (value));
 
1670
      break;
 
1671
 
 
1672
    default:
 
1673
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
 
1674
      break;
 
1675
    }
 
1676
}
 
1677
 
 
1678
static void
 
1679
clutter_animator_get_property (GObject    *gobject,
 
1680
                               guint       prop_id,
 
1681
                               GValue     *value,
 
1682
                               GParamSpec *pspec)
 
1683
{
 
1684
  ClutterAnimatorPrivate *priv = CLUTTER_ANIMATOR (gobject)->priv;
 
1685
 
 
1686
  switch (prop_id)
 
1687
    {
 
1688
    case PROP_DURATION:
 
1689
      g_value_set_uint (value, clutter_timeline_get_duration (priv->timeline));
 
1690
      break;
 
1691
 
 
1692
    case PROP_TIMELINE:
 
1693
      g_value_set_object (value, priv->timeline);
 
1694
      break;
 
1695
 
 
1696
    default:
 
1697
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
 
1698
      break;
 
1699
    }
 
1700
}
 
1701
 
 
1702
static void
 
1703
clutter_animator_class_init (ClutterAnimatorClass *klass)
 
1704
{
 
1705
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
1706
  GParamSpec *pspec;
 
1707
 
 
1708
  g_type_class_add_private (klass, sizeof (ClutterAnimatorPrivate));
 
1709
 
 
1710
  gobject_class->set_property = clutter_animator_set_property;
 
1711
  gobject_class->get_property = clutter_animator_get_property;
 
1712
  gobject_class->finalize = clutter_animator_finalize;
 
1713
 
 
1714
  /**
 
1715
   * ClutterAnimator:duration:
 
1716
   *
 
1717
   * The duration of the #ClutterTimeline used by the #ClutterAnimator
 
1718
   * to drive the animation
 
1719
   *
 
1720
   * Since: 1.2
 
1721
   */
 
1722
  pspec = g_param_spec_uint ("duration",
 
1723
                             "Duration",
 
1724
                             "The duration of the animation",
 
1725
                             0, G_MAXUINT,
 
1726
                             2000,
 
1727
                             CLUTTER_PARAM_READWRITE);
 
1728
  g_object_class_install_property (gobject_class, PROP_DURATION, pspec);
 
1729
 
 
1730
  /**
 
1731
   * ClutterAnimator:timeline:
 
1732
   *
 
1733
   * The #ClutterTimeline used by the #ClutterAnimator to drive the
 
1734
   * animation
 
1735
   *
 
1736
   * Since: 1.2
 
1737
   */
 
1738
  pspec = g_param_spec_object ("timeline",
 
1739
                               "Timeline",
 
1740
                               "The timeline of the animation",
 
1741
                               CLUTTER_TYPE_TIMELINE,
 
1742
                               CLUTTER_PARAM_READWRITE);
 
1743
  g_object_class_install_property (gobject_class, PROP_TIMELINE, pspec);
 
1744
}
 
1745
 
 
1746
static void
 
1747
clutter_animator_init (ClutterAnimator *animator)
 
1748
{
 
1749
  ClutterAnimatorPrivate *priv;
 
1750
 
 
1751
  animator->priv = priv = CLUTTER_ANIMATOR_GET_PRIVATE (animator);
 
1752
 
 
1753
  priv->properties = g_hash_table_new_full (prop_actor_hash,
 
1754
                                            prop_actor_equal,
 
1755
                                            prop_actor_key_free,
 
1756
                                            key_animator_free);
 
1757
 
 
1758
  clutter_animator_set_timeline (animator, clutter_timeline_new (2000));
 
1759
 
 
1760
  priv->slave_timeline = clutter_timeline_new (10000);
 
1761
  g_object_ref_sink (priv->slave_timeline);
 
1762
}
 
1763
 
 
1764
 
 
1765
/**
 
1766
 * clutter_animator_property_get_ease_in:
 
1767
 * @animator: a #ClutterAnimatorKey
 
1768
 * @object: a #GObject
 
1769
 * @property_name: the name of a property on object
 
1770
 *
 
1771
 * Checks if a property value is to be eased into the animation.
 
1772
 *
 
1773
 * Return value: %TRUE if the property is eased in
 
1774
 *
 
1775
 * Since: 1.2
 
1776
 */
 
1777
gboolean
 
1778
clutter_animator_property_get_ease_in (ClutterAnimator *animator,
 
1779
                                       GObject         *object,
 
1780
                                       const gchar     *property_name)
 
1781
{
 
1782
  ClutterAnimatorKey  key, *initial_key;
 
1783
  GList              *initial;
 
1784
 
 
1785
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator), FALSE);
 
1786
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
 
1787
  g_return_val_if_fail (property_name, FALSE);
 
1788
 
 
1789
  key.object        = object;
 
1790
  key.property_name = g_intern_string (property_name);
 
1791
  initial = g_list_find_custom (animator->priv->score, &key,
 
1792
                                sort_actor_prop_func);
 
1793
  if (initial != NULL)
 
1794
    {
 
1795
      initial_key = initial->data;
 
1796
 
 
1797
      return initial_key->ease_in;
 
1798
    }
 
1799
 
 
1800
  return FALSE;
 
1801
}
 
1802
 
 
1803
/**
 
1804
 * clutter_animator_property_set_ease_in:
 
1805
 * @animator: a #ClutterAnimatorKey
 
1806
 * @object: a #GObject
 
1807
 * @property_name: the name of a property on object
 
1808
 * @ease_in: we are going to be easing in this property
 
1809
 *
 
1810
 * Sets whether a property value is to be eased into the animation.
 
1811
 *
 
1812
 * Since: 1.2
 
1813
 */
 
1814
void
 
1815
clutter_animator_property_set_ease_in (ClutterAnimator *animator,
 
1816
                                       GObject         *object,
 
1817
                                       const gchar     *property_name,
 
1818
                                       gboolean         ease_in)
 
1819
{
 
1820
  ClutterAnimatorKey  key, *initial_key;
 
1821
  GList              *initial;
 
1822
 
 
1823
  g_return_if_fail (CLUTTER_IS_ANIMATOR (animator));
 
1824
  g_return_if_fail (G_IS_OBJECT (object));
 
1825
  g_return_if_fail (property_name);
 
1826
 
 
1827
  key.object        = object;
 
1828
  key.property_name = g_intern_string (property_name);
 
1829
  initial = g_list_find_custom (animator->priv->score, &key,
 
1830
                                sort_actor_prop_func);
 
1831
  if (initial)
 
1832
    {
 
1833
      initial_key = initial->data;
 
1834
      initial_key->ease_in = ease_in;
 
1835
    }
 
1836
  else
 
1837
    g_warning ("The animator has no object of type '%s' with a "
 
1838
               "property named '%s'",
 
1839
               G_OBJECT_TYPE_NAME (object),
 
1840
               property_name);
 
1841
}
 
1842
 
 
1843
 
 
1844
/**
 
1845
 * clutter_animator_property_get_interpolation:
 
1846
 * @animator: a #ClutterAnimatorKey
 
1847
 * @object: a #GObject
 
1848
 * @property_name: the name of a property on object
 
1849
 *
 
1850
 * Get the interpolation used by animator for a property on a particular
 
1851
 * object.
 
1852
 *
 
1853
 * Returns: a ClutterInterpolation value.
 
1854
 * Since: 1.2
 
1855
 */
 
1856
ClutterInterpolation
 
1857
clutter_animator_property_get_interpolation (ClutterAnimator *animator,
 
1858
                                             GObject         *object,
 
1859
                                             const gchar     *property_name)
 
1860
{
 
1861
  GList              *initial;
 
1862
  ClutterAnimatorKey  key, *initial_key;
 
1863
 
 
1864
  g_return_val_if_fail (CLUTTER_IS_ANIMATOR (animator),
 
1865
                        CLUTTER_INTERPOLATION_LINEAR);
 
1866
  g_return_val_if_fail (G_IS_OBJECT (object),
 
1867
                        CLUTTER_INTERPOLATION_LINEAR);
 
1868
  g_return_val_if_fail (property_name,
 
1869
                        CLUTTER_INTERPOLATION_LINEAR);
 
1870
 
 
1871
  key.object        = object;
 
1872
  key.property_name = g_intern_string (property_name);
 
1873
  initial = g_list_find_custom (animator->priv->score, &key,
 
1874
                                sort_actor_prop_func);
 
1875
  if (initial)
 
1876
    {
 
1877
      initial_key = initial->data;
 
1878
 
 
1879
      return initial_key->interpolation;
 
1880
    }
 
1881
 
 
1882
  return CLUTTER_INTERPOLATION_LINEAR;
 
1883
}
 
1884
 
 
1885
/**
 
1886
 * clutter_animator_property_set_interpolation:
 
1887
 * @animator: a #ClutterAnimatorKey
 
1888
 * @object: a #GObject
 
1889
 * @property_name: the name of a property on object
 
1890
 * @interpolation: the #ClutterInterpolation to use
 
1891
 *
 
1892
 * Set the interpolation method to use, %CLUTTER_INTERPOLATION_LINEAR causes
 
1893
 * the values to linearly change between the values, and
 
1894
 * %CLUTTER_INTERPOLATION_CUBIC causes the values to smoothly change between
 
1895
 * the values.
 
1896
 *
 
1897
 * Since: 1.2
 
1898
 */
 
1899
void
 
1900
clutter_animator_property_set_interpolation (ClutterAnimator     *animator,
 
1901
                                             GObject             *object,
 
1902
                                             const gchar         *property_name,
 
1903
                                             ClutterInterpolation interpolation)
 
1904
{
 
1905
  GList              *initial;
 
1906
  ClutterAnimatorKey  key, *initial_key;
 
1907
 
 
1908
  g_return_if_fail (CLUTTER_IS_ANIMATOR (animator));
 
1909
  g_return_if_fail (G_IS_OBJECT (object));
 
1910
  g_return_if_fail (property_name);
 
1911
 
 
1912
  key.object        = object;
 
1913
  key.property_name = g_intern_string (property_name);
 
1914
  initial = g_list_find_custom (animator->priv->score, &key,
 
1915
                                sort_actor_prop_func);
 
1916
  if (initial)
 
1917
    {
 
1918
      initial_key = initial->data;
 
1919
      initial_key->interpolation = interpolation;
 
1920
    }
 
1921
}
 
1922
 
 
1923
GType
 
1924
clutter_animator_key_get_type (void)
 
1925
{
 
1926
  static GType our_type = 0;
 
1927
 
 
1928
  if (!our_type)
 
1929
    our_type = g_boxed_type_register_static (I_("ClutterAnimatorKey"),
 
1930
                                             clutter_animator_key_copy,
 
1931
                                             clutter_animator_key_free);
 
1932
 
 
1933
  return our_type;
 
1934
}
 
1935
 
 
1936
 
 
1937
/**
 
1938
 * clutter_animator_key_get_object:
 
1939
 * @key: a #ClutterAnimatorKey
 
1940
 *
 
1941
 * Retrieves the object a key applies to.
 
1942
 *
 
1943
 * Return value: (transfer none): the object an animator_key exist for.
 
1944
 *
 
1945
 * Since: 1.2
 
1946
 */
 
1947
GObject *
 
1948
clutter_animator_key_get_object (const ClutterAnimatorKey *key)
 
1949
{
 
1950
  g_return_val_if_fail (key != NULL, NULL);
 
1951
 
 
1952
  return key->object;
 
1953
}
 
1954
 
 
1955
/**
 
1956
 * clutter_animator_key_get_property_name:
 
1957
 * @key: a #ClutterAnimatorKey
 
1958
 *
 
1959
 * Retrieves the name of the property a key applies to.
 
1960
 *
 
1961
 * Return value: the name of the property an animator_key exist for.
 
1962
 *
 
1963
 * Since: 1.2
 
1964
 */
 
1965
G_CONST_RETURN gchar *
 
1966
clutter_animator_key_get_property_name (const ClutterAnimatorKey *key)
 
1967
{
 
1968
  g_return_val_if_fail (key != NULL, NULL);
 
1969
 
 
1970
  return key->property_name;
 
1971
}
 
1972
 
 
1973
/**
 
1974
 * clutter_animator_key_get_property_type:
 
1975
 * @key: a #ClutterAnimatorKey
 
1976
 *
 
1977
 * Retrieves the #GType of the property a key applies to
 
1978
 *
 
1979
 * You can use this type to initialize the #GValue to pass to
 
1980
 * clutter_animator_key_get_value()
 
1981
 *
 
1982
 * Return value: the #GType of the property
 
1983
 *
 
1984
 * Since: 1.2
 
1985
 */
 
1986
GType
 
1987
clutter_animator_key_get_property_type (const ClutterAnimatorKey *key)
 
1988
{
 
1989
  g_return_val_if_fail (key != NULL, G_TYPE_INVALID);
 
1990
 
 
1991
  return G_VALUE_TYPE (&key->value);
 
1992
}
 
1993
 
 
1994
/**
 
1995
 * clutter_animator_key_get_mode:
 
1996
 * @key: a #ClutterAnimatorKey
 
1997
 *
 
1998
 * Retrieves the mode of a #ClutterAnimator key, for the first key of a
 
1999
 * property for an object this represents the whether the animation is
 
2000
 * open ended and or curved for the remainding keys for the property it
 
2001
 * represents the easing mode.
 
2002
 *
 
2003
 * Return value: the mode of a #ClutterAnimatorKey
 
2004
 *
 
2005
 * Since: 1.2
 
2006
 */
 
2007
gulong
 
2008
clutter_animator_key_get_mode (const ClutterAnimatorKey *key)
 
2009
{
 
2010
  g_return_val_if_fail (key != NULL, 0);
 
2011
 
 
2012
  return key->mode;
 
2013
}
 
2014
 
 
2015
/**
 
2016
 * clutter_animator_key_get_progress:
 
2017
 * @key: a #ClutterAnimatorKey
 
2018
 *
 
2019
 * Retrieves the progress of an clutter_animator_key
 
2020
 *
 
2021
 * Return value: the progress defined for a #ClutterAnimator key.
 
2022
 *
 
2023
 * Since: 1.2
 
2024
 */
 
2025
gdouble
 
2026
clutter_animator_key_get_progress (const ClutterAnimatorKey *key)
 
2027
{
 
2028
  g_return_val_if_fail (key != NULL, 0.0);
 
2029
 
 
2030
  return key->progress;
 
2031
}
 
2032
 
 
2033
/**
 
2034
 * clutter_animator_key_get_value:
 
2035
 * @key: a #ClutterAnimatorKey
 
2036
 * @value: a #GValue initialized with the correct type for the animator key
 
2037
 *
 
2038
 * Retrieves a copy of the value for a #ClutterAnimatorKey.
 
2039
 *
 
2040
 * The passed in #GValue needs to be already initialized for the value
 
2041
 * type of the key or to a type that allow transformation from the value
 
2042
 * type of the key.
 
2043
 *
 
2044
 * Use g_value_unset() when done.
 
2045
 *
 
2046
 * Return value: %TRUE if the passed #GValue was successfully set, and
 
2047
 *   %FALSE otherwise
 
2048
 *
 
2049
 * Since: 1.2
 
2050
 */
 
2051
gboolean
 
2052
clutter_animator_key_get_value (const ClutterAnimatorKey *key,
 
2053
                                GValue                   *value)
 
2054
{
 
2055
  GType gtype;
 
2056
 
 
2057
  g_return_val_if_fail (key != NULL, FALSE);
 
2058
 
 
2059
  gtype = G_VALUE_TYPE (&key->value);
 
2060
 
 
2061
  if (g_value_type_compatible (gtype, G_VALUE_TYPE (value)))
 
2062
    {
 
2063
      g_value_copy (&key->value, value);
 
2064
      return TRUE;
 
2065
    }
 
2066
 
 
2067
  if (g_value_type_transformable (gtype, G_VALUE_TYPE (value)))
 
2068
    {
 
2069
      if (g_value_transform (&key->value, value))
 
2070
        return TRUE;
 
2071
    }
 
2072
 
 
2073
  return FALSE;
 
2074
}