File: clutter-page-turn-effect.c

package info (click to toggle)
clutter-1.0 1.26.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 34,768 kB
  • sloc: ansic: 128,524; sh: 5,483; makefile: 1,585; xml: 1,248; ruby: 149; perl: 142; sed: 16
file content (424 lines) | stat: -rw-r--r-- 12,028 bytes parent folder | download | duplicates (7)
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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2010  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>
 *
 * Based on MxDeformPageTurn, written by:
 *   Chris Lord <chris@linux.intel.com>
 */

/**
 * SECTION:clutter-page-turn-effect
 * @Title: ClutterPageTurnEffect
 * @Short_Description: A page turning effect
 *
 * A simple page turning effect
 *
 * #ClutterPageTurnEffect is available since Clutter 1.4
 */

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

#include <math.h>

#include "clutter-page-turn-effect.h"

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

#define CLUTTER_PAGE_TURN_EFFECT_CLASS(k)       (G_TYPE_CHECK_CLASS_CAST ((k), CLUTTER_TYPE_PAGE_TURN_EFFECT, ClutterPageTurnEffectClass))
#define CLUTTER_IS_PAGE_TURN_EFFECT_CLASS(k)    (G_TYPE_CHECK_CLASS_TYPE ((k), CLUTTER_TYPE_PAGE_TURN_EFFECT))
#define CLUTTER_PAGE_TURN_EFFECT_GET_CLASS(o)   (G_TYPE_INSTANCE_GET_CLASS ((o), CLUTTER_TYPE_PAGE_TURN_EFFECT, ClutterPageTurnEffectClass))

struct _ClutterPageTurnEffect
{
  ClutterDeformEffect parent_instance;

  gdouble period;
  gdouble angle;

  gfloat radius;
};

struct _ClutterPageTurnEffectClass
{
  ClutterDeformEffectClass parent_class;
};

enum
{
  PROP_0,

  PROP_PERIOD,
  PROP_ANGLE,
  PROP_RADIUS,

  PROP_LAST
};

static GParamSpec *obj_props[PROP_LAST];

G_DEFINE_TYPE (ClutterPageTurnEffect,
               clutter_page_turn_effect,
               CLUTTER_TYPE_DEFORM_EFFECT);

static void
clutter_page_turn_effect_deform_vertex (ClutterDeformEffect *effect,
                                        gfloat               width,
                                        gfloat               height,
                                        CoglTextureVertex   *vertex)
{
  ClutterPageTurnEffect *self = CLUTTER_PAGE_TURN_EFFECT (effect);
  gfloat cx, cy, rx, ry, radians, turn_angle;
  guint shade;

  if (self->period == 0.0)
    return;

  radians = self->angle / (180.0f / G_PI);

  /* Rotate the point around the centre of the page-curl ray to align it with
   * the y-axis.
   */
  cx = (1.f - self->period) * width;
  cy = (1.f - self->period) * height;

  rx = ((vertex->x - cx) * cos (- radians))
     - ((vertex->y - cy) * sin (- radians))
     - self->radius;
  ry = ((vertex->x - cx) * sin (- radians))
     + ((vertex->y - cy) * cos (- radians));

  turn_angle = 0.f;
  if (rx > self->radius * -2.0f)
    {
      /* Calculate the curl angle as a function from the distance of the curl
       * ray (i.e. the page crease)
       */
      turn_angle = (rx / self->radius * G_PI_2) - G_PI_2;
      shade = (sin (turn_angle) * 96.0f) + 159.0f;

      /* Add a gradient that makes it look like lighting and hides the switch
       * between textures.
       */
      cogl_color_init_from_4ub (&vertex->color, shade, shade, shade, 0xff);
    }

  if (rx > 0)
    {
      /* Make the curl radius smaller as more circles are formed (stops
       * z-fighting and looks cool). Note that 10 is a semi-arbitrary
       * number here - divide it by two and it's the amount of space
       * between curled layers of the texture, in pixels.
       */
      gfloat small_radius;
      
      small_radius = self->radius
                   - MIN (self->radius, (turn_angle * 10) / G_PI);

      /* Calculate a point on a cylinder (maybe make this a cone at some
       * point) and rotate it by the specified angle.
       */
      rx = (small_radius * cos (turn_angle)) + self->radius;

      vertex->x = (rx * cos (radians)) - (ry * sin (radians)) + cx;
      vertex->y = (rx * sin (radians)) + (ry * cos (radians)) + cy;
      vertex->z = (small_radius * sin (turn_angle)) + self->radius;
    }
}

