File: test-draggable.c

package info (click to toggle)
mx 1.4.6-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,628 kB
  • sloc: ansic: 46,358; sh: 11,012; makefile: 645; xml: 13
file content (405 lines) | stat: -rw-r--r-- 11,889 bytes parent folder | download | duplicates (3)
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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * Copyright 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cogl/cogl.h>
#include <cogl-pango/cogl-pango.h>
#include <clutter/clutter.h>
#include <mx/mx.h>

#define DRAGGABLE_TYPE_RECTANGLE        (draggable_rectangle_get_type ())
#define DRAGGABLE_RECTANGLE(obj)        (G_TYPE_CHECK_INSTANCE_CAST ((obj), DRAGGABLE_TYPE_RECTANGLE, DraggableRectangle))
#define DRAGGABLE_IS_RECTANGLE(obj)     (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DRAGGABLE_TYPE_RECTANGLE))

typedef struct _DraggableRectangle      DraggableRectangle;
typedef struct _DraggableRectangleClass DraggableRectangleClass;

struct _DraggableRectangle
{
  ClutterRectangle parent_instance;

  /* Draggable properties */
  guint threshold;

  MxDragAxis axis;

#if 0
  MxDragContainment containment;
#endif
  ClutterActorBox area;
  ClutterActor *actor;

  guint is_enabled : 1;

  gboolean drag_clone;
};

struct _DraggableRectangleClass
{
  ClutterRectangleClass parent_class;
};

enum
{
  PROP_0,

  PROP_DRAG_THRESHOLD,
  PROP_AXIS,
#if 0
  PROP_CONTAINMENT_TYPE,
  PROP_CONTAINMENT_AREA,
#endif
  PROP_ENABLED,
  PROP_ACTOR,
};

static void mx_draggable_iface_init (MxDraggableIface *iface);
static GType draggable_rectangle_get_type ();
G_DEFINE_TYPE_WITH_CODE (DraggableRectangle,
                         draggable_rectangle,
                         CLUTTER_TYPE_RECTANGLE,
                         G_IMPLEMENT_INTERFACE (MX_TYPE_DRAGGABLE,
                                                mx_draggable_iface_init));

static void
draggable_rectangle_drag_begin (MxDraggable       *draggable,
                                gfloat               event_x,
                                gfloat               event_y,
                                gint                 event_button,
                                ClutterModifierType  modifiers)
{
  gfloat x, y;
  ClutterActor *self = CLUTTER_ACTOR (draggable);
  ClutterActor *actor = mx_draggable_get_drag_actor (draggable);
  ClutterActor *stage = clutter_actor_get_stage (self);

  g_debug ("%s: drag of '%s' begin at %.2f, %.2f",
           G_STRLOC,
           clutter_actor_get_name (self),
           event_x,
           event_y);

  if (actor)
    {
      clutter_actor_get_position (self, &x, &y);
      clutter_actor_set_position (actor, x, y);

      clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor);
    }
  else
    actor = self;

  clutter_actor_set_opacity (actor, 224);
  clutter_actor_set_rotation (actor, CLUTTER_Y_AXIS,
                              30.0,
                              clutter_actor_get_width (actor) / 2.0,
                              0,
                              clutter_actor_get_height (actor) / 2.0);
}

static void
draggable_rectangle_drag_motion (MxDraggable *draggable,
                                 gfloat         delta_x,
                                 gfloat         delta_y)
{
  ClutterActor *actor = mx_draggable_get_drag_actor (draggable);

  g_debug ("%s: drag motion of '%s' (dx: %.2f, dy: %.2f)",
           G_STRLOC,
           clutter_actor_get_name (CLUTTER_ACTOR (draggable)),
           delta_x,
           delta_y);

  if (!actor)
    actor = CLUTTER_ACTOR (draggable);

  clutter_actor_move_by (actor, delta_x, delta_y);
}

