summaryrefslogtreecommitdiff
path: root/clutter/clutter-transition-group.c
blob: f479dc44c15bb9977ec2e5527a5a9fd6945eaa44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Emmanuele Bassi <ebassi@linux.intel.com>
 */

/**
 * SECTION:clutter-transition-group
 * @Title: ClutterTransitionGroup
 * @Short_Description: Group transitions together
 *
 * The #ClutterTransitionGroup allows running multiple #ClutterTransition
 * instances concurrently.
 *
 * The transitions inside a group will run within the boundaries of the
 * group; for instance, if a transition has a duration of 10 seconds, and
 * the group that contains it has a duration of 5 seconds, only the first
 * 5 seconds of the transition will be played.
 *
 * #ClutterTransitionGroup is available since Clutter 1.12
 */

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

#include "clutter-transition-group.h"

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

struct _ClutterTransitionGroupPrivate
{
  GHashTable *transitions;
};

G_DEFINE_TYPE_WITH_PRIVATE (ClutterTransitionGroup, clutter_transition_group, CLUTTER_TYPE_TRANSITION)

static void
clutter_transition_group_new_frame (ClutterTimeline *timeline,
                                    gint             elapsed)
{
  ClutterTransitionGroupPrivate *priv;
  GHashTableIter iter;
  gpointer element;
  gint64 msecs;

  priv = CLUTTER_TRANSITION_GROUP (timeline)->priv;

  /* get the time elapsed since the last ::new-frame... */
  msecs = clutter_timeline_get_delta (timeline);

  g_hash_table_iter_init (&iter, priv->transitions);
  while (g_hash_table_iter_next (&iter, &element, NULL))
    {
      ClutterTimeline *t = element;

      /* ... and advance every timeline */
      clutter_timeline_set_direction (t, clutter_timeline_get_direction (timeline));
      clutter_timeline_set_duration (t, clutter_timeline_get_duration (timeline));

      _clutter_timeline_advance (t, msecs);
    }
}

static void
clutter_transition_group_attached (ClutterTransition *transition,
                                   ClutterAnimatable *animatable)
{
  ClutterTransitionGroupPrivate *priv;
  GHashTableIter iter;
  gpointer element;

  priv = CLUTTER_TRANSITION_GROUP (transition)->priv;

  g_hash_table_iter_init (&iter, priv->transitions);
  while (g_hash_table_iter_next (&iter, &element, NULL))
    {
      ClutterTransition *t = element;

      clutter_transition_set_animatable (t, animatable);
    }
}

static void
clutter_transition_group_detached (ClutterTransition *transition,
                                   ClutterAnimatable *animatable)
{
  ClutterTransitionGroupPrivate *priv;
  GHashTableIter iter;
  gpointer element;

  priv = CLUTTER_TRANSITION_GROUP (transition)->priv;

  g_hash_table_iter_init (&iter, priv->transitions);
  while (g_hash_table_iter_next (&iter, &element, NULL))
    {
      ClutterTransition *t = element;

      clutter_transition_set_animatable (t, NULL);
    }
}

static void
clutter_transition_group_started (ClutterTimeline *timeline)
{
  ClutterTransitionGroupPrivate *priv;
  GHashTableIter iter;
  gpointer element;

  priv = CLUTTER_TRANSITION_GROUP (timeline)->priv;

  g_hash_table_iter_init (&iter, priv->transitions);
  while (g_hash_table_iter_next (&iter, &element, NULL))
    {
      ClutterTransition *t = element;

      g_signal_emit_by_name (t, "started");
    }
}

static void
clutter_transition_group_finalize (GObject *gobject)
{
  ClutterTransitionGroupPrivate *priv;

  priv = CLUTTER_TRANSITION_GROUP (gobject)->priv;

  g_hash_table_unref (priv->transitions);

  G_OBJECT_CLASS (clutter_transition_group_parent_class)->finalize (gobject);
}

static void
clutter_transition_group_class_init (ClutterTransitionGroupClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  ClutterTimelineClass *timeline_class = CLUTTER_TIMELINE_CLASS (klass);
  ClutterTransitionClass *transition_class = CLUTTER_TRANSITION_CLASS (klass);

  gobject_class->finalize = clutter_transition_group_finalize;

  timeline_class->started = clutter_transition_group_started;
  timeline_class->new_frame = clutter_transition_group_new_frame;

  transition_class->attached = clutter_transition_group_attached;
  transition_class->detached = clutter_transition_group_detached;
}

static void
clutter_transition_group_init (ClutterTransitionGroup *self)
{
  self->priv = clutter_transition_group_get_instance_private (self);
  self->priv->transitions =
    g_hash_table_new_full (NULL, NULL, (GDestroyNotify) g_object_unref, NULL);
}

/**
 * clutter_transition_group_new:
 *
 * Creates a new #ClutterTransitionGroup instance.
 *
 * Return value: the newly created #ClutterTransitionGroup. Use
 *   g_object_unref() when done to deallocate the resources it
 *   uses
 *
 * Since: 1.12
 */
ClutterTransition *
clutter_transition_group_new (void)
{
  return g_object_new (CLUTTER_TYPE_TRANSITION_GROUP, NULL);
}

/**
 * clutter_transition_group_add_transition:
 * @group: a #ClutterTransitionGroup
 * @transition: a #ClutterTransition
 *
 * Adds @transition to @group.
 *
 * This function acquires a reference on @transition that will be released
 * when calling clutter_transition_group_remove_transition().
 *
 * Since: 1.12
 */
void
clutter_transition_group_add_transition (ClutterTransitionGroup *group,
                                         ClutterTransition      *transition)
{
  g_return_if_fail (CLUTTER_IS_TRANSITION_GROUP (group));
  g_return_if_fail (CLUTTER_IS_TRANSITION (transition));

  g_hash_table_add (group->priv->transitions, g_object_ref (transition));
}

/**
 * clutter_transition_group_remove_transition:
 * @group: a #ClutterTransitionGroup
 * @transition: a #ClutterTransition
 *
 * Removes @transition from @group.
 *
 * This function releases the reference acquired on @transition when
 * calling clutter_transition_group_add_transition().
 *
 * Since: 1.12
 */
void
clutter_transition_group_remove_transition (ClutterTransitionGroup *group,
                                            ClutterTransition      *transition)
{
  g_return_if_fail (CLUTTER_IS_TRANSITION_GROUP (group));

  g_hash_table_remove (group->priv->transitions, transition);
}

/**
 * clutter_transition_group_remove_all:
 * @group: a #ClutterTransitionGroup
 *
 * Removes all transitions from @group.
 *
 * This function releases the reference acquired when calling
 * clutter_transition_group_add_transition().
 *
 * Since: 1.12
 */
void
clutter_transition_group_remove_all (ClutterTransitionGroup *group)
{
  g_return_if_fail (CLUTTER_IS_TRANSITION_GROUP (group));

  g_hash_table_remove_all (group->priv->transitions);
}