Cogs.Core
Gestures.h
1#pragma once
2#include "Mouse.h"
3
4#include <optional>
5#include <unordered_map>
6
7namespace Cogs
8{
9 using PointerId = size_t;
10
11 enum class PointerType
12 {
13 Mouse,
14 Touch,
15
16 Count,
17 Unknown,
18 };
19
20 struct Gesture
21 {
22 struct Coord {
23 int32_t x;
24 int32_t y;
25 };
26
27 struct Hover
28 {
30 };
31 struct Wheel
32 {
34 int32_t delta;
35 };
36 struct Tap
37 {
38 MouseButton button;
40 };
41 struct DoubleTap
42 {
43 MouseButton button;
45 };
46 struct Press
47 {
48 MouseButton button;
50 };
51 struct Drag
52 {
53 MouseButton button;
56 };
57 struct Swipe
58 {
59 enum Direction {
60 Left,
61 Right,
62 Up,
63 Down
64 };
65
66 MouseButton button;
67 Direction direction;
69 float velocity;
70 };
71 struct Pan
72 {
75 };
76 struct Rotate
77 {
79 float angle;
80 };
81 struct Pinch
82 {
84 float scale;
85 };
86
87 enum struct Kind : uint32_t
88 {
89 Hover,
90 Wheel,
91 Tap,
92 DoubleTap,
93 Press,
94 Drag,
95 Swipe,
96 Pan,
97 Rotate,
98 Pinch,
99 };
100
101 enum struct State : uint8_t
102 {
103 Started,
104 Changed,
105 Ended,
106 Cancelled
107 };
108
110 uint32_t id;
112 union {
114 Wheel wheel;
115 Tap tap;
116 DoubleTap doubleTap;
117 Press press;
118 Drag drag;
119 Swipe swipe;
120 Pan pan;
121 Rotate rotate;
122 Pinch pinch;
123 };
124 };
125
126 class COGSFOUNDATION_API Gestures
127 {
128 public:
129 // Enable/disable gestures
130 bool hoverEnable = true;
131 bool wheelEnable = true;
132 bool tapEnable = true;
133 bool doubleTapEnable = true;
134 bool pressEnable = true;
135 bool dragEnable = true;
136 bool swipeEnable = true;
137 bool panEnable = true;
138 bool rotateEnable = true;
139 bool pinchEnable = true;
140
141 // Configuration
142 uint32_t tapMaxDuration = 250;
143 uint32_t doubleTapTreshold = 300;
144 uint32_t swipeMaxDuration = 200;
145 float mouseMoveThreshold = 1.f;
146 float touchMoveThreshold = 16.f;
147 float touchVelocityThreshold = 500.f;
148 float mouseVelocityThreshold = 100.f;
149 float rotateThreshold = 0.05f;
150 float pinchThreshold = 0.1f;
151
152 Gestures();
153 void update();
154 void setDisplayScale(float scale) { displayScale = scale; }
155
156 void pointerDown(PointerType pointerType, PointerId pointerId, MouseButton button, glm::ivec2 position, double timestamp_ms);
157 void pointerUp(PointerType pointerType, PointerId pointerId, MouseButton button, glm::ivec2 position, double timestamp_ms);
158 void pointerMove(PointerType pointerType, PointerId pointerId, glm::ivec2 position, double timestamp_ms);
159 void mouseWheelMove(int32_t delta);
160 void reset();
161
163 PointerType getPointerType();
164
166 const std::vector<Gesture>& getGestures() const { return presentedGestures; }
167
168 private:
169 float displayScale = 1.0f;
170
171 PointerType currPointerType = PointerType::Unknown;
172
173 std::vector<Gesture> presentedGestures; // Will be available next frame and last one frame
174 std::vector<Gesture> currentGestures; // Update durring the current frame. At the end of the frame it is moved to presentedGestures, then cleaned.
175
176 // Timestamp of last tap, used for double tap detection.
177 double lastTapTimestamp = 0;
178 // State for updatable gestures.
179 std::optional<Gesture> currPress = std::nullopt;
180 std::optional<Gesture> currDrag = std::nullopt;
181 std::optional<Gesture> currPan = std::nullopt;
182 std::optional<Gesture> currRotate = std::nullopt;
183 std::optional<Gesture> currPinch = std::nullopt;
184
186 {
187 enum State: uint32_t
188 {
189 None,
190 Pressed,
191 Moving,
192 Released
193 };
194
195 struct Data
196 {
197 double timestamp_ms = 0.0;
198 glm::ivec2 position;
199 };
200
201 PointerType type = PointerType::Unknown;
202 MouseButton button = MouseButton::NoButton;
203 State state = State::None;
207 };
208
209 std::unordered_map<PointerId, PointerState> currentPointers;
210
211 uint32_t idCounter = 0;
212 [[nodiscard]] uint32_t nextGestureId() { return idCounter++; }
213
214
215 void endUpdatingGesture(std::optional<Gesture>& gesture, Gesture::State state);
216 };
217}
const std::vector< Gesture > & getGestures() const
Return all gestures recognised in the previous frame. Use id to differentiate between gestures.
Definition: Gestures.h:166
std::unordered_map< PointerId, PointerState > currentPointers
Set of currently active pointers.
Definition: Gestures.h:209
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
MouseButton button
Button that was tapped.
Definition: Gestures.h:43
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:44
Coord currCoord
Pointer position when the event was triggered.
Definition: Gestures.h:55
MouseButton button
Button that was tapped.
Definition: Gestures.h:53
Coord startCoord
Pointer position when the gesture has started.
Definition: Gestures.h:54
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:29
Coord midStartCoord
Mid position between the two pointer positions when the gesture has started.
Definition: Gestures.h:73
Coord midCurrCoord
Mid position between the two pointer positions when the event was triggered.
Definition: Gestures.h:74
Coord center
Mid position between the two pointer positions when the event was triggered.
Definition: Gestures.h:83
float scale
Scale factor, 1 when the gesture starts.
Definition: Gestures.h:84
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:49
MouseButton button
Button that was tapped.
Definition: Gestures.h:48
float angle
Rotation angle in radians, 0 when the gesture starts.
Definition: Gestures.h:79
Coord center
Mid position between the two pointer positions when the event was triggered.
Definition: Gestures.h:78
Direction direction
Swipe general direction.
Definition: Gestures.h:67
Coord startCoord
Pointer position when the gesture has started.
Definition: Gestures.h:68
float velocity
Pointer velocity relative to startCoord.
Definition: Gestures.h:69
MouseButton button
Button that was tapped.
Definition: Gestures.h:66
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:39
MouseButton button
Button that was tapped.
Definition: Gestures.h:38
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:33
int32_t delta
Amount by which the mouse wheel was moved.
Definition: Gestures.h:34
@ Ended
Gesture ended or single-fire events, used by all gestures.
@ Changed
Gesture changed, used by: Drag, Swipe, Pan, Rotate, Pinch.
@ Started
Gesture started, used by: Press, Drag, Swipe, Pan, Rotate, Pinch.
@ Cancelled
Gesture cancelled, used by: Press, Drag, Swipe, Pan, Rotate, Pinch.
Hover hover
< Specific gesture data.
Definition: Gestures.h:113
Kind kind
Gesture type, see Kind.
Definition: Gestures.h:109
State state
Gesture state, see State.
Definition: Gestures.h:111
uint32_t id
Unique gesture id.
Definition: Gestures.h:110
glm::ivec2 position
Pixel x and y position.
Definition: Gestures.h:198
Data curr
Current pointer data.
Definition: Gestures.h:206
Data init
Pointer data when pointer was pressed.
Definition: Gestures.h:204
Data prev
Pointer data before previous update.
Definition: Gestures.h:205