SDL  2.0
SDL_gesture_c.h File Reference
#include "../SDL_internal.h"
+ Include dependency graph for SDL_gesture_c.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int SDL_GestureAddTouch (SDL_TouchID touchId)
 
void SDL_GestureProcessEvent (SDL_Event *event)
 
int SDL_RecordGesture (SDL_TouchID touchId)
 Begin Recording a gesture on the specified touch, or all touches (-1) More...
 

Function Documentation

int SDL_GestureAddTouch ( SDL_TouchID  touchId)

Definition at line 442 of file SDL_gesture.c.

References SDL_GestureTouch::id, SDL_numGestureTouches, SDL_OutOfMemory, SDL_realloc, and SDL_zero.

Referenced by SDL_AddTouch().

443 {
445  (SDL_numGestureTouches + 1) *
446  sizeof(SDL_GestureTouch));
447 
448  if (!gestureTouch) {
449  return SDL_OutOfMemory();
450  }
451 
452  SDL_gestureTouch = gestureTouch;
453 
457  return 0;
458 }
int SDL_numGestureTouches
Definition: SDL_gesture.c:75
#define SDL_realloc
SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
#define SDL_zero(x)
Definition: SDL_stdinc.h:355
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_TouchID id
Definition: SDL_gesture.c:63
void SDL_GestureProcessEvent ( SDL_Event event)

Definition at line 510 of file SDL_gesture.c.

References SDL_GestureTouch::centroid, dollarNormalize(), DOLLARNPOINTS, SDL_GestureTouch::dollarPath, dollarRecognize(), SDL_GestureTouch::dollarTemplate, SDL_DollarTemplate::hash, i, SDL_DollarPath::length, MAXPATHSIZE, NULL, SDL_GestureTouch::numDownFingers, SDL_DollarPath::numPoints, SDL_DollarPath::p, recordAll, SDL_GestureTouch::recording, SDL_AddDollarGesture(), SDL_atan2, SDL_FALSE, SDL_FINGERDOWN, SDL_FINGERMOTION, SDL_FINGERUP, SDL_GetGestureTouch(), SDL_numGestureTouches, SDL_SendDollarRecord(), SDL_SendGestureDollar(), SDL_SendGestureMulti(), SDL_sqrt, SDL_Event::tfinger, SDL_TouchFingerEvent::touchId, SDL_Event::type, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by SDL_PushEvent().

