From 295e8c2a2faa7b0c80a840695e37f3ed1740eb88 Mon Sep 17 00:00:00 2001 From: ubkp <118854183+ubkp@users.noreply.github.com> Date: Sat, 22 Jul 2023 15:50:04 -0300 Subject: [PATCH] Optimize and simplify the gesture system (#3190) * Optimize and simplify the gesture system * Decouples GESTURE_SWIPE_* from GESTURE_DRAG --- src/rgestures.h | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/src/rgestures.h b/src/rgestures.h index d8bb3b1d..779dbed5 100644 --- a/src/rgestures.h +++ b/src/rgestures.h @@ -180,7 +180,7 @@ float GetGesturePinchAngle(void); // Get gesture pinch ang //---------------------------------------------------------------------------------- #define FORCE_TO_SWIPE 0.2f // Swipe force, measured in normalized screen units/time #define MINIMUM_DRAG 0.015f // Drag minimum force, measured in normalized screen units (0.0f to 1.0f) -#define DRAG_TIMEOUT 0.2f // Drag minimum time for web, measured in seconds +#define DRAG_TIMEOUT 0.3f // Drag minimum time for web, measured in seconds #define MINIMUM_PINCH 0.005f // Pinch minimum force, measured in normalized screen units (0.0f to 1.0f) #define TAP_TIMEOUT 0.3f // Tap minimum time, measured in seconds #define PINCH_TIMEOUT 0.3f // Pinch minimum time, measured in seconds @@ -219,8 +219,7 @@ typedef struct { float intensity; // DRAG intensity, how far why did the DRAG (pixels per frame) } Drag; struct { - bool start; // SWIPE used to define when start measuring GESTURES.Swipe.timeDuration - double timeDuration; // SWIPE time to calculate drag intensity + double startTime; // SWIPE start time to calculate drag intensity } Swipe; struct { Vector2 vector; // PINCH vector (between first and second touch points) @@ -292,7 +291,7 @@ void ProcessGestureEvent(GestureEvent event) GESTURES.Touch.upPosition = GESTURES.Touch.downPositionA; GESTURES.Touch.eventTime = rgGetCurrentTime(); - GESTURES.Touch.firstId = event.pointId[0]; + GESTURES.Swipe.startTime = rgGetCurrentTime(); GESTURES.Drag.vector = (Vector2){ 0.0f, 0.0f }; } @@ -303,12 +302,10 @@ void ProcessGestureEvent(GestureEvent event) // NOTE: GESTURES.Drag.intensity dependent on the resolution of the screen GESTURES.Drag.distance = rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition); - GESTURES.Drag.intensity = GESTURES.Drag.distance/(float)((rgGetCurrentTime() - GESTURES.Swipe.timeDuration)); - - GESTURES.Swipe.start = false; + GESTURES.Drag.intensity = GESTURES.Drag.distance/(float)((rgGetCurrentTime() - GESTURES.Swipe.startTime)); // Detect GESTURE_SWIPE - if ((GESTURES.Drag.intensity > FORCE_TO_SWIPE) && (GESTURES.Touch.firstId == event.pointId[0])) + if ((GESTURES.Drag.intensity > FORCE_TO_SWIPE) && (GESTURES.current != GESTURE_DRAG)) { // NOTE: Angle should be inverted in Y GESTURES.Drag.angle = 360.0f - rgVector2Angle(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition); @@ -333,14 +330,6 @@ void ProcessGestureEvent(GestureEvent event) } else if (event.touchAction == TOUCH_ACTION_MOVE) { - if (GESTURES.current == GESTURE_DRAG) GESTURES.Touch.eventTime = rgGetCurrentTime(); - - if (!GESTURES.Swipe.start) - { - GESTURES.Swipe.timeDuration = rgGetCurrentTime(); - GESTURES.Swipe.start = true; - } - GESTURES.Touch.moveDownPositionA = event.position[0]; if (GESTURES.current == GESTURE_HOLD) @@ -350,12 +339,7 @@ void ProcessGestureEvent(GestureEvent event) GESTURES.Hold.resetRequired = false; // Detect GESTURE_DRAG -#if defined(PLATFORM_WEB) - // An alternative check to detect gesture drag is necessary since moveDownPositionA on touch for web is always zero, causing the distance calculation to be inaccurate if ((rgGetCurrentTime() - GESTURES.Touch.eventTime) > DRAG_TIMEOUT) -#else - if (rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.moveDownPositionA) >= MINIMUM_DRAG) -#endif { GESTURES.Touch.eventTime = rgGetCurrentTime(); GESTURES.current = GESTURE_DRAG; @@ -436,13 +420,6 @@ void UpdateGestures(void) GESTURES.Hold.timeDuration = rgGetCurrentTime(); } - if (((rgGetCurrentTime() - GESTURES.Touch.eventTime) > TAP_TIMEOUT) && (GESTURES.current == GESTURE_DRAG) && (GESTURES.Touch.pointCount < 2)) - { - GESTURES.current = GESTURE_HOLD; - GESTURES.Hold.timeDuration = rgGetCurrentTime(); - GESTURES.Hold.resetRequired = true; - } - // Detect GESTURE_NONE if ((GESTURES.current == GESTURE_SWIPE_RIGHT) || (GESTURES.current == GESTURE_SWIPE_UP) || (GESTURES.current == GESTURE_SWIPE_LEFT) || (GESTURES.current == GESTURE_SWIPE_DOWN)) {