]> git.tdb.fi Git - libs/gui.git/blob - source/input/gesturedetector.h
Add decorations for things which should be exported from the library
[libs/gui.git] / source / input / gesturedetector.h
1 #ifndef MSP_INPUT_GESTUREDETECTOR_H_
2 #define MSP_INPUT_GESTUREDETECTOR_H_
3
4 #include <msp/graphics/mspgui_api.h>
5 #include "device.h"
6
7 namespace Msp {
8 namespace Input {
9
10 class Touchscreen;
11
12 enum Gesture
13 {
14         GESTURE_NONE,
15         GESTURE_TAP,
16         GESTURE_TAP_2,
17         GESTURE_TAP_3,
18         GESTURE_DRAG,
19         GESTURE_DRAG_2,
20         GESTURE_DRAG_3,
21         GESTURE_PINCH,
22         GESTURE_ROTATE
23 };
24
25 /**
26 Interprets events from a Touchscreen as high-level gestures.  One button is
27 provided for each type of gesture.  Axes 0 and 1 indicate the starting position
28 of the gesture; axes 2 and 3 track its progress.  The progress axis may exhibit
29 absolute values greater than one.
30 */
31 class MSPGUI_API GestureDetector: public Device
32 {
33 private:
34         static constexpr size_t MAX_POINTS = 3;
35
36         struct TouchPoint
37         {
38                 bool down;
39                 float down_x;
40                 float down_y;
41                 float x;
42                 float y;
43                 bool threshold_exceeded;
44
45                 TouchPoint();
46         };
47
48         Touchscreen &touchscreen;
49         TouchPoint points[MAX_POINTS];
50         Gesture current_gesture = GESTURE_NONE;
51         Gesture pending_tap = GESTURE_NONE;
52         bool invalid_gesture = false;
53         float threshold_x_sq = 1.0f;
54         float threshold_y_sq = 1.0f;
55
56 public:
57         GestureDetector(Touchscreen &);
58
59         std::string get_button_name(unsigned) const override;
60         std::string get_axis_name(unsigned) const override;
61
62 private:
63         void touch_down(unsigned);
64         void touch_up(unsigned);
65         void touch_move(unsigned, float, float);
66         void start_gesture();
67         void set_gesture_location(unsigned);
68         void set_gesture_delta(unsigned);
69         void update_progress();
70         void end_gesture();
71         void window_resized(unsigned, unsigned);
72 };
73
74
75 MSPGUI_API unsigned gesture_points(Gesture);
76
77 } // namespace Input
78 } // namespace Msp
79
80 #endif