src/events/SDL_gesture.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 7009 161b7b6a5303
child 7191 75360622e65f
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


Fixes Bugzilla #1778.
jim@4657
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
jim@4657
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
jim@4657
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
jim@4657
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
kees@6065
    14
  claim that you wrote the original software. If you use this software
kees@6065
    15
  in a product, an acknowledgment in the product documentation would be
kees@6065
    16
  appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
kees@6065
    18
  misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
jim@4657
    20
*/
aschiffler@4865
    21
jim@4657
    22
#include "SDL_config.h"
jim@4657
    23
jim@4657
    24
/* General mouse handling code for SDL */
jim@4657
    25
jim@4657
    26
#include "SDL_events.h"
jim@4657
    27
#include "SDL_events_c.h"
jim@4657
    28
#include "SDL_gesture_c.h"
jim@4657
    29
kimonline@7009
    30
#if !defined(__PSP__)
aschiffler@4865
    31
#include <memory.h>
kimonline@7009
    32
#endif
kimonline@7009
    33
aschiffler@4865
    34
#include <string.h>
aschiffler@4865
    35
#include <stdio.h>
aschiffler@4865
    36
#include <math.h>
aschiffler@4865
    37
jim@4657
    38
//TODO: Replace with malloc
jim@4688
    39
jim@4658
    40
#define MAXPATHSIZE 1024
jim@4658
    41
kees@6065
    42
jim@4688
    43
jim@4688
    44
jim@4658
    45
#define DOLLARNPOINTS 64
jim@4658
    46
#define DOLLARSIZE 256
jim@4658
    47
jim@4684
    48
#define ENABLE_DOLLAR
jim@4684
    49
kees@6065
    50
#define PHI 0.618033989
jim@4657
    51
jim@4657
    52
typedef struct {
kees@6065
    53
    float x,y;
jim@4688
    54
} SDL_FloatPoint;
jim@4658
    55
jim@4658
    56
typedef struct {
kees@6065
    57
    float length;
kees@6065
    58
kees@6065
    59
    int numPoints;
kees@6065
    60
    SDL_FloatPoint p[MAXPATHSIZE];
jim@4688
    61
} SDL_DollarPath;
jim@4658
    62
jim@4657
    63
typedef struct {
kees@6065
    64
    SDL_FloatPoint path[DOLLARNPOINTS];
kees@6065
    65
    unsigned long hash;
jim@4688
    66
} SDL_DollarTemplate;
jim@4657
    67
jim@4657
    68
typedef struct {
kees@6069
    69
    SDL_TouchID id;
kees@6065
    70
    SDL_FloatPoint centroid;
kees@6065
    71
    SDL_DollarPath dollarPath;
kees@6065
    72
    Uint16 numDownFingers;
jim@4658
    73
kees@6065
    74
    int numDollarTemplates;
kees@6065
    75
    SDL_DollarTemplate *dollarTemplate;
jim@4659
    76
kees@6065
    77
    SDL_bool recording;
jim@4688
    78
} SDL_GestureTouch;
jim@4657
    79
jim@4688
    80
SDL_GestureTouch *SDL_gestureTouch;
jim@4688
    81
int SDL_numGestureTouches = 0;
jim@4659
    82
SDL_bool recordAll;
jim@4659
    83
slouken@4868
    84
#if 0
kees@6065
    85
static void PrintPath(SDL_FloatPoint *path)
kees@6065
    86
{
kees@6065
    87
    int i;
kees@6065
    88
    printf("Path:");
kees@6066
    89
    for (i=0; i<DOLLARNPOINTS; i++) {
kees@6065
    90
        printf(" (%f,%f)",path[i].x,path[i].y);
kees@6065
    91
    }
kees@6065
    92
    printf("\n");
jim@4685
    93
}
slouken@4868
    94
#endif
jim@4685
    95
kees@6065
    96
