src/events/SDL_gesture.c
author Jim Grandpre <jim.tla@gmail.com>
Wed, 07 Jul 2010 04:13:08 -0700
changeset 4657 eed063a0bf5b
child 4658 454385d76845
permissions -rw-r--r--
Moved Multi finger gesture recognition into the library.
jim@4657
     1
/*
jim@4657
     2
    SDL - Simple DirectMedia Layer
jim@4657
     3
    Copyright (C) 1997-2010 Sam Lantinga
jim@4657
     4
jim@4657
     5
    This library is free software; you can redistribute it and/or
jim@4657
     6
    modify it under the terms of the GNU Lesser General Public
jim@4657
     7
    License as published by the Free Software Foundation; either
jim@4657
     8
    version 2.1 of the License, or (at your option) any later version.
jim@4657
     9
jim@4657
    10
    This library is distributed in the hope that it will be useful,
jim@4657
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
jim@4657
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
jim@4657
    13
    Lesser General Public License for more details.
jim@4657
    14
jim@4657
    15
    You should have received a copy of the GNU Lesser General Public
jim@4657
    16
    License along with this library; if not, write to the Free Software
jim@4657
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
jim@4657
    18
jim@4657
    19
    Sam Lantinga
jim@4657
    20
    slouken@libsdl.org
jim@4657
    21
*/
jim@4657
    22
#include "SDL_config.h"
jim@4657
    23
jim@4657
    24
/* General mouse handling code for SDL */
jim@4657
    25
jim@4657
    26
#include "SDL_events.h"
jim@4657
    27
#include "SDL_events_c.h"
jim@4657
    28
#include "SDL_gesture_c.h"
jim@4657
    29
jim@4657
    30
//TODO: Replace with malloc
jim@4657
    31
#define MAXFINGERS 3
jim@4657
    32
#define MAXTOUCHES 2
jim@4657
    33
jim@4657
    34
typedef struct {
jim@4657
    35
  float x,y;
jim@4657
    36
} Point;
jim@4657
    37
jim@4657
    38
jim@4657
    39
typedef struct {
jim@4657
    40
  Point p;
jim@4657
    41
  float pressure;
jim@4657
    42
  int id;
jim@4657
    43
} Finger;
jim@4657
    44
jim@4657
    45
typedef struct {
jim@4657
    46
  Finger f;
jim@4657
    47
  Point cv;
jim@4657
    48
  float dtheta,dDist;
jim@4657
    49
} TouchPoint;
jim@4657
    50
jim@4657
    51
jim@4657
    52
typedef struct {
jim@4657
    53
  int id;
jim@4657
    54
  Point res;
jim@4657
    55
  Point centroid;
jim@4657
    56
  TouchPoint gestureLast[MAXFINGERS];
jim@4657
    57
  int numDownFingers;
jim@4657
    58
} GestureTouch;
jim@4657
    59
jim@4657
    60
GestureTouch gestureTouch[MAXTOUCHES];
jim@4657
    61
int numGestureTouches = 0;
jim@4657
    62
int SDL_GestureAddTouch(SDL_Touch* touch) { 
jim@4657
    63
  if(numGestureTouches >= MAXTOUCHES) return -1;
jim@4657
    64
  
jim@4657
    65
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
    66
  gestureTouch[numGestureTouches].res.y = touch->yres;
jim@4657
    67
  gestureTouch[numGestureTouches].numDownFingers = 0;
jim@4657
    68
jim@4657
    69
  gestureTouch[numGestureTouches].res.x = touch->xres;
jim@4657
    70
  gestureTouch[numGestureTouches].id = touch->id;
jim@4657
    71
jim@4657
    72
  numGestureTouches++;
jim@4657
    73
  return 0;
jim@4657
    74
}
jim@4657
    75
jim@4657
    76
GestureTouch * SDL_GetGestureTouch(int id) {
jim@4657
    77
  int i;
jim@4657
    78
  for(i = 0;i < numGestureTouches; i++) {
jim@4657
    79
    //printf("%i ?= %i\n",gestureTouch[i].id,id);
jim@4657
    80
    if(gestureTouch[i].id == id) return &gestureTouch[i];
jim@4657
    81
  }
jim@4657
    82
  return NULL;
jim@4657
    83
}
jim@4657
    84
jim@4657
    85
int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
jim@4657
    86
  SDL_Event event;
jim@4657
    87
  event.mgesture.type = SDL_MULTIGESTURE;
jim@4657
    88
  event.mgesture.touchId = touch->id;
jim@4657
    89
  event.mgesture.x = touch->centroid.x;
jim@4657
    90
  event.mgesture.y = touch->centroid.y;
jim@4657
    91
  event.mgesture.dTheta = dTheta;
jim@4657
    92
  event.mgesture.dDist = dDist;  
jim@4657
    93
  return SDL_PushEvent(&event) > 0;
jim@4657
    94
}
jim@4657
    95
jim@4657
    96
void SDL_GestureProcessEvent(SDL_Event* event)
jim@4657
    97
{
jim@4657
    98
  if(event->type == SDL_FINGERMOTION || 
jim@4657
    99
     event->type == SDL_FINGERDOWN ||
jim@4657
   100
     event->type == SDL_FINGERUP) {
jim@4657
   101
    GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
jim@4657
   102
jim@4657
   103
jim@4657
   104
    //Shouldn't be possible
jim@4657
   105
    if(inTouch == NULL) return;
jim@4657
   106
    
jim@4657
   107
    
jim@4657
   108
    float x = ((float)event->tfinger.x)/inTouch->res.x;
jim@4657
   109
    float y = ((float)event->tfinger.y)/inTouch->res.y;
jim@4657
   110
    int j,empty = -1;
jim@4657
   111
    
jim@4657
   112
    for(j = 0;j<inTouch->numDownFingers;j++) {
jim@4657
   113
      if(inTouch->gestureLast[j].f.id != event->tfinger.fingerId) continue;
jim@4657
   114
jim@4657
   115
      if(event->type == SDL_FINGERUP) {
jim@4657
   116
	inTouch->numDownFingers--;
jim@4657
   117
	inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
jim@4657
   118
	break;
jim@4657
   119
      }
jim@4657
   120
      else {
jim@4657
   121
	float dx = x - inTouch->gestureLast[j].f.p.x;
jim@4657
   122
	float dy = y - inTouch->gestureLast[j].f.p.y;
jim@4657
   123
	inTouch->centroid.x += dx/inTouch->numDownFingers;
jim@4657
   124
	inTouch->centroid.y += dy/inTouch->numDownFingers;    
jim@4657
   125
	if(inTouch->numDownFingers > 1) {
jim@4657
   126
	  Point lv; //Vector from centroid to last x,y position
jim@4657
   127
	  Point v; //Vector from centroid to current x,y position
jim@4657
   128
	  lv = inTouch->gestureLast[j].cv;
jim@4657
   129
	  float lDist = sqrt(lv.x*lv.x + lv.y*lv.y);
jim@4657
   130
	  //printf("lDist = %f\n",lDist);
jim@4657
   131
	  v.x = x - inTouch->centroid.x;
jim@4657
   132
	  v.y = y - inTouch->centroid.y;
jim@4657
   133
	  inTouch->gestureLast[j].cv = v;
jim@4657
   134
	  float Dist = sqrt(v.x*v.x+v.y*v.y);
jim@4657
   135
	  // cos(dTheta) = (v . lv)/(|v| * |lv|)
jim@4657
   136
	  
jim@4657
   137
	  //Normalize Vectors to simplify angle calculation
jim@4657
   138
	  lv.x/=lDist;
jim@4657
   139
	  lv.y/=lDist;
jim@4657
   140
	  v.x/=Dist;
jim@4657
   141
	  v.y/=Dist;
jim@4657
   142
	  float dtheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
jim@4657
   143
	  
jim@4657
   144
	  float dDist = (Dist - lDist);
jim@4657
   145
	  if(lDist == 0) {dDist = 0;dtheta = 0;} //To avoid impossible values
jim@4657
   146
	  inTouch->gestureLast[j].dDist = dDist;
jim@4657
   147
	  inTouch->gestureLast[j].dtheta = dtheta;
jim@4657
   148
	  
jim@4657
   149
	  //printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
jim@4657
   150
	  //gdtheta = gdtheta*.9 + dtheta*.1;
jim@4657
   151
	  //gdDist  =  gdDist*.9 +  dDist*.1
jim@4657
   152
	  //knob.r += dDist/numDownFingers;
jim@4657
   153
	  //knob.ang += dtheta;
jim@4657
   154
	  //printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
jim@4657
   155
	  //printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist);
jim@4657
   156
	  SDL_SendGestureMulti(inTouch,dtheta,dDist);
jim@4657
   157
	}
jim@4657
   158
	else {
jim@4657
   159
	  inTouch->gestureLast[j].dDist = 0;
jim@4657
   160
	  inTouch->gestureLast[j].dtheta = 0;
jim@4657
   161
	  inTouch->gestureLast[j].cv.x = 0;
jim@4657
   162
	  inTouch->gestureLast[j].cv.y = 0;
jim@4657
   163
	}
jim@4657
   164
	inTouch->gestureLast[j].f.p.x = x;
jim@4657
   165
	inTouch->gestureLast[j].f.p.y = y;
jim@4657
   166
	break;
jim@4657
   167
	//pressure?
jim@4657
   168
      }      
jim@4657
   169
    }
jim@4657
   170
    
jim@4657
   171
    if(j == inTouch->numDownFingers) {
jim@4657
   172
      //printf("Finger Down!!!\n");
jim@4657
   173
      inTouch->numDownFingers++;
jim@4657
   174
      inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+ 
jim@4657
   175
			     x)/inTouch->numDownFingers;
jim@4657
   176
      inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
jim@4657
   177
			     y)/inTouch->numDownFingers;
jim@4657
   178
      
jim@4657
   179
      inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
jim@4657
   180
      inTouch->gestureLast[j].f.p.x  = x;
jim@4657
   181
      inTouch->gestureLast[j].f.p.y  = y;	
jim@4657
   182
      inTouch->gestureLast[j].cv.x = 0;
jim@4657
   183
      inTouch->gestureLast[j].cv.y = 0;
jim@4657
   184
    }
jim@4657
   185
  }
jim@4657
   186
}  
jim@4657
   187
  
jim@4657
   188
  /* vi: set ts=4 sw=4 expandtab: */
jim@4657
   189