Reviewed gestures module

This commit is contained in:
Ray 2016-02-10 10:31:06 +01:00
parent 687fe2c3c7
commit a847df921f
4 changed files with 95 additions and 57 deletions

View file

@ -117,7 +117,8 @@
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#define STORAGE_FILENAME "storage.data" #define STORAGE_FILENAME "storage.data"
#define MAX_TOUCH_POINTS 2
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // 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) static Matrix downscaleView; // Matrix to downscale view (in case screen size bigger than display size)
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) #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 #endif
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB) #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
@ -1228,7 +1229,7 @@ bool IsGamepadButtonUp(int gamepad, int button)
int GetTouchX(void) int GetTouchX(void)
{ {
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) #if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
return (int)touchPosition.x; return (int)touchPosition[0].x;
#else // PLATFORM_DESKTOP, PLATFORM_RPI #else // PLATFORM_DESKTOP, PLATFORM_RPI
return GetMouseX(); return GetMouseX();
#endif #endif
@ -1238,7 +1239,7 @@ int GetTouchX(void)
int GetTouchY(void) int GetTouchY(void)
{ {
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) #if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
return (int)touchPosition.y; return (int)touchPosition[0].y;
#else // PLATFORM_DESKTOP, PLATFORM_RPI #else // PLATFORM_DESKTOP, PLATFORM_RPI
return GetMouseY(); return GetMouseY();
#endif #endif
@ -1246,10 +1247,13 @@ int GetTouchY(void)
// Returns touch position XY // Returns touch position XY
// TODO: touch position should be scaled depending on display size and render size // 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) #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)) if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
{ {
@ -1263,7 +1267,7 @@ Vector2 GetTouchPosition(void)
position.y = position.y*((float)renderHeight/(float)displayHeight) - renderOffsetY/2; position.y = position.y*((float)renderHeight/(float)displayHeight) - renderOffsetY/2;
} }
#else // PLATFORM_DESKTOP, PLATFORM_RPI #else // PLATFORM_DESKTOP, PLATFORM_RPI
Vector2 position = GetMousePosition(); if (index == 0) position = GetMousePosition();
#endif #endif
return position; return position;
@ -1916,8 +1920,13 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
if (type == AINPUT_EVENT_TYPE_MOTION) if (type == AINPUT_EVENT_TYPE_MOTION)
{ {
touchPosition.x = AMotionEvent_getX(event, 0); // Get first touch position
touchPosition.y = AMotionEvent_getY(event, 0); 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) 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[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].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 // Gesture data is sent to gestures system for processing
ProcessGestureEvent(gestureEvent); // Process obtained gestures data ProcessGestureEvent(gestureEvent); // Process obtained gestures data

View file

@ -31,9 +31,7 @@
#include "raylib.h" // Required for typedef(s): Vector2, Gestures #include "raylib.h" // Required for typedef(s): Vector2, Gestures
#endif #endif
#include <stdlib.h> // malloc(), free() #include <math.h> // Used for: atan2(), sqrt()
#include <stdio.h> // printf(), fprintf()
#include <math.h> // Used for ...
#include <stdint.h> // Defines int32_t, int64_t #include <stdint.h> // Defines int32_t, int64_t
#if defined(_WIN32) #if defined(_WIN32)
@ -47,13 +45,12 @@
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Defines and Macros // Defines and Macros
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
#define FORCE_TO_SWIPE 20 #define FORCE_TO_SWIPE 1
#define FORCE_TO_DRAG 20 #define FORCE_TO_DRAG 20
#define FORCE_TO_PINCH 5 #define FORCE_TO_PINCH 5
#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 30
//#define MAX_TOUCH_POINTS 4
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
@ -64,30 +61,33 @@
// Global Variables Definition // Global Variables Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Drag gesture variables
static Vector2 dragVector = { 0.0f , 0.0f };
// Touch gesture variables // Touch gesture variables
static Vector2 touchDownPosition = { 0, 0 }; static Vector2 touchDownPosition = { 0.0f, 0.0f };
static Vector2 touchDownPosition2 = { 0, 0 }; static Vector2 touchDownPosition2 = { 0.0f, 0.0f };
static Vector2 touchUpPosition = { 0, 0 }; static Vector2 touchDownDragPosition = { 0.0f, 0.0f };
static Vector2 moveDownPosition = { 0, 0 }; static Vector2 touchUpPosition = { 0.0f, 0.0f };
static Vector2 moveDownPosition2 = { 0, 0 }; static Vector2 moveDownPosition = { 0.0f, 0.0f };
static Vector2 moveDownPosition2 = { 0.0f, 0.0f };
static int numTap = 0; static int numTap = 0;
static int numHold = 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;
// 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 dragDistance = 0.0f; // DRAG distance (from initial touch point to current)
static float dragAngle = 0; // DRAG angle direction static float dragAngle = 0.0f; // DRAG angle direction
static float dragIntensity = 0; // DRAG intensity, how far why did the DRAG (pixels per frame) static float dragIntensity = 0.0f; // DRAG intensity, how far why did the DRAG (pixels per frame)
static int draggingTimeCounter = 0; // DRAG time // RAY: WTF!!! Counting... frames???
// Pinch gesture variables // Pinch gesture variables
static float pinchDistance = 0.0f; // Pinch displacement distance // RAY: Not used! o__O 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 // Detected gestures
static int previousGesture = GESTURE_NONE; static int previousGesture = GESTURE_NONE;
@ -111,9 +111,6 @@ static double GetCurrentTime();
void ProcessGestureEvent(GestureEvent event) void ProcessGestureEvent(GestureEvent event)
{ {
// Reset required variables // Reset required variables
dragVector = (Vector2){ 0.0f, 0.0f }; // RAY: Not used???
pinchDistance = 0.0f;
previousGesture = currentGesture; previousGesture = currentGesture;
pointCount = event.pointCount; // Required on UpdateGestures() pointCount = event.pointCount; // Required on UpdateGestures()
@ -139,9 +136,12 @@ void ProcessGestureEvent(GestureEvent event)
} }
touchDownPosition = event.position[0]; touchDownPosition = event.position[0];
touchDownDragPosition = event.position[0];
touchUpPosition = touchDownPosition; touchUpPosition = touchDownPosition;
eventTime = GetCurrentTime(); eventTime = GetCurrentTime();
dragVector = (Vector2){ 0.0f, 0.0f };
} }
else if (event.touchAction == TOUCH_UP) else if (event.touchAction == TOUCH_UP)
{ {
@ -149,12 +149,15 @@ void ProcessGestureEvent(GestureEvent event)
// Calculate for swipe // Calculate for swipe
dragDistance = Vector2Distance(touchDownPosition, touchUpPosition); dragDistance = Vector2Distance(touchDownPosition, touchUpPosition);
dragIntensity = dragDistance/(float)draggingTimeCounter; // RAY: WTF!!! Counting frames??? dragIntensity = dragDistance/(float)((GetCurrentTime() - swipeTime));
isMoving = 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)???
{ {
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 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
@ -171,12 +174,18 @@ void ProcessGestureEvent(GestureEvent event)
currentGesture = GESTURE_NONE; currentGesture = GESTURE_NONE;
} }
draggingTimeCounter = 0; touchDownPosition = (Vector2){ 0.0f, 0.0f };
} }
else if (event.touchAction == TOUCH_MOVE) else if (event.touchAction == TOUCH_MOVE)
{ {
if (Vector2Distance(moveDownPosition, event.position[0]) > 5) eventTime = GetCurrentTime(); if (Vector2Distance(moveDownPosition, event.position[0]) > 5) eventTime = GetCurrentTime();
if (!isMoving)
{
swipeTime = GetCurrentTime();
isMoving = true;
}
moveDownPosition = event.position[0]; moveDownPosition = event.position[0];
if (currentGesture == GESTURE_HOLD) if (currentGesture == GESTURE_HOLD)
@ -191,7 +200,8 @@ void ProcessGestureEvent(GestureEvent event)
if (dragDistance >= FORCE_TO_DRAG) currentGesture = 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 else // Two touch points
@ -200,8 +210,10 @@ void ProcessGestureEvent(GestureEvent event)
{ {
touchDownPosition = event.position[0]; touchDownPosition = event.position[0];
touchDownPosition2 = event.position[1]; touchDownPosition2 = event.position[1];
pinchDistance = Vector2Distance(touchDownPosition, touchDownPosition2);
currentGesture = GESTURE_HOLD; currentGesture = GESTURE_HOLD;
timeHold = GetCurrentTime();
} }
else if (event.touchAction == TOUCH_MOVE) else if (event.touchAction == TOUCH_MOVE)
{ {
@ -221,11 +233,16 @@ void ProcessGestureEvent(GestureEvent event)
else 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) else if (event.touchAction == TOUCH_UP)
{ {
pinchDistance = 0.0f; pinchDistance = 0.0f;
pinchAngle = 0.0f;
currentGesture = GESTURE_NONE; currentGesture = GESTURE_NONE;
} }
@ -238,11 +255,16 @@ void UpdateGestures(void)
// NOTE: Gestures are processed through system callbacks on touch events // NOTE: Gestures are processed through system callbacks on touch events
// Detect GESTURE_HOLD // Detect GESTURE_HOLD
if (((currentGesture == GESTURE_TAP) || (currentGesture == GESTURE_DOUBLETAP)) && pointCount < 2) currentGesture = GESTURE_HOLD; if (((currentGesture == GESTURE_TAP) || (currentGesture == GESTURE_DOUBLETAP)) && (pointCount < 2))
if ((GetCurrentTime() - eventTime) > TAP_TIMEOUT && (currentGesture == GESTURE_DRAG) && pointCount < 2)
{ {
currentGesture = GESTURE_HOLD; currentGesture = GESTURE_HOLD;
timeHold = GetCurrentTime();
}
if (((GetCurrentTime() - eventTime) > TAP_TIMEOUT) && (currentGesture == GESTURE_DRAG) && (pointCount < 2))
{
currentGesture = GESTURE_HOLD;
timeHold = GetCurrentTime();
numHold = 1; numHold = 1;
} }
@ -288,17 +310,19 @@ float GetGestureDragAngle(void)
// Get drag vector (between initial and final position) // Get drag vector (between initial and final position)
Vector2 GetGestureDragVector(void) Vector2 GetGestureDragVector(void)
{ {
// TODO: Calculate DRAG vector // NOTE: Calculated in...
return dragVector; return dragVector;
} }
// Hold time measured in ms // 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 // Get distance between two pinch points
@ -307,32 +331,34 @@ float GetGesturePinchDelta(void)
return pinchDistance; return pinchDistance;
} }
// Get number of touch points
int GetTouchPointsCount(void)
{
return pointCount;
}
// Get angle beween two pinch points // Get angle beween two pinch points
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise // NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
float GetGesturePinchAngle(void) 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 // Module specific Functions Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// RAY: Do we really need magnitude??? why??? // Returns angle from two-points vector with X-axis
// TODO: Remove magnitude dependency...
static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition) static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition)
{ {
float angle; float angle;
// RAY: review this (better) solution
angle = atan2(finalPosition.y - initialPosition.y, finalPosition.x - initialPosition.x); angle = atan2(finalPosition.y - initialPosition.y, finalPosition.x - initialPosition.x);
angle *= RAD2DEG; angle *= RAD2DEG;
if (angle < 0) angle += 360; if (angle < 0) angle += 360.0f;
// http://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors
return angle; return angle;
} }

