4 * An OpenGL based 'interactive canvas' library.
6 * Copyright (C) 2012 Intel Corporation
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Author: Emmanuele Bassi <ebassi@linux.intel.com>
25 * SECTION:clutter-keyframe-transition
26 * @Title: ClutterKeyframeTransition
27 * @Short_Description: Keyframe property transition
29 * #ClutterKeyframeTransition allows animating a property by defining
30 * "key frames": values at a normalized position on the transition
33 * The #ClutterKeyframeTransition interpolates the value of the property
34 * to which it's bound across these key values.
36 * Setting up a #ClutterKeyframeTransition means providing the times,
37 * values, and easing modes between these key frames, for instance:
40 * ClutterTransition *keyframe;
42 * keyframe = clutter_keyframe_transition_new ("opacity");
43 * clutter_transition_set_from (keyframe, G_TYPE_UINT, 255);
44 * clutter_transition_set_to (keyframe, G_TYPE_UINT, 0);
45 * clutter_keyframe_transition_set_keys (CLUTTER_KEYFRAME_TRANSITION (keyframe),
47 * 1, /* number of key frames */
48 * 0.5, 128, CLUTTER_EASE_IN_OUT_CUBIC);
51 * The example above sets up a keyframe transition for the #ClutterActor:opacity
52 * property of a #ClutterActor; the transition starts and sets the value of the
53 * property to fully transparent; between the start of the transition and its mid
54 * point, it will animate the property to half opacity, using an easy in/easy out
55 * progress. Once the transition reaches the mid point, it will linearly fade the
56 * actor out until it reaches the end of the transition.
58 * The #ClutterKeyframeTransition will add an implicit key frame between the last
59 * and the 1.0 value, to interpolate to the final value of the transition's
62 * #ClutterKeyframeTransition is available since Clutter 1.12.
69 #include "clutter-keyframe-transition.h"
71 #include "clutter-debug.h"
72 #include "clutter-easing.h"
73 #include "clutter-interval.h"
74 #include "clutter-private.h"
75 #include "clutter-timeline.h"
78 #include <gobject/gvaluecollector.h>
80 typedef struct _KeyFrame
87 ClutterAnimationMode mode;
89 ClutterInterval *interval;
92 struct _ClutterKeyframeTransitionPrivate
99 G_DEFINE_TYPE (ClutterKeyframeTransition,
100 clutter_keyframe_transition,
101 CLUTTER_TYPE_PROPERTY_TRANSITION)
104 key_frame_free (gpointer data)
108 KeyFrame *key = data;
110 g_object_unref (key->interval);
115 sort_by_key (gconstpointer a,
118 const KeyFrame *k_a = a;
119 const KeyFrame *k_b = b;
121 if (fabs (k_a->key - k_b->key) < 0.0001)
124 if (k_a->key > k_a->key)
131 clutter_keyframe_transition_sort_frames (ClutterKeyframeTransition *transition)
133 if (transition->priv->frames != NULL)
134 g_array_sort (transition->priv->frames, sort_by_key);
138 clutter_keyframe_transition_init_frames (ClutterKeyframeTransition *transition,
141 ClutterKeyframeTransitionPrivate *priv = transition->priv;
144 priv->frames = g_array_sized_new (FALSE, FALSE,
147 g_array_set_clear_func (priv->frames, key_frame_free);
149 /* we add an implicit key frame that goes to 1.0, so that the
150 * user doesn't have to do that an can simply add key frames
151 * in between 0.0 and 1.0
153 for (i = 0; i < n_key_frames + 1; i++)
157 if (i == n_key_frames)
162 frame.mode = CLUTTER_LINEAR;
163 frame.interval = NULL;
165 g_array_insert_val (priv->frames, i, frame);
170 clutter_keyframe_transition_update_frames (ClutterKeyframeTransition *transition)
172 ClutterKeyframeTransitionPrivate *priv = transition->priv;
175 if (priv->frames == NULL)
178 for (i = 0; i < priv->frames->len; i++)
180 KeyFrame *cur_frame = &g_array_index (priv->frames, KeyFrame, i);
181 KeyFrame *prev_frame;
184 prev_frame = &g_array_index (priv->frames, KeyFrame, i - 1);
188 if (prev_frame != NULL)
190 cur_frame->start = prev_frame->key;
192 if (prev_frame->interval != NULL)
196 value = clutter_interval_peek_final_value (prev_frame->interval);
198 if (cur_frame->interval != NULL)
199 clutter_interval_set_initial_value (cur_frame->interval, value);
202 cur_frame->interval =
203 clutter_interval_new_with_values (G_VALUE_TYPE (value), value, NULL);
208 cur_frame->start = 0.0;
210 cur_frame->end = cur_frame->key;
215 clutter_keyframe_transition_compute_value (ClutterTransition *transition,
216 ClutterAnimatable *animatable,
217 ClutterInterval *interval,
220 ClutterKeyframeTransition *self = CLUTTER_KEYFRAME_TRANSITION (transition);
221 ClutterTimeline *timeline = CLUTTER_TIMELINE (transition);
222 ClutterKeyframeTransitionPrivate *priv = self->priv;
223 ClutterTransitionClass *parent_class;
224 ClutterTimelineDirection direction;
225 ClutterInterval *real_interval;
226 gdouble real_progress;
228 KeyFrame *cur_frame = NULL;
230 real_interval = interval;
231 real_progress = progress;
233 /* if we don't have any keyframe, we behave like our parent class */
234 if (priv->frames == NULL)
237 direction = clutter_timeline_get_direction (timeline);
239 /* we need a normalized linear value */
240 t = clutter_timeline_get_elapsed_time (timeline);
241 d = clutter_timeline_get_duration (timeline);
244 if (priv->current_frame < 0)
246 if (direction == CLUTTER_TIMELINE_FORWARD)
247 priv->current_frame = 0;
249 priv->current_frame = priv->frames->len - 1;
252 cur_frame = &g_array_index (priv->frames, KeyFrame, priv->current_frame);
254 /* skip to the next key frame, depending on the direction of the timeline */
255 if (direction == CLUTTER_TIMELINE_FORWARD)
257 if (p > cur_frame->end)
259 priv->current_frame = MIN (priv->current_frame + 1,
260 priv->frames->len - 1);
262 cur_frame = &g_array_index (priv->frames, KeyFrame, priv->current_frame);
267 if (p < cur_frame->start)
269 priv->current_frame = MAX (priv->current_frame - 1, 0);
271 cur_frame = &g_array_index (priv->frames, KeyFrame, priv->current_frame);
275 /* if we are at the boundaries of the transition, use the from and to
276 * value from the transition
278 if (priv->current_frame == 0)
282 value = clutter_interval_peek_initial_value (interval);
283 clutter_interval_set_initial_value (cur_frame->interval, value);
285 else if (priv->current_frame == priv->frames->len - 1)
289 cur_frame->mode = clutter_timeline_get_progress_mode (timeline);
291 value = clutter_interval_peek_final_value (interval);
292 clutter_interval_set_final_value (cur_frame->interval, value);
295 /* update the interval to be used to interpolate the property */
296 real_interval = cur_frame->interval;
298 /* normalize the progress */
299 real_progress = (p - cur_frame->start) / (cur_frame->end - cur_frame->start);
301 #ifdef CLUTTER_ENABLE_DEBUG
302 if (CLUTTER_HAS_DEBUG (ANIMATION))
307 value = clutter_interval_peek_initial_value (cur_frame->interval);
308 from = g_strdup_value_contents (value);
310 value = clutter_interval_peek_final_value (cur_frame->interval);
311 to = g_strdup_value_contents (value);
313 CLUTTER_NOTE (ANIMATION,
314 "cur_frame [%d] => { %g, %s, %s %s %s } - "
315 "progress: %g, sub-progress: %g\n",
318 clutter_get_easing_name_for_mode (cur_frame->mode),
320 direction == CLUTTER_TIMELINE_FORWARD ? "->" : "<-",
327 #endif /* CLUTTER_ENABLE_DEBUG */
331 CLUTTER_TRANSITION_CLASS (clutter_keyframe_transition_parent_class);
332 parent_class->compute_value (transition, animatable, real_interval, real_progress);
336 clutter_keyframe_transition_started (ClutterTimeline *timeline)
338 ClutterKeyframeTransition *transition;
340 transition = CLUTTER_KEYFRAME_TRANSITION (timeline);
342 transition->priv->current_frame = -1;
344 clutter_keyframe_transition_sort_frames (transition);
345 clutter_keyframe_transition_update_frames (transition);
349 clutter_keyframe_transition_completed (ClutterTimeline *timeline)
351 ClutterKeyframeTransitionPrivate *priv;
353 priv = CLUTTER_KEYFRAME_TRANSITION (timeline)->priv;
355 priv->current_frame = -1;
359 clutter_keyframe_transition_finalize (GObject *gobject)
361 ClutterKeyframeTransitionPrivate *priv;
363 priv = CLUTTER_KEYFRAME_TRANSITION (gobject)->priv;
365 if (priv->frames != NULL)
366 g_array_unref (priv->frames);
368 G_OBJECT_CLASS (clutter_keyframe_transition_parent_class)->finalize (gobject);
372 clutter_keyframe_transition_class_init (ClutterKeyframeTransitionClass *klass)
374 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
375 ClutterTimelineClass *timeline_class = CLUTTER_TIMELINE_CLASS (klass);
376 ClutterTransitionClass *transition_class = CLUTTER_TRANSITION_CLASS (klass);
378 g_type_class_add_private (klass, sizeof (ClutterKeyframeTransitionPrivate));
380 gobject_class->finalize = clutter_keyframe_transition_finalize;
382 timeline_class->started = clutter_keyframe_transition_started;
383 timeline_class->completed = clutter_keyframe_transition_completed;
385 transition_class->compute_value = clutter_keyframe_transition_compute_value;
389 clutter_keyframe_transition_init (ClutterKeyframeTransition *self)
392 G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_KEYFRAME_TRANSITION,
393 ClutterKeyframeTransitionPrivate);
397 * clutter_keyframe_transition_new:
398 * @property_name: the property to animate
400 * Creates a new #ClutterKeyframeTransition for @property_name.
402 * Return value: (transfer full): the newly allocated
403 * #ClutterKeyframeTransition instance. Use g_object_unref() when
404 * done to free its resources.
409 clutter_keyframe_transition_new (const char *property_name)
411 return g_object_new (CLUTTER_TYPE_KEYFRAME_TRANSITION,
412 "property-name", property_name,
417 * clutter_keyframe_transition_set_key_frames:
418 * @transition: a #ClutterKeyframeTransition
419 * @n_key_frames: the number of values
420 * @key_frames: (array length=n_key_frames): an array of keys between 0.0
421 * and 1.0, one for each key frame
423 * Sets the keys for each key frame inside @transition.
425 * If @transition does not hold any key frame, @n_key_frames key frames
426 * will be created; if @transition already has key frames, @key_frames must
427 * have at least as many elements as the number of key frames.
432 clutter_keyframe_transition_set_key_frames (ClutterKeyframeTransition *transition,
434 const double *key_frames)
436 ClutterKeyframeTransitionPrivate *priv;
439 g_return_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition));
440 g_return_if_fail (n_key_frames > 0);
441 g_return_if_fail (key_frames != NULL);
443 priv = transition->priv;
445 if (priv->frames == NULL)
446 clutter_keyframe_transition_init_frames (transition, n_key_frames);
448 g_return_if_fail (n_key_frames == priv->frames->len - 1);
450 for (i = 0; i < n_key_frames; i++)
452 KeyFrame *frame = &g_array_index (priv->frames, KeyFrame, i);
454 frame->key = key_frames[i];
459 * clutter_keyframe_transition_set_values:
460 * @transition: a #ClutterKeyframeTransition
461 * @n_values: the number of values
462 * @values: (array length=n_values): an array of values, one for each
465 * Sets the values for each key frame inside @transition.
467 * If @transition does not hold any key frame, @n_values key frames will
468 * be created; if @transition already has key frames, @values must have
469 * at least as many elements as the number of key frames.
474 clutter_keyframe_transition_set_values (ClutterKeyframeTransition *transition,
476 const GValue *values)
478 ClutterKeyframeTransitionPrivate *priv;
481 g_return_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition));
482 g_return_if_fail (n_values > 0);
483 g_return_if_fail (values != NULL);
485 priv = transition->priv;
487 if (priv->frames == NULL)
488 clutter_keyframe_transition_init_frames (transition, n_values);
490 g_return_if_fail (n_values == priv->frames->len - 1);
492 for (i = 0; i < n_values; i++)
494 KeyFrame *frame = &g_array_index (priv->frames, KeyFrame, i);
496 clutter_interval_set_final_value (frame->interval, &values[i]);
501 * clutter_keyframe_transition_set_modes:
502 * @transition: a #ClutterKeyframeTransition
503 * @n_modes: the number of easing modes
504 * @modes: (array length=n_modes): an array of easing modes, one for
507 * Sets the easing modes for each key frame inside @transition.
509 * If @transition does not hold any key frame, @n_modes key frames will
510 * be created; if @transition already has key frames, @modes must have
511 * at least as many elements as the number of key frames.
516 clutter_keyframe_transition_set_modes (ClutterKeyframeTransition *transition,
518 const ClutterAnimationMode *modes)
520 ClutterKeyframeTransitionPrivate *priv;
523 g_return_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition));
524 g_return_if_fail (n_modes > 0);
525 g_return_if_fail (modes != NULL);
527 priv = transition->priv;
529 if (priv->frames == NULL)
530 clutter_keyframe_transition_init_frames (transition, n_modes);
532 g_return_if_fail (n_modes == priv->frames->len - 1);
534 for (i = 0; i < n_modes; i++)
536 KeyFrame *frame = &g_array_index (priv->frames, KeyFrame, i);
538 frame->mode = modes[i];
543 * clutter_keyframe_transition_set: (skip)
544 * @transition: a #ClutterKeyframeTransition
545 * @gtype: the type of the values to use for the key frames
546 * @n_key_frames: the number of key frames between the initial
548 * @...: a list of tuples, containing the key frame index, the value
549 * at the key frame, and the animation mode
551 * Sets the key frames of the @transition.
553 * This variadic arguments function is a convenience for C developers;
554 * language bindings should use clutter_keyframe_transition_set_keys(),
555 * clutter_keyframe_transition_set_modes(), and
556 * clutter_keyframe_transition_set_values() instead.
561 clutter_keyframe_transition_set (ClutterKeyframeTransition *transition,
566 ClutterKeyframeTransitionPrivate *priv;
570 g_return_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition));
571 g_return_if_fail (gtype != G_TYPE_INVALID);
572 g_return_if_fail (n_key_frames > 0);
574 priv = transition->priv;
576 if (priv->frames == NULL)
577 clutter_keyframe_transition_init_frames (transition, n_key_frames);
579 g_return_if_fail (n_key_frames == priv->frames->len - 1);
581 va_start (args, n_key_frames);
583 for (i = 0; i < n_key_frames; i++)
585 KeyFrame *frame = &g_array_index (priv->frames, KeyFrame, i);
586 GValue value = G_VALUE_INIT;
589 frame->key = va_arg (args, double);
591 G_VALUE_COLLECT_INIT (&value, gtype, args, 0, &error);
594 g_warning ("%s: %s", G_STRLOC, error);
599 frame->mode = va_arg (args, ClutterAnimationMode);
601 g_clear_object (&frame->interval);
602 frame->interval = clutter_interval_new_with_values (gtype, NULL, &value);
604 g_value_unset (&value);
611 * clutter_keyframe_transition_clear:
612 * @transition: a #ClutterKeyframeTransition
614 * Removes all key frames from @transition.
619 clutter_keyframe_transition_clear (ClutterKeyframeTransition *transition)
621 g_return_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition));
623 if (transition->priv->frames != NULL)
625 g_array_unref (transition->priv->frames);
626 transition->priv->frames = NULL;
631 * clutter_keyframe_transition_get_n_key_frames:
632 * @transition: a #ClutterKeyframeTransition
634 * Retrieves the number of key frames inside @transition.
636 * Return value: the number of key frames
641 clutter_keyframe_transition_get_n_key_frames (ClutterKeyframeTransition *transition)
643 g_return_val_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition), 0);
645 if (transition->priv->frames == NULL)
648 return transition->priv->frames->len - 1;
652 * clutter_keyframe_transition_set_key_frame:
653 * @transition: a #ClutterKeyframeTransition
654 * @index_: the index of the key frame
655 * @key: the key of the key frame
656 * @mode: the easing mode of the key frame
657 * @value: a #GValue containing the value of the key frame
659 * Sets the details of the key frame at @index_ inside @transition.
661 * The @transition must already have a key frame at @index_, and @index_
662 * must be smaller than the number of key frames inside @transition.
667 clutter_keyframe_transition_set_key_frame (ClutterKeyframeTransition *transition,
670 ClutterAnimationMode mode,
673 ClutterKeyframeTransitionPrivate *priv;
676 g_return_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition));
678 priv = transition->priv;
679 g_return_if_fail (priv->frames != NULL);
680 g_return_if_fail (index_ < priv->frames->len - 1);
682 frame = &g_array_index (priv->frames, KeyFrame, index_);
685 clutter_interval_set_final_value (frame->interval, value);
689 * clutter_keyframe_transition_get_key_frame:
690 * @transition: a #ClutterKeyframeTransition
691 * @index_: the index of the key frame
692 * @key: (out) (allow-none): return location for the key, or %NULL
693 * @mode: (out) (allow-none): return location for the easing mode, or %NULL
694 * @value: (out caller-allocates): a #GValue initialized with the type of
697 * Retrieves the details of the key frame at @index_ inside @transition.
699 * The @transition must already have key frames set, and @index_ must be
700 * smaller than the number of key frames.
705 clutter_keyframe_transition_get_key_frame (ClutterKeyframeTransition *transition,
708 ClutterAnimationMode *mode,
711 ClutterKeyframeTransitionPrivate *priv;
712 const KeyFrame *frame;
714 g_return_if_fail (CLUTTER_IS_KEYFRAME_TRANSITION (transition));
716 priv = transition->priv;
717 g_return_if_fail (priv->frames != NULL);
718 g_return_if_fail (index_ < priv->frames->len - 1);
720 frame = &g_array_index (priv->frames, KeyFrame, index_);
729 clutter_interval_get_final_value (frame->interval, value);