event-controller: Implement rotate-controller on top of SugarTouchController

Signed-off-by: Carlos Garnacho <carlos@lanedo.com>
Acked-by: Simon Schampijer <simon@laptop.org>
This commit is contained in:
Carlos Garnacho 2012-09-13 14:34:36 +02:00 committed by Simon Schampijer
parent 1ba68fa120
commit 1fe3d4234d
2 changed files with 43 additions and 136 deletions

View File

@ -23,25 +23,14 @@
#include "sugar-rotate-controller.h" #include "sugar-rotate-controller.h"
typedef struct _SugarRotateControllerPriv SugarRotateControllerPriv; typedef struct _SugarRotateControllerPriv SugarRotateControllerPriv;
typedef struct _SugarTouch SugarTouch;
enum { enum {
ANGLE_CHANGED, ANGLE_CHANGED,
LAST_SIGNAL LAST_SIGNAL
}; };
struct _SugarTouch
{
GdkEventSequence *sequence;
gint x;
gint y;
guint set : 1;
};
struct _SugarRotateControllerPriv struct _SugarRotateControllerPriv
{ {
GdkDevice *device;
SugarTouch touches[2];
gdouble initial_angle; gdouble initial_angle;
}; };
@ -49,7 +38,7 @@ static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (SugarRotateController, G_DEFINE_TYPE (SugarRotateController,
sugar_rotate_controller, sugar_rotate_controller,
SUGAR_TYPE_EVENT_CONTROLLER) SUGAR_TYPE_TOUCH_CONTROLLER)
static void static void
sugar_rotate_controller_init (SugarRotateController *controller) sugar_rotate_controller_init (SugarRotateController *controller)
@ -65,30 +54,13 @@ sugar_rotate_controller_finalize (GObject *object)
G_OBJECT_CLASS (sugar_rotate_controller_parent_class)->finalize (object); G_OBJECT_CLASS (sugar_rotate_controller_parent_class)->finalize (object);
} }
static SugarTouch * static void
_sugar_rotate_controller_find_touch (SugarRotateController *controller, sugar_rotate_controller_constructed (GObject *object)
GdkEventSequence *sequence)
{ {
SugarRotateControllerPriv *priv; g_object_set (object,
gint unset = -1, i; "min-touches", 2,
"max-touches", 2,
priv = controller->_priv; NULL);
for (i = 0; i < 2; i++)
{
if (priv->touches[i].sequence == sequence)
return &priv->touches[i];
else if (!priv->touches[i].set && unset < 0)
unset = i;
}
if (unset < 0)
return NULL;
priv->touches[unset].sequence = sequence;
priv->touches[unset].set = TRUE;
return &priv->touches[unset];
} }
static gboolean static gboolean
@ -96,15 +68,24 @@ _sugar_rotate_controller_get_angle (SugarRotateController *controller,
gdouble *angle) gdouble *angle)
{ {
SugarRotateControllerPriv *priv; SugarRotateControllerPriv *priv;
gint x1, y1, x2, y2;
gdouble dx, dy; gdouble dx, dy;
GList *touches;
priv = controller->_priv; priv = controller->_priv;
if (!priv->touches[0].set || !priv->touches[1].set) if (sugar_touch_controller_get_num_touches (SUGAR_TOUCH_CONTROLLER (controller)) != 2)
return FALSE; return FALSE;
dx = priv->touches[0].x - priv->touches[1].x; touches = sugar_touch_controller_get_sequences (SUGAR_TOUCH_CONTROLLER (controller));
dy = priv->touches[0].y - priv->touches[1].y;
sugar_touch_controller_get_coords (SUGAR_TOUCH_CONTROLLER (controller),
touches->data, &x1, &y1);
sugar_touch_controller_get_coords (SUGAR_TOUCH_CONTROLLER (controller),
touches->next->data, &x2, &y2);
dx = x1 - x2;
dy = y1 - y2;
*angle = atan2 (dx, dy); *angle = atan2 (dx, dy);
@ -133,115 +114,40 @@ _sugar_rotate_controller_check_emit (SugarRotateController *controller)
return TRUE; return TRUE;
} }
static gboolean
sugar_rotate_controller_handle_event (SugarEventController *controller,
GdkEvent *event)
{
SugarRotateControllerPriv *priv;
GdkEventSequence *sequence;
gboolean handled = TRUE;
GdkDevice *device;
SugarTouch *touch;
priv = SUGAR_ROTATE_CONTROLLER (controller)->_priv;
device = gdk_event_get_device (event);
sequence = gdk_event_get_event_sequence (event);
if (priv->device && priv->device != device)
return FALSE;
touch = _sugar_rotate_controller_find_touch (SUGAR_ROTATE_CONTROLLER (controller),
sequence);
if (!touch)
return FALSE;
switch (event->type)
{
case GDK_TOUCH_BEGIN:
touch->x = event->touch.x;
touch->y = event->touch.y;
if (!priv->device)
priv->device = g_object_ref (device);
if (priv->touches[0].set && priv->touches[1].set)
{
_sugar_rotate_controller_get_angle (SUGAR_ROTATE_CONTROLLER (controller),
&priv->initial_angle);
g_signal_emit_by_name (G_OBJECT (controller), "started");
g_object_notify (G_OBJECT (controller), "state");
}
break;
case GDK_TOUCH_END:
touch->sequence = NULL;
touch->set = FALSE;
if (!priv->touches[0].set && !priv->touches[1].set)
{
g_object_unref (priv->device);
priv->device = NULL;
}
else if (priv->touches[0].set || priv->touches[1].set)
{
g_signal_emit_by_name (G_OBJECT (controller), "ended");
g_object_notify (G_OBJECT (controller), "state");
}
break;
case GDK_TOUCH_UPDATE:
touch->x = event->touch.x;
touch->y = event->touch.y;
_sugar_rotate_controller_check_emit (SUGAR_ROTATE_CONTROLLER (controller));
break;
default:
handled = FALSE;
break;
}
return handled;
}
SugarEventControllerState SugarEventControllerState
sugar_rotate_controller_get_state (SugarEventController *controller) sugar_rotate_controller_get_state (SugarEventController *controller)
{ {
SugarRotateControllerPriv *priv; SugarRotateControllerPriv *priv;
gint num_touches;
priv = SUGAR_ROTATE_CONTROLLER (controller)->_priv; priv = SUGAR_ROTATE_CONTROLLER (controller)->_priv;
num_touches = sugar_touch_controller_get_num_touches (SUGAR_TOUCH_CONTROLLER (controller));
if (priv->device) if (num_touches == 2)
{ return SUGAR_EVENT_CONTROLLER_STATE_RECOGNIZED;
if (priv->touches[0].set && priv->touches[1].set) else if (num_touches == 1)
return SUGAR_EVENT_CONTROLLER_STATE_RECOGNIZED; return SUGAR_EVENT_CONTROLLER_STATE_COLLECTING;
else if (priv->touches[0].set || priv->touches[1].set)
return SUGAR_EVENT_CONTROLLER_STATE_COLLECTING;
}
return SUGAR_EVENT_CONTROLLER_STATE_NONE; return SUGAR_EVENT_CONTROLLER_STATE_NONE;
} }
void void
sugar_rotate_controller_reset (SugarEventController *controller) sugar_rotate_controller_began (SugarEventController *controller)
{ {
SugarRotateControllerPriv *priv; SugarRotateControllerPriv *priv;
priv = SUGAR_ROTATE_CONTROLLER (controller)->_priv; priv = SUGAR_ROTATE_CONTROLLER (controller)->_priv;
_sugar_rotate_controller_get_angle (SUGAR_ROTATE_CONTROLLER (controller),
if (priv->touches[0].set && priv->touches[1].set) &priv->initial_angle);
g_signal_emit_by_name (G_OBJECT (controller), "ended");
priv->touches[0].sequence = NULL;
priv->touches[0].set = FALSE;
priv->touches[1].sequence = NULL;
priv->touches[1].set = FALSE;
if (priv->device)
{
g_object_unref (priv->device);
priv->device = NULL;
}
g_object_notify (G_OBJECT (controller), "state"); g_object_notify (G_OBJECT (controller), "state");
} }
void
sugar_rotate_controller_updated (SugarEventController *controller)
{
_sugar_rotate_controller_check_emit (SUGAR_ROTATE_CONTROLLER (controller));
}
static void static void
sugar_rotate_controller_class_init (SugarRotateControllerClass *klass) sugar_rotate_controller_class_init (SugarRotateControllerClass *klass)
{ {
@ -250,17 +156,18 @@ sugar_rotate_controller_class_init (SugarRotateControllerClass *klass)
object_class = G_OBJECT_CLASS (klass); object_class = G_OBJECT_CLASS (klass);
object_class->finalize = sugar_rotate_controller_finalize; object_class->finalize = sugar_rotate_controller_finalize;
object_class->constructed = sugar_rotate_controller_constructed;
controller_class = SUGAR_EVENT_CONTROLLER_CLASS (klass); controller_class = SUGAR_EVENT_CONTROLLER_CLASS (klass);
controller_class->handle_event = sugar_rotate_controller_handle_event;
controller_class->get_state = sugar_rotate_controller_get_state; controller_class->get_state = sugar_rotate_controller_get_state;
controller_class->reset = sugar_rotate_controller_reset; controller_class->began = sugar_rotate_controller_began;
controller_class->updated = sugar_rotate_controller_updated;
/** /**
* SugarRotateController::angle-changed: * SugarRotateController::angle-changed:
* @controller: the object on which the signal is emitted * @controller: the object on which the signal is emitted
* @angle: Current angle * @angle: Current angle in radians
* @angle_delta: Difference with the starting angle * @angle_delta: Difference with the starting angle in radians
*/ */
signals[ANGLE_CHANGED] = signals[ANGLE_CHANGED] =
g_signal_new ("angle-changed", g_signal_new ("angle-changed",
@ -288,7 +195,7 @@ sugar_rotate_controller_new (void)
* *
* If @controller is on state %SUGAR_EVENT_CONTROLLER_STATE_RECOGNIZED, * If @controller is on state %SUGAR_EVENT_CONTROLLER_STATE_RECOGNIZED,
* this function returns %TRUE and fills in @delta with the angle difference * this function returns %TRUE and fills in @delta with the angle difference
* since the gesture was first recognized. * in radians since the gesture was first recognized.
* *
* Returns: %TRUE if @controller is recognizing a rotate gesture * Returns: %TRUE if @controller is recognizing a rotate gesture
**/ **/

View File

@ -26,7 +26,7 @@
#ifndef __SUGAR_ROTATE_CONTROLLER_H__ #ifndef __SUGAR_ROTATE_CONTROLLER_H__
#define __SUGAR_ROTATE_CONTROLLER_H__ #define __SUGAR_ROTATE_CONTROLLER_H__
#include "sugar-event-controller.h" #include "sugar-touch-controller.h"
#include <gtk/gtk.h> #include <gtk/gtk.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -43,13 +43,13 @@ typedef struct _SugarRotateControllerClass SugarRotateControllerClass;
struct _SugarRotateController struct _SugarRotateController
{ {
SugarEventController parent_instance; SugarTouchController parent_instance;
gpointer _priv; gpointer _priv;
}; };
struct _SugarRotateControllerClass struct _SugarRotateControllerClass
{ {
SugarEventControllerClass parent_class; SugarTouchControllerClass parent_class;
void (* angle_changed) (SugarRotateController *controller, void (* angle_changed) (SugarRotateController *controller,
gdouble angle, gdouble angle,