X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Fgesturedetector.cpp;fp=source%2Finput%2Fgesturedetector.cpp;h=c43e65a0d61c5be930e2672ae356f702c8059bf9;hb=f9ebab54c3debe98e108790be9c21f43f4de116f;hp=0000000000000000000000000000000000000000;hpb=754d00736b05c0f07f2fe34889527f8e3013dd37;p=libs%2Fgui.git diff --git a/source/input/gesturedetector.cpp b/source/input/gesturedetector.cpp new file mode 100644 index 0000000..c43e65a --- /dev/null +++ b/source/input/gesturedetector.cpp @@ -0,0 +1,210 @@ +#include +#include +#include "gesturedetector.h" +#include "touchscreen.h" + +using namespace std; + +namespace Msp { +namespace Input { + +GestureDetector::GestureDetector(Touchscreen &ts): + touchscreen(ts), + current_gesture(GESTURE_NONE), + active_points(0), + invalid_gesture(false) +{ + name = "Gesture"; + + touchscreen.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_down), false)); + touchscreen.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_up), false)); + touchscreen.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &GestureDetector::touch_move), false)); + Graphics::Window &window = touchscreen.get_window(); + window.signal_resize.connect(sigc::mem_fun(this, &GestureDetector::window_resized)); + window_resized(window.get_width(), window.get_height()); +} + +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"; + else if(btn==GESTURE_PINCH) + return "Pinch"; + else + return Device::get_button_name(btn); +} + +string GestureDetector::get_axis_name(unsigned axis) const +{ + if(axis==0) + return "X"; + else if(axis==1) + return "Y"; + else if(axis==2) + return "Progress"; + else + return Device::get_axis_name(axis); +} + +void GestureDetector::touch_down(unsigned btn) +{ + if(btn<3) + { + TouchPoint &p = points[btn]; + p.down = true; + p.down_x = p.x; + p.down_y = p.y; + } +} + +void GestureDetector::touch_up(unsigned btn) +{ + if(btn<3) + { + TouchPoint &p = points[btn]; + p.down = false; + + if(active_points&(1<>1; + TouchPoint &p = points[i]; + if(axis&1) + p.y = value; + else + p.x = value; + + if(p.down) + { + if(current_gesture==GESTURE_NONE && !invalid_gesture) + start_gesture(); + else if(active_points&(1<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_button_state(current_gesture, true, true); +} + +void GestureDetector::update_progress() +{ + TouchPoint &p = points[0]; + + if(current_gesture==GESTURE_SWIPE_DOWN) + set_axis_value(2, p.down_y-p.y, true); + else if(current_gesture==GESTURE_SWIPE_UP) + set_axis_value(2, p.y-p.down_y, true); + else if(current_gesture==GESTURE_SWIPE_LEFT) + set_axis_value(2, p.down_x-p.x, true); + else if(current_gesture==GESTURE_SWIPE_RIGHT) + set_axis_value(2, p.x-p.down_x, true); + else if(current_gesture==GESTURE_PINCH) + { + TouchPoint &p2 = points[1]; + float dx = p.x-p2.x; + float dy = p.y-p2.y; + float ddx = p.down_x-p2.down_x; + float ddy = p.down_y-p2.down_y; + set_axis_value(2, sqrt(dx*dx+dy*dy)/sqrt(ddx*ddx+ddy*ddy)-1, true); + } +} + +void GestureDetector::window_resized(unsigned w, unsigned h) +{ + threshold_x_sq = 2500.0/(w*w); + threshold_y_sq = 2500.0/(h*h); +} + + +GestureDetector::TouchPoint::TouchPoint(): + down(false), + down_x(0), + down_y(0), + x(0), + y(0) +{ } + +} // namespace Input +} // namespace Msp