]> git.tdb.fi Git - libs/gui.git/commitdiff
Implement tap gestures
authorMikko Rasa <tdb@tdb.fi>
Mon, 23 Nov 2015 17:57:11 +0000 (19:57 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 23 Nov 2015 18:07:31 +0000 (20:07 +0200)
source/input/gesturedetector.cpp
source/input/gesturedetector.h

index aecfa81bb3b7ab88fb4602956f6ca11368a74634..98e3ff30522bdf6de8fbe7a8b052ddbadb54f92d 100644 (file)
@@ -12,6 +12,7 @@ GestureDetector::GestureDetector(Touchscreen &ts):
        touchscreen(ts),
        current_gesture(GESTURE_NONE),
        active_points(0),
+       pending_tap(GESTURE_NONE),
        invalid_gesture(false)
 {
        name = "Gesture";
@@ -62,6 +63,14 @@ void GestureDetector::touch_down(unsigned btn)
        p.x = 0;
        p.y = 0;
        p.threshold_exceeded = false;
+
+       if(current_gesture==GESTURE_NONE && !invalid_gesture)
+       {
+               if(btn==0)
+                       pending_tap = GESTURE_TAP;
+               else if(btn==1)
+                       pending_tap = GESTURE_TAP_2;
+       }
 }
 
 void GestureDetector::touch_up(unsigned btn)
@@ -83,6 +92,16 @@ void GestureDetector::touch_up(unsigned btn)
                invalid_gesture = false;
                for(unsigned i=0; (i<MAX_POINTS && !invalid_gesture); ++i)
                        invalid_gesture = points[i].down;
+
+               if(!invalid_gesture && pending_tap!=GESTURE_NONE)
+               {
+                       unsigned n_points = min<unsigned>((pending_tap-GESTURE_TAP)+1, MAX_POINTS);
+                       set_gesture_location((1<<n_points)-1);
+                       set_button_state(pending_tap, true, true);
+                       set_button_state(pending_tap, false, true);
+
+                       pending_tap = GESTURE_NONE;
+               }
        }
 }
 
@@ -102,7 +121,10 @@ void GestureDetector::touch_move(unsigned axis, float value, float)
        if(p.down)
        {
                if(p.x*p.x/threshold_x_sq+p.y*p.y/threshold_y_sq>=1)
+               {
                        p.threshold_exceeded = true;
+                       pending_tap = GESTURE_NONE;
+               }
 
                if(current_gesture==GESTURE_NONE && !invalid_gesture)
                        start_gesture();
@@ -148,28 +170,42 @@ void GestureDetector::start_gesture()
                        current_gesture = GESTURE_DRAG_2;
 
                if(current_gesture!=GESTURE_NONE)
-               {
                        active_points = 3;
-                       set_axis_value(0, (p.down_x+p2.down_x)/2, true);
-                       set_axis_value(1, (p.down_y+p2.down_y)/2, true);
-               }
        }
        else
        {
                current_gesture = GESTURE_DRAG;
                active_points = 1;
-               set_axis_value(0, p.down_x, true);
-               set_axis_value(1, p.down_y, true);
        }
 
-       update_progress();
 
        if(current_gesture!=GESTURE_NONE)
+       {
+               set_gesture_location(active_points);
+               update_progress();
                set_button_state(current_gesture, true, true);
+       }
        else
                invalid_gesture = true;
 }
 
+void GestureDetector::set_gesture_location(unsigned mask)
+{
+       float x = 0;
+       float y = 0;
+       unsigned count = 0;
+       for(unsigned i=0; i<MAX_POINTS; ++i)
+               if(mask&(1<<i))
+               {
+                       x += points[i].down_x;
+                       y += points[i].down_y;
+                       ++count;
+               }
+
+       set_axis_value(0, x/count, true);
+       set_axis_value(1, y/count, true);
+}
+
 void GestureDetector::update_progress()
 {
        TouchPoint &p = points[0];
@@ -213,6 +249,7 @@ void GestureDetector::end_gesture()
        set_axis_value(2, 0, false);
        current_gesture = GESTURE_NONE;
        active_points = 0;
+       pending_tap = GESTURE_NONE;
 }
 
 void GestureDetector::window_resized(unsigned w, unsigned h)
index ccccc888d27b67bc57f1652e31f898897aee8814..a6186fdef815dce6fa56956bd8261360b1632f3f 100644 (file)
@@ -11,6 +11,8 @@ class Touchscreen;
 enum Gesture
 {
        GESTURE_NONE,
+       GESTURE_TAP,
+       GESTURE_TAP_2,
        GESTURE_DRAG,
        GESTURE_DRAG_2,
        GESTURE_PINCH,
@@ -47,6 +49,7 @@ private:
        TouchPoint points[MAX_POINTS];
        Gesture current_gesture;
        unsigned active_points;
+       Gesture pending_tap;
        bool invalid_gesture;
        float threshold_x_sq;
        float threshold_y_sq;
@@ -62,6 +65,7 @@ private:
        void touch_up(unsigned);
        void touch_move(unsigned, float, float);
        void start_gesture();
+       void set_gesture_location(unsigned);
        void update_progress();
        void end_gesture();
        void window_resized(unsigned, unsigned);