diff --git a/engine/src/main/java/org/terasology/engine/input/lwjgl/LwjglMouseDevice.java b/engine/src/main/java/org/terasology/engine/input/lwjgl/LwjglMouseDevice.java index 18b061757f6..104baa6b228 100644 --- a/engine/src/main/java/org/terasology/engine/input/lwjgl/LwjglMouseDevice.java +++ b/engine/src/main/java/org/terasology/engine/input/lwjgl/LwjglMouseDevice.java @@ -50,6 +50,7 @@ public LwjglMouseDevice(RenderingConfig renderingConfig) { public void registerToLwjglWindow(long window) { GLFW.glfwSetMouseButtonCallback(window, this::mouseButtonCallback); GLFW.glfwSetScrollCallback(window, this::scrollCallback); + GLFW.glfwSetWindowFocusCallback(window, this::focusCallback); } @Override @@ -59,12 +60,6 @@ public void update() { DoubleBuffer mouseY = BufferUtils.createDoubleBuffer(1); GLFW.glfwGetCursorPos(window, mouseX, mouseY); - mouseX.rewind(); - mouseY.rewind(); - - GLFW.glfwGetCursorPos(window, mouseX, mouseY); - mouseX.rewind(); - mouseY.rewind(); double x = mouseX.get(0); double y = mouseY.get(0); @@ -82,7 +77,6 @@ public Vector2i getPosition() { @Override public Vector2d getDelta() { - Vector2d result = new Vector2d(xposDelta, yposDelta); return result; } @@ -123,6 +117,21 @@ public void propertyChange(PropertyChangeEvent evt) { } } + private void focusCallback(long window, boolean isFocused) { + // When the window is unfocused and focused again, the mouse coordinates + // reported by GLFW change even if the mouse hasn't moved. + // So when the window regains focus, we set the new position as the reference without generating deltas. + if (isFocused) { + DoubleBuffer mouseX = BufferUtils.createDoubleBuffer(1); + DoubleBuffer mouseY = BufferUtils.createDoubleBuffer(1); + + GLFW.glfwGetCursorPos(window, mouseX, mouseY); + + xpos = mouseX.get(0); + ypos = mouseY.get(0); + } + } + private void mouseButtonCallback(long window, int button, int action, int mods) { ButtonState state; if (action == GLFW.GLFW_PRESS) {