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