From 0d175a69ae1e7072e05587b5f7505bac0c07b4ce Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 11 Oct 2023 11:36:44 +0200 Subject: [PATCH] REVIEWED: Mouse and Touch functions generic to all platforms #3313 --- src/rcore.c | 65 ++++++++++++++++++++++++++++++++++++------ src/rcore_android.c | 53 +++-------------------------------- src/rcore_desktop.c | 67 +++++--------------------------------------- src/rcore_drm.c | 57 ------------------------------------- src/rcore_template.c | 55 ------------------------------------ src/rcore_web.c | 59 -------------------------------------- 6 files changed, 68 insertions(+), 288 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index 2e6d3213..595cbfe0 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2113,11 +2113,7 @@ float GetGamepadAxisMovement(int gamepad, int axis) //---------------------------------------------------------------------------------- // NOTE: Functions with a platform-specific implementation on rcore_.c -//int GetMouseX(void) ** -//int GetMouseY(void) ** -//Vector2 GetMousePosition(void) ** //void SetMousePosition(int x, int y) -//float GetMouseWheelMove(void) ** //void SetMouseCursor(int cursor) // Check if a mouse button has been pressed once @@ -2172,6 +2168,29 @@ bool IsMouseButtonUp(int button) return up; } +// Get mouse position X +int GetMouseX(void) +{ + return (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); +} + +// Get mouse position Y +int GetMouseY(void) +{ + return (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); +} + +// Get mouse position XY +Vector2 GetMousePosition(void) +{ + Vector2 position = { 0 }; + + position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; + position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; + + return position; +} + // Get mouse delta between frames Vector2 GetMouseDelta(void) { @@ -2197,6 +2216,17 @@ void SetMouseScale(float scaleX, float scaleY) CORE.Input.Mouse.scale = (Vector2){ scaleX, scaleY }; } +// Get mouse wheel movement Y +float GetMouseWheelMove(void) +{ + float result = 0.0f; + + if (fabsf(CORE.Input.Mouse.currentWheelMove.x) > fabsf(CORE.Input.Mouse.currentWheelMove.y)) result = (float)CORE.Input.Mouse.currentWheelMove.x; + else result = (float)CORE.Input.Mouse.currentWheelMove.y; + + return result; +} + // Get mouse wheel movement X/Y as a vector Vector2 GetMouseWheelMoveV(void) { @@ -2211,10 +2241,29 @@ Vector2 GetMouseWheelMoveV(void) // Module Functions Definition: Input Handling: Touch //---------------------------------------------------------------------------------- -// NOTE: Functions with a platform-specific implementation on rcore_.c -//int GetTouchX(void) -//int GetTouchY(void) -//Vector2 GetTouchPosition(int index) +// Get touch position X for touch point 0 (relative to screen size) +int GetTouchX(void) +{ + return (int)CORE.Input.Touch.position[0].x; +} + +// Get touch position Y for touch point 0 (relative to screen size) +int GetTouchY(void) +{ + return (int)CORE.Input.Touch.position[0].y; +} + +// Get touch position XY for a touch point index (relative to screen size) +// TODO: Touch position should be scaled depending on display size and render size +Vector2 GetTouchPosition(int index) +{ + Vector2 position = { -1.0f, -1.0f }; + + if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index]; + else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS); + + return position; +} // Get touch point identifier for given index int GetTouchPointId(int index) diff --git a/src/rcore_android.c b/src/rcore_android.c index d9f72894..1921ed79 100644 --- a/src/rcore_android.c +++ b/src/rcore_android.c @@ -642,24 +642,6 @@ int SetGamepadMappings(const char *mappings) return 0; } -// Get mouse position X -int GetMouseX(void) -{ - return (int)CORE.Input.Touch.position[0].x; -} - -// Get mouse position Y -int GetMouseY(void) -{ - return (int)CORE.Input.Touch.position[0].y; -} - -// Get mouse position XY -Vector2 GetMousePosition(void) -{ - return GetTouchPosition(0); -} - // Set mouse position XY void SetMousePosition(int x, int y) { @@ -667,43 +649,12 @@ void SetMousePosition(int x, int y) CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; } -// Get mouse wheel movement Y -float GetMouseWheelMove(void) -{ - TRACELOG(LOG_WARNING, "GetMouseWheelMove() not implemented on target platform"); - return 0.0f; -} - // Set mouse cursor void SetMouseCursor(int cursor) { TRACELOG(LOG_WARNING, "SetMouseCursor() not implemented on target platform"); } -// Get touch position X for touch point 0 (relative to screen size) -int GetTouchX(void) -{ - return (int)CORE.Input.Touch.position[0].x; -} - -// Get touch position Y for touch point 0 (relative to screen size) -int GetTouchY(void) -{ - return (int)CORE.Input.Touch.position[0].y; -} - -// Get touch position XY for a touch point index (relative to screen size) -// TODO: Touch position should be scaled depending on display size and render size -Vector2 GetTouchPosition(int index) -{ - Vector2 position = { -1.0f, -1.0f }; - - if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index]; - else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS); - - return position; -} - // Register all input events void PollInputEvents(void) { @@ -1247,6 +1198,10 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event) if (CORE.Input.Touch.pointCount > 0) CORE.Input.Touch.currentTouchState[MOUSE_BUTTON_LEFT] = 1; else CORE.Input.Touch.currentTouchState[MOUSE_BUTTON_LEFT] = 0; + + // Map touch[0] as mouse input for convenience + CORE.Input.Mouse.currentPosition = CORE.Input.Touch.position[0]; + CORE.Input.Mouse.currentWheelMove = (Vector2){ 0.0f, 0.0f }; return 0; } diff --git a/src/rcore_desktop.c b/src/rcore_desktop.c index eaef9517..8c4b73c8 100644 --- a/src/rcore_desktop.c +++ b/src/rcore_desktop.c @@ -1228,29 +1228,6 @@ int SetGamepadMappings(const char *mappings) return glfwUpdateGamepadMappings(mappings); } -// Get mouse position X -int GetMouseX(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); -} - -// Get mouse position Y -int GetMouseY(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); -} - -// Get mouse position XY -Vector2 GetMousePosition(void) -{ - Vector2 position = { 0 }; - - position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; - position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; - - return position; -} - // Set mouse position XY void SetMousePosition(int x, int y) { @@ -1261,17 +1238,6 @@ void SetMousePosition(int x, int y) glfwSetCursorPos(platform.handle, CORE.Input.Mouse.currentPosition.x, CORE.Input.Mouse.currentPosition.y); } -// Get mouse wheel movement Y -float GetMouseWheelMove(void) -{ - float result = 0.0f; - - if (fabsf(CORE.Input.Mouse.currentWheelMove.x) > fabsf(CORE.Input.Mouse.currentWheelMove.y)) result = (float)CORE.Input.Mouse.currentWheelMove.x; - else result = (float)CORE.Input.Mouse.currentWheelMove.y; - - return result; -} - // Set mouse cursor void SetMouseCursor(int cursor) { @@ -1284,32 +1250,6 @@ void SetMouseCursor(int cursor) } } -// Get touch position X for touch point 0 (relative to screen size) -int GetTouchX(void) -{ - return GetMouseX(); -} - -// Get touch position Y for touch point 0 (relative to screen size) -int GetTouchY(void) -{ - return GetMouseY(); -} - -// Get touch position XY for a touch point index (relative to screen size) -// TODO: Touch position should be scaled depending on display size and render size -Vector2 GetTouchPosition(int index) -{ - Vector2 position = { -1.0f, -1.0f }; - - // TODO: GLFW does not support multi-touch input just yet - // https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch - // https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages - if (index == 0) position = GetMousePosition(); - - return position; -} - // Register all input events void PollInputEvents(void) { @@ -1352,6 +1292,13 @@ void PollInputEvents(void) // Reset touch positions //for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.position[i] = (Vector2){ 0, 0 }; + + // Map touch position to mouse position for convenience + // WARNING: If the target desktop device supports touch screen, this behavious should be reviewed! + // TODO: GLFW does not support multi-touch input just yet + // https://www.codeproject.com/Articles/668404/Programming-for-Multi-Touch + // https://docs.microsoft.com/en-us/windows/win32/wintouch/getting-started-with-multi-touch-messages + CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition; // Check if gamepads are ready // NOTE: We do it here in case of disconnection diff --git a/src/rcore_drm.c b/src/rcore_drm.c index 13d41bbb..839edeb4 100644 --- a/src/rcore_drm.c +++ b/src/rcore_drm.c @@ -746,29 +746,6 @@ int SetGamepadMappings(const char *mappings) return 0; } -// Get mouse position X -int GetMouseX(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); -} - -// Get mouse position Y -int GetMouseY(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); -} - -// Get mouse position XY -Vector2 GetMousePosition(void) -{ - Vector2 position = { 0 }; - - position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; - position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; - - return position; -} - // Set mouse position XY void SetMousePosition(int x, int y) { @@ -776,46 +753,12 @@ void SetMousePosition(int x, int y) CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; } -// Get mouse wheel movement Y -float GetMouseWheelMove(void) -{ - float result = 0.0f; - - if (fabsf(CORE.Input.Mouse.currentWheelMove.x) > fabsf(CORE.Input.Mouse.currentWheelMove.y)) result = (float)CORE.Input.Mouse.currentWheelMove.x; - else result = (float)CORE.Input.Mouse.currentWheelMove.y; - - return result; -} - // Set mouse cursor void SetMouseCursor(int cursor) { TRACELOG(LOG_WARNING, "SetMouseCursor() not implemented on target platform"); } -// Get touch position X for touch point 0 (relative to screen size) -int GetTouchX(void) -{ - return GetMouseX(); -} - -// Get touch position Y for touch point 0 (relative to screen size) -int GetTouchY(void) -{ - return GetMouseY(); -} - -// Get touch position XY for a touch point index (relative to screen size) -Vector2 GetTouchPosition(int index) -{ - Vector2 position = { -1.0f, -1.0f }; - - if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index]; - else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS); - - return position; -} - // Register all input events void PollInputEvents(void) { diff --git a/src/rcore_template.c b/src/rcore_template.c index 68a43b2c..b6f0ff97 100644 --- a/src/rcore_template.c +++ b/src/rcore_template.c @@ -570,30 +570,6 @@ int SetGamepadMappings(const char *mappings) return 0; } -// Get mouse position X -int GetMouseX(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); -} - -// Get mouse position Y -int GetMouseY(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); -} - -// Get mouse position XY -Vector2 GetMousePosition(void) -{ - Vector2 position = { 0 }; - - // NOTE: On canvas scaling, mouse position is proportionally returned - position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; - position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; - - return position; -} - // Set mouse position XY void SetMousePosition(int x, int y) { @@ -601,43 +577,12 @@ void SetMousePosition(int x, int y) CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; } -// Get mouse wheel movement Y -float GetMouseWheelMove(void) -{ - TRACELOG(LOG_WARNING, "GetMouseWheelMove() not implemented on target platform"); - return 0.0f; -} - // Set mouse cursor void SetMouseCursor(int cursor) { TRACELOG(LOG_WARNING, "SetMouseCursor() not implemented on target platform"); } -// Get touch position X for touch point 0 (relative to screen size) -int GetTouchX(void) -{ - return (int)CORE.Input.Touch.position[0].x; -} - -// Get touch position Y for touch point 0 (relative to screen size) -int GetTouchY(void) -{ - return (int)CORE.Input.Touch.position[0].y; -} - -// Get touch position XY for a touch point index (relative to screen size) -// TODO: Touch position should be scaled depending on display size and render size -Vector2 GetTouchPosition(int index) -{ - Vector2 position = { -1.0f, -1.0f }; - - if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index]; - else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS); - - return position; -} - // Register all input events void PollInputEvents(void) { diff --git a/src/rcore_web.c b/src/rcore_web.c index 00691e43..d6af5a6d 100644 --- a/src/rcore_web.c +++ b/src/rcore_web.c @@ -705,30 +705,6 @@ int SetGamepadMappings(const char *mappings) return 0; } -// Get mouse position X -int GetMouseX(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); -} - -// Get mouse position Y -int GetMouseY(void) -{ - return (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); -} - -// Get mouse position XY -Vector2 GetMousePosition(void) -{ - Vector2 position = { 0 }; - - // NOTE: On canvas scaling, mouse position is proportionally returned - position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; - position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; - - return position; -} - // Set mouse position XY void SetMousePosition(int x, int y) { @@ -739,47 +715,12 @@ void SetMousePosition(int x, int y) glfwSetCursorPos(platform.handle, CORE.Input.Mouse.currentPosition.x, CORE.Input.Mouse.currentPosition.y); } -// Get mouse wheel movement Y -float GetMouseWheelMove(void) -{ - float result = 0.0f; - - if (fabsf(CORE.Input.Mouse.currentWheelMove.x) > fabsf(CORE.Input.Mouse.currentWheelMove.y)) result = (float)CORE.Input.Mouse.currentWheelMove.x; - else result = (float)CORE.Input.Mouse.currentWheelMove.y; - - return result; -} - // Set mouse cursor void SetMouseCursor(int cursor) { TRACELOG(LOG_INFO, "SetMouseCursor not implemented in rcore_web.c"); } -// Get touch position X for touch point 0 (relative to screen size) -int GetTouchX(void) -{ - return (int)CORE.Input.Touch.position[0].x; -} - -// Get touch position Y for touch point 0 (relative to screen size) -int GetTouchY(void) -{ - return (int)CORE.Input.Touch.position[0].y; -} - -// Get touch position XY for a touch point index (relative to screen size) -// TODO: Touch position should be scaled depending on display size and render size -Vector2 GetTouchPosition(int index) -{ - Vector2 position = { -1.0f, -1.0f }; - - if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index]; - else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS); - - return position; -} - // Register all input events void PollInputEvents(void) {