Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Added some files I had previosuly missed
  • Loading branch information
jimtla committed Jul 11, 2010
1 parent d2f4e83 commit 42b3698
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 319 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Expand Up @@ -44,7 +44,7 @@ EMBEDSPU = @EMBEDSPU@

DIST = acinclude autogen.sh Borland.html Borland.zip BUGS build-scripts configure configure.in COPYING CREDITS include INSTALL Makefile.minimal Makefile.in README* sdl-config.in sdl.m4 sdl.pc.in SDL.spec SDL.spec.in src test TODO VisualC.html VisualC VisualCE Watcom-Win32.zip WhatsNew Xcode Xcode-iPhoneOS

HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h SDL_touch.h
HDRS = SDL.h SDL_assert.h SDL_atomic.h SDL_audio.h SDL_compat.h SDL_cpuinfo.h SDL_endian.h SDL_error.h SDL_events.h SDL_haptic.h SDL_joystick.h SDL_keyboard.h SDL_keysym.h SDL_loadso.h SDL_main.h SDL_mouse.h SDL_mutex.h SDL_name.h SDL_opengl.h SDL_opengles.h SDL_pixels.h SDL_platform.h SDL_power.h SDL_quit.h SDL_rect.h SDL_revision.h SDL_rwops.h SDL_scancode.h SDL_stdinc.h SDL_surface.h SDL_syswm.h SDL_thread.h SDL_timer.h SDL_types.h SDL_version.h SDL_video.h begin_code.h close_code.h SDL_touch.h SDL_gesture.h

LT_AGE = @LT_AGE@
LT_CURRENT = @LT_CURRENT@
Expand Down
6 changes: 5 additions & 1 deletion include/SDL_events.h
Expand Up @@ -36,6 +36,8 @@
#include "SDL_mouse.h"
#include "SDL_joystick.h"
#include "SDL_quit.h"
#include "SDL_gesture.h"
#include "SDL_touch.h"

#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
Expand Down Expand Up @@ -95,6 +97,7 @@ typedef enum

/*Gesture events*/
SDL_DOLLARGESTURE = 0x800,
SDL_DOLLARRECORD,
SDL_MULTIGESTURE,

/* Obsolete events */
Expand Down Expand Up @@ -359,9 +362,10 @@ typedef struct SDL_DollarGestureEvent
Uint32 type; /**< ::SDL_DOLLARGESTURE */
Uint32 windowID; /**< The window with mouse focus, if any */
Uint8 touchId; /**< The touch device index */
Uint8 gestureId;
Uint8 padding1;
Uint8 padding2;
Uint8 padding3;
unsigned long gestureId;
float error;
/*
//TODO: Enable to give location?
Expand Down
90 changes: 90 additions & 0 deletions include/SDL_gesture.h
@@ -0,0 +1,90 @@
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2010 Sam Lantinga
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.1 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/

/**
* \file SDL_gesture.h
*
* Include file for SDL gesture event handling.
*/

#ifndef _SDL_gesture_h
#define _SDL_gesture_h

#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "SDL_video.h"

#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C" {
/* *INDENT-ON* */
#endif


/* Function prototypes */

/**
* \brief Begin Recording a gesture on the specified touch, or all touches (-1)
*
*
*/
extern DECLSPEC int SDLCALL SDL_RecordGesture(int touchId);


/**
* \brief Save all currently loaded Dollar Gesture templates
*
*
*/
extern DECLSPEC int SDLCALL SDL_SaveAllDollarTemplates(FILE *fp);

/**
* \brief Save a currently loaded Dollar Gesture template
*
*
*/
extern DECLSPEC int
SDLCALL SDL_SaveDollarTemplate(unsigned long gestureId,FILE *fp);


/**
* \brief Load Dollar Gesture templates from a file
*
*
*/
extern DECLSPEC int SDLCALL SDL_LoadDollarTemplates(int touchId, FILE *fp);



/* Ends C function definitions when using C++ */
#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif
#include "close_code.h"

#endif /* _SDL_gesture_h */

/* vi: set ts=4 sw=4 expandtab: */
2 changes: 1 addition & 1 deletion include/SDL_touch.h
Expand Up @@ -23,7 +23,7 @@
/**
* \file SDL_touch.h
*
* Include file for SDL mouse event handling.
* Include file for SDL touch event handling.
*/

#ifndef _SDL_touch_h
Expand Down
200 changes: 183 additions & 17 deletions src/events/SDL_gesture.c
Expand Up @@ -13,8 +13,7 @@
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
License along with this library; if not, write to the Free Software Founation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
Expand Down Expand Up @@ -66,6 +65,10 @@ typedef struct {
DollarPath dollarPath;
} TouchPoint;

typedef struct {
Point path[DOLLARNPOINTS];
unsigned long hash;
} DollarTemplate;

