Skip to content

[rcore][web] Support mouse position update on HideCursor() #4955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/platforms/rcore_web.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
//----------------------------------------------------------------------------------
Expand Down Expand Up @@ -836,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;
Expand All @@ -860,8 +868,11 @@ void EnableCursor(void)

// Set cursor position in the middle
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)
Expand All @@ -870,10 +881,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: CORE.Input.Mouse.cursorHidden handled by EmscriptenPointerlockCallback()
// NOTE: lockedMouseCursor handled by EmscriptenPointerlockCallback()
}

// Swap back buffer with front buffer (screen drawing)
Expand Down Expand Up @@ -954,7 +968,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);
Expand Down Expand Up @@ -1588,13 +1602,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 (!CORE.Input.Mouse.cursorHidden)
{
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
Expand Down Expand Up @@ -1623,7 +1633,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;
Expand Down Expand Up @@ -1720,9 +1730,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;
Expand Down