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