Initial Import
[profile/ivi/clutter-toys.git] / attic / fluttr / src / fluttr-behave.c
1 /*
2  * Copyright (C) 2007 Neil J. Patel
3  * Copyright (C) 2007 OpenedHand Ltd
4  *
5  * Author: Neil J. Patel  <njp@o-hand.com>
6  */
7
8
9 /* This is a utility ClutterBehaviour-derived class, in which you can set the
10    alphanotify function. It is useful for situations where you do not need the
11    full capabilities of the ClutterBehvaiour class, you just want a function to
12    be called for each iteration along the timeline
13 */
14
15 #include "fluttr-behave.h"
16
17 #include "math.h"
18
19 G_DEFINE_TYPE (FluttrBehave, fluttr_behave, CLUTTER_TYPE_BEHAVIOUR);
20
21 #define FLUTTR_BEHAVE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj),\
22         FLUTTR_TYPE_BEHAVE, \
23         FluttrBehavePrivate))
24
25 struct _FluttrBehavePrivate
26 {
27         FluttrBehaveAlphaFunc     func;
28         gpointer                data;
29 };
30
31 guint32 
32 alpha_sine_inc_func (ClutterAlpha *alpha,
33                      gpointer      dummy)
34 {
35         ClutterTimeline *timeline;
36         gint current_frame_num, n_frames;
37         gdouble x, sine;
38   
39         timeline = clutter_alpha_get_timeline (alpha);
40
41         current_frame_num = clutter_timeline_get_current_frame (timeline);
42         n_frames = clutter_timeline_get_n_frames (timeline);
43
44         x = (gdouble) (current_frame_num * 0.5f * M_PI) / n_frames ;
45         /* sine = (sin (x - (M_PI / 0.5f)) + 1.0f) * 0.5f; */
46   
47         sine = (sin (x - (M_PI / 0.5f))) ;
48   
49         return (guint32)(sine * (gdouble) CLUTTER_ALPHA_MAX_ALPHA);
50 }
51
52 guint32 
53 alpha_linear_inc_func (ClutterAlpha *alpha,
54                        gpointer      dummy)
55 {
56         ClutterTimeline *timeline;
57         gint current_frame_num, n_frames;
58         gdouble x;
59   
60         timeline = clutter_alpha_get_timeline (alpha);
61
62         current_frame_num = clutter_timeline_get_current_frame (timeline);
63         n_frames = clutter_timeline_get_n_frames (timeline);
64
65         x = (gdouble) (current_frame_num) / n_frames ;
66         /* sine = (sin (x - (M_PI / 0.5f)) + 1.0f) * 0.5f; */
67   
68         return (guint32)(x * (gdouble) CLUTTER_ALPHA_MAX_ALPHA);      
69 }
70
71 static void
72 fluttr_behave_alpha_notify (ClutterBehaviour *behave, guint32 alpha_value)
73 {
74         FluttrBehave *fluttr_behave = FLUTTR_BEHAVE(behave);
75         FluttrBehavePrivate *priv;
76         
77         priv = FLUTTR_BEHAVE_GET_PRIVATE (fluttr_behave);
78         
79         if (priv->func != NULL) {
80                 priv->func (behave, alpha_value, priv->data);
81         }
82 }
83
84 static void
85 fluttr_behave_class_init (FluttrBehaveClass *klass)
86 {
87         GObjectClass        *gobject_class = G_OBJECT_CLASS (klass);
88         ClutterBehaviourClass *behave_class = CLUTTER_BEHAVIOUR_CLASS (klass);
89
90         behave_class->alpha_notify = fluttr_behave_alpha_notify;
91         
92         g_type_class_add_private (gobject_class, sizeof (FluttrBehavePrivate));
93 }
94
95 static void
96 fluttr_behave_init (FluttrBehave *self)
97 {
98         FluttrBehavePrivate *priv;
99         
100         priv = FLUTTR_BEHAVE_GET_PRIVATE (self);
101         
102         priv->func = NULL;
103         priv->data = NULL;
104 }
105
106 ClutterBehaviour*
107 fluttr_behave_new (ClutterAlpha                 *alpha,
108                  FluttrBehaveAlphaFunc   func,
109                  gpointer                data)
110 {
111         FluttrBehave *behave;
112         FluttrBehavePrivate *priv;
113         
114         behave = g_object_new (FLUTTR_TYPE_BEHAVE, 
115                                "alpha", alpha,
116                                NULL);
117
118         priv = FLUTTR_BEHAVE_GET_PRIVATE (behave);  
119         
120         priv->func = func;
121         priv->data = data;
122                 
123         return CLUTTER_BEHAVIOUR(behave);
124 }