comparison src/events/SDL_gesture.c @ 4659:063b9455bd1a

Added some files I had previosuly missed
author Jim Grandpre <jim.tla@gmail.com>
date Sun, 11 Jul 2010 01:15:39 -0400
parents 454385d76845
children 56a2d70de945 317a151b79ad
comparison
equal deleted inserted replaced
4658:454385d76845 4659:063b9455bd1a
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details. 13 Lesser General Public License for more details.
14 14
15 You should have received a copy of the GNU Lesser General Public 15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software 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 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 17
19 Sam Lantinga 18 Sam Lantinga
20 slouken@libsdl.org 19 slouken@libsdl.org
21 */ 20 */
22 #include "SDL_config.h" 21 #include "SDL_config.h"
64 Point cv; 63 Point cv;
65 float dtheta,dDist; 64 float dtheta,dDist;
66 DollarPath dollarPath; 65 DollarPath dollarPath;
67 } TouchPoint; 66 } TouchPoint;
68 67
68 typedef struct {
69 Point path[DOLLARNPOINTS];
70 unsigned long hash;
71 } DollarTemplate;
69 72
70 typedef struct { 73 typedef struct {
71 int id; 74 int id;
72 Point res; 75 Point res;
73 Point centroid; 76 Point centroid;
74 TouchPoint gestureLast[MAXFINGERS]; 77 TouchPoint gestureLast[MAXFINGERS];
75 int numDownFingers; 78 int numDownFingers;
76 79
77 int numDollarTemplates; 80 int numDollarTemplates;
78 Point dollarTemplate[MAXTEMPLATES][DOLLARNPOINTS]; 81 DollarTemplate dollarTemplate[MAXTEMPLATES];
82
83 SDL_bool recording;
79 } GestureTouch; 84 } GestureTouch;
80 85
81 GestureTouch gestureTouch[MAXTOUCHES]; 86 GestureTouch gestureTouch[MAXTOUCHES];
82 int numGestureTouches = 0; 87 int numGestureTouches = 0;
88 SDL_bool recordAll;
89
90 int SDL_RecordGesture(int touchId) {
91 int i;
92 if(touchId < 0) recordAll = SDL_TRUE;
93 for(i = 0;i < numGestureTouches; i++) {
94 if((touchId < 0) || (gestureTouch[i].id == touchId)) {
95 gestureTouch[i].recording = SDL_TRUE;
96 if(touchId >= 0)
97 return 1;
98 }
99 }
100 return (touchId < 0);
101 }
102
103 unsigned long SDL_HashDollar(Point* points) {
104 unsigned long hash = 5381;
105 int i;
106 for(i = 0;i < DOLLARNPOINTS; i++) {
107 hash = ((hash<<5) + hash) + points[i].x;
108 hash = ((hash<<5) + hash) + points[i].y;
109 }
110 return hash;
111 }
112
113 int SaveTemplate(DollarTemplate *templ, FILE *fp) {
114 int i;
115 fprintf(fp,"%lu ",templ->hash);
116 for(i = 0;i < DOLLARNPOINTS;i++) {
117 fprintf(fp,"%i %i ",(int)templ->path[i].x,(int)templ->path[i].y);
118 }
119 fprintf(fp,"\n");
120 }
121
122
123 int SDL_SaveAllDollarTemplates(FILE *fp) {
124 int i,j,rtrn = 0;
125 for(i = 0; i < numGestureTouches; i++) {
126 GestureTouch* touch = &gestureTouch[i];
127 for(j = 0;j < touch->numDollarTemplates; j++) {
128 rtrn += SaveTemplate(&touch->dollarTemplate[i],fp);
129 }
130 }
131 return rtrn;
132 }
133
134 int SDL_SaveDollarTemplate(unsigned long gestureId, FILE *fp) {
135 int i,j;
136 for(i = 0; i < numGestureTouches; i++) {
137 GestureTouch* touch = &gestureTouch[i];
138 for(j = 0;j < touch->numDollarTemplates; j++) {
139 if(touch->dollarTemplate[i].hash == gestureId) {
140 return SaveTemplate(&touch->dollarTemplate[i],fp);
141 }
142 }
143 }
144 }
145
146 int SDL_LoadDollarTemplates(int touchId, FILE *fp) {
147 int i,loaded = 0;
148 GestureTouch *touch = NULL;
149 if(touchId >= 0) {
150 for(i = 0;i < numGestureTouches; i++)
151 if(gestureTouch[i].id == touchId)
152 touch = &gestureTouch[i];
153 if(touch == NULL) return -1;
154 }
155
156 while(!feof(fp)) {
157 DollarTemplate templ;
158 fscanf(fp,"%lu ",&templ.hash);
159 for(i = 0;i < DOLLARNPOINTS; i++) {
160 int x,y;
161 if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
162 templ.path[i].x = x;
163 templ.path[i].y = y;
164 }
165 fscanf(fp,"\n");
166
167 if(touchId >= 0) {
168 if(SDL_AddDollarGesture(touch,templ)) loaded++;
169 }
170 else {
171 for(i = 0;i < numGestureTouches; i++) {
172 if(gestureTouch[i].id == touchId) {
173 touch = &gestureTouch[i];
174 SDL_AddDollarGesture(touch,templ);
175 }
176 }
177 loaded++;
178 }
179 }
180
181 return 1;
182 }
183
184
185 //path is an already sampled set of points
186 //Returns the index of the gesture on success, or -1
187 int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
188 if(inTouch == NULL) {
189 if(numGestureTouches == 0) return -1;
190 int i = 0;
191 for(i = 0;i < numGestureTouches; i++) {
192 inTouch = &gestureTouch[i];
193 if(inTouch->numDollarTemplates < MAXTEMPLATES) {
194 DollarTemplate *templ =
195 &inTouch->dollarTemplate[inTouch->numDollarTemplates];
196 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
197 templ->hash = SDL_HashDollar(templ->path);
198 inTouch->numDollarTemplates++;
199 }
200 }
201 return inTouch->numDollarTemplates - 1;
202 }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
203 DollarTemplate *templ =
204 &inTouch->dollarTemplate[inTouch->numDollarTemplates];
205 memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
206 templ->hash = SDL_HashDollar(templ->path);
207 inTouch->numDollarTemplates++;
208 return inTouch->numDollarTemplates - 1;
209 }
210 return -1;
211 }
212
213
214
83 215
84 float dollarDifference(Point* points,Point* templ,float ang) { 216 float dollarDifference(Point* points,Point* templ,float ang) {
85 // Point p[DOLLARNPOINTS]; 217 // Point p[DOLLARNPOINTS];
86 float dist = 0; 218 float dist = 0;
87 Point p; 219 Point p;
139 int i; 271 int i;
140 272
141 int bestDiff = 10000; 273 int bestDiff = 10000;
142 *bestTempl = -1; 274 *bestTempl = -1;
143 for(i = 0;i < touch->numDollarTemplates;i++) { 275 for(i = 0;i < touch->numDollarTemplates;i++) {
144 int diff = bestDollarDifference(points,touch->dollarTemplate[i]); 276 int diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
145 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} 277 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
146 } 278 }
147 return bestDiff; 279 return bestDiff;
148 } 280 }
149 281
237 gestureTouch[numGestureTouches].res.x = touch->xres; 369 gestureTouch[numGestureTouches].res.x = touch->xres;
238 gestureTouch[numGestureTouches].id = touch->id; 370 gestureTouch[numGestureTouches].id = touch->id;
239 371
240 gestureTouch[numGestureTouches].numDollarTemplates = 0; 372 gestureTouch[numGestureTouches].numDollarTemplates = 0;
241 373
374 gestureTouch[numGestureTouches].recording = SDL_FALSE;
375
242 numGestureTouches++; 376 numGestureTouches++;
243 return 0; 377 return 0;
244 } 378 }
245 379
246 GestureTouch * SDL_GetGestureTouch(int id) { 380 GestureTouch * SDL_GetGestureTouch(int id) {
275 event.dgesture.gestureId = gestureId; 409 event.dgesture.gestureId = gestureId;
276 event.dgesture.error = error; 410 event.dgesture.error = error;
277 return SDL_PushEvent(&event) > 0; 411 return SDL_PushEvent(&event) > 0;
278 } 412 }
279 413
414
415 int SDL_SendDollarRecord(GestureTouch* touch,int gestureId) {
416 SDL_Event event;
417 event.dgesture.type = SDL_DOLLARRECORD;
418 event.dgesture.touchId = touch->id;
419 event.dgesture.gestureId = gestureId;
420
421 return SDL_PushEvent(&event) > 0;
422 }
423
424
280 void SDL_GestureProcessEvent(SDL_Event* event) 425 void SDL_GestureProcessEvent(SDL_Event* event)
281 { 426 {
282 if(event->type == SDL_FINGERMOTION || 427 if(event->type == SDL_FINGERMOTION ||
283 event->type == SDL_FINGERDOWN || 428 event->type == SDL_FINGERDOWN ||
284 event->type == SDL_FINGERUP) { 429 event->type == SDL_FINGERUP) {
292 float y = ((float)event->tfinger.y)/inTouch->res.y; 437 float y = ((float)event->tfinger.y)/inTouch->res.y;
293 int j,empty = -1; 438 int j,empty = -1;
294 439
295 for(j = 0;j<inTouch->numDownFingers;j++) { 440 for(j = 0;j<inTouch->numDownFingers;j++) {
296 if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue; 441 if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;
297 442 //Finger Up
298 if(event->type == SDL_FINGERUP) { 443 if(event->type == SDL_FINGERUP) {
299 inTouch->numDownFingers--; 444 inTouch->numDownFingers--;
300 445
301 int bestTempl; 446 if(inTouch->recording) {
302 float error; 447 inTouch->recording = SDL_FALSE;
303 error = dollarRecognize(inTouch->gestureLast[j].dollarPath, 448 Point path[DOLLARNPOINTS];
304 &bestTempl,inTouch); 449 dollarNormalize(inTouch->gestureLast[j].dollarPath,path);
305 if(bestTempl >= 0){ 450 int index;
306 //Send Event 451 if(recordAll) {
307 int gestureId = 0; //? 452 index = SDL_AddDollarGesture(NULL,path);
308 SDL_SendGestureDollar(inTouch->id,gestureId,error); 453 int i;
309 454 for(i = 0;i < numGestureTouches; i++)
310 455 gestureTouch[i].recording = SDL_FALSE;
311 printf("Dollar error: %f\n",error); 456 }
457 else {
458 index = SDL_AddDollarGesture(inTouch,path);
459 }
460
461 if(index >= 0) {
462 SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
463 }
464 else {
465 SDL_SendDollarRecord(inTouch,-1);
466 }
312 } 467 }
313 468 else {
469 int bestTempl;
470 float error;
471 error = dollarRecognize(inTouch->gestureLast[j].dollarPath,
472 &bestTempl,inTouch);
473 if(bestTempl >= 0){
474 //Send Event
475 int gestureId = inTouch->dollarTemplate[bestTempl].hash;
476 SDL_SendGestureDollar(inTouch,gestureId,error);
477 printf("Dollar error: %f\n",error);
478 }
479 }
314 inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; 480 inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
315 break; 481 break;
316 } 482 }
317 else { 483 else {
318 float dx = x - inTouch->gestureLast[j].f.p.x; 484 float dx = x - inTouch->gestureLast[j].f.p.x;