Jelajahi Sumber

Decoupling gestures system module

Now gestures system can be used as standalone module
raysan5 10 tahun lalu
induk
melakukan
0b24330d86
2 mengubah file dengan 220 tambahan dan 112 penghapusan
  1. 113 112
      src/gestures.c
  2. 107 0
      src/gestures.h

+ 113 - 112
src/gestures.c

@@ -1,8 +1,6 @@
 /**********************************************************************************************
 *
-*   raylib.gestures
-*
-*   Gestures Detection and Usage Functions Definitions
+*   raylib Gestures System - Gestures Detection and Usage Functions (Android and HTML5)
 *
 *   Copyright (c) 2015 Marc Palau and Ramon Santamaria
 *
@@ -23,8 +21,13 @@
 *
 **********************************************************************************************/
 
-#include "raylib.h"
-#include "utils.h"
+//#define GESTURES_STANDALONE     // NOTE: To use the gestures module as standalone lib, just uncomment this line
+
+#if defined(GESTURES_STANDALONE)
+    #include "gestures.h"
+#else
+    #include "raylib.h"         // Required for typedef(s): Vector2, Gestures
+#endif
 
 #include <stdlib.h>             // malloc(), free()
 #include <stdio.h>              // printf(), fprintf()
@@ -53,9 +56,11 @@
 //----------------------------------------------------------------------------------
 #define FORCE_TO_SWIPE          20
 #define TAP_TIMEOUT             300
-
 #define MAX_TOUCH_POINTS        4
 
