src/events/SDL_gesture.c
changeset 4657 eed063a0bf5b
child 4658 454385d76845
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/events/SDL_gesture.c	Wed Jul 07 04:13:08 2010 -0700
     1.3 @@ -0,0 +1,189 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/* General mouse handling code for SDL */
    1.28 +
    1.29 +#include "SDL_events.h"
    1.30 +#include "SDL_events_c.h"
    1.31 +#include "SDL_gesture_c.h"
    1.32 +
    1.33 +//TODO: Replace with malloc
    1.34 +#define MAXFINGERS 3
    1.35 +#define MAXTOUCHES 2
    1.36 +
    1.37 +typedef struct {
    1.38 +  float x,y;
    1.39 +} Point;
    1.40 +
    1.41 +
    1.42 +typedef struct {
    1.43 +  Point p;
    1.44 +  float pressure;
    1.45 +  int id;
    1.46 +} Finger;
    1.47 +
    1.48 +typedef struct {
    1.49 +  Finger f;
    1.50 +  Point cv;
    1.51 +  float dtheta,dDist;
    1.52 +} TouchPoint;
    1.53 +
    1.54 +
    1.55 +typedef struct {
    1.56 +  int id;
    1.57 +  Point res;
    1.58 +  Point centroid;
    1.59 +  TouchPoint gestureLast[MAXFINGERS];
    1.60 +  int numDownFingers;
    1.61 +} GestureTouch;
    1.62 +
    1.63 +GestureTouch gestureTouch[MAXTOUCHES];
    1.64 +int numGestureTouches = 0;
    1.65 +int SDL_GestureAddTouch(SDL_Touch* touch) { 
    1.66 +  if(numGestureTouches >= MAXTOUCHES) return -1;
    1.67 +  
    1.68 +  gestureTouch[numGestureTouches].res.x = touch->xres;
    1.69 +  gestureTouch[numGestureTouches].res.y = touch->yres;
    1.70 +  gestureTouch[numGestureTouches].numDownFingers = 0;
    1.71 +
    1.72 +  gestureTouch[numGestureTouches].res.x = touch->xres;
    1.73 +  gestureTouch[numGestureTouches].id = touch->id;
    1.74 +
    1.75 +  numGestureTouches++;
    1.76 +  return 0;
    1.77 +}
    1.78 +
    1.79 +GestureTouch * SDL_GetGestureTouch(int id) {
    1.80 +  int i;
    1.81 +  for(i = 0;i < numGestureTouches; i++) {
    1.82 +    //printf("%i ?= %i\n",gestureTouch[i].id,id);
    1.83 +    if(gestureTouch[i].id == id) return &gestureTouch[i];
    1.84 +  }
    1.85 +  return NULL;
    1.86 +}
    1.87 +
    1.88 +int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
    1.89 +  SDL_Event event;
    1.90 +  event.mgesture.type = SDL_MULTIGESTURE;
    1.91 +  event.mgesture.touchId = touch->id;
    1.92 +  event.mgesture.x = touch->centroid.x;
    1.93 +  event.mgesture.y = touch->centroid.y;
    1.94 +  event.mgesture.dTheta = dTheta;
    1.95 +  event.mgesture.dDist = dDist;  
    1.96 +  return SDL_PushEvent(&event) > 0;
    1.97 +}
    1.98 +
    1.99 +void SDL_GestureProcessEvent(SDL_Event* event)
   1.100 +{
   1.101 +  if(event->type == SDL_FINGERMOTION || 
   1.102 +     event->type == SDL_FINGERDOWN ||
   1.103 +     event->type == SDL_FINGERUP) {
   1.104 +    GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
   1.105 +
   1.106 +
   1.107 +    //Shouldn't be possible
   1.108 +    if(inTouch == NULL) return;
   1.109 +    
   1.110 +    
   1.111 +    float x = ((float)event->tfinger.x)/inTouch->res.x;
   1.112 +    float y = ((float)event->tfinger.y)/inTouch->res.y;
   1.113 +    int j,empty = -1;
   1.114 +    
   1.115 +    for(j = 0;j<inTouch->numDownFingers;j++) {
   1.116 +      if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;
   1.117 +
   1.118 +      if(event->type == SDL_FINGERUP) {
   1.119 +	inTouch->numDownFingers--;
   1.120 +	inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
   1.121 +	break;
   1.122 +      }
   1.123 +      else {
   1.124 +	float dx = x - inTouch->gestureLast[j].f.p.x;
   1.125 +	float dy = y - inTouch->gestureLast[j].f.p.y;
   1.126 +	inTouch->centroid.x += dx/inTouch->numDownFingers;
   1.127 +	inTouch->centroid.y += dy/inTouch->numDownFingers;    
   1.128 +	if(inTouch->numDownFingers > 1) {
   1.129 +	  Point lv; //Vector from centroid to last x,y position
   1.130 +	  Point v; //Vector from centroid to current x,y position
   1.131 +	  lv = inTouch->gestureLast[j].cv;
   1.132 +	  float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
   1.133 +	  //printf("lDist = %f\n",lDist);
   1.134 +	  v.x = x - inTouch->centroid.x;
   1.135 +	  v.y = y - inTouch->centroid.y;
   1.136 +	  inTouch->gestureLast[j].cv = v;
   1.137 +	  float Dist = sqrt(v.x*v.x+v.y*v.y);
   1.138 +	  // cos(dTheta) = (v . lv)/(|v| * |lv|)
   1.139 +	  
   1.140 +	  //Normalize Vectors to simplify angle calculation
   1.141 +	  lv.x/=lDist;
   1.142 +	  lv.y/=lDist;
   1.143 +	  v.x/=Dist;
   1.144 +	  v.y/=Dist;
   1.145 +	  float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
   1.146 +	  
   1.147 +	  float dDist = (Dist - lDist);
   1.148 +	  if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
   1.149 +	  inTouch->gestureLast[j].dDist = dDist;
   1.150 +	  inTouch->gestureLast[j].dtheta = dtheta;
   1.151 +	  
   1.152 +	  //printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
   1.153 +	  //gdtheta = gdtheta*.9 + dtheta*.1;
   1.154 +	  //gdDist  =  gdDist*.9 +  dDist*.1
   1.155 +	  //knob.r += dDist/numDownFingers;
   1.156 +	  //knob.ang += dtheta;
   1.157 +	  //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
   1.158 +	  //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
   1.159 +	  SDL_SendGestureMulti(inTouch,dtheta,dDist);
   1.160 +	}
   1.161 +	else {
   1.162 +	  inTouch->gestureLast[j].dDist = 0;
   1.163 +	  inTouch->gestureLast[j].dtheta = 0;
   1.164 +	  inTouch->gestureLast[j].cv.x = 0;
   1.165 +	  inTouch->gestureLast[j].cv.y = 0;
   1.166 +	}
   1.167 +	inTouch->gestureLast[j].f.p.x = x;
   1.168 +	inTouch->gestureLast[j].f.p.y = y;
   1.169 +	break;
   1.170 +	//pressure?
   1.171 +      }      
   1.172 +    }
   1.173 +    
   1.174 +    if(j == inTouch->numDownFingers) {
   1.175 +      //printf("Finger Down!!!\n");
   1.176 +      inTouch->numDownFingers++;
   1.177 +      inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ 
   1.178 +			     x)/inTouch->numDownFingers;
   1.179 +      inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
   1.180 +			     y)/inTouch->numDownFingers;
   1.181 +      
   1.182 +      inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
   1.183 +      inTouch->gestureLast[j].f.p.x  = x;
   1.184 +      inTouch->gestureLast[j].f.p.y  = y;	
   1.185 +      inTouch->gestureLast[j].cv.x = 0;
   1.186 +      inTouch->gestureLast[j].cv.y = 0;
   1.187 +    }
   1.188 +  }
   1.189 +}  
   1.190 +  
   1.191 +  /* vi: set ts=4 sw=4 expandtab: */
   1.192 +