static void
clutter_page_turn_effect_set_property (GObject      *gobject,
                                       guint         prop_id,
                                       const GValue *value,
                                       GParamSpec   *pspec)
{
  ClutterPageTurnEffect *effect = CLUTTER_PAGE_TURN_EFFECT (gobject);

  switch (prop_id)
    {
    case PROP_PERIOD:
      clutter_page_turn_effect_set_period (effect, g_value_get_double (value));
      break;

    case PROP_ANGLE:
      clutter_page_turn_effect_set_angle (effect, g_value_get_double (value));
      break;

    case PROP_RADIUS:
      clutter_page_turn_effect_set_radius (effect, g_value_get_float (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
      break;
    }
}

static void
clutter_page_turn_effect_get_property (GObject    *gobject,
                                       guint       prop_id,
                                       GValue     *value,
                                       GParamSpec *pspec)
{
  ClutterPageTurnEffect *effect = CLUTTER_PAGE_TURN_EFFECT (gobject);

  switch (prop_id)
    {
    case PROP_PERIOD:
      g_value_set_double (value, effect->period);
      break;

    case PROP_ANGLE:
      g_value_set_double (value, effect->angle);
      break;

    case PROP_RADIUS:
      g_value_set_float (value, effect->radius);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
      break;
    }
}

static void
clutter_page_turn_effect_class_init (ClutterPageTurnEffectClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  ClutterDeformEffectClass *deform_class = CLUTTER_DEFORM_EFFECT_CLASS (klass);
  GParamSpec *pspec;

  gobject_class->set_property = clutter_page_turn_effect_set_property;
  gobject_class->get_property = clutter_page_turn_effect_get_property;

  /**
   * ClutterPageTurnEffect:period:
   *
   * The period of the page turn, between 0.0 (no curling) and
   * 1.0 (fully curled)
   *
   * Since: 1.4
   */
  pspec = g_param_spec_double ("period",
                               "Period",
                               "The period of the page turn",
                               0.0, 1.0,
                               0.0,
                               CLUTTER_PARAM_READWRITE);
  obj_props[PROP_PERIOD] = pspec;
  g_object_class_install_property (gobject_class, PROP_PERIOD, pspec);

  /**
   * ClutterPageTurnEffect:angle:
   *
   * The angle of the page rotation, in degrees, between 0.0 and 360.0
   *
   * Since: 1.4
   */
  pspec = g_param_spec_double ("angle",
                               "Angle",
                               "The angle of the page rotation, in degrees",
                               0.0, 360.0,
                               0.0,
                               CLUTTER_PARAM_READWRITE);
  obj_props[PROP_ANGLE] = pspec;
  g_object_class_install_property (gobject_class, PROP_ANGLE, pspec);

  /**
   * ClutterPageTurnEffect:radius:
   *
   * The radius of the page curl, in pixels
   *
   * Since: 1.4
   */
  pspec = g_param_spec_float ("radius",
                              "Radius",
                              "The radius of the page curl",
                              -G_MAXFLOAT, G_MAXFLOAT,
                              24.0,
                              CLUTTER_PARAM_READWRITE);
  obj_props[PROP_RADIUS] = pspec;
  g_object_class_install_property (gobject_class, PROP_RADIUS, pspec);

  deform_class->deform_vertex = clutter_page_turn_effect_deform_vertex;
}

static void
clutter_page_turn_effect_init (ClutterPageTurnEffect *self)
{
  self->period = 0.0;
  self->angle = 0.0;
  self->radius = 24.0f;
}

/**
 * clutter_page_turn_effect_new:
 * @period: the period of the page curl, between 0.0 and 1.0
 * @angle: the angle of the page curl, between 0.0 and 360.0
 * @radius: the radius of the page curl, in pixels
 *
 * Creates a new #ClutterPageTurnEffect instance with the given parameters
 *
 * Return value: the newly created #ClutterPageTurnEffect
 *
 * Since: 1.4
 */
ClutterEffect *
clutter_page_turn_effect_new (gdouble period,
                              gdouble angle,
                              gfloat  radius)
{
  g_return_val_if_fail (period >= 0.0 && period <= 1.0, NULL);
  g_return_val_if_fail (angle >= 0.0 && angle <= 360.0, NULL);

  return g_object_new (CLUTTER_TYPE_PAGE_TURN_EFFECT,
                       "period", period,
                       "angle", angle,
                       "radius", radius,
                       NULL);
}

/**
 * clutter_page_turn_effect_set_period:
 * @effect: a #ClutterPageTurnEffect
 * @period: the period of the page curl, between 0.0 and 1.0
 *
 * Sets the period of the page curling, between 0.0 (no curling)
 * and 1.0 (fully curled)
 *
 * Since: 1.4
 */
void
clutter_page_turn_effect_set_period (ClutterPageTurnEffect *effect,
                                     gdouble                period)
{
  g_return_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect));
  g_return_if_fail (period >= 0.0 && period <= 1.0);

  effect->period = period;

  clutter_deform_effect_invalidate (CLUTTER_DEFORM_EFFECT (effect));

  g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_PERIOD]);
}