+//----------------------------------------------------------------------------------
+// Types and Structures Definition
+//----------------------------------------------------------------------------------
 typedef enum {
     TYPE_MOTIONLESS,
     TYPE_DRAG,
@@ -75,72 +80,56 @@ typedef struct {
     Vector2 position[MAX_TOUCH_POINTS];
 } GestureEvent;
 
+
 //----------------------------------------------------------------------------------
 // Global Variables Definition
 //----------------------------------------------------------------------------------
-
 static GestureType gestureType = TYPE_MOTIONLESS;
 static double eventTime = 0;
-//static int32_t touchId;         // Not used...
+//static int32_t touchId;               // Not used...
 
-// Tap
-// Our initial press position on tap
+// Tap gesture variables
 static Vector2 initialTapPosition = { 0, 0 };
 
-// Double tap
-// If we are double tapping or not
+// Double Tap gesture variables
 static bool doubleTapping = false;
-// If we recently made a tap
-static bool untap = false;
+static bool untap = false;              // Check if recently done a tap
 
-// Drag
-// Our initial press position on drag
+// Drag gesture variables
 static Vector2 initialDragPosition = { 0, 0 };
-// Position that will compare itself with the mouse one
 static Vector2 endDragPosition = { 0, 0 };
-// Position of the last event detection
 static Vector2 lastDragPosition = { 0, 0 };
-// The total drag vector
 static Vector2 dragVector = { 0, 0 };
-// The distance traveled dragging
-static float magnitude = 0;
-// The angle direction of the drag
-static float angle = 0;
-// A magnitude to calculate how fast we did the drag ( pixels per frame )
-static float intensity = 0;
-// Time that have passed while dragging
-static int draggingTimeCounter = 0;
-
-// Pinch
-// First initial pinch position
+
+static float magnitude = 0;             // Distance traveled dragging
+static float angle = 0;                 // Angle direction of the drag
+static float intensity = 0;             // How fast we did the drag (pixels per frame)
+static int draggingTimeCounter = 0;     // Time that have passed while dragging
+
+// Pinch gesture variables
 static Vector2 firstInitialPinchPosition = { 0, 0 };
-// Second initial pinch position
 static Vector2 secondInitialPinchPosition = { 0, 0 };
-// First end pinch position
 static Vector2 firstEndPinchPosition = { 0, 0 };
-// Second end pinch position
 static Vector2 secondEndPinchPosition = { 0, 0 };
-// Delta Displacement
-static float pinchDelta = 0;
+static float pinchDelta = 0;            // Pinch delta displacement
 
-// Detected gesture
+// Detected gestures
+static int previousGesture = GESTURE_NONE;
 static int currentGesture = GESTURE_NONE;
-unsigned int enabledGestures = 0;           // TODO: Currently not in use...
+
+static unsigned int enabledGestures = 0;       // TODO: Currently not in use...
 
 static Vector2 touchPosition;
 
 //----------------------------------------------------------------------------------
 // Module specific Functions Declaration
 //----------------------------------------------------------------------------------
-extern void ResetGestures(void);
-extern Vector2 GetRawPosition(void);
-
 static void ProcessMotionEvent(GestureEvent event);
+
+static void InitPinchGesture(Vector2 posA, Vector2 posB);
 static float CalculateAngle(Vector2 initialPosition, Vector2 actualPosition, float magnitude);
-static float OnPinch();
-static void SetDualInput(GestureEvent event);
-static float Distance(Vector2 v1, Vector2 v2);
-static float DotProduct(Vector2 v1, Vector2 v2);
+static float VectorDistance(Vector2 v1, Vector2 v2);
+static float VectorDotProduct(Vector2 v1, Vector2 v2);
 static double GetCurrentTime();
 
 #if defined(PLATFORM_WEB)
@@ -155,15 +144,43 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
 // Module Functions Definition
 //----------------------------------------------------------------------------------
 
-// Returns tap position XY
-extern Vector2 GetRawPosition(void)
+// Returns touch position X
+int GetTouchX(void)
 {
-    return touchPosition;
+    return (int)touchPosition.x;
+}
+
+// Returns touch position Y
+int GetTouchY(void)
+{
+    return (int)touchPosition.y;
+}
+
+// Returns touch position XY
+// TODO: touch position should be scaled depending on display size and render size
+Vector2 GetTouchPosition(void)
+{
+    Vector2 position = touchPosition;
+/*
+    if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
+    {
+        // TODO: Seems to work ok but... review!
+        position.x = position.x*((float)screenWidth / (float)(displayWidth - renderOffsetX)) - renderOffsetX/2;
+        position.y = position.y*((float)screenHeight / (float)(displayHeight - renderOffsetY)) - renderOffsetY/2;
+    }
+    else
+    {
+        position.x = position.x*((float)renderWidth / (float)displayWidth) - renderOffsetX/2;
+        position.y = position.y*((float)renderHeight / (float)displayHeight) - renderOffsetY/2;
+    }
+*/
+    return position;
 }
 
 // Check if a gesture have been detected
 bool IsGestureDetected(void)
 {
+/*
     if (currentGesture == GESTURE_DRAG)                 TraceLog(INFO, "DRAG");
     else if (currentGesture == GESTURE_TAP)             TraceLog(INFO, "TAP");
     else if (currentGesture == GESTURE_DOUBLETAP)       TraceLog(INFO, "DOUBLE");
@@ -174,6 +191,7 @@ bool IsGestureDetected(void)
     else if (currentGesture == GESTURE_SWIPE_DOWN)      TraceLog(INFO, "DOWN");
     else if (currentGesture == GESTURE_PINCH_IN)        TraceLog(INFO, "PINCH IN");
     else if (currentGesture == GESTURE_PINCH_OUT)       TraceLog(INFO, "PINCH OUT");
+*/
 
     if (currentGesture != GESTURE_NONE) return true;
     else return false;
@@ -228,33 +246,24 @@ float GetGesturePinchAngle(void)
     return 0;
 }
 
-extern void ResetGestures(void)
-{
-    if (currentGesture == GESTURE_TAP) currentGesture = GESTURE_HOLD;
-    else if (currentGesture != GESTURE_HOLD) currentGesture = GESTURE_NONE;
-}
-
 #if defined(PLATFORM_WEB)
-extern void InitWebGestures(void)
+// Init gestures system (web)
+void InitGesturesSystem(void)
 {
-    /*
-    emscripten_set_touchstart_callback("#canvas", data, 0, Emscripten_HandleTouch);
-    emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
-    emscripten_set_touchmove_callback("#canvas", data, 0, Emscripten_HandleTouch);
-    emscripten_set_touchcancel_callback("#canvas", data, 0, Emscripten_HandleTouch);
-    */
-
+    // Init gestures system web (emscripten)
+    
+    // NOTE: Some code examples
     //emscripten_set_touchstart_callback(0, NULL, 1, Emscripten_HandleTouch);
+    //emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
     
     emscripten_set_touchstart_callback("#canvas", NULL, 1, EmscriptenInputCallback);
     emscripten_set_touchend_callback("#canvas", NULL, 1, EmscriptenInputCallback);
     emscripten_set_touchmove_callback("#canvas", NULL, 1, EmscriptenInputCallback);
     emscripten_set_touchcancel_callback("#canvas", NULL, 1, EmscriptenInputCallback);
 }
-#endif
-
-#if defined(PLATFORM_ANDROID)
-extern void InitAndroidGestures(struct android_app *app)
+#elif defined(PLATFORM_ANDROID)
+// Init gestures system (android)
+void InitGesturesSystem(struct android_app *app)
 {
     app->onInputEvent = AndroidInputCallback;
     
@@ -262,6 +271,15 @@ extern void InitAndroidGestures(struct android_app *app)
 }
 #endif
 
+// Update gestures detected (must be called every frame)
+void UpdateGestures(void)
+{
+    // NOTE: Gestures are processed through system callbacks on touch events
+    
+    if ((previousGesture == GESTURE_TAP) && (currentGesture == GESTURE_TAP)) currentGesture = GESTURE_HOLD;
+    else if (currentGesture != GESTURE_HOLD) currentGesture = GESTURE_NONE;
+}
+
 //----------------------------------------------------------------------------------
 // Module specific Functions Definition
 //----------------------------------------------------------------------------------
@@ -271,13 +289,15 @@ static void ProcessMotionEvent(GestureEvent event)
     dragVector = (Vector2){ 0, 0 };
     pinchDelta = 0;
     
+    previousGesture = currentGesture;
+    
     switch (gestureType)
     {
         case TYPE_MOTIONLESS: // Detect TAP, DOUBLE_TAP and HOLD events
         {
             if (event.action == DOWN)
             {
-                if (event.pointCount > 1) SetDualInput(event);
+                if (event.pointCount > 1) InitPinchGesture(event.position[0], event.position[1]);
                 else
                 {
                     // Set the press position
@@ -317,7 +337,7 @@ static void ProcessMotionEvent(GestureEvent event)
             // Begin dragging
             else if (event.action == MOVE)
             {
-                if (event.pointCount > 1) SetDualInput(event);
+                if (event.pointCount > 1) InitPinchGesture(event.position[0], event.position[1]);
                 else
                 {
                     // Set the drag starting position
@@ -354,7 +374,7 @@ static void ProcessMotionEvent(GestureEvent event)
             // Update while we are dragging
             else if (event.action == MOVE)
             {
-                if (event.pointCount > 1) SetDualInput(event);
+                if (event.pointCount > 1) InitPinchGesture(event.position[0], event.position[1]);
                 else
                 {
                     lastDragPosition = endDragPosition;
@@ -395,8 +415,17 @@ static void ProcessMotionEvent(GestureEvent event)
                 // If there is no more than two inputs
                 if (event.pointCount == 2)
                 {
-                    // Detect pinch delta
-                    pinchDelta = OnPinch();
+                    // Calculate distances
+                    float initialDistance = VectorDistance(firstInitialPinchPosition, secondInitialPinchPosition);
+                    float endDistance = VectorDistance(firstEndPinchPosition, secondEndPinchPosition);
+
+                    // Calculate Vectors
+                    Vector2 firstTouchVector = { firstEndPinchPosition.x - firstInitialPinchPosition.x, firstEndPinchPosition.y - firstInitialPinchPosition.y };
+                    Vector2 secondTouchVector = { secondEndPinchPosition.x - secondInitialPinchPosition.x, secondEndPinchPosition.y - secondInitialPinchPosition.y };
+
+                    // Detect the pinch gesture
+                    if (VectorDotProduct(firstTouchVector, secondTouchVector) < -0.5) pinchDelta = initialDistance - endDistance;
+                    else pinchDelta = 0;
                     
                     // Pinch gesture resolution
                     if (pinchDelta != 0)
@@ -422,36 +451,30 @@ static void ProcessMotionEvent(GestureEvent event)
     //--------------------------------------------------------------------
 }
 
-static float CalculateAngle(Vector2 initialPosition, Vector2 actualPosition, float magnitude)
+static float CalculateAngle(Vector2 initialPosition, Vector2 finalPosition, float magnitude)
 {
     float angle;
     
-    // Calculate arcsinus of the movement ( Our sinus is (actualPosition.y - initialPosition.y) / magnitude)
-    angle = asin((actualPosition.y - initialPosition.y) / magnitude);
+    // Calculate arcsinus of the movement
+    angle = asin((finalPosition.y - initialPosition.y)/magnitude);
     angle *= RAD2DEG;
     
     // Calculate angle depending on the sector
-    if (actualPosition.x - initialPosition.x >= 0)
+    if ((finalPosition.x - initialPosition.x) >= 0)
     {
         // Sector 4
-        if (actualPosition.y - initialPosition.y >= 0)
+        if ((finalPosition.y - initialPosition.y) >= 0)
         {
             angle *= -1;
             angle += 360;
         }
         // Sector 1
-        else
-        {
-            angle *= -1;
-        }
+        else angle *= -1;
     }
     else
     {
         // Sector 3
-        if (actualPosition.y - initialPosition.y >= 0)
-        {
-            angle += 180;
-        }
+        if ((finalPosition.y - initialPosition.y) >= 0) angle += 180;
         // Sector 2
         else
         {
@@ -463,31 +486,15 @@ static float CalculateAngle(Vector2 initialPosition, Vector2 actualPosition, flo
     return angle;
 }
 
-static float OnPinch()
-{
-    // Calculate distances
-    float initialDistance = Distance(firstInitialPinchPosition, secondInitialPinchPosition);
-    float endDistance = Distance(firstEndPinchPosition, secondEndPinchPosition);
-    
-    // Calculate Vectors
-    Vector2 firstTouchVector = { firstEndPinchPosition.x - firstInitialPinchPosition.x, firstEndPinchPosition.y - firstInitialPinchPosition.y };
-    Vector2 secondTouchVector = { secondEndPinchPosition.x - secondInitialPinchPosition.x, secondEndPinchPosition.y - secondInitialPinchPosition.y };
-    
-    // Detect the pinch gesture
-    // Calculate Distances
-    if (DotProduct(firstTouchVector, secondTouchVector) < -0.5) return initialDistance - endDistance;
-    else return 0;
-}
-
-static void SetDualInput(GestureEvent event)
+static void InitPinchGesture(Vector2 posA, Vector2 posB)
 {
     initialDragPosition = (Vector2){ 0, 0 };
     endDragPosition = (Vector2){ 0, 0 };
     lastDragPosition = (Vector2){ 0, 0 };
 
     // Initialize positions
-    firstInitialPinchPosition = event.position[0];
-    secondInitialPinchPosition = event.position[1];
+    firstInitialPinchPosition = posA;
+    secondInitialPinchPosition = posB;
     
     firstEndPinchPosition = firstInitialPinchPosition;
     secondEndPinchPosition = secondInitialPinchPosition;
@@ -500,7 +507,7 @@ static void SetDualInput(GestureEvent event)
     gestureType = TYPE_DUAL_INPUT;
 }
 
-static float Distance(Vector2 v1, Vector2 v2)
+static float VectorDistance(Vector2 v1, Vector2 v2)
 {
     float result;
 
@@ -512,7 +519,7 @@ static float Distance(Vector2 v1, Vector2 v2)
     return result;
 }
 
-static float DotProduct(Vector2 v1, Vector2 v2)
+static float VectorDotProduct(Vector2 v1, Vector2 v2)
 {
     float result;
 
@@ -569,7 +576,7 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
         //int32_t key = AKeyEvent_getKeyCode(event);
         //int32_t AKeyEvent_getMetaState(event);
         
-        int32_t code = AKeyEvent_getKeyCode((const AInputEvent *)event);
+        //int32_t code = AKeyEvent_getKeyCode((const AInputEvent *)event);
         
         // If we are in active mode, we eat the back button and move into pause mode.  
         // If we are already in pause mode, we allow the back button to be handled by the OS, which means we'll be shut down.
@@ -652,10 +659,4 @@ static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent
 
     return 1;
 }
-#endif
-
-
-
-
-
-
+#endif

+ 107 - 0
src/gestures.h

@@ -0,0 +1,107 @@
+/**********************************************************************************************
+*
+*   raylib Gestures System - Gestures Detection and Usage Functions (Android and HTML5)
+*
+*   Copyright (c) 2015 Marc Palau and Ramon Santamaria
+*
+*   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.
+*
+**********************************************************************************************/
+
+#ifndef GESTURES_H
+#define GESTURES_H
+
+#ifndef PI
+    #define PI 3.14159265358979323846
+#endif
+
+#define DEG2RAD (PI / 180.0f)
+#define RAD2DEG (180.0f / PI)
+
+//----------------------------------------------------------------------------------
+// Defines and Macros
+//----------------------------------------------------------------------------------
+//...
+
+//----------------------------------------------------------------------------------
+// Types and Structures Definition
+// NOTE: Below types are required for GESTURES_STANDALONE usage
+//----------------------------------------------------------------------------------
+#ifndef __cplusplus
+    // Boolean type
+    typedef enum { false, true } bool;
+#endif
+
+// Vector2 type
+typedef struct Vector2 {
+    float x;
+    float y;
+} Vector2;
+
+// Gestures type
+// NOTE: It could be used as flags to enable only some gestures
+typedef enum {
+    GESTURE_NONE        = 1,
+    GESTURE_TAP         = 2,
+    GESTURE_DOUBLETAP   = 4,
+    GESTURE_HOLD        = 8,
+    GESTURE_DRAG        = 16,
+    GESTURE_SWIPE_RIGHT = 32,
+    GESTURE_SWIPE_LEFT  = 64,
+    GESTURE_SWIPE_UP    = 128,
+    GESTURE_SWIPE_DOWN  = 256,
+    GESTURE_PINCH_IN    = 512,
+    GESTURE_PINCH_OUT   = 1024
+} Gestures;
+
+#ifdef __cplusplus
+extern "C" {            // Prevents name mangling of functions
+#endif
+
+//----------------------------------------------------------------------------------
+// Global Variables Definition
+//----------------------------------------------------------------------------------
+//...
+
+//----------------------------------------------------------------------------------
+// Module Functions Declaration
+//----------------------------------------------------------------------------------
+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)
+
+#if defined(PLATFORM_WEB)
+void InitGesturesSystem(void);                          // Init gestures system (web)
+#elif defined(PLATFORM_ANDROID)
+void InitGesturesSystem(struct android_app *app);       // Init gestures system (android)
+#endif
+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
+
+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
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // GESTURES_H