This commit is contained in:
Moros Smith 2025-06-02 15:31:29 -05:00 committed by GitHub
commit 034195e05d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -104,6 +104,9 @@ static const char cursorLUT[11][12] = {
Vector2 lockedMousePos = { 0 }; Vector2 lockedMousePos = { 0 };
// an alternative might be to add CORE.Input.Mouse.cursorLocked to CoreData
static int lockedMouseCursor = 0;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Internal Functions Declaration // Module Internal Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -836,6 +839,11 @@ void ShowCursor(void)
{ {
if (CORE.Input.Mouse.cursorHidden) if (CORE.Input.Mouse.cursorHidden)
{ {
if(lockedMouseCursor)
{
emscripten_exit_pointerlock();
}
EM_ASM( { Module.canvas.style.cursor = UTF8ToString($0); }, cursorLUT[CORE.Input.Mouse.cursor]); EM_ASM( { Module.canvas.style.cursor = UTF8ToString($0); }, cursorLUT[CORE.Input.Mouse.cursor]);
CORE.Input.Mouse.cursorHidden = false; CORE.Input.Mouse.cursorHidden = false;
@ -860,8 +868,11 @@ void EnableCursor(void)
// Set cursor position in the middle // Set cursor position in the middle
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
// Show cursor
ShowCursor();
// NOTE: CORE.Input.Mouse.cursorHidden handled by EmscriptenPointerlockCallback() // NOTE: lockedMouseCursor handled by EmscriptenPointerlockCallback()
} }
// Disables cursor (lock cursor) // Disables cursor (lock cursor)
@ -870,10 +881,13 @@ void DisableCursor(void)
// TODO: figure out how not to hard code the canvas ID here. // TODO: figure out how not to hard code the canvas ID here.
emscripten_request_pointerlock(GetCanvasId(), 1); emscripten_request_pointerlock(GetCanvasId(), 1);
// Hide cursor
HideCursor();
// Set cursor position in the middle // Set cursor position in the middle
SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
// NOTE: CORE.Input.Mouse.cursorHidden handled by EmscriptenPointerlockCallback() // NOTE: lockedMouseCursor handled by EmscriptenPointerlockCallback()
} }
// Swap back buffer with front buffer (screen drawing) // Swap back buffer with front buffer (screen drawing)
@ -954,7 +968,7 @@ void SetMousePosition(int x, int y)
CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y }; CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition; CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
if (CORE.Input.Mouse.cursorHidden) lockedMousePos = CORE.Input.Mouse.currentPosition; if (lockedMouseCursor) lockedMousePos = CORE.Input.Mouse.currentPosition;
// NOTE: emscripten not implemented // NOTE: emscripten not implemented
glfwSetCursorPos(platform.handle, CORE.Input.Mouse.currentPosition.x, CORE.Input.Mouse.currentPosition.y); glfwSetCursorPos(platform.handle, CORE.Input.Mouse.currentPosition.x, CORE.Input.Mouse.currentPosition.y);
@ -1588,13 +1602,9 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
// GLFW3 Cursor Position Callback, runs on mouse move // GLFW3 Cursor Position Callback, runs on mouse move
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
{ {
// If the pointer is not locked, follow the position CORE.Input.Mouse.currentPosition.x = (float)x;
if (!CORE.Input.Mouse.cursorHidden) CORE.Input.Mouse.currentPosition.y = (float)y;
{ CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
CORE.Input.Mouse.currentPosition.x = (float)x;
CORE.Input.Mouse.currentPosition.y = (float)y;
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;
}
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES) #if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
// Process mouse events as touches to be able to use mouse-gestures // Process mouse events as touches to be able to use mouse-gestures
@ -1623,7 +1633,7 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
static EM_BOOL EmscriptenMouseMoveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) static EM_BOOL EmscriptenMouseMoveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
{ {
// To emulate the GLFW_RAW_MOUSE_MOTION property. // To emulate the GLFW_RAW_MOUSE_MOTION property.
if (CORE.Input.Mouse.cursorHidden) if (lockedMouseCursor)
{ {
CORE.Input.Mouse.previousPosition.x = lockedMousePos.x - mouseEvent->movementX; CORE.Input.Mouse.previousPosition.x = lockedMousePos.x - mouseEvent->movementX;
CORE.Input.Mouse.previousPosition.y = lockedMousePos.y - mouseEvent->movementY; CORE.Input.Mouse.previousPosition.y = lockedMousePos.y - mouseEvent->movementY;
@ -1720,9 +1730,9 @@ static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent
// Register pointer lock events // Register pointer lock events
static EM_BOOL EmscriptenPointerlockCallback(int eventType, const EmscriptenPointerlockChangeEvent *pointerlockChangeEvent, void *userData) static EM_BOOL EmscriptenPointerlockCallback(int eventType, const EmscriptenPointerlockChangeEvent *pointerlockChangeEvent, void *userData)
{ {
CORE.Input.Mouse.cursorHidden = EM_ASM_INT( { if (document.pointerLockElement) return 1; }, 0); lockedMouseCursor = EM_ASM_INT( { if (document.pointerLockElement) return 1; }, 0);
if (CORE.Input.Mouse.cursorHidden) if (lockedMouseCursor)
{ {
lockedMousePos = CORE.Input.Mouse.currentPosition; lockedMousePos = CORE.Input.Mouse.currentPosition;
CORE.Input.Mouse.previousPosition = lockedMousePos; CORE.Input.Mouse.previousPosition = lockedMousePos;