From 2bc7c82fc14c38cc21d3689a54ed013f0a202a43 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 10 Aug 2019 14:31:19 -0400 Subject: [PATCH] Ignore mouseDelta (set to zero) after locking and unlocking cursor. --- src/window.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/window.cpp b/src/window.cpp index 4759e7e5..d606be5c 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -93,6 +93,8 @@ struct Window::Internal { int lastWindowY = 0; int lastWindowWidth = 0; int lastWindowHeight = 0; + + bool ignoreNextMouseDelta = false; }; @@ -123,6 +125,13 @@ static void cursorPosCallback(GLFWwindow* win, double xpos, double ypos) { math::Vec mousePos = math::Vec(xpos, ypos).div(window->pixelRatio / window->windowRatio).round(); math::Vec mouseDelta = mousePos.minus(window->mousePos); + // Workaround for GLFW warping mouse to a different position when the cursor is locked or unlocked. + if (window->internal->ignoreNextMouseDelta) { + window->internal->ignoreNextMouseDelta = false; + mouseDelta = math::Vec(); + } + DEBUG("(%f %f) (%f %f)", VEC_ARGS(mouseDelta), VEC_ARGS(mousePos)); + int cursorMode = glfwGetInputMode(win, GLFW_CURSOR); (void) cursorMode; @@ -472,12 +481,14 @@ void Window::cursorLock() { #else glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); #endif + internal->ignoreNextMouseDelta = true; } } void Window::cursorUnlock() { if (settings::allowCursorLock) { glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + internal->ignoreNextMouseDelta = true; } }