]> git.tdb.fi Git - libs/gui.git/blobdiff - source/input/gesturedetector.h
Add a gesture detector input device
[libs/gui.git] / source / input / gesturedetector.h
diff --git a/source/input/gesturedetector.h b/source/input/gesturedetector.h
new file mode 100644 (file)
index 0000000..2b5ea3c
--- /dev/null
@@ -0,0 +1,67 @@
+#ifndef MSP_INPUT_GESTUREDETECTOR_H_
+#define MSP_INPUT_GESTUREDETECTOR_H_
+
+#include "device.h"
+
+namespace Msp {
+namespace Input {
+
+class Touchscreen;
+
+enum Gesture
+{
+       GESTURE_NONE,
+       GESTURE_SWIPE_DOWN,
+       GESTURE_SWIPE_UP,
+       GESTURE_SWIPE_LEFT,
+       GESTURE_SWIPE_RIGHT,
+       GESTURE_PINCH
+};
+
+/**
+Interprets events from a Touchscreen as high-level gestures.  One button is
+provided for each type of gesture.  Axes 0 and 1 indicate the starting position
+of the gesture; axis 2 tracks its progress.  The progress axis may exhibit
+absolute values greater than one.
+*/
+class GestureDetector: public Device
+{
+private:
+       struct TouchPoint
+       {
+               bool down;
+               float down_x;
+               float down_y;
+               float x;
+               float y;
+
+               TouchPoint();
+       };
+
+       Touchscreen &touchscreen;
+       TouchPoint points[3];
+       Gesture current_gesture;
+       unsigned active_points;
+       bool invalid_gesture;
+       float threshold_x_sq;
+       float threshold_y_sq;
+
+public:
+       GestureDetector(Touchscreen &);
+
+       virtual std::string get_button_name(unsigned) const;
+       virtual std::string get_axis_name(unsigned) const;
+
+private:
+       void touch_down(unsigned);
+       void touch_up(unsigned);
+       void touch_move(unsigned, float, float);
+       void start_gesture();
+       void update_progress();
+       void window_resized(unsigned, unsigned);
+};
+
+} // namespace Input
+} // namespace Msp
+
+#endif