Release Clutter 1.11.4 (snapshot)
[profile/ivi/clutter.git] / clutter / clutter-page-turn-effect.c
1 /*
2  * Clutter.
3  *
4  * An OpenGL based 'interactive canvas' library.
5  *
6  * Copyright (C) 2010  Intel Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  * Author:
22  *   Emmanuele Bassi <ebassi@linux.intel.com>
23  *
24  * Based on MxDeformPageTurn, written by:
25  *   Chris Lord <chris@linux.intel.com>
26  */
27
28 /**
29  * SECTION:clutter-page-turn-effect
30  * @Title: ClutterPageTurnEffect
31  * @Short_Description: A page turning effect
32  *
33  * A simple page turning effect
34  *
35  * #ClutterPageTurnEffect is available since Clutter 1.4
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <math.h>
43
44 #include "clutter-page-turn-effect.h"
45
46 #include "clutter-debug.h"
47 #include "clutter-private.h"
48
49 #define CLUTTER_PAGE_TURN_EFFECT_CLASS(k)       (G_TYPE_CHECK_CLASS_CAST ((k), CLUTTER_TYPE_PAGE_TURN_EFFECT, ClutterPageTurnEffectClass))
50 #define CLUTTER_IS_PAGE_TURN_EFFECT_CLASS(k)    (G_TYPE_CHECK_CLASS_TYPE ((k), CLUTTER_TYPE_PAGE_TURN_EFFECT))
51 #define CLUTTER_PAGE_TURN_EFFECT_GET_CLASS(o)   (G_TYPE_INSTANCE_GET_CLASS ((o), CLUTTER_TYPE_PAGE_TURN_EFFECT, ClutterPageTurnEffectClass))
52
53 struct _ClutterPageTurnEffect
54 {
55   ClutterDeformEffect parent_instance;
56
57   gdouble period;
58   gdouble angle;
59
60   gfloat radius;
61 };
62
63 struct _ClutterPageTurnEffectClass
64 {
65   ClutterDeformEffectClass parent_class;
66 };
67
68 enum
69 {
70   PROP_0,
71
72   PROP_PERIOD,
73   PROP_ANGLE,
74   PROP_RADIUS,
75
76   PROP_LAST
77 };
78
79 static GParamSpec *obj_props[PROP_LAST];
80
81 G_DEFINE_TYPE (ClutterPageTurnEffect,
82                clutter_page_turn_effect,
83                CLUTTER_TYPE_DEFORM_EFFECT);
84
85 static void
86 clutter_page_turn_effect_deform_vertex (ClutterDeformEffect *effect,
87                                         gfloat               width,
88                                         gfloat               height,
89                                         CoglTextureVertex   *vertex)
90 {
91   ClutterPageTurnEffect *self = CLUTTER_PAGE_TURN_EFFECT (effect);
92   gfloat cx, cy, rx, ry, radians, turn_angle;
93   guint shade;
94
95   if (self->period == 0.0)
96     return;
97
98   radians = self->angle / (180.0f / G_PI);
99
100   /* Rotate the point around the centre of the page-curl ray to align it with
101    * the y-axis.
102    */
103   cx = (1.f - self->period) * width;
104   cy = (1.f - self->period) * height;
105
106   rx = ((vertex->x - cx) * cos (- radians))
107      - ((vertex->y - cy) * sin (- radians))
108      - self->radius;
109   ry = ((vertex->x - cx) * sin (- radians))
110      + ((vertex->y - cy) * cos (- radians));
111
112   turn_angle = 0.f;
113   if (rx > self->radius * -2.0f)
114     {
115       /* Calculate the curl angle as a function from the distance of the curl
116        * ray (i.e. the page crease)
117        */
118       turn_angle = (rx / self->radius * G_PI_2) - G_PI_2;
119       shade = (sin (turn_angle) * 96.0f) + 159.0f;
120
121       /* Add a gradient that makes it look like lighting and hides the switch
122        * between textures.
123        */
124       cogl_color_init_from_4ub (&vertex->color, shade, shade, shade, 0xff);
125     }
126
127   if (rx > 0)
128     {
129       /* Make the curl radius smaller as more circles are formed (stops
130        * z-fighting and looks cool). Note that 10 is a semi-arbitrary
131        * number here - divide it by two and it's the amount of space
132        * between curled layers of the texture, in pixels.
133        */
134       gfloat small_radius;
135       
136       small_radius = self->radius
137                    - MIN (self->radius, (turn_angle * 10) / G_PI);
138
139       /* Calculate a point on a cylinder (maybe make this a cone at some
140        * point) and rotate it by the specified angle.
141        */
142       rx = (small_radius * cos (turn_angle)) + self->radius;
143
144       vertex->x = (rx * cos (radians)) - (ry * sin (radians)) + cx;
145       vertex->y = (rx * sin (radians)) + (ry * cos (radians)) + cy;
146       vertex->z = (small_radius * sin (turn_angle)) + self->radius;
147     }
148 }
149
150 static void
151 clutter_page_turn_effect_set_property (GObject      *gobject,
152                                        guint         prop_id,
153                                        const GValue *value,
154                                        GParamSpec   *pspec)
155 {
156   ClutterPageTurnEffect *effect = CLUTTER_PAGE_TURN_EFFECT (gobject);
157
158   switch (prop_id)
159     {
160     case PROP_PERIOD:
161       clutter_page_turn_effect_set_period (effect, g_value_get_double (value));
162       break;
163
164     case PROP_ANGLE:
165       clutter_page_turn_effect_set_angle (effect, g_value_get_double (value));
166       break;
167
168     case PROP_RADIUS:
169       clutter_page_turn_effect_set_radius (effect, g_value_get_float (value));
170       break;
171
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
174       break;
175     }
176 }
177
178 static void
179 clutter_page_turn_effect_get_property (GObject    *gobject,
180                                        guint       prop_id,
181                                        GValue     *value,
182                                        GParamSpec *pspec)
183 {
184   ClutterPageTurnEffect *effect = CLUTTER_PAGE_TURN_EFFECT (gobject);
185
186   switch (prop_id)
187     {
188     case PROP_PERIOD:
189       g_value_set_double (value, effect->period);
190       break;
191
192     case PROP_ANGLE:
193       g_value_set_double (value, effect->angle);
194       break;
195
196     case PROP_RADIUS:
197       g_value_set_float (value, effect->radius);
198       break;
199
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
202       break;
203     }
204 }
205
206 static void
207 clutter_page_turn_effect_class_init (ClutterPageTurnEffectClass *klass)
208 {
209   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
210   ClutterDeformEffectClass *deform_class = CLUTTER_DEFORM_EFFECT_CLASS (klass);
211   GParamSpec *pspec;
212
213   gobject_class->set_property = clutter_page_turn_effect_set_property;
214   gobject_class->get_property = clutter_page_turn_effect_get_property;
215
216   /**
217    * ClutterPageTurnEffect:period:
218    *
219    * The period of the page turn, between 0.0 (no curling) and
220    * 1.0 (fully curled)
221    *
222    * Since: 1.4
223    */
224   pspec = g_param_spec_double ("period",
225                                "Period",
226                                "The period of the page turn",
227                                0.0, 1.0,
228                                0.0,
229                                CLUTTER_PARAM_READWRITE);
230   obj_props[PROP_PERIOD] = pspec;
231   g_object_class_install_property (gobject_class, PROP_PERIOD, pspec);
232
233   /**
234    * ClutterPageTurnEffect:angle:
235    *
236    * The angle of the page rotation, in degrees, between 0.0 and 360.0
237    *
238    * Since: 1.4
239    */
240   pspec = g_param_spec_double ("angle",
241                                "Angle",
242                                "The angle of the page rotation, in degrees",
243                                0.0, 360.0,
244                                0.0,
245                                CLUTTER_PARAM_READWRITE);
246   obj_props[PROP_ANGLE] = pspec;
247   g_object_class_install_property (gobject_class, PROP_ANGLE, pspec);
248
249   /**
250    * ClutterPageTurnEffect:radius:
251    *
252    * The radius of the page curl, in pixels
253    *
254    * Since: 1.4
255    */
256   pspec = g_param_spec_float ("radius",
257                               "Radius",
258                               "The radius of the page curl",
259                               -G_MAXFLOAT, G_MAXFLOAT,
260                               24.0,
261                               CLUTTER_PARAM_READWRITE);
262   obj_props[PROP_RADIUS] = pspec;
263   g_object_class_install_property (gobject_class, PROP_RADIUS, pspec);
264
265   deform_class->deform_vertex = clutter_page_turn_effect_deform_vertex;
266 }
267
268 static void
269 clutter_page_turn_effect_init (ClutterPageTurnEffect *self)
270 {
271   self->period = 0.0;
272   self->angle = 0.0;
273   self->radius = 24.0f;
274 }
275
276 /**
277  * clutter_page_turn_effect_new:
278  * @period: the period of the page curl, between 0.0 and 1.0
279  * @angle: the angle of the page curl, between 0.0 and 360.0
280  * @radius: the radius of the page curl, in pixels
281  *
282  * Creates a new #ClutterPageTurnEffect instance with the given parameters
283  *
284  * Return value: the newly created #ClutterPageTurnEffect
285  *
286  * Since: 1.4
287  */
288 ClutterEffect *
289 clutter_page_turn_effect_new (gdouble period,
290                               gdouble angle,
291                               gfloat  radius)
292 {
293   g_return_val_if_fail (period >= 0.0 && period <= 1.0, NULL);
294   g_return_val_if_fail (angle >= 0.0 && angle <= 360.0, NULL);
295
296   return g_object_new (CLUTTER_TYPE_PAGE_TURN_EFFECT,
297                        "period", period,
298                        "angle", angle,
299                        "radius", radius,
300                        NULL);
301 }
302
303 /**
304  * clutter_page_turn_effect_set_period:
305  * @effect: a #ClutterPageTurnEffect
306  * @period: the period of the page curl, between 0.0 and 1.0
307  *
308  * Sets the period of the page curling, between 0.0 (no curling)
309  * and 1.0 (fully curled)
310  *
311  * Since: 1.4
312  */
313 void
314 clutter_page_turn_effect_set_period (ClutterPageTurnEffect *effect,
315                                      gdouble                period)
316 {
317   g_return_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect));
318   g_return_if_fail (period >= 0.0 && period <= 1.0);
319
320   effect->period = period;
321
322   clutter_deform_effect_invalidate (CLUTTER_DEFORM_EFFECT (effect));
323
324   g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_PERIOD]);
325 }
326
327 /**
328  * clutter_page_turn_effect_get_period:
329  * @effect: a #ClutterPageTurnEffect
330  *
331  * Retrieves the value set using clutter_page_turn_effect_get_period()
332  *
333  * Return value: the period of the page curling
334  *
335  * Since: 1.4
336  */
337 gdouble
338 clutter_page_turn_effect_get_period (ClutterPageTurnEffect *effect)
339 {
340   g_return_val_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect), 0.0);
341
342   return effect->period;
343 }
344
345 /**
346  * clutter_page_turn_effect_set_angle:
347  * @effect: #ClutterPageTurnEffect
348  * @angle: the angle of the page curl, in degrees
349  *
350  * Sets the angle of the page curling, in degrees
351  *
352  * Since: 1.4
353  */
354 void
355 clutter_page_turn_effect_set_angle (ClutterPageTurnEffect *effect,
356                                     gdouble                angle)
357 {
358   g_return_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect));
359   g_return_if_fail (angle >= 0.0 && angle <= 360.0);
360
361   effect->angle = angle;
362
363   clutter_deform_effect_invalidate (CLUTTER_DEFORM_EFFECT (effect));
364
365   g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_ANGLE]);
366 }
367
368 /**
369  * clutter_page_turn_effect_get_angle:
370  * @effect: a #ClutterPageTurnEffect:
371  *
372  * Retrieves the value set using clutter_page_turn_effect_get_angle()
373  *
374  * Return value: the angle of the page curling
375  *
376  * Since: 1.4
377  */
378 gdouble
379 clutter_page_turn_effect_get_angle (ClutterPageTurnEffect *effect)
380 {
381   g_return_val_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect), 0.0);
382
383   return effect->angle;
384 }
385
386 /**
387  * clutter_page_turn_effect_set_radius:
388  * @effect: a #ClutterPageTurnEffect:
389  * @radius: the radius of the page curling, in pixels
390  *
391  * Sets the radius of the page curling
392  *
393  * Since: 1.4
394  */
395 void
396 clutter_page_turn_effect_set_radius (ClutterPageTurnEffect *effect,
397                                      gfloat                 radius)
398 {
399   g_return_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect));
400
401   effect->radius = radius;
402
403   clutter_deform_effect_invalidate (CLUTTER_DEFORM_EFFECT (effect));
404
405   g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_RADIUS]);
406 }
407
408 /**
409  * clutter_page_turn_effect_get_radius:
410  * @effect: a #ClutterPageTurnEffect
411  *
412  * Retrieves the value set using clutter_page_turn_effect_set_radius()
413  *
414  * Return value: the radius of the page curling
415  *
416  * Since: 1.4
417  */
418 gfloat
419 clutter_page_turn_effect_get_radius (ClutterPageTurnEffect *effect)
420 {
421   g_return_val_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect), 0.0);
422
423   return effect->radius;
424 }