event-controller: Implement zoom-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:37:02 +02:00 committed by Simon Schampijer
parent 1fe3d4234d
commit aa652b8754
2 changed files with 42 additions and 134 deletions

View File

@ -23,25 +23,14 @@
#include "sugar-zoom-controller.h" #include "sugar-zoom-controller.h"
typedef struct _SugarZoomControllerPriv SugarZoomControllerPriv; typedef struct _SugarZoomControllerPriv SugarZoomControllerPriv;
typedef struct _SugarTouch SugarTouch;
enum { enum {
ZOOM_CHANGED, SCALE_CHANGED,
LAST_SIGNAL LAST_SIGNAL
}; };
struct _SugarTouch
{
GdkEventSequence *sequence;
gint x;
gint y;
guint set : 1;
};
struct _SugarZoomControllerPriv struct _SugarZoomControllerPriv
{ {
GdkDevice *device;
SugarTouch touches[2];
gdouble initial_distance; gdouble initial_distance;
}; };
@ -49,7 +38,7 @@ static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (SugarZoomController, G_DEFINE_TYPE (SugarZoomController,
sugar_zoom_controller, sugar_zoom_controller,
SUGAR_TYPE_EVENT_CONTROLLER) SUGAR_TYPE_TOUCH_CONTROLLER)
static void static void
sugar_zoom_controller_init (SugarZoomController *controller) sugar_zoom_controller_init (SugarZoomController *controller)
@ -65,29 +54,13 @@ sugar_zoom_controller_finalize (GObject *object)
G_OBJECT_CLASS (sugar_zoom_controller_parent_class)->finalize (object); G_OBJECT_CLASS (sugar_zoom_controller_parent_class)->finalize (object);
} }
static SugarTouch * static void
_sugar_zoom_controller_find_touch (SugarZoomController *controller, sugar_zoom_controller_constructed (GObject *object)
GdkEventSequence *sequence)
{ {
SugarZoomControllerPriv *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
@ -95,15 +68,24 @@ _sugar_zoom_controller_get_distance (SugarZoomController *controller,
gdouble *distance) gdouble *distance)
{ {
SugarZoomControllerPriv *priv; SugarZoomControllerPriv *priv;
gint x1, y1, x2, y2;
GList *touches;
gdouble dx, dy; gdouble dx, dy;
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;;
*distance = sqrt ((dx * dx) + (dy * dy)); *distance = sqrt ((dx * dx) + (dy * dy));
return TRUE; return TRUE;
@ -129,115 +111,40 @@ _sugar_zoom_controller_check_emit (SugarZoomController *controller)
return TRUE; return TRUE;
} }
static gboolean
sugar_zoom_controller_handle_event (SugarEventController *controller,
GdkEvent *event)
{
SugarZoomControllerPriv *priv;
GdkEventSequence *sequence;
gboolean handled = TRUE;
GdkDevice *device;
SugarTouch *touch;
priv = SUGAR_ZOOM_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_zoom_controller_find_touch (SUGAR_ZOOM_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_zoom_controller_get_distance (SUGAR_ZOOM_CONTROLLER (controller),
&priv->initial_distance);
g_signal_emit_by_name (G_OBJECT (controller), "began");
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_zoom_controller_check_emit (SUGAR_ZOOM_CONTROLLER (controller));
break;
default:
handled = FALSE;
break;
}
return handled;
}
SugarEventControllerState SugarEventControllerState
sugar_zoom_controller_get_state (SugarEventController *controller) sugar_zoom_controller_get_state (SugarEventController *controller)
{ {
SugarZoomControllerPriv *priv; SugarZoomControllerPriv *priv;
gint num_touches;
priv = SUGAR_ZOOM_CONTROLLER (controller)->_priv; priv = SUGAR_ZOOM_CONTROLLER (controller)->_priv;
num_touches = sugar_touch_controller_get_num_touches (SUGAR_TOUCH_CONTROLLER (controller));
if (priv->device) if (num_touches == 2)
{
if (priv->touches[0].set && priv->touches[1].set)
return SUGAR_EVENT_CONTROLLER_STATE_RECOGNIZED; return SUGAR_EVENT_CONTROLLER_STATE_RECOGNIZED;
else if (priv->touches[0].set || priv->touches[1].set) else if (num_touches == 1)
return SUGAR_EVENT_CONTROLLER_STATE_COLLECTING; return SUGAR_EVENT_CONTROLLER_STATE_COLLECTING;
}
return SUGAR_EVENT_CONTROLLER_STATE_NONE; return SUGAR_EVENT_CONTROLLER_STATE_NONE;
} }
void static void
sugar_zoom_controller_reset (SugarEventController *controller) sugar_zoom_controller_began (SugarEventController *controller)
{ {
SugarZoomControllerPriv *priv; SugarZoomControllerPriv *priv;
priv = SUGAR_ZOOM_CONTROLLER (controller)->_priv; priv = SUGAR_ZOOM_CONTROLLER (controller)->_priv;
_sugar_zoom_controller_get_distance (SUGAR_ZOOM_CONTROLLER (controller),
if (priv->touches[0].set && priv->touches[1].set) &priv->initial_distance);
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");
} }
static void
sugar_zoom_controller_updated (SugarEventController *controller)
{
_sugar_zoom_controller_check_emit (SUGAR_ZOOM_CONTROLLER (controller));
}
static void static void
sugar_zoom_controller_class_init (SugarZoomControllerClass *klass) sugar_zoom_controller_class_init (SugarZoomControllerClass *klass)
{ {
@ -246,11 +153,12 @@ sugar_zoom_controller_class_init (SugarZoomControllerClass *klass)
object_class = G_OBJECT_CLASS (klass); object_class = G_OBJECT_CLASS (klass);
object_class->finalize = sugar_zoom_controller_finalize; object_class->finalize = sugar_zoom_controller_finalize;
object_class->constructed = sugar_zoom_controller_constructed;
controller_class = SUGAR_EVENT_CONTROLLER_CLASS (klass); controller_class = SUGAR_EVENT_CONTROLLER_CLASS (klass);
controller_class->handle_event = sugar_zoom_controller_handle_event;
controller_class->get_state = sugar_zoom_controller_get_state; controller_class->get_state = sugar_zoom_controller_get_state;
controller_class->reset = sugar_zoom_controller_reset; controller_class->began = sugar_zoom_controller_began;
controller_class->updated = sugar_zoom_controller_updated;
/** /**
* SugarZoomController::zoom-changed: * SugarZoomController::zoom-changed:

View File

@ -26,7 +26,7 @@
#ifndef __SUGAR_ZOOM_CONTROLLER_H__ #ifndef __SUGAR_ZOOM_CONTROLLER_H__
#define __SUGAR_ZOOM_CONTROLLER_H__ #define __SUGAR_ZOOM_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 _SugarZoomControllerClass SugarZoomControllerClass;
struct _SugarZoomController struct _SugarZoomController
{ {
SugarEventController parent_instance; SugarTouchController parent_instance;
gpointer _priv; gpointer _priv;
}; };
struct _SugarZoomControllerClass struct _SugarZoomControllerClass
{ {
SugarEventControllerClass parent_class; SugarTouchControllerClass parent_class;
void (* zoom_changed) (SugarZoomController *controller, void (* zoom_changed) (SugarZoomController *controller,
gdouble zoom); gdouble zoom);