From 23cd32ff45477cc35ef596bc29a3344d99a477b1 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Tue, 27 May 2025 17:05:07 -0400 Subject: [PATCH 1/4] replace cursorHidden checks with a new lockedMouseCursor check --- src/platforms/rcore_web.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/platforms/rcore_web.c b/src/platforms/rcore_web.c index bf5a6ba5a..1368af2fa 100644 --- a/src/platforms/rcore_web.c +++ b/src/platforms/rcore_web.c @@ -104,6 +104,9 @@ static const char cursorLUT[11][12] = { Vector2 lockedMousePos = { 0 }; +// an alternative might be to add CORE.Input.Mouse.cursorLocked to CoreData +static int lockedMouseCursor = 0; + //---------------------------------------------------------------------------------- // Module Internal Functions Declaration //---------------------------------------------------------------------------------- @@ -861,7 +864,7 @@ void EnableCursor(void) // Set cursor position in the middle SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); - // NOTE: CORE.Input.Mouse.cursorHidden handled by EmscriptenPointerlockCallback() + // NOTE: isMouseLocked handled by EmscriptenPointerlockCallback() } // Disables cursor (lock cursor) @@ -873,7 +876,7 @@ void DisableCursor(void) // Set cursor position in the middle SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); - // NOTE: CORE.Input.Mouse.cursorHidden handled by EmscriptenPointerlockCallback() + // NOTE: isMouseLocked handled by EmscriptenPointerlockCallback() } // Swap back buffer with front buffer (screen drawing) @@ -954,7 +957,7 @@ void SetMousePosition(int x, int y) CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y }; 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 glfwSetCursorPos(platform.handle, CORE.Input.Mouse.currentPosition.x, CORE.Input.Mouse.currentPosition.y); @@ -1589,7 +1592,7 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) { // If the pointer is not locked, follow the position - if (!CORE.Input.Mouse.cursorHidden) + if (!lockedMouseCursor) { CORE.Input.Mouse.currentPosition.x = (float)x; CORE.Input.Mouse.currentPosition.y = (float)y; @@ -1623,7 +1626,7 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) static EM_BOOL EmscriptenMouseMoveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) { // 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.y = lockedMousePos.y - mouseEvent->movementY; @@ -1720,9 +1723,9 @@ static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent // Register pointer lock events 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; CORE.Input.Mouse.previousPosition = lockedMousePos; From 04ac84f2998feb9153b5bb5d030f893047a52703 Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Thu, 29 May 2025 06:28:43 -0400 Subject: [PATCH 2/4] make behavior consistent with windows version --- src/platforms/rcore_web.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/platforms/rcore_web.c b/src/platforms/rcore_web.c index 1368af2fa..53d07f630 100644 --- a/src/platforms/rcore_web.c +++ b/src/platforms/rcore_web.c @@ -863,8 +863,11 @@ void EnableCursor(void) // Set cursor position in the middle SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); + + // Hide cursor + ShowCursor(); - // NOTE: isMouseLocked handled by EmscriptenPointerlockCallback() + // NOTE: lockedMouseCursor handled by EmscriptenPointerlockCallback() } // Disables cursor (lock cursor) @@ -873,10 +876,13 @@ void DisableCursor(void) // TODO: figure out how not to hard code the canvas ID here. emscripten_request_pointerlock(GetCanvasId(), 1); + // Hide cursor + HideCursor(); + // Set cursor position in the middle SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); - // NOTE: isMouseLocked handled by EmscriptenPointerlockCallback() + // NOTE: lockedMouseCursor handled by EmscriptenPointerlockCallback() } // Swap back buffer with front buffer (screen drawing) @@ -1591,13 +1597,9 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int // GLFW3 Cursor Position Callback, runs on mouse move static void MouseCursorPosCallback(GLFWwindow *window, double x, double y) { - // If the pointer is not locked, follow the position - if (!lockedMouseCursor) - { - CORE.Input.Mouse.currentPosition.x = (float)x; - 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) // Process mouse events as touches to be able to use mouse-gestures From 1ae7a89a42893bbfd3b319dd508db22ae2d3979f Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Thu, 29 May 2025 06:36:45 -0400 Subject: [PATCH 3/4] fix comment --- src/platforms/rcore_web.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platforms/rcore_web.c b/src/platforms/rcore_web.c index 53d07f630..50a2039fc 100644 --- a/src/platforms/rcore_web.c +++ b/src/platforms/rcore_web.c @@ -864,7 +864,7 @@ void EnableCursor(void) // Set cursor position in the middle SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); - // Hide cursor + // Show cursor ShowCursor(); // NOTE: lockedMouseCursor handled by EmscriptenPointerlockCallback() From 31229add28a9de3367a271086e36de78586e3d6e Mon Sep 17 00:00:00 2001 From: Moros Smith Date: Thu, 29 May 2025 06:37:09 -0400 Subject: [PATCH 4/4] come out of lock in ShowCursor --- src/platforms/rcore_web.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/platforms/rcore_web.c b/src/platforms/rcore_web.c index 50a2039fc..394efb466 100644 --- a/src/platforms/rcore_web.c +++ b/src/platforms/rcore_web.c @@ -839,6 +839,11 @@ void ShowCursor(void) { if (CORE.Input.Mouse.cursorHidden) { + if(lockedMouseCursor) + { + emscripten_exit_pointerlock(); + } + EM_ASM( { Module.canvas.style.cursor = UTF8ToString($0); }, cursorLUT[CORE.Input.Mouse.cursor]); CORE.Input.Mouse.cursorHidden = false;