Merge from https://bitbucket.org/keestux/sdl ... SDL_gesture code cleanup.

This commit is contained in:
Ryan C. Gordon 2011-11-08 16:24:02 -05:00
commit 311a3e9453

View file

@ -63,7 +63,7 @@ typedef struct {
} SDL_DollarTemplate; } SDL_DollarTemplate;
typedef struct { typedef struct {
SDL_GestureID id; SDL_TouchID id;
SDL_FloatPoint res; SDL_FloatPoint res;
SDL_FloatPoint centroid; SDL_FloatPoint centroid;
SDL_DollarPath dollarPath; SDL_DollarPath dollarPath;
@ -80,7 +80,8 @@ int SDL_numGestureTouches = 0;
SDL_bool recordAll; SDL_bool recordAll;
#if 0 #if 0
static void PrintPath(SDL_FloatPoint *path) { static void PrintPath(SDL_FloatPoint *path)
{
int i; int i;
printf("Path:"); printf("Path:");
for (i=0; i<DOLLARNPOINTS; i++) { for (i=0; i<DOLLARNPOINTS; i++) {
@ -90,7 +91,8 @@ static void PrintPath(SDL_FloatPoint *path) {
} }
#endif #endif
int SDL_RecordGesture(SDL_TouchID touchId) { int SDL_RecordGesture(SDL_TouchID touchId)
{
int i; int i;
if (touchId < 0) recordAll = SDL_TRUE; if (touchId < 0) recordAll = SDL_TRUE;
for (i = 0; i < SDL_numGestureTouches; i++) { for (i = 0; i < SDL_numGestureTouches; i++) {
@ -103,7 +105,8 @@ int SDL_RecordGesture(SDL_TouchID touchId) {
return (touchId < 0); return (touchId < 0);
} }
static unsigned long SDL_HashDollar(SDL_FloatPoint* points) { static unsigned long SDL_HashDollar(SDL_FloatPoint* points)
{
unsigned long hash = 5381; unsigned long hash = 5381;
int i; int i;
for (i = 0; i < DOLLARNPOINTS; i++) { for (i = 0; i < DOLLARNPOINTS; i++) {
@ -114,7 +117,8 @@ static unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
} }
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) { static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src)
{
if (src == NULL) return 0; if (src == NULL) return 0;
@ -129,7 +133,8 @@ static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
} }
int SDL_SaveAllDollarTemplates(SDL_RWops *src) { int SDL_SaveAllDollarTemplates(SDL_RWops *src)
{
int i,j,rtrn = 0; int i,j,rtrn = 0;
for (i = 0; i < SDL_numGestureTouches; i++) { for (i = 0; i < SDL_numGestureTouches; i++) {
SDL_GestureTouch* touch = &SDL_gestureTouch[i]; SDL_GestureTouch* touch = &SDL_gestureTouch[i];
@ -140,7 +145,8 @@ int SDL_SaveAllDollarTemplates(SDL_RWops *src) {
return rtrn; return rtrn;
} }
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) { int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src)
{
int i,j; int i,j;
for (i = 0; i < SDL_numGestureTouches; i++) { for (i = 0; i < SDL_numGestureTouches; i++) {
SDL_GestureTouch* touch = &SDL_gestureTouch[i]; SDL_GestureTouch* touch = &SDL_gestureTouch[i];
@ -156,56 +162,53 @@ int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
//path is an already sampled set of points //path is an already sampled set of points
//Returns the index of the gesture on success, or -1 //Returns the index of the gesture on success, or -1
static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) { static int SDL_AddDollarGesture_one(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
{
SDL_DollarTemplate* dollarTemplate; SDL_DollarTemplate* dollarTemplate;
SDL_DollarTemplate *templ; SDL_DollarTemplate *templ;
int index;
index = inTouch->numDollarTemplates;
dollarTemplate =
(SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
(index + 1) *
sizeof(SDL_DollarTemplate));
if (!dollarTemplate) {
SDL_OutOfMemory();
return -1;
}
inTouch->dollarTemplate = dollarTemplate;
templ = &inTouch->dollarTemplate[index];
SDL_memcpy(templ->path, path, DOLLARNPOINTS*sizeof(SDL_FloatPoint));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
return index;
}
static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
{
int index;
int i = 0; int i = 0;
if (inTouch == NULL) { if (inTouch == NULL) {
if (SDL_numGestureTouches == 0) return -1; if (SDL_numGestureTouches == 0) return -1;
for (i = 0; i < SDL_numGestureTouches; i++) { for (i = 0; i < SDL_numGestureTouches; i++) {
inTouch = &SDL_gestureTouch[i]; inTouch = &SDL_gestureTouch[i];
index = SDL_AddDollarGesture_one(inTouch, path);
dollarTemplate = if (index < 0)
(SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
(inTouch->numDollarTemplates + 1) *
sizeof(SDL_DollarTemplate));
if(!dollarTemplate) {
SDL_OutOfMemory();
return -1; return -1;
} }
// Use the index of the last one added.
inTouch->dollarTemplate = dollarTemplate; return index;
templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
}
return inTouch->numDollarTemplates - 1;
} else { } else {
SDL_DollarTemplate* dollarTemplate = return SDL_AddDollarGesture_one(inTouch, path);
( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
(inTouch->numDollarTemplates + 1) *
sizeof(SDL_DollarTemplate));
if(!dollarTemplate) {
SDL_OutOfMemory();
return -1;
}
inTouch->dollarTemplate = dollarTemplate;
templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
return inTouch->numDollarTemplates - 1;
} }
return -1; return -1;
} }
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) { int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
{
int i,loaded = 0; int i,loaded = 0;
SDL_GestureTouch *touch = NULL; SDL_GestureTouch *touch = NULL;
if (src == NULL) return 0; if (src == NULL) return 0;
@ -224,7 +227,8 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
if (touchId >= 0) { if (touchId >= 0) {
//printf("Adding loaded gesture to 1 touch\n"); //printf("Adding loaded gesture to 1 touch\n");
if(SDL_AddDollarGesture(touch,templ.path)) loaded++; if (SDL_AddDollarGesture(touch, templ.path) >= 0)
loaded++;
} }
else { else {
//printf("Adding to: %i touches\n",SDL_numGestureTouches); //printf("Adding to: %i touches\n",SDL_numGestureTouches);
@ -242,7 +246,8 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
} }
static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) { static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang)
{
// SDL_FloatPoint p[DOLLARNPOINTS]; // SDL_FloatPoint p[DOLLARNPOINTS];
float dist = 0; float dist = 0;
SDL_FloatPoint p; SDL_FloatPoint p;
@ -257,7 +262,8 @@ static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float
} }
static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) { static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
{
//------------BEGIN DOLLAR BLACKBOX----------------// //------------BEGIN DOLLAR BLACKBOX----------------//
//-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-// //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
//-"http://depts.washington.edu/aimgroup/proj/dollar/"-// //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
@ -294,7 +300,8 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
} }
//DollarPath contains raw points, plus (possibly) the calculated length //DollarPath contains raw points, plus (possibly) the calculated length
static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) { static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points)
{
int i; int i;
float interval; float interval;
float dist; float dist;
@ -308,10 +315,8 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
//Calculate length if it hasn't already been done //Calculate length if it hasn't already been done
if (length <= 0) { if (length <= 0) {
for (i=1;i < path->numPoints; i++) { for (i=1;i < path->numPoints; i++) {
float dx = path->p[i ].x - float dx = path->p[i ].x - path->p[i-1].x;
path->p[i-1].x; float dy = path->p[i ].y - path->p[i-1].y;
float dy = path->p[i ].y -
path->p[i-1].y;
length += (float)(SDL_sqrt(dx*dx+dy*dy)); length += (float)(SDL_sqrt(dx*dx+dy*dy));
} }
} }
@ -387,7 +392,8 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
return numPoints; return numPoints;
} }
static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch) { static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch)
{
SDL_FloatPoint points[DOLLARNPOINTS]; SDL_FloatPoint points[DOLLARNPOINTS];
int i; int i;
@ -404,7 +410,8 @@ static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_Gestu
return bestDiff; return bestDiff;
} }
int SDL_GestureAddTouch(SDL_Touch* touch) { int SDL_GestureAddTouch(SDL_Touch* touch)
{
SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch, SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
(SDL_numGestureTouches + 1) * (SDL_numGestureTouches + 1) *
sizeof(SDL_GestureTouch)); sizeof(SDL_GestureTouch));
@ -431,16 +438,19 @@ int SDL_GestureAddTouch(SDL_Touch* touch) {
return 0; return 0;
} }
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) { static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
{
int i; int i;
for (i = 0; i < SDL_numGestureTouches; i++) { for (i = 0; i < SDL_numGestureTouches; i++) {
//printf("%i ?= %i\n",SDL_gestureTouch[i].id,id); //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i]; if (SDL_gestureTouch[i].id == id)
return &SDL_gestureTouch[i];
} }
return NULL; return NULL;
} }
static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist) { int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist)
{
SDL_Event event; SDL_Event event;
event.mgesture.type = SDL_MULTIGESTURE; event.mgesture.type = SDL_MULTIGESTURE;
event.mgesture.touchId = touch->id; event.mgesture.touchId = touch->id;
@ -453,7 +463,8 @@ static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist
} }
static int SDL_SendGestureDollar(SDL_GestureTouch* touch, static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
SDL_GestureID gestureId,float error) { SDL_GestureID gestureId,float error)
{
SDL_Event event; SDL_Event event;
event.dgesture.type = SDL_DOLLARGESTURE; event.dgesture.type = SDL_DOLLARGESTURE;
event.dgesture.touchId = touch->id; event.dgesture.touchId = touch->id;
@ -470,7 +481,8 @@ static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
} }
static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId) { static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId)
{
SDL_Event event; SDL_Event event;
event.dgesture.type = SDL_DOLLARRECORD; event.dgesture.type = SDL_DOLLARRECORD;
event.dgesture.touchId = touch->id; event.dgesture.touchId = touch->id;
@ -649,4 +661,3 @@ void SDL_GestureProcessEvent(SDL_Event* event)
} }
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */