diff --git a/src/core.c b/src/core.c index a31e1007..f1016c36 100644 --- a/src/core.c +++ b/src/core.c @@ -426,6 +426,8 @@ static int gamepadStream[MAX_GAMEPADS] = { -1 };// Gamepad device file descripto static pthread_t gamepadThreadId; // Gamepad reading thread id static char gamepadName[64]; // Gamepad name holder #endif + +bool touchDetected = false; //----------------------------------------------------------------------------------- // Timming system variables @@ -2427,12 +2429,11 @@ bool IsMouseButtonPressed(int button) if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 1)) pressed = true; #endif -/* + #if defined(PLATFORM_WEB) - Vector2 pos = GetTouchPosition(0); - if ((pos.x > 0) && (pos.y > 0)) pressed = true; // There was a touch! + if (IsTouchDetected()) pressed = true; // There was a touch! #endif -*/ + return pressed; } @@ -2504,14 +2505,11 @@ Vector2 GetMousePosition(void) #else position = (Vector2){ (mousePosition.x + mouseOffset.x)*mouseScale.x, (mousePosition.y + mouseOffset.y)*mouseScale.y }; #endif -/* -#if defined(PLATFORM_WEB) - Vector2 pos = GetTouchPosition(0); - // Touch position has priority over mouse position - if ((pos.x > 0) && (pos.y > 0)) position = pos; // There was a touch! +#if defined(PLATFORM_WEB) + if (IsTouchDetected()) position = GetTouchPosition(0); #endif -*/ + return position; } @@ -2558,6 +2556,16 @@ int GetMouseWheelMove(void) #endif } +// Detect if a touch has happened +bool IsTouchDetected(void) +{ +#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) + return touchDetected; +#else // PLATFORM_DESKTOP, PLATFORM_RPI + return false; +#endif +} + // Returns touch position X for touch point 0 (relative to screen size) int GetTouchX(void) { @@ -3718,6 +3726,8 @@ static void PollInputEvents(void) previousMouseWheelY = currentMouseWheelY; currentMouseWheelY = 0; + + touchDetected = false; #endif #if defined(PLATFORM_DESKTOP) @@ -4362,6 +4372,7 @@ static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent // Register touch input events static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData) { + touchDetected = true; /* for (int i = 0; i < touchEvent->numTouches; i++) { diff --git a/src/raylib.h b/src/raylib.h index 946e71a2..2c2d7246 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1013,6 +1013,7 @@ RLAPI void SetMouseScale(float scaleX, float scaleY); // Set mouse scali RLAPI int GetMouseWheelMove(void); // Returns mouse wheel movement Y // Input-related functions: touch +RLAPI bool IsTouchDetected(void); // Detect if a touch has happened RLAPI int GetTouchX(void); // Returns touch position X for touch point 0 (relative to screen size) RLAPI int GetTouchY(void); // Returns touch position Y for touch point 0 (relative to screen size) RLAPI Vector2 GetTouchPosition(int index); // Returns touch position XY for a touch point index (relative to screen size)