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