src/events/SDL_gesture.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 21 Oct 2018 22:40:17 -0400
changeset 12345 50e1cca28b39
parent 11811 5d94cb6b24d3
child 12479 f2c510ee0f2a
permissions -rw-r--r--
wasapi/win32: Sort initial device lists by device GUID.

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