int SDL_RecordGesture(SDL_TouchID touchId)
kees@6065
    97
{
kees@6065
    98
    int i;
kees@6066
    99
    if (touchId < 0) recordAll = SDL_TRUE;
kees@6066
   100
    for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6066
   101
        if ((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
kees@6065
   102
            SDL_gestureTouch[i].recording = SDL_TRUE;
kees@6066
   103
            if (touchId >= 0)
kees@6065
   104
                return 1;
kees@6065
   105
        }
kees@6065
   106
    }
kees@6065
   107
    return (touchId < 0);
jim@4659
   108
}
jim@4659
   109
icculus@6070
   110
static unsigned long SDL_HashDollar(SDL_FloatPoint* points)
kees@6065
   111
{
kees@6065
   112
    unsigned long hash = 5381;
kees@6065
   113
    int i;
kees@6066
   114
    for (i = 0; i < DOLLARNPOINTS; i++) {
kees@6065
   115
        hash = ((hash<<5) + hash) + (unsigned long)points[i].x;
kees@6065
   116
        hash = ((hash<<5) + hash) + (unsigned long)points[i].y;
kees@6065
   117
    }
kees@6065
   118
    return hash;
jim@4659
   119
}
jim@4659
   120
jim@4665
   121
kees@6065
   122
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src)
kees@6065
   123
{
kees@6066
   124
    if (src == NULL) return 0;
jim@4665
   125
jim@4682
   126
kees@6065
   127
    //No Longer storing the Hash, rehash on load
kees@6065
   128
    //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
kees@6065
   129
kees@6066
   130
    if (SDL_RWwrite(src,templ->path,
kees@6066
   131
                    sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
kees@6065
   132
        return 0;
kees@6065
   133
kees@6065
   134
    return 1;
jim@4659
   135
}
jim@4659
   136
jim@4659
   137
kees@6065
   138
int SDL_SaveAllDollarTemplates(SDL_RWops *src)
kees@6065
   139
{
kees@6065
   140
    int i,j,rtrn = 0;
kees@6066
   141
    for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   142
        SDL_GestureTouch* touch = &SDL_gestureTouch[i];
kees@6066
   143
        for (j = 0; j < touch->numDollarTemplates; j++) {
kees@6065
   144
            rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
kees@6065
   145
        }
jim@4659
   146
    }
kees@6065
   147
    return rtrn;
jim@4659
   148
}
jim@4659
   149
kees@6065
   150
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src)
kees@6065
   151
{
kees@6065
   152
    int i,j;
kees@6066
   153
    for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   154
        SDL_GestureTouch* touch = &SDL_gestureTouch[i];
kees@6066
   155
        for (j = 0; j < touch->numDollarTemplates; j++) {
kees@6066
   156
            if (touch->dollarTemplate[i].hash == gestureId) {
kees@6065
   157
                return SaveTemplate(&touch->dollarTemplate[i],src);
kees@6065
   158
            }
kees@6065
   159
        }
jim@4659
   160
    }
icculus@7037
   161
    return SDL_SetError("Unknown gestureId");
slouken@4663
   162
}
slouken@4663
   163
slouken@4663
   164
//path is an already sampled set of points
slouken@4663
   165
//Returns the index of the gesture on success, or -1
kees@6067
   166