static void
draggable_rectangle_drag_end (MxDraggable *draggable,
                              gfloat         event_x,
                              gfloat         event_y)
{
  gfloat x, y;
  ClutterActor *self = CLUTTER_ACTOR (draggable);
  ClutterActor *actor = mx_draggable_get_drag_actor (draggable);
  ClutterActor *stage = clutter_actor_get_stage (self);

  g_debug ("%s: drag of '%s' end at %.2f, %.2f",
           G_STRLOC,
           clutter_actor_get_name (CLUTTER_ACTOR (draggable)),
           event_x,
           event_y);

  if (!actor)
    {
      clutter_actor_set_rotation (self, CLUTTER_Y_AXIS,
                                  0.0, 0, 0, 0);
      clutter_actor_set_opacity (self, 255);
      return;
    }

  clutter_actor_get_position (actor, &x, &y);
  clutter_actor_animate (self, CLUTTER_EASE_OUT_QUAD, 150,
                         "x", x,
                         "y", y,
                         NULL);

  clutter_container_remove_actor (CLUTTER_CONTAINER (stage), actor);
}

static void
mx_draggable_iface_init (MxDraggableIface *iface)
{
  iface->drag_begin = draggable_rectangle_drag_begin;
  iface->drag_motion = draggable_rectangle_drag_motion;
  iface->drag_end = draggable_rectangle_drag_end;
}

static void
draggable_rectangle_set_property (GObject      *gobject,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  DraggableRectangle *rect = DRAGGABLE_RECTANGLE (gobject);

  switch (prop_id)
    {
    case PROP_DRAG_THRESHOLD:
      rect->threshold = g_value_get_uint (value);
      break;

    case PROP_AXIS:
      rect->axis = g_value_get_enum (value);
      break;

#if 0
    case PROP_CONTAINMENT_TYPE:
      rect->containment = g_value_get_enum (value);
      break;

    case PROP_CONTAINMENT_AREA:
      {
        ClutterActorBox *box = g_value_get_boxed (value);

        if (box)
          rect->area = *box;
        else
          memset (&rect->area, 0, sizeof (ClutterActorBox));
      }
      break;
#endif

    case PROP_ENABLED:
      rect->is_enabled = g_value_get_boolean (value);
      if (rect->is_enabled)
        mx_draggable_enable (MX_DRAGGABLE (gobject));
      else
        mx_draggable_disable (MX_DRAGGABLE (gobject));
      break;

    case PROP_ACTOR:
      break;

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

static void
draggable_rectangle_get_property (GObject    *gobject,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  DraggableRectangle *rect = DRAGGABLE_RECTANGLE (gobject);

  switch (prop_id)
    {
    case PROP_DRAG_THRESHOLD:
      g_value_set_uint (value, rect->threshold);
      break;

    case PROP_AXIS:
      g_value_set_enum (value, rect->axis);
      break;

#if 0
    case PROP_CONTAINMENT_TYPE:
      g_value_set_enum (value, rect->containment);
      break;

    case PROP_CONTAINMENT_AREA:
      g_value_set_boxed (value, &rect->area);
      break;
#endif

    case PROP_ENABLED:
      g_value_set_boolean (value, rect->is_enabled);
      break;

    case PROP_ACTOR:
      if (rect->drag_clone)
        g_value_set_object (value, rect->actor);
      break;

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

static void
draggable_rectangle_paint (ClutterActor *actor)
{
  gfloat text_x, text_y;
  ClutterActorBox box = { 0, };
  gint layout_width, layout_height;
  CoglColor color = { 0, };
  PangoLayout *layout;
  const gchar *name;

  CLUTTER_ACTOR_CLASS (draggable_rectangle_parent_class)->paint (actor);

  name = clutter_actor_get_name (actor);

  clutter_actor_get_allocation_box (actor, &box);

  layout = clutter_actor_create_pango_layout (actor, name);
  pango_layout_get_size (layout, &layout_width, &layout_height);

  text_x = ((box.x2 - box.x1) - (layout_width / 1024)) / 2;
  text_y = ((box.y2 - box.y1) - (layout_height / 1024)) / 2;

  cogl_color_set_from_4ub (&color, 0, 0, 0, 255);
  cogl_pango_render_layout (layout,
                            (int) text_x,
                            (int) text_y,
                            &color, 0);

  g_object_unref (layout);
}

static void
draggable_rectangle_dispose (GObject *object)
{
  DraggableRectangle *rect = DRAGGABLE_RECTANGLE (object);

  if (rect->actor)
    {
      g_object_unref (rect->actor);
      rect->actor = NULL;
    }

  G_OBJECT_CLASS (draggable_rectangle_parent_class)->dispose (object);
}

static void
draggable_rectangle_class_init (DraggableRectangleClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass);

  gobject_class->set_property = draggable_rectangle_set_property;
  gobject_class->get_property = draggable_rectangle_get_property;
  gobject_class->dispose = draggable_rectangle_dispose;

  actor_class->paint = draggable_rectangle_paint;

  g_object_class_override_property (gobject_class,
                                    PROP_DRAG_THRESHOLD,
                                    "drag-threshold");
  g_object_class_override_property (gobject_class,
                                    PROP_AXIS,
                                    "axis");
#if 0
  g_object_class_override_property (gobject_class,
                                    PROP_CONTAINMENT_TYPE,
                                    "containment-type");
  g_object_class_override_property (gobject_class,
                                    PROP_CONTAINMENT_AREA,
                                    "containment-area");
#endif
  g_object_class_override_property (gobject_class,
                                    PROP_ENABLED,
                                    "drag-enabled");
  g_object_class_override_property (gobject_class,
                                    PROP_ACTOR,
                                    "drag-actor");
}

static void
draggable_rectangle_init (DraggableRectangle *self)
{
  self->threshold = 0;
  self->axis = 0;
#if 0
  self->containment = MX_DISABLE_CONTAINMENT;
#endif
  self->is_enabled = FALSE;
  self->actor = g_object_ref_sink (clutter_clone_new (CLUTTER_ACTOR (self)));
}

int
main (int argc, char *argv[])
{
  ClutterActor *stage;
  ClutterActor *draggable;
  ClutterColor rect_color = { 204, 204, 204, 255 };

  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;

  stage = clutter_stage_get_default ();
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Draggable Example");
  clutter_actor_set_size (stage, 800, 600);

  draggable = g_object_new (DRAGGABLE_TYPE_RECTANGLE, NULL);
  ((DraggableRectangle*)draggable)->drag_clone = TRUE;
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), draggable);
  clutter_rectangle_set_color (CLUTTER_RECTANGLE (draggable), &rect_color);
  clutter_actor_set_size (draggable, 100, 100);
  clutter_actor_set_position (draggable, 350, 100);
  clutter_actor_set_reactive (draggable, TRUE);
  clutter_actor_set_name (draggable, "h-handle");
  mx_draggable_set_axis (MX_DRAGGABLE (draggable), MX_DRAG_AXIS_X);
  mx_draggable_enable (MX_DRAGGABLE (draggable));

  draggable = g_object_new (DRAGGABLE_TYPE_RECTANGLE, NULL);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), draggable);
  clutter_rectangle_set_color (CLUTTER_RECTANGLE (draggable), &rect_color);
  clutter_actor_set_size (draggable, 100, 100);
  clutter_actor_set_position (draggable, 350, 300);
  clutter_actor_set_reactive (draggable, TRUE);
  clutter_actor_set_name (draggable, "v-handle");
  mx_draggable_set_axis (MX_DRAGGABLE (draggable), MX_DRAG_AXIS_Y);
  mx_draggable_enable (MX_DRAGGABLE (draggable));

  clutter_actor_show_all (stage);

  clutter_main ();

  return EXIT_SUCCESS;
}