mutter/clutter/clutter-alpha.c

345 lines
7.9 KiB
C
Raw Normal View History

/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Authored By Matthew Allum <mallum@openedhand.com>
* Jorn Baayen <jorn@openedhand.com>
*
* Copyright (C) 2006 OpenedHand
*
* 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:clutter-alpha
* @short_description: A class for calculating an alpha value as a function
* of time.
*
* #ClutterAlpha is a class for calculating an alpha value as a function
* of time.
*/
#include "clutter-alpha.h"
#include "clutter-main.h"
#include "clutter-marshal.h"
2006-09-08 16:57:31 -04:00
G_DEFINE_TYPE (ClutterAlpha, clutter_alpha, G_TYPE_OBJECT);
struct _ClutterAlphaPrivate
{
ClutterTimeline *timeline;
2006-09-08 16:57:31 -04:00
guint timeline_new_frame_id;
guint32 alpha;
ClutterAlphaFunc func;
};
enum
{
PROP_0,
PROP_TIMELINE,
2006-09-08 16:57:31 -04:00
PROP_FUNC,
PROP_ALPHA
};
/* Alpha funcs */
guint32
2006-09-08 16:57:31 -04:00
clutter_alpha_ramp_inc_func (ClutterAlpha *alpha)
{
2006-09-08 16:57:31 -04:00
int current_frame_num, nframes;
2006-09-08 16:57:31 -04:00
current_frame_num =
clutter_timeline_get_current_frame (alpha->priv->timeline);
nframes =
clutter_timeline_get_n_frames (alpha->priv->timeline);
2006-09-08 16:57:31 -04:00
return (current_frame_num * CLUTTER_ALPHA_MAX_ALPHA) / nframes;
}
guint32
2006-09-08 16:57:31 -04:00
clutter_alpha_ramp_dec_func (ClutterAlpha *alpha)
{
2006-09-08 16:57:31 -04:00
int current_frame_num, nframes;
2006-09-08 16:57:31 -04:00
current_frame_num =
clutter_timeline_get_current_frame (alpha->priv->timeline);
nframes =
clutter_timeline_get_n_frames (alpha->priv->timeline);
2006-09-08 16:57:31 -04:00
return ((nframes - current_frame_num) * CLUTTER_ALPHA_MAX_ALPHA) / nframes;
}
guint32
2006-09-08 16:57:31 -04:00
clutter_alpha_ramp_func (ClutterAlpha *alpha)
{
2006-09-08 16:57:31 -04:00
int current_frame_num, nframes;
2006-09-08 16:57:31 -04:00
current_frame_num =
clutter_timeline_get_current_frame (alpha->priv->timeline);
nframes =
clutter_timeline_get_n_frames (alpha->priv->timeline);
2006-09-08 16:57:31 -04:00
if (current_frame_num > (nframes/2))
{
2006-09-08 16:57:31 -04:00
return ((nframes - current_frame_num)
* CLUTTER_ALPHA_MAX_ALPHA) / (nframes/2);
}
else
{
2006-09-08 16:57:31 -04:00
return (current_frame_num * CLUTTER_ALPHA_MAX_ALPHA) / (nframes/2);
}
}
/* Object */
static void
timeline_new_frame_cb (ClutterTimeline *timeline,
guint current_frame_num,
ClutterAlpha *alpha)
{
2006-09-08 16:57:31 -04:00
ClutterAlphaPrivate *priv;
2006-09-08 16:57:31 -04:00
priv = alpha->priv;
/* Update alpha value */
2006-09-08 16:57:31 -04:00
if (priv->func) {
priv->alpha = priv->func(alpha);
g_object_notify (G_OBJECT (alpha), "alpha");
}
}
static void
clutter_alpha_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
2006-09-08 16:57:31 -04:00
ClutterAlpha *alpha;
ClutterAlphaPrivate *priv;
alpha = CLUTTER_ALPHA(object);
priv = alpha->priv;
switch (prop_id)
{
case PROP_TIMELINE:
clutter_alpha_set_timeline (alpha, g_value_get_object (value));
break;
2006-09-08 16:57:31 -04:00
case PROP_FUNC:
priv->func = g_value_get_pointer (value);
break;
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)
{
2006-09-08 16:57:31 -04:00
ClutterAlpha *alpha;
ClutterAlphaPrivate *priv;
2006-09-08 16:57:31 -04:00
alpha = CLUTTER_ALPHA(object);
priv = alpha->priv;
switch (prop_id)
{
case PROP_TIMELINE:
g_value_set_object (value, priv->timeline);
break;
2006-09-08 16:57:31 -04:00
case PROP_FUNC:
g_value_set_pointer (value, priv->func);
break;
case PROP_ALPHA:
g_value_set_uint (value, priv->alpha);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
clutter_alpha_finalize (GObject *object)
{
G_OBJECT_CLASS (clutter_alpha_parent_class)->finalize (object);
}
static void
clutter_alpha_dispose (GObject *object)
{
2006-09-08 16:57:31 -04:00
ClutterAlpha *self = CLUTTER_ALPHA(object);
clutter_alpha_set_timeline (self, NULL);
G_OBJECT_CLASS (clutter_alpha_parent_class)->dispose (object);
}
static void
clutter_alpha_class_init (ClutterAlphaClass *klass)
{
GObjectClass *object_class;
2006-09-08 16:57:31 -04:00
object_class = (GObjectClass*) klass;
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));
2006-09-08 16:57:31 -04:00
g_object_class_install_property
(object_class, PROP_TIMELINE,
g_param_spec_object ("timeline",
"Timeline",
"Timeline",
CLUTTER_TYPE_TIMELINE,
G_PARAM_READWRITE));
g_object_class_install_property
(object_class, PROP_FUNC,
g_param_spec_pointer ("func",
"Alpha function",
"Alpha function",
G_PARAM_READWRITE));
g_object_class_install_property
(object_class, PROP_ALPHA,
g_param_spec_uint ("alpha",
"Alpha value",
"Alpha value",
0,
CLUTTER_ALPHA_MAX_ALPHA,
0,
G_PARAM_READABLE));
}
static void
clutter_alpha_init (ClutterAlpha *self)
{
2006-09-08 16:57:31 -04:00
self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
CLUTTER_TYPE_ALPHA,
ClutterAlphaPrivate);
self->priv->func = CLUTTER_ALPHA_RAMP_INC;
}
/**
2006-09-08 16:57:31 -04:00
* clutter_alpha_get_alpha:
* @alpha: A #ClutterAlpha
*
* Query the current alpha value.
*
2006-09-08 16:57:31 -04:00
* Return Value: The current alpha value for the alpha
*/
2006-09-08 16:57:31 -04:00
gint32
clutter_alpha_get_alpha (ClutterAlpha *alpha)
{
g_return_val_if_fail (CLUTTER_IS_ALPHA (alpha), FALSE);
return alpha->priv->alpha;
}
/**
* clutter_alpha_set_func:
* @alpha: A #ClutterAlpha
* @func: A #ClutterAlphaAlphaFunc
*
*/
void
2006-09-08 16:57:31 -04:00
clutter_alpha_set_func (ClutterAlpha *alpha,
ClutterAlphaFunc func)
{
2006-09-08 16:57:31 -04:00
alpha->priv->func = func;
}
/**
* clutter_alpha_set_timeline:
* @alpha: A #ClutterAlpha
2006-09-08 16:57:31 -04:00
* @timeline: A #ClutterTimeline
*
2006-09-08 16:57:31 -04:00
* Binds @alpha to @timeline.
*/
void
2006-09-08 16:57:31 -04:00
clutter_alpha_set_timeline (ClutterAlpha *alpha,
ClutterTimeline *timeline)
{
2006-09-08 16:57:31 -04:00
if (alpha->priv->timeline)
{
2006-09-08 16:57:31 -04:00
g_signal_handler_disconnect (alpha->priv->timeline,
alpha->priv->timeline_new_frame_id);
2006-09-08 16:57:31 -04:00
g_object_unref (alpha->priv->timeline);
alpha->priv->timeline = NULL;
}
if (timeline)
{
2006-09-08 16:57:31 -04:00
alpha->priv->timeline = g_object_ref (timeline);
2006-09-08 16:57:31 -04:00
alpha->priv->timeline_new_frame_id =
g_signal_connect (alpha->priv->timeline,
"new-frame",
G_CALLBACK (timeline_new_frame_cb),
alpha);
}
}
/**
* clutter_alpha_get_timeline:
* @alpha: A #ClutterAlpha
*
2006-09-08 16:57:31 -04:00
* Return value: The #ClutterTimeline
*/
ClutterTimeline *
clutter_alpha_get_timeline (ClutterAlpha *alpha)
{
g_return_val_if_fail (CLUTTER_IS_ALPHA (alpha), NULL);
return alpha->priv->timeline;
}
/**
* clutter_alpha_new:
2006-09-08 16:57:31 -04:00
* @timeline: #ClutterTimeline timeline
* @func: #ClutterAlphaFunc alpha function
*
2006-09-08 16:57:31 -04:00
* Create a new #ClutterAlpha instance.
*
2006-09-08 16:57:31 -04:00
* Return Value: a new #ClutterAlpha
*/
2006-09-08 16:57:31 -04:00
ClutterAlpha*
clutter_alpha_new (ClutterTimeline *timeline,
ClutterAlphaFunc func)
{
g_return_val_if_fail (CLUTTER_IS_TIMELINE (timeline), NULL);
g_return_val_if_fail (func != NULL, NULL);
2006-09-08 16:57:31 -04:00
return g_object_new (CLUTTER_TYPE_ALPHA,
"timeline", timeline,
"func", func,
NULL);
}