src/events/SDL_gesture.c
changeset 6066 1d66a8dce3ef
parent 6065 fcd144b830ce
child 6067 9a8faef6fdbe
     1.1 --- a/src/events/SDL_gesture.c	Thu Oct 27 21:26:44 2011 +0200
     1.2 +++ b/src/events/SDL_gesture.c	Thu Oct 27 22:23:09 2011 +0200
     1.3 @@ -84,7 +84,7 @@
     1.4  {
     1.5      int i;
     1.6      printf("Path:");
     1.7 -    for(i=0;i<DOLLARNPOINTS;i++) {
     1.8 +    for (i=0; i<DOLLARNPOINTS; i++) {
     1.9          printf(" (%f,%f)",path[i].x,path[i].y);
    1.10      }
    1.11      printf("\n");
    1.12 @@ -94,11 +94,11 @@
    1.13  int SDL_RecordGesture(SDL_TouchID touchId)
    1.14  {
    1.15      int i;
    1.16 -    if(touchId < 0) recordAll = SDL_TRUE;
    1.17 -    for(i = 0;i < SDL_numGestureTouches; i++) {
    1.18 -        if((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
    1.19 +    if (touchId < 0) recordAll = SDL_TRUE;
    1.20 +    for (i = 0; i < SDL_numGestureTouches; i++) {
    1.21 +        if ((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
    1.22              SDL_gestureTouch[i].recording = SDL_TRUE;
    1.23 -            if(touchId >= 0)
    1.24 +            if (touchId >= 0)
    1.25                  return 1;
    1.26          }
    1.27      }
    1.28 @@ -109,7 +109,7 @@
    1.29  {
    1.30      unsigned long hash = 5381;
    1.31      int i;
    1.32 -    for(i = 0;i < DOLLARNPOINTS; i++) {
    1.33 +    for (i = 0; i < DOLLARNPOINTS; i++) {
    1.34          hash = ((hash<<5) + hash) + (unsigned long)points[i].x;
    1.35          hash = ((hash<<5) + hash) + (unsigned long)points[i].y;
    1.36      }
    1.37 @@ -119,14 +119,14 @@
    1.38  
    1.39  static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src)
    1.40  {
    1.41 -    if(src == NULL) return 0;
    1.42 +    if (src == NULL) return 0;
    1.43  
    1.44  
    1.45      //No Longer storing the Hash, rehash on load
    1.46      //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
    1.47  
    1.48 -    if(SDL_RWwrite(src,templ->path,
    1.49 -                   sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
    1.50 +    if (SDL_RWwrite(src,templ->path,
    1.51 +                    sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
    1.52          return 0;
    1.53  
    1.54      return 1;
    1.55 @@ -136,9 +136,9 @@
    1.56  int SDL_SaveAllDollarTemplates(SDL_RWops *src)
    1.57  {
    1.58      int i,j,rtrn = 0;
    1.59 -    for(i = 0; i < SDL_numGestureTouches; i++) {
    1.60 +    for (i = 0; i < SDL_numGestureTouches; i++) {
    1.61          SDL_GestureTouch* touch = &SDL_gestureTouch[i];
    1.62 -        for(j = 0;j < touch->numDollarTemplates; j++) {
    1.63 +        for (j = 0; j < touch->numDollarTemplates; j++) {
    1.64              rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
    1.65          }
    1.66      }
    1.67 @@ -148,10 +148,10 @@
    1.68  int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src)
    1.69  {
    1.70      int i,j;
    1.71 -    for(i = 0; i < SDL_numGestureTouches; i++) {
    1.72 +    for (i = 0; i < SDL_numGestureTouches; i++) {
    1.73          SDL_GestureTouch* touch = &SDL_gestureTouch[i];
    1.74 -        for(j = 0;j < touch->numDollarTemplates; j++) {
    1.75 -            if(touch->dollarTemplate[i].hash == gestureId) {
    1.76 +        for (j = 0; j < touch->numDollarTemplates; j++) {
    1.77 +            if (touch->dollarTemplate[i].hash == gestureId) {
    1.78                  return SaveTemplate(&touch->dollarTemplate[i],src);
    1.79              }
    1.80          }
    1.81 @@ -167,16 +167,16 @@
    1.82      SDL_DollarTemplate* dollarTemplate;
    1.83      SDL_DollarTemplate *templ;
    1.84      int i = 0;
    1.85 -    if(inTouch == NULL) {
    1.86 -        if(SDL_numGestureTouches == 0) return -1;
    1.87 -        for(i = 0;i < SDL_numGestureTouches; i++) {
    1.88 +    if (inTouch == NULL) {
    1.89 +        if (SDL_numGestureTouches == 0) return -1;
    1.90 +        for (i = 0; i < SDL_numGestureTouches; i++) {
    1.91              inTouch = &SDL_gestureTouch[i];
    1.92  
    1.93              dollarTemplate =
    1.94                  (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
    1.95                                                    (inTouch->numDollarTemplates + 1) *
    1.96                                                    sizeof(SDL_DollarTemplate));
    1.97 -            if(!dollarTemplate) {
    1.98 +            if (!dollarTemplate) {
    1.99                  SDL_OutOfMemory();
   1.100                  return -1;
   1.101              }
   1.102 @@ -195,7 +195,7 @@
   1.103              ( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
   1.104                                                 (inTouch->numDollarTemplates + 1) *
   1.105                                                 sizeof(SDL_DollarTemplate));
   1.106 -        if(!dollarTemplate) {
   1.107 +        if (!dollarTemplate) {
   1.108              SDL_OutOfMemory();
   1.109              return -1;
   1.110          }
   1.111 @@ -216,27 +216,27 @@
   1.112  {
   1.113      int i,loaded = 0;
   1.114      SDL_GestureTouch *touch = NULL;
   1.115 -    if(src == NULL) return 0;
   1.116 -    if(touchId >= 0) {
   1.117 -        for(i = 0;i < SDL_numGestureTouches; i++)
   1.118 -            if(SDL_gestureTouch[i].id == touchId)
   1.119 +    if (src == NULL) return 0;
   1.120 +    if (touchId >= 0) {
   1.121 +        for (i = 0; i < SDL_numGestureTouches; i++)
   1.122 +            if (SDL_gestureTouch[i].id == touchId)
   1.123                  touch = &SDL_gestureTouch[i];
   1.124 -        if(touch == NULL) return -1;
   1.125 +        if (touch == NULL) return -1;
   1.126      }
   1.127  
   1.128 -    while(1) {
   1.129 +    while (1) {
   1.130          SDL_DollarTemplate templ;
   1.131  
   1.132 -        if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) <
   1.133 +        if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) <
   1.134             DOLLARNPOINTS) break;
   1.135  
   1.136 -        if(touchId >= 0) {
   1.137 +        if (touchId >= 0) {
   1.138              //printf("Adding loaded gesture to 1 touch\n");
   1.139 -            if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
   1.140 +            if (SDL_AddDollarGesture(touch,templ.path)) loaded++;
   1.141          }
   1.142          else {
   1.143              //printf("Adding to: %i touches\n",SDL_numGestureTouches);
   1.144 -            for(i = 0;i < SDL_numGestureTouches; i++) {
   1.145 +            for (i = 0; i < SDL_numGestureTouches; i++) {
   1.146                  touch = &SDL_gestureTouch[i];
   1.147                  //printf("Adding loaded gesture to + touches\n");
   1.148                  //TODO: What if this fails?
   1.149 @@ -256,7 +256,7 @@
   1.150      float dist = 0;
   1.151      SDL_FloatPoint p;
   1.152      int i;
   1.153 -    for(i = 0; i < DOLLARNPOINTS; i++) {
   1.154 +    for (i = 0; i < DOLLARNPOINTS; i++) {
   1.155          p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang));
   1.156          p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang));
   1.157          dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
   1.158 @@ -278,8 +278,8 @@
   1.159      float f1 = dollarDifference(points,templ,x1);
   1.160      float x2 = (float)((1-PHI)*ta + PHI*tb);
   1.161      float f2 = dollarDifference(points,templ,x2);
   1.162 -    while(SDL_fabs(ta-tb) > dt) {
   1.163 -        if(f1 < f2) {
   1.164 +    while (SDL_fabs(ta-tb) > dt) {
   1.165 +        if (f1 < f2) {
   1.166              tb = x2;
   1.167              x2 = x1;
   1.168              f2 = f1;
   1.169 @@ -295,10 +295,10 @@
   1.170          }
   1.171      }
   1.172      /*
   1.173 -      if(f1 <= f2)
   1.174 -      printf("Min angle (x1): %f\n",x1);
   1.175 -      else if(f1 >  f2)
   1.176 -      printf("Min angle (x2): %f\n",x2);
   1.177 +      if (f1 <= f2)
   1.178 +          printf("Min angle (x1): %f\n",x1);
   1.179 +      else if (f1 >  f2)
   1.180 +          printf("Min angle (x2): %f\n",x2);
   1.181      */
   1.182      return SDL_min(f1,f2);
   1.183  }
   1.184 @@ -317,12 +317,10 @@
   1.185      float length = path->length;
   1.186  
   1.187      //Calculate length if it hasn't already been done
   1.188 -    if(length <= 0) {
   1.189 -        for(i=1;i<path->numPoints;i++) {
   1.190 -            float dx = path->p[i  ].x -
   1.191 -                path->p[i-1].x;
   1.192 -            float dy = path->p[i  ].y -
   1.193 -                path->p[i-1].y;
   1.194 +    if (length <= 0) {
   1.195 +        for (i=1;i < path->numPoints; i++) {
   1.196 +            float dx = path->p[i  ].x - path->p[i-1].x;
   1.197 +            float dy = path->p[i  ].y - path->p[i-1].y;
   1.198              length += (float)(SDL_sqrt(dx*dx+dy*dy));
   1.199          }
   1.200      }
   1.201 @@ -334,11 +332,11 @@
   1.202      centroid.x = 0;centroid.y = 0;
   1.203  
   1.204      //printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y);
   1.205 -    for(i = 1;i < path->numPoints;i++) {
   1.206 +    for (i = 1; i < path->numPoints; i++) {
   1.207          float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+
   1.208                                     (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y)));
   1.209          //printf("d = %f dist = %f/%f\n",d,dist,interval);
   1.210 -        while(dist + d > interval) {
   1.211 +        while (dist + d > interval) {
   1.212              points[numPoints].x = path->p[i-1].x +
   1.213                  ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x);
   1.214              points[numPoints].y = path->p[i-1].y +
   1.215 @@ -351,7 +349,7 @@
   1.216          }
   1.217          dist += d;
   1.218      }
   1.219 -    if(numPoints < DOLLARNPOINTS-1) {
   1.220 +    if (numPoints < DOLLARNPOINTS-1) {
   1.221          SDL_SetError("ERROR: NumPoints = %i\n",numPoints);
   1.222          return 0;
   1.223      }
   1.224 @@ -372,7 +370,7 @@
   1.225      ang = (float)(SDL_atan2(centroid.y - points[0].y,
   1.226                              centroid.x - points[0].x));
   1.227  
   1.228 -    for(i = 0;i<numPoints;i++) {
   1.229 +    for (i = 0; i<numPoints; i++) {
   1.230          float px = points[i].x;
   1.231          float py = points[i].y;
   1.232          points[i].x = (float)((px - centroid.x)*SDL_cos(ang) -
   1.233 @@ -381,17 +379,17 @@
   1.234                                (py - centroid.y)*SDL_cos(ang) + centroid.y);
   1.235  
   1.236  
   1.237 -        if(points[i].x < xmin) xmin = points[i].x;
   1.238 -        if(points[i].x > xmax) xmax = points[i].x;
   1.239 -        if(points[i].y < ymin) ymin = points[i].y;
   1.240 -        if(points[i].y > ymax) ymax = points[i].y;
   1.241 +        if (points[i].x < xmin) xmin = points[i].x;
   1.242 +        if (points[i].x > xmax) xmax = points[i].x;
   1.243 +        if (points[i].y < ymin) ymin = points[i].y;
   1.244 +        if (points[i].y > ymax) ymax = points[i].y;
   1.245      }
   1.246  
   1.247      //Scale points to DOLLARSIZE, and translate to the origin
   1.248      w = xmax-xmin;
   1.249      h = ymax-ymin;
   1.250  
   1.251 -    for(i=0;i<numPoints;i++) {
   1.252 +    for (i=0; i<numPoints; i++) {
   1.253          points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
   1.254          points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
   1.255      }
   1.256 @@ -409,9 +407,9 @@
   1.257  
   1.258      //PrintPath(points);
   1.259      *bestTempl = -1;
   1.260 -    for(i = 0;i < touch->numDollarTemplates;i++) {
   1.261 +    for (i = 0; i < touch->numDollarTemplates; i++) {
   1.262          float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
   1.263 -        if(diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.264 +        if (diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
   1.265      }
   1.266      return bestDiff;
   1.267  }
   1.268 @@ -422,7 +420,7 @@
   1.269                                                                       (SDL_numGestureTouches + 1) *
   1.270                                                                       sizeof(SDL_GestureTouch));
   1.271  
   1.272 -    if(!gestureTouch) {
   1.273 +    if (!gestureTouch) {
   1.274          SDL_OutOfMemory();
   1.275          return -1;
   1.276      }
   1.277 @@ -461,9 +459,10 @@
   1.278  SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
   1.279  {
   1.280      int i;
   1.281 -    for(i = 0;i < SDL_numGestureTouches; i++) {
   1.282 +    for (i = 0; i < SDL_numGestureTouches; i++) {
   1.283          //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
   1.284 -        if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i];
   1.285 +        if (SDL_gestureTouch[i].id == id)
   1.286 +            return &SDL_gestureTouch[i];
   1.287      }
   1.288      return NULL;
   1.289  }
   1.290 @@ -524,13 +523,13 @@
   1.291      float dtheta;
   1.292      float dDist;
   1.293  
   1.294 -    if(event->type == SDL_FINGERMOTION ||
   1.295 -       event->type == SDL_FINGERDOWN ||
   1.296 -       event->type == SDL_FINGERUP) {
   1.297 +    if (event->type == SDL_FINGERMOTION ||
   1.298 +        event->type == SDL_FINGERDOWN ||
   1.299 +        event->type == SDL_FINGERUP) {
   1.300          SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
   1.301  
   1.302          //Shouldn't be possible
   1.303 -        if(inTouch == NULL) return;
   1.304 +        if (inTouch == NULL) return;
   1.305  
   1.306          //printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
   1.307          //           (int)event->tfinger.y,
   1.308 @@ -542,24 +541,24 @@
   1.309  
   1.310  
   1.311          //Finger Up
   1.312 -        if(event->type == SDL_FINGERUP) {
   1.313 +        if (event->type == SDL_FINGERUP) {
   1.314              inTouch->numDownFingers--;
   1.315  
   1.316  #ifdef ENABLE_DOLLAR
   1.317 -            if(inTouch->recording) {
   1.318 +            if (inTouch->recording) {
   1.319                  inTouch->recording = SDL_FALSE;
   1.320                  dollarNormalize(&inTouch->dollarPath,path);
   1.321                  //PrintPath(path);
   1.322 -                if(recordAll) {
   1.323 +                if (recordAll) {
   1.324                      index = SDL_AddDollarGesture(NULL,path);
   1.325 -                    for(i = 0;i < SDL_numGestureTouches; i++)
   1.326 +                    for (i = 0; i < SDL_numGestureTouches; i++)
   1.327                          SDL_gestureTouch[i].recording = SDL_FALSE;
   1.328                  }
   1.329                  else {
   1.330                      index = SDL_AddDollarGesture(inTouch,path);
   1.331                  }
   1.332  
   1.333 -                if(index >= 0) {
   1.334 +                if (index >= 0) {
   1.335                      SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
   1.336                  }
   1.337                  else {
   1.338 @@ -571,7 +570,7 @@
   1.339                  float error;
   1.340                  error = dollarRecognize(&inTouch->dollarPath,
   1.341                                          &bestTempl,inTouch);
   1.342 -                if(bestTempl >= 0){
   1.343 +                if (bestTempl >= 0){
   1.344                      //Send Event
   1.345                      unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
   1.346                      SDL_SendGestureDollar(inTouch,gestureId,error);
   1.347 @@ -580,20 +579,20 @@
   1.348              }
   1.349  #endif
   1.350              //inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
   1.351 -            if(inTouch->numDownFingers > 0) {
   1.352 +            if (inTouch->numDownFingers > 0) {
   1.353                  inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
   1.354                                         x)/inTouch->numDownFingers;
   1.355                  inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
   1.356                                         y)/inTouch->numDownFingers;
   1.357              }
   1.358          }
   1.359 -        else if(event->type == SDL_FINGERMOTION) {
   1.360 +        else if (event->type == SDL_FINGERMOTION) {
   1.361              float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x;
   1.362              float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y;
   1.363              //printf("dx,dy: (%f,%f)\n",dx,dy);
   1.364  #ifdef ENABLE_DOLLAR
   1.365              SDL_DollarPath* path = &inTouch->dollarPath;
   1.366 -            if(path->numPoints < MAXPATHSIZE) {
   1.367 +            if (path->numPoints < MAXPATHSIZE) {
   1.368                  path->p[path->numPoints].x = inTouch->centroid.x;
   1.369                  path->p[path->numPoints].y = inTouch->centroid.y;
   1.370                  pathDx =
   1.371 @@ -611,7 +610,7 @@
   1.372              inTouch->centroid.x += dx/inTouch->numDownFingers;
   1.373              inTouch->centroid.y += dy/inTouch->numDownFingers;
   1.374              //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
   1.375 -            if(inTouch->numDownFingers > 1) {
   1.376 +            if (inTouch->numDownFingers > 1) {
   1.377                  SDL_FloatPoint lv; //Vector from centroid to last x,y position
   1.378                  SDL_FloatPoint v; //Vector from centroid to current x,y position
   1.379                  //lv = inTouch->gestureLast[j].cv;
   1.380 @@ -633,7 +632,7 @@
   1.381                  dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
   1.382  
   1.383                  dDist = (Dist - lDist);
   1.384 -                if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
   1.385 +                if (lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
   1.386  
   1.387                  //inTouch->gestureLast[j].dDist = dDist;
   1.388                  //inTouch->gestureLast[j].dtheta = dtheta;
   1.389 @@ -659,7 +658,7 @@
   1.390              //pressure?
   1.391          }
   1.392  
   1.393 -        if(event->type == SDL_FINGERDOWN) {
   1.394 +        if (event->type == SDL_FINGERDOWN) {
   1.395  
   1.396              inTouch->numDownFingers++;
   1.397              inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+