/**
 * clutter_page_turn_effect_get_period:
 * @effect: a #ClutterPageTurnEffect
 *
 * Retrieves the value set using clutter_page_turn_effect_get_period()
 *
 * Return value: the period of the page curling
 *
 * Since: 1.4
 */
gdouble
clutter_page_turn_effect_get_period (ClutterPageTurnEffect *effect)
{
  g_return_val_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect), 0.0);

  return effect->period;
}

/**
 * clutter_page_turn_effect_set_angle:
 * @effect: #ClutterPageTurnEffect
 * @angle: the angle of the page curl, in degrees
 *
 * Sets the angle of the page curling, in degrees
 *
 * Since: 1.4
 */
void
clutter_page_turn_effect_set_angle (ClutterPageTurnEffect *effect,
                                    gdouble                angle)
{
  g_return_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect));
  g_return_if_fail (angle >= 0.0 && angle <= 360.0);

  effect->angle = angle;

  clutter_deform_effect_invalidate (CLUTTER_DEFORM_EFFECT (effect));

  g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_ANGLE]);
}

/**
 * clutter_page_turn_effect_get_angle:
 * @effect: a #ClutterPageTurnEffect:
 *
 * Retrieves the value set using clutter_page_turn_effect_get_angle()
 *
 * Return value: the angle of the page curling
 *
 * Since: 1.4
 */
gdouble
clutter_page_turn_effect_get_angle (ClutterPageTurnEffect *effect)
{
  g_return_val_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect), 0.0);

  return effect->angle;
}

/**
 * clutter_page_turn_effect_set_radius:
 * @effect: a #ClutterPageTurnEffect:
 * @radius: the radius of the page curling, in pixels
 *
 * Sets the radius of the page curling
 *
 * Since: 1.4
 */
void
clutter_page_turn_effect_set_radius (ClutterPageTurnEffect *effect,
                                     gfloat                 radius)
{
  g_return_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect));

  effect->radius = radius;

  clutter_deform_effect_invalidate (CLUTTER_DEFORM_EFFECT (effect));

  g_object_notify_by_pspec (G_OBJECT (effect), obj_props[PROP_RADIUS]);
}

/**
 * clutter_page_turn_effect_get_radius:
 * @effect: a #ClutterPageTurnEffect
 *
 * Retrieves the value set using clutter_page_turn_effect_set_radius()
 *
 * Return value: the radius of the page curling
 *
 * Since: 1.4
 */
gfloat
clutter_page_turn_effect_get_radius (ClutterPageTurnEffect *effect)
{
  g_return_val_if_fail (CLUTTER_IS_PAGE_TURN_EFFECT (effect), 0.0);

  return effect->radius;
}