2015-04-22 18:34:42 +03:00
|
|
|
/**********************************************************************************************
|
|
|
|
*
|
2016-02-03 21:29:59 +03:00
|
|
|
* raylib Gestures System - Gestures Processing based on input gesture events (touch/mouse)
|
2015-04-22 18:34:42 +03:00
|
|
|
*
|
2016-02-03 21:29:59 +03:00
|
|
|
* Initial design by Marc Palau
|
2016-02-13 19:08:09 +03:00
|
|
|
* Redesigned by Albert Martos and Ian Eito
|
|
|
|
* Reviewed by Ramon Santamaria (@raysan5)
|
2015-04-22 18:34:42 +03:00
|
|
|
*
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
2015-07-29 22:42:43 +03:00
|
|
|
//#define GESTURES_STANDALONE // NOTE: To use the gestures module as standalone lib, just uncomment this line
|
|
|
|
|
|
|
|
#if defined(GESTURES_STANDALONE)
|
|
|
|
#include "gestures.h"
|
|
|
|
#else
|
2016-06-01 15:01:35 +03:00
|
|
|
#include "raylib.h" // Required for: Vector2, Gestures
|
2015-07-29 22:42:43 +03:00
|
|
|
#endif
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2016-06-01 15:01:35 +03:00
|
|
|
#include <math.h> // Required for: atan2(), sqrt()
|
|
|
|
#include <stdint.h> // Required for: uint64_t
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2015-05-05 00:46:31 +03:00
|
|
|
#if defined(_WIN32)
|
2015-12-30 15:34:45 +03:00
|
|
|
// Functions required to query time on Windows
|
|
|
|
int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
|
|
|
|
int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency);
|
2015-05-05 00:46:31 +03:00
|
|
|
#elif defined(__linux)
|
2016-06-01 15:01:35 +03:00
|
|
|
#include <sys/time.h> // Required for: timespec
|
|
|
|
#include <time.h> // Required for: clock_gettime()
|
2015-05-05 00:46:31 +03:00
|
|
|
#endif
|
|
|
|
|
2015-04-22 18:34:42 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-06-03 19:51:19 +03:00
|
|
|
#define FORCE_TO_SWIPE 0.0005f // Measured in normalized screen units/time
|
|
|
|
#define MINIMUM_DRAG 0.015f // Measured in normalized screen units (0.0f to 1.0f)
|
|
|
|
#define MINIMUM_PINCH 0.005f // Measured in normalized screen units (0.0f to 1.0f)
|
2016-02-17 15:00:48 +03:00
|
|
|
#define TAP_TIMEOUT 300 // Time in milliseconds
|
|
|
|
#define PINCH_TIMEOUT 300 // Time in milliseconds
|
2016-06-03 19:51:19 +03:00
|
|
|
#define DOUBLETAP_RANGE 0.03f // Measured in normalized screen units (0.0f to 1.0f)
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2015-07-29 22:42:43 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-02-03 21:29:59 +03:00
|
|
|
// ...
|
2015-04-22 18:34:42 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2016-02-02 19:59:13 +03:00
|
|
|
// Touch gesture variables
|
2016-02-10 12:31:06 +03:00
|
|
|
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 };
|
2016-02-02 18:43:42 +03:00
|
|
|
static int numTap = 0;
|
2016-02-17 15:00:48 +03:00
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
static int pointCount = 0;
|
2016-03-09 12:15:28 +03:00
|
|
|
static int firstTouchId = -1;
|
2016-02-02 18:43:42 +03:00
|
|
|
|
2016-02-03 21:29:59 +03:00
|
|
|
static double eventTime = 0.0;
|
2016-02-10 12:31:06 +03:00
|
|
|
static double swipeTime = 0.0;
|
2016-02-02 18:43:42 +03:00
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
// Hold gesture variables
|
|
|
|
static int numHold = 0;
|
|
|
|
static float timeHold = 0.0f;
|
|
|
|
|
2016-02-10 12:31:06 +03:00
|
|
|
// Drag gesture variables
|
2016-02-10 15:54:32 +03:00
|
|
|
static Vector2 dragVector = { 0.0f , 0.0f }; // DRAG vector (between initial and current position)
|
2016-02-17 15:00:48 +03:00
|
|
|
static float dragAngle = 0.0f; // DRAG angle (relative to x-axis)
|
|
|
|
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)
|
|
|
|
static bool startMoving = false; // SWIPE used to define when start measuring swipeTime
|
2015-07-29 22:42:43 +03:00
|
|
|
|
|
|
|
// Pinch gesture variables
|
2016-02-17 15:00:48 +03:00
|
|
|
static Vector2 pinchVector = { 0.0f , 0.0f }; // PINCH vector (between first and second touch points)
|
|
|
|
static float pinchAngle = 0.0f; // PINCH angle (relative to x-axis)
|
|
|
|
static float pinchDistance = 0.0f; // PINCH displacement distance (normalized [0..1])
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2015-07-29 22:42:43 +03:00
|
|
|
// Detected gestures
|
|
|
|
static int previousGesture = GESTURE_NONE;
|
2015-04-22 18:34:42 +03:00
|
|
|
static int currentGesture = GESTURE_NONE;
|
2015-07-29 22:42:43 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Enabled gestures flags, all gestures enabled by default
|
2016-02-02 18:43:42 +03:00
|
|
|
static unsigned int enabledGestures = 0b0000001111111111;
|
2015-04-22 18:34:42 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-02-08 11:04:33 +03:00
|
|
|
static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition);
|
2016-02-02 19:59:13 +03:00
|
|
|
static float Vector2Distance(Vector2 v1, Vector2 v2);
|
2016-02-17 15:00:48 +03:00
|
|
|
static double GetCurrentTime(void);
|
2015-04-22 18:34:42 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2016-06-14 13:01:57 +03:00
|
|
|
// Enable only desired getures to be detected
|
|
|
|
void SetGesturesEnabled(unsigned int gestureFlags)
|
|
|
|
{
|
|
|
|
enabledGestures = gestureFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a gesture have been detected
|
|
|
|
bool IsGestureDetected(int gesture)
|
|
|
|
{
|
|
|
|
if ((enabledGestures & currentGesture) == gesture) return true;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Process gesture event and translate it into gestures
|
|
|
|
void ProcessGestureEvent(GestureEvent event)
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-03 21:29:59 +03:00
|
|
|
// Reset required variables
|
2015-07-29 22:42:43 +03:00
|
|
|
previousGesture = currentGesture;
|
|
|
|
|
2016-02-02 19:59:13 +03:00
|
|
|
pointCount = event.pointCount; // Required on UpdateGestures()
|
2016-02-02 18:43:42 +03:00
|
|
|
|
|
|
|
if (pointCount < 2)
|
2016-03-09 12:15:28 +03:00
|
|
|
{
|
2016-02-02 18:43:42 +03:00
|
|
|
if (event.touchAction == TOUCH_DOWN)
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-02 18:43:42 +03:00
|
|
|
numTap++; // Tap counter
|
2016-02-10 15:54:32 +03:00
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
// Detect GESTURE_DOUBLE_TAP
|
2016-02-02 19:59:13 +03:00
|
|
|
if ((currentGesture == GESTURE_NONE) && (numTap >= 2) && ((GetCurrentTime() - eventTime) < TAP_TIMEOUT) && (Vector2Distance(touchDownPosition, event.position[0]) < DOUBLETAP_RANGE))
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-02 18:43:42 +03:00
|
|
|
currentGesture = GESTURE_DOUBLETAP;
|
|
|
|
numTap = 0;
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|
2016-02-02 18:43:42 +03:00
|
|
|
else // Detect GESTURE_TAP
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-02 18:43:42 +03:00
|
|
|
numTap = 1;
|
|
|
|
currentGesture = GESTURE_TAP;
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|
2016-02-02 18:43:42 +03:00
|
|
|
|
|
|
|
touchDownPosition = event.position[0];
|
2016-02-10 12:31:06 +03:00
|
|
|
touchDownDragPosition = event.position[0];
|
2016-02-02 18:43:42 +03:00
|
|
|
|
|
|
|
touchUpPosition = touchDownPosition;
|
|
|
|
eventTime = GetCurrentTime();
|
2016-02-10 12:31:06 +03:00
|
|
|
|
2016-03-09 12:15:28 +03:00
|
|
|
firstTouchId = event.pointerId[0];
|
|
|
|
|
2016-02-10 12:31:06 +03:00
|
|
|
dragVector = (Vector2){ 0.0f, 0.0f };
|
2016-02-02 18:43:42 +03:00
|
|
|
}
|
|
|
|
else if (event.touchAction == TOUCH_UP)
|
|
|
|
{
|
2016-02-02 19:59:13 +03:00
|
|
|
if (currentGesture == GESTURE_DRAG) touchUpPosition = event.position[0];
|
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
// NOTE: dragIntensity dependend on the resolution of the screen
|
2016-02-03 21:29:59 +03:00
|
|
|
dragDistance = Vector2Distance(touchDownPosition, touchUpPosition);
|
2016-02-10 12:31:06 +03:00
|
|
|
dragIntensity = dragDistance/(float)((GetCurrentTime() - swipeTime));
|
2016-02-17 15:00:48 +03:00
|
|
|
|
|
|
|
startMoving = false;
|
2016-02-02 18:43:42 +03:00
|
|
|
|
|
|
|
// Detect GESTURE_SWIPE
|
2016-03-09 12:15:28 +03:00
|
|
|
if ((dragIntensity > FORCE_TO_SWIPE) && firstTouchId == event.pointerId[0])
|
2016-02-02 18:43:42 +03:00
|
|
|
{
|
2016-02-10 12:31:06 +03:00
|
|
|
// NOTE: Angle should be inverted in Y
|
2016-02-17 15:00:48 +03:00
|
|
|
dragAngle = 360.0f - Vector2Angle(touchDownPosition, touchUpPosition);
|
2016-02-02 19:59:13 +03:00
|
|
|
|
2016-02-03 21:29:59 +03:00
|
|
|
if ((dragAngle < 30) || (dragAngle > 330)) currentGesture = GESTURE_SWIPE_RIGHT; // Right
|
|
|
|
else if ((dragAngle > 30) && (dragAngle < 120)) currentGesture = GESTURE_SWIPE_UP; // Up
|
|
|
|
else if ((dragAngle > 120) && (dragAngle < 210)) currentGesture = GESTURE_SWIPE_LEFT; // Left
|
|
|
|
else if ((dragAngle > 210) && (dragAngle < 300)) currentGesture = GESTURE_SWIPE_DOWN; // Down
|
2016-02-02 18:43:42 +03:00
|
|
|
else currentGesture = GESTURE_NONE;
|
|
|
|
}
|
|
|
|
else
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-03 21:29:59 +03:00
|
|
|
dragDistance = 0.0f;
|
|
|
|
dragIntensity = 0.0f;
|
|
|
|
dragAngle = 0.0f;
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
currentGesture = GESTURE_NONE;
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|
2016-02-02 18:43:42 +03:00
|
|
|
|
2016-02-10 15:54:32 +03:00
|
|
|
touchDownDragPosition = (Vector2){ 0.0f, 0.0f };
|
2016-03-16 19:51:21 +03:00
|
|
|
pointCount = 0;
|
2016-02-02 18:43:42 +03:00
|
|
|
}
|
|
|
|
else if (event.touchAction == TOUCH_MOVE)
|
|
|
|
{
|
2016-02-20 21:01:41 +03:00
|
|
|
if (currentGesture == GESTURE_DRAG) eventTime = GetCurrentTime();
|
2016-02-02 19:59:13 +03:00
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
if (!startMoving)
|
2016-02-10 12:31:06 +03:00
|
|
|
{
|
|
|
|
swipeTime = GetCurrentTime();
|
2016-02-17 15:00:48 +03:00
|
|
|
startMoving = true;
|
2016-02-10 12:31:06 +03:00
|
|
|
}
|
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
moveDownPosition = event.position[0];
|
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
if (currentGesture == GESTURE_HOLD)
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-02 18:43:42 +03:00
|
|
|
if (numHold == 1) touchDownPosition = event.position[0];
|
|
|
|
|
|
|
|
numHold = 2;
|
|
|
|
|
|
|
|
// Detect GESTURE_DRAG
|
2016-02-17 15:00:48 +03:00
|
|
|
if (Vector2Distance(touchDownPosition, moveDownPosition) >= MINIMUM_DRAG)
|
|
|
|
{
|
|
|
|
eventTime = GetCurrentTime();
|
|
|
|
currentGesture = GESTURE_DRAG;
|
|
|
|
}
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|
2016-02-10 12:31:06 +03:00
|
|
|
|
|
|
|
dragVector.x = moveDownPosition.x - touchDownDragPosition.x;
|
|
|
|
dragVector.y = moveDownPosition.y - touchDownDragPosition.y;
|
2016-02-02 18:43:42 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 19:59:13 +03:00
|
|
|
else // Two touch points
|
2016-02-02 18:43:42 +03:00
|
|
|
{
|
|
|
|
if (event.touchAction == TOUCH_DOWN)
|
|
|
|
{
|
|
|
|
touchDownPosition = event.position[0];
|
|
|
|
touchDownPosition2 = event.position[1];
|
2016-02-17 15:00:48 +03:00
|
|
|
|
|
|
|
//pinchDistance = Vector2Distance(touchDownPosition, touchDownPosition2);
|
|
|
|
|
|
|
|
pinchVector.x = touchDownPosition2.x - touchDownPosition.x;
|
|
|
|
pinchVector.y = touchDownPosition2.y - touchDownPosition.y;
|
2016-02-02 18:43:42 +03:00
|
|
|
|
|
|
|
currentGesture = GESTURE_HOLD;
|
2016-02-10 12:31:06 +03:00
|
|
|
timeHold = GetCurrentTime();
|
2016-02-02 18:43:42 +03:00
|
|
|
}
|
|
|
|
else if (event.touchAction == TOUCH_MOVE)
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-03 21:29:59 +03:00
|
|
|
pinchDistance = Vector2Distance(moveDownPosition, moveDownPosition2);
|
2016-02-17 15:00:48 +03:00
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
touchDownPosition = moveDownPosition;
|
|
|
|
touchDownPosition2 = moveDownPosition2;
|
|
|
|
|
|
|
|
moveDownPosition = event.position[0];
|
|
|
|
moveDownPosition2 = event.position[1];
|
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
pinchVector.x = moveDownPosition2.x - moveDownPosition.x;
|
|
|
|
pinchVector.y = moveDownPosition2.y - moveDownPosition.y;
|
|
|
|
|
|
|
|
if ((Vector2Distance(touchDownPosition, moveDownPosition) >= MINIMUM_PINCH) || (Vector2Distance(touchDownPosition2, moveDownPosition2) >= MINIMUM_PINCH))
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-03 21:29:59 +03:00
|
|
|
if ((Vector2Distance(moveDownPosition, moveDownPosition2) - pinchDistance) < 0) currentGesture = GESTURE_PINCH_IN;
|
2016-02-02 18:43:42 +03:00
|
|
|
else currentGesture = GESTURE_PINCH_OUT;
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|
2016-02-17 15:00:48 +03:00
|
|
|
else
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2016-02-17 15:00:48 +03:00
|
|
|
currentGesture = GESTURE_HOLD;
|
|
|
|
timeHold = GetCurrentTime();
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|
2016-02-10 12:31:06 +03:00
|
|
|
|
|
|
|
// NOTE: Angle should be inverted in Y
|
|
|
|
pinchAngle = 360.0f - Vector2Angle(moveDownPosition, moveDownPosition2);
|
2016-02-02 18:43:42 +03:00
|
|
|
}
|
|
|
|
else if (event.touchAction == TOUCH_UP)
|
|
|
|
{
|
2016-02-03 21:29:59 +03:00
|
|
|
pinchDistance = 0.0f;
|
2016-02-10 12:31:06 +03:00
|
|
|
pinchAngle = 0.0f;
|
2016-02-17 15:00:48 +03:00
|
|
|
pinchVector = (Vector2){ 0.0f, 0.0f };
|
2016-03-16 19:51:21 +03:00
|
|
|
pointCount = 0;
|
2016-02-03 21:29:59 +03:00
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
currentGesture = GESTURE_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update gestures detected (must be called every frame)
|
|
|
|
void UpdateGestures(void)
|
|
|
|
{
|
|
|
|
// NOTE: Gestures are processed through system callbacks on touch events
|
|
|
|
|
|
|
|
// Detect GESTURE_HOLD
|
2016-02-10 12:31:06 +03:00
|
|
|
if (((currentGesture == GESTURE_TAP) || (currentGesture == GESTURE_DOUBLETAP)) && (pointCount < 2))
|
|
|
|
{
|
|
|
|
currentGesture = GESTURE_HOLD;
|
|
|
|
timeHold = GetCurrentTime();
|
|
|
|
}
|
2016-02-02 19:59:13 +03:00
|
|
|
|
2016-02-10 12:31:06 +03:00
|
|
|
if (((GetCurrentTime() - eventTime) > TAP_TIMEOUT) && (currentGesture == GESTURE_DRAG) && (pointCount < 2))
|
2016-02-02 18:43:42 +03:00
|
|
|
{
|
|
|
|
currentGesture = GESTURE_HOLD;
|
2016-02-10 12:31:06 +03:00
|
|
|
timeHold = GetCurrentTime();
|
2016-02-02 18:43:42 +03:00
|
|
|
numHold = 1;
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|
2016-02-02 18:43:42 +03:00
|
|
|
|
|
|
|
// Detect GESTURE_NONE
|
|
|
|
if ((currentGesture == GESTURE_SWIPE_RIGHT) || (currentGesture == GESTURE_SWIPE_UP) || (currentGesture == GESTURE_SWIPE_LEFT) || (currentGesture == GESTURE_SWIPE_DOWN))
|
|
|
|
{
|
|
|
|
currentGesture = GESTURE_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
// Get number of touch points
|
|
|
|
int GetTouchPointsCount(void)
|
2016-01-03 15:01:21 +03:00
|
|
|
{
|
2016-02-17 15:00:48 +03:00
|
|
|
// NOTE: point count is calculated when ProcessGestureEvent(GestureEvent event) is called
|
|
|
|
|
|
|
|
return pointCount;
|
2016-01-03 15:01:21 +03:00
|
|
|
}
|
|
|
|
|
2016-06-14 13:01:57 +03:00
|
|
|
// Get latest detected gesture
|
|
|
|
int GetGestureDetected(void)
|
2016-01-03 15:01:21 +03:00
|
|
|
{
|
2016-06-14 13:01:57 +03:00
|
|
|
// Get current gesture only if enabled
|
|
|
|
return (enabledGestures & currentGesture);
|
2016-01-03 15:01:21 +03:00
|
|
|
}
|
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
// Hold time measured in ms
|
|
|
|
float GetGestureHoldDuration(void)
|
2016-01-03 15:01:21 +03:00
|
|
|
{
|
2016-02-17 15:00:48 +03:00
|
|
|
// NOTE: time is calculated on current gesture HOLD
|
2016-02-10 15:54:32 +03:00
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
float time = 0.0f;
|
|
|
|
|
|
|
|
if (currentGesture == GESTURE_HOLD) time = (float)GetCurrentTime() - timeHold;
|
|
|
|
|
|
|
|
return time;
|
2016-01-03 15:01:21 +03:00
|
|
|
}
|
|
|
|
|
2016-02-10 15:54:32 +03:00
|
|
|
// Get drag vector (between initial touch point to current)
|
2016-01-03 15:01:21 +03:00
|
|
|
Vector2 GetGestureDragVector(void)
|
|
|
|
{
|
2016-02-10 15:54:32 +03:00
|
|
|
// NOTE: drag vector is calculated on one touch points TOUCH_MOVE
|
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
return dragVector;
|
|
|
|
}
|
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
// Get drag angle
|
|
|
|
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
|
|
|
|
float GetGestureDragAngle(void)
|
2016-01-03 15:01:21 +03:00
|
|
|
{
|
2016-02-17 15:00:48 +03:00
|
|
|
// NOTE: drag angle is calculated on one touch points TOUCH_UP
|
2016-02-03 21:29:59 +03:00
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
return dragAngle;
|
2016-01-03 15:01:21 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 21:29:59 +03:00
|
|
|
// Get distance between two pinch points
|
2016-02-17 15:00:48 +03:00
|
|
|
Vector2 GetGesturePinchVector(void)
|
2016-01-03 15:01:21 +03:00
|
|
|
{
|
2016-02-10 15:54:32 +03:00
|
|
|
// 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
|
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
return pinchVector;
|
2016-02-10 12:31:06 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Get angle beween two pinch points
|
|
|
|
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
|
|
|
|
float GetGesturePinchAngle(void)
|
|
|
|
{
|
2016-02-10 12:31:06 +03:00
|
|
|
// NOTE: pinch angle is calculated on two touch points TOUCH_MOVE
|
2016-02-03 21:29:59 +03:00
|
|
|
|
2016-02-10 12:31:06 +03:00
|
|
|
return pinchAngle;
|
2016-01-03 15:01:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2016-02-10 12:31:06 +03:00
|
|
|
// Returns angle from two-points vector with X-axis
|
2016-02-08 11:04:33 +03:00
|
|
|
static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition)
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
|
|
|
float angle;
|
2016-02-10 12:31:06 +03:00
|
|
|
|
2016-02-08 11:04:33 +03:00
|
|
|
angle = atan2(finalPosition.y - initialPosition.y, finalPosition.x - initialPosition.x);
|
|
|
|
angle *= RAD2DEG;
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2016-02-10 12:31:06 +03:00
|
|
|
if (angle < 0) angle += 360.0f;
|
2016-02-08 11:04:33 +03:00
|
|
|
|
2015-04-22 18:34:42 +03:00
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:29:59 +03:00
|
|
|
// Calculate distance between two Vector2
|
2016-02-02 19:59:13 +03:00
|
|
|
static float Vector2Distance(Vector2 v1, Vector2 v2)
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
|
|
|
float result;
|
|
|
|
|
|
|
|
float dx = v2.x - v1.x;
|
|
|
|
float dy = v2.y - v1.y;
|
|
|
|
|
|
|
|
result = sqrt(dx*dx + dy*dy);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
// Time measure returned are milliseconds
|
2016-02-17 15:00:48 +03:00
|
|
|
static double GetCurrentTime(void)
|
2015-04-22 18:34:42 +03:00
|
|
|
{
|
2015-05-21 01:18:22 +03:00
|
|
|
double time = 0;
|
2015-10-06 18:21:04 +03:00
|
|
|
|
2015-12-30 15:34:45 +03:00
|
|
|
#if defined(_WIN32)
|
|
|
|
unsigned long long int clockFrequency, currentTime;
|
2015-10-06 18:21:04 +03:00
|
|
|
|
2015-12-30 15:34:45 +03:00
|
|
|
QueryPerformanceFrequency(&clockFrequency);
|
|
|
|
QueryPerformanceCounter(¤tTime);
|
2015-10-06 18:21:04 +03:00
|
|
|
|
2016-02-08 11:04:33 +03:00
|
|
|
time = (double)currentTime/clockFrequency*1000.0f; // Time in miliseconds
|
2015-05-05 00:46:31 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux)
|
|
|
|
// NOTE: Only for Linux-based systems
|
2015-04-22 18:34:42 +03:00
|
|
|
struct timespec now;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
2016-02-08 11:04:33 +03:00
|
|
|
uint64_t nowTime = (uint64_t)now.tv_sec*1000000000LLU + (uint64_t)now.tv_nsec; // Time in nanoseconds
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2016-02-08 11:04:33 +03:00
|
|
|
time = ((double)nowTime/1000000.0); // Time in miliseconds
|
2015-05-05 00:46:31 +03:00
|
|
|
#endif
|
2015-05-21 01:18:22 +03:00
|
|
|
|
|
|
|
return time;
|
2015-04-22 18:34:42 +03:00
|
|
|
}
|