~golfish/netbook-remix-launcher/desktop

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
/* tidy-texture-reflection.c: adds a reflection on a texture
 *
 * Copyright (C) 2007 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, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Matthew Allum <mallum@o-hand.com>
 */

/**
 * SECTION:tidy-texture-reflection
 * @short_description: Creates a reflection of a texture
 *
 * #ClutterReflectTexture allows the cloning of existing #ClutterTexture with 
 * a reflection-like effect.
 */

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

#include <clutter/cogl.h>
#include <clutter/clutter-util.h>

#include "tidy-texture-reflection.h"
#include "tidy-private.h"

enum
{
  PROP_0,

  PROP_REFLECTION_HEIGHT
};

#define TIDY_TEXTURE_REFLECTION_GET_PRIVATE(obj)        \
        (G_TYPE_INSTANCE_GET_PRIVATE ((obj),            \
         TIDY_TYPE_TEXTURE_REFLECTION,                  \
         TidyTextureReflectionPrivate))

struct _TidyTextureReflectionPrivate
{
  gint reflection_height;
};

G_DEFINE_TYPE (TidyTextureReflection,
               tidy_texture_reflection,
               CLUTTER_TYPE_CLONE_TEXTURE);

static void
reflect_texture_render_to_gl_quad (TidyTextureReflection *texture,
                                   ClutterTexture        *parent,
                                   gint                   x1, 
                                   gint                   y1,
                                   gint                   x2,
                                   gint                   y2)
{
  TidyTextureReflectionPrivate *priv = texture->priv;
  gint   qx1 = 0, qx2 = 0, qy1 = 0, qy2 = 0;
  gint   qwidth = 0, qheight = 0;
  gint   x, y, i =0, lastx = 0, lasty = 0;
  gint   n_x_tiles, n_y_tiles; 
  gint   pwidth, pheight, rheight;
  float tx, ty, ty2 = 0.0;
  ClutterColor white = { 255, 255, 255, 255 };

  qwidth  = x2 - x1;
  qheight = y2 - y1;

  rheight = priv->reflection_height;
  if (rheight < 0)
    rheight = clutter_actor_get_height (CLUTTER_ACTOR (parent)) / 2;

  if (rheight > qheight)
    rheight = qheight;

  /* Only paint if parent is in a state to do so */
  if (!clutter_texture_has_generated_tiles (parent))
    return;
  
  clutter_texture_get_base_size (parent, &pwidth, &pheight); 

  if (!clutter_texture_is_tiled (parent))
    {
      clutter_texture_bind_tile (parent, 0);

      /* NPOTS textures *always* used if extension available
       */
      if (clutter_feature_available (CLUTTER_FEATURE_TEXTURE_RECTANGLE))
	{
	  tx = (float) pwidth;
	  ty = (float) pheight;
	  ty2 = (float) (clutter_actor_get_height (CLUTTER_ACTOR (texture)) * rheight)
                        / pheight;
	  ty2 = pheight - ty2;

	}
      else
	{
	  tx = (float) pwidth / clutter_util_next_p2 (pwidth);  
	  ty = (float) pheight / clutter_util_next_p2 (pheight);
	}

      qx1 = x1; qx2 = x2;
      qy1 = y1; qy2 = y1 + rheight;

#ifdef CLUTTER_COGL_HAS_GL
      glBegin (GL_QUADS);

      white.alpha = clutter_actor_get_opacity (CLUTTER_ACTOR (texture));
      cogl_color (&white);

      glTexCoord2f (0, ty);   
      glVertex2i   (qx1, qy1);

      glTexCoord2f (tx,  ty);   
      glVertex2i   (qx2, qy1);

      white.alpha = 0;
      cogl_color (&white);

      glTexCoord2f (tx,  ty2);    
      glVertex2i   (qx2, qy2);
      
      glTexCoord2f (0, ty2);    
      glVertex2i   (qx1, qy2);

      glEnd ();	
#else
#warning "TidyTextureReflection does not currently support GL ES"
#endif
      return;
    }
  else
    {
      g_warning("TidyTextureReflection doesn't support tiled textures.."); 
    }

  /* FIXME: Below wont actually render the corrent graduated effect. */

#ifdef CLUTTER_COGL_HAS_GL
  clutter_texture_get_n_tiles (parent, &n_x_tiles, &n_y_tiles); 

  for (x = 0; x < n_x_tiles; x++)
    {
      lasty = 0;

      for (y = 0; y < n_y_tiles; y++)
	{
	  gint actual_w, actual_h;
	  gint xpos, ypos, xsize, ysize, ywaste, xwaste;
	  
	  clutter_texture_bind_tile (parent, i);
	 
	  clutter_texture_get_x_tile_detail (parent, x,
                                             &xpos, &xsize, &xwaste);
	  clutter_texture_get_y_tile_detail (parent, y,
                                             &ypos, &ysize, &ywaste);

	  actual_w = xsize - xwaste;
	  actual_h = ysize - ywaste;

	  tx = (float) actual_w / xsize;
	  ty = (float) actual_h / ysize;

	  qx1 = x1 + lastx;
	  qx2 = qx1 + ((qwidth * actual_w ) / pwidth );
	  
	  qy1 = y1 + lasty;
	  qy2 = qy1 + ((qheight * actual_h) / pheight );

	  glBegin (GL_QUADS);
	  glTexCoord2f (tx, ty);   glVertex2i   (qx2, qy2);
	  glTexCoord2f (0,  ty);   glVertex2i   (qx1, qy2);
	  glTexCoord2f (0,  0);    glVertex2i   (qx1, qy1);
	  glTexCoord2f (tx, 0);    glVertex2i   (qx2, qy1);
	  glEnd ();	

	  lasty += qy2 - qy1;	  

	  i++;
	}
      lastx += qx2 - qx1;
    }
#endif
}

static void
tidy_texture_reflection_paint (ClutterActor *actor)
{
#ifdef CLUTTER_COGL_HAS_GL
  TidyTextureReflection *texture;
  ClutterCloneTexture *clone;
  ClutterTexture *parent;
  ClutterActorBox box = { 0, };
  COGLenum target_type;
  ClutterColor white = { 255, 255, 255, 255 };

  texture = TIDY_TEXTURE_REFLECTION (actor);
  clone = CLUTTER_CLONE_TEXTURE (actor);

  parent = clutter_clone_texture_get_parent_texture (clone);
  if (!parent)
    return;

  if (!CLUTTER_ACTOR_IS_REALIZED (parent))
    clutter_actor_realize (CLUTTER_ACTOR (parent));

  if (clutter_feature_available (CLUTTER_FEATURE_TEXTURE_RECTANGLE) &&
      clutter_texture_is_tiled (parent) == FALSE)
    {
      target_type = CGL_TEXTURE_RECTANGLE_ARB;
      cogl_enable (CGL_ENABLE_TEXTURE_RECT | CGL_ENABLE_BLEND);
    }
  else
    {
      target_type = CGL_TEXTURE_2D;
      cogl_enable (CGL_ENABLE_TEXTURE_2D | CGL_ENABLE_BLEND);
    }

  cogl_push_matrix ();

  white.alpha = clutter_actor_get_opacity (actor);
  cogl_color (&white);

  clutter_actor_query_coords (actor, &box);

  reflect_texture_render_to_gl_quad (texture, parent,
                                     0, 0,
                                     CLUTTER_UNITS_TO_DEVICE (box.x2 - box.x1),
                                     CLUTTER_UNITS_TO_DEVICE (box.y2 - box.y1));

  cogl_pop_matrix ();
#endif
}

static void
tidy_texture_reflection_set_property (GObject      *gobject,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  TidyTextureReflection *texture;

  texture = TIDY_TEXTURE_REFLECTION (gobject);

  switch (prop_id)
    {
    case PROP_REFLECTION_HEIGHT:
      tidy_texture_reflection_set_reflection_height (texture,
                                                     g_value_get_int (value));
      break;

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

static void
tidy_texture_reflection_get_property (GObject    *gobject,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  TidyTextureReflection *texture;

  texture = TIDY_TEXTURE_REFLECTION (gobject);

  switch (prop_id)
    {
    case PROP_REFLECTION_HEIGHT:
      g_value_set_int (value,
                       tidy_texture_reflection_get_reflection_height (texture));
      break;

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

static void
tidy_texture_reflection_class_init (TidyTextureReflectionClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);

  g_type_class_add_private (klass, sizeof (TidyTextureReflectionPrivate));

  gobject_class->set_property = tidy_texture_reflection_set_property;
  gobject_class->get_property = tidy_texture_reflection_get_property;

  actor_class->paint = tidy_texture_reflection_paint;

  g_object_class_install_property (gobject_class,
                                   PROP_REFLECTION_HEIGHT,
                                   g_param_spec_int ("reflection-height",
                                                     "Reflection Height",
                                                     "Height of the reflection, or -1",
                                                     -1, G_MAXINT, -1,
                                                     TIDY_PARAM_READWRITE));
}

static void
tidy_texture_reflection_init (TidyTextureReflection *texture)
{
  TidyTextureReflectionPrivate *priv;

  texture->priv = priv = TIDY_TEXTURE_REFLECTION_GET_PRIVATE (texture);
  
  priv->reflection_height = -1;
}

ClutterActor *
tidy_texture_reflection_new (ClutterTexture *texture)
{
  g_return_val_if_fail (texture == NULL || CLUTTER_IS_TEXTURE (texture), NULL);

  return g_object_new (TIDY_TYPE_TEXTURE_REFLECTION,
                       "parent-texture", texture,
                       NULL);
}

void
tidy_texture_reflection_set_reflection_height (TidyTextureReflection *texture,
                                               gint                   height)
{
  TidyTextureReflectionPrivate *priv;

  g_return_if_fail (TIDY_IS_TEXTURE_REFLECTION (texture));

  priv = texture->priv;

  if (priv->reflection_height != height)
    {
      priv->reflection_height = height;

      if (CLUTTER_ACTOR_IS_VISIBLE (texture))
        clutter_actor_queue_redraw (CLUTTER_ACTOR (texture));

      g_object_notify (G_OBJECT (texture), "reflection-height");
    }
}

gint
tidy_texture_reflection_get_reflection_height (TidyTextureReflection *texture)
{
  g_return_val_if_fail (TIDY_IS_TEXTURE_REFLECTION (texture), -1);

  return texture->priv->reflection_height;
}