aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2016-02-17 13:00:48 +0100
committerRay <raysan5@gmail.com>2016-02-17 13:00:48 +0100
commitafd2ffb74a84bf48b9129c613e1673ceae0bd46b (patch)
tree6b36467cb71b55e3073d0dd59b1c16bf006eefca
parent825e42dc008793c16da9f267bf1c8d020d2d9ece (diff)
downloadraylib-afd2ffb74a84bf48b9129c613e1673ceae0bd46b.tar.gz
raylib-afd2ffb74a84bf48b9129c613e1673ceae0bd46b.zip
Updated gestures module
Using normalized [0..1] input points
-rw-r--r--src/core.c28
-rw-r--r--src/gestures.c134
-rw-r--r--src/gestures.h12
-rw-r--r--src/raylib.h11
4 files changed, 109 insertions, 76 deletions
diff --git a/src/core.c b/src/core.c
index 0c85ab26..7b981097 100644
--- a/src/core.c
+++ b/src/core.c
@@ -1700,14 +1700,22 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
// Register touch actions
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;
+ // NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback()
+
+ // Assign a pointer ID
+ gestureEvent.pointerId[0] = 0;
+
// Register touch points count
gestureEvent.pointCount = 1;
// Register touch points position, only one point registered
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
ProcessGestureEvent(gestureEvent);
@@ -1729,6 +1737,10 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
// Register touch points position, only one point registered
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
ProcessGestureEvent(gestureEvent);
@@ -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[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
ProcessGestureEvent(gestureEvent);
@@ -2564,6 +2583,13 @@ static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent
touchPosition[0] = gestureEvent.position[0];
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
ProcessGestureEvent(gestureEvent); // Process obtained gestures data
diff --git a/src/gestures.c b/src/gestures.c
index 16442e45..af92ba3d 100644
--- a/src/gestures.c
+++ b/src/gestures.c
@@ -45,12 +45,12 @@
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
-#define FORCE_TO_SWIPE 1 // Time in milliseconds
-#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 FORCE_TO_SWIPE 0.0005f // Measured in normalized pixels / time
+#define MINIMUM_DRAG 0.015f // Measured in normalized pixels [0..1]
+#define MINIMUM_PINCH 0.005f // Measured in normalized pixels [0..1]
+#define TAP_TIMEOUT 300 // Time in milliseconds
+#define PINCH_TIMEOUT 300 // Time in milliseconds
+#define DOUBLETAP_RANGE 0.03f
//----------------------------------------------------------------------------------
// Types and Structures Definition
@@ -68,26 +68,29 @@ 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;
+// Hold gesture variables
+static int numHold = 0;
+static float timeHold = 0.0f;
+
// Drag gesture variables
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 direction for SWIPE GESTURE
-static float dragIntensity = 0.0f; // DRAG intensity, how far why did the DRAG (pixels per frame) for SWIPE GESTURE
+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
// Pinch gesture variables
-static float pinchDistance = 0.0f; // Pinch displacement distance
-static float pinchAngle = 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 angle (relative to x-axis)
+static float pinchDistance = 0.0f; // PINCH displacement distance (normalized [0..1])
// Detected gestures
static int previousGesture = GESTURE_NONE;
@@ -101,7 +104,7 @@ static unsigned int enabledGestures = 0b0000001111111111;
//----------------------------------------------------------------------------------
static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition);
static float Vector2Distance(Vector2 v1, Vector2 v2);
-static double GetCurrentTime();
+static double GetCurrentTime(void);
//----------------------------------------------------------------------------------
// Module Functions Definition
@@ -147,17 +150,19 @@ void ProcessGestureEvent(GestureEvent event)
{
if (currentGesture == GESTURE_DRAG) touchUpPosition = event.position[0];
- // Calculate for swipe
+ // NOTE: dragIntensity dependend on the resolution of the screen
dragDistance = Vector2Distance(touchDownPosition, touchUpPosition);
dragIntensity = dragDistance/(float)((GetCurrentTime() - swipeTime));
+
+ // TODO: Make getures detection resolution independant
- isMoving = false;
+ startMoving = false;
// Detect GESTURE_SWIPE
if ((dragIntensity > FORCE_TO_SWIPE) && (touchId == 0)) // RAY: why check (touchId == 0)???
{
// 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
else if ((dragAngle > 30) && (dragAngle < 120)) currentGesture = GESTURE_SWIPE_UP; // Up
@@ -178,26 +183,28 @@ void ProcessGestureEvent(GestureEvent event)
}
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();
- isMoving = true;
+ startMoving = true;
}
moveDownPosition = event.position[0];
- if (currentGesture == GESTURE_HOLD)
+ if (currentGesture == GESTURE_HOLD)
{
if (numHold == 1) touchDownPosition = event.position[0];
numHold = 2;
-
- dragDistance = Vector2Distance(touchDownPosition, moveDownPosition);
// 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;
@@ -210,7 +217,11 @@ void ProcessGestureEvent(GestureEvent event)
{
touchDownPosition = event.position[0];
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;
timeHold = GetCurrentTime();
@@ -218,22 +229,25 @@ void ProcessGestureEvent(GestureEvent event)
else if (event.touchAction == TOUCH_MOVE)
{
pinchDistance = Vector2Distance(moveDownPosition, moveDownPosition2);
-
+
touchDownPosition = moveDownPosition;
touchDownPosition2 = moveDownPosition2;
moveDownPosition = event.position[0];
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;
else currentGesture = GESTURE_PINCH_OUT;
}
- else
+ else
{
- currentGesture = GESTURE_HOLD;
- timeHold = GetCurrentTime();
+ currentGesture = GESTURE_HOLD;
+ timeHold = GetCurrentTime();
}
// NOTE: Angle should be inverted in Y
@@ -243,6 +257,7 @@ void ProcessGestureEvent(GestureEvent event)
{
pinchDistance = 0.0f;
pinchAngle = 0.0f;
+ pinchVector = (Vector2){ 0.0f, 0.0f };
currentGesture = GESTURE_NONE;
}
@@ -289,26 +304,30 @@ int GetGestureType(void)
return (enabledGestures & currentGesture);
}
-void SetGesturesEnabled(unsigned int gestureFlags)
+// Get number of touch points
+int GetTouchPointsCount(void)
{
- enabledGestures = gestureFlags;
+ // NOTE: point count is calculated when ProcessGestureEvent(GestureEvent event) is called
+
+ return pointCount;
}
-// Get drag dragIntensity (pixels per frame)
-float GetGestureDragdragIntensity(void)
+// Enable only desired getures to be detected
+void SetGesturesEnabled(unsigned int gestureFlags)
{
- // NOTE: drag intensity is calculated on one touch points TOUCH_UP
-
- return dragIntensity;
+ enabledGestures = gestureFlags;
}
-// Get drag angle
-// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
-float GetGestureDragAngle(void)
+// Hold time measured in ms
+float GetGestureHoldDuration(void)
{
- // NOTE: drag angle is calculated on one touch points TOUCH_UP
+ // NOTE: time is calculated on current gesture HOLD
- return dragAngle;
+ float time = 0.0f;
+
+ if (currentGesture == GESTURE_HOLD) time = (float)GetCurrentTime() - timeHold;
+
+ return time;
}
// Get drag vector (between initial touch point to current)
@@ -319,33 +338,22 @@ Vector2 GetGestureDragVector(void)
return dragVector;
}
-// Hold time measured in ms
-float GetGestureHoldDuration(void)
+// Get drag angle
+// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
+float GetGestureDragAngle(void)
{
- // NOTE: time is calculated on current gesture HOLD
-
- float time = 0.0f;
-
- if (currentGesture == GESTURE_HOLD) time = (float)GetCurrentTime() - timeHold;
+ // NOTE: drag angle is calculated on one touch points TOUCH_UP
- return time;
+ return dragAngle;
}
// 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: pinch distance is calculated on two touch points TOUCH_MOVE
- return pinchDistance;
-}
-
-// Get number of touch points
-int GetTouchPointsCount(void)
-{
- // NOTE: point count is calculated when ProcessGestureEvent(GestureEvent event) is called
-
- return pointCount;
+ return pinchVector;
}
// Get angle beween two pinch points
@@ -388,7 +396,7 @@ static float Vector2Distance(Vector2 v1, Vector2 v2)
}
// Time measure returned are milliseconds
-static double GetCurrentTime()
+static double GetCurrentTime(void)
{
double time = 0;
diff --git a/src/gestures.h b/src/gestures.h
index 52f778be..5468eb54 100644
--- a/src/gestures.h
+++ b/src/gestures.h
@@ -90,19 +90,19 @@ extern "C" { // Prevents name mangling of functions
//----------------------------------------------------------------------------------
// 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)
bool IsGestureDetected(void); // Check if a gesture have been detected
int GetGestureType(void); // Get latest detected gesture
void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags
-void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
+int GetTouchPointsCount(void); // Get touch points count
-float GetGestureDragIntensity(void); // Get gesture drag intensity
-float GetGestureDragAngle(void); // Get gesture drag angle
+float GetGestureHoldDuration(void); // Get gesture hold time in milliseconds
Vector2 GetGestureDragVector(void); // Get gesture drag vector
-int GetGestureHoldDuration(void); // Get gesture hold time in frames
-float GetGesturePinchDelta(void); // Get gesture pinch delta
+float GetGestureDragAngle(void); // Get gesture drag angle
+Vector2 GetGesturePinchVector(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle
-int GetTouchPointsCount(void); // Get touch points count
+
#ifdef __cplusplus
}
diff --git a/src/raylib.h b/src/raylib.h
index f5a3cc31..def56ee2 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -607,19 +607,18 @@ bool IsButtonReleased(int button); // Detect if an android
//------------------------------------------------------------------------------------
// 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)
bool IsGestureDetected(void); // Check if a gesture have been detected
int GetGestureType(void); // Get latest detected gesture
void SetGesturesEnabled(unsigned int gestureFlags); // Enable a set of gestures using flags
-void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
+int GetTouchPointsCount(void); // Get touch points count
-float GetGestureDragIntensity(void); // Get gesture drag intensity
-float GetGestureDragAngle(void); // Get gesture drag angle
+float GetGestureHoldDuration(void); // Get gesture hold time in milliseconds
Vector2 GetGestureDragVector(void); // Get gesture drag vector
-float GetGestureHoldDuration(void); // Get gesture hold time in ms
-float GetGesturePinchDelta(void); // Get gesture pinch delta
+float GetGestureDragAngle(void); // Get gesture drag angle
+Vector2 GetGesturePinchVector(void); // Get gesture pinch delta
float GetGesturePinchAngle(void); // Get gesture pinch angle
-int GetTouchPointsCount(void); // Get touch points count
//------------------------------------------------------------------------------------
// Camera System Functions (Module: camera)