]> git.tdb.fi Git - libs/gui.git/commitdiff
Implement additional two-finger gestures
authorMikko Rasa <tdb@tdb.fi>
Sat, 21 Nov 2015 09:58:39 +0000 (11:58 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 21 Nov 2015 09:58:39 +0000 (11:58 +0200)
source/input/gesturedetector.cpp
source/input/gesturedetector.h

index cc027973fdc868a319bdce0fe5db5b312ed742a5..aecfa81bb3b7ab88fb4602956f6ca11368a74634 100644 (file)
@@ -28,8 +28,12 @@ string GestureDetector::get_button_name(unsigned btn) const
 {
        if(btn==GESTURE_DRAG)
                return "Drag";
+       else if(btn==GESTURE_DRAG_2)
+               return "Two-finger drag";
        else if(btn==GESTURE_PINCH)
                return "Pinch";
+       else if(btn==GESTURE_ROTATE)
+               return "Rotate";
        else
                return Device::get_button_name(btn);
 }
@@ -135,8 +139,13 @@ void GestureDetector::start_gesture()
                        /* If the points moved away from or towards each other without rotating
                        significantly, it's a pinch gesture. */
                        current_gesture = GESTURE_PINCH;
-               else
-                       invalid_gesture = true;
+               else if(turn*turn2>0 && abs(turn)>abs(away) && abs(turn2)>abs(away2))
+                       /* If the points both turned in the same direction without significant
+                       changes in distance, it's a rotate gesture. */
+                       current_gesture = GESTURE_ROTATE;
+               else if((p.x*p2.x+p.y*p2.y)>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)
                {
@@ -157,6 +166,8 @@ void GestureDetector::start_gesture()
 
        if(current_gesture!=GESTURE_NONE)
                set_button_state(current_gesture, true, true);
+       else
+               invalid_gesture = true;
 }
 
 void GestureDetector::update_progress()
@@ -168,7 +179,13 @@ void GestureDetector::update_progress()
                set_axis_value(2, p.x, true);
                set_axis_value(3, p.y, true);
        }
-       else if(current_gesture==GESTURE_PINCH)
+       else if(current_gesture==GESTURE_DRAG_2)
+       {
+               TouchPoint &p2 = points[1];
+               set_axis_value(2, (p.x+p2.x)/2, true);
+               set_axis_value(3, (p.y+p2.y)/2, true);
+       }
+       else if(current_gesture==GESTURE_PINCH || current_gesture==GESTURE_ROTATE)
        {
                TouchPoint &p2 = points[1];
                /* Pinch progress is the ratio between the current distance of the points
@@ -177,8 +194,16 @@ void GestureDetector::update_progress()
                float ddy = p.down_y-p2.down_y;
                float dx = ddx+p.x-p2.x;
                float dy = ddy+p.y-p2.y;
-               set_axis_value(2, sqrt(dx*dx+dy*dy)/sqrt(ddx*ddx+ddy*ddy)-1, true);
-               set_axis_value(3, 0, true);
+               if(current_gesture==GESTURE_PINCH)
+               {
+                       set_axis_value(2, sqrt(dx*dx+dy*dy)/sqrt(ddx*ddx+ddy*ddy)-1, true);
+                       set_axis_value(3, 0, true);
+               }
+               else if(current_gesture==GESTURE_ROTATE)
+               {
+                       set_axis_value(2, atan2(dy*ddx-dx*ddy, dx*ddx+dy*ddy)/M_PI/2, true);
+                       set_axis_value(3, 0, true);
+               }
        }
 }
 
index fdd083de0372feb7cb4cde113881f6175d1e27cb..ccccc888d27b67bc57f1652e31f898897aee8814 100644 (file)
@@ -12,7 +12,9 @@ enum Gesture
 {
        GESTURE_NONE,
        GESTURE_DRAG,
-       GESTURE_PINCH
+       GESTURE_DRAG_2,
+       GESTURE_PINCH,
+       GESTURE_ROTATE
 };
 
 /**