clutter-alpha.c 36.6 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Authored By Matthew Allum  <mallum@openedhand.com>
 *             Jorn Baayen  <jorn@openedhand.com>
8
 *             Emmanuele Bassi  <ebassi@openedhand.com>
9
 *             Tomas Frydrych <tf@openedhand.com>
10
 *
11
 * Copyright (C) 2006, 2007, 2008 OpenedHand
12
 * Copyright (C) 2009, 2010 Intel Corp.
13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
25
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
26 27 28 29
 */

/**
 * SECTION:clutter-alpha
30
 * @short_description: A class for calculating a value as a function of time
31
 *
32 33
 * #ClutterAlpha is a class for calculating an floating point value
 * dependent only on the position of a #ClutterTimeline.
34 35 36 37 38 39 40
 *
 * A #ClutterAlpha binds a #ClutterTimeline to a progress function which
 * translates the time T into an adimensional factor alpha. The factor can
 * then be used to drive a #ClutterBehaviour, which will translate the
 * alpha value into something meaningful for a #ClutterActor.
 *
 * You should provide a #ClutterTimeline and bind it to the #ClutterAlpha
41
 * instance using clutter_alpha_set_timeline(). You should also set an
42
 * "animation mode", either by using the #ClutterAnimationMode values that
43 44
 * Clutter itself provides or by registering custom functions using
 * clutter_alpha_register_func().
45 46 47 48 49 50
 *
 * Instead of a #ClutterAnimationMode you may provide a function returning
 * the alpha value depending on the progress of the timeline, using
 * clutter_alpha_set_func() or clutter_alpha_set_closure(). The alpha
 * function will be executed each time a new frame in the #ClutterTimeline
 * is reached.
51 52 53 54
 *
 * Since the alpha function is controlled by the timeline instance, you can
 * pause, stop or resume the #ClutterAlpha from calling the alpha function by
 * using the appropriate functions of the #ClutterTimeline object.
55
 *
56 57
 * #ClutterAlpha is used to "drive" a #ClutterBehaviour instance, and it
 * is internally used by the #ClutterAnimation API.
58
 *
59 60 61 62 63
 * <figure id="easing-modes">
 *   <title>Easing modes provided by Clutter</title>
 *   <graphic fileref="easing-modes.png" format="PNG"/>
 * </figure>
 *
64 65 66 67 68 69 70 71 72 73 74
 * <refsect2 id="ClutterAlpha-script">
 *   <title>ClutterAlpha custom properties for #ClutterScript</title>
 *   <para>#ClutterAlpha defines a custom "function" property for
 *   #ClutterScript which allows to reference a custom alpha function
 *   available in the source code. Setting the "function" property
 *   is equivalent to calling clutter_alpha_set_func() with the
 *   specified function name. No user data or #GDestroyNotify is
 *   available to be passed.</para>
 *   <example id="ClutterAlpha-script-example">
 *     <title>Defining a ClutterAlpha in ClutterScript</title>
 *     <para>The following JSON fragment defines a #ClutterAlpha
75
 *     using a #ClutterTimeline with id "sine-timeline" and an alpha
76 77
 *     function called my_sine_alpha(). The defined #ClutterAlpha
 *     instance can be reused in multiple #ClutterBehaviour
78
 *     definitions or for #ClutterAnimation definitions.</para>
79 80 81 82 83 84 85 86 87 88 89
 *     <programlisting><![CDATA[
 * {
 *   "id" : "sine-alpha",
 *   "timeline" : {
 *     "id" : "sine-timeline",
 *     "duration" : 500,
 *     "loop" : true
 *   },
 *   "function" : "my_sine_alpha"
 * }
 *    ]]></programlisting>
90
 *   </example>
91 92 93 94
 *   <para>For the way to define the #ClutterAlpha:mode property
 *   inside a ClutterScript fragment, see <link
 *   linkend="clutter-AnimationMode-Script">the corresponding section</link>
 *   in #ClutterAnimation.</para>
95 96
 * </refsect2>
 *
97
 * Since: 0.2
98 99
 */

100 101 102
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
103

104 105
#include <math.h>

106 107
#include <gmodule.h>

108
#include "clutter-alpha.h"
109 110
#include "clutter-debug.h"
#include "clutter-enum-types.h"
111 112
#include "clutter-main.h"
#include "clutter-marshal.h"
113
#include "clutter-private.h"
114
#include "clutter-scriptable.h"
115
#include "clutter-script-private.h"
116

117
static void clutter_scriptable_iface_init (ClutterScriptableIface *iface);
118

119 120 121 122 123
G_DEFINE_TYPE_WITH_CODE (ClutterAlpha,
                         clutter_alpha,
                         G_TYPE_INITIALLY_UNOWNED,
                         G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_SCRIPTABLE,
                                                clutter_scriptable_iface_init));
124

125
struct _ClutterAlphaPrivate
126 127
{
  ClutterTimeline *timeline;
128 129
  guint timeline_new_frame_id;

130
  gdouble alpha;
131 132

  GClosure *closure;
133

134
  gulong mode;
135 136 137 138 139
};

enum
{
  PROP_0,
140
  
141
  PROP_TIMELINE,
142 143
  PROP_ALPHA,
  PROP_MODE
144 145 146 147 148 149 150
};

