From: Mikko Rasa Date: Mon, 19 Sep 2016 20:30:14 +0000 (+0300) Subject: Refactor GestureDetector logic X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=598786409491a119ba761eca53c5eb2d94a4d9e8;p=libs%2Fgui.git Refactor GestureDetector logic Active_points only ever had the one or two bottom bits set and always matched current gesture. It's simpler to just use the number of points for the gesture. --- diff --git a/source/input/gesturedetector.cpp b/source/input/gesturedetector.cpp index 145262a..26968c0 100644 --- a/source/input/gesturedetector.cpp +++ b/source/input/gesturedetector.cpp @@ -12,7 +12,6 @@ namespace Input { GestureDetector::GestureDetector(Touchscreen &ts): touchscreen(ts), current_gesture(GESTURE_NONE), - active_points(0), pending_tap(GESTURE_NONE), invalid_gesture(false) { @@ -90,7 +89,7 @@ void GestureDetector::touch_up(unsigned btn) p.y += p.down_y; p.down = false; - if(active_points&(1<((pending_tap-GESTURE_TAP)+1, MAX_POINTS); - set_gesture_location((1<2*abs(p.x*p2.y-p.y*p2.x)) // If both points moved in the same direction, it's a two-finger drag. current_gesture = GESTURE_DRAG_2; - - if(current_gesture!=GESTURE_NONE) - active_points = 3; } else - { current_gesture = GESTURE_DRAG; - active_points = 1; - } - if(current_gesture!=GESTURE_NONE) { - set_gesture_location(active_points); + set_gesture_location(gesture_points(current_gesture)); update_progress(); set_button_state(current_gesture, true, true); } @@ -196,40 +187,41 @@ void GestureDetector::start_gesture() invalid_gesture = true; } -void GestureDetector::set_gesture_location(unsigned mask) +void GestureDetector::set_gesture_location(unsigned n_points) { float x = 0; float y = 0; - unsigned count = 0; - for(unsigned i=0; i=GESTURE_DRAG && current_gesture<=GESTURE_DRAG_2) + set_gesture_delta(gesture_points(current_gesture)); else if(current_gesture==GESTURE_PINCH || current_gesture==GESTURE_ROTATE) { + TouchPoint &p = points[0]; TouchPoint &p2 = points[1]; /* Pinch progress is the ratio between the current distance of the points and their distance when they were pressed. */ @@ -255,7 +247,6 @@ void GestureDetector::end_gesture() set_button_state(current_gesture, false, true); set_axis_value(2, 0, false); current_gesture = GESTURE_NONE; - active_points = 0; pending_tap = GESTURE_NONE; } @@ -275,5 +266,23 @@ GestureDetector::TouchPoint::TouchPoint(): threshold_exceeded(false) { } + +unsigned gesture_points(Gesture gesture) +{ + switch(gesture) + { + case GESTURE_NONE: return 0; + case GESTURE_TAP: return 1; + case GESTURE_TAP_2: return 2; + case GESTURE_TAP_3: return 3; + case GESTURE_DRAG: return 1; + case GESTURE_DRAG_2: return 2; + case GESTURE_DRAG_3: return 3; + case GESTURE_PINCH: return 2; + case GESTURE_ROTATE: return 2; + default: throw invalid_argument("gesture_points"); + } +} + } // namespace Input } // namespace Msp diff --git a/source/input/gesturedetector.h b/source/input/gesturedetector.h index a6186fd..f39fdce 100644 --- a/source/input/gesturedetector.h +++ b/source/input/gesturedetector.h @@ -48,7 +48,6 @@ private: Touchscreen &touchscreen; TouchPoint points[MAX_POINTS]; Gesture current_gesture; - unsigned active_points; Gesture pending_tap; bool invalid_gesture; float threshold_x_sq; @@ -66,11 +65,15 @@ private: void touch_move(unsigned, float, float); void start_gesture(); void set_gesture_location(unsigned); + void set_gesture_delta(unsigned); void update_progress(); void end_gesture(); void window_resized(unsigned, unsigned); }; + +unsigned gesture_points(Gesture); + } // namespace Input } // namespace Msp