static int SDL_AddDollarGesture_one(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
kees@6065
   167
{
kees@6065
   168
    SDL_DollarTemplate* dollarTemplate;
kees@6065
   169
    SDL_DollarTemplate *templ;
kees@6067
   170
    int index;
kees@6067
   171
kees@6067
   172
    index = inTouch->numDollarTemplates;
kees@6067
   173
    dollarTemplate =
kees@6067
   174
        (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
kees@6067
   175
                                          (index + 1) *
kees@6067
   176
                                          sizeof(SDL_DollarTemplate));
kees@6067
   177
    if (!dollarTemplate) {
icculus@7037
   178
        return SDL_OutOfMemory();
kees@6067
   179
    }
kees@6067
   180
    inTouch->dollarTemplate = dollarTemplate;
kees@6067
   181
kees@6067
   182
    templ = &inTouch->dollarTemplate[index];
kees@6067
   183
    SDL_memcpy(templ->path, path, DOLLARNPOINTS*sizeof(SDL_FloatPoint));
kees@6067
   184
    templ->hash = SDL_HashDollar(templ->path);
kees@6067
   185
    inTouch->numDollarTemplates++;
kees@6067
   186
kees@6067
   187
    return index;
kees@6067
   188
}
kees@6067
   189
kees@6067
   190
static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
kees@6067
   191
{
icculus@6071
   192
    int index = -1;
kees@6065
   193
    int i = 0;
kees@6066
   194
    if (inTouch == NULL) {
kees@6066
   195
        if (SDL_numGestureTouches == 0) return -1;
kees@6066
   196
        for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   197
            inTouch = &SDL_gestureTouch[i];
kees@6067
   198
            index = SDL_AddDollarGesture_one(inTouch, path);
kees@6067
   199
            if (index < 0)
kees@6065
   200
                return -1;
kees@6065
   201
        }
kees@6067
   202
        // Use the index of the last one added.
kees@6067
   203
        return index;
kees@6065
   204
    } else {
kees@6067
   205
        return SDL_AddDollarGesture_one(inTouch, path);
slouken@4663
   206
    }
kees@6065
   207
    return -1;
jim@4659
   208
}
jim@4659
   209
kees@6065
   210
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
kees@6065
   211
{
kees@6065
   212
    int i,loaded = 0;
kees@6065
   213
    SDL_GestureTouch *touch = NULL;
kees@6066
   214
    if (src == NULL) return 0;
kees@6066
   215
    if (touchId >= 0) {
kees@6066
   216
        for (i = 0; i < SDL_numGestureTouches; i++)
kees@6066
   217
            if (SDL_gestureTouch[i].id == touchId)
kees@6065
   218
                touch = &SDL_gestureTouch[i];
kees@6066
   219
        if (touch == NULL) return -1;
kees@6065
   220
    }
jim@4659
   221
kees@6066
   222
    while (1) {
kees@6065
   223
        SDL_DollarTemplate templ;
jim@4688
   224
kees@6066
   225
        if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) <
kees@6065
   226
           DOLLARNPOINTS) break;
jim@4659
   227
kees@6066
   228
        if (touchId >= 0) {
kees@6065
   229
            //printf("Adding loaded gesture to 1 touch\n");
kees@6068
   230
            if (SDL_AddDollarGesture(touch, templ.path) >= 0)
kees@6068
   231
                loaded++;
kees@6065
   232
        }
kees@6065
   233
        else {
kees@6065
   234
            //printf("Adding to: %i touches\n",SDL_numGestureTouches);
kees@6066
   235
            for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   236
                touch = &SDL_gestureTouch[i];
kees@6065
   237
                //printf("Adding loaded gesture to + touches\n");
kees@6065
   238
                //TODO: What if this fails?
kees@6065
   239
                SDL_AddDollarGesture(touch,templ.path);
kees@6065
   240
            }
kees@6065
   241
            loaded++;
kees@6065
   242
        }
jim@4659
   243
    }
jim@4659
   244
kees@6065
   245
    return loaded;
jim@4659
   246
}
jim@4659
   247
jim@4659
   248
icculus@6070
   249
static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang)
kees@6065
   250
{
kees@6065
   251
    //  SDL_FloatPoint p[DOLLARNPOINTS];
kees@6065
   252
    float dist = 0;
kees@6065
   253
    SDL_FloatPoint p;
kees@6065
   254
    int i;
kees@6066
   255
    for (i = 0; i < DOLLARNPOINTS; i++) {
kees@6065
   256
        p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang));
kees@6065
   257
        p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang));
kees@6065
   258
        dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
kees@6065
   259
                                 (p.y-templ[i].y)*(p.y-templ[i].y)));
kees@6065
   260
    }
kees@6065
   261
    return dist/DOLLARNPOINTS;
kees@6065
   262
jim@4658
   263
}
jim@4658
   264
icculus@6070
   265
