4 * An OpenGL based 'interactive canvas' library.
6 * Authored By Matthew Allum <mallum@openedhand.com>
8 * Copyright (C) 2006 OpenedHand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
27 * SECTION:clutter-timeline
28 * @short_description: A class for time-based events
29 * @see_also: #ClutterAnimation, #ClutterAnimator, #ClutterState
31 * #ClutterTimeline is a base class for managing time-based event that cause
32 * Clutter to redraw a stage, such as animations.
34 * Each #ClutterTimeline instance has a duration: once a timeline has been
35 * started, using clutter_timeline_start(), it will emit a signal that can
36 * be used to update the state of the actors.
38 * It is important to note that #ClutterTimeline is not a generic API for
39 * calling closures after an interval; each Timeline is tied into the master
40 * clock used to drive the frame cycle. If you need to schedule a closure
41 * after an interval, see clutter_threads_add_timeout() instead.
43 * Users of #ClutterTimeline should connect to the #ClutterTimeline::new-frame
44 * signal, which is emitted each time a timeline is advanced during the maste
45 * clock iteration. The #ClutterTimeline::new-frame signal provides the time
46 * elapsed since the beginning of the timeline, in milliseconds. A normalized
47 * progress value can be obtained by calling clutter_timeline_get_progress().
48 * By using clutter_timeline_get_delta() it is possible to obtain the wallclock
49 * time elapsed since the last emission of the #ClutterTimeline::new-frame
52 * Initial state can be set up by using the #ClutterTimeline::started signal,
53 * while final state can be set up by using the #ClutterTimeline::completed
54 * signal. The #ClutterTimeline guarantees the emission of at least a single
55 * #ClutterTimeline::new-frame signal, as well as the emission of the
56 * #ClutterTimeline::completed signal.
58 * It is possible to connect to specific points in the timeline progress by
59 * adding <emphasis>markers</emphasis> using clutter_timeline_add_marker_at_time()
60 * and connecting to the #ClutterTimeline::marker-reached signal.
62 * Timelines can be made to loop once they reach the end of their duration, by
63 * using clutter_timeline_set_repeat_count(); a looping timeline will still
64 * emit the #ClutterTimeline::completed signal once it reaches the end of its
67 * Timelines have a #ClutterTimeline:direction: the default direction is
68 * %CLUTTER_TIMELINE_FORWARD, and goes from 0 to the duration; it is possible
69 * to change the direction to %CLUTTER_TIMELINE_BACKWARD, and have the timeline
70 * go from the duration to 0. The direction can be automatically reversed
71 * when reaching completion by using the #ClutterTimeline:auto-reverse property.
73 * Timelines are used in the Clutter animation framework by classes like
74 * #ClutterAnimation, #ClutterAnimator, and #ClutterState.
76 * <refsect2 id="timeline-script">
77 * <title>Defining Timelines in ClutterScript</title>
78 * <para>A #ClutterTimeline can be described in #ClutterScript like any
79 * other object. Additionally, it is possible to define markers directly
80 * inside the JSON definition by using the <emphasis>markers</emphasis>
81 * JSON object member, such as:</para>
82 * <informalexample><programlisting><![CDATA[
84 "type" : "ClutterTimeline",
87 { "name" : "quarter", "time" : 250 },
88 { "name" : "half-time", "time" : 500 },
89 { "name" : "three-quarters", "time" : 750 }
92 * ]]></programlisting></informalexample>
100 #include "clutter-timeline.h"
102 #include "clutter-debug.h"
103 #include "clutter-easing.h"
104 #include "clutter-enum-types.h"
105 #include "clutter-main.h"
106 #include "clutter-marshal.h"
107 #include "clutter-master-clock.h"
108 #include "clutter-private.h"
109 #include "clutter-scriptable.h"
111 #include "deprecated/clutter-timeline.h"
113 static void clutter_scriptable_iface_init (ClutterScriptableIface *iface);
115 G_DEFINE_TYPE_WITH_CODE (ClutterTimeline, clutter_timeline, G_TYPE_OBJECT,
116 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_SCRIPTABLE,
117 clutter_scriptable_iface_init));
119 struct _ClutterTimelinePrivate
121 ClutterTimelineDirection direction;
125 /* The total length in milliseconds of this timeline */
129 /* The current amount of elapsed time */
132 /* The elapsed time since the last frame was fired */
135 GHashTable *markers_by_name;
137 /* Time we last advanced the elapsed time and showed a frame */
138 gint64 last_frame_time;
140 /* How many times the timeline should repeat */
143 /* The number of times the timeline has repeated */
146 ClutterTimelineProgressFunc progress_func;
147 gpointer progress_data;
148 GDestroyNotify progress_notify;
149 ClutterAnimationMode progress_mode;
151 guint is_playing : 1;
153 /* If we've just started playing and haven't yet gotten
154 * a tick from the master clock
156 guint waiting_first_tick : 1;
157 guint auto_reverse : 1;
181 static GParamSpec *obj_props[PROP_LAST] = { NULL, };
195 static guint timeline_signals[LAST_SIGNAL] = { 0, };
197 static TimelineMarker *
198 timeline_marker_new (const gchar *name,
201 TimelineMarker *marker = g_slice_new0 (TimelineMarker);
203 marker->name = g_strdup (name);
204 marker->quark = g_quark_from_string (marker->name);
205 marker->msecs = msecs;
211 timeline_marker_free (gpointer data)
215 TimelineMarker *marker = data;
217 g_free (marker->name);
218 g_slice_free (TimelineMarker, marker);
223 * clutter_timeline_add_marker_internal:
224 * @timeline: a #ClutterTimeline
225 * @marker: a TimelineMarker
227 * Adds @marker into the hash table of markers for @timeline.
229 * The TimelineMarker will either be added or, in case of collisions
230 * with another existing marker, freed. In any case, this function
231 * assumes the ownership of the passed @marker.
234 clutter_timeline_add_marker_internal (ClutterTimeline *timeline,
235 TimelineMarker *marker)
237 ClutterTimelinePrivate *priv = timeline->priv;
238 TimelineMarker *old_marker;
240 /* create the hash table that will hold the markers */
241 if (G_UNLIKELY (priv->markers_by_name == NULL))
242 priv->markers_by_name = g_hash_table_new_full (g_str_hash, g_str_equal,
244 timeline_marker_free);
246 old_marker = g_hash_table_lookup (priv->markers_by_name, marker->name);
247 if (old_marker != NULL)
249 g_warning ("A marker named '%s' already exists at time %d",
252 timeline_marker_free (marker);
256 g_hash_table_insert (priv->markers_by_name, marker->name, marker);
260 clutter_timeline_set_loop_internal (ClutterTimeline *timeline,
263 gint old_repeat_count;
265 old_repeat_count = timeline->priv->repeat_count;
268 clutter_timeline_set_repeat_count (timeline, -1);
270 clutter_timeline_set_repeat_count (timeline, 0);
272 if (old_repeat_count != timeline->priv->repeat_count)
273 g_object_notify_by_pspec (G_OBJECT (timeline), obj_props[PROP_LOOP]);
277 typedef struct _ParseClosure {
278 ClutterTimeline *timeline;
279 ClutterScript *script;
285 parse_timeline_markers (JsonArray *array,
290 ParseClosure *clos = data;
292 TimelineMarker *marker;
295 if (JSON_NODE_TYPE (element) != JSON_NODE_OBJECT)
297 g_warning ("The 'markers' member of a ClutterTimeline description "
298 "should be an array of objects, but the element %d of the "
299 "array is of type '%s'. The element will be ignored.",
301 json_node_type_name (element));
305 object = json_node_get_object (element);
307 if (!(json_object_has_member (object, "name") &&
308 json_object_has_member (object, "time")))
310 g_warning ("The marker definition in a ClutterTimeline description "
311 "must be an object with the 'name' and 'time' members, "
312 "but the element %d of the 'markers' array does not have "
318 if (G_IS_VALUE (clos->value))
319 markers = g_value_get_pointer (clos->value);
322 g_value_init (clos->value, G_TYPE_POINTER);
326 marker = timeline_marker_new (json_object_get_string_member (object, "name"),
327 json_object_get_int_member (object, "time"));
329 markers = g_list_prepend (markers, marker);
331 g_value_set_pointer (clos->value, markers);
337 clutter_timeline_parse_custom_node (ClutterScriptable *scriptable,
338 ClutterScript *script,
345 if (strcmp (name, "markers") != 0)
348 if (JSON_NODE_TYPE (node) != JSON_NODE_ARRAY)
351 clos.timeline = CLUTTER_TIMELINE (scriptable);
352 clos.script = script;
356 json_array_foreach_element (json_node_get_array (node),
357 parse_timeline_markers,
364 clutter_timeline_set_custom_property (ClutterScriptable *scriptable,
365 ClutterScript *script,
369 if (strcmp (name, "markers") == 0)
371 ClutterTimeline *timeline = CLUTTER_TIMELINE (scriptable);
372 GList *markers = g_value_get_pointer (value);
375 /* the list was created through prepend() */
376 markers = g_list_reverse (markers);
378 for (m = markers; m != NULL; m = m->next)
379 clutter_timeline_add_marker_internal (timeline, m->data);
381 g_list_free (markers);
384 g_object_set_property (G_OBJECT (scriptable), name, value);
389 clutter_scriptable_iface_init (ClutterScriptableIface *iface)
391 iface->parse_custom_node = clutter_timeline_parse_custom_node;
392 iface->set_custom_property = clutter_timeline_set_custom_property;
398 clutter_timeline_set_property (GObject *object,
403 ClutterTimeline *timeline = CLUTTER_TIMELINE (object);
408 clutter_timeline_set_loop_internal (timeline, g_value_get_boolean (value));
412 clutter_timeline_set_delay (timeline, g_value_get_uint (value));
416 clutter_timeline_set_duration (timeline, g_value_get_uint (value));
420 clutter_timeline_set_direction (timeline, g_value_get_enum (value));
423 case PROP_AUTO_REVERSE:
424 clutter_timeline_set_auto_reverse (timeline, g_value_get_boolean (value));
427 case PROP_REPEAT_COUNT:
428 clutter_timeline_set_repeat_count (timeline, g_value_get_int (value));
431 case PROP_PROGRESS_MODE:
432 clutter_timeline_set_progress_mode (timeline, g_value_get_enum (value));
436 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
442 clutter_timeline_get_property (GObject *object,
447 ClutterTimeline *timeline = CLUTTER_TIMELINE (object);
448 ClutterTimelinePrivate *priv = timeline->priv;
453 g_value_set_boolean (value, priv->repeat_count != 0);
457 g_value_set_uint (value, priv->delay);
461 g_value_set_uint (value, clutter_timeline_get_duration (timeline));
465 g_value_set_enum (value, priv->direction);
468 case PROP_AUTO_REVERSE:
469 g_value_set_boolean (value, priv->auto_reverse);
472 case PROP_REPEAT_COUNT:
473 g_value_set_int (value, priv->repeat_count);
476 case PROP_PROGRESS_MODE:
477 g_value_set_enum (value, priv->progress_mode);
481 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
487 clutter_timeline_finalize (GObject *object)
489 ClutterTimeline *self = CLUTTER_TIMELINE (object);
490 ClutterTimelinePrivate *priv = self->priv;
491 ClutterMasterClock *master_clock;
493 if (priv->markers_by_name)
494 g_hash_table_destroy (priv->markers_by_name);
496 if (priv->is_playing)
498 master_clock = _clutter_master_clock_get_default ();
499 _clutter_master_clock_remove_timeline (master_clock, self);
502 G_OBJECT_CLASS (clutter_timeline_parent_class)->finalize (object);
506 clutter_timeline_dispose (GObject *object)
508 ClutterTimeline *self = CLUTTER_TIMELINE(object);
509 ClutterTimelinePrivate *priv;
515 g_source_remove (priv->delay_id);
519 if (priv->progress_notify != NULL)
521 priv->progress_notify (priv->progress_data);
522 priv->progress_func = NULL;
523 priv->progress_data = NULL;
524 priv->progress_notify = NULL;
527 G_OBJECT_CLASS (clutter_timeline_parent_class)->dispose (object);
531 clutter_timeline_class_init (ClutterTimelineClass *klass)
533 GObjectClass *object_class = G_OBJECT_CLASS (klass);
535 g_type_class_add_private (klass, sizeof (ClutterTimelinePrivate));
538 * ClutterTimeline:loop:
540 * Whether the timeline should automatically rewind and restart.
542 * As a side effect, setting this property to %TRUE will set the
543 * #ClutterTimeline:repeat-count property to -1, while setting this
544 * property to %FALSE will set the #ClutterTimeline:repeat-count
547 * Deprecated: 1.10: Use the #ClutterTimeline:repeat-count property instead.
549 obj_props[PROP_LOOP] =
550 g_param_spec_boolean ("loop",
552 P_("Should the timeline automatically restart"),
554 CLUTTER_PARAM_READWRITE | G_PARAM_DEPRECATED);
557 * ClutterTimeline:delay:
559 * A delay, in milliseconds, that should be observed by the
560 * timeline before actually starting.
564 obj_props[PROP_DELAY] =
565 g_param_spec_uint ("delay",
567 P_("Delay before start"),
570 CLUTTER_PARAM_READWRITE);
573 * ClutterTimeline:duration:
575 * Duration of the timeline in milliseconds, depending on the
576 * ClutterTimeline:fps value.
580 obj_props[PROP_DURATION] =
581 g_param_spec_uint ("duration",
583 P_("Duration of the timeline in milliseconds"),
586 CLUTTER_PARAM_READWRITE);
589 * ClutterTimeline:direction:
591 * The direction of the timeline, either %CLUTTER_TIMELINE_FORWARD or
592 * %CLUTTER_TIMELINE_BACKWARD.
596 obj_props[PROP_DIRECTION] =
597 g_param_spec_enum ("direction",
599 P_("Direction of the timeline"),
600 CLUTTER_TYPE_TIMELINE_DIRECTION,
601 CLUTTER_TIMELINE_FORWARD,
602 CLUTTER_PARAM_READWRITE);
605 * ClutterTimeline:auto-reverse:
607 * If the direction of the timeline should be automatically reversed
608 * when reaching the end.
612 obj_props[PROP_AUTO_REVERSE] =
613 g_param_spec_boolean ("auto-reverse",
615 P_("Whether the direction should be reversed when reaching the end"),
617 CLUTTER_PARAM_READWRITE);
620 * ClutterTimeline:repeat-count:
622 * Defines how many times the timeline should repeat.
624 * If the repeat count is 0, the timeline does not repeat.
626 * If the repeat count is set to -1, the timeline will repeat until it is
631 obj_props[PROP_REPEAT_COUNT] =
632 g_param_spec_int ("repeat-count",
634 P_("How many times the timeline should repeat"),
637 CLUTTER_PARAM_READWRITE);
640 * ClutterTimeline:progress-mode:
642 * Controls the way a #ClutterTimeline computes the normalized progress.
646 obj_props[PROP_PROGRESS_MODE] =
647 g_param_spec_enum ("progress-mode",
649 P_("How the timeline should compute the progress"),
650 CLUTTER_TYPE_ANIMATION_MODE,
652 CLUTTER_PARAM_READWRITE);
654 object_class->dispose = clutter_timeline_dispose;
655 object_class->finalize = clutter_timeline_finalize;
656 object_class->set_property = clutter_timeline_set_property;
657 object_class->get_property = clutter_timeline_get_property;
658 g_object_class_install_properties (object_class, PROP_LAST, obj_props);
661 * ClutterTimeline::new-frame:
662 * @timeline: the timeline which received the signal
663 * @msecs: the elapsed time between 0 and duration
665 * The ::new-frame signal is emitted for each timeline running
666 * timeline before a new frame is drawn to give animations a chance
667 * to update the scene.
669 timeline_signals[NEW_FRAME] =
670 g_signal_new (I_("new-frame"),
671 G_TYPE_FROM_CLASS (object_class),
673 G_STRUCT_OFFSET (ClutterTimelineClass, new_frame),
675 _clutter_marshal_VOID__INT,
679 * ClutterTimeline::completed:
680 * @timeline: the #ClutterTimeline which received the signal
682 * The #ClutterTimeline::completed signal is emitted when the timeline's
683 * elapsed time reaches the value of the #ClutterTimeline:duration
686 * This signal will be emitted even if the #ClutterTimeline is set to be
689 * If you want to get notification on whether the #ClutterTimeline has
690 * been stopped or has finished its run, including its eventual repeats,
691 * you should use the #ClutterTimeline::stopped signal instead.
693 timeline_signals[COMPLETED] =
694 g_signal_new (I_("completed"),
695 G_TYPE_FROM_CLASS (object_class),
697 G_STRUCT_OFFSET (ClutterTimelineClass, completed),
699 _clutter_marshal_VOID__VOID,
702 * ClutterTimeline::started:
703 * @timeline: the #ClutterTimeline which received the signal
705 * The ::started signal is emitted when the timeline starts its run.
706 * This might be as soon as clutter_timeline_start() is invoked or
707 * after the delay set in the ClutterTimeline:delay property has
710 timeline_signals[STARTED] =
711 g_signal_new (I_("started"),
712 G_TYPE_FROM_CLASS (object_class),
714 G_STRUCT_OFFSET (ClutterTimelineClass, started),
716 _clutter_marshal_VOID__VOID,
719 * ClutterTimeline::paused:
720 * @timeline: the #ClutterTimeline which received the signal
722 * The ::paused signal is emitted when clutter_timeline_pause() is invoked.
724 timeline_signals[PAUSED] =
725 g_signal_new (I_("paused"),
726 G_TYPE_FROM_CLASS (object_class),
728 G_STRUCT_OFFSET (ClutterTimelineClass, paused),
730 _clutter_marshal_VOID__VOID,
733 * ClutterTimeline::marker-reached:
734 * @timeline: the #ClutterTimeline which received the signal
735 * @marker_name: the name of the marker reached
736 * @msecs: the elapsed time
738 * The ::marker-reached signal is emitted each time a timeline
739 * reaches a marker set with
740 * clutter_timeline_add_marker_at_time(). This signal is detailed
741 * with the name of the marker as well, so it is possible to connect
742 * a callback to the ::marker-reached signal for a specific marker
745 * <informalexample><programlisting>
746 * clutter_timeline_add_marker_at_time (timeline, "foo", 500);
747 * clutter_timeline_add_marker_at_time (timeline, "bar", 750);
749 * g_signal_connect (timeline, "marker-reached",
750 * G_CALLBACK (each_marker_reached), NULL);
751 * g_signal_connect (timeline, "marker-reached::foo",
752 * G_CALLBACK (foo_marker_reached), NULL);
753 * g_signal_connect (timeline, "marker-reached::bar",
754 * G_CALLBACK (bar_marker_reached), NULL);
755 * </programlisting></informalexample>
757 * In the example, the first callback will be invoked for both
758 * the "foo" and "bar" marker, while the second and third callbacks
759 * will be invoked for the "foo" or "bar" markers, respectively.
763 timeline_signals[MARKER_REACHED] =
764 g_signal_new (I_("marker-reached"),
765 G_TYPE_FROM_CLASS (object_class),
766 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE |
767 G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
768 G_STRUCT_OFFSET (ClutterTimelineClass, marker_reached),
770 _clutter_marshal_VOID__STRING_INT,
775 * ClutterTimeline::stopped:
776 * @timeline: the #ClutterTimeline that emitted the signal
777 * @is_finished: %TRUE if the signal was emitted at the end of the
780 * The #ClutterTimeline::stopped signal is emitted when the timeline
781 * has been stopped, either because clutter_timeline_stop() has been
782 * called, or because it has been exhausted.
784 * This is different from the #ClutterTimeline::completed signal,
785 * which gets emitted after every repeat finishes.
787 * If the #ClutterTimeline has is marked as infinitely repeating,
788 * this signal will never be emitted.
792 timeline_signals[STOPPED] =
793 g_signal_new (I_("stopped"),
794 G_TYPE_FROM_CLASS (object_class),
796 G_STRUCT_OFFSET (ClutterTimelineClass, completed),
798 _clutter_marshal_VOID__BOOLEAN,
804 clutter_timeline_init (ClutterTimeline *self)
806 ClutterTimelinePrivate *priv;
809 G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_TIMELINE,
810 ClutterTimelinePrivate);
812 priv->progress_mode = CLUTTER_LINEAR;
815 struct CheckIfMarkerHitClosure
817 ClutterTimeline *timeline;
818 ClutterTimelineDirection direction;
825 have_passed_time (const struct CheckIfMarkerHitClosure *data,
828 /* Ignore markers that are outside the duration of the timeline */
829 if (msecs < 0 || msecs > data->duration)
832 if (data->direction == CLUTTER_TIMELINE_FORWARD)
834 /* We need to special case when a marker is added at the
835 beginning of the timeline */
838 data->new_time - data->delta <= 0)
841 /* Otherwise it's just a simple test if the time is in range of
842 the previous time and the new time */
843 return (msecs > data->new_time - data->delta
844 && msecs <= data->new_time);
848 /* We need to special case when a marker is added at the
849 end of the timeline */
850 if (msecs == data->duration &&
852 data->new_time + data->delta >= data->duration)
855 /* Otherwise it's just a simple test if the time is in range of
856 the previous time and the new time */
857 return (msecs >= data->new_time
858 && msecs < data->new_time + data->delta);
863 check_if_marker_hit (const gchar *name,
864 TimelineMarker *marker,
865 struct CheckIfMarkerHitClosure *data)
867 if (have_passed_time (data, marker->msecs))
869 CLUTTER_NOTE (SCHEDULER, "Marker '%s' reached", name);
871 g_signal_emit (data->timeline, timeline_signals[MARKER_REACHED],
879 check_markers (ClutterTimeline *timeline,
882 ClutterTimelinePrivate *priv = timeline->priv;
883 struct CheckIfMarkerHitClosure data;
885 /* shortcircuit here if we don't have any marker installed */
886 if (priv->markers_by_name == NULL)
889 /* store the details of the timeline so that changing them in a
890 marker signal handler won't affect which markers are hit */
891 data.timeline = timeline;
892 data.direction = priv->direction;
893 data.new_time = priv->elapsed_time;
894 data.duration = priv->duration;
897 g_hash_table_foreach (priv->markers_by_name,
898 (GHFunc) check_if_marker_hit,
903 emit_frame_signal (ClutterTimeline *timeline)
905 ClutterTimelinePrivate *priv = timeline->priv;
907 /* see bug https://bugzilla.gnome.org/show_bug.cgi?id=654066 */
908 gint elapsed = (gint) priv->elapsed_time;
910 CLUTTER_NOTE (SCHEDULER, "Emitting ::new-frame signal on timeline[%p]", timeline);
912 g_signal_emit (timeline, timeline_signals[NEW_FRAME], 0, elapsed);
916 is_complete (ClutterTimeline *timeline)
918 ClutterTimelinePrivate *priv = timeline->priv;
920 return (priv->direction == CLUTTER_TIMELINE_FORWARD
921 ? priv->elapsed_time >= priv->duration
922 : priv->elapsed_time <= 0);
926 set_is_playing (ClutterTimeline *timeline,
929 ClutterTimelinePrivate *priv = timeline->priv;
930 ClutterMasterClock *master_clock;
932 is_playing = !!is_playing;
934 if (is_playing == priv->is_playing)
937 priv->is_playing = is_playing;
939 master_clock = _clutter_master_clock_get_default ();
940 if (priv->is_playing)
942 _clutter_master_clock_add_timeline (master_clock, timeline);
943 priv->waiting_first_tick = TRUE;
944 priv->current_repeat = 0;
947 _clutter_master_clock_remove_timeline (master_clock, timeline);
951 clutter_timeline_do_frame (ClutterTimeline *timeline)
953 ClutterTimelinePrivate *priv;
955 priv = timeline->priv;
957 g_object_ref (timeline);
959 CLUTTER_NOTE (SCHEDULER, "Timeline [%p] activated (elapsed time: %ld)\n",
961 (long) priv->elapsed_time);
964 if (priv->direction == CLUTTER_TIMELINE_FORWARD)
965 priv->elapsed_time += priv->msecs_delta;
967 priv->elapsed_time -= priv->msecs_delta;
969 /* If we have not reached the end of the timeline: */
970 if (!is_complete (timeline))
972 /* Emit the signal */
973 emit_frame_signal (timeline);
974 check_markers (timeline, priv->msecs_delta);
976 g_object_unref (timeline);
978 return priv->is_playing;
982 /* Handle loop or stop */
983 ClutterTimelineDirection saved_direction = priv->direction;
984 gint elapsed_time_delta = priv->msecs_delta;
985 guint overflow_msecs = priv->elapsed_time;
988 /* Update the current elapsed time in case the signal handlers
989 * want to take a peek. If we clamp elapsed time, then we need
990 * to correpondingly reduce elapsed_time_delta to reflect the correct
992 if (priv->direction == CLUTTER_TIMELINE_FORWARD)
994 elapsed_time_delta -= (priv->elapsed_time - priv->duration);
995 priv->elapsed_time = priv->duration;
997 else if (priv->direction == CLUTTER_TIMELINE_BACKWARD)
999 elapsed_time_delta -= - priv->elapsed_time;
1000 priv->elapsed_time = 0;
1003 end_msecs = priv->elapsed_time;
1005 /* Emit the signal */
1006 emit_frame_signal (timeline);
1007 check_markers (timeline, elapsed_time_delta);
1009 /* Did the signal handler modify the elapsed time? */
1010 if (priv->elapsed_time != end_msecs)
1012 g_object_unref (timeline);
1016 /* Note: If the new-frame signal handler paused the timeline
1017 * on the last frame we will still go ahead and send the
1018 * completed signal */
1019 CLUTTER_NOTE (SCHEDULER,
1020 "Timeline [%p] completed (cur: %ld, tot: %ld)",
1022 (long) priv->elapsed_time,
1023 (long) priv->msecs_delta);
1025 if (priv->is_playing &&
1026 (priv->repeat_count == 0 ||
1027 priv->repeat_count == priv->current_repeat))
1029 /* We stop the timeline now, so that the completed signal handler
1030 * may choose to re-start the timeline
1032 * XXX Perhaps we should do this earlier, and regardless of
1033 * priv->repeat_count. Are we limiting the things that could be
1034 * done in the above new-frame signal handler?
1036 set_is_playing (timeline, FALSE);
1037 g_signal_emit (timeline, timeline_signals[STOPPED], 0, TRUE);
1040 g_signal_emit (timeline, timeline_signals[COMPLETED], 0);
1042 priv->current_repeat += 1;
1044 if (priv->auto_reverse)
1046 /* :auto-reverse changes the direction of the timeline */
1047 if (priv->direction == CLUTTER_TIMELINE_FORWARD)
1048 priv->direction = CLUTTER_TIMELINE_BACKWARD;
1050 priv->direction = CLUTTER_TIMELINE_FORWARD;
1052 g_object_notify_by_pspec (G_OBJECT (timeline),
1053 obj_props[PROP_DIRECTION]);
1056 /* Again check to see if the user has manually played with
1057 * the elapsed time, before we finally stop or loop the timeline */
1059 if (priv->elapsed_time != end_msecs &&
1060 !(/* Except allow changing time from 0 -> duration (or vice-versa)
1061 since these are considered equivalent */
1062 (priv->elapsed_time == 0 && end_msecs == priv->duration) ||
1063 (priv->elapsed_time == priv->duration && end_msecs == 0)
1066 g_object_unref (timeline);
1070 if (priv->repeat_count != 0)
1072 /* We try and interpolate smoothly around a loop */
1073 if (saved_direction == CLUTTER_TIMELINE_FORWARD)
1074 priv->elapsed_time = overflow_msecs - priv->duration;
1076 priv->elapsed_time = priv->duration + overflow_msecs;
1078 /* Or if the direction changed, we try and bounce */
1079 if (priv->direction != saved_direction)
1080 priv->elapsed_time = priv->duration - priv->elapsed_time;
1082 /* If we have overflowed then we are changing the elapsed
1083 time without emitting the new frame signal so we need to
1084 check for markers again */
1085 check_markers (timeline,
1086 priv->direction == CLUTTER_TIMELINE_FORWARD
1087 ? priv->elapsed_time
1088 : priv->duration - priv->elapsed_time);
1090 g_object_unref (timeline);
1095 clutter_timeline_rewind (timeline);
1097 g_object_unref (timeline);
1104 delay_timeout_func (gpointer data)
1106 ClutterTimeline *timeline = data;
1107 ClutterTimelinePrivate *priv = timeline->priv;
1110 priv->msecs_delta = 0;
1111 set_is_playing (timeline, TRUE);
1113 g_signal_emit (timeline, timeline_signals[STARTED], 0);
1119 * clutter_timeline_start:
1120 * @timeline: A #ClutterTimeline
1122 * Starts the #ClutterTimeline playing.
1125 clutter_timeline_start (ClutterTimeline *timeline)
1127 ClutterTimelinePrivate *priv;
1129 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1131 priv = timeline->priv;
1133 if (priv->delay_id || priv->is_playing)
1136 if (priv->duration == 0)
1140 priv->delay_id = clutter_threads_add_timeout (priv->delay,
1145 priv->msecs_delta = 0;
1146 set_is_playing (timeline, TRUE);
1148 g_signal_emit (timeline, timeline_signals[STARTED], 0);
1153 * clutter_timeline_pause:
1154 * @timeline: A #ClutterTimeline
1156 * Pauses the #ClutterTimeline on current frame
1159 clutter_timeline_pause (ClutterTimeline *timeline)
1161 ClutterTimelinePrivate *priv;
1163 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1165 priv = timeline->priv;
1167 if (priv->delay_id == 0 && !priv->is_playing)
1172 g_source_remove (priv->delay_id);
1176 priv->msecs_delta = 0;
1177 set_is_playing (timeline, FALSE);
1179 g_signal_emit (timeline, timeline_signals[PAUSED], 0);
1183 * clutter_timeline_stop:
1184 * @timeline: A #ClutterTimeline
1186 * Stops the #ClutterTimeline and moves to frame 0
1189 clutter_timeline_stop (ClutterTimeline *timeline)
1191 gboolean was_playing;
1193 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1195 /* we check the is_playing here because pause() will return immediately
1196 * if the timeline wasn't playing, so we don't know if it was actually
1197 * stopped, and yet we still don't want to emit a ::stopped signal if
1198 * the timeline was not playing in the first place.
1200 was_playing = timeline->priv->is_playing;
1202 clutter_timeline_pause (timeline);
1203 clutter_timeline_rewind (timeline);
1206 g_signal_emit (timeline, timeline_signals[STOPPED], 0, FALSE);
1210 * clutter_timeline_set_loop:
1211 * @timeline: a #ClutterTimeline
1212 * @loop: %TRUE for enable looping
1214 * Sets whether @timeline should loop.
1216 * This function is equivalent to calling clutter_timeline_set_repeat_count()
1217 * with -1 if @loop is %TRUE, and with 0 if @loop is %FALSE.
1219 * Deprecated: 1.10: Use clutter_timeline_set_repeat_count() instead.
1222 clutter_timeline_set_loop (ClutterTimeline *timeline,
1225 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1227 clutter_timeline_set_loop_internal (timeline, loop);
1231 * clutter_timeline_get_loop:
1232 * @timeline: a #ClutterTimeline
1234 * Gets whether @timeline is looping
1236 * Return value: %TRUE if the timeline is looping
1238 * Deprecated: 1.10: Use clutter_timeline_get_repeat_count() instead.
1241 clutter_timeline_get_loop (ClutterTimeline *timeline)
1243 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), FALSE);
1245 return timeline->priv->repeat_count != 0;
1249 * clutter_timeline_rewind:
1250 * @timeline: A #ClutterTimeline
1252 * Rewinds #ClutterTimeline to the first frame if its direction is
1253 * %CLUTTER_TIMELINE_FORWARD and the last frame if it is
1254 * %CLUTTER_TIMELINE_BACKWARD.
1257 clutter_timeline_rewind (ClutterTimeline *timeline)
1259 ClutterTimelinePrivate *priv;
1261 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1263 priv = timeline->priv;
1265 if (priv->direction == CLUTTER_TIMELINE_FORWARD)
1266 clutter_timeline_advance (timeline, 0);
1267 else if (priv->direction == CLUTTER_TIMELINE_BACKWARD)
1268 clutter_timeline_advance (timeline, priv->duration);
1272 * clutter_timeline_skip:
1273 * @timeline: A #ClutterTimeline
1274 * @msecs: Amount of time to skip
1276 * Advance timeline by the requested time in milliseconds
1279 clutter_timeline_skip (ClutterTimeline *timeline,
1282 ClutterTimelinePrivate *priv;
1284 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1286 priv = timeline->priv;
1288 if (priv->direction == CLUTTER_TIMELINE_FORWARD)
1290 priv->elapsed_time += msecs;
1292 if (priv->elapsed_time > priv->duration)
1293 priv->elapsed_time = 1;
1295 else if (priv->direction == CLUTTER_TIMELINE_BACKWARD)
1297 priv->elapsed_time -= msecs;
1299 if (priv->elapsed_time < 1)
1300 priv->elapsed_time = priv->duration - 1;
1303 priv->msecs_delta = 0;
1307 * clutter_timeline_advance:
1308 * @timeline: A #ClutterTimeline
1309 * @msecs: Time to advance to
1311 * Advance timeline to the requested point. The point is given as a
1312 * time in milliseconds since the timeline started.
1314 * <note><para>The @timeline will not emit the #ClutterTimeline::new-frame
1315 * signal for the given time. The first ::new-frame signal after the call to
1316 * clutter_timeline_advance() will be emit the skipped markers.
1320 clutter_timeline_advance (ClutterTimeline *timeline,
1323 ClutterTimelinePrivate *priv;
1325 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1327 priv = timeline->priv;
1329 priv->elapsed_time = CLAMP (msecs, 0, priv->duration);
1333 * clutter_timeline_get_elapsed_time:
1334 * @timeline: A #ClutterTimeline
1336 * Request the current time position of the timeline.
1338 * Return value: current elapsed time in milliseconds.
1341 clutter_timeline_get_elapsed_time (ClutterTimeline *timeline)
1343 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0);
1345 return timeline->priv->elapsed_time;
1349 * clutter_timeline_is_playing:
1350 * @timeline: A #ClutterTimeline
1352 * Queries state of a #ClutterTimeline.
1354 * Return value: %TRUE if timeline is currently playing
1357 clutter_timeline_is_playing (ClutterTimeline *timeline)
1359 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), FALSE);
1361 return timeline->priv->is_playing;
1365 * clutter_timeline_clone:
1366 * @timeline: #ClutterTimeline to duplicate.
1368 * Create a new #ClutterTimeline instance which has property values
1369 * matching that of supplied timeline. The cloned timeline will not
1370 * be started and will not be positioned to the current position of
1371 * the original @timeline: you will have to start it with clutter_timeline_start().
1373 * <note><para>The only cloned properties are:</para>
1375 * <listitem><simpara>#ClutterTimeline:duration</simpara></listitem>
1376 * <listitem><simpara>#ClutterTimeline:loop</simpara></listitem>
1377 * <listitem><simpara>#ClutterTimeline:delay</simpara></listitem>
1378 * <listitem><simpara>#ClutterTimeline:direction</simpara></listitem>
1379 * </itemizedlist></note>
1381 * Return value: (transfer full): a new #ClutterTimeline, cloned
1386 * Deprecated: 1.10: Use clutter_timeline_new() or g_object_new()
1390 clutter_timeline_clone (ClutterTimeline *timeline)
1392 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), NULL);
1394 return g_object_new (CLUTTER_TYPE_TIMELINE,
1395 "duration", timeline->priv->duration,
1396 "loop", timeline->priv->repeat_count != 0,
1397 "delay", timeline->priv->delay,
1398 "direction", timeline->priv->direction,
1403 * clutter_timeline_new:
1404 * @msecs: Duration of the timeline in milliseconds
1406 * Creates a new #ClutterTimeline with a duration of @msecs.
1408 * Return value: the newly created #ClutterTimeline instance. Use
1409 * g_object_unref() when done using it
1414 clutter_timeline_new (guint msecs)
1416 return g_object_new (CLUTTER_TYPE_TIMELINE,
1422 * clutter_timeline_get_delay:
1423 * @timeline: a #ClutterTimeline
1425 * Retrieves the delay set using clutter_timeline_set_delay().
1427 * Return value: the delay in milliseconds.
1432 clutter_timeline_get_delay (ClutterTimeline *timeline)
1434 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0);
1436 return timeline->priv->delay;
1440 * clutter_timeline_set_delay:
1441 * @timeline: a #ClutterTimeline
1442 * @msecs: delay in milliseconds
1444 * Sets the delay, in milliseconds, before @timeline should start.
1449 clutter_timeline_set_delay (ClutterTimeline *timeline,
1452 ClutterTimelinePrivate *priv;
1454 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1456 priv = timeline->priv;
1458 if (priv->delay != msecs)
1460 priv->delay = msecs;
1461 g_object_notify_by_pspec (G_OBJECT (timeline), obj_props[PROP_DELAY]);
1466 * clutter_timeline_get_duration:
1467 * @timeline: a #ClutterTimeline
1469 * Retrieves the duration of a #ClutterTimeline in milliseconds.
1470 * See clutter_timeline_set_duration().
1472 * Return value: the duration of the timeline, in milliseconds.
1477 clutter_timeline_get_duration (ClutterTimeline *timeline)
1479 ClutterTimelinePrivate *priv;
1481 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0);
1483 priv = timeline->priv;
1485 return priv->duration;
1489 * clutter_timeline_set_duration:
1490 * @timeline: a #ClutterTimeline
1491 * @msecs: duration of the timeline in milliseconds
1493 * Sets the duration of the timeline, in milliseconds. The speed
1494 * of the timeline depends on the ClutterTimeline:fps setting.
1499 clutter_timeline_set_duration (ClutterTimeline *timeline,
1502 ClutterTimelinePrivate *priv;
1504 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1505 g_return_if_fail (msecs > 0);
1507 priv = timeline->priv;
1509 if (priv->duration != msecs)
1511 priv->duration = msecs;
1513 g_object_notify_by_pspec (G_OBJECT (timeline), obj_props[PROP_DURATION]);
1518 * clutter_timeline_get_progress:
1519 * @timeline: a #ClutterTimeline
1521 * The position of the timeline in a normalized [-1, 2] interval.
1523 * The return value of this function is determined by the progress
1524 * mode set using clutter_timeline_set_progress_mode(), or by the
1525 * progress function set using clutter_timeline_set_progress_func().
1527 * Return value: the normalized current position in the timeline.
1532 clutter_timeline_get_progress (ClutterTimeline *timeline)
1534 ClutterTimelinePrivate *priv;
1536 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0.0);
1538 priv = timeline->priv;
1540 /* short-circuit linear progress */
1541 if (priv->progress_func == NULL)
1542 return (gdouble) priv->elapsed_time / (gdouble) priv->duration;
1544 return priv->progress_func (timeline,
1545 (gdouble) priv->elapsed_time,
1546 (gdouble) priv->duration,
1547 priv->progress_data);
1551 * clutter_timeline_get_direction:
1552 * @timeline: a #ClutterTimeline
1554 * Retrieves the direction of the timeline set with
1555 * clutter_timeline_set_direction().
1557 * Return value: the direction of the timeline
1561 ClutterTimelineDirection
1562 clutter_timeline_get_direction (ClutterTimeline *timeline)
1564 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline),
1565 CLUTTER_TIMELINE_FORWARD);
1567 return timeline->priv->direction;
1571 * clutter_timeline_set_direction:
1572 * @timeline: a #ClutterTimeline
1573 * @direction: the direction of the timeline
1575 * Sets the direction of @timeline, either %CLUTTER_TIMELINE_FORWARD or
1576 * %CLUTTER_TIMELINE_BACKWARD.
1581 clutter_timeline_set_direction (ClutterTimeline *timeline,
1582 ClutterTimelineDirection direction)
1584 ClutterTimelinePrivate *priv;
1586 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1588 priv = timeline->priv;
1590 if (priv->direction != direction)
1592 priv->direction = direction;
1594 if (priv->elapsed_time == 0)
1595 priv->elapsed_time = priv->duration;
1597 g_object_notify_by_pspec (G_OBJECT (timeline), obj_props[PROP_DIRECTION]);
1602 * clutter_timeline_get_delta:
1603 * @timeline: a #ClutterTimeline
1605 * Retrieves the amount of time elapsed since the last
1606 * ClutterTimeline::new-frame signal.
1608 * This function is only useful inside handlers for the ::new-frame
1609 * signal, and its behaviour is undefined if the timeline is not
1612 * Return value: the amount of time in milliseconds elapsed since the
1618 clutter_timeline_get_delta (ClutterTimeline *timeline)
1620 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0);
1622 if (!clutter_timeline_is_playing (timeline))
1625 return timeline->priv->msecs_delta;
1629 _clutter_timeline_advance (ClutterTimeline *timeline,
1632 ClutterTimelinePrivate *priv = timeline->priv;
1634 g_object_ref (timeline);
1636 priv->msecs_delta = tick_time;
1637 priv->is_playing = TRUE;
1639 clutter_timeline_do_frame (timeline);
1641 priv->is_playing = FALSE;
1643 g_object_unref (timeline);
1647 * clutter_timeline_do_tick
1648 * @timeline: a #ClutterTimeline
1649 * @tick_time: time of advance
1651 * Advances @timeline based on the time passed in @tick_time. This
1652 * function is called by the master clock. The @timeline will use this
1653 * interval to emit the #ClutterTimeline::new-frame signal and
1654 * eventually skip frames.
1657 _clutter_timeline_do_tick (ClutterTimeline *timeline,
1660 ClutterTimelinePrivate *priv;
1662 priv = timeline->priv;
1664 /* Check the is_playing variable before performing the timeline tick.
1665 * This is necessary, as if a timeline is stopped in response to a
1666 * master-clock generated signal of a different timeline, this code can
1669 if (!priv->is_playing)
1672 if (priv->waiting_first_tick)
1674 priv->last_frame_time = tick_time;
1675 priv->msecs_delta = 0;
1676 priv->waiting_first_tick = FALSE;
1677 clutter_timeline_do_frame (timeline);
1683 msecs = tick_time - priv->last_frame_time;
1685 /* if the clock rolled back between ticks we need to
1686 * account for it; the best course of action, since the
1687 * clock roll back can happen by any arbitrary amount
1688 * of milliseconds, is to drop a frame here
1692 priv->last_frame_time = tick_time;
1698 /* Avoid accumulating error */
1699 priv->last_frame_time += msecs;
1700 priv->msecs_delta = msecs;
1701 clutter_timeline_do_frame (timeline);
1707 * clutter_timeline_add_marker_at_time:
1708 * @timeline: a #ClutterTimeline
1709 * @marker_name: the unique name for this marker
1710 * @msecs: position of the marker in milliseconds
1712 * Adds a named marker that will be hit when the timeline has been
1713 * running for @msecs milliseconds. Markers are unique string
1714 * identifiers for a given time. Once @timeline reaches
1715 * @msecs, it will emit a ::marker-reached signal for each marker
1716 * attached to that time.
1718 * A marker can be removed with clutter_timeline_remove_marker(). The
1719 * timeline can be advanced to a marker using
1720 * clutter_timeline_advance_to_marker().
1725 clutter_timeline_add_marker_at_time (ClutterTimeline *timeline,
1726 const gchar *marker_name,
1729 TimelineMarker *marker;
1731 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1732 g_return_if_fail (marker_name != NULL);
1733 g_return_if_fail (msecs <= clutter_timeline_get_duration (timeline));
1735 marker = timeline_marker_new (marker_name, msecs);
1736 clutter_timeline_add_marker_internal (timeline, marker);
1739 struct CollectMarkersClosure
1746 collect_markers (const gchar *key,
1747 TimelineMarker *marker,
1748 struct CollectMarkersClosure *data)
1750 if (marker->msecs == data->msecs)
1752 gchar *name_copy = g_strdup (key);
1753 g_array_append_val (data->markers, name_copy);
1758 * clutter_timeline_list_markers:
1759 * @timeline: a #ClutterTimeline
1760 * @msecs: the time to check, or -1
1761 * @n_markers: the number of markers returned
1763 * Retrieves the list of markers at time @msecs. If @msecs is a
1764 * negative integer, all the markers attached to @timeline will be
1767 * Return value: (transfer full) (array zero-terminated=1 length=n_markers):
1768 * a newly allocated, %NULL terminated string array containing the names
1769 * of the markers. Use g_strfreev() when done.
1774 clutter_timeline_list_markers (ClutterTimeline *timeline,
1778 ClutterTimelinePrivate *priv;
1779 gchar **retval = NULL;
1782 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), NULL);
1784 priv = timeline->priv;
1786 if (G_UNLIKELY (priv->markers_by_name == NULL))
1798 markers = g_hash_table_get_keys (priv->markers_by_name);
1799 retval = g_new0 (gchar*, g_list_length (markers) + 1);
1801 for (i = 0, l = markers; l != NULL; i++, l = l->next)
1802 retval[i] = g_strdup (l->data);
1804 g_list_free (markers);
1808 struct CollectMarkersClosure data;
1811 data.markers = g_array_new (TRUE, FALSE, sizeof (gchar *));
1813 g_hash_table_foreach (priv->markers_by_name,
1814 (GHFunc) collect_markers,
1817 i = data.markers->len;
1818 retval = (gchar **) (void *) g_array_free (data.markers, FALSE);
1828 * clutter_timeline_advance_to_marker:
1829 * @timeline: a #ClutterTimeline
1830 * @marker_name: the name of the marker
1832 * Advances @timeline to the time of the given @marker_name.
1834 * <note><para>Like clutter_timeline_advance(), this function will not
1835 * emit the #ClutterTimeline::new-frame for the time where @marker_name
1836 * is set, nor it will emit #ClutterTimeline::marker-reached for
1837 * @marker_name.</para></note>
1842 clutter_timeline_advance_to_marker (ClutterTimeline *timeline,
1843 const gchar *marker_name)
1845 ClutterTimelinePrivate *priv;
1846 TimelineMarker *marker;
1848 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1849 g_return_if_fail (marker_name != NULL);
1851 priv = timeline->priv;
1853 if (G_UNLIKELY (priv->markers_by_name == NULL))
1855 g_warning ("No marker named '%s' found.", marker_name);
1859 marker = g_hash_table_lookup (priv->markers_by_name, marker_name);
1862 g_warning ("No marker named '%s' found.", marker_name);
1866 clutter_timeline_advance (timeline, marker->msecs);
1870 * clutter_timeline_remove_marker:
1871 * @timeline: a #ClutterTimeline
1872 * @marker_name: the name of the marker to remove
1874 * Removes @marker_name, if found, from @timeline.
1879 clutter_timeline_remove_marker (ClutterTimeline *timeline,
1880 const gchar *marker_name)
1882 ClutterTimelinePrivate *priv;
1883 TimelineMarker *marker;
1885 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1886 g_return_if_fail (marker_name != NULL);
1888 priv = timeline->priv;
1890 if (G_UNLIKELY (priv->markers_by_name == NULL))
1892 g_warning ("No marker named '%s' found.", marker_name);
1896 marker = g_hash_table_lookup (priv->markers_by_name, marker_name);
1899 g_warning ("No marker named '%s' found.", marker_name);
1903 /* this will take care of freeing the marker as well */
1904 g_hash_table_remove (priv->markers_by_name, marker_name);
1908 * clutter_timeline_has_marker:
1909 * @timeline: a #ClutterTimeline
1910 * @marker_name: the name of the marker
1912 * Checks whether @timeline has a marker set with the given name.
1914 * Return value: %TRUE if the marker was found
1919 clutter_timeline_has_marker (ClutterTimeline *timeline,
1920 const gchar *marker_name)
1922 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), FALSE);
1923 g_return_val_if_fail (marker_name != NULL, FALSE);
1925 if (G_UNLIKELY (timeline->priv->markers_by_name == NULL))
1928 return NULL != g_hash_table_lookup (timeline->priv->markers_by_name,
1933 * clutter_timeline_set_auto_reverse:
1934 * @timeline: a #ClutterTimeline
1935 * @reverse: %TRUE if the @timeline should reverse the direction
1937 * Sets whether @timeline should reverse the direction after the
1938 * emission of the #ClutterTimeline::completed signal.
1940 * Setting the #ClutterTimeline:auto-reverse property to %TRUE is the
1941 * equivalent of connecting a callback to the #ClutterTimeline::completed
1942 * signal and changing the direction of the timeline from that callback;
1943 * for instance, this code:
1947 * reverse_timeline (ClutterTimeline *timeline)
1949 * ClutterTimelineDirection dir = clutter_timeline_get_direction (timeline);
1951 * if (dir == CLUTTER_TIMELINE_FORWARD)
1952 * dir = CLUTTER_TIMELINE_BACKWARD;
1954 * dir = CLUTTER_TIMELINE_FORWARD;
1956 * clutter_timeline_set_direction (timeline, dir);
1959 * timeline = clutter_timeline_new (1000);
1960 * clutter_timeline_set_repeat_count (timeline, -1);
1961 * g_signal_connect (timeline, "completed",
1962 * G_CALLBACK (reverse_timeline),
1966 * can be effectively replaced by:
1969 * timeline = clutter_timeline_new (1000);
1970 * clutter_timeline_set_repeat_count (timeline, -1);
1971 * clutter_timeline_set_auto_reverse (timeline);
1977 clutter_timeline_set_auto_reverse (ClutterTimeline *timeline,
1980 ClutterTimelinePrivate *priv;
1982 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
1984 reverse = !!reverse;
1986 priv = timeline->priv;
1988 if (priv->auto_reverse != reverse)
1990 priv->auto_reverse = reverse;
1992 g_object_notify_by_pspec (G_OBJECT (timeline),
1993 obj_props[PROP_AUTO_REVERSE]);
1998 * clutter_timeline_get_auto_reverse:
1999 * @timeline: a #ClutterTimeline
2001 * Retrieves the value set by clutter_timeline_set_auto_reverse().
2003 * Return value: %TRUE if the timeline should automatically reverse, and
2009 clutter_timeline_get_auto_reverse (ClutterTimeline *timeline)
2011 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), FALSE);
2013 return timeline->priv->auto_reverse;
2017 * clutter_timeline_set_repeat_count:
2018 * @timeline: a #ClutterTimeline
2019 * @count: the number of times the timeline should repeat
2021 * Sets the number of times the @timeline should repeat.
2023 * If @count is 0, the timeline never repeats.
2025 * If @count is -1, the timeline will always repeat until
2031 clutter_timeline_set_repeat_count (ClutterTimeline *timeline,
2034 ClutterTimelinePrivate *priv;
2036 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
2037 g_return_if_fail (count >= -1);
2039 priv = timeline->priv;
2041 if (priv->repeat_count != count)
2043 priv->repeat_count = count;
2045 g_object_notify_by_pspec (G_OBJECT (timeline),
2046 obj_props[PROP_REPEAT_COUNT]);
2051 * clutter_timeline_get_repeat_count:
2052 * @timeline: a #ClutterTimeline
2054 * Retrieves the number set using clutter_timeline_set_repeat_count().
2056 * Return value: the number of repeats
2061 clutter_timeline_get_repeat_count (ClutterTimeline *timeline)
2063 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0);
2065 return timeline->priv->repeat_count;
2069 * clutter_timeline_set_progress_func:
2070 * @timeline: a #ClutterTimeline
2071 * @func: (scope notified) (allow-none): a progress function, or %NULL
2072 * @data: (closure): data to pass to @func
2073 * @notify: a function to be called when the progress function is removed
2074 * or the timeline is disposed
2076 * Sets a custom progress function for @timeline. The progress function will
2077 * be called by clutter_timeline_get_progress() and will be used to compute
2078 * the progress value based on the elapsed time and the total duration of the
2081 * If @func is not %NULL, the #ClutterTimeline:progress-mode property will
2082 * be set to %CLUTTER_CUSTOM_MODE.
2084 * If @func is %NULL, any previously set progress function will be unset, and
2085 * the #ClutterTimeline:progress-mode property will be set to %CLUTTER_LINEAR.
2090 clutter_timeline_set_progress_func (ClutterTimeline *timeline,
2091 ClutterTimelineProgressFunc func,
2093 GDestroyNotify notify)
2095 ClutterTimelinePrivate *priv;
2097 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
2099 priv = timeline->priv;
2101 if (priv->progress_notify != NULL)
2102 priv->progress_notify (priv->progress_data);
2104 priv->progress_func = func;
2105 priv->progress_data = data;
2106 priv->progress_notify = notify;
2108 if (priv->progress_func != NULL)
2109 priv->progress_mode = CLUTTER_CUSTOM_MODE;
2111 priv->progress_mode = CLUTTER_LINEAR;
2113 g_object_notify_by_pspec (G_OBJECT (timeline), obj_props[PROP_PROGRESS_MODE]);
2117 clutter_timeline_progress_func (ClutterTimeline *timeline,
2120 gpointer user_data G_GNUC_UNUSED)
2122 ClutterTimelinePrivate *priv = timeline->priv;
2124 return clutter_easing_for_mode (priv->progress_mode, elapsed, duration);
2128 * clutter_timeline_set_progress_mode:
2129 * @timeline: a #ClutterTimeline
2130 * @mode: the progress mode, as a #ClutterAnimationMode
2132 * Sets the progress function using a value from the #ClutterAnimationMode
2133 * enumeration. The @mode cannot be %CLUTTER_CUSTOM_MODE or bigger than
2134 * %CLUTTER_ANIMATION_LAST.
2139 clutter_timeline_set_progress_mode (ClutterTimeline *timeline,
2140 ClutterAnimationMode mode)
2142 ClutterTimelinePrivate *priv;
2144 g_return_if_fail (CLUTTER_IS_TIMELINE (timeline));
2145 g_return_if_fail (mode < CLUTTER_ANIMATION_LAST);
2146 g_return_if_fail (mode != CLUTTER_CUSTOM_MODE);
2148 priv = timeline->priv;
2150 if (priv->progress_mode == mode)
2153 if (priv->progress_notify != NULL)
2154 priv->progress_notify (priv->progress_data);
2156 priv->progress_mode = mode;
2158 /* short-circuit linear progress */
2159 if (priv->progress_mode != CLUTTER_LINEAR)
2160 priv->progress_func = clutter_timeline_progress_func;
2162 priv->progress_func = NULL;
2164 priv->progress_data = NULL;
2165 priv->progress_notify = NULL;
2167 g_object_notify_by_pspec (G_OBJECT (timeline), obj_props[PROP_PROGRESS_MODE]);
2171 * clutter_timeline_get_progress_mode:
2172 * @timeline: a #ClutterTimeline
2174 * Retrieves the progress mode set using clutter_timeline_set_progress_mode()
2175 * or clutter_timeline_set_progress_func().
2177 * Return value: a #ClutterAnimationMode
2181 ClutterAnimationMode
2182 clutter_timeline_get_progress_mode (ClutterTimeline *timeline)
2184 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), CLUTTER_LINEAR);
2186 return timeline->priv->progress_mode;
2190 * clutter_timeline_get_duration_hint:
2191 * @timeline: a #ClutterTimeline
2193 * Retrieves the full duration of the @timeline, taking into account the
2194 * current value of the #ClutterTimeline:repeat-count property.
2196 * If the #ClutterTimeline:repeat-count property is set to -1, this function
2197 * will return %G_MAXINT64.
2199 * The returned value is to be considered a hint, and it's only valid
2200 * as long as the @timeline hasn't been changed.
2202 * Return value: the full duration of the #ClutterTimeline
2207 clutter_timeline_get_duration_hint (ClutterTimeline *timeline)
2209 ClutterTimelinePrivate *priv;
2211 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0);
2213 priv = timeline->priv;
2215 if (priv->repeat_count == 0)
2216 return priv->duration;
2217 else if (priv->repeat_count < 0)
2220 return priv->repeat_count * priv->duration;
2224 * clutter_timeline_get_current_repeat:
2225 * @timeline: a #ClutterTimeline
2227 * Retrieves the current repeat for a timeline.
2229 * Repeats start at 0.
2231 * Return value: the current repeat
2236 clutter_timeline_get_current_repeat (ClutterTimeline *timeline)
2238 g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), 0);
2240 return timeline->priv->current_repeat;