typedef struct {
int id;
Expand All @@ -75,11 +78,140 @@ typedef struct {
int numDownFingers;

int numDollarTemplates;
Point dollarTemplate[MAXTEMPLATES][DOLLARNPOINTS];
DollarTemplate dollarTemplate[MAXTEMPLATES];

SDL_bool recording;
} GestureTouch;

GestureTouch gestureTouch[MAXTOUCHES];
int numGestureTouches = 0;
SDL_bool recordAll;

int SDL_RecordGesture(int touchId) {
int i;
if(touchId < 0) recordAll = SDL_TRUE;
for(i = 0;i < numGestureTouches; i++) {
if((touchId < 0) || (gestureTouch[i].id == touchId)) {
gestureTouch[i].recording = SDL_TRUE;
if(touchId >= 0)
return 1;
}
}
return (touchId < 0);
}

unsigned long SDL_HashDollar(Point* points) {
unsigned long hash = 5381;
int i;
for(i = 0;i < DOLLARNPOINTS; i++) {
hash = ((hash<<5) + hash) + points[i].x;
hash = ((hash<<5) + hash) + points[i].y;
}
return hash;
}

int SaveTemplate(DollarTemplate *templ, FILE *fp) {
int i;
fprintf(fp,"%lu ",templ->hash);
for(i = 0;i < DOLLARNPOINTS;i++) {
fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
}
fprintf(fp,"\n");
}


int SDL_SaveAllDollarTemplates(FILE *fp) {
int i,j,rtrn = 0;
for(i = 0; i < numGestureTouches; i++) {
GestureTouch* touch = &gestureTouch[i];
for(j = 0;j < touch->numDollarTemplates; j++) {
rtrn += SaveTemplate(&touch->dollarTemplate[i],fp);
}
}
return rtrn;
}

int SDL_SaveDollarTemplate(unsigned long gestureId, FILE *fp) {
int i,j;
for(i = 0; i < numGestureTouches; i++) {
GestureTouch* touch = &gestureTouch[i];
for(j = 0;j < touch->numDollarTemplates; j++) {
if(touch->dollarTemplate[i].hash == gestureId) {
return SaveTemplate(&touch->dollarTemplate[i],fp);
}
}
}
}

int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
int i,loaded = 0;
GestureTouch *touch = NULL;
if(touchId >= 0) {
for(i = 0;i < numGestureTouches; i++)
if(gestureTouch[i].id == touchId)
touch = &gestureTouch[i];
if(touch == NULL) return -1;
}

while(!feof(fp)) {
DollarTemplate templ;
fscanf(fp,"%lu ",&templ.hash);
for(i = 0;i < DOLLARNPOINTS; i++) {
int x,y;
if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
templ.path[i].x = x;
templ.path[i].y = y;
}
fscanf(fp,"\n");

if(touchId >= 0) {
if(SDL_AddDollarGesture(touch,templ)) loaded++;
}
else {
for(i = 0;i < numGestureTouches; i++) {
if(gestureTouch[i].id == touchId) {
touch = &gestureTouch[i];
SDL_AddDollarGesture(touch,templ);
}
}
loaded++;
}
}

return 1;
}


//path is an already sampled set of points
//Returns the index of the gesture on success, or -1
int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
if(inTouch == NULL) {
if(numGestureTouches == 0) return -1;
int i = 0;
for(i = 0;i < numGestureTouches; i++) {
inTouch = &gestureTouch[i];
if(inTouch->numDollarTemplates < MAXTEMPLATES) {
DollarTemplate *templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
}
}
return inTouch->numDollarTemplates - 1;
}else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
DollarTemplate *templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
return inTouch->numDollarTemplates - 1;
}
return -1;
}




float dollarDifference(Point* points,Point* templ,float ang) {
// Point p[DOLLARNPOINTS];
Expand Down Expand Up @@ -141,7 +273,7 @@ float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
int bestDiff = 10000;
*bestTempl = -1;
for(i = 0;i < touch->numDollarTemplates;i++) {
int diff = bestDollarDifference(points,touch->dollarTemplate[i]);
int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
}
return bestDiff;
Expand Down Expand Up @@ -239,6 +371,8 @@ int SDL_GestureAddTouch(SDL_Touch* touch) {

gestureTouch[numGestureTouches].numDollarTemplates = 0;

gestureTouch[numGestureTouches].recording = SDL_FALSE;

numGestureTouches++;
return 0;
}
Expand Down Expand Up @@ -277,6 +411,17 @@ int SDL_SendGestureDollar(GestureTouch* touch,int gestureId,float error) {
return SDL_PushEvent(&event) > 0;
}


int SDL_SendDollarRecord(GestureTouch* touch,int gestureId) {
SDL_Event event;
event.dgesture.type = SDL_DOLLARRECORD;
event.dgesture.touchId = touch->id;
event.dgesture.gestureId = gestureId;

return SDL_PushEvent(&event) > 0;
}


void SDL_GestureProcessEvent(SDL_Event* event)
{
if(event->type == SDL_FINGERMOTION ||
Expand All @@ -294,23 +439,44 @@ void SDL_GestureProcessEvent(SDL_Event* event)

for(j = 0;j<inTouch->numDownFingers;j++) {
if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;

//Finger Up
if(event->type == SDL_FINGERUP) {
inTouch->numDownFingers--;

int bestTempl;
float error;
error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
&bestTempl,inTouch);
if(bestTempl >= 0){
//Send Event
int gestureId = 0; //?
SDL_SendGestureDollar(inTouch->id,gestureId,error);


printf("Dollar error: %f\n",error);
if(inTouch->recording) {
inTouch->recording = SDL_FALSE;
Point path[DOLLARNPOINTS];
dollarNormalize(inTouch->gestureLast[j].dollarPath,path);
int index;
if(recordAll) {
index = SDL_AddDollarGesture(NULL,path);
int i;
for(i = 0;i < numGestureTouches; i++)
gestureTouch[i].recording = SDL_FALSE;
}
else {
index = SDL_AddDollarGesture(inTouch,path);
}

if(index >= 0) {
SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
}
else {
SDL_SendDollarRecord(inTouch,-1);
}
}

else {
int bestTempl;
float error;
error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
&bestTempl,inTouch);
if(bestTempl >= 0){
//Send Event
int gestureId = inTouch->dollarTemplate[bestTempl].hash;
SDL_SendGestureDollar(inTouch,gestureId,error);
printf("Dollar error: %f\n",error);
}
}
inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
break;
}
Expand Down

0 comments on commit 42b3698

Please sign in to comment.