touchTest/gestureTest.c
changeset 4653 744b75ad18b8
parent 4651 86c171888eee
child 4654 7dbcd71216df
equal deleted inserted replaced
4652:78f6e0b80d1e 4653:744b75ad18b8
    37   Point p;
    37   Point p;
    38   float pressure;
    38   float pressure;
    39   int id;
    39   int id;
    40 } Finger;
    40 } Finger;
    41 
    41 
       
    42 typedef struct { //dt + s
       
    43   Point d,s; //direction, start
       
    44   int points;
       
    45 } Line;
       
    46 
    42 
    47 
    43 Finger finger[MAXFINGERS];
    48 Finger finger[MAXFINGERS];
    44 
    49 
    45 Finger gestureLast[MAXFINGERS];
    50 Finger gestureLast[MAXFINGERS];
       
    51 Line gestureLine[MAXFINGERS];
    46 
    52 
    47 void handler (int sig)
    53 void handler (int sig)
    48 {
    54 {
    49   printf ("\exiting...(%d)\n", sig);
    55   printf ("\exiting...(%d)\n", sig);
    50   exit (0);
    56   exit (0);
    84   
    90   
    85 
    91 
    86   *pixmem32 = colour;
    92   *pixmem32 = colour;
    87 }
    93 }
    88 
    94 
       
    95 void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) {
       
    96   float t;
       
    97   for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1)))
       
    98     setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
       
    99 }
    89 void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
   100 void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
    90 {
   101 {
    91 
   102 
    92   float a;
   103   float a;
    93   int tx;
   104   int tx;
   143 	  setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   154 	  setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   144 	  //setpix(screen,x,y,0); //Inefficient, but that's okay...
   155 	  //setpix(screen,x,y,0); //Inefficient, but that's okay...
   145         }
   156         }
   146     }
   157     }
   147   drawCircle(screen,mousx,mousy,-30,0xFFFFFF);
   158   drawCircle(screen,mousx,mousy,-30,0xFFFFFF);
   148   
   159   drawLine(screen,0,0,screen->w,screen->h,0xFFFFFF);
   149   int i;
   160   int i;
   150 //draw Touch History
   161 //draw Touch History
   151   for(i = 0;i < MAXFINGERS;i++) gestureLast[i].id = -1;
   162   for(i = 0;i < MAXFINGERS;i++) {
       
   163     gestureLast[i].id = -1;
       
   164     gestureLine[i].points = 0;
       
   165   }
   152   for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i != eventWrite;i++) {
   166   for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i != eventWrite;i++) {
   153     SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   167     SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   154     int age = eventWrite - i - 1;
   168     int age = eventWrite - i - 1;
   155     if(event.type == SDL_FINGERMOTION || 
   169     if(event.type == SDL_FINGERMOTION || 
   156        event.type == SDL_FINGERDOWN ||
   170        event.type == SDL_FINGERDOWN ||
   162       float y = ((float)event.tfinger.y)/inTouch->yres;
   176       float y = ((float)event.tfinger.y)/inTouch->yres;
   163       int j,empty = -1;
   177       int j,empty = -1;
   164       for(j = 0;j<MAXFINGERS;j++) {
   178       for(j = 0;j<MAXFINGERS;j++) {
   165 	if(gestureLast[j].id == event.tfinger.fingerId) {
   179 	if(gestureLast[j].id == event.tfinger.fingerId) {
   166 	  if(event.type == SDL_FINGERUP) {
   180 	  if(event.type == SDL_FINGERUP) {
       
   181 	    if(gestureLine[j].points > 0)
       
   182 	    drawLine(screen,
       
   183 		     gestureLine[j].s.x*screen->w,
       
   184 		     gestureLine[j].s.y*screen->h,
       
   185 		     (gestureLine[j].s.x +50*gestureLine[j].d.x)*screen->w,
       
   186 		     (gestureLine[j].s.y +50*gestureLine[j].d.y)*screen->h,
       
   187 		     0xFF00);
       
   188 
       
   189 	    gestureLine[j].points = 0;
   167 	    gestureLast[j].id = -1;
   190 	    gestureLast[j].id = -1;
   168 	    break;
   191 	    break;
   169 	  }
   192 	  }
   170 	  else {
   193 	  else {
       
   194 	    if(gestureLine[j].points == 1) {
       
   195 	      gestureLine[j].d.x = x - gestureLine[j].s.x;
       
   196 	      gestureLine[j].d.y = y - gestureLine[j].s.y;
       
   197 	    }
       
   198 
       
   199 	    gestureLine[j].s.x = gestureLine[j].s.x*gestureLine[j].points+x;
       
   200 	    gestureLine[j].s.y = gestureLine[j].s.y*gestureLine[j].points+y;
       
   201 
       
   202 	    gestureLine[j].d.x = gestureLine[j].d.x*gestureLine[j].points+
       
   203 	      x - gestureLast[j].p.x;
       
   204 	    gestureLine[j].d.y = gestureLine[j].d.y*gestureLine[j].points+
       
   205 	      y - gestureLast[j].p.y;;
       
   206 
       
   207 
       
   208 	    gestureLine[j].points++;
       
   209 	    
       
   210 	    gestureLine[j].s.x /= gestureLine[j].points;
       
   211 	    gestureLine[j].s.y /= gestureLine[j].points;
       
   212 
       
   213 	    gestureLine[j].d.x /= gestureLine[j].points;
       
   214 	    gestureLine[j].d.y /= gestureLine[j].points;
       
   215 
       
   216 	    
       
   217 	    
       
   218 
   171 	    gestureLast[j].p.x = x;
   219 	    gestureLast[j].p.x = x;
   172 	    gestureLast[j].p.y = y;
   220 	    gestureLast[j].p.y = y;
   173 	    break;
   221 	    break;
   174 	    //pressure?
   222 	    //pressure?
   175 	  }	  
   223 	  }	  
   183 	printf("Finger Down!!!\n");
   231 	printf("Finger Down!!!\n");
   184 	j = empty; //important that j is the index of the added finger
   232 	j = empty; //important that j is the index of the added finger
   185 	gestureLast[j].id = event.tfinger.fingerId;
   233 	gestureLast[j].id = event.tfinger.fingerId;
   186 	gestureLast[j].p.x  = x;
   234 	gestureLast[j].p.x  = x;
   187 	gestureLast[j].p.y  = y;
   235 	gestureLast[j].p.y  = y;
       
   236 
       
   237 	gestureLine[j].s.x = x;
       
   238 	gestureLine[j].s.y = y;
       
   239 	gestureLine[j].points = 1;
   188       }
   240       }
   189 
   241 
   190       //draw the touch && each centroid:
   242       //draw the touch && each centroid:
   191 
   243 
   192       if(gestureLast[j].id < 0) continue; //Finger up. Or some error...
   244       if(gestureLast[j].id < 0) continue; //Finger up. Or some error...