511 {
512  float x,y;
513  int index;
514  int i;
515  float pathDx, pathDy;
516  SDL_FloatPoint lastP;
517  SDL_FloatPoint lastCentroid;
518  float lDist;
519  float Dist;
520  float dtheta;
521  float dDist;
522 
523  if (event->type == SDL_FINGERMOTION ||
524  event->type == SDL_FINGERDOWN ||
525  event->type == SDL_FINGERUP) {
527 
528  /* Shouldn't be possible */
529  if (inTouch == NULL) return;
530 
531  x = event->tfinger.x;
532  y = event->tfinger.y;
533 
534  /* Finger Up */
535  if (event->type == SDL_FINGERUP) {
537 
538  inTouch->numDownFingers--;
539 
540 #ifdef ENABLE_DOLLAR
541  if (inTouch->recording) {
542  inTouch->recording = SDL_FALSE;
543  dollarNormalize(&inTouch->dollarPath,path);
544  /* PrintPath(path); */
545  if (recordAll) {
546  index = SDL_AddDollarGesture(NULL,path);
547  for (i = 0; i < SDL_numGestureTouches; i++)
548  SDL_gestureTouch[i].recording = SDL_FALSE;
549  }
550  else {
551  index = SDL_AddDollarGesture(inTouch,path);
552  }
553 
554  if (index >= 0) {
555  SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
556  }
557  else {
558  SDL_SendDollarRecord(inTouch,-1);
559  }
560  }
561  else {
562  int bestTempl;
563  float error;
564  error = dollarRecognize(&inTouch->dollarPath,
565  &bestTempl,inTouch);
566  if (bestTempl >= 0){
567  /* Send Event */
568  unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
569  SDL_SendGestureDollar(inTouch,gestureId,error);
570  /* printf ("%s\n",);("Dollar error: %f\n",error); */
571  }
572  }
573 #endif
574  /* inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; */
575  if (inTouch->numDownFingers > 0) {
576  inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
577  x)/inTouch->numDownFingers;
578  inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
579  y)/inTouch->numDownFingers;
580  }
581  }
582  else if (event->type == SDL_FINGERMOTION) {
583  float dx = event->tfinger.dx;
584  float dy = event->tfinger.dy;
585 #ifdef ENABLE_DOLLAR
586  SDL_DollarPath* path = &inTouch->dollarPath;
587  if (path->numPoints < MAXPATHSIZE) {
588  path->p[path->numPoints].x = inTouch->centroid.x;
589  path->p[path->numPoints].y = inTouch->centroid.y;
590  pathDx =
591  (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
592  pathDy =
593  (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
594  path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy);
595  path->numPoints++;
596  }
597 #endif
598  lastP.x = x - dx;
599  lastP.y = y - dy;
600  lastCentroid = inTouch->centroid;
601 
602  inTouch->centroid.x += dx/inTouch->numDownFingers;
603  inTouch->centroid.y += dy/inTouch->numDownFingers;
604  /* printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); */
605  if (inTouch->numDownFingers > 1) {
606  SDL_FloatPoint lv; /* Vector from centroid to last x,y position */
607  SDL_FloatPoint v; /* Vector from centroid to current x,y position */
608  /* lv = inTouch->gestureLast[j].cv; */
609  lv.x = lastP.x - lastCentroid.x;
610  lv.y = lastP.y - lastCentroid.y;
611  lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y);
612  /* printf("lDist = %f\n",lDist); */
613  v.x = x - inTouch->centroid.x;
614  v.y = y - inTouch->centroid.y;
615  /* inTouch->gestureLast[j].cv = v; */
616  Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y);
617  /* SDL_cos(dTheta) = (v . lv)/(|v| * |lv|) */
618 
619  /* Normalize Vectors to simplify angle calculation */
620  lv.x/=lDist;
621  lv.y/=lDist;
622  v.x/=Dist;
623  v.y/=Dist;
624  dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
625 
626  dDist = (Dist - lDist);
627  if (lDist == 0) {dDist = 0;dtheta = 0;} /* To avoid impossible values */
628 
629  /* inTouch->gestureLast[j].dDist = dDist;
630  inTouch->gestureLast[j].dtheta = dtheta;
631 
632  printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
633  gdtheta = gdtheta*.9 + dtheta*.1;
634  gdDist = gdDist*.9 + dDist*.1
635  knob.r += dDist/numDownFingers;
636  knob.ang += dtheta;
637  printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
638  printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); */
639  SDL_SendGestureMulti(inTouch,dtheta,dDist);
640  }
641  else {
642  /* inTouch->gestureLast[j].dDist = 0;
643  inTouch->gestureLast[j].dtheta = 0;
644  inTouch->gestureLast[j].cv.x = 0;
645  inTouch->gestureLast[j].cv.y = 0; */
646  }
647  /* inTouch->gestureLast[j].f.p.x = x;
648  inTouch->gestureLast[j].f.p.y = y;
649  break;
650  pressure? */
651  }
652  else if (event->type == SDL_FINGERDOWN) {
653 
654  inTouch->numDownFingers++;
655  inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+
656  x)/inTouch->numDownFingers;
657  inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
658  y)/inTouch->numDownFingers;
659  /* printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
660  inTouch->centroid.x,inTouch->centroid.y); */
661 
662 #ifdef ENABLE_DOLLAR
663  inTouch->dollarPath.length = 0;
664  inTouch->dollarPath.p[0].x = x;
665  inTouch->dollarPath.p[0].y = y;
666  inTouch->dollarPath.numPoints = 1;
667 #endif
668  }
669  }
670 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
unsigned long hash
Definition: SDL_gesture.c:59
SDL_FloatPoint p[MAXPATHSIZE]
Definition: SDL_gesture.c:54
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
Definition: SDL_gesture.c:460
int SDL_SendGestureMulti(SDL_GestureTouch *touch, float dTheta, float dDist)
Definition: SDL_gesture.c:471
#define SDL_atan2
int SDL_numGestureTouches
Definition: SDL_gesture.c:75
SDL_TouchID touchId
Definition: SDL_events.h:414
static int SDL_SendDollarRecord(SDL_GestureTouch *touch, SDL_GestureID gestureId)
Definition: SDL_gesture.c:500
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
SDL_bool recording
Definition: SDL_gesture.c:71
SDL_FloatPoint centroid
Definition: SDL_gesture.c:64
const GLdouble * v
Definition: SDL_opengl.h:2057
Uint16 numDownFingers
Definition: SDL_gesture.c:66
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_bool recordAll
Definition: SDL_gesture.c:76
static float dollarRecognize(const SDL_DollarPath *path, int *bestTempl, SDL_GestureTouch *touch)
Definition: SDL_gesture.c:423
SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
static int dollarNormalize(const SDL_DollarPath *path, SDL_FloatPoint *points)
Definition: SDL_gesture.c:331
GLuint index
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
static int SDL_SendGestureDollar(SDL_GestureTouch *touch, SDL_GestureID gestureId, float error)
Definition: SDL_gesture.c:484
#define NULL
Definition: begin_code.h:143
SDL_DollarTemplate * dollarTemplate
Definition: SDL_gesture.c:69
static int SDL_AddDollarGesture(SDL_GestureTouch *inTouch, SDL_FloatPoint *path)
Definition: SDL_gesture.c:203
#define SDL_sqrt
#define MAXPATHSIZE
Definition: SDL_gesture.c:37
GLsizei const GLchar *const * path
SDL_DollarPath dollarPath
Definition: SDL_gesture.c:65
Uint32 type
Definition: SDL_events.h:523
SDL_TouchFingerEvent tfinger
Definition: SDL_events.h:544
int SDL_RecordGesture ( SDL_TouchID  touchId)

Begin Recording a gesture on the specified touch, or all touches (-1)

Definition at line 90 of file SDL_gesture.c.

References i, recordAll, SDL_GestureTouch::recording, SDL_numGestureTouches, and SDL_TRUE.

91 {
92  int i;
93  if (touchId < 0) recordAll = SDL_TRUE;
94  for (i = 0; i < SDL_numGestureTouches; i++) {
95  if ((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
97  if (touchId >= 0)
98  return 1;
99  }
100  }
101  return (touchId < 0);
102 }
int SDL_numGestureTouches
Definition: SDL_gesture.c:75
SDL_bool recording
Definition: SDL_gesture.c:71
SDL_bool recordAll
Definition: SDL_gesture.c:76
SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42