X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Fgesturedetector.cpp;h=49ddba2b8478173dfbd65b9e80233ab356c0da03;hb=1d418a1547bcea8b983815403d0a07b62793a460;hp=c43e65a0d61c5be930e2672ae356f702c8059bf9;hpb=f9ebab54c3debe98e108790be9c21f43f4de116f;p=libs%2Fgui.git diff --git a/source/input/gesturedetector.cpp b/source/input/gesturedetector.cpp index c43e65a..49ddba2 100644 --- a/source/input/gesturedetector.cpp +++ b/source/input/gesturedetector.cpp @@ -1,3 +1,4 @@ +#define _USE_MATH_DEFINES #include #include #include "gesturedetector.h" @@ -12,6 +13,7 @@ GestureDetector::GestureDetector(Touchscreen &ts): touchscreen(ts), current_gesture(GESTURE_NONE), active_points(0), + pending_tap(GESTURE_NONE), invalid_gesture(false) { name = "Gesture"; @@ -26,16 +28,14 @@ GestureDetector::GestureDetector(Touchscreen &ts): string GestureDetector::get_button_name(unsigned btn) const { - if(btn==GESTURE_SWIPE_DOWN) - return "Swipe down"; - else if(btn==GESTURE_SWIPE_UP) - return "Swipe up"; - else if(btn==GESTURE_SWIPE_LEFT) - return "Swipe left"; - else if(btn==GESTURE_SWIPE_RIGHT) - return "Swipe right"; + 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); } @@ -54,59 +54,83 @@ string GestureDetector::get_axis_name(unsigned axis) const void GestureDetector::touch_down(unsigned btn) { - if(btn<3) + if(btn>=MAX_POINTS) + return; + + TouchPoint &p = points[btn]; + p.down = true; + p.down_x = p.x; + p.down_y = p.y; + p.x = 0; + p.y = 0; + p.threshold_exceeded = false; + + if(current_gesture==GESTURE_NONE && !invalid_gesture) { - TouchPoint &p = points[btn]; - p.down = true; - p.down_x = p.x; - p.down_y = p.y; + if(btn==0) + pending_tap = GESTURE_TAP; + else if(btn==1) + pending_tap = GESTURE_TAP_2; } } void GestureDetector::touch_up(unsigned btn) { - if(btn<3) + if(btn>=MAX_POINTS) + return; + + TouchPoint &p = points[btn]; + p.x += p.down_x; + p.y += p.down_y; + p.down = false; + + if(active_points&(1<((pending_tap-GESTURE_TAP)+1, MAX_POINTS); + set_gesture_location((1<=MAX_POINTS*2) + return; + + unsigned i = axis>>1; + TouchPoint &p = points[i]; + // Track relative position when pressed, absolute when not. + if(axis&1) + p.y = (p.down ? value-p.down_y : value); + else + p.x = (p.down ? value-p.down_x : value); + + if(p.down) { - unsigned i = axis>>1; - TouchPoint &p = points[i]; - if(axis&1) - p.y = value; - else - p.x = value; - - if(p.down) + if(p.x*p.x/threshold_x_sq+p.y*p.y/threshold_y_sq>=1) { - if(current_gesture==GESTURE_NONE && !invalid_gesture) - start_gesture(); - else if(active_points&(1<0 && abs(away)>abs(turn) && abs(away2)>abs(turn2)) + /* If the points moved away from or towards each other without rotating + significantly, it's a pinch gesture. */ current_gesture = GESTURE_PINCH; - active_points = 3; - } - 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) - { - set_axis_value(0, (p.down_x+p2.down_x)/2, true); - set_axis_value(1, (p.down_y+p2.down_y)/2, true); - } + active_points = 3; } else { + current_gesture = GESTURE_DRAG; active_points = 1; - if(abs(dx)>2*abs(dy)) - current_gesture = (dx>0 ? GESTURE_SWIPE_RIGHT : GESTURE_SWIPE_LEFT); - else if(abs(dy)>2*abs(dx)) - current_gesture = (dy>0 ? GESTURE_SWIPE_UP : GESTURE_SWIPE_DOWN); - else - invalid_gesture = true; - - if(current_gesture!=GESTURE_NONE) - { - 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