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