]> git.tdb.fi Git - libs/gui.git/blob - source/input/gesturedetector.h
Add a gesture detector input device
[libs/gui.git] / source / input / gesturedetector.h
1 #ifndef MSP_INPUT_GESTUREDETECTOR_H_
2 #define MSP_INPUT_GESTUREDETECTOR_H_
3
4 #include "device.h"
5
6 namespace Msp {
7 namespace Input {
8
9 class Touchscreen;
10
11 enum Gesture
12 {
13         GESTURE_NONE,
14         GESTURE_SWIPE_DOWN,
15         GESTURE_SWIPE_UP,
16         GESTURE_SWIPE_LEFT,
17         GESTURE_SWIPE_RIGHT,
18         GESTURE_PINCH
19 };
20
21 /**
22 Interprets events from a Touchscreen as high-level gestures.  One button is
23 provided for each type of gesture.  Axes 0 and 1 indicate the starting position
24 of the gesture; axis 2 tracks its progress.  The progress axis may exhibit
25 absolute values greater than one.
26 */
27 class GestureDetector: public Device
28 {
29 private:
30         struct TouchPoint
31         {
32                 bool down;
33                 float down_x;
34                 float down_y;
35                 float x;
36                 float y;
37
38                 TouchPoint();
39         };
40
41         Touchscreen &touchscreen;
42         TouchPoint points[3];
43         Gesture current_gesture;
44         unsigned active_points;
45         bool invalid_gesture;
46         float threshold_x_sq;
47         float threshold_y_sq;
48
49 public:
50         GestureDetector(Touchscreen &);
51
52         virtual std::string get_button_name(unsigned) const;
53         virtual std::string get_axis_name(unsigned) const;
54
55 private:
56         void touch_down(unsigned);
57         void touch_up(unsigned);
58         void touch_move(unsigned, float, float);
59         void start_gesture();
60         void update_progress();
61         void window_resized(unsigned, unsigned);
62 };
63
64 } // namespace Input
65 } // namespace Msp
66
67 #endif