0/**********************************************************************************************
1*
2* rgestures - Gestures system, gestures processing based on input events (touch/mouse)
3*
4* CONFIGURATION:
5* #define RGESTURES_IMPLEMENTATION
6* Generates the implementation of the library into the included file.
7* If not defined, the library is in header only mode and can be included in other headers
8* or source files without problems. But only ONE file should hold the implementation.
9*
10* #define RGESTURES_STANDALONE
11* If defined, the library can be used as standalone to process gesture events with
12* no external dependencies.
13*
14* CONTRIBUTORS:
15* Marc Palau: Initial implementation (2014)
16* Albert Martos: Complete redesign and testing (2015)
17* Ian Eito: Complete redesign and testing (2015)
18* Ramon Santamaria: Supervision, review, update and maintenance
19*
20*
21* LICENSE: zlib/libpng
22*
23* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
24*
25* This software is provided "as-is", without any express or implied warranty. In no event
26* will the authors be held liable for any damages arising from the use of this software.
27*
28* Permission is granted to anyone to use this software for any purpose, including commercial
29* applications, and to alter it and redistribute it freely, subject to the following restrictions:
30*
31* 1. The origin of this software must not be misrepresented; you must not claim that you
32* wrote the original software. If you use this software in a product, an acknowledgment
33* in the product documentation would be appreciated but is not required.
34*
35* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
36* as being the original software.
37*
38* 3. This notice may not be removed or altered from any source distribution.
39*
40**********************************************************************************************/
42#ifndef RGESTURES_H
43#define RGESTURES_H
45#ifndef PI
46 #define PI 3.14159265358979323846
47#endif
49//----------------------------------------------------------------------------------
50// Defines and Macros
51//----------------------------------------------------------------------------------
52#ifndef MAX_TOUCH_POINTS
53 #define MAX_TOUCH_POINTS 8 // Maximum number of touch points supported
54#endif
56//----------------------------------------------------------------------------------
57// Types and Structures Definition
58// NOTE: Below types are required for standalone usage
59//----------------------------------------------------------------------------------
60// Boolean type
61#if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800)
62 #include <stdbool.h>
63#elif !defined(__cplusplus) && !defined(bool) && !defined(RL_BOOL_TYPE)
64 typedef enum bool { false = 0, true = !false } bool;
65#endif
67#if !defined(RL_VECTOR2_TYPE)
68// Vector2 type
69typedef struct Vector2 {
70 float x;
71 float y;
72} Vector2;
73#endif
75#if defined(RGESTURES_STANDALONE)
76// Gestures type
77// NOTE: It could be used as flags to enable only some gestures
78typedef enum {
79 GESTURE_NONE = 0,
80 GESTURE_TAP = 1,
81 GESTURE_DOUBLETAP = 2,
82 GESTURE_HOLD = 4,
83 GESTURE_DRAG = 8,
84 GESTURE_SWIPE_RIGHT = 16,
85 GESTURE_SWIPE_LEFT = 32,
86 GESTURE_SWIPE_UP = 64,
87 GESTURE_SWIPE_DOWN = 128,
88 GESTURE_PINCH_IN = 256,
89 GESTURE_PINCH_OUT = 512
90} Gesture;
91#endif
93typedef enum {
94 TOUCH_ACTION_UP = 0,
95 TOUCH_ACTION_DOWN,
96 TOUCH_ACTION_MOVE,
97 TOUCH_ACTION_CANCEL
98} TouchAction;
100// Gesture event
101typedef struct {
102 int touchAction;
103 int pointCount;
104 int pointId[MAX_TOUCH_POINTS];
105 Vector2 position[MAX_TOUCH_POINTS];
106} GestureEvent;
108//----------------------------------------------------------------------------------
109// Global Variables Definition
110//----------------------------------------------------------------------------------
111//...
113//----------------------------------------------------------------------------------
114// Module Functions Declaration
115//----------------------------------------------------------------------------------
117#if defined(__cplusplus)
118extern "C" { // Prevents name mangling of functions
119#endif
121void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
122void UpdateGestures(void); // Update gestures detected (must be called every frame)
124#if defined(RGESTURES_STANDALONE)
125void SetGesturesEnabled(unsigned int flags); // Enable a set of gestures using flags
126bool IsGestureDetected(int gesture); // Check if a gesture have been detected
127int GetGestureDetected(void); // Get latest detected gesture
129float GetGestureHoldDuration(void); // Get gesture hold time in seconds
130Vector2 GetGestureDragVector(void); // Get gesture drag vector
131float GetGestureDragAngle(void); // Get gesture drag angle
132Vector2 GetGesturePinchVector(void); // Get gesture pinch delta
133float GetGesturePinchAngle(void); // Get gesture pinch angle
134#endif
136#if defined(__cplusplus)
137}
138#endif
140#endif // RGESTURES_H
142/***********************************************************************************
143*
144* RGESTURES IMPLEMENTATION
145*
146************************************************************************************/
148#if defined(RGESTURES_IMPLEMENTATION)
150#if defined(RGESTURES_STANDALONE)
151#if defined(_WIN32)
152 #if defined(__cplusplus)
153 extern "C" { // Prevents name mangling of functions
154 #endif
155 // Functions required to query time on Windows
156 int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
157 int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency);
158 #if defined(__cplusplus)
159 }
160 #endif
161#elif defined(__linux__)
162 #if _POSIX_C_SOURCE < 199309L
163 #undef _POSIX_C_SOURCE
164 #define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext.
165 #endif
166 #include <sys/time.h> // Required for: timespec
167 #include <time.h> // Required for: clock_gettime()
169 #include <math.h> // Required for: sqrtf(), atan2f()
170#endif
171#if defined(__APPLE__) // macOS also defines __MACH__
172 #include <mach/clock.h> // Required for: clock_get_time()
173 #include <mach/mach.h> // Required for: mach_timespec_t
174#endif
175#endif
177//----------------------------------------------------------------------------------
178// Defines and Macros
179//----------------------------------------------------------------------------------
180#define FORCE_TO_SWIPE 0.2f // Swipe force, measured in normalized screen units/time
181#define MINIMUM_DRAG 0.015f // Drag minimum force, measured in normalized screen units (0.0f to 1.0f)
182#define DRAG_TIMEOUT 0.3f // Drag minimum time for web, measured in seconds
183#define MINIMUM_PINCH 0.005f // Pinch minimum force, measured in normalized screen units (0.0f to 1.0f)
184#define TAP_TIMEOUT 0.3f // Tap minimum time, measured in seconds
185#define PINCH_TIMEOUT 0.3f // Pinch minimum time, measured in seconds
186#define DOUBLETAP_RANGE 0.03f // DoubleTap range, measured in normalized screen units (0.0f to 1.0f)
188//----------------------------------------------------------------------------------
189// Types and Structures Definition
190//----------------------------------------------------------------------------------
192// Gestures module state context [136 bytes]
193typedef struct {
194 unsigned int current; // Current detected gesture
195 unsigned int enabledFlags; // Enabled gestures flags
196 struct {
197 int firstId; // Touch id for first touch point
198 int pointCount; // Touch points counter
199 double eventTime; // Time stamp when an event happened
200 Vector2 upPosition; // Touch up position
201 Vector2 downPositionA; // First touch down position
202 Vector2 downPositionB; // Second touch down position
203 Vector2 downDragPosition; // Touch drag position
204 Vector2 moveDownPositionA; // First touch down position on move
205 Vector2 moveDownPositionB; // Second touch down position on move
206 Vector2 previousPositionA; // Previous position A to compare for pinch gestures
207 Vector2 previousPositionB; // Previous position B to compare for pinch gestures
208 int tapCounter; // TAP counter (one tap implies TOUCH_ACTION_DOWN and TOUCH_ACTION_UP actions)
209 } Touch;
210 struct {
211 bool resetRequired; // HOLD reset to get first touch point again
212 double timeDuration; // HOLD duration in seconds
213 } Hold;
214 struct {
215 Vector2 vector; // DRAG vector (between initial and current position)
216 float angle; // DRAG angle (relative to x-axis)
217 float distance; // DRAG distance (from initial touch point to final) (normalized [0..1])
218 float intensity; // DRAG intensity, how far why did the DRAG (pixels per frame)
219 } Drag;
220 struct {
221 double startTime; // SWIPE start time to calculate drag intensity
222 } Swipe;
223 struct {
224 Vector2 vector; // PINCH vector (between first and second touch points)
225 float angle; // PINCH angle (relative to x-axis)
226 float distance; // PINCH displacement distance (normalized [0..1])
227 } Pinch;
228} GesturesData;
230//----------------------------------------------------------------------------------
231// Global Variables Definition
232//----------------------------------------------------------------------------------
233static GesturesData GESTURES = {
234 .current = GESTURE_NONE, // No current gesture detected
235 .Touch.firstId = -1,
236 .enabledFlags = 0b0000001111111111 // All gestures supported by default
237};
239//----------------------------------------------------------------------------------
240// Module Internal Functions Declaration
241//----------------------------------------------------------------------------------
242static float rgVector2Angle(Vector2 initialPosition, Vector2 finalPosition);
243static float rgVector2Distance(Vector2 v1, Vector2 v2);
244static double rgGetCurrentTime(void);
246//----------------------------------------------------------------------------------
247// Module Functions Definition
248//----------------------------------------------------------------------------------
250// Enable only desired gestures to be detected
251void SetGesturesEnabled(unsigned int flags)
252{
253 GESTURES.enabledFlags = flags;
254}
256// Check if a gesture have been detected
257bool IsGestureDetected(unsigned int gesture)
258{
259 if ((GESTURES.enabledFlags & GESTURES.current) == gesture) return true;
260 else return false;
261}
263// Process gesture event and translate it into gestures
264void ProcessGestureEvent(GestureEvent event)
265{
266 // Reset required variables
267 GESTURES.Touch.pointCount = event.pointCount; // Required on UpdateGestures()
269 if (GESTURES.Touch.pointCount == 1) // One touch point
270 {
271 if (event.touchAction == TOUCH_ACTION_DOWN)
272 {
273 GESTURES.Touch.tapCounter++; // Tap counter
275 // Detect GESTURE_DOUBLE_TAP
276 if ((GESTURES.current == GESTURE_NONE) && (GESTURES.Touch.tapCounter >= 2) && ((rgGetCurrentTime() - GESTURES.Touch.eventTime) < TAP_TIMEOUT) && (rgVector2Distance(GESTURES.Touch.downPositionA, event.position[0]) < DOUBLETAP_RANGE))
277 {
278 GESTURES.current = GESTURE_DOUBLETAP;
279 GESTURES.Touch.tapCounter = 0;
280 }
281 else // Detect GESTURE_TAP
282 {
283 GESTURES.Touch.tapCounter = 1;
284 GESTURES.current = GESTURE_TAP;
285 }
287 GESTURES.Touch.downPositionA = event.position[0];
288 GESTURES.Touch.downDragPosition = event.position[0];
290 GESTURES.Touch.upPosition = GESTURES.Touch.downPositionA;
291 GESTURES.Touch.eventTime = rgGetCurrentTime();
293 GESTURES.Swipe.startTime = rgGetCurrentTime();
295 GESTURES.Drag.vector = (Vector2){ 0.0f, 0.0f };
296 }
297 else if (event.touchAction == TOUCH_ACTION_UP)
298 {
299 // A swipe can happen while the current gesture is drag, but (specially for web) also hold, so set upPosition for both cases
300 if (GESTURES.current == GESTURE_DRAG || GESTURES.current == GESTURE_HOLD) GESTURES.Touch.upPosition = event.position[0];
302 // NOTE: GESTURES.Drag.intensity dependent on the resolution of the screen
303 GESTURES.Drag.distance = rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition);
304 GESTURES.Drag.intensity = GESTURES.Drag.distance/(float)((rgGetCurrentTime() - GESTURES.Swipe.startTime));
306 // Detect GESTURE_SWIPE
307 if ((GESTURES.Drag.intensity > FORCE_TO_SWIPE) && (GESTURES.current != GESTURE_DRAG))
308 {
309 // NOTE: Angle should be inverted in Y
310 GESTURES.Drag.angle = 360.0f - rgVector2Angle(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition);
312 if ((GESTURES.Drag.angle < 30) || (GESTURES.Drag.angle > 330)) GESTURES.current = GESTURE_SWIPE_RIGHT; // Right
313 else if ((GESTURES.Drag.angle >= 30) && (GESTURES.Drag.angle <= 150)) GESTURES.current = GESTURE_SWIPE_UP; // Up
314 else if ((GESTURES.Drag.angle > 150) && (GESTURES.Drag.angle < 210)) GESTURES.current = GESTURE_SWIPE_LEFT; // Left
315 else if ((GESTURES.Drag.angle >= 210) && (GESTURES.Drag.angle <= 330)) GESTURES.current = GESTURE_SWIPE_DOWN; // Down
316 else GESTURES.current = GESTURE_NONE;
317 }
318 else
319 {
320 GESTURES.Drag.distance = 0.0f;
321 GESTURES.Drag.intensity = 0.0f;
322 GESTURES.Drag.angle = 0.0f;
324 GESTURES.current = GESTURE_NONE;
325 }
327 GESTURES.Touch.downDragPosition = (Vector2){ 0.0f, 0.0f };
328 GESTURES.Touch.pointCount = 0;
329 }
330 else if (event.touchAction == TOUCH_ACTION_MOVE)
331 {
332 GESTURES.Touch.moveDownPositionA = event.position[0];
334 if (GESTURES.current == GESTURE_HOLD)
335 {
336 if (GESTURES.Hold.resetRequired) GESTURES.Touch.downPositionA = event.position[0];
338 GESTURES.Hold.resetRequired = false;
340 // Detect GESTURE_DRAG
341 if ((rgGetCurrentTime() - GESTURES.Touch.eventTime) > DRAG_TIMEOUT)
342 {
343 GESTURES.Touch.eventTime = rgGetCurrentTime();
344 GESTURES.current = GESTURE_DRAG;
345 }
346 }
348 GESTURES.Drag.vector.x = GESTURES.Touch.moveDownPositionA.x - GESTURES.Touch.downDragPosition.x;
349 GESTURES.Drag.vector.y = GESTURES.Touch.moveDownPositionA.y - GESTURES.Touch.downDragPosition.y;
350 }
351 }
352 else if (GESTURES.Touch.pointCount == 2) // Two touch points
353 {
354 if (event.touchAction == TOUCH_ACTION_DOWN)
355 {
356 GESTURES.Touch.downPositionA = event.position[0];
357 GESTURES.Touch.downPositionB = event.position[1];
359 GESTURES.Touch.previousPositionA = GESTURES.Touch.downPositionA;
360 GESTURES.Touch.previousPositionB = GESTURES.Touch.downPositionB;
362 //GESTURES.Pinch.distance = rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.downPositionB);
364 GESTURES.Pinch.vector.x = GESTURES.Touch.downPositionB.x - GESTURES.Touch.downPositionA.x;
365 GESTURES.Pinch.vector.y = GESTURES.Touch.downPositionB.y - GESTURES.Touch.downPositionA.y;
367 GESTURES.current = GESTURE_HOLD;
368 GESTURES.Hold.timeDuration = rgGetCurrentTime();
369 }
370 else if (event.touchAction == TOUCH_ACTION_MOVE)
371 {
372 GESTURES.Pinch.distance = rgVector2Distance(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB);
374 GESTURES.Touch.moveDownPositionA = event.position[0];
375 GESTURES.Touch.moveDownPositionB = event.position[1];
377 GESTURES.Pinch.vector.x = GESTURES.Touch.moveDownPositionB.x - GESTURES.Touch.moveDownPositionA.x;
378 GESTURES.Pinch.vector.y = GESTURES.Touch.moveDownPositionB.y - GESTURES.Touch.moveDownPositionA.y;
380 if ((rgVector2Distance(GESTURES.Touch.previousPositionA, GESTURES.Touch.moveDownPositionA) >= MINIMUM_PINCH) || (rgVector2Distance(GESTURES.Touch.previousPositionB, GESTURES.Touch.moveDownPositionB) >= MINIMUM_PINCH))
381 {
382 if ( rgVector2Distance(GESTURES.Touch.previousPositionA, GESTURES.Touch.previousPositionB) > rgVector2Distance(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB) ) GESTURES.current = GESTURE_PINCH_IN;
383 else GESTURES.current = GESTURE_PINCH_OUT;
384 }
385 else
386 {
387 GESTURES.current = GESTURE_HOLD;
388 GESTURES.Hold.timeDuration = rgGetCurrentTime();
389 }
391 // NOTE: Angle should be inverted in Y
392 GESTURES.Pinch.angle = 360.0f - rgVector2Angle(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB);
393 }
394 else if (event.touchAction == TOUCH_ACTION_UP)
395 {
396 GESTURES.Pinch.distance = 0.0f;
397 GESTURES.Pinch.angle = 0.0f;
398 GESTURES.Pinch.vector = (Vector2){ 0.0f, 0.0f };
399 GESTURES.Touch.pointCount = 0;
401 GESTURES.current = GESTURE_NONE;
402 }
403 }
404 else if (GESTURES.Touch.pointCount > 2) // More than two touch points
405 {
406 // TODO: Process gesture events for more than two points
407 }
408}
410// Update gestures detected (must be called every frame)
411void UpdateGestures(void)
412{
413 // NOTE: Gestures are processed through system callbacks on touch events
415 // Detect GESTURE_HOLD
416 if (((GESTURES.current == GESTURE_TAP) || (GESTURES.current == GESTURE_DOUBLETAP)) && (GESTURES.Touch.pointCount < 2))
417 {
418 GESTURES.current = GESTURE_HOLD;
419 GESTURES.Hold.timeDuration = rgGetCurrentTime();
420 }
422 // Detect GESTURE_NONE
423 if ((GESTURES.current == GESTURE_SWIPE_RIGHT) || (GESTURES.current == GESTURE_SWIPE_UP) || (GESTURES.current == GESTURE_SWIPE_LEFT) || (GESTURES.current == GESTURE_SWIPE_DOWN))
424 {
425 GESTURES.current = GESTURE_NONE;
426 }
427}
429// Get latest detected gesture
430int GetGestureDetected(void)
431{
432 // Get current gesture only if enabled
433 return (GESTURES.enabledFlags & GESTURES.current);
434}
436// Hold time measured in seconds
437float GetGestureHoldDuration(void)
438{
439 // NOTE: time is calculated on current gesture HOLD
441 double time = 0.0;
443 if (GESTURES.current == GESTURE_HOLD) time = rgGetCurrentTime() - GESTURES.Hold.timeDuration;
445 return (float)time;
446}
448// Get drag vector (between initial touch point to current)
449Vector2 GetGestureDragVector(void)
450{
451 // NOTE: drag vector is calculated on one touch points TOUCH_ACTION_MOVE
453 return GESTURES.Drag.vector;
454}
456// Get drag angle
457// NOTE: Angle in degrees, horizontal-right is 0, counterclockwise
458float GetGestureDragAngle(void)
459{
460 // NOTE: drag angle is calculated on one touch points TOUCH_ACTION_UP
462 return GESTURES.Drag.angle;
463}
465// Get distance between two pinch points
466Vector2 GetGesturePinchVector(void)
467{
468 // NOTE: Pinch distance is calculated on two touch points TOUCH_ACTION_MOVE
470 return GESTURES.Pinch.vector;
471}
473// Get angle between two pinch points
474// NOTE: Angle in degrees, horizontal-right is 0, counterclockwise
475float GetGesturePinchAngle(void)
476{
477 // NOTE: pinch angle is calculated on two touch points TOUCH_ACTION_MOVE
479 return GESTURES.Pinch.angle;
480}
482//----------------------------------------------------------------------------------
483// Module Internal Functions Definition
484//----------------------------------------------------------------------------------
485// Get angle from two-points vector with X-axis
486static float rgVector2Angle(Vector2 v1, Vector2 v2)
487{
488 float angle = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/PI);
490 if (angle < 0) angle += 360.0f;
492 return angle;
493}
495// Calculate distance between two Vector2
496static float rgVector2Distance(Vector2 v1, Vector2 v2)
497{
498 float result;
500 float dx = v2.x - v1.x;
501 float dy = v2.y - v1.y;
503 result = (float)sqrt(dx*dx + dy*dy);
505 return result;
506}
508// Time measure returned are seconds
509static double rgGetCurrentTime(void)
510{
511 double time = 0;
513#if !defined(RGESTURES_STANDALONE)
514 time = GetTime();
515#else
516#if defined(_WIN32)
517 unsigned long long int clockFrequency, currentTime;
519 QueryPerformanceFrequency(&clockFrequency); // BE CAREFUL: Costly operation!
520 QueryPerformanceCounter(¤tTime);
522 time = (double)currentTime/clockFrequency; // Time in seconds
523#endif
525#if defined(__linux__)
526 // NOTE: Only for Linux-based systems
527 struct timespec now;
528 clock_gettime(CLOCK_MONOTONIC, &now);
529 unsigned long long int nowTime = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec; // Time in nanoseconds
531 time = ((double)nowTime*1e-9); // Time in seconds
532#endif
534#if defined(__APPLE__)
535 //#define CLOCK_REALTIME CALENDAR_CLOCK // returns UTC time since 1970-01-01
536 //#define CLOCK_MONOTONIC SYSTEM_CLOCK // returns the time since boot time
538 clock_serv_t cclock;
539 mach_timespec_t now;
540 host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
542 // NOTE: OS X does not have clock_gettime(), using clock_get_time()
543 clock_get_time(cclock, &now);
544 mach_port_deallocate(mach_task_self(), cclock);
545 unsigned long long int nowTime = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec; // Time in nanoseconds
547 time = ((double)nowTime*1e-9); // Time in seconds
548#endif
549#endif
551 return time;
552}
554#endif // RGESTURES_IMPLEMENTATION
index : raylib-jai
---