View file

@ -102,6 +102,7 @@ Vector2 GetGestureDragVector(void); // Get gesture drag vect
int GetGestureHoldDuration(void); // Get gesture hold time in frames int GetGestureHoldDuration(void); // Get gesture hold time in frames
float GetGesturePinchDelta(void); // Get gesture pinch delta float GetGesturePinchDelta(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle float GetGesturePinchAngle(void); // Get gesture pinch angle
int GetTouchPointsCount(void); // Get touch points count
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -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 bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad button is NOT being pressed
#endif #endif
int GetTouchX(void); // Returns touch position X (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 (relative to screen size) int GetTouchY(void); // Returns touch position Y for touch point 0 (relative to screen size)
Vector2 GetTouchPosition(void); // Returns touch position XY (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) #if defined(PLATFORM_ANDROID)
bool IsButtonPressed(int button); // Detect if an android physic button has been pressed 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 GetGestureDragIntensity(void); // Get gesture drag intensity
float GetGestureDragAngle(void); // Get gesture drag angle float GetGestureDragAngle(void); // Get gesture drag angle
Vector2 GetGestureDragVector(void); // Get gesture drag vector 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 GetGesturePinchDelta(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle float GetGesturePinchAngle(void); // Get gesture pinch angle
int GetTouchPointsCount(void); // Get touch points count
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Camera System Functions (Module: camera) // Camera System Functions (Module: camera)