static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
kees@6065
   266
{
kees@6065
   267
    //------------BEGIN DOLLAR BLACKBOX----------------//
kees@6065
   268
    //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
kees@6065
   269
    //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
kees@6065
   270
    double ta = -M_PI/4;
kees@6065
   271
    double tb = M_PI/4;
kees@6065
   272
    double dt = M_PI/90;
kees@6065
   273
    float x1 = (float)(PHI*ta + (1-PHI)*tb);
kees@6065
   274
    float f1 = dollarDifference(points,templ,x1);
kees@6065
   275
    float x2 = (float)((1-PHI)*ta + PHI*tb);
kees@6065
   276
    float f2 = dollarDifference(points,templ,x2);
kees@6066
   277
    while (SDL_fabs(ta-tb) > dt) {
kees@6066
   278
        if (f1 < f2) {
kees@6065
   279
            tb = x2;
kees@6065
   280
            x2 = x1;
kees@6065
   281
            f2 = f1;
kees@6065
   282
            x1 = (float)(PHI*ta + (1-PHI)*tb);
kees@6065
   283
            f1 = dollarDifference(points,templ,x1);
kees@6065
   284
        }
kees@6065
   285
        else {
kees@6065
   286
            ta = x1;
kees@6065
   287
            x1 = x2;
kees@6065
   288
            f1 = f2;
kees@6065
   289
            x2 = (float)((1-PHI)*ta + PHI*tb);
kees@6065
   290
            f2 = dollarDifference(points,templ,x2);
kees@6065
   291
        }
jim@4658
   292
    }
kees@6065
   293
    /*
kees@6066
   294
      if (f1 <= f2)
kees@6066
   295
          printf("Min angle (x1): %f\n",x1);
kees@6066
   296
      else if (f1 >  f2)
kees@6066
   297
          printf("Min angle (x2): %f\n",x2);
kees@6065
   298
    */
kees@6065
   299
    return SDL_min(f1,f2);
jim@4658
   300
}
jim@4658
   301
jim@4658
   302
//DollarPath contains raw points, plus (possibly) the calculated length
icculus@6070
   303
static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points)
kees@6065
   304
{
kees@6065
   305
    int i;
kees@6065
   306
    float interval;
kees@6065
   307
    float dist;
kees@6065
   308
    int numPoints = 0;
kees@6065
   309
    SDL_FloatPoint centroid;
kees@6065
   310
    float xmin,xmax,ymin,ymax;
kees@6065
   311
    float ang;
kees@6065
   312
    float w,h;
kees@6065
   313
    float length = path->length;
aschiffler@4865
   314
kees@6065
   315
    //Calculate length if it hasn't already been done
kees@6066
   316
    if (length <= 0) {
kees@6066
   317
        for (i=1;i < path->numPoints; i++) {
kees@6066
   318
            float dx = path->p[i  ].x - path->p[i-1].x;
kees@6066
   319
            float dy = path->p[i  ].y - path->p[i-1].y;
kees@6065
   320
            length += (float)(SDL_sqrt(dx*dx+dy*dy));
kees@6065
   321
        }
jim@4658
   322
    }
jim@4658
   323
kees@6065
   324
    //Resample
kees@6065
   325
    interval = length/(DOLLARNPOINTS - 1);
kees@6065
   326
    dist = interval;
jim@4658
   327
kees@6065
   328
    centroid.x = 0;centroid.y = 0;
jim@4658
   329
kees@6065
   330
    //printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y);
kees@6066
   331
    for (i = 1; i < path->numPoints; i++) {
kees@6065
   332
        float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+
kees@6065
   333
                                   (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y)));
kees@6065
   334
        //printf("d = %f dist = %f/%f\n",d,dist,interval);
kees@6066
   335
        while (dist + d > interval) {
kees@6065
   336
            points[numPoints].x = path->p[i-1].x +
kees@6065
   337
                ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x);
kees@6065
   338
            points[numPoints].y = path->p[i-1].y +
kees@6065
   339
                ((interval-dist)/d)*(path->p[i].y-path->p[i-1].y);
kees@6065
   340
            centroid.x += points[numPoints].x;
kees@6065
   341
            centroid.y += points[numPoints].y;
kees@6065
   342
            numPoints++;
kees@6065
   343
kees@6065
   344
            dist -= interval;
kees@6065
   345
        }
kees@6065
   346
        dist += d;
jim@4658
   347
    }
kees@6066
   348
    if (numPoints < DOLLARNPOINTS-1) {
kees@6065
   349
        SDL_SetError("ERROR: NumPoints = %i\n",numPoints);
kees@6065
   350
        return 0;
kees@6065
   351
    }
kees@6065
   352
    //copy the last point
kees@6065
   353
    points[DOLLARNPOINTS-1] = path->p[path->numPoints-1];
kees@6065
   354
    numPoints = DOLLARNPOINTS;
jim@4685
   355
kees@6065
   356
    centroid.x /= numPoints;
kees@6065
   357
    centroid.y /= numPoints;
jim@4658
   358
kees@6065
   359
    //printf("Centroid (%f,%f)",centroid.x,centroid.y);
kees@6065
   360
    //Rotate Points so point 0 is left of centroid and solve for the bounding box
kees@6065
   361
    xmin = centroid.x;
kees@6065
   362
    xmax = centroid.x;
kees@6065
   363
    ymin = centroid.y;
kees@6065
   364
    ymax = centroid.y;
jim@4658
   365
kees@6065
   366
    ang = (float)(SDL_atan2(centroid.y - points[0].y,
kees@6065
   367
                            centroid.x - points[0].x));
jim@4658
   368
kees@6066
   369
    for (i = 0; i<numPoints; i++) {
kees@6065
   370
        float px = points[i].x;
kees@6065
   371
        float py = points[i].y;
kees@6065
   372
        points[i].x = (float)((px - centroid.x)*SDL_cos(ang) -
kees@6065
   373
                              (py - centroid.y)*SDL_sin(ang) + centroid.x);
kees@6065
   374
        points[i].y = (float)((px - centroid.x)*SDL_sin(ang) +
kees@6065
   375
                              (py - centroid.y)*SDL_cos(ang) + centroid.y);
jim@4658
   376
jim@4658
   377
kees@6066
   378
        if (points[i].x < xmin) xmin = points[i].x;
kees@6066
   379
        if (points[i].x > xmax) xmax = points[i].x;
kees@6066
   380
        if (points[i].y < ymin) ymin = points[i].y;
kees@6066
   381
        if (points[i].y > ymax) ymax = points[i].y;
kees@6065
   382
    }
kees@6065
   383
kees@6065
   384
    //Scale points to DOLLARSIZE, and translate to the origin
kees@6065
   385
    w = xmax-xmin;
kees@6065
   386
    h = ymax-ymin;
kees@6065
   387
kees@6066
   388
    for (i=0; i<numPoints; i++) {
kees@6065
   389
        points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
kees@6065
   390
        points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
kees@6065
   391
    }
kees@6065
   392
    return numPoints;
jim@4658
   393
}
jim@4658
   394
icculus@6070
   395
static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch)
kees@6065
   396
{
slouken@4868
   397
kees@6065
   398
    SDL_FloatPoint points[DOLLARNPOINTS];
kees@6065
   399
    int i;
kees@6065
   400
    float bestDiff = 10000;
icculus@5981
   401
kees@6065
   402
    dollarNormalize(path,points);
kees@6065
   403
kees@6065
   404
    //PrintPath(points);
kees@6065
   405
    *bestTempl = -1;
kees@6066
   406
    for (i = 0; i < touch->numDollarTemplates; i++) {
kees@6065
   407
        float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
kees@6066
   408
        if (diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
kees@6065
   409
    }
kees@6065
   410
    return bestDiff;
slouken@4663
   411
}
slouken@4663
   412
slouken@6951
   413
int SDL_GestureAddTouch(SDL_TouchID touchId)
kees@6065
   414
{
kees@6065
   415
    SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
kees@6065
   416
                                                                     (SDL_numGestureTouches + 1) *
kees@6065
   417
                                                                     sizeof(SDL_GestureTouch));
jim@4657
   418
kees@6066
   419
    if (!gestureTouch) {
icculus@7037
   420
        return SDL_OutOfMemory();
kees@6065
   421
    }
jim@4657
   422
kees@6065
   423
    SDL_gestureTouch = gestureTouch;
jim@4658
   424
kees@6065
   425
    SDL_gestureTouch[SDL_numGestureTouches].numDownFingers = 0;
slouken@6951
   426
    SDL_gestureTouch[SDL_numGestureTouches].id = touchId;
jim@4688
   427
kees@6065
   428
    SDL_gestureTouch[SDL_numGestureTouches].numDollarTemplates = 0;
jim@4688
   429
kees@6065
   430
    SDL_gestureTouch[SDL_numGestureTouches].recording = SDL_FALSE;
jim@4688
   431
kees@6065
   432
    SDL_numGestureTouches++;
kees@6065
   433
    return 0;
jim@4657
   434
}
jim@4657
   435
icculus@6070
   436
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
kees@6065
   437
{
kees@6065
   438
    int i;
kees@6066
   439
    for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   440
        //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
kees@6066
   441
        if (SDL_gestureTouch[i].id == id)
kees@6066
   442
            return &SDL_gestureTouch[i];
kees@6065
   443
    }
kees@6065
   444
    return NULL;
jim@4657
   445
}
jim@4657
   446
kees@6065
   447
int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist)
kees@6065
   448
{
kees@6065
   449
    SDL_Event event;
kees@6065
   450
    event.mgesture.type = SDL_MULTIGESTURE;
kees@6065
   451
    event.mgesture.touchId = touch->id;
kees@6065
   452
    event.mgesture.x = touch->centroid.x;
kees@6065
   453
    event.mgesture.y = touch->centroid.y;
kees@6065
   454
    event.mgesture.dTheta = dTheta;
kees@6065
   455
    event.mgesture.dDist = dDist;
kees@6065
   456
    event.mgesture.numFingers = touch->numDownFingers;
kees@6065
   457
    return SDL_PushEvent(&event) > 0;
jim@4657
   458
}
jim@4657
   459
slouken@6044
   460
static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
kees@6065
   461
                          SDL_GestureID gestureId,float error)
kees@6065
   462
{
kees@6065
   463
    SDL_Event event;
kees@6065
   464
    event.dgesture.type = SDL_DOLLARGESTURE;
kees@6065
   465
    event.dgesture.touchId = touch->id;
kees@6065
   466
    event.mgesture.x = touch->centroid.x;
kees@6065
   467
    event.mgesture.y = touch->centroid.y;
kees@6065
   468
    event.dgesture.gestureId = gestureId;
kees@6065
   469
    event.dgesture.error = error;
kees@6065
   470
    //A finger came up to trigger this event.
kees@6065
   471
    event.dgesture.numFingers = touch->numDownFingers + 1;
kees@6065
   472
    return SDL_PushEvent(&event) > 0;
jim@4658
   473
}
jim@4658
   474
jim@4659
   475
icculus@6070
   476
static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId)
kees@6065
   477
{
kees@6065
   478
    SDL_Event event;
kees@6065
   479
    event.dgesture.type = SDL_DOLLARRECORD;
kees@6065
   480
    event.dgesture.touchId = touch->id;
kees@6065
   481
    event.dgesture.gestureId = gestureId;
kees@6065
   482
    return SDL_PushEvent(&event) > 0;
jim@4659
   483
}
jim@4659
   484
jim@4659
   485
jim@4657
   486
void SDL_GestureProcessEvent(SDL_Event* event)
jim@4657
   487
{
kees@6065
   488
    float x,y;
kees@6065
   489
    SDL_FloatPoint path[DOLLARNPOINTS];
kees@6065
   490
    int index;
kees@6065
   491
    int i;
kees@6065
   492
    float pathDx, pathDy;
kees@6065
   493
    SDL_FloatPoint lastP;
kees@6065
   494
    SDL_FloatPoint lastCentroid;
kees@6065
   495
    float lDist;
kees@6065
   496
    float Dist;
kees@6065
   497
    float dtheta;
kees@6065
   498
    float dDist;
aschiffler@4865
   499
kees@6066
   500
    if (event->type == SDL_FINGERMOTION ||
kees@6066
   501
        event->type == SDL_FINGERDOWN ||
kees@6066
   502
        event->type == SDL_FINGERUP) {
kees@6065
   503
        SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
jim@4683
   504
kees@6065
   505
        //Shouldn't be possible
kees@6066
   506
        if (inTouch == NULL) return;
jim@4658
   507
slouken@6951
   508
        x = event->tfinger.x;
slouken@6951
   509
        y = event->tfinger.y;
kees@6065
   510
kees@6065
   511
        //Finger Up
kees@6066
   512
        if (event->type == SDL_FINGERUP) {
kees@6065
   513
            inTouch->numDownFingers--;
jim@4688
   514
jim@4684
   515
#ifdef ENABLE_DOLLAR
kees@6066
   516
            if (inTouch->recording) {
kees@6065
   517
                inTouch->recording = SDL_FALSE;
kees@6065
   518
                dollarNormalize(&inTouch->dollarPath,path);
kees@6065
   519
                //PrintPath(path);
kees@6066
   520
                if (recordAll) {
kees@6065
   521
                    index = SDL_AddDollarGesture(NULL,path);
kees@6066
   522
                    for (i = 0; i < SDL_numGestureTouches; i++)
kees@6065
   523
                        SDL_gestureTouch[i].recording = SDL_FALSE;
kees@6065
   524
                }
kees@6065
   525
                else {
kees@6065
   526
                    index = SDL_AddDollarGesture(inTouch,path);
kees@6065
   527
                }
kees@6065
   528
kees@6066
   529
                if (index >= 0) {
kees@6065
   530
                    SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
kees@6065
   531
                }
kees@6065
   532
                else {
kees@6065
   533
                    SDL_SendDollarRecord(inTouch,-1);
kees@6065
   534
                }
kees@6065
   535
            }
kees@6065
   536
            else {
kees@6065
   537
                int bestTempl;
kees@6065
   538
                float error;
kees@6065
   539
                error = dollarRecognize(&inTouch->dollarPath,
kees@6065
   540
                                        &bestTempl,inTouch);
kees@6066
   541
                if (bestTempl >= 0){
kees@6065
   542
                    //Send Event
kees@6065
   543
                    unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
kees@6065
   544
                    SDL_SendGestureDollar(inTouch,gestureId,error);
kees@6065
   545
                    //printf ("%s\n",);("Dollar error: %f\n",error);
kees@6065
   546
                }
kees@6065
   547
            }
jim@4683
   548
#endif
kees@6065
   549
            //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
kees@6066
   550
            if (inTouch->numDownFingers > 0) {
kees@6065
   551
                inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
kees@6065
   552
                                       x)/inTouch->numDownFingers;
kees@6065
   553
                inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
kees@6065
   554
                                       y)/inTouch->numDownFingers;
kees@6065
   555
            }
kees@6065
   556
        }
kees@6066
   557
        else if (event->type == SDL_FINGERMOTION) {
slouken@6951
   558
            float dx = event->tfinger.dx;
slouken@6951
   559
            float dy = event->tfinger.dy;
kees@6065
   560
#ifdef ENABLE_DOLLAR
kees@6065
   561
            SDL_DollarPath* path = &inTouch->dollarPath;
kees@6066
   562
            if (path->numPoints < MAXPATHSIZE) {
kees@6065
   563
                path->p[path->numPoints].x = inTouch->centroid.x;
kees@6065
   564
                path->p[path->numPoints].y = inTouch->centroid.y;
kees@6065
   565
                pathDx =
kees@6065
   566
                    (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
kees@6065
   567
                pathDy =
kees@6065
   568
                    (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
kees@6065
   569
                path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy);
kees@6065
   570
                path->numPoints++;
kees@6065
   571
            }
kees@6065
   572
#endif
kees@6065
   573
            lastP.x = x - dx;
kees@6065
   574
            lastP.y = y - dy;
kees@6065
   575
            lastCentroid = inTouch->centroid;
kees@6065
   576
kees@6065
   577
            inTouch->centroid.x += dx/inTouch->numDownFingers;
kees@6065
   578
            inTouch->centroid.y += dy/inTouch->numDownFingers;
kees@6065
   579
            //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
kees@6066
   580
            if (inTouch->numDownFingers > 1) {
kees@6065
   581
                SDL_FloatPoint lv; //Vector from centroid to last x,y position
kees@6065
   582
                SDL_FloatPoint v; //Vector from centroid to current x,y position
kees@6065
   583
                //lv = inTouch->gestureLast[j].cv;
kees@6065
   584
                lv.x = lastP.x - lastCentroid.x;
kees@6065
   585
                lv.y = lastP.y - lastCentroid.y;
kees@6065
   586
                lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y);
kees@6065
   587
                //printf("lDist = %f\n",lDist);
kees@6065
   588
                v.x = x - inTouch->centroid.x;
kees@6065
   589
                v.y = y - inTouch->centroid.y;
kees@6065
   590
                //inTouch->gestureLast[j].cv = v;
kees@6065
   591
                Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y);
kees@6065
   592
                // SDL_cos(dTheta) = (v . lv)/(|v| * |lv|)
kees@6065
   593
kees@6065
   594
                //Normalize Vectors to simplify angle calculation
kees@6065
   595
                lv.x/=lDist;
kees@6065
   596
                lv.y/=lDist;
kees@6065
   597
                v.x/=Dist;
kees@6065
   598
                v.y/=Dist;
kees@6065
   599
                dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
kees@6065
   600
kees@6065
   601
                dDist = (Dist - lDist);
kees@6066
   602
                if (lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
kees@6065
   603
kees@6065
   604
                //inTouch->gestureLast[j].dDist = dDist;
kees@6065
   605
                //inTouch->gestureLast[j].dtheta = dtheta;
kees@6065
   606
kees@6065
   607
                //printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
kees@6065
   608
                //gdtheta = gdtheta*.9 + dtheta*.1;
kees@6065
   609
                //gdDist  =  gdDist*.9 +  dDist*.1
kees@6065
   610
                //knob.r += dDist/numDownFingers;
kees@6065
   611
                //knob.ang += dtheta;
kees@6065
   612
                //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
kees@6065
   613
                //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
kees@6065
   614
                SDL_SendGestureMulti(inTouch,dtheta,dDist);
kees@6065
   615
            }
kees@6065
   616
            else {
kees@6065
   617
                //inTouch->gestureLast[j].dDist = 0;
kees@6065
   618
                //inTouch->gestureLast[j].dtheta = 0;
kees@6065
   619
                //inTouch->gestureLast[j].cv.x = 0;
kees@6065
   620
                //inTouch->gestureLast[j].cv.y = 0;
kees@6065
   621
            }
kees@6065
   622
            //inTouch->gestureLast[j].f.p.x = x;
kees@6065
   623
            //inTouch->gestureLast[j].f.p.y = y;
kees@6065
   624
            //break;
kees@6065
   625
            //pressure?
kees@6065
   626
        }
kees@6065
   627
kees@6066
   628
        if (event->type == SDL_FINGERDOWN) {
kees@6065
   629
kees@6065
   630
            inTouch->numDownFingers++;
kees@6065
   631
            inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+
kees@6065
   632
                                   x)/inTouch->numDownFingers;
kees@6065
   633
            inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
kees@6065
   634
                                   y)/inTouch->numDownFingers;
kees@6065
   635
            //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
kees@6065
   636
            //     inTouch->centroid.x,inTouch->centroid.y);
kees@6065
   637
kees@6065
   638
#ifdef ENABLE_DOLLAR
kees@6065
   639
            inTouch->dollarPath.length = 0;
kees@6065
   640
            inTouch->dollarPath.p[0].x = x;
kees@6065
   641
            inTouch->dollarPath.p[0].y = y;
kees@6065
   642
            inTouch->dollarPath.numPoints = 1;
kees@6065
   643
#endif
kees@6065
   644
        }
jim@4657
   645
    }
jim@4683
   646
}
jim@4683
   647
kees@6065
   648
/* vi: set ts=4 sw=4 expandtab: */