[clutter/android-enter-leave: 26/29] Add rotate action
- From: Lionel Landwerlin <llandwerlin src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [clutter/android-enter-leave: 26/29] Add rotate action
- Date: Wed, 11 Jul 2012 17:09:14 +0000 (UTC)
commit 2a33d472bdf583dfdf87a5742c8f2fbb0385747c
Author: Lionel Landwerlin <llandwerlin gmail com>
Date: Fri Jun 22 02:38:21 2012 +0100
Add rotate action
Allow rotation of an actor using 2 points (touch or pointers) events
https://bugzilla.gnome.org/show_bug.cgi?id=678587
clutter/Makefile.am | 2 +
clutter/clutter-rotate-action.c | 230 +++++++++++++++++++++++++++++++++++++++
clutter/clutter-rotate-action.h | 98 +++++++++++++++++
clutter/clutter.h | 3 +-
4 files changed, 332 insertions(+), 1 deletions(-)
---
diff --git a/clutter/Makefile.am b/clutter/Makefile.am
index 8c4634c..0d38b5d 100644
--- a/clutter/Makefile.am
+++ b/clutter/Makefile.am
@@ -108,6 +108,7 @@ source_h = \
$(srcdir)/clutter-path-constraint.h \
$(srcdir)/clutter-path.h \
$(srcdir)/clutter-property-transition.h \
+ $(srcdir)/clutter-rotate-action.h \
$(srcdir)/clutter-script.h \
$(srcdir)/clutter-scriptable.h \
$(srcdir)/clutter-settings.h \
@@ -187,6 +188,7 @@ source_c = \
$(srcdir)/clutter-path-constraint.c \
$(srcdir)/clutter-path.c \
$(srcdir)/clutter-property-transition.c \
+ $(srcdir)/clutter-rotate-action.c \
$(srcdir)/clutter-script.c \
$(srcdir)/clutter-script-parser.c \
$(srcdir)/clutter-scriptable.c \
diff --git a/clutter/clutter-rotate-action.c b/clutter/clutter-rotate-action.c
new file mode 100644
index 0000000..9313377
--- /dev/null
+++ b/clutter/clutter-rotate-action.c
@@ -0,0 +1,230 @@
+/*
+ * Clutter.
+ *
+ * An OpenGL based 'interactive canvas' library.
+ *
+ * Copyright (C) 2012 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:
+ * Lionel Landwerlin <lionel g landwerlin linux intel com>
+ */
+
+/**
+ * SECTION:clutter-rotate-action
+ * @Title: ClutterRotateAction
+ * @Short_Description: Action to rotate an actor
+ *
+ * #ClutterRotateAction is a sub-class of #ClutterGestureAction that implements
+ * the logic for recognizing rotate gestures.
+ *
+ * Since: 1.12
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <math.h>
+
+#include "clutter-rotate-action.h"
+
+#include "clutter-debug.h"
+#include "clutter-enum-types.h"
+#include "clutter-marshal.h"
+#include "clutter-private.h"
+
+struct _ClutterRotateActionPrivate
+{
+ gfloat initial_vector[2];
+ gdouble initial_vector_norm;
+ gdouble initial_rotation;
+
+ int threshold;
+};
+
+enum
+{
+ ROTATE,
+
+ LAST_SIGNAL
+};
+
+static guint rotate_signals[LAST_SIGNAL] = { 0, };
+
+G_DEFINE_TYPE (ClutterRotateAction, clutter_rotate_action,
+ CLUTTER_TYPE_GESTURE_ACTION);
+
+static void
+clutter_rotate_action_real_rotate (ClutterRotateAction *action,
+ ClutterActor *actor,
+ gdouble angle)
+{
+ g_object_set (G_OBJECT (actor),
+ "rotation-angle-z", action->priv->initial_rotation + angle,
+ NULL);
+}
+
+static gboolean
+clutter_rotate_action_gesture_begin (ClutterGestureAction *action,
+ ClutterActor *actor)
+{
+ ClutterRotateActionPrivate *priv = CLUTTER_ROTATE_ACTION (action)->priv;
+ gfloat p1[2], p2[2];
+
+ /* capture initial vector */
+ clutter_gesture_action_get_motion_coords (action, 0, &p1[0], &p1[1]);
+ clutter_gesture_action_get_motion_coords (action, 1, &p2[0], &p2[1]);
+
+ priv->initial_vector[0] = p2[0] - p1[0];
+ priv->initial_vector[1] = p2[1] - p1[1];
+
+ priv->initial_vector_norm =
+ sqrt (priv->initial_vector[0] * priv->initial_vector[0] +
+ priv->initial_vector[1] * priv->initial_vector[1]);
+
+ priv->initial_rotation = clutter_actor_get_rotation (actor, CLUTTER_Z_AXIS,
+ NULL, NULL, NULL);
+
+ return TRUE;
+}
+
+static gboolean
+clutter_rotate_action_gesture_progress (ClutterGestureAction *action,
+ ClutterActor *actor)
+{
+ ClutterRotateActionPrivate *priv = CLUTTER_ROTATE_ACTION (action)->priv;
+ gfloat p1[2], p2[2];
+ gfloat vector[2];
+
+ /* capture current vector */
+ clutter_gesture_action_get_motion_coords (action, 0, &p1[0], &p1[1]);
+ clutter_gesture_action_get_motion_coords (action, 1, &p2[0], &p2[1]);
+
+ vector[0] = p2[0] - p1[0];
+ vector[1] = p2[1] - p1[1];
+
+ if ((vector[0] == priv->initial_vector[0]) &&
+ (vector[1] == priv->initial_vector[1]))
+ {
+ g_signal_emit (action, rotate_signals[ROTATE], 0,
+ actor, (gdouble) 0.0);
+ }
+ else
+ {
+ gfloat mult[2];
+ gfloat norm;
+ gdouble angle;
+
+ /* Computes angle between the 2 initial touch points and the
+ current position of the 2 touch points. */
+ norm = sqrt (vector[0] * vector[0] + vector[1] * vector[1]);
+ norm = (priv->initial_vector[0] * vector[0] +
+ priv->initial_vector[1] * vector[1]) / (priv->initial_vector_norm * norm);
+
+ if ((norm >= -1.0) && (norm <= 1.0))
+ angle = acos (norm);
+ else
+ angle = 0;
+
+ /* The angle given is comprise between 0 and 180 degrees, we
+ need some logic on top to get a value between 0 and 360. */
+ mult[0] = priv->initial_vector[0] * vector[1] -
+ priv->initial_vector[1] * vector[0];
+ mult[1] = priv->initial_vector[1] * vector[0] -
+ priv->initial_vector[0] * vector[1];
+
+ if (mult[0] < 0)
+ angle = -angle;
+
+ /* Convert radians to degrees */
+ angle = angle * 180.0 / M_PI;
+
+ g_signal_emit (action, rotate_signals[ROTATE], 0,
+ actor, angle);
+ }
+
+ return TRUE;
+}
+
+static void
+clutter_rotate_action_gesture_cancel (ClutterGestureAction *action,
+ ClutterActor *actor)
+{
+ g_signal_emit (action, rotate_signals[ROTATE], 0,
+ actor, (gdouble) 0.0);
+}
+
+static void
+clutter_rotate_action_class_init (ClutterRotateActionClass *klass)
+{
+ ClutterGestureActionClass *gesture_class =
+ CLUTTER_GESTURE_ACTION_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (ClutterRotateActionPrivate));
+
+ klass->rotate = clutter_rotate_action_real_rotate;
+
+ gesture_class->gesture_begin = clutter_rotate_action_gesture_begin;
+ gesture_class->gesture_progress = clutter_rotate_action_gesture_progress;
+ gesture_class->gesture_cancel = clutter_rotate_action_gesture_cancel;
+
+ /**
+ * ClutterRotateAction::rotate:
+ * @action: the #ClutterRotateAction that emitted the signal
+ * @actor: the #ClutterActor attached to the @action
+ * @angle: the difference of angle of rotation between the initial
+ * rotation and the current rotation
+ *
+ * The ::rotate signal is emitted when a rotate gesture is recognized on the
+ * attached actor.
+ *
+ * Since: 1.12
+ */
+ rotate_signals[ROTATE] =
+ g_signal_new (I_("rotate"),
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (ClutterRotateActionClass, rotate),
+ NULL, NULL,
+ _clutter_marshal_VOID__OBJECT_DOUBLE,
+ G_TYPE_NONE, 2,
+ CLUTTER_TYPE_ACTOR,
+ G_TYPE_DOUBLE);
+}
+
+static void
+clutter_rotate_action_init (ClutterRotateAction *self)
+{
+ self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, CLUTTER_TYPE_ROTATE_ACTION,
+ ClutterRotateActionPrivate);
+
+ clutter_gesture_action_set_n_touch_points (CLUTTER_GESTURE_ACTION (self), 2);
+}
+
+/**
+ * clutter_rotate_action_new:
+ *
+ * Creates a new #ClutterRotateAction instance
+ *
+ * Return value: the newly created #ClutterRotateAction
+ *
+ * Since: 1.12
+ */
+ClutterAction *
+clutter_rotate_action_new (void)
+{
+ return g_object_new (CLUTTER_TYPE_ROTATE_ACTION, NULL);
+}
diff --git a/clutter/clutter-rotate-action.h b/clutter/clutter-rotate-action.h
new file mode 100644
index 0000000..c36deb1
--- /dev/null
+++ b/clutter/clutter-rotate-action.h
@@ -0,0 +1,98 @@
+/*
+ * Clutter.
+ *
+ * An OpenGL based 'interactive canvas' library.
+ *
+ * Copyright (C) 2012 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:
+ * Lionel Landwerlin <lionel g landwerlin linux intel com>
+ */
+
+#if !defined(__CLUTTER_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
+#error "Only <clutter/clutter.h> can be included directly."
+#endif
+
+#ifndef __CLUTTER_ROTATE_ACTION_H__
+#define __CLUTTER_ROTATE_ACTION_H__
+
+#include <clutter/clutter-gesture-action.h>
+
+G_BEGIN_DECLS
+
+#define CLUTTER_TYPE_ROTATE_ACTION (clutter_rotate_action_get_type ())
+#define CLUTTER_ROTATE_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_ROTATE_ACTION, ClutterRotateAction))
+#define CLUTTER_IS_ROTATE_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_ROTATE_ACTION))
+#define CLUTTER_ROTATE_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_ROTATE_ACTION, ClutterRotateActionClass))
+#define CLUTTER_IS_ROTATE_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_ROTATE_ACTION))
+#define CLUTTER_ROTATE_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_ROTATE_ACTION, ClutterRotateActionClass))
+
+typedef struct _ClutterRotateAction ClutterRotateAction;
+typedef struct _ClutterRotateActionPrivate ClutterRotateActionPrivate;
+typedef struct _ClutterRotateActionClass ClutterRotateActionClass;
+
+/**
+ * ClutterRotateAction:
+ *
+ * The <structname>ClutterRotateAction</structname> structure contains
+ * only private data and should be accessed using the provided API
+ *
+ * Since: 1.8
+ */
+struct _ClutterRotateAction
+{
+ /*< private >*/
+ ClutterGestureAction parent_instance;
+
+ ClutterRotateActionPrivate *priv;
+};
+
+/**
+ * ClutterRotateActionClass:
+ * @swept: class handler for the #ClutterRotateAction::rotate signal
+ *
+ * The <structname>ClutterRotateActionClass</structname> structure contains
+ * only private data.
+ *
+ * Since: 1.8
+ */
+struct _ClutterRotateActionClass
+{
+ /*< private >*/
+ ClutterGestureActionClass parent_class;
+
+ /*< public >*/
+ void (* rotate) (ClutterRotateAction *action,
+ ClutterActor *actor,
+ gdouble angle);
+
+ /*< private >*/
+ void (* _clutter_rotate_action1) (void);
+ void (* _clutter_rotate_action2) (void);
+ void (* _clutter_rotate_action3) (void);
+ void (* _clutter_rotate_action4) (void);
+ void (* _clutter_rotate_action5) (void);
+ void (* _clutter_rotate_action6) (void);
+ void (* _clutter_rotate_action7) (void);
+};
+
+GType clutter_rotate_action_get_type (void) G_GNUC_CONST;
+
+ClutterAction * clutter_rotate_action_new (void);
+
+G_END_DECLS
+
+#endif /* __CLUTTER_ROTATE_ACTION_H__ */
diff --git a/clutter/clutter.h b/clutter/clutter.h
index bb63ede..172e8b4 100644
--- a/clutter/clutter.h
+++ b/clutter/clutter.h
@@ -73,7 +73,7 @@
#include "clutter-image.h"
#include "clutter-input-device.h"
#include "clutter-interval.h"
-#include "clutter-keysyms.h"
+#include "clutter-keysyms.h"
#include "clutter-layout-manager.h"
#include "clutter-layout-meta.h"
#include "clutter-list-model.h"
@@ -88,6 +88,7 @@
#include "clutter-path-constraint.h"
#include "clutter-path.h"
#include "clutter-property-transition.h"
+#include "clutter-rotate-action.h"
#include "clutter-scriptable.h"
#include "clutter-script.h"
#include "clutter-settings.h"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]