]> git.tdb.fi Git - libs/gui.git/blob - source/input/gesturedetector.h
Implement tap gestures
[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_DRAG,
17         GESTURE_DRAG_2,
18         GESTURE_PINCH,
19         GESTURE_ROTATE
20 };
21
22 /**
23 Interprets events from a Touchscreen as high-level gestures.  One button is
24 provided for each type of gesture.  Axes 0 and 1 indicate the starting position
25 of the gesture; axes 2 and 3 track its progress.  The progress axis may exhibit
26 absolute values greater than one.
27 */
28 class GestureDetector: public Device
29 {
30 private:
31         enum
32         {
33                 MAX_POINTS = 3
34         };
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;
51         unsigned active_points;
52         Gesture pending_tap;
53         bool invalid_gesture;
54         float threshold_x_sq;
55         float threshold_y_sq;
56
57 public:
58         GestureDetector(Touchscreen &);
59
60         virtual std::string get_button_name(unsigned) const;
61         virtual std::string get_axis_name(unsigned) const;
62
63 private:
64         void touch_down(unsigned);
65         void touch_up(unsigned);
66         void touch_move(unsigned, float, float);
67         void start_gesture();
68         void set_gesture_location(unsigned);
69         void update_progress();
70         void end_gesture();
71         void window_resized(unsigned, unsigned);
72 };
73
74 } // namespace Input
75 } // namespace Msp
76
77 #endif