Mercurial > sdl-ios-xcode
comparison src/events/SDL_gesture.c @ 4919:716b2cbf4c9e
First pass at Windows multi-touch gesture support
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 30 Nov 2010 17:58:51 -0800 |
parents | d6adaafcfb10 |
children | a4032241deb5 |
comparison
equal
deleted
inserted
replaced
4918:f5f70fed2c4c | 4919:716b2cbf4c9e |
---|---|
95 if(touchId < 0) recordAll = SDL_TRUE; | 95 if(touchId < 0) recordAll = SDL_TRUE; |
96 for(i = 0;i < SDL_numGestureTouches; i++) { | 96 for(i = 0;i < SDL_numGestureTouches; i++) { |
97 if((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) { | 97 if((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) { |
98 SDL_gestureTouch[i].recording = SDL_TRUE; | 98 SDL_gestureTouch[i].recording = SDL_TRUE; |
99 if(touchId >= 0) | 99 if(touchId >= 0) |
100 return 1; | 100 return 1; |
101 } | 101 } |
102 } | 102 } |
103 return (touchId < 0); | 103 return (touchId < 0); |
104 } | 104 } |
105 | 105 |
120 | 120 |
121 //No Longer storing the Hash, rehash on load | 121 //No Longer storing the Hash, rehash on load |
122 //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0; | 122 //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0; |
123 | 123 |
124 if(SDL_RWwrite(src,templ->path, | 124 if(SDL_RWwrite(src,templ->path, |
125 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) | 125 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) |
126 return 0; | 126 return 0; |
127 | 127 |
128 return 1; | 128 return 1; |
129 } | 129 } |
130 | 130 |
132 int SDL_SaveAllDollarTemplates(SDL_RWops *src) { | 132 int SDL_SaveAllDollarTemplates(SDL_RWops *src) { |
133 int i,j,rtrn = 0; | 133 int i,j,rtrn = 0; |
134 for(i = 0; i < SDL_numGestureTouches; i++) { | 134 for(i = 0; i < SDL_numGestureTouches; i++) { |
135 SDL_GestureTouch* touch = &SDL_gestureTouch[i]; | 135 SDL_GestureTouch* touch = &SDL_gestureTouch[i]; |
136 for(j = 0;j < touch->numDollarTemplates; j++) { | 136 for(j = 0;j < touch->numDollarTemplates; j++) { |
137 rtrn += SaveTemplate(&touch->dollarTemplate[i],src); | 137 rtrn += SaveTemplate(&touch->dollarTemplate[i],src); |
138 } | 138 } |
139 } | 139 } |
140 return rtrn; | 140 return rtrn; |
141 } | 141 } |
142 | 142 |
144 int i,j; | 144 int i,j; |
145 for(i = 0; i < SDL_numGestureTouches; i++) { | 145 for(i = 0; i < SDL_numGestureTouches; i++) { |
146 SDL_GestureTouch* touch = &SDL_gestureTouch[i]; | 146 SDL_GestureTouch* touch = &SDL_gestureTouch[i]; |
147 for(j = 0;j < touch->numDollarTemplates; j++) { | 147 for(j = 0;j < touch->numDollarTemplates; j++) { |
148 if(touch->dollarTemplate[i].hash == gestureId) { | 148 if(touch->dollarTemplate[i].hash == gestureId) { |
149 return SaveTemplate(&touch->dollarTemplate[i],src); | 149 return SaveTemplate(&touch->dollarTemplate[i],src); |
150 } | 150 } |
151 } | 151 } |
152 } | 152 } |
153 SDL_SetError("Unknown gestureId"); | 153 SDL_SetError("Unknown gestureId"); |
154 return -1; | 154 return -1; |
164 if(SDL_numGestureTouches == 0) return -1; | 164 if(SDL_numGestureTouches == 0) return -1; |
165 for(i = 0;i < SDL_numGestureTouches; i++) { | 165 for(i = 0;i < SDL_numGestureTouches; i++) { |
166 inTouch = &SDL_gestureTouch[i]; | 166 inTouch = &SDL_gestureTouch[i]; |
167 | 167 |
168 dollarTemplate = | 168 dollarTemplate = |
169 (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, | 169 (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, |
170 (inTouch->numDollarTemplates + 1) * | 170 (inTouch->numDollarTemplates + 1) * |
171 sizeof(SDL_DollarTemplate)); | 171 sizeof(SDL_DollarTemplate)); |
172 if(!dollarTemplate) { | 172 if(!dollarTemplate) { |
173 SDL_OutOfMemory(); | 173 SDL_OutOfMemory(); |
174 return -1; | 174 return -1; |
175 } | 175 } |
176 | 176 |
177 inTouch->dollarTemplate = dollarTemplate; | 177 inTouch->dollarTemplate = dollarTemplate; |
178 | 178 |
179 templ = | 179 templ = |
180 &inTouch->dollarTemplate[inTouch->numDollarTemplates]; | 180 &inTouch->dollarTemplate[inTouch->numDollarTemplates]; |
181 SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); | 181 SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint)); |
182 templ->hash = SDL_HashDollar(templ->path); | 182 templ->hash = SDL_HashDollar(templ->path); |
183 inTouch->numDollarTemplates++; | 183 inTouch->numDollarTemplates++; |
184 } | 184 } |
185 return inTouch->numDollarTemplates - 1; | 185 return inTouch->numDollarTemplates - 1; |
186 } else { | 186 } else { |
187 SDL_DollarTemplate* dollarTemplate = | 187 SDL_DollarTemplate* dollarTemplate = |
188 ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, | 188 ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate, |
189 (inTouch->numDollarTemplates + 1) * | 189 (inTouch->numDollarTemplates + 1) * |
190 sizeof(SDL_DollarTemplate)); | 190 sizeof(SDL_DollarTemplate)); |
191 if(!dollarTemplate) { | 191 if(!dollarTemplate) { |
192 SDL_OutOfMemory(); | 192 SDL_OutOfMemory(); |
193 return -1; | 193 return -1; |
194 } | 194 } |
195 | 195 |
210 SDL_GestureTouch *touch = NULL; | 210 SDL_GestureTouch *touch = NULL; |
211 if(src == NULL) return 0; | 211 if(src == NULL) return 0; |
212 if(touchId >= 0) { | 212 if(touchId >= 0) { |
213 for(i = 0;i < SDL_numGestureTouches; i++) | 213 for(i = 0;i < SDL_numGestureTouches; i++) |
214 if(SDL_gestureTouch[i].id == touchId) | 214 if(SDL_gestureTouch[i].id == touchId) |
215 touch = &SDL_gestureTouch[i]; | 215 touch = &SDL_gestureTouch[i]; |
216 if(touch == NULL) return -1; | 216 if(touch == NULL) return -1; |
217 } | 217 } |
218 | 218 |
219 while(1) { | 219 while(1) { |
220 SDL_DollarTemplate templ; | 220 SDL_DollarTemplate templ; |
227 if(SDL_AddDollarGesture(touch,templ.path)) loaded++; | 227 if(SDL_AddDollarGesture(touch,templ.path)) loaded++; |
228 } | 228 } |
229 else { | 229 else { |
230 //printf("Adding to: %i touches\n",SDL_numGestureTouches); | 230 //printf("Adding to: %i touches\n",SDL_numGestureTouches); |
231 for(i = 0;i < SDL_numGestureTouches; i++) { | 231 for(i = 0;i < SDL_numGestureTouches; i++) { |
232 touch = &SDL_gestureTouch[i]; | 232 touch = &SDL_gestureTouch[i]; |
233 //printf("Adding loaded gesture to + touches\n"); | 233 //printf("Adding loaded gesture to + touches\n"); |
234 //TODO: What if this fails? | 234 //TODO: What if this fails? |
235 SDL_AddDollarGesture(touch,templ.path); | 235 SDL_AddDollarGesture(touch,templ.path); |
236 } | 236 } |
237 loaded++; | 237 loaded++; |
238 } | 238 } |
239 } | 239 } |
240 | 240 |
249 int i; | 249 int i; |
250 for(i = 0; i < DOLLARNPOINTS; i++) { | 250 for(i = 0; i < DOLLARNPOINTS; i++) { |
251 p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang)); | 251 p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang)); |
252 p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang)); | 252 p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang)); |
253 dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+ | 253 dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+ |
254 (p.y-templ[i].y)*(p.y-templ[i].y))); | 254 (p.y-templ[i].y)*(p.y-templ[i].y))); |
255 } | 255 } |
256 return dist/DOLLARNPOINTS; | 256 return dist/DOLLARNPOINTS; |
257 | 257 |
258 } | 258 } |
259 | 259 |
306 | 306 |
307 //Calculate length if it hasn't already been done | 307 //Calculate length if it hasn't already been done |
308 if(path.length <= 0) { | 308 if(path.length <= 0) { |
309 for(i=1;i<path.numPoints;i++) { | 309 for(i=1;i<path.numPoints;i++) { |
310 float dx = path.p[i ].x - | 310 float dx = path.p[i ].x - |
311 path.p[i-1].x; | 311 path.p[i-1].x; |
312 float dy = path.p[i ].y - | 312 float dy = path.p[i ].y - |
313 path.p[i-1].y; | 313 path.p[i-1].y; |
314 path.length += (float)(SDL_sqrt(dx*dx+dy*dy)); | 314 path.length += (float)(SDL_sqrt(dx*dx+dy*dy)); |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 //Resample | 318 //Resample |
322 centroid.x = 0;centroid.y = 0; | 322 centroid.x = 0;centroid.y = 0; |
323 | 323 |
324 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y); | 324 //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y); |
325 for(i = 1;i < path.numPoints;i++) { | 325 for(i = 1;i < path.numPoints;i++) { |
326 float d = (float)(SDL_sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+ | 326 float d = (float)(SDL_sqrt((path.p[i-1].x-path.p[i].x)*(path.p[i-1].x-path.p[i].x)+ |
327 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y))); | 327 (path.p[i-1].y-path.p[i].y)*(path.p[i-1].y-path.p[i].y))); |
328 //printf("d = %f dist = %f/%f\n",d,dist,interval); | 328 //printf("d = %f dist = %f/%f\n",d,dist,interval); |
329 while(dist + d > interval) { | 329 while(dist + d > interval) { |
330 points[numPoints].x = path.p[i-1].x + | 330 points[numPoints].x = path.p[i-1].x + |
331 ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x); | 331 ((interval-dist)/d)*(path.p[i].x-path.p[i-1].x); |
332 points[numPoints].y = path.p[i-1].y + | 332 points[numPoints].y = path.p[i-1].y + |
333 ((interval-dist)/d)*(path.p[i].y-path.p[i-1].y); | 333 ((interval-dist)/d)*(path.p[i].y-path.p[i-1].y); |
334 centroid.x += points[numPoints].x; | 334 centroid.x += points[numPoints].x; |
335 centroid.y += points[numPoints].y; | 335 centroid.y += points[numPoints].y; |
336 numPoints++; | 336 numPoints++; |
337 | 337 |
338 dist -= interval; | 338 dist -= interval; |
356 xmax = centroid.x; | 356 xmax = centroid.x; |
357 ymin = centroid.y; | 357 ymin = centroid.y; |
358 ymax = centroid.y; | 358 ymax = centroid.y; |
359 | 359 |
360 ang = (float)(SDL_atan2(centroid.y - points[0].y, | 360 ang = (float)(SDL_atan2(centroid.y - points[0].y, |
361 centroid.x - points[0].x)); | 361 centroid.x - points[0].x)); |
362 | 362 |
363 for(i = 0;i<numPoints;i++) { | 363 for(i = 0;i<numPoints;i++) { |
364 float px = points[i].x; | 364 float px = points[i].x; |
365 float py = points[i].y; | 365 float py = points[i].y; |
366 points[i].x = (float)((px - centroid.x)*SDL_cos(ang) - | 366 points[i].x = (float)((px - centroid.x)*SDL_cos(ang) - |
367 (py - centroid.y)*SDL_sin(ang) + centroid.x); | 367 (py - centroid.y)*SDL_sin(ang) + centroid.x); |
368 points[i].y = (float)((px - centroid.x)*SDL_sin(ang) + | 368 points[i].y = (float)((px - centroid.x)*SDL_sin(ang) + |
385 } | 385 } |
386 return numPoints; | 386 return numPoints; |
387 } | 387 } |
388 | 388 |
389 float dollarRecognize(SDL_DollarPath path,int *bestTempl,SDL_GestureTouch* touch) { | 389 float dollarRecognize(SDL_DollarPath path,int *bestTempl,SDL_GestureTouch* touch) { |
390 | 390 |
391 SDL_FloatPoint points[DOLLARNPOINTS]; | 391 SDL_FloatPoint points[DOLLARNPOINTS]; |
392 int numPoints = dollarNormalize(path,points); | 392 int numPoints = dollarNormalize(path,points); |
393 int i; | 393 int i; |
394 float bestDiff = 10000; | 394 float bestDiff = 10000; |
395 | 395 |
396 //PrintPath(points); | 396 //PrintPath(points); |
397 *bestTempl = -1; | 397 *bestTempl = -1; |
398 for(i = 0;i < touch->numDollarTemplates;i++) { | 398 for(i = 0;i < touch->numDollarTemplates;i++) { |
399 float diff = bestDollarDifference(points,touch->dollarTemplate[i].path); | 399 float diff = bestDollarDifference(points,touch->dollarTemplate[i].path); |
400 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} | 400 if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;} |
401 } | 401 } |
402 return bestDiff; | 402 return bestDiff; |
403 } | 403 } |
404 | 404 |
405 int SDL_GestureAddTouch(SDL_Touch* touch) { | 405 int SDL_GestureAddTouch(SDL_Touch* touch) { |
406 SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch, | 406 SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch, |
407 (SDL_numGestureTouches + 1) * | 407 (SDL_numGestureTouches + 1) * |
408 sizeof(SDL_GestureTouch)); | 408 sizeof(SDL_GestureTouch)); |
409 | 409 |
410 if(!gestureTouch) { | 410 if(!gestureTouch) { |
411 SDL_OutOfMemory(); | 411 SDL_OutOfMemory(); |
412 return -1; | 412 return -1; |
413 } | 413 } |
462 event.mgesture.numFingers = touch->numDownFingers; | 462 event.mgesture.numFingers = touch->numDownFingers; |
463 return SDL_PushEvent(&event) > 0; | 463 return SDL_PushEvent(&event) > 0; |
464 } | 464 } |
465 | 465 |
466 int SDL_SendGestureDollar(SDL_GestureTouch* touch, | 466 int SDL_SendGestureDollar(SDL_GestureTouch* touch, |
467 SDL_GestureID gestureId,float error) { | 467 SDL_GestureID gestureId,float error) { |
468 SDL_Event event; | 468 SDL_Event event; |
469 event.dgesture.type = SDL_DOLLARGESTURE; | 469 event.dgesture.type = SDL_DOLLARGESTURE; |
470 event.dgesture.touchId = touch->id; | 470 event.dgesture.touchId = touch->id; |
471 /* | 471 /* |
472 //TODO: Add this to give location of gesture? | 472 //TODO: Add this to give location of gesture? |
511 | 511 |
512 //Shouldn't be possible | 512 //Shouldn't be possible |
513 if(inTouch == NULL) return; | 513 if(inTouch == NULL) return; |
514 | 514 |
515 //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x, | 515 //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x, |
516 // (int)event->tfinger.y, | 516 // (int)event->tfinger.y, |
517 // (int)inTouch->res.x,(int)inTouch->res.y); | 517 // (int)inTouch->res.x,(int)inTouch->res.y); |
518 | 518 |
519 | 519 |
520 x = ((float)event->tfinger.x)/(float)inTouch->res.x; | 520 x = ((float)event->tfinger.x)/(float)inTouch->res.x; |
521 y = ((float)event->tfinger.y)/(float)inTouch->res.y; | 521 y = ((float)event->tfinger.y)/(float)inTouch->res.y; |
525 if(event->type == SDL_FINGERUP) { | 525 if(event->type == SDL_FINGERUP) { |
526 inTouch->numDownFingers--; | 526 inTouch->numDownFingers--; |
527 | 527 |
528 #ifdef ENABLE_DOLLAR | 528 #ifdef ENABLE_DOLLAR |
529 if(inTouch->recording) { | 529 if(inTouch->recording) { |
530 inTouch->recording = SDL_FALSE; | 530 inTouch->recording = SDL_FALSE; |
531 dollarNormalize(inTouch->dollarPath,path); | 531 dollarNormalize(inTouch->dollarPath,path); |
532 //PrintPath(path); | 532 //PrintPath(path); |
533 if(recordAll) { | 533 if(recordAll) { |
534 index = SDL_AddDollarGesture(NULL,path); | 534 index = SDL_AddDollarGesture(NULL,path); |
535 for(i = 0;i < SDL_numGestureTouches; i++) | 535 for(i = 0;i < SDL_numGestureTouches; i++) |
536 SDL_gestureTouch[i].recording = SDL_FALSE; | 536 SDL_gestureTouch[i].recording = SDL_FALSE; |
537 } | 537 } |
538 else { | 538 else { |
539 index = SDL_AddDollarGesture(inTouch,path); | 539 index = SDL_AddDollarGesture(inTouch,path); |
540 } | 540 } |
541 | 541 |
542 if(index >= 0) { | 542 if(index >= 0) { |
543 SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash); | 543 SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash); |
544 } | 544 } |
545 else { | 545 else { |
546 SDL_SendDollarRecord(inTouch,-1); | 546 SDL_SendDollarRecord(inTouch,-1); |
547 } | 547 } |
548 } | 548 } |
549 else { | 549 else { |
550 int bestTempl; | 550 int bestTempl; |
551 float error; | 551 float error; |
552 error = dollarRecognize(inTouch->dollarPath, | 552 error = dollarRecognize(inTouch->dollarPath, |
553 &bestTempl,inTouch); | 553 &bestTempl,inTouch); |
554 if(bestTempl >= 0){ | 554 if(bestTempl >= 0){ |
555 //Send Event | 555 //Send Event |
556 unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash; | 556 unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash; |
557 SDL_SendGestureDollar(inTouch,gestureId,error); | 557 SDL_SendGestureDollar(inTouch,gestureId,error); |
558 //printf ("%s\n",);("Dollar error: %f\n",error); | 558 //printf ("%s\n",);("Dollar error: %f\n",error); |
559 } | 559 } |
560 } | 560 } |
561 #endif | 561 #endif |
562 //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; | 562 //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; |
563 if(inTouch->numDownFingers > 0) { | 563 if(inTouch->numDownFingers > 0) { |
564 inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)- | 564 inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)- |
565 x)/inTouch->numDownFingers; | 565 x)/inTouch->numDownFingers; |
566 inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)- | 566 inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)- |
567 y)/inTouch->numDownFingers; | 567 y)/inTouch->numDownFingers; |
568 } | 568 } |
569 } | 569 } |
570 else if(event->type == SDL_FINGERMOTION) { | 570 else if(event->type == SDL_FINGERMOTION) { |
571 float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x; | 571 float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x; |
572 float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y; | 572 float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y; |
573 //printf("dx,dy: (%f,%f)\n",dx,dy); | 573 //printf("dx,dy: (%f,%f)\n",dx,dy); |
574 #ifdef ENABLE_DOLLAR | 574 #ifdef ENABLE_DOLLAR |
575 SDL_DollarPath* path = &inTouch->dollarPath; | 575 SDL_DollarPath* path = &inTouch->dollarPath; |
576 if(path->numPoints < MAXPATHSIZE) { | 576 if(path->numPoints < MAXPATHSIZE) { |
577 path->p[path->numPoints].x = inTouch->centroid.x; | 577 path->p[path->numPoints].x = inTouch->centroid.x; |
578 path->p[path->numPoints].y = inTouch->centroid.y; | 578 path->p[path->numPoints].y = inTouch->centroid.y; |
579 pathDx = | 579 pathDx = |
580 (path->p[path->numPoints].x-path->p[path->numPoints-1].x); | 580 (path->p[path->numPoints].x-path->p[path->numPoints-1].x); |
581 pathDy = | 581 pathDy = |
582 (path->p[path->numPoints].y-path->p[path->numPoints-1].y); | 582 (path->p[path->numPoints].y-path->p[path->numPoints-1].y); |
583 path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy); | 583 path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy); |
584 path->numPoints++; | 584 path->numPoints++; |
585 } | 585 } |
586 #endif | 586 #endif |
587 lastP.x = x - dx; | 587 lastP.x = x - dx; |
588 lastP.y = y - dy; | 588 lastP.y = y - dy; |
589 lastCentroid = inTouch->centroid; | 589 lastCentroid = inTouch->centroid; |
590 | 590 |
591 inTouch->centroid.x += dx/inTouch->numDownFingers; | 591 inTouch->centroid.x += dx/inTouch->numDownFingers; |
592 inTouch->centroid.y += dy/inTouch->numDownFingers; | 592 inTouch->centroid.y += dy/inTouch->numDownFingers; |
593 //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); | 593 //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); |
594 if(inTouch->numDownFingers > 1) { | 594 if(inTouch->numDownFingers > 1) { |
595 SDL_FloatPoint lv; //Vector from centroid to last x,y position | 595 SDL_FloatPoint lv; //Vector from centroid to last x,y position |
596 SDL_FloatPoint v; //Vector from centroid to current x,y position | 596 SDL_FloatPoint v; //Vector from centroid to current x,y position |
597 //lv = inTouch->gestureLast[j].cv; | 597 //lv = inTouch->gestureLast[j].cv; |
598 lv.x = lastP.x - lastCentroid.x; | 598 lv.x = lastP.x - lastCentroid.x; |
599 lv.y = lastP.y - lastCentroid.y; | 599 lv.y = lastP.y - lastCentroid.y; |
600 lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y); | 600 lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y); |
601 //printf("lDist = %f\n",lDist); | 601 //printf("lDist = %f\n",lDist); |
602 v.x = x - inTouch->centroid.x; | 602 v.x = x - inTouch->centroid.x; |
603 v.y = y - inTouch->centroid.y; | 603 v.y = y - inTouch->centroid.y; |
604 //inTouch->gestureLast[j].cv = v; | 604 //inTouch->gestureLast[j].cv = v; |
605 Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y); | 605 Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y); |
606 // SDL_cos(dTheta) = (v . lv)/(|v| * |lv|) | 606 // SDL_cos(dTheta) = (v . lv)/(|v| * |lv|) |
607 | 607 |
608 //Normalize Vectors to simplify angle calculation | 608 //Normalize Vectors to simplify angle calculation |
609 lv.x/=lDist; | 609 lv.x/=lDist; |
610 lv.y/=lDist; | 610 lv.y/=lDist; |
611 v.x/=Dist; | 611 v.x/=Dist; |
612 v.y/=Dist; | 612 v.y/=Dist; |
613 dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y); | 613 dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y); |
614 | 614 |
615 dDist = (Dist - lDist); | 615 dDist = (Dist - lDist); |
616 if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values | 616 if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values |
617 | 617 |
618 //inTouch->gestureLast[j].dDist = dDist; | 618 //inTouch->gestureLast[j].dDist = dDist; |
619 //inTouch->gestureLast[j].dtheta = dtheta; | 619 //inTouch->gestureLast[j].dtheta = dtheta; |
620 | 620 |
621 //printf("dDist = %f, dTheta = %f\n",dDist,dtheta); | 621 //printf("dDist = %f, dTheta = %f\n",dDist,dtheta); |
622 //gdtheta = gdtheta*.9 + dtheta*.1; | 622 //gdtheta = gdtheta*.9 + dtheta*.1; |
623 //gdDist = gdDist*.9 + dDist*.1 | 623 //gdDist = gdDist*.9 + dDist*.1 |
624 //knob.r += dDist/numDownFingers; | 624 //knob.r += dDist/numDownFingers; |
625 //knob.ang += dtheta; | 625 //knob.ang += dtheta; |
626 //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist); | 626 //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist); |
627 //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); | 627 //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); |
628 SDL_SendGestureMulti(inTouch,dtheta,dDist); | 628 SDL_SendGestureMulti(inTouch,dtheta,dDist); |
629 } | 629 } |
630 else { | 630 else { |
631 //inTouch->gestureLast[j].dDist = 0; | 631 //inTouch->gestureLast[j].dDist = 0; |
632 //inTouch->gestureLast[j].dtheta = 0; | 632 //inTouch->gestureLast[j].dtheta = 0; |
633 //inTouch->gestureLast[j].cv.x = 0; | 633 //inTouch->gestureLast[j].cv.x = 0; |
634 //inTouch->gestureLast[j].cv.y = 0; | 634 //inTouch->gestureLast[j].cv.y = 0; |
635 } | 635 } |
636 //inTouch->gestureLast[j].f.p.x = x; | 636 //inTouch->gestureLast[j].f.p.x = x; |
637 //inTouch->gestureLast[j].f.p.y = y; | 637 //inTouch->gestureLast[j].f.p.y = y; |
638 //break; | 638 //break; |
639 //pressure? | 639 //pressure? |
641 | 641 |
642 if(event->type == SDL_FINGERDOWN) { | 642 if(event->type == SDL_FINGERDOWN) { |
643 | 643 |
644 inTouch->numDownFingers++; | 644 inTouch->numDownFingers++; |
645 inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ | 645 inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ |
646 x)/inTouch->numDownFingers; | 646 x)/inTouch->numDownFingers; |
647 inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+ | 647 inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+ |
648 y)/inTouch->numDownFingers; | 648 y)/inTouch->numDownFingers; |
649 //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y, | 649 //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y, |
650 // inTouch->centroid.x,inTouch->centroid.y); | 650 // inTouch->centroid.x,inTouch->centroid.y); |
651 | 651 |
652 #ifdef ENABLE_DOLLAR | 652 #ifdef ENABLE_DOLLAR |
653 inTouch->dollarPath.length = 0; | 653 inTouch->dollarPath.length = 0; |