static void
timeline_new_frame_cb (ClutterTimeline *timeline,
                       guint            current_frame_num,
                       ClutterAlpha    *alpha)
{
151
  ClutterAlphaPrivate *priv = alpha->priv;
152

153
  /* Update alpha value and notify */
154 155
  priv->alpha = clutter_alpha_get_alpha (alpha);
  g_object_notify (G_OBJECT (alpha), "alpha");
156 157 158 159 160 161 162 163
}

static void 
clutter_alpha_set_property (GObject      *object, 
			    guint         prop_id,
			    const GValue *value, 
			    GParamSpec   *pspec)
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
164
  ClutterAlpha *alpha = CLUTTER_ALPHA (object);
165 166 167 168 169 170

  switch (prop_id) 
    {
    case PROP_TIMELINE:
      clutter_alpha_set_timeline (alpha, g_value_get_object (value));
      break;
171

172
    case PROP_MODE:
173
      clutter_alpha_set_mode (alpha, g_value_get_ulong (value));
174
      break;
175

176 177 178 179 180 181 182 183 184 185 186 187
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void 
clutter_alpha_get_property (GObject    *object, 
			    guint       prop_id,
			    GValue     *value, 
			    GParamSpec *pspec)
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
188
  ClutterAlphaPrivate *priv = CLUTTER_ALPHA (object)->priv;
189 190 191 192 193 194

  switch (prop_id) 
    {
    case PROP_TIMELINE:
      g_value_set_object (value, priv->timeline);
      break;
195

196
    case PROP_ALPHA:
197
      g_value_set_double (value, priv->alpha);
198
      break;
199

200
    case PROP_MODE:
201
      g_value_set_ulong (value, priv->mode);
202
      break;
203

204 205 206 207 208 209 210 211 212
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void 
clutter_alpha_finalize (GObject *object)
{
213 214
  ClutterAlphaPrivate *priv = CLUTTER_ALPHA (object)->priv;

215
  if (priv->closure)
216
    g_closure_unref (priv->closure);
217

218 219 220 221 222 223
  G_OBJECT_CLASS (clutter_alpha_parent_class)->finalize (object);
}

static void 
clutter_alpha_dispose (GObject *object)
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
224
  ClutterAlpha *self = CLUTTER_ALPHA(object);
225 226 227 228 229 230

  clutter_alpha_set_timeline (self, NULL);

  G_OBJECT_CLASS (clutter_alpha_parent_class)->dispose (object);
}

231 232 233 234 235 236 237 238 239
static ClutterAlphaFunc
resolve_alpha_func (const gchar *name)
{
  static GModule *module = NULL;
  ClutterAlphaFunc func;

  CLUTTER_NOTE (SCRIPT, "Looking up '%s' alpha function", name);

  if (G_UNLIKELY (module == NULL))
240
    module = g_module_open (NULL, 0);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  if (g_module_symbol (module, name, (gpointer) &func))
    {
      CLUTTER_NOTE (SCRIPT, "Found '%s' alpha function in the symbols table",
                    name);
      return func;
    }

  return NULL;
}

static void
clutter_alpha_set_custom_property (ClutterScriptable *scriptable,
                                   ClutterScript     *script,
                                   const gchar       *name,
                                   const GValue      *value)
{
  if (strncmp (name, "function", 8) == 0)
    {
      g_assert (G_VALUE_HOLDS (value, G_TYPE_POINTER));
      if (g_value_get_pointer (value) != NULL)
        {
          clutter_alpha_set_func (CLUTTER_ALPHA (scriptable),
                                  g_value_get_pointer (value),
                                  NULL, NULL);
        }
    }
  else
    g_object_set_property (G_OBJECT (scriptable), name, value);
}

static gboolean
clutter_alpha_parse_custom_node (ClutterScriptable *scriptable,
                                 ClutterScript     *script,
                                 GValue            *value,
                                 const gchar       *name,
                                 JsonNode          *node)
{
  if (strncmp (name, "function", 8) == 0)
    {
      const gchar *func_name = json_node_get_string (node);

      g_value_init (value, G_TYPE_POINTER);
      g_value_set_pointer (value, resolve_alpha_func (func_name));

      return TRUE;
    }

289 290 291 292 293
  /* we need to do this because we use gulong in place
   * of ClutterAnimationMode for ClutterAlpha:mode
   */
  if (strncmp (name, "mode", 4) == 0)
    {
294
      gulong mode;
295

296
      mode = clutter_script_resolve_animation_mode (node);
297

298 299
      g_value_init (value, G_TYPE_ULONG);
      g_value_set_ulong (value, mode);
300

301
      return TRUE;
302 303
    }

304 305 306 307 308 309 310 311 312
  return FALSE;
}

static void
clutter_scriptable_iface_init (ClutterScriptableIface *iface)
{
  iface->parse_custom_node = clutter_alpha_parse_custom_node;
  iface->set_custom_property = clutter_alpha_set_custom_property;
}
313 314 315 316

static void
clutter_alpha_class_init (ClutterAlphaClass *klass)
{
317
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
318
  GParamSpec *pspec;
319 320 321 322 323 324 325 326

  object_class->set_property = clutter_alpha_set_property;
  object_class->get_property = clutter_alpha_get_property;
  object_class->finalize     = clutter_alpha_finalize;
  object_class->dispose      = clutter_alpha_dispose;

  g_type_class_add_private (klass, sizeof (ClutterAlphaPrivate));

327 328 329 330
  /**
   * ClutterAlpha:timeline:
   *
   * A #ClutterTimeline instance used to drive the alpha function.
331 332
   *
   * Since: 0.2
333
   */
334
  pspec = g_param_spec_object ("timeline",
335 336
                               P_("Timeline"),
                               P_("Timeline used by the alpha"),
337 338 339 340
                               CLUTTER_TYPE_TIMELINE,
                               CLUTTER_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_TIMELINE, pspec);

341 342 343
  /**
   * ClutterAlpha:alpha:
   *
344 345 346
   * The alpha value as computed by the alpha function. The linear
   * interval is 0.0 to 1.0, but the Alpha allows overshooting by
   * one unit in each direction, so the valid interval is -1.0 to 2.0.
347 348
   *
   * Since: 0.2
349
   */
350
  pspec = g_param_spec_double ("alpha",
351 352
                               P_("Alpha value"),
                               P_("Alpha value as computed by the alpha"),
353 354 355 356 357
                               -1.0, 2.0,
                               0.0,
                               CLUTTER_PARAM_READABLE);
  g_object_class_install_property (object_class, PROP_ALPHA, pspec);

358 359 360
  /**
   * ClutterAlpha:mode:
   *
361 362 363 364 365 366
   * The progress function logical id - either a value from the
   * #ClutterAnimationMode enumeration or a value returned by
   * clutter_alpha_register_func().
   *
   * If %CLUTTER_CUSTOM_MODE is used then the function set using
   * clutter_alpha_set_closure() or clutter_alpha_set_func()
367 368 369 370
   * will be used.
   *
   * Since: 1.0
   */
371
  pspec = g_param_spec_ulong ("mode",
372 373
                              P_("Mode"),
                              P_("Progress mode"),
374 375 376 377
                              0, G_MAXULONG,
                              CLUTTER_CUSTOM_MODE,
                              G_PARAM_CONSTRUCT | CLUTTER_PARAM_READWRITE);
  g_object_class_install_property (object_class, PROP_MODE, pspec);
378 379 380 381 382
}

static void
clutter_alpha_init (ClutterAlpha *self)
{
383 384 385
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
					    CLUTTER_TYPE_ALPHA,
					    ClutterAlphaPrivate);
386 387

  self->priv->mode = CLUTTER_CUSTOM_MODE;
388
  self->priv->alpha = 0.0;
389 390 391
}

