annotate src/events/SDL_gesture.c @ 4658:454385d76845

Moved $1 Gestures into the SDL Library
author Jim Grandpre <jim.tla@gmail.com>
date Fri, 09 Jul 2010 00:50:40 -0700
parents eed063a0bf5b
children 063b9455bd1a
rev   line source
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
1 /*
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
3 Copyright (C) 1997-2010 Sam Lantinga
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
4
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
9
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
13 Lesser General Public License for more details.
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
14
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
18
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
19 Sam Lantinga
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
20 slouken@libsdl.org
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
21 */
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
22 #include "SDL_config.h"
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
23
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
24 /* General mouse handling code for SDL */
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
25
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
26 #include "SDL_events.h"
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
27 #include "SDL_events_c.h"
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
28 #include "SDL_gesture_c.h"
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
29
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
30 //TODO: Replace with malloc
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
31 #define MAXFINGERS 3
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
32 #define MAXTOUCHES 2
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
33 #define MAXTEMPLATES 4
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
34 #define MAXPATHSIZE 1024
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
35
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
36 #define DOLLARNPOINTS 64
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
37 #define DOLLARSIZE 256
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
38
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
39 //PHI = ((sqrt(5)-1)/2)
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
40 #define PHI 0.618033989
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
41
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
42 typedef struct {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
43 float x,y;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
44 } Point;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
45
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
46
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
47 typedef struct {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
48 Point p;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
49 float pressure;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
50 int id;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
51 } Finger;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
52
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
53
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
54 typedef struct {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
55 float length;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
56
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
57 int numPoints;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
58 Point p[MAXPATHSIZE];
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
59 } DollarPath;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
60
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
61
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
62 typedef struct {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
63 Finger f;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
64 Point cv;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
65 float dtheta,dDist;
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
66 DollarPath dollarPath;
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
67 } TouchPoint;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
68
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
69
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
70 typedef struct {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
71 int id;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
72 Point res;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
73 Point centroid;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
74 TouchPoint gestureLast[MAXFINGERS];
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
75 int numDownFingers;
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
76
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
77 int numDollarTemplates;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
78 Point dollarTemplate[MAXTEMPLATES][DOLLARNPOINTS];
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
79 } GestureTouch;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
80
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
81 GestureTouch gestureTouch[MAXTOUCHES];
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
82 int numGestureTouches = 0;
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
83
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
84 float dollarDifference(Point* points,Point* templ,float ang) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
85 // Point p[DOLLARNPOINTS];
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
86 float dist = 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
87 Point p;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
88 int i;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
89 for(i = 0; i < DOLLARNPOINTS; i++) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
90 p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
91 p.y = points[i].x * sin(ang) + points[i].y * cos(ang);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
92 dist += sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
93 (p.y-templ[i].y)*(p.y-templ[i].y));
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
94 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
95 return dist/DOLLARNPOINTS;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
96
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
97 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
98
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
99 float bestDollarDifference(Point* points,Point* templ) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
100 //------------BEGIN DOLLAR BLACKBOX----------------//
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
101 //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
102 //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
103 float ta = -M_PI/4;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
104 float tb = M_PI/4;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
105 float dt = M_PI/90;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
106 float x1 = PHI*ta + (1-PHI)*tb;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
107 float f1 = dollarDifference(points,templ,x1);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
108 float x2 = (1-PHI)*ta + PHI*tb;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
109 float f2 = dollarDifference(points,templ,x2);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
110 while(abs(ta-tb) > dt) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
111 if(f1 < f2) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
112 tb = x2;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
113 x2 = x1;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
114 f2 = f1;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
115 x1 = PHI*ta + (1-PHI)*tb;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
116 f1 = dollarDifference(points,templ,x1);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
117 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
118 else {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
119 ta = x1;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
120 x1 = x2;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
121 f1 = f2;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
122 x2 = (1-PHI)*ta + PHI*tb;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
123 f2 = dollarDifference(points,templ,x2);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
124 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
125 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
126 /*
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
127 if(f1 <= f2)
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
128 printf("Min angle (x1): %f\n",x1);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
129 else if(f1 > f2)
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
130 printf("Min angle (x2): %f\n",x2);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
131 */
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
132 return SDL_min(f1,f2);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
133 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
134
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
135 float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
136
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
137 Point points[DOLLARNPOINTS];
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
138 int numPoints = dollarNormalize(path,points);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
139 int i;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
140
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
141 int bestDiff = 10000;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
142 *bestTempl = -1;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
143 for(i = 0;i < touch->numDollarTemplates;i++) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
144 int diff = bestDollarDifference(points,touch->dollarTemplate[i]);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
145 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
146 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
147 return bestDiff;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
148 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
149
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
150 //DollarPath contains raw points, plus (possibly) the calculated length
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
151 int dollarNormalize(DollarPath path,Point *points) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
152 int i;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
153 //Calculate length if it hasn't already been done
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
154 if(path.length <= 0) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
155 for(i=1;i<path.numPoints;i++) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
156 float dx = path.p[i ].x -
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
157 path.p[i-1].x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
158 float dy = path.p[i ].y -
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
159 path.p[i-1].y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
160 path.length += sqrt(dx*dx+dy*dy);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
161 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
162 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
163
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
164
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
165 //Resample
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
166 float interval = path.length/(DOLLARNPOINTS - 1);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
167 float dist = 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
168
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
169 int numPoints = 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
170 Point centroid; centroid.x = 0;centroid.y = 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
171 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
172 for(i = 1;i < path.numPoints;i++) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
173 float d = sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
174 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y));
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
175 //printf("d = %f dist = %f/%f\n",d,dist,interval);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
176 while(dist + d > interval) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
177 points[numPoints].x = path.p[i-1].x +
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
178 ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
179 points[numPoints].y = path.p[i-1].y +
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
180 ((interval-dist)/d)*(path.p[i].y-path.p[i-1].y);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
181 centroid.x += points[numPoints].x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
182 centroid.y += points[numPoints].y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
183 numPoints++;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
184
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
185 dist -= interval;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
186 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
187 dist += d;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
188 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
189 if(numPoints < 1) return 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
190 centroid.x /= numPoints;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
191 centroid.y /= numPoints;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
192
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
193 //printf("Centroid (%f,%f)",centroid.x,centroid.y);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
194 //Rotate Points so point 0 is left of centroid and solve for the bounding box
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
195 float xmin,xmax,ymin,ymax;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
196 xmin = centroid.x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
197 xmax = centroid.x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
198 ymin = centroid.y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
199 ymax = centroid.y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
200
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
201 float ang = atan2(centroid.y - points[0].y,
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
202 centroid.x - points[0].x);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
203
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
204 for(i = 0;i<numPoints;i++) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
205 float px = points[i].x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
206 float py = points[i].y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
207 points[i].x = (px - centroid.x)*cos(ang) -
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
208 (py - centroid.y)*sin(ang) + centroid.x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
209 points[i].y = (px - centroid.x)*sin(ang) +
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
210 (py - centroid.y)*cos(ang) + centroid.y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
211
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
212
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
213 if(points[i].x < xmin) xmin = points[i].x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
214 if(points[i].x > xmax) xmax = points[i].x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
215 if(points[i].y < ymin) ymin = points[i].y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
216 if(points[i].y > ymax) ymax = points[i].y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
217 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
218
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
219 //Scale points to DOLLARSIZE, and translate to the origin
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
220 float w = xmax-xmin;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
221 float h = ymax-ymin;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
222
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
223 for(i=0;i<numPoints;i++) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
224 points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
225 points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
226 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
227 return numPoints;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
228 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
229
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
230 int SDL_GestureAddTouch(SDL_Touch* touch) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
231 if(numGestureTouches >= MAXTOUCHES) return -1;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
232
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
233 gestureTouch[numGestureTouches].res.x = touch->xres;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
234 gestureTouch[numGestureTouches].res.y = touch->yres;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
235 gestureTouch[numGestureTouches].numDownFingers = 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
236
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
237 gestureTouch[numGestureTouches].res.x = touch->xres;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
238 gestureTouch[numGestureTouches].id = touch->id;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
239
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
240 gestureTouch[numGestureTouches].numDollarTemplates = 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
241
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
242 numGestureTouches++;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
243 return 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
244 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
245
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
246 GestureTouch * SDL_GetGestureTouch(int id) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
247 int i;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
248 for(i = 0;i < numGestureTouches; i++) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
249 //printf("%i ?= %i\n",gestureTouch[i].id,id);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
250 if(gestureTouch[i].id == id) return &gestureTouch[i];
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
251 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
252 return NULL;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
253 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
254
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
255 int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
256 SDL_Event event;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
257 event.mgesture.type = SDL_MULTIGESTURE;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
258 event.mgesture.touchId = touch->id;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
259 event.mgesture.x = touch->centroid.x;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
260 event.mgesture.y = touch->centroid.y;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
261 event.mgesture.dTheta = dTheta;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
262 event.mgesture.dDist = dDist;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
263 return SDL_PushEvent(&event) > 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
264 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
265
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
266 int SDL_SendGestureDollar(GestureTouch* touch,int gestureId,float error) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
267 SDL_Event event;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
268 event.dgesture.type = SDL_DOLLARGESTURE;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
269 event.dgesture.touchId = touch->id;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
270 /*
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
271 //TODO: Add this to give location of gesture?
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
272 event.mgesture.x = touch->centroid.x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
273 event.mgesture.y = touch->centroid.y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
274 */
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
275 event.dgesture.gestureId = gestureId;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
276 event.dgesture.error = error;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
277 return SDL_PushEvent(&event) > 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
278 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
279
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
280 void SDL_GestureProcessEvent(SDL_Event* event)
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
281 {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
282 if(event->type == SDL_FINGERMOTION ||
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
283 event->type == SDL_FINGERDOWN ||
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
284 event->type == SDL_FINGERUP) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
285 GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
286
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
287 //Shouldn't be possible
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
288 if(inTouch == NULL) return;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
289
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
290
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
291 float x = ((float)event->tfinger.x)/inTouch->res.x;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
292 float y = ((float)event->tfinger.y)/inTouch->res.y;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
293 int j,empty = -1;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
294
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
295 for(j = 0;j<inTouch->numDownFingers;j++) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
296 if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
297
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
298 if(event->type == SDL_FINGERUP) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
299 inTouch->numDownFingers--;
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
300
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
301 int bestTempl;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
302 float error;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
303 error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
304 &bestTempl,inTouch);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
305 if(bestTempl >= 0){
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
306 //Send Event
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
307 int gestureId = 0; //?
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
308 SDL_SendGestureDollar(inTouch->id,gestureId,error);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
309
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
310
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
311 printf("Dollar error: %f\n",error);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
312 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
313
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
314 inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
315 break;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
316 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
317 else {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
318 float dx = x - inTouch->gestureLast[j].f.p.x;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
319 float dy = y - inTouch->gestureLast[j].f.p.y;
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
320 DollarPath* path = &inTouch->gestureLast[j].dollarPath;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
321 if(path->numPoints < MAXPATHSIZE) {
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
322 path->p[path->numPoints].x = x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
323 path->p[path->numPoints].y = y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
324 path->length += sqrt(dx*dx + dy*dy);
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
325 path->numPoints++;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
326 }
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
327
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
328
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
329 inTouch->centroid.x += dx/inTouch->numDownFingers;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
330 inTouch->centroid.y += dy/inTouch->numDownFingers;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
331 if(inTouch->numDownFingers > 1) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
332 Point lv; //Vector from centroid to last x,y position
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
333 Point v; //Vector from centroid to current x,y position
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
334 lv = inTouch->gestureLast[j].cv;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
335 float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
336 //printf("lDist = %f\n",lDist);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
337 v.x = x - inTouch->centroid.x;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
338 v.y = y - inTouch->centroid.y;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
339 inTouch->gestureLast[j].cv = v;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
340 float Dist = sqrt(v.x*v.x+v.y*v.y);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
341 // cos(dTheta) = (v . lv)/(|v| * |lv|)
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
342
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
343 //Normalize Vectors to simplify angle calculation
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
344 lv.x/=lDist;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
345 lv.y/=lDist;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
346 v.x/=Dist;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
347 v.y/=Dist;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
348 float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
349
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
350 float dDist = (Dist - lDist);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
351 if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
352 inTouch->gestureLast[j].dDist = dDist;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
353 inTouch->gestureLast[j].dtheta = dtheta;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
354
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
355 //printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
356 //gdtheta = gdtheta*.9 + dtheta*.1;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
357 //gdDist = gdDist*.9 + dDist*.1
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
358 //knob.r += dDist/numDownFingers;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
359 //knob.ang += dtheta;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
360 //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
361 //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
362 SDL_SendGestureMulti(inTouch,dtheta,dDist);
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
363 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
364 else {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
365 inTouch->gestureLast[j].dDist = 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
366 inTouch->gestureLast[j].dtheta = 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
367 inTouch->gestureLast[j].cv.x = 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
368 inTouch->gestureLast[j].cv.y = 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
369 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
370 inTouch->gestureLast[j].f.p.x = x;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
371 inTouch->gestureLast[j].f.p.y = y;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
372 break;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
373 //pressure?
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
374 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
375 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
376
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
377 if(j == inTouch->numDownFingers) {
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
378 //printf("Finger Down!!!\n");
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
379 inTouch->numDownFingers++;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
380 inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
381 x)/inTouch->numDownFingers;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
382 inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
383 y)/inTouch->numDownFingers;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
384
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
385 inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
386 inTouch->gestureLast[j].f.p.x = x;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
387 inTouch->gestureLast[j].f.p.y = y;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
388 inTouch->gestureLast[j].cv.x = 0;
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
389 inTouch->gestureLast[j].cv.y = 0;
4658
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
390
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
391 inTouch->gestureLast[j].dollarPath.length = 0;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
392 inTouch->gestureLast[j].dollarPath.p[0].x = x;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
393 inTouch->gestureLast[j].dollarPath.p[0].y = y;
454385d76845 Moved $1 Gestures into the SDL Library
Jim Grandpre <jim.tla@gmail.com>
parents: 4657
diff changeset
394 inTouch->gestureLast[j].dollarPath.numPoints = 1;
4657
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
395 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
396 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
397 }
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
398
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
399 /* vi: set ts=4 sw=4 expandtab: */
eed063a0bf5b Moved Multi finger gesture recognition into the library.
Jim Grandpre <jim.tla@gmail.com>
parents:
diff changeset
400