1 #define _USE_MATH_DEFINES
3 #include <sigc++/bind_return.h>
4 #include "gesturedetector.h"
5 #include "touchscreen.h"
12 GestureDetector::GestureDetector(Touchscreen &ts):
14 current_gesture(GESTURE_NONE),
15 pending_tap(GESTURE_NONE),
16 invalid_gesture(false)
20 touchscreen.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_down), false));
21 touchscreen.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_up), false));
22 touchscreen.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_move), false));
23 Graphics::Window &window = touchscreen.get_window();
24 window.signal_resize.connect(sigc::mem_fun(this, &GestureDetector::window_resized));
25 window_resized(window.get_width(), window.get_height());
28 string GestureDetector::get_button_name(unsigned btn) const
32 else if(btn==GESTURE_TAP_2)
33 return "Two-finger tap";
34 else if(btn==GESTURE_DRAG)
36 else if(btn==GESTURE_DRAG_2)
37 return "Two-finger drag";
38 else if(btn==GESTURE_PINCH)
40 else if(btn==GESTURE_ROTATE)
43 return Device::get_button_name(btn);
46 string GestureDetector::get_axis_name(unsigned axis) const
57 return Device::get_axis_name(axis);
60 void GestureDetector::touch_down(unsigned btn)
65 TouchPoint &p = points[btn];
71 p.threshold_exceeded = false;
73 if(current_gesture==GESTURE_NONE && !invalid_gesture)
76 pending_tap = GESTURE_TAP;
78 pending_tap = GESTURE_TAP_2;
82 void GestureDetector::touch_up(unsigned btn)
87 TouchPoint &p = points[btn];
92 if(btn<gesture_points(current_gesture))
95 if(current_gesture==GESTURE_NONE)
97 // New gesture can't start until all points have been released.
98 invalid_gesture = false;
99 for(unsigned i=0; (i<MAX_POINTS && !invalid_gesture); ++i)
100 invalid_gesture = points[i].down;
102 if(!invalid_gesture && pending_tap!=GESTURE_NONE)
104 set_gesture_location(gesture_points(pending_tap));
105 set_button_state(pending_tap, true, true);
106 set_button_state(pending_tap, false, true);
108 pending_tap = GESTURE_NONE;
113 void GestureDetector::touch_move(unsigned axis, float value, float)
115 if(axis>=MAX_POINTS*2)
118 unsigned i = axis>>1;
119 TouchPoint &p = points[i];
120 // Track relative position when pressed, absolute when not.
122 p.y = (p.down ? value-p.down_y : value);
124 p.x = (p.down ? value-p.down_x : value);
128 if(p.x*p.x/threshold_x_sq+p.y*p.y/threshold_y_sq>=1)
130 p.threshold_exceeded = true;
131 pending_tap = GESTURE_NONE;
134 if(current_gesture==GESTURE_NONE && !invalid_gesture)
136 else if(i<gesture_points(current_gesture))
141 void GestureDetector::start_gesture()
143 TouchPoint &p = points[0];
147 /* At least one point needs to have moved more than the threshold to start
149 bool threshold_exceeded = false;
150 for(unsigned i=0; (i<MAX_POINTS && !threshold_exceeded); ++i)
151 threshold_exceeded = (points[i].down && points[i].threshold_exceeded);
152 if(!threshold_exceeded)
155 invalid_gesture = false;
158 TouchPoint &p2 = points[1];
159 float ddx = p.down_x-p2.down_x;
160 float ddy = p.down_y-p2.down_y;
161 float away = p.x*ddx+p.y*ddy;
162 float turn = p.y*ddx-p.x*ddy;
163 float away2 = -(p2.x*ddx+p2.y*ddy);
164 float turn2 = -(p2.y*ddx-p2.x*ddy);
165 if(away*away2>0 && abs(away)>abs(turn) && abs(away2)>abs(turn2))
166 /* If the points moved away from or towards each other without rotating
167 significantly, it's a pinch gesture. */
168 current_gesture = GESTURE_PINCH;
169 else if(turn*turn2>0 && abs(turn)>abs(away) && abs(turn2)>abs(away2))
170 /* If the points both turned in the same direction without significant
171 changes in distance, it's a rotate gesture. */
172 current_gesture = GESTURE_ROTATE;
173 else if((p.x*p2.x+p.y*p2.y)>2*abs(p.x*p2.y-p.y*p2.x))
174 // If both points moved in the same direction, it's a two-finger drag.
175 current_gesture = GESTURE_DRAG_2;
178 current_gesture = GESTURE_DRAG;
180 if(current_gesture!=GESTURE_NONE)
182 set_gesture_location(gesture_points(current_gesture));
184 set_button_state(current_gesture, true, true);
187 invalid_gesture = true;
190 void GestureDetector::set_gesture_location(unsigned n_points)
194 for(unsigned i=0; i<n_points; ++i)
196 x += points[i].down_x;
197 y += points[i].down_y;
200 set_axis_value(0, x/n_points, true);
201 set_axis_value(1, y/n_points, true);
204 void GestureDetector::set_gesture_delta(unsigned n_points)
208 for(unsigned i=0; i<n_points; ++i)
214 set_axis_value(2, x/n_points, true);
215 set_axis_value(3, y/n_points, true);
218 void GestureDetector::update_progress()
220 if(current_gesture>=GESTURE_DRAG && current_gesture<=GESTURE_DRAG_2)
221 set_gesture_delta(gesture_points(current_gesture));
222 else if(current_gesture==GESTURE_PINCH || current_gesture==GESTURE_ROTATE)
224 TouchPoint &p = points[0];
225 TouchPoint &p2 = points[1];
226 /* Pinch progress is the ratio between the current distance of the points
227 and their distance when they were pressed. */
228 float ddx = p.down_x-p2.down_x;
229 float ddy = p.down_y-p2.down_y;
230 float dx = ddx+p.x-p2.x;
231 float dy = ddy+p.y-p2.y;
232 if(current_gesture==GESTURE_PINCH)
234 set_axis_value(2, sqrt(dx*dx+dy*dy)/sqrt(ddx*ddx+ddy*ddy)-1, true);
235 set_axis_value(3, 0, true);
237 else if(current_gesture==GESTURE_ROTATE)
239 set_axis_value(2, atan2(dy*ddx-dx*ddy, dx*ddx+dy*ddy)/M_PI/2, true);
240 set_axis_value(3, 0, true);
245 void GestureDetector::end_gesture()
247 set_button_state(current_gesture, false, true);
248 set_axis_value(2, 0, false);
249 current_gesture = GESTURE_NONE;
250 pending_tap = GESTURE_NONE;
253 void GestureDetector::window_resized(unsigned w, unsigned h)
255 threshold_x_sq = 2500.0/(w*w);
256 threshold_y_sq = 2500.0/(h*h);
260 GestureDetector::TouchPoint::TouchPoint():
266 threshold_exceeded(false)
270 unsigned gesture_points(Gesture gesture)
274 case GESTURE_NONE: return 0;
275 case GESTURE_TAP: return 1;
276 case GESTURE_TAP_2: return 2;
277 case GESTURE_TAP_3: return 3;
278 case GESTURE_DRAG: return 1;
279 case GESTURE_DRAG_2: return 2;
280 case GESTURE_DRAG_3: return 3;
281 case GESTURE_PINCH: return 2;
282 case GESTURE_ROTATE: return 2;
283 default: throw invalid_argument("gesture_points");