]> git.tdb.fi Git - libs/gui.git/blob - source/input/gesturedetector.h
b2d0d4fdd36139411c3fb61e8b1062e575ea3ba4
[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         enum
31         {
32                 MAX_POINTS = 3
33         };
34
35         struct TouchPoint
36         {
37                 bool down;
38                 float down_x;
39                 float down_y;
40                 float x;
41                 float y;
42                 bool threshold_exceeded;
43
44                 TouchPoint();
45         };
46
47         Touchscreen &touchscreen;
48         TouchPoint points[MAX_POINTS];
49         Gesture current_gesture;
50         unsigned active_points;
51         bool invalid_gesture;
52         float threshold_x_sq;
53         float threshold_y_sq;
54
55 public:
56         GestureDetector(Touchscreen &);
57
58         virtual std::string get_button_name(unsigned) const;
59         virtual std::string get_axis_name(unsigned) const;
60
61 private:
62         void touch_down(unsigned);
63         void touch_up(unsigned);
64         void touch_move(unsigned, float, float);
65         void start_gesture();
66         void update_progress();
67         void end_gesture();
68         void window_resized(unsigned, unsigned);
69 };
70
71 } // namespace Input
72 } // namespace Msp
73
74 #endif