Updated gestures module

Using normalized [0..1] input points
This commit is contained in:
Ray 2016-02-17 13:00:48 +01:00
parent 825e42dc00
commit afd2ffb74a
4 changed files with 118 additions and 85 deletions

View file

@ -1700,15 +1700,23 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
// Register touch actions // Register touch actions
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_DOWN; if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_DOWN;
//else if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_MOVE;
else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_UP; else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_UP;
// NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback()
// Assign a pointer ID
gestureEvent.pointerId[0] = 0;
// Register touch points count // Register touch points count
gestureEvent.pointCount = 1; gestureEvent.pointCount = 1;
// Register touch points position, only one point registered // Register touch points position, only one point registered
gestureEvent.position[0] = GetMousePosition(); gestureEvent.position[0] = GetMousePosition();
// Normalize gestureEvent.position[0] for screenWidth and screenHeight
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures system for processing // Gesture data is sent to gestures system for processing
ProcessGestureEvent(gestureEvent); ProcessGestureEvent(gestureEvent);
#endif #endif
@ -1730,6 +1738,10 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
// Register touch points position, only one point registered // Register touch points position, only one point registered
gestureEvent.position[0] = (Vector2){ (float)x, (float)y }; gestureEvent.position[0] = (Vector2){ (float)x, (float)y };
// Normalize gestureEvent.position[0] for screenWidth and screenHeight
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures system for processing // Gesture data is sent to gestures system for processing
ProcessGestureEvent(gestureEvent); ProcessGestureEvent(gestureEvent);
#endif #endif
@ -1992,6 +2004,13 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
gestureEvent.position[0] = (Vector2){ AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0) }; gestureEvent.position[0] = (Vector2){ AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0) };
gestureEvent.position[1] = (Vector2){ AMotionEvent_getX(event, 1), AMotionEvent_getY(event, 1) }; gestureEvent.position[1] = (Vector2){ AMotionEvent_getX(event, 1), AMotionEvent_getY(event, 1) };
// Normalize gestureEvent.position[x] for screenWidth and screenHeight
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
gestureEvent.position[1].x /= (float)GetScreenWidth();
gestureEvent.position[1].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures system for processing // Gesture data is sent to gestures system for processing
ProcessGestureEvent(gestureEvent); ProcessGestureEvent(gestureEvent);
@ -2564,6 +2583,13 @@ static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent
touchPosition[0] = gestureEvent.position[0]; touchPosition[0] = gestureEvent.position[0];
touchPosition[1] = gestureEvent.position[1]; touchPosition[1] = gestureEvent.position[1];
// Normalize gestureEvent.position[x] for screenWidth and screenHeight
gestureEvent.position[0].x /= (float)GetScreenWidth();
gestureEvent.position[0].y /= (float)GetScreenHeight();
gestureEvent.position[1].x /= (float)GetScreenWidth();
gestureEvent.position[1].y /= (float)GetScreenHeight();
// Gesture data is sent to gestures system for processing // Gesture data is sent to gestures system for processing
ProcessGestureEvent(gestureEvent); // Process obtained gestures data ProcessGestureEvent(gestureEvent); // Process obtained gestures data

View file

