Skip to content

Commit

Permalink
Optimize and simplify the gesture system (#3190)
Browse files Browse the repository at this point in the history
* Optimize and simplify the gesture system

* Decouples GESTURE_SWIPE_* from GESTURE_DRAG
  • Loading branch information
ubkp authored Jul 22, 2023
1 parent ad2338b commit 295e8c2
Showing 1 changed file with 5 additions and 28 deletions.
33 changes: 5 additions & 28 deletions src/rgestures.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 };
}
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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))
{
Expand Down

0 comments on commit 295e8c2

Please sign in to comment.