]> git.tdb.fi Git - libs/gui.git/blob - source/input/gesturedetector.h
Use constexpr instead of enum to define "typeless" constants
[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_TAP,
15         GESTURE_TAP_2,
16         GESTURE_TAP_3,
17         GESTURE_DRAG,
18         GESTURE_DRAG_2,
19         GESTURE_DRAG_3,
20         GESTURE_PINCH,
21         GESTURE_ROTATE
22 };
23
24 /**
25 Interprets events from a Touchscreen as high-level gestures.  One button is
26 provided for each type of gesture.  Axes 0 and 1 indicate the starting position
27 of the gesture; axes 2 and 3 track its progress.  The progress axis may exhibit
28 absolute values greater than one.
29 */
30 class GestureDetector: public Device
31 {
32 private:
33         static constexpr size_t MAX_POINTS = 3;
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 = GESTURE_NONE;
50         Gesture pending_tap = GESTURE_NONE;
51         bool invalid_gesture = false;
52         float threshold_x_sq = 1.0f;
53         float threshold_y_sq = 1.0f;
54
55 public:
56         GestureDetector(Touchscreen &);
57
58         std::string get_button_name(unsigned) const override;
59         std::string get_axis_name(unsigned) const override;
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 set_gesture_location(unsigned);
67         void set_gesture_delta(unsigned);
68         void update_progress();
69         void end_gesture();
70         void window_resized(unsigned, unsigned);
71 };
72
73
74 unsigned gesture_points(Gesture);
75
76 } // namespace Input
77 } // namespace Msp
78
79 #endif