Reviewed gestures module
This commit is contained in:
parent
687fe2c3c7
commit
a847df921f
30
src/core.c
30
src/core.c
@ -117,7 +117,8 @@
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#define STORAGE_FILENAME "storage.data"
|
||||
#define STORAGE_FILENAME "storage.data"
|
||||
#define MAX_TOUCH_POINTS 2
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
@ -182,7 +183,7 @@ static bool fullscreen = false; // Fullscreen mode (useful only for
|
||||
static Matrix downscaleView; // Matrix to downscale view (in case screen size bigger than display size)
|
||||
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
||||
static Vector2 touchPosition; // Touch position on screen
|
||||
static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
|
||||
@ -1228,7 +1229,7 @@ bool IsGamepadButtonUp(int gamepad, int button)
|
||||
int GetTouchX(void)
|
||||
{
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
||||
return (int)touchPosition.x;
|
||||
return (int)touchPosition[0].x;
|
||||
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
||||
return GetMouseX();
|
||||
#endif
|
||||
@ -1238,7 +1239,7 @@ int GetTouchX(void)
|
||||
int GetTouchY(void)
|
||||
{
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
||||
return (int)touchPosition.y;
|
||||
return (int)touchPosition[0].y;
|
||||
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
||||
return GetMouseY();
|
||||
#endif
|
||||
@ -1246,10 +1247,13 @@ int GetTouchY(void)
|
||||
|
||||
// Returns touch position XY
|
||||
// TODO: touch position should be scaled depending on display size and render size
|
||||
Vector2 GetTouchPosition(void)
|
||||
Vector2 GetTouchPosition(int index)
|
||||
{
|
||||
Vector2 position = { -1.0f, -1.0f };
|
||||
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
||||
Vector2 position = touchPosition;
|
||||
if (index < MAX_TOUCH_POINTS) position = touchPosition[index];
|
||||
else TraceLog(WARNING, "Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS);
|
||||
|
||||
if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
|
||||
{
|
||||
@ -1263,7 +1267,7 @@ Vector2 GetTouchPosition(void)
|
||||
position.y = position.y*((float)renderHeight/(float)displayHeight) - renderOffsetY/2;
|
||||
}
|
||||
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
||||
Vector2 position = GetMousePosition();
|
||||
if (index == 0) position = GetMousePosition();
|
||||
#endif
|
||||
|
||||
return position;
|
||||
@ -1916,8 +1920,13 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
|
||||
|
||||
if (type == AINPUT_EVENT_TYPE_MOTION)
|
||||
{
|
||||
touchPosition.x = AMotionEvent_getX(event, 0);
|
||||
touchPosition.y = AMotionEvent_getY(event, 0);
|
||||
// Get first touch position
|
||||
touchPosition[0].x = AMotionEvent_getX(event, 0);
|
||||
touchPosition[0].y = AMotionEvent_getY(event, 0);
|
||||
|
||||
// Get second touch position
|
||||
touchPosition[1].x = AMotionEvent_getX(event, 1);
|
||||
touchPosition[1].y = AMotionEvent_getY(event, 1);
|
||||
}
|
||||
else if (type == AINPUT_EVENT_TYPE_KEY)
|
||||
{
|
||||
@ -2535,7 +2544,8 @@ static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent
|
||||
gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
|
||||
gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].targetY };
|
||||
|
||||
touchPosition = gestureEvent.position[0];
|
||||
touchPosition[0] = gestureEvent.position[0];
|
||||
touchPosition[1] = gestureEvent.position[1];
|
||||
|
||||
// Gesture data is sent to gestures system for processing
|
||||
ProcessGestureEvent(gestureEvent); // Process obtained gestures data
|
||||
|
112
src/gestures.c
112
src/gestures.c
@ -31,9 +31,7 @@
|
||||
#include "raylib.h" // Required for typedef(s): Vector2, Gestures
|
||||
#endif
|
||||
|
||||
#include <stdlib.h> // malloc(), free()
|
||||
#include <stdio.h> // printf(), fprintf()
|
||||
#include <math.h> // Used for ...
|
||||
#include <math.h> // Used for: atan2(), sqrt()
|
||||
#include <stdint.h> // Defines int32_t, int64_t
|
||||
|
||||
#if defined(_WIN32)
|
||||
@ -47,13 +45,12 @@
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#define FORCE_TO_SWIPE 20
|
||||
#define FORCE_TO_SWIPE 1
|
||||
#define FORCE_TO_DRAG 20
|
||||
#define FORCE_TO_PINCH 5
|
||||
#define TAP_TIMEOUT 300 // Time in milliseconds
|
||||
#define PINCH_TIMEOUT 300 // Time in milliseconds
|
||||
#define DOUBLETAP_RANGE 30
|
||||
//#define MAX_TOUCH_POINTS 4
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
@ -64,30 +61,33 @@
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Drag gesture variables
|
||||
static Vector2 dragVector = { 0.0f , 0.0f };
|
||||
|
||||
// Touch gesture variables
|
||||
static Vector2 touchDownPosition = { 0, 0 };
|
||||
static Vector2 touchDownPosition2 = { 0, 0 };
|
||||
static Vector2 touchUpPosition = { 0, 0 };
|
||||
static Vector2 moveDownPosition = { 0, 0 };
|
||||
static Vector2 moveDownPosition2 = { 0, 0 };
|
||||
static Vector2 touchDownPosition = { 0.0f, 0.0f };
|
||||
static Vector2 touchDownPosition2 = { 0.0f, 0.0f };
|
||||
static Vector2 touchDownDragPosition = { 0.0f, 0.0f };
|
||||
static Vector2 touchUpPosition = { 0.0f, 0.0f };
|
||||
static Vector2 moveDownPosition = { 0.0f, 0.0f };
|
||||
static Vector2 moveDownPosition2 = { 0.0f, 0.0f };
|
||||
|
||||
static int numTap = 0;
|
||||
static int numHold = 0;
|
||||
static bool isMoving = false;
|
||||
static float timeHold = 0.0f;
|
||||
static int pointCount = 0;
|
||||
static int touchId = -1;
|
||||
|
||||
static double eventTime = 0.0;
|
||||
static double swipeTime = 0.0;
|
||||
|
||||
// Drag gesture variables
|
||||
static Vector2 dragVector = { 0.0f , 0.0f };
|
||||
static float dragDistance = 0.0f; // DRAG distance (from initial touch point to current)
|
||||
static float dragAngle = 0; // DRAG angle direction
|
||||
static float dragIntensity = 0; // DRAG intensity, how far why did the DRAG (pixels per frame)
|
||||
static int draggingTimeCounter = 0; // DRAG time // RAY: WTF!!! Counting... frames???
|
||||
static float dragAngle = 0.0f; // DRAG angle direction
|
||||
static float dragIntensity = 0.0f; // DRAG intensity, how far why did the DRAG (pixels per frame)
|
||||
|
||||
// Pinch gesture variables
|
||||
static float pinchDistance = 0.0f; // Pinch displacement distance // RAY: Not used! o__O
|
||||
static float pinchAngle = 0.0f; // Pinch displacement distance // RAY: Not used! o__O
|
||||
|
||||
// Detected gestures
|
||||
static int previousGesture = GESTURE_NONE;
|
||||
@ -111,9 +111,6 @@ static double GetCurrentTime();
|
||||
void ProcessGestureEvent(GestureEvent event)
|
||||
{
|
||||
// Reset required variables
|
||||
dragVector = (Vector2){ 0.0f, 0.0f }; // RAY: Not used???
|
||||
pinchDistance = 0.0f;
|
||||
|
||||
previousGesture = currentGesture;
|
||||
|
||||
pointCount = event.pointCount; // Required on UpdateGestures()
|
||||
@ -139,9 +136,12 @@ void ProcessGestureEvent(GestureEvent event)
|
||||
}
|
||||
|
||||
touchDownPosition = event.position[0];
|
||||
touchDownDragPosition = event.position[0];
|
||||
|
||||
touchUpPosition = touchDownPosition;
|
||||
eventTime = GetCurrentTime();
|
||||
|
||||
dragVector = (Vector2){ 0.0f, 0.0f };
|
||||
}
|
||||
else if (event.touchAction == TOUCH_UP)
|
||||
{
|
||||
@ -149,12 +149,15 @@ void ProcessGestureEvent(GestureEvent event)
|
||||
|
||||
// Calculate for swipe
|
||||
dragDistance = Vector2Distance(touchDownPosition, touchUpPosition);
|
||||
dragIntensity = dragDistance/(float)draggingTimeCounter; // RAY: WTF!!! Counting frames???
|
||||
dragIntensity = dragDistance/(float)((GetCurrentTime() - swipeTime));
|
||||
|
||||
isMoving = false;
|
||||
|
||||
// Detect GESTURE_SWIPE
|
||||
if ((dragIntensity > FORCE_TO_SWIPE) && (touchId == 0)) // RAY: why check (touchId == 0)???
|
||||
{
|
||||
dragAngle = Vector2Angle(touchDownPosition, touchUpPosition);
|
||||
// NOTE: Angle should be inverted in Y
|
||||
dragAngle = 360.0f - Vector2Angle(touchDownPosition, touchUpPosition);;
|
||||
|
||||
if ((dragAngle < 30) || (dragAngle > 330)) currentGesture = GESTURE_SWIPE_RIGHT; // Right
|
||||
else if ((dragAngle > 30) && (dragAngle < 120)) currentGesture = GESTURE_SWIPE_UP; // Up
|
||||
@ -171,12 +174,18 @@ void ProcessGestureEvent(GestureEvent event)
|
||||
currentGesture = GESTURE_NONE;
|
||||
}
|
||||
|
||||
draggingTimeCounter = 0;
|
||||
touchDownPosition = (Vector2){ 0.0f, 0.0f };
|
||||
}
|
||||
else if (event.touchAction == TOUCH_MOVE)
|
||||
{
|
||||
if (Vector2Distance(moveDownPosition, event.position[0]) > 5) eventTime = GetCurrentTime();
|
||||
|
||||
if (!isMoving)
|
||||
{
|
||||
swipeTime = GetCurrentTime();
|
||||
isMoving = true;
|
||||
}
|
||||
|
||||
moveDownPosition = event.position[0];
|
||||
|
||||
if (currentGesture == GESTURE_HOLD)
|
||||
@ -190,8 +199,9 @@ void ProcessGestureEvent(GestureEvent event)
|
||||
// Detect GESTURE_DRAG
|
||||
if (dragDistance >= FORCE_TO_DRAG) currentGesture = GESTURE_DRAG;
|
||||
}
|
||||
|
||||
draggingTimeCounter++; // RAY: What do you count??? Move event actions?
|
||||
|
||||
dragVector.x = moveDownPosition.x - touchDownDragPosition.x;
|
||||
dragVector.y = moveDownPosition.y - touchDownDragPosition.y;
|
||||
}
|
||||
}
|
||||
else // Two touch points
|
||||
@ -200,13 +210,15 @@ void ProcessGestureEvent(GestureEvent event)
|
||||
{
|
||||
touchDownPosition = event.position[0];
|
||||
touchDownPosition2 = event.position[1];
|
||||
pinchDistance = Vector2Distance(touchDownPosition, touchDownPosition2);
|
||||
|
||||
currentGesture = GESTURE_HOLD;
|
||||
timeHold = GetCurrentTime();
|
||||
}
|
||||
else if (event.touchAction == TOUCH_MOVE)
|
||||
{
|
||||
pinchDistance = Vector2Distance(moveDownPosition, moveDownPosition2);
|
||||
|
||||
|
||||
touchDownPosition = moveDownPosition;
|
||||
touchDownPosition2 = moveDownPosition2;
|
||||
|
||||
@ -220,12 +232,17 @@ void ProcessGestureEvent(GestureEvent event)
|
||||
}
|
||||
else
|
||||
{
|
||||
currentGesture = GESTURE_HOLD;
|
||||
currentGesture = GESTURE_HOLD;
|
||||
timeHold = GetCurrentTime();
|
||||
}
|
||||
|
||||
// NOTE: Angle should be inverted in Y
|
||||
pinchAngle = 360.0f - Vector2Angle(moveDownPosition, moveDownPosition2);
|
||||
}
|
||||
else if (event.touchAction == TOUCH_UP)
|
||||
{
|
||||
pinchDistance = 0.0f;
|
||||
pinchAngle = 0.0f;
|
||||
|
||||
currentGesture = GESTURE_NONE;
|
||||
}
|
||||
@ -238,11 +255,16 @@ void UpdateGestures(void)
|
||||
// NOTE: Gestures are processed through system callbacks on touch events
|
||||
|
||||
// Detect GESTURE_HOLD
|
||||
if (((currentGesture == GESTURE_TAP) || (currentGesture == GESTURE_DOUBLETAP)) && pointCount < 2) currentGesture = GESTURE_HOLD;
|
||||
|
||||
if ((GetCurrentTime() - eventTime) > TAP_TIMEOUT && (currentGesture == GESTURE_DRAG) && pointCount < 2)
|
||||
if (((currentGesture == GESTURE_TAP) || (currentGesture == GESTURE_DOUBLETAP)) && (pointCount < 2))
|
||||
{
|
||||
currentGesture = GESTURE_HOLD;
|
||||
timeHold = GetCurrentTime();
|
||||
}
|
||||
|
||||
if (((GetCurrentTime() - eventTime) > TAP_TIMEOUT) && (currentGesture == GESTURE_DRAG) && (pointCount < 2))
|
||||
{
|
||||
currentGesture = GESTURE_HOLD;
|
||||
timeHold = GetCurrentTime();
|
||||
numHold = 1;
|
||||
}
|
||||
|
||||
@ -288,17 +310,19 @@ float GetGestureDragAngle(void)
|
||||
// Get drag vector (between initial and final position)
|
||||
Vector2 GetGestureDragVector(void)
|
||||
{
|
||||
// TODO: Calculate DRAG vector
|
||||
|
||||
// NOTE: Calculated in...
|
||||
return dragVector;
|
||||
}
|
||||
|
||||
// Hold time measured in ms
|
||||
int GetGestureHoldDuration(void)
|
||||
float GetGestureHoldDuration(void)
|
||||
{
|
||||
// TODO: Return last hold time in ms
|
||||
float time = 0.0f;
|
||||
|
||||
return 0;
|
||||
// DONE: Return last hold time in ms
|
||||
if (currentGesture == GESTURE_HOLD) time = (float)GetCurrentTime() - timeHold;
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
// Get distance between two pinch points
|
||||
@ -307,32 +331,34 @@ float GetGesturePinchDelta(void)
|
||||
return pinchDistance;
|
||||
}
|
||||
|
||||
// Get number of touch points
|
||||
int GetTouchPointsCount(void)
|
||||
{
|
||||
return pointCount;
|
||||
}
|
||||
|
||||
// Get angle beween two pinch points
|
||||
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
|
||||
float GetGesturePinchAngle(void)
|
||||
{
|
||||
// TODO: Calculate pinch angle
|
||||
// NOTE: pinch angle is calculated on two touch points TOUCH_MOVE
|
||||
|
||||
return 0;
|
||||
return pinchAngle;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// RAY: Do we really need magnitude??? why???
|
||||
// TODO: Remove magnitude dependency...
|
||||
// Returns angle from two-points vector with X-axis
|
||||
static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition)
|
||||
{
|
||||
float angle;
|
||||
|
||||
// RAY: review this (better) solution
|
||||
|
||||
angle = atan2(finalPosition.y - initialPosition.y, finalPosition.x - initialPosition.x);
|
||||
angle *= RAD2DEG;
|
||||
|
||||
if (angle < 0) angle += 360;
|
||||
|
||||
// http://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors
|
||||
if (angle < 0) angle += 360.0f;
|
||||
|
||||
return angle;
|
||||
}
|
||||
|
@ -102,6 +102,7 @@ Vector2 GetGestureDragVector(void); // Get gesture drag vect
|
||||
int GetGestureHoldDuration(void); // Get gesture hold time in frames
|
||||
float GetGesturePinchDelta(void); // Get gesture pinch delta
|
||||
float GetGesturePinchAngle(void); // Get gesture pinch angle
|
||||
int GetTouchPointsCount(void); // Get touch points count
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -600,9 +600,9 @@ bool IsGamepadButtonReleased(int gamepad, int button); // Detect if a gamepad b
|
||||
bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad button is NOT being pressed
|
||||
#endif
|
||||
|
||||
int GetTouchX(void); // Returns touch position X (relative to screen size)
|
||||
int GetTouchY(void); // Returns touch position Y (relative to screen size)
|
||||
Vector2 GetTouchPosition(void); // Returns touch position XY (relative to screen size)
|
||||
int GetTouchX(void); // Returns touch position X for touch point 0 (relative to screen size)
|
||||
int GetTouchY(void); // Returns touch position Y for touch point 0 (relative to screen size)
|
||||
Vector2 GetTouchPosition(int index) // Returns touch position XY for a touch point index (relative to screen size)
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
bool IsButtonPressed(int button); // Detect if an android physic button has been pressed
|
||||
@ -622,9 +622,10 @@ void ProcessGestureEvent(GestureEvent event); // Process gesture event
|
||||
float GetGestureDragIntensity(void); // Get gesture drag intensity
|
||||
float GetGestureDragAngle(void); // Get gesture drag angle
|
||||
Vector2 GetGestureDragVector(void); // Get gesture drag vector
|
||||
int GetGestureHoldDuration(void); // Get gesture hold time in frames
|
||||
float GetGestureHoldDuration(void); // Get gesture hold time in ms
|
||||
float GetGesturePinchDelta(void); // Get gesture pinch delta
|
||||
float GetGesturePinchAngle(void); // Get gesture pinch angle
|
||||
int GetTouchPointsCount(void); // Get touch points count
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Camera System Functions (Module: camera)
|
||||
|
Loading…
Reference in New Issue
Block a user