/**
Emmanuele Bassi's avatar
Emmanuele Bassi committed
392
 * clutter_alpha_get_alpha:
393 394 395 396
 * @alpha: A #ClutterAlpha
 *
 * Query the current alpha value.
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
397
 * Return Value: The current alpha value for the alpha
398 399
 *
 * Since: 0.2
400
 */
401
gdouble
Emmanuele Bassi's avatar
Emmanuele Bassi committed
402
clutter_alpha_get_alpha (ClutterAlpha *alpha)
403
{
404
  ClutterAlphaPrivate *priv;
405
  gdouble retval = 0;
406 407 408 409 410 411 412 413 414 415 416 417

  g_return_val_if_fail (CLUTTER_IS_ALPHA (alpha), 0);

  priv = alpha->priv;

  if (G_LIKELY (priv->closure))
    {
      GValue params = { 0, };
      GValue result_value = { 0, };

      g_object_ref (alpha);

418
      g_value_init (&result_value, G_TYPE_DOUBLE);
419 420 421 422

      g_value_init (&params, CLUTTER_TYPE_ALPHA);
      g_value_set_object (&params, alpha);
		     
423
      g_closure_invoke (priv->closure, &result_value, 1, &params, NULL);
424

425
      retval = g_value_get_double (&result_value);
426 427 428 429 430 431 432 433

      g_value_unset (&result_value);
      g_value_unset (&params);

      g_object_unref (alpha);
    }

  return retval;
434 435
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/*
 * clutter_alpha_set_closure_internal:
 * @alpha: a #ClutterAlpha
 * @closure: a #GClosure
 *
 * Sets the @closure for @alpha. This function does not
 * set the #ClutterAlpha:mode property and does not emit
 * the #GObject::notify signal for it.
 */
static inline void
clutter_alpha_set_closure_internal (ClutterAlpha *alpha,
                                    GClosure     *closure)
{
  ClutterAlphaPrivate *priv = alpha->priv;

  if (priv->closure)
    g_closure_unref (priv->closure);

  /* need to take ownership of the closure before sinking it */
  priv->closure = g_closure_ref (closure);
  g_closure_sink (closure);

  /* set the marshaller */
  if (G_CLOSURE_NEEDS_MARSHAL (closure))
    {
Emmanuele Bassi's avatar
Emmanuele Bassi committed
461
      GClosureMarshal marshal = _clutter_marshal_DOUBLE__VOID;
462 463 464 465 466

      g_closure_set_marshal (closure, marshal);
    }
}

467 468 469 470 471
/**
 * clutter_alpha_set_closure:
 * @alpha: A #ClutterAlpha
 * @closure: A #GClosure
 *
472 473
 * Sets the #GClosure used to compute the alpha value at each
 * frame of the #ClutterTimeline bound to @alpha.
474 475 476 477 478 479 480 481 482 483 484 485 486 487
 *
 * Since: 0.8
 */
void
clutter_alpha_set_closure (ClutterAlpha *alpha,
                           GClosure     *closure)
{
  ClutterAlphaPrivate *priv;

  g_return_if_fail (CLUTTER_IS_ALPHA (alpha));
  g_return_if_fail (closure != NULL);

  priv = alpha->priv;

488
  clutter_alpha_set_closure_internal (alpha, closure);
489 490 491

  priv->mode = CLUTTER_CUSTOM_MODE;
  g_object_notify (G_OBJECT (alpha), "mode");
492 493
}

494 495 496
/**
 * clutter_alpha_set_func:
 * @alpha: A #ClutterAlpha
497
 * @func: A #ClutterAlphaFunc
498 499
 * @data: user data to be passed to the alpha function, or %NULL
 * @destroy: notify function used when disposing the alpha function
500
 *
501 502 503
 * Sets the #ClutterAlphaFunc function used to compute
 * the alpha value at each frame of the #ClutterTimeline
 * bound to @alpha.
504
 *
505 506
 * This function will not register @func as a global alpha function.
 *
507
 * Since: 0.2
508 509
 */
void
Emmanuele Bassi's avatar
Emmanuele Bassi committed
510
clutter_alpha_set_func (ClutterAlpha    *alpha,
511 512 513
		        ClutterAlphaFunc func,
                        gpointer         data,
                        GDestroyNotify   destroy)
514
{
515
  ClutterAlphaPrivate *priv;
516
  GClosure *closure;
517 518

  g_return_if_fail (CLUTTER_IS_ALPHA (alpha));
519
  g_return_if_fail (func != NULL);
520 521 522

  priv = alpha->priv;

523
  closure = g_cclosure_new (G_CALLBACK (func), data, (GClosureNotify) destroy);
524 525 526 527
  clutter_alpha_set_closure_internal (alpha, closure);

  priv->mode = CLUTTER_CUSTOM_MODE;
  g_object_notify (G_OBJECT (alpha), "mode");
528 529 530 531 532
}

/**
 * clutter_alpha_set_timeline:
 * @alpha: A #ClutterAlpha
Emmanuele Bassi's avatar
Emmanuele Bassi committed
533
 * @timeline: A #ClutterTimeline
534
 *
Emmanuele Bassi's avatar
Emmanuele Bassi committed
535
 * Binds @alpha to @timeline.
536 537
 *
 * Since: 0.2
538 539
 */
void
540
clutter_alpha_set_timeline (ClutterAlpha    *alpha,
541 542
                            ClutterTimeline *timeline)
{
543 544 545 546 547 548 549
  ClutterAlphaPrivate *priv;

  g_return_if_fail (CLUTTER_IS_ALPHA (alpha));
  g_return_if_fail (timeline == NULL || CLUTTER_IS_TIMELINE (timeline));
  
  priv = alpha->priv;

550 551 552
  if (priv->timeline == timeline)
    return;

553
  if (priv->timeline)
554
    {
555 556 557
      g_signal_handlers_disconnect_by_func (priv->timeline,
                                            timeline_new_frame_cb,
                                            alpha);
558

559 560
      g_object_unref (priv->timeline);
      priv->timeline = NULL;
561 562 563 564
    }

  if (timeline)
    {
565
      priv->timeline = g_object_ref (timeline);
566

567 568 569
      g_signal_connect (priv->timeline, "new-frame",
                        G_CALLBACK (timeline_new_frame_cb),
                        alpha);
570
    }
571 572

  g_object_notify (G_OBJECT (alpha), "timeline");
573 574 575 576 577 578
}

/**
 * clutter_alpha_get_timeline:
 * @alpha: A #ClutterAlpha
 *
579 580
 * Gets the #ClutterTimeline bound to @alpha.
 *
581
 * Return value: (transfer none): a #ClutterTimeline instance
582 583
 *
 * Since: 0.2
584 585 586 587
 */
ClutterTimeline *
clutter_alpha_get_timeline (ClutterAlpha *alpha)
{
588 589
  g_return_val_if_fail (CLUTTER_IS_ALPHA (alpha), NULL);

590 591 592 593 594
  return alpha->priv->timeline;
}

/**
 * clutter_alpha_new:
595 596 597 598 599 600 601 602 603 604
 * 
 * Creates a new #ClutterAlpha instance.  You must set a function
 * to compute the alpha value using clutter_alpha_set_func() and
 * bind a #ClutterTimeline object to the #ClutterAlpha instance
 * using clutter_alpha_set_timeline().
 *
 * You should use the newly created #ClutterAlpha instance inside
 * a #ClutterBehaviour object.
 *
 * Return value: the newly created empty #ClutterAlpha instance.
605 606
 *
 * Since: 0.2
607 608 609 610 611 612 613 614 615
 */
ClutterAlpha *
clutter_alpha_new (void)
{
  return g_object_new (CLUTTER_TYPE_ALPHA, NULL);
}

/**
 * clutter_alpha_new_full:
Emmanuele Bassi's avatar
Emmanuele Bassi committed
616
 * @timeline: #ClutterTimeline timeline
617
 * @mode: animation mode
618
 *
619
 * Creates a new #ClutterAlpha instance and sets the timeline
620 621 622
 * and animation mode.
 *
 * See also clutter_alpha_set_timeline() and clutter_alpha_set_mode().
623
 *
624
 * Return Value: the newly created #ClutterAlpha
625
 *
626
 * Since: 1.0
627
 */
628
ClutterAlpha *
629 630
clutter_alpha_new_full (ClutterTimeline *timeline,
                        gulong           mode)
631
{
632
  g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), NULL);
633
  g_return_val_if_fail (mode != CLUTTER_ANIMATION_LAST, NULL);
634

635 636 637 638
  return g_object_new (CLUTTER_TYPE_ALPHA,
                       "timeline", timeline,
                       "mode", mode,
                       NULL);
639 640
}

641
/**
642 643 644 645 646
 * clutter_alpha_new_with_func:
 * @timeline: a #ClutterTimeline
 * @func: a #ClutterAlphaFunc
 * @data: data to pass to the function, or %NULL
 * @destroy: function to call when removing the alpha function, or %NULL
647
 *
648 649
 * Creates a new #ClutterAlpha instances and sets the timeline
 * and the alpha function.
650
 *
651 652 653 654
 * This function will not register @func as a global alpha function.
 *
 * See also clutter_alpha_set_timeline() and clutter_alpha_set_func().
 *
655 656 657 658 659
 * Return value: the newly created #ClutterAlpha
 *
 * Since: 1.0
 */
ClutterAlpha *
660 661 662 663
clutter_alpha_new_with_func (ClutterTimeline  *timeline,
                             ClutterAlphaFunc  func,
                             gpointer          data,
                             GDestroyNotify    destroy)
664
{
665 666 667 668 669 670 671 672 673 674
  ClutterAlpha *retval;

  g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), NULL);
  g_return_val_if_fail (func != NULL, NULL);

  retval = clutter_alpha_new ();
  clutter_alpha_set_timeline (retval, timeline);
  clutter_alpha_set_func (retval, func, data, destroy);

  return retval;
675 676 677 678 679 680
}

/**
 * clutter_alpha_get_mode:
 * @alpha: a #ClutterAlpha
 *
681
 * Retrieves the #ClutterAnimationMode used by @alpha.
682 683 684 685 686
 *
 * Return value: the animation mode
 *
 * Since: 1.0
 */
687
gulong
688 689 690 691 692 693 694
clutter_alpha_get_mode (ClutterAlpha *alpha)
{
  g_return_val_if_fail (CLUTTER_IS_ALPHA (alpha), CLUTTER_CUSTOM_MODE);

  return alpha->priv->mode;
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
static gdouble
clutter_linear (ClutterAlpha *alpha,
                gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;

  return clutter_timeline_get_progress (timeline);
}

static gdouble
clutter_ease_in_quad (ClutterAlpha *alpha,
                      gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
  gdouble progress = clutter_timeline_get_progress (timeline);

  return progress * progress;
}

static gdouble
clutter_ease_out_quad (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
719
  gdouble p = clutter_timeline_get_progress (timeline);
720

721
  return -1.0 * p * (p - 2);
722 723 724 725 726 727 728
}

static gdouble
clutter_ease_in_out_quad (ClutterAlpha *alpha,
                          gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
729 730
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
731
  gdouble p = t / (d / 2);
732

733 734
  if (p < 1)
    return 0.5 * p * p;
735

736 737 738
  p -= 1;

  return -0.5 * (p * (p - 2) - 1);
739 740 741 742 743 744 745
}

static gdouble
clutter_ease_in_cubic (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
746
  gdouble p = clutter_timeline_get_progress (timeline);
747

748
  return p * p * p;
749 750 751 752 753 754 755
}

static gdouble
clutter_ease_out_cubic (ClutterAlpha *alpha,
                        gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
756 757
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
758
  gdouble p = t / d - 1;
759

760
  return p * p * p + 1;
761 762 763 764 765 766 767
}

static gdouble
clutter_ease_in_out_cubic (ClutterAlpha *alpha,
                           gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
768 769
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
770 771 772 773
  gdouble p = t / (d / 2);

  if (p < 1)
    return 0.5 * p * p * p;
774

775
  p -= 2;
776

777
  return 0.5 * (p * p * p + 2);
778 779 780 781 782 783 784
}

static gdouble
clutter_ease_in_quart (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
785
  gdouble p = clutter_timeline_get_progress (timeline);
786

787
  return p * p * p * p;
788 789 790 791 792 793 794
}

static gdouble
clutter_ease_out_quart (ClutterAlpha *alpha,
                        gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
795 796
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
797
  gdouble p = t / d - 1;
798

799
  return -1.0 * (p * p * p * p - 1);
800 801 802 803 804 805 806
}

static gdouble
clutter_ease_in_out_quart (ClutterAlpha *alpha,
                           gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
807 808
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
809 810 811 812
  gdouble p = t / (d / 2);

  if (p < 1)
    return 0.5 * p * p * p * p;
813

814
  p -= 2;
815

816
  return -0.5 * (p * p * p * p - 2);
817 818 819 820 821 822 823
}

static gdouble
clutter_ease_in_quint (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
 {
  ClutterTimeline *timeline = alpha->priv->timeline;
824
  gdouble p = clutter_timeline_get_progress (timeline);
825

826
  return p * p * p * p * p;
827 828 829 830 831 832 833
}

static gdouble
clutter_ease_out_quint (ClutterAlpha *alpha,
                        gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
834 835
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
836
  gdouble p = t / d - 1;
837

838
  return p * p * p * p * p + 1;
839 840 841 842 843 844 845
}

static gdouble
clutter_ease_in_out_quint (ClutterAlpha *alpha,
                           gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
846 847
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
848
  gdouble p = t / (d / 2);
849

850 851
  if (p < 1)
    return 0.5 * p * p * p * p * p;
852

853 854 855
  p -= 2;

  return 0.5 * (p * p * p * p * p + 2);
856 857 858 859 860 861 862
}

static gdouble
clutter_ease_in_sine (ClutterAlpha *alpha,
                      gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
863 864
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
865 866 867 868 869 870 871 872 873

  return -1.0 * cos (t / d * G_PI_2) + 1.0;
}

static gdouble
clutter_ease_out_sine (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
874 875
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
876 877 878 879 880 881 882 883 884

  return sin (t / d * G_PI_2);
}

static gdouble
clutter_ease_in_out_sine (ClutterAlpha *alpha,
                          gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
885 886
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
887 888 889 890 891 892 893 894 895

  return -0.5 * (cos (G_PI * t / d) - 1);
}

static gdouble
clutter_ease_in_expo (ClutterAlpha *alpha,
                      gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
896 897
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
898 899 900 901 902 903 904 905 906

  return (t == 0) ? 0.0 : pow (2, 10 * (t / d - 1));
}

static gdouble
clutter_ease_out_expo (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
907 908
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
909 910 911 912 913 914 915 916 917

  return (t == d) ? 1.0 : -pow (2, -10 * t / d) + 1;
}

static gdouble
clutter_ease_in_out_expo (ClutterAlpha *alpha,
                          gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
918 919
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
920
  gdouble p;
921 922 923 924 925 926 927

  if (t == 0)
    return 0.0;

  if (t == d)
    return 1.0;

928 929 930 931 932 933
  p = t / (d / 2);

  if (p < 1)
    return 0.5 * pow (2, 10 * (p - 1));

  p -= 1;
934

935
  return 0.5 * (-pow (2, -10 * p) + 2);
936 937 938 939 940 941 942
}

static gdouble
clutter_ease_in_circ (ClutterAlpha *alpha,
                      gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
943
  gdouble p = clutter_timeline_get_progress (timeline);
944

945
  return -1.0 * (sqrt (1 - p * p) - 1);
946 947 948 949 950 951 952
}

static gdouble
clutter_ease_out_circ (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
953 954
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
955
  gdouble p = t / d - 1;
956

957
  return sqrt (1 - p * p);
958 959 960 961 962 963 964
}

static gdouble
clutter_ease_in_out_circ (ClutterAlpha *alpha,
                          gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
965 966
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
967
  gdouble p = t / (d / 2);
968

969 970
  if (p < 1)
    return -0.5 * (sqrt (1 - p * p) - 1);
971

972 973 974
  p -= 2;

  return 0.5 * (sqrt (1 - p * p) + 1);
975 976 977 978 979 980 981
}

static gdouble
clutter_ease_in_elastic (ClutterAlpha *alpha,
                         gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
982 983
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
984 985
  gdouble p = d * .3;
  gdouble s = p / 4;
986
  gdouble q = t / d;
987

988
  if (q == 1)
989 990
    return 1.0;

991 992 993
  q -= 1;

  return -(pow (2, 10 * q) * sin ((q * d - s) * (2 * G_PI) / p));
994 995 996 997 998 999 1000
}

static gdouble
clutter_ease_out_elastic (ClutterAlpha *alpha,
                          gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1001 1002
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
1003 1004
  gdouble p = d * .3;
  gdouble s = p / 4;
1005
  gdouble q = t / d;
1006

1007
  if (q == 1)
1008 1009
    return 1.0;

1010
  return pow (2, -10 * q) * sin ((q * d - s) * (2 * G_PI) / p) + 1.0;
1011 1012 1013 1014 1015 1016 1017
}

static gdouble
clutter_ease_in_out_elastic (ClutterAlpha *alpha,
                             gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1018 1019
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
1020 1021
  gdouble p = d * (.3 * 1.5);
  gdouble s = p / 4;
1022
  gdouble q = t / (d / 2);
1023

1024
  if (q == 2)
1025 1026
    return 1.0;

1027 1028 1029 1030 1031 1032
  if (q < 1)
    {
      q -= 1;

      return -.5 * (pow (2, 10 * q) * sin ((q * d - s) * (2 * G_PI) / p));
    }
1033 1034
  else
    {
1035 1036 1037 1038
      q -= 1;

      return pow (2, -10 * q)
           * sin ((q * d - s) * (2 * G_PI) / p)
1039 1040 1041 1042 1043 1044 1045 1046 1047
           * .5 + 1.0;
    }
}

static gdouble
clutter_ease_in_back (ClutterAlpha *alpha,
                      gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1048
  gdouble p = clutter_timeline_get_progress (timeline);
1049

1050
  return p * p * ((1.70158 + 1) * p - 1.70158);
1051 1052 1053 1054 1055 1056 1057
}

static gdouble
clutter_ease_out_back (ClutterAlpha *alpha,
                       gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1058 1059
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
1060
  gdouble p = t / d - 1;
1061

1062
  return p * p * ((1.70158 + 1) * p + 1.70158) + 1;
1063 1064 1065 1066 1067 1068 1069
}

static gdouble
clutter_ease_in_out_back (ClutterAlpha *alpha,
                          gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1070 1071
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
1072 1073
  gdouble p = t / (d / 2);
  gdouble s = 1.70158 * 1.525;
1074

1075 1076
  if (p < 1)
    return 0.5 * (p * p * ((s + 1) * p - s));
1077

1078 1079 1080
  p -= 2;

  return 0.5 * (p * p * ((s + 1) * p + s) + 2);
1081 1082 1083 1084 1085 1086
}

static gdouble
ease_out_bounce_internal (gdouble t,
                          gdouble d)
{
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  gdouble p = t / d;

  if (p < (1 / 2.75))
    return 7.5625 * p * p;
  else if (p < (2 / 2.75))
    {
      p -= (1.5 / 2.75);

      return 7.5625 * p * p + .75;
    }
  else if (p < (2.5 / 2.75))
    {
      p -= (2.25 / 2.75);

      return 7.5625 * p * p + .9375;
    }
1103
  else
1104 1105 1106 1107 1108
    {
      p -= (2.625 / 2.75);

      return 7.5625 * p * p + .984375;
    }
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
}

static gdouble
ease_in_bounce_internal (gdouble t,
                         gdouble d)
{
  return 1.0 - ease_out_bounce_internal (d - t, d);
}

static gdouble
clutter_ease_in_bounce (ClutterAlpha *alpha,
                        gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1123 1124
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
1125 1126 1127 1128 1129 1130 1131 1132 1133

  return ease_in_bounce_internal (t, d);
}

static gdouble
clutter_ease_out_bounce (ClutterAlpha *alpha,
                         gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1134 1135
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
1136 1137 1138 1139 1140 1141 1142 1143 1144

  return ease_out_bounce_internal (t, d);
}

static gdouble
clutter_ease_in_out_bounce (ClutterAlpha *alpha,
                            gpointer      dummy G_GNUC_UNUSED)
{
  ClutterTimeline *timeline = alpha->priv->timeline;
1145 1146
  gdouble t = clutter_timeline_get_elapsed_time (timeline);
  gdouble d = clutter_timeline_get_duration (timeline);
1147 1148 1149 1150 1151 1152 1153

  if (t < d / 2)
    return ease_in_bounce_internal (t * 2, d) * 0.5;
  else
    return ease_out_bounce_internal (t * 2 - d, d) * 0.5 + 1.0 * 0.5;
}

1154 1155 1156 1157 1158
/* static enum/function mapping table for the animation modes
 * we provide internally
 *
 * XXX - keep in sync with ClutterAnimationMode
 */
1159
static const struct {
1160
  gulong mode;
1161 1162
  ClutterAlphaFunc func;
} animation_modes[] = {
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
  { CLUTTER_CUSTOM_MODE,         NULL },

  { CLUTTER_LINEAR,              clutter_linear },
  { CLUTTER_EASE_IN_QUAD,        clutter_ease_in_quad },
  { CLUTTER_EASE_OUT_QUAD,       clutter_ease_out_quad },
  { CLUTTER_EASE_IN_OUT_QUAD,    clutter_ease_in_out_quad },
  { CLUTTER_EASE_IN_CUBIC,       clutter_ease_in_cubic },
  { CLUTTER_EASE_OUT_CUBIC,      clutter_ease_out_cubic },
  { CLUTTER_EASE_IN_OUT_CUBIC,   clutter_ease_in_out_cubic },
  { CLUTTER_EASE_IN_QUART,       clutter_ease_in_quart },
  { CLUTTER_EASE_OUT_QUART,      clutter_ease_out_quart },
  { CLUTTER_EASE_IN_OUT_QUART,   clutter_ease_in_out_quart },
  { CLUTTER_EASE_IN_QUINT,       clutter_ease_in_quint },
  { CLUTTER_EASE_OUT_QUINT,      clutter_ease_out_quint },
  { CLUTTER_EASE_IN_OUT_QUINT,   clutter_ease_in_out_quint },
  { CLUTTER_EASE_IN_SINE,        clutter_ease_in_sine },
  { CLUTTER_EASE_OUT_SINE,       clutter_ease_out_sine },
  { CLUTTER_EASE_IN_OUT_SINE,    clutter_ease_in_out_sine },
  { CLUTTER_EASE_IN_EXPO,        clutter_ease_in_expo },
  { CLUTTER_EASE_OUT_EXPO,       clutter_ease_out_expo },
  { CLUTTER_EASE_IN_OUT_EXPO,    clutter_ease_in_out_expo },
  { CLUTTER_EASE_IN_CIRC,        clutter_ease_in_circ },
  { CLUTTER_EASE_OUT_CIRC,       clutter_ease_out_circ },
  { CLUTTER_EASE_IN_OUT_CIRC,    clutter_ease_in_out_circ },
  { CLUTTER_EASE_IN_ELASTIC,     clutter_ease_in_elastic },
  { CLUTTER_EASE_OUT_ELASTIC,    clutter_ease_out_elastic },
  { CLUTTER_EASE_IN_OUT_ELASTIC, clutter_ease_in_out_elastic },
  { CLUTTER_EASE_IN_BACK,        clutter_ease_in_back },
  { CLUTTER_EASE_OUT_BACK,       clutter_ease_out_back },
  { CLUTTER_EASE_IN_OUT_BACK,    clutter_ease_in_out_back },
  { CLUTTER_EASE_IN_BOUNCE,      clutter_ease_in_bounce },
  { CLUTTER_EASE_OUT_BOUNCE,     clutter_ease_out_bounce },
  { CLUTTER_EASE_IN_OUT_BOUNCE,  clutter_ease_in_out_bounce },

  { CLUTTER_ANIMATION_LAST,      NULL },
1198 1199
};

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
typedef struct _AlphaData {
  guint closure_set : 1;

  ClutterAlphaFunc func;
  gpointer data;

  GClosure *closure;
} AlphaData;

static GPtrArray *clutter_alphas = NULL;

1211 1212 1213 1214 1215 1216
/**
 * clutter_alpha_set_mode:
 * @alpha: a #ClutterAlpha
 * @mode: a #ClutterAnimationMode
 *
 * Sets the progress function of @alpha using the symbolic value
1217 1218
 * of @mode, as taken by the #ClutterAnimationMode enumeration or
 * using the value returned by clutter_alpha_register_func().
1219 1220 1221 1222
 *
 * Since: 1.0
 */
void
1223 1224
clutter_alpha_set_mode (ClutterAlpha *alpha,
                        gulong        mode)
1225 1226 1227 1228
{
  ClutterAlphaPrivate *priv;

  g_return_if_fail (CLUTTER_IS_ALPHA (alpha));
1229
  g_return_if_fail (mode != CLUTTER_ANIMATION_LAST);
1230 1231 1232

  priv = alpha->priv;

1233 1234 1235 1236 1237
  if (mode == CLUTTER_CUSTOM_MODE)
    {
      priv->mode = mode;
    }
  else if (mode < CLUTTER_ANIMATION_LAST)
1238
    {
1239 1240
      GClosure *closure;

1241 1242 1243 1244
      /* sanity check to avoid getting an out of sync
       * enum/function mapping
       */
      g_assert (animation_modes[mode].mode == mode);
1245
      g_assert (animation_modes[mode].func != NULL);
1246

1247 1248 1249 1250
      closure = g_cclosure_new (G_CALLBACK (animation_modes[mode].func),
                                NULL,
                                NULL);
      clutter_alpha_set_closure_internal (alpha, closure);
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

      priv->mode = mode;
    }
  else if (mode > CLUTTER_ANIMATION_LAST)
    {
      AlphaData *alpha_data = NULL;
      gulong real_index = 0;

      if (G_UNLIKELY (clutter_alphas == NULL))
        {
          g_warning ("No alpha functions defined for ClutterAlpha to use. "
                     "Use clutter_alpha_register_func() to register an "
                     "alpha function.");
          return;
        }

      real_index = mode - CLUTTER_ANIMATION_LAST - 1;

      alpha_data = g_ptr_array_index (clutter_alphas, real_index);
      if (G_UNLIKELY (alpha_data == NULL))
        {
          g_warning ("No alpha function registered for mode %lu.",
                     mode);
          return;
        }

      if (alpha_data->closure_set)
        clutter_alpha_set_closure (alpha, alpha_data->closure);
      else
1280 1281 1282 1283 1284 1285 1286 1287
        {
          GClosure *closure;

          closure = g_cclosure_new (G_CALLBACK (alpha_data->func),
                                    alpha_data->data,
                                    NULL);
          clutter_alpha_set_closure_internal (alpha, closure);
        }
1288 1289 1290 1291 1292

      priv->mode = mode;
    }
  else
    g_assert_not_reached ();
1293

1294 1295 1296
  g_object_notify (G_OBJECT (alpha), "mode");
}

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
static gulong
register_alpha_internal (AlphaData *alpha_data)
{
  if (G_UNLIKELY (clutter_alphas == NULL))
    clutter_alphas = g_ptr_array_new ();

  g_ptr_array_add (clutter_alphas, alpha_data);

  return clutter_alphas->len + CLUTTER_ANIMATION_LAST;
}

1308
/**
1309 1310 1311
 * clutter_alpha_register_func:
 * @func: a #ClutterAlphaFunc
 * @data: user data to pass to @func, or %NULL
1312
 *
1313 1314
 * Registers a global alpha function and returns its logical id
 * to be used by clutter_alpha_set_mode() or by #ClutterAnimation.
1315
 *
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
 * The logical id is always greater than %CLUTTER_ANIMATION_LAST.
 *
 * Return value: the logical id of the alpha function
 *
 * Since: 1.0
 */
gulong
clutter_alpha_register_func (ClutterAlphaFunc func,
                             gpointer         data)
{
  AlphaData *alpha_data;

  g_return_val_if_fail (func != NULL, 0);

  alpha_data = g_slice_new (AlphaData);
  alpha_data->closure_set = FALSE;
  alpha_data->func = func;
  alpha_data->data = data;

1335
  return register_alpha_internal (alpha_data);
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
}

/**
 * clutter_alpha_register_closure:
 * @closure: a #GClosure
 *
 * #GClosure variant of clutter_alpha_register_func().
 *
 * Registers a global alpha function and returns its logical id
 * to be used by clutter_alpha_set_mode() or by #ClutterAnimation.
 *
 * The logical id is always greater than %CLUTTER_ANIMATION_LAST.
 *
 * Return value: the logical id of the alpha function
 *
 * Since: 1.0
1352
 */
1353 1354 1355
gulong
clutter_alpha_register_closure (GClosure *closure)
{
1356
  AlphaData *alpha_data;
1357 1358 1359

  g_return_val_if_fail (closure != NULL, 0);

1360 1361 1362
  alpha_data = g_slice_new (AlphaData);
  alpha_data->closure_set = TRUE;
  alpha_data->closure = closure;
1363

1364
  return register_alpha_internal (alpha_data);
1365
}