REVIEWED: Mouse and Touch functions generic to all platforms #3313

This commit is contained in:
Ray 2023-10-11 11:36:44 +02:00
parent 6ebfec99c5
commit 0d175a69ae
6 changed files with 68 additions and 288 deletions

View File

@ -2113,11 +2113,7 @@ float GetGamepadAxisMovement(int gamepad, int axis)
//----------------------------------------------------------------------------------
// NOTE: Functions with a platform-specific implementation on rcore_<platform>.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_<platform>.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)

View File

@ -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;
}

View File

@ -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

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{