2 #include <sigc++/bind_return.h>
3 #include "gesturedetector.h"
4 #include "touchscreen.h"
11 GestureDetector::GestureDetector(Touchscreen &ts):
13 current_gesture(GESTURE_NONE),
15 invalid_gesture(false)
19 touchscreen.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_down), false));
20 touchscreen.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_up), false));
21 touchscreen.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_move), false));
22 Graphics::Window &window = touchscreen.get_window();
23 window.signal_resize.connect(sigc::mem_fun(this, &GestureDetector::window_resized));
24 window_resized(window.get_width(), window.get_height());
27 string GestureDetector::get_button_name(unsigned btn) const
31 else if(btn==GESTURE_DRAG_2)
32 return "Two-finger drag";
33 else if(btn==GESTURE_PINCH)
35 else if(btn==GESTURE_ROTATE)
38 return Device::get_button_name(btn);
41 string GestureDetector::get_axis_name(unsigned axis) const
50 return Device::get_axis_name(axis);
53 void GestureDetector::touch_down(unsigned btn)
58 TouchPoint &p = points[btn];
64 p.threshold_exceeded = false;
67 void GestureDetector::touch_up(unsigned btn)
72 TouchPoint &p = points[btn];
77 if(active_points&(1<<btn))
80 if(current_gesture==GESTURE_NONE)
82 // New gesture can't start until all points have been released.
83 invalid_gesture = false;
84 for(unsigned i=0; (i<MAX_POINTS && !invalid_gesture); ++i)
85 invalid_gesture = points[i].down;
89 void GestureDetector::touch_move(unsigned axis, float value, float)
91 if(axis>=MAX_POINTS*2)
95 TouchPoint &p = points[i];
96 // Track relative position when pressed, absolute when not.
98 p.y = (p.down ? value-p.down_y : value);
100 p.x = (p.down ? value-p.down_x : value);
104 if(p.x*p.x/threshold_x_sq+p.y*p.y/threshold_y_sq>=1)
105 p.threshold_exceeded = true;
107 if(current_gesture==GESTURE_NONE && !invalid_gesture)
109 else if(active_points&(1<<i))
114 void GestureDetector::start_gesture()
116 TouchPoint &p = points[0];
120 /* At least one point needs to have moved more than the threshold to start
122 bool threshold_exceeded = false;
123 for(unsigned i=0; (i<MAX_POINTS && !threshold_exceeded); ++i)
124 threshold_exceeded = (points[i].down && points[i].threshold_exceeded);
125 if(!threshold_exceeded)
128 invalid_gesture = false;
131 TouchPoint &p2 = points[1];
132 float ddx = p.down_x-p2.down_x;
133 float ddy = p.down_y-p2.down_y;
134 float away = p.x*ddx+p.y*ddy;
135 float turn = p.y*ddx-p.x*ddy;
136 float away2 = -(p2.x*ddx+p2.y*ddy);
137 float turn2 = -(p2.y*ddx-p2.x*ddy);
138 if(away*away2>0 && abs(away)>abs(turn) && abs(away2)>abs(turn2))
139 /* If the points moved away from or towards each other without rotating
140 significantly, it's a pinch gesture. */
141 current_gesture = GESTURE_PINCH;
142 else if(turn*turn2>0 && abs(turn)>abs(away) && abs(turn2)>abs(away2))
143 /* If the points both turned in the same direction without significant
144 changes in distance, it's a rotate gesture. */
145 current_gesture = GESTURE_ROTATE;
146 else if((p.x*p2.x+p.y*p2.y)>2*abs(p.x*p2.y-p.y*p2.x))
147 // If both points moved in the same direction, it's a two-finger drag.
148 current_gesture = GESTURE_DRAG_2;
150 if(current_gesture!=GESTURE_NONE)
153 set_axis_value(0, (p.down_x+p2.down_x)/2, true);
154 set_axis_value(1, (p.down_y+p2.down_y)/2, true);
159 current_gesture = GESTURE_DRAG;
161 set_axis_value(0, p.down_x, true);
162 set_axis_value(1, p.down_y, true);
167 if(current_gesture!=GESTURE_NONE)
168 set_button_state(current_gesture, true, true);
170 invalid_gesture = true;
173 void GestureDetector::update_progress()
175 TouchPoint &p = points[0];
177 if(current_gesture==GESTURE_DRAG)
179 set_axis_value(2, p.x, true);
180 set_axis_value(3, p.y, true);
182 else if(current_gesture==GESTURE_DRAG_2)
184 TouchPoint &p2 = points[1];
185 set_axis_value(2, (p.x+p2.x)/2, true);
186 set_axis_value(3, (p.y+p2.y)/2, true);
188 else if(current_gesture==GESTURE_PINCH || current_gesture==GESTURE_ROTATE)
190 TouchPoint &p2 = points[1];
191 /* Pinch progress is the ratio between the current distance of the points
192 and their distance when they were pressed. */
193 float ddx = p.down_x-p2.down_x;
194 float ddy = p.down_y-p2.down_y;
195 float dx = ddx+p.x-p2.x;
196 float dy = ddy+p.y-p2.y;
197 if(current_gesture==GESTURE_PINCH)
199 set_axis_value(2, sqrt(dx*dx+dy*dy)/sqrt(ddx*ddx+ddy*ddy)-1, true);
200 set_axis_value(3, 0, true);
202 else if(current_gesture==GESTURE_ROTATE)
204 set_axis_value(2, atan2(dy*ddx-dx*ddy, dx*ddx+dy*ddy)/M_PI/2, true);
205 set_axis_value(3, 0, true);
210 void GestureDetector::end_gesture()
212 set_button_state(current_gesture, false, true);
213 set_axis_value(2, 0, false);
214 current_gesture = GESTURE_NONE;
218 void GestureDetector::window_resized(unsigned w, unsigned h)
220 threshold_x_sq = 2500.0/(w*w);
221 threshold_y_sq = 2500.0/(h*h);
225 GestureDetector::TouchPoint::TouchPoint():