@ -45,12 +45,12 @@
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#define FORCE_TO_SWIPE 1 // Time in milliseconds #define FORCE_TO_SWIPE 0.0005f // Measured in normalized pixels / time
#define FORCE_TO_DRAG 20 #define MINIMUM_DRAG 0.015f // Measured in normalized pixels [0..1]
#define FORCE_TO_PINCH 5 #define MINIMUM_PINCH 0.005f // Measured in normalized pixels [0..1]
#define TAP_TIMEOUT 300 // Time in milliseconds #define TAP_TIMEOUT 300 // Time in milliseconds
#define PINCH_TIMEOUT 300 // Time in milliseconds #define PINCH_TIMEOUT 300 // Time in milliseconds
#define DOUBLETAP_RANGE 30 #define DOUBLETAP_RANGE 0.03f
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
@ -68,26 +68,29 @@ static Vector2 touchDownDragPosition = { 0.0f, 0.0f };
static Vector2 touchUpPosition = { 0.0f, 0.0f }; static Vector2 touchUpPosition = { 0.0f, 0.0f };
static Vector2 moveDownPosition = { 0.0f, 0.0f }; static Vector2 moveDownPosition = { 0.0f, 0.0f };
static Vector2 moveDownPosition2 = { 0.0f, 0.0f }; static Vector2 moveDownPosition2 = { 0.0f, 0.0f };
static int numTap = 0; static int numTap = 0;
static int numHold = 0;
static bool isMoving = false;
static float timeHold = 0.0f;
static int pointCount = 0; static int pointCount = 0;
static int touchId = -1; static int touchId = -1;
static double eventTime = 0.0; static double eventTime = 0.0;
static double swipeTime = 0.0; static double swipeTime = 0.0;
// Hold gesture variables
static int numHold = 0;
static float timeHold = 0.0f;
// Drag gesture variables // Drag gesture variables
static Vector2 dragVector = { 0.0f , 0.0f }; // DRAG vector (between initial and current position) static Vector2 dragVector = { 0.0f , 0.0f }; // DRAG vector (between initial and current position)
static float dragDistance = 0.0f; // DRAG distance (from initial touch point to final) for SWIPE GESTURE static float dragAngle = 0.0f; // DRAG angle (relative to x-axis)
static float dragAngle = 0.0f; // DRAG angle direction for SWIPE GESTURE static float dragDistance = 0.0f; // DRAG distance (from initial touch point to final) (normalized [0..1])
static float dragIntensity = 0.0f; // DRAG intensity, how far why did the DRAG (pixels per frame) for SWIPE GESTURE static float dragIntensity = 0.0f; // DRAG intensity, how far why did the DRAG (pixels per frame)
static bool startMoving = false; // SWIPE used to define when start measuring swipeTime
// Pinch gesture variables // Pinch gesture variables
static float pinchDistance = 0.0f; // Pinch displacement distance static Vector2 pinchVector = { 0.0f , 0.0f }; // PINCH vector (between first and second touch points)
static float pinchAngle = 0.0f; // Pinch displacement distance static float pinchAngle = 0.0f; // PINCH angle (relative to x-axis)
static float pinchDistance = 0.0f; // PINCH displacement distance (normalized [0..1])
// Detected gestures // Detected gestures
static int previousGesture = GESTURE_NONE; static int previousGesture = GESTURE_NONE;
@ -101,7 +104,7 @@ static unsigned int enabledGestures = 0b0000001111111111;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition); static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition);
static float Vector2Distance(Vector2 v1, Vector2 v2); static float Vector2Distance(Vector2 v1, Vector2 v2);
static double GetCurrentTime(); static double GetCurrentTime(void);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Definition // Module Functions Definition
@ -147,17 +150,19 @@ void ProcessGestureEvent(GestureEvent event)
{ {
if (currentGesture == GESTURE_DRAG) touchUpPosition = event.position[0]; if (currentGesture == GESTURE_DRAG) touchUpPosition = event.position[0];
// Calculate for swipe // NOTE: dragIntensity dependend on the resolution of the screen
dragDistance = Vector2Distance(touchDownPosition, touchUpPosition); dragDistance = Vector2Distance(touchDownPosition, touchUpPosition);
dragIntensity = dragDistance/(float)((GetCurrentTime() - swipeTime)); dragIntensity = dragDistance/(float)((GetCurrentTime() - swipeTime));
isMoving = false; // TODO: Make getures detection resolution independant
startMoving = false;
// Detect GESTURE_SWIPE // Detect GESTURE_SWIPE
if ((dragIntensity > FORCE_TO_SWIPE) && (touchId == 0)) // RAY: why check (touchId == 0)??? if ((dragIntensity > FORCE_TO_SWIPE) && (touchId == 0)) // RAY: why check (touchId == 0)???
{ {
// NOTE: Angle should be inverted in Y // NOTE: Angle should be inverted in Y
dragAngle = 360.0f - Vector2Angle(touchDownPosition, touchUpPosition);; dragAngle = 360.0f - Vector2Angle(touchDownPosition, touchUpPosition);
if ((dragAngle < 30) || (dragAngle > 330)) currentGesture = GESTURE_SWIPE_RIGHT; // Right if ((dragAngle < 30) || (dragAngle > 330)) currentGesture = GESTURE_SWIPE_RIGHT; // Right
else if ((dragAngle > 30) && (dragAngle < 120)) currentGesture = GESTURE_SWIPE_UP; // Up else if ((dragAngle > 30) && (dragAngle < 120)) currentGesture = GESTURE_SWIPE_UP; // Up
@ -178,12 +183,12 @@ void ProcessGestureEvent(GestureEvent event)
} }
else if (event.touchAction == TOUCH_MOVE) else if (event.touchAction == TOUCH_MOVE)
{ {
if (Vector2Distance(moveDownPosition, event.position[0]) > 5) eventTime = GetCurrentTime(); if ((currentGesture == GESTURE_DRAG)) eventTime = GetCurrentTime();
if (!isMoving) if (!startMoving)
{ {
swipeTime = GetCurrentTime(); swipeTime = GetCurrentTime();
isMoving = true; startMoving = true;
} }
moveDownPosition = event.position[0]; moveDownPosition = event.position[0];
@ -194,10 +199,12 @@ void ProcessGestureEvent(GestureEvent event)
numHold = 2; numHold = 2;
dragDistance = Vector2Distance(touchDownPosition, moveDownPosition);
// Detect GESTURE_DRAG // Detect GESTURE_DRAG
if (dragDistance >= FORCE_TO_DRAG) currentGesture = GESTURE_DRAG; if (Vector2Distance(touchDownPosition, moveDownPosition) >= MINIMUM_DRAG)
{
eventTime = GetCurrentTime();
currentGesture = GESTURE_DRAG;
}
} }
dragVector.x = moveDownPosition.x - touchDownDragPosition.x; dragVector.x = moveDownPosition.x - touchDownDragPosition.x;
@ -210,7 +217,11 @@ void ProcessGestureEvent(GestureEvent event)
{ {
touchDownPosition = event.position[0]; touchDownPosition = event.position[0];
touchDownPosition2 = event.position[1]; touchDownPosition2 = event.position[1];
pinchDistance = Vector2Distance(touchDownPosition, touchDownPosition2);
//pinchDistance = Vector2Distance(touchDownPosition, touchDownPosition2);
pinchVector.x = touchDownPosition2.x - touchDownPosition.x;
pinchVector.y = touchDownPosition2.y - touchDownPosition.y;
currentGesture = GESTURE_HOLD; currentGesture = GESTURE_HOLD;
timeHold = GetCurrentTime(); timeHold = GetCurrentTime();
@ -225,7 +236,10 @@ void ProcessGestureEvent(GestureEvent event)
moveDownPosition = event.position[0]; moveDownPosition = event.position[0];
moveDownPosition2 = event.position[1]; moveDownPosition2 = event.position[1];
if ((Vector2Distance(touchDownPosition, moveDownPosition) > FORCE_TO_PINCH) || (Vector2Distance(touchDownPosition2, moveDownPosition2) > FORCE_TO_PINCH)) pinchVector.x = moveDownPosition2.x - moveDownPosition.x;
pinchVector.y = moveDownPosition2.y - moveDownPosition.y;
if ((Vector2Distance(touchDownPosition, moveDownPosition) >= MINIMUM_PINCH) || (Vector2Distance(touchDownPosition2, moveDownPosition2) >= MINIMUM_PINCH))
{ {
if ((Vector2Distance(moveDownPosition, moveDownPosition2) - pinchDistance) < 0) currentGesture = GESTURE_PINCH_IN; if ((Vector2Distance(moveDownPosition, moveDownPosition2) - pinchDistance) < 0) currentGesture = GESTURE_PINCH_IN;
else currentGesture = GESTURE_PINCH_OUT; else currentGesture = GESTURE_PINCH_OUT;
@ -243,6 +257,7 @@ void ProcessGestureEvent(GestureEvent event)
{ {
pinchDistance = 0.0f; pinchDistance = 0.0f;
pinchAngle = 0.0f; pinchAngle = 0.0f;
pinchVector = (Vector2){ 0.0f, 0.0f };
currentGesture = GESTURE_NONE; currentGesture = GESTURE_NONE;
} }
@ -289,36 +304,20 @@ int GetGestureType(void)
return (enabledGestures & currentGesture); return (enabledGestures & currentGesture);
} }
// Get number of touch points
int GetTouchPointsCount(void)
{
// NOTE: point count is calculated when ProcessGestureEvent(GestureEvent event) is called
return pointCount;
}
// Enable only desired getures to be detected
void SetGesturesEnabled(unsigned int gestureFlags) void SetGesturesEnabled(unsigned int gestureFlags)
{ {
enabledGestures = gestureFlags; enabledGestures = gestureFlags;
} }
// Get drag dragIntensity (pixels per frame)
float GetGestureDragdragIntensity(void)
{
// NOTE: drag intensity is calculated on one touch points TOUCH_UP
return dragIntensity;
}
// Get drag angle
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
float GetGestureDragAngle(void)
{
// NOTE: drag angle is calculated on one touch points TOUCH_UP
return dragAngle;
}
// Get drag vector (between initial touch point to current)
Vector2 GetGestureDragVector(void)
{
// NOTE: drag vector is calculated on one touch points TOUCH_MOVE
return dragVector;
}
// Hold time measured in ms // Hold time measured in ms
float GetGestureHoldDuration(void) float GetGestureHoldDuration(void)
{ {
@ -331,21 +330,30 @@ float GetGestureHoldDuration(void)
return time; return time;
} }
// Get drag vector (between initial touch point to current)
Vector2 GetGestureDragVector(void)
{
// NOTE: drag vector is calculated on one touch points TOUCH_MOVE
return dragVector;
}
// Get drag angle
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
float GetGestureDragAngle(void)
{
// NOTE: drag angle is calculated on one touch points TOUCH_UP
return dragAngle;
}
// Get distance between two pinch points // Get distance between two pinch points
float GetGesturePinchDelta(void) Vector2 GetGesturePinchVector(void)
{ {
// NOTE: The position values used for pinchDistance are not modified like the position values of [core.c]-->GetTouchPosition(int index) // NOTE: The position values used for pinchDistance are not modified like the position values of [core.c]-->GetTouchPosition(int index)
// NOTE: pinch distance is calculated on two touch points TOUCH_MOVE // NOTE: pinch distance is calculated on two touch points TOUCH_MOVE
return pinchDistance; return pinchVector;
}
// Get number of touch points
int GetTouchPointsCount(void)
{
// NOTE: point count is calculated when ProcessGestureEvent(GestureEvent event) is called
return pointCount;
} }
// Get angle beween two pinch points // Get angle beween two pinch points
@ -388,7 +396,7 @@ static float Vector2Distance(Vector2 v1, Vector2 v2)
} }
// Time measure returned are milliseconds // Time measure returned are milliseconds
static double GetCurrentTime() static double GetCurrentTime(void)
{ {
double time = 0; double time = 0;

View file

@ -90,20 +90,20 @@ extern "C" { // Prevents name mangling of functions
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Declaration // Module Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
void UpdateGestures(void); // Update gestures detected (must be called every frame) void UpdateGestures(void); // Update gestures detected (must be called every frame)
bool IsGestureDetected(void); // Check if a gesture have been detected bool IsGestureDetected(void); // Check if a gesture have been detected
int GetGestureType(void); // Get latest detected gesture int GetGestureType(void); // Get latest detected gesture
void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags
void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
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 GetGesturePinchDelta(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle
int GetTouchPointsCount(void); // Get touch points count int GetTouchPointsCount(void); // Get touch points count
float GetGestureHoldDuration(void); // Get gesture hold time in milliseconds
Vector2 GetGestureDragVector(void); // Get gesture drag vector
float GetGestureDragAngle(void); // Get gesture drag angle
Vector2 GetGesturePinchVector(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -607,20 +607,19 @@ bool IsButtonReleased(int button); // Detect if an android
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Gestures and Touch Handling Functions (Module: gestures) // Gestures and Touch Handling Functions (Module: gestures)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
void UpdateGestures(void); // Update gestures detected (must be called every frame) void UpdateGestures(void); // Update gestures detected (must be called every frame)
bool IsGestureDetected(void); // Check if a gesture have been detected bool IsGestureDetected(void); // Check if a gesture have been detected
int GetGestureType(void); // Get latest detected gesture int GetGestureType(void); // Get latest detected gesture
void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags
void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
float GetGestureDragIntensity(void); // Get gesture drag intensity
float GetGestureDragAngle(void); // Get gesture drag angle
Vector2 GetGestureDragVector(void); // Get gesture drag vector
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 int GetTouchPointsCount(void); // Get touch points count
float GetGestureHoldDuration(void); // Get gesture hold time in milliseconds
Vector2 GetGestureDragVector(void); // Get gesture drag vector
float GetGestureDragAngle(void); // Get gesture drag angle
Vector2 GetGesturePinchVector(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Camera System Functions (Module: camera) // Camera System Functions (Module: camera)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------