Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
664 lines (560 loc) · 19.6 KB

SDL_gesture.c

File metadata and controls

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