Cogs.Foundation
Loading...
Searching...
No Matches
Gestures.h
Go to the documentation of this file.
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 {
40 };
41 struct DoubleTap
42 {
45 };
46 struct Press
47 {
50 };
51 struct Drag
52 {
56 };
57 struct Swipe
58 {
59 enum Direction {
63 Down
64 };
65
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 {
123 };
124 };
125
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 {
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}
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
Definition: Gestures.h:127
std::vector< Gesture > presentedGestures
Definition: Gestures.h:173
std::vector< Gesture > currentGestures
Definition: Gestures.h:174
uint32_t nextGestureId()
Definition: Gestures.h:212
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
void setDisplayScale(float scale)
Definition: Gestures.h:154
Definition: Mouse.h:25
Main Cogs namespace.
Definition: MortonCode.h:5
PointerType
Definition: Gestures.h:12
size_t PointerId
Definition: Gestures.h:9
MouseButton
Definition: Mouse.h:15
@ Count
Definition: Mouse.h:20
Definition: Gestures.h:22
int32_t x
Definition: Gestures.h:23
int32_t y
Definition: Gestures.h:24
Definition: Gestures.h:42
MouseButton button
Button that was tapped.
Definition: Gestures.h:43
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:44
Definition: Gestures.h:52
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
Definition: Gestures.h:28
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:29
Definition: Gestures.h:72
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
Definition: Gestures.h:82
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
Definition: Gestures.h:47
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:49
MouseButton button
Button that was tapped.
Definition: Gestures.h:48
Definition: Gestures.h:77
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
Definition: Gestures.h:58
Direction
Definition: Gestures.h:59
@ Left
Definition: Gestures.h:60
@ Down
Definition: Gestures.h:63
@ Up
Definition: Gestures.h:62
@ Right
Definition: Gestures.h:61
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
Definition: Gestures.h:37
Coord coord
Pointer position when the event was triggered.
Definition: Gestures.h:39
MouseButton button
Button that was tapped.
Definition: Gestures.h:38
Definition: Gestures.h:32
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
Definition: Gestures.h:21
Kind
Definition: Gestures.h:88
Rotate rotate
Definition: Gestures.h:121
Press press
Definition: Gestures.h:117
State
Definition: Gestures.h:102
@ 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
Pan pan
Definition: Gestures.h:120
Pinch pinch
Definition: Gestures.h:122
Drag drag
Definition: Gestures.h:118
Swipe swipe
Definition: Gestures.h:119
Kind kind
Gesture type, see Kind.
Definition: Gestures.h:109
State state
Gesture state, see State.
Definition: Gestures.h:111
Tap tap
Definition: Gestures.h:115
Wheel wheel
Definition: Gestures.h:114
DoubleTap doubleTap
Definition: Gestures.h:116
uint32_t id
Unique gesture id.
Definition: Gestures.h:110
Definition: Gestures.h:196
glm::ivec2 position
Pixel x and y position.
Definition: Gestures.h:198
Definition: Gestures.h:186
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
State
Definition: Gestures.h:188
@ Moving
Definition: Gestures.h:191
@ Pressed
Definition: Gestures.h:190
@ None
Definition: Gestures.h:189