From ff7560b3ba14377fb951b9c59c60a791d405a5fe Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Mon, 30 Nov 2026 21:33:07 +0100 Subject: [PATCH 1/2] Fix iOS mouse coordinate scaling --- .../com/jme3/input/ios/IosInputHandler.java | 32 +++++--- .../com/jme3/system/ios/IGLESContext.java | 30 ++++--- .../input/ios/MouseInputPrecisionTest.java | 78 +++++++++++++++++++ 3 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java diff --git a/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java b/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java index 5a7f9164798..6ed10c0f38f 100644 --- a/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java +++ b/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java @@ -35,6 +35,8 @@ public class IosInputHandler implements TouchInput { private IosTouchHandler touchHandler; private float scaleX = 1f; private float scaleY = 1f; + private float mouseScaleX = 1f; + private float mouseScaleY = 1f; private int toJmeMouseButton(int sdlButton) { switch (sdlButton) { @@ -172,6 +174,20 @@ public void setFramebufferSize(int width, int height) { } } + /** + * Updates the input dimensions and the scale from SDL window coordinates to jME coordinates. + * + * @param width the jME input width + * @param height the jME input height + * @param windowWidth the SDL window width + * @param windowHeight the SDL window height + */ + public void setInputSize(int width, int height, int windowWidth, int windowHeight) { + setFramebufferSize(width, height); + mouseScaleX = this.width > 0 && windowWidth > 0 ? (float) this.width / windowWidth : 1f; + mouseScaleY = this.height > 0 && windowHeight > 0 ? (float) this.height / windowHeight : 1f; + } + public boolean isMouseEventsInvertX() { return mouseEventsInvertX; } @@ -318,21 +334,13 @@ private float nativeY(float value) { return isNormalized(value) ? value * Math.max(height, 1) : value; } - private float nativeDeltaX(float value) { - return Math.abs(value) <= 1f ? value * Math.max(width, 1) : value; - } - - private float nativeDeltaY(float value) { - return Math.abs(value) <= 1f ? value * Math.max(height, 1) : value; - } - private float mouseX(float value) { - float x = nativeX(value); + float x = value * mouseScaleX; return mouseEventsInvertX ? invertX(x) : x; } private float mouseY(float value) { - float y = invertY(nativeY(value)); + float y = invertY(value * mouseScaleY); return mouseEventsInvertY ? invertY(y) : y; } @@ -341,12 +349,12 @@ private float touchY(float value) { } private float mouseDeltaX(float value) { - float deltaX = nativeDeltaX(value); + float deltaX = value * mouseScaleX; return mouseEventsInvertX ? -deltaX : deltaX; } private float mouseDeltaY(float value) { - float deltaY = -nativeDeltaY(value); + float deltaY = -value * mouseScaleY; return mouseEventsInvertY ? -deltaY : deltaY; } diff --git a/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java b/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java index 1c9188eb92c..1336f7831b2 100644 --- a/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java +++ b/jme3-ios/src/main/java/com/jme3/system/ios/IGLESContext.java @@ -348,9 +348,6 @@ public void resizeFramebuffer(int width, int height) { settings.setResolution(logicalWidth, logicalHeight); settings.setWindowSize(logicalWidth, logicalHeight); } - if (input != null) { - input.setFramebufferSize(logicalWidth, logicalHeight); - } if (framebufferWidth != oldWidth || framebufferHeight != oldHeight || logicalWidth != oldLogicalWidth || logicalHeight != oldLogicalHeight || displayScaleModeChanged) { @@ -369,12 +366,6 @@ private void updateLogicalSize() { displayScale.set(scale, scale); appliedDisplayScaleMode = settings.getDisplayScaleMode(); - if (DisplayScaleUtils.isNativePixelsMode(appliedDisplayScaleMode)) { - logicalWidth = Math.max(framebufferWidth, 1); - logicalHeight = Math.max(framebufferHeight, 1); - return; - } - int windowWidth = LibJGLIOSEglBridge.windowWidth(); int windowHeight = LibJGLIOSEglBridge.windowHeight(); if (windowWidth <= 0) { @@ -384,11 +375,19 @@ private void updateLogicalSize() { windowHeight = Math.max(Math.round(framebufferHeight / scale), 1); } - int[] logicalSize = DisplayScaleUtils.resolveLogicalSize(AppSettings.DISPLAY_SCALE_DPI_AWARE, - windowWidth, windowHeight, - framebufferWidth, framebufferHeight, displayScale.x, displayScale.y); - logicalWidth = logicalSize[0]; - logicalHeight = logicalSize[1]; + if (DisplayScaleUtils.isNativePixelsMode(appliedDisplayScaleMode)) { + logicalWidth = Math.max(framebufferWidth, 1); + logicalHeight = Math.max(framebufferHeight, 1); + } else { + int[] logicalSize = DisplayScaleUtils.resolveLogicalSize(AppSettings.DISPLAY_SCALE_DPI_AWARE, + windowWidth, windowHeight, + framebufferWidth, framebufferHeight, displayScale.x, displayScale.y); + logicalWidth = logicalSize[0]; + logicalHeight = logicalSize[1]; + } + if (input != null) { + input.setInputSize(logicalWidth, logicalHeight, windowWidth, windowHeight); + } } private void applyDisplayScaleModeIfNeeded() { @@ -399,9 +398,6 @@ private void applyDisplayScaleModeIfNeeded() { updateLogicalSize(); settings.setResolution(logicalWidth, logicalHeight); settings.setWindowSize(logicalWidth, logicalHeight); - if (input != null) { - input.setFramebufferSize(logicalWidth, logicalHeight); - } linearFrameBufferDirty = true; if (renderable.get() && listener != null) { listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); diff --git a/jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java b/jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java new file mode 100644 index 00000000000..345992fff55 --- /dev/null +++ b/jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2009-2026 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.input.ios; + +import com.jme3.input.RawInputListenerAdapter; +import com.jme3.input.event.MouseButtonEvent; +import com.jme3.input.event.MouseMotionEvent; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.ngengine.libjglios.core.LibJGLIOSInputBridge; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MouseInputPrecisionTest { + + @Test + void shouldScaleNativeMouseCoordinates() { + IosInputHandler input = new IosInputHandler(); + input.setInputSize(1000, 800, 500, 400); + List motion = new ArrayList<>(); + List buttons = new ArrayList<>(); + input.setInputListener(new RawInputListenerAdapter() { + @Override + public void onMouseMotionEvent(MouseMotionEvent event) { + motion.add(event); + } + + @Override + public void onMouseButtonEvent(MouseButtonEvent event) { + buttons.add(event); + } + }); + input.dispatchBridgeEvent(new int[]{LibJGLIOSInputBridge.EVENT_MOUSE_MOTION}, + new float[]{0.75f, 0.75f, 0.5f, -0.5f}, 1); + input.dispatchBridgeEvent(new int[]{LibJGLIOSInputBridge.EVENT_MOUSE_BUTTON, 1, 1}, + new float[]{0.75f, 0.75f}, 2); + input.dispatchQueuedEvents(); + + assertEquals(1, motion.size()); + assertEquals(1, motion.get(0).getDX()); + assertEquals(1, motion.get(0).getDY()); + assertEquals(2, motion.get(0).getX()); + assertEquals(799, motion.get(0).getY()); + assertEquals(1, buttons.size()); + assertEquals(motion.get(0).getX(), buttons.get(0).getX()); + assertEquals(motion.get(0).getY(), buttons.get(0).getY()); + } +} From fcd23b384ff9e1a4781afabe4fb4749092323189 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Mon, 30 Nov 2026 21:33:07 +0100 Subject: [PATCH 2/2] Preserve fractional mouse motion --- jme3-android/build.gradle | 2 +- .../input/android/AndroidMouseInput14.java | 12 ++- .../input/android/AndroidMouseInput26.java | 24 ++++- .../jme3/input/android/AndroidTouchInput.java | 32 ++---- .../android/MouseInputPrecisionTest.java | 102 ++++++++++++++++++ .../jme3/input/MouseMotionAccumulator.java | 80 ++++++++++++++ .../com/jme3/input/ios/IosInputHandler.java | 20 +++- .../com/jme3/input/ios/IosTouchHandler.java | 31 ++---- .../input/ios/MouseInputPrecisionTest.java | 45 ++++++-- .../com/jme3/input/lwjgl/SdlMouseInput.java | 11 +- .../jme3/input/lwjgl/SdlMouseInputTest.java | 66 ++++++++++++ 11 files changed, 365 insertions(+), 60 deletions(-) create mode 100644 jme3-android/src/test/java/com/jme3/input/android/MouseInputPrecisionTest.java create mode 100644 jme3-core/src/main/java/com/jme3/input/MouseMotionAccumulator.java diff --git a/jme3-android/build.gradle b/jme3-android/build.gradle index f33d4c36849..4dda1c362a9 100644 --- a/jme3-android/build.gradle +++ b/jme3-android/build.gradle @@ -15,7 +15,7 @@ dependencies { api project(':jme3-core') implementation libs.jme3.android.natives compileOnly files(rootProject.file('lib/android.jar')) - testRuntimeOnly files(rootProject.file('lib/android.jar')) + testImplementation files(rootProject.file('lib/android.jar')) } compileJava { diff --git a/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput14.java b/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput14.java index 9e76171dfca..373b9d89c39 100644 --- a/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput14.java +++ b/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput14.java @@ -133,11 +133,19 @@ public AndroidMouseInput14(AndroidInputHandler inputHandler) { } protected int getJmeX(float origX) { - return (int) (origX * scaleX); + return (int) scaleMouseX(origX); } protected int getJmeY(float origY) { - return (int) (origY * scaleY); + return (int) scaleMouseY(origY); + } + + protected float scaleMouseX(float origX) { + return origX * scaleX; + } + + protected float scaleMouseY(float origY) { + return origY * scaleY; } public void loadSettings(AppSettings settings) { diff --git a/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput26.java b/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput26.java index 34240f01041..225cb063b50 100644 --- a/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput26.java +++ b/jme3-android/src/main/java/com/jme3/input/android/AndroidMouseInput26.java @@ -37,6 +37,8 @@ import android.view.PointerIcon; import com.jme3.cursors.plugins.JmeCursor; +import com.jme3.input.MouseMotionAccumulator; +import com.jme3.system.AppSettings; import java.nio.IntBuffer; import java.util.logging.Level; @@ -50,6 +52,7 @@ */ public class AndroidMouseInput26 extends AndroidMouseInput24{ private static final Logger logger = Logger.getLogger(AndroidMouseInput26.class.getName()); + private final MouseMotionAccumulator capturedMotion = new MouseMotionAccumulator(); public AndroidMouseInput26(AndroidInputHandler inputHandler) { super(inputHandler); @@ -94,7 +97,7 @@ public boolean onCapturedPointer(MotionEvent event) { case MotionEvent.ACTION_SCROLL: case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_HOVER_MOVE: - addMouseMotionEventRelativePositions(getJmeX(event.getX()), getJmeY(event.getY()), (int) event.getAxisValue(MotionEvent.AXIS_VSCROLL)); + addCapturedMotion(event); consumed = true; break; } @@ -106,8 +109,27 @@ public boolean onCapturedPointer(MotionEvent event) { return consumed; } + private void addCapturedMotion(MotionEvent event) { + synchronized (capturedMotion) { + int dx = capturedMotion.addX(scaleMouseX(event.getX())); + int dy = capturedMotion.addY(scaleMouseY(event.getY())); + addMouseMotionEventRelativePositions(dx, dy, (int) event.getAxisValue(MotionEvent.AXIS_VSCROLL)); + } + } + + @Override + public void loadSettings(AppSettings settings) { + synchronized (capturedMotion) { + super.loadSettings(settings); + capturedMotion.clear(); + } + } + @Override public void setCursorVisible(boolean visible) { + synchronized (capturedMotion) { + capturedMotion.clear(); + } if(!visible) { inputHandler.getView().requestPointerCapture(); } else { diff --git a/jme3-android/src/main/java/com/jme3/input/android/AndroidTouchInput.java b/jme3-android/src/main/java/com/jme3/input/android/AndroidTouchInput.java index ecddc05cbdf..c7bfa47e5f3 100644 --- a/jme3-android/src/main/java/com/jme3/input/android/AndroidTouchInput.java +++ b/jme3-android/src/main/java/com/jme3/input/android/AndroidTouchInput.java @@ -448,32 +448,16 @@ private static long pointerBit(int pointerId) { // TODO: Ring Buffer for mouse events? public InputEvent generateMouseEvent(TouchEvent event) { InputEvent inputEvent = null; - int newX; - int newY; - int newDX; - int newDY; // MouseEvents do not support multi-touch, so only evaluate 1 finger pointer events if (!isSimulateMouse() || numPointers > 1) { return null; } - - if (isMouseEventsInvertX()) { - newX = (int) (invertX(event.getX())); - newDX = (int)event.getDeltaX() * -1; - } else { - newX = (int) event.getX(); - newDX = (int)event.getDeltaX(); - } - - if (isMouseEventsInvertY()) { - newY = (int) (invertY(event.getY())); - newDY = (int)event.getDeltaY() * -1; - } else { - newY = (int) event.getY(); - newDY = (int)event.getDeltaY(); - } + boolean flipX = isMouseEventsInvertX(); + boolean flipY = isMouseEventsInvertY(); + int newX = (int) (flipX ? invertX(event.getX()) : event.getX()); + int newY = (int) (flipY ? invertY(event.getY()) : event.getY()); switch (event.getType()) { case DOWN: @@ -490,7 +474,13 @@ public InputEvent generateMouseEvent(TouchEvent event) { case HOVER_MOVE: case MOVE: - inputEvent = new MouseMotionEvent(newX, newY, newDX, newDY, (int)event.getScaleSpan(), (int)event.getDeltaScaleSpan()); + // Derive deltas from quantized positions so subpixel moves build up across events. + float previousX = event.getX() - event.getDeltaX(); + float previousY = event.getY() - event.getDeltaY(); + int oldX = (int) (flipX ? invertX(previousX) : previousX); + int oldY = (int) (flipY ? invertY(previousY) : previousY); + inputEvent = new MouseMotionEvent(newX, newY, newX - oldX, newY - oldY, + (int) event.getScaleSpan(), (int) event.getDeltaScaleSpan()); inputEvent.setTime(event.getTime()); break; } diff --git a/jme3-android/src/test/java/com/jme3/input/android/MouseInputPrecisionTest.java b/jme3-android/src/test/java/com/jme3/input/android/MouseInputPrecisionTest.java new file mode 100644 index 00000000000..66dc86b429f --- /dev/null +++ b/jme3-android/src/test/java/com/jme3/input/android/MouseInputPrecisionTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2009-2026 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.input.android; + +import android.view.MotionEvent; +import android.view.View; +import com.jme3.input.RawInputListenerAdapter; +import com.jme3.input.event.MouseMotionEvent; +import com.jme3.input.event.TouchEvent; +import com.jme3.system.AppSettings; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class MouseInputPrecisionTest { + + @Test + void shouldAccumulateScaledCapturedMotionAndResetOnRelease() { + AndroidInputHandler handler = mock(AndroidInputHandler.class); + View view = mock(View.class); + when(handler.getView()).thenReturn(view); + when(view.getWidth()).thenReturn(100); + when(view.getHeight()).thenReturn(100); + AppSettings settings = new AppSettings(false); + settings.setResolution(200, 50); + + AndroidMouseInput26 input = new AndroidMouseInput26(handler); + input.loadSettings(settings); + List reported = new ArrayList<>(); + input.setInputListener(new RawInputListenerAdapter() { + @Override + public void onMouseMotionEvent(MouseMotionEvent event) { + reported.add(event); + } + }); + MotionEvent motion = mock(MotionEvent.class); + when(motion.getAction()).thenReturn(MotionEvent.ACTION_MOVE); + when(motion.getX()).thenReturn(0.25f, 0.25f, -0.25f, -0.25f, 0.25f, 0.25f); + when(motion.getY()).thenReturn(-1f, -1f, 1f, 1f, -1f, -1f); + for (int i = 0; i < 5; i++) { + input.onCapturedPointer(motion); + } + input.setCursorVisible(true); + input.setCursorVisible(false); + input.onCapturedPointer(motion); + input.update(); + + assertEquals(6, reported.size()); + assertEquals(List.of(0, 1, 0, -1, 0, 0), reported.stream().map(MouseMotionEvent::getDX).toList()); + assertEquals(List.of(0, -1, 0, 1, 0, 0), reported.stream().map(MouseMotionEvent::getDY).toList()); + } + + @Test + void shouldReportSubpixelTouchMotionWhenItCrossesAPixel() { + AndroidTouchInput input = new AndroidTouchInput(null); + input.setSimulateMouse(true); + TouchEvent touch = new TouchEvent(); + + touch.set(TouchEvent.Type.MOVE, 10.75f, 20.25f, 0.5f, -0.5f); + MouseMotionEvent first = (MouseMotionEvent) input.generateMouseEvent(touch); + assertEquals(0, first.getDX()); + assertEquals(0, first.getDY()); + + touch.set(TouchEvent.Type.MOVE, 11.25f, 19.75f, 0.5f, -0.5f); + MouseMotionEvent second = (MouseMotionEvent) input.generateMouseEvent(touch); + assertEquals(1, second.getDX()); + assertEquals(-1, second.getDY()); + } +} diff --git a/jme3-core/src/main/java/com/jme3/input/MouseMotionAccumulator.java b/jme3-core/src/main/java/com/jme3/input/MouseMotionAccumulator.java new file mode 100644 index 00000000000..8aeea4a7e21 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/input/MouseMotionAccumulator.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2009-2026 jMonkeyEngine + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jme3.input; + +/** + * Converts fractional relative mouse motion to whole pixels for input backends, + * retaining the unreported fraction independently on each axis. + */ +public final class MouseMotionAccumulator { + + private double remainderX; + private double remainderY; + + /** + * Adds horizontal movement and returns the whole pixels ready to report. + * + * @param delta horizontal movement in input pixels + * @return the signed horizontal delta to report + */ + public int addX(double delta) { + double accumulated = remainderX + delta; + int whole = (int) accumulated; + remainderX = remainder(accumulated, whole); + return whole; + } + + /** + * Adds vertical movement and returns the whole pixels ready to report. + * + * @param delta vertical movement in input pixels + * @return the signed vertical delta to report + */ + public int addY(double delta) { + double accumulated = remainderY + delta; + int whole = (int) accumulated; + remainderY = remainder(accumulated, whole); + return whole; + } + + /** Discards unreported fractions, for example when a mouse capture ends. */ + public void clear() { + remainderX = 0.0; + remainderY = 0.0; + } + + private static double remainder(double accumulated, int whole) { + double fraction = accumulated - whole; + // A non-finite or out-of-range event must not affect later events. + return Math.abs(fraction) < 1.0 ? fraction : 0.0; + } +} diff --git a/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java b/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java index 6ed10c0f38f..3da79a481ab 100644 --- a/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java +++ b/jme3-ios/src/main/java/com/jme3/input/ios/IosInputHandler.java @@ -2,6 +2,7 @@ import com.jme3.input.RawInputListener; import com.jme3.input.MouseInput; +import com.jme3.input.MouseMotionAccumulator; import com.jme3.input.TouchInput; import com.jme3.input.event.InputEvent; import com.jme3.input.event.KeyInputEvent; @@ -37,6 +38,7 @@ public class IosInputHandler implements TouchInput { private float scaleY = 1f; private float mouseScaleX = 1f; private float mouseScaleY = 1f; + private final MouseMotionAccumulator mouseMotion = new MouseMotionAccumulator(); private int toJmeMouseButton(int sdlButton) { switch (sdlButton) { @@ -95,6 +97,7 @@ void dispatchQueuedEvents() { @Override public void destroy() { initialized = false; + mouseMotion.clear(); touchEventPool.destroy(); if (touchHandler != null) { touchHandler.destroy(); @@ -149,6 +152,7 @@ public void loadSettings(AppSettings settings) { mouseEventsEnabled = settings.isEmulateMouse(); mouseEventsInvertX = settings.isEmulateMouseFlipX(); mouseEventsInvertY = settings.isEmulateMouseFlipY(); + mouseMotion.clear(); // view width and height are 0 until the view is displayed on the screen //if (view.getWidth() != 0 && view.getHeight() != 0) { @@ -166,6 +170,9 @@ public void loadSettings(AppSettings settings) { } public void setFramebufferSize(int width, int height) { + if ((width > 0 && this.width != width) || (height > 0 && this.height != height)) { + mouseMotion.clear(); + } if (width > 0) { this.width = width; } @@ -184,8 +191,13 @@ public void setFramebufferSize(int width, int height) { */ public void setInputSize(int width, int height, int windowWidth, int windowHeight) { setFramebufferSize(width, height); - mouseScaleX = this.width > 0 && windowWidth > 0 ? (float) this.width / windowWidth : 1f; - mouseScaleY = this.height > 0 && windowHeight > 0 ? (float) this.height / windowHeight : 1f; + float newScaleX = this.width > 0 && windowWidth > 0 ? (float) this.width / windowWidth : 1f; + float newScaleY = this.height > 0 && windowHeight > 0 ? (float) this.height / windowHeight : 1f; + if (mouseScaleX != newScaleX || mouseScaleY != newScaleY) { + mouseScaleX = newScaleX; + mouseScaleY = newScaleY; + mouseMotion.clear(); + } } public boolean isMouseEventsInvertX() { @@ -288,8 +300,8 @@ void dispatchBridgeEvent(int[] intData, float[] floatData, long time) { MouseMotionEvent motion = new MouseMotionEvent( Math.round(mouseX(floatData[0])), Math.round(mouseY(floatData[1])), - Math.round(mouseDeltaX(floatData[2])), - Math.round(mouseDeltaY(floatData[3])), + mouseMotion.addX(mouseDeltaX(floatData[2])), + mouseMotion.addY(mouseDeltaY(floatData[3])), 0, 0); motion.setTime(time); diff --git a/jme3-ios/src/main/java/com/jme3/input/ios/IosTouchHandler.java b/jme3-ios/src/main/java/com/jme3/input/ios/IosTouchHandler.java index 21284b0b85e..351814ba963 100644 --- a/jme3-ios/src/main/java/com/jme3/input/ios/IosTouchHandler.java +++ b/jme3-ios/src/main/java/com/jme3/input/ios/IosTouchHandler.java @@ -164,26 +164,11 @@ protected void processEvent(TouchEvent event) { // TODO: Ring Buffer for mouse events? protected InputEvent generateMouseEvent(TouchEvent event) { InputEvent inputEvent = null; - int newX; - int newY; - int newDX; - int newDY; - - if (iosInput.isMouseEventsInvertX()) { - newX = (int) (iosInput.invertX(event.getX())); - newDX = (int)event.getDeltaX() * -1; - } else { - newX = (int) event.getX(); - newDX = (int)event.getDeltaX(); - } - if (iosInput.isMouseEventsInvertY()) { - newY = (int) (iosInput.invertY(event.getY())); - newDY = (int)event.getDeltaY() * -1; - } else { - newY = (int) event.getY(); - newDY = (int)event.getDeltaY(); - } + boolean flipX = iosInput.isMouseEventsInvertX(); + boolean flipY = iosInput.isMouseEventsInvertY(); + int newX = (int) (flipX ? iosInput.invertX(event.getX()) : event.getX()); + int newY = (int) (flipY ? iosInput.invertY(event.getY()) : event.getY()); switch (event.getType()) { case DOWN: @@ -200,7 +185,13 @@ protected InputEvent generateMouseEvent(TouchEvent event) { case HOVER_MOVE: case MOVE: - inputEvent = new MouseMotionEvent(newX, newY, newDX, newDY, (int)event.getScaleSpan(), (int)event.getDeltaScaleSpan()); + // Derive deltas from quantized positions so subpixel moves build up across events. + float previousX = event.getX() - event.getDeltaX(); + float previousY = event.getY() - event.getDeltaY(); + int oldX = (int) (flipX ? iosInput.invertX(previousX) : previousX); + int oldY = (int) (flipY ? iosInput.invertY(previousY) : previousY); + inputEvent = new MouseMotionEvent(newX, newY, newX - oldX, newY - oldY, + (int) event.getScaleSpan(), (int) event.getDeltaScaleSpan()); inputEvent.setTime(event.getTime()); break; } diff --git a/jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java b/jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java index 345992fff55..b037d396172 100644 --- a/jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java +++ b/jme3-ios/src/test/java/com/jme3/input/ios/MouseInputPrecisionTest.java @@ -34,6 +34,8 @@ import com.jme3.input.RawInputListenerAdapter; import com.jme3.input.event.MouseButtonEvent; import com.jme3.input.event.MouseMotionEvent; +import com.jme3.input.event.TouchEvent; +import com.jme3.system.AppSettings; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; @@ -44,7 +46,7 @@ class MouseInputPrecisionTest { @Test - void shouldScaleNativeMouseCoordinates() { + void shouldScaleNativeMouseCoordinatesAndAccumulateSignedMotion() { IosInputHandler input = new IosInputHandler(); input.setInputSize(1000, 800, 500, 400); List motion = new ArrayList<>(); @@ -60,19 +62,44 @@ public void onMouseButtonEvent(MouseButtonEvent event) { buttons.add(event); } }); - input.dispatchBridgeEvent(new int[]{LibJGLIOSInputBridge.EVENT_MOUSE_MOTION}, - new float[]{0.75f, 0.75f, 0.5f, -0.5f}, 1); + int[] motionType = {LibJGLIOSInputBridge.EVENT_MOUSE_MOTION}; + input.dispatchBridgeEvent(motionType, new float[]{0.75f, 0.75f, 0.25f, -0.25f}, 1); + input.setInputSize(1000, 800, 500, 400); + input.dispatchBridgeEvent(motionType, new float[]{1f, 0.5f, 0.25f, -0.25f}, 2); + input.dispatchBridgeEvent(motionType, new float[]{0.75f, 0.75f, -0.25f, 0.25f}, 3); + input.dispatchBridgeEvent(motionType, new float[]{0.5f, 1f, -0.25f, 0.25f}, 4); input.dispatchBridgeEvent(new int[]{LibJGLIOSInputBridge.EVENT_MOUSE_BUTTON, 1, 1}, - new float[]{0.75f, 0.75f}, 2); + new float[]{0.5f, 1f}, 5); input.dispatchQueuedEvents(); - assertEquals(1, motion.size()); - assertEquals(1, motion.get(0).getDX()); - assertEquals(1, motion.get(0).getDY()); + assertEquals(4, motion.size()); + assertEquals(List.of(0, 1, 0, -1), motion.stream().map(MouseMotionEvent::getDX).toList()); + assertEquals(List.of(0, 1, 0, -1), motion.stream().map(MouseMotionEvent::getDY).toList()); assertEquals(2, motion.get(0).getX()); assertEquals(799, motion.get(0).getY()); assertEquals(1, buttons.size()); - assertEquals(motion.get(0).getX(), buttons.get(0).getX()); - assertEquals(motion.get(0).getY(), buttons.get(0).getY()); + assertEquals(motion.get(3).getX(), buttons.get(0).getX()); + assertEquals(motion.get(3).getY(), buttons.get(0).getY()); + } + + @Test + void shouldReportSubpixelTouchMotionWithInvertedMouseAxes() { + AppSettings settings = new AppSettings(false); + settings.setResolution(1000, 800); + settings.setEmulateMouseFlipAxis(true, true); + IosInputHandler input = new IosInputHandler(); + input.loadSettings(settings); + IosTouchHandler touchHandler = new IosTouchHandler(input); + TouchEvent touch = new TouchEvent(); + + touch.set(TouchEvent.Type.MOVE, 10.75f, 20.25f, 0.5f, -0.5f); + MouseMotionEvent first = (MouseMotionEvent) touchHandler.generateMouseEvent(touch); + assertEquals(0, first.getDX()); + assertEquals(0, first.getDY()); + + touch.set(TouchEvent.Type.MOVE, 11.25f, 19.75f, 0.5f, -0.5f); + MouseMotionEvent second = (MouseMotionEvent) touchHandler.generateMouseEvent(touch); + assertEquals(-1, second.getDX()); + assertEquals(1, second.getDY()); } } diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/SdlMouseInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/SdlMouseInput.java index 34d19876082..7ba0b7db7af 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/SdlMouseInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/SdlMouseInput.java @@ -34,6 +34,7 @@ import com.jme3.cursors.plugins.JmeCursor; import com.jme3.input.JoyInput; import com.jme3.input.MouseInput; +import com.jme3.input.MouseMotionAccumulator; import com.jme3.input.RawInputListener; import com.jme3.input.event.MouseButtonEvent; import com.jme3.input.event.MouseMotionEvent; @@ -73,6 +74,7 @@ public class SdlMouseInput implements MouseInput, SdlEventListener { private final Queue mouseMotionEvents = new ArrayDeque<>(); private final Queue mouseButtonEvents = new ArrayDeque<>(); private final Vector2f inputScale = new Vector2f(1f, 1f); + private final MouseMotionAccumulator relativeMotion = new MouseMotionAccumulator(); private final LwjglWindow context; private RawInputListener listener; @@ -171,13 +173,14 @@ public void onSDLEvent(SDL_Event event) { final int yDelta; if (relativeMode) { - xDelta = Math.round(event.motion().xrel() * inputScale.x); - yDelta = -Math.round(event.motion().yrel() * inputScale.y); + xDelta = relativeMotion.addX(event.motion().xrel() * inputScale.x); + yDelta = relativeMotion.addY(-event.motion().yrel() * inputScale.y); mouseX += xDelta; mouseY += yDelta; x = mouseX; y = mouseY; } else { + relativeMotion.clear(); x = toInputX(event.motion().x()); y = toInputY(event.motion().y()); xDelta = x - mouseX; @@ -264,6 +267,7 @@ private void refreshWindowMetrics() { } private void initCurrentMousePosition() { + relativeMotion.clear(); try (MemoryStack stack = MemoryStack.stackPush()) { FloatBuffer x = stack.callocFloat(1); FloatBuffer y = stack.callocFloat(1); @@ -357,6 +361,9 @@ public void destroy() { public void setCursorVisible(boolean visible) { boolean wasVisible = cursorVisible; cursorVisible = visible; + if (wasVisible != visible) { + relativeMotion.clear(); + } if (!context.isRenderable()) { return; } diff --git a/jme3-lwjgl3/src/test/java/com/jme3/input/lwjgl/SdlMouseInputTest.java b/jme3-lwjgl3/src/test/java/com/jme3/input/lwjgl/SdlMouseInputTest.java index c97c2de598f..8b94efcfe96 100644 --- a/jme3-lwjgl3/src/test/java/com/jme3/input/lwjgl/SdlMouseInputTest.java +++ b/jme3-lwjgl3/src/test/java/com/jme3/input/lwjgl/SdlMouseInputTest.java @@ -2,6 +2,7 @@ import com.jme3.input.event.MouseButtonEvent; import com.jme3.input.event.MouseMotionEvent; +import com.jme3.math.Vector2f; import com.jme3.system.AppSettings; import com.jme3.system.lwjgl.LwjglWindow; import java.lang.reflect.Field; @@ -13,6 +14,7 @@ import org.junit.jupiter.api.Test; import org.lwjgl.sdl.SDL_Event; import org.lwjgl.sdl.SDLMouse; +import org.lwjgl.system.MemoryStack; import org.mockito.MockedStatic; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -122,6 +124,70 @@ public void shouldNotClampVirtualCursorPositionInRelativeMode() throws Exception } } + @Test + public void shouldAccumulateFractionalRelativeMotionSymmetrically() throws Exception { + LwjglWindow context = mockRenderableContext(800, 600); + when(context.getWindowId()).thenReturn(7); + when(context.getMouseInputScale(any(Vector2f.class))) + .thenAnswer(invocation -> invocation.getArgument(0).set(2f, 4f)); + + try (MockedStatic sdlMouse = mockStatic(SDLMouse.class); + MemoryStack stack = MemoryStack.stackPush()) { + sdlMouse.when(() -> SDLMouse.SDL_GetWindowRelativeMouseMode(42L)).thenReturn(true); + SdlMouseInput mouseInput = new SdlMouseInput(context); + Queue mouseMotionEvents = getField(mouseInput, "mouseMotionEvents"); + SDL_Event event = SDL_Event.calloc(stack); + event.type(SDL_EVENT_MOUSE_MOTION); + event.motion().windowID(7); + + event.motion().xrel(0.25f); + event.motion().yrel(-0.125f); + mouseInput.onSDLEvent(event); + assertTrue(mouseMotionEvents.isEmpty()); + mouseInput.onSDLEvent(event); + MouseMotionEvent forward = mouseMotionEvents.remove(); + assertEquals(1, forward.getDX()); + assertEquals(1, forward.getDY()); + + event.motion().xrel(-0.25f); + event.motion().yrel(0.125f); + mouseInput.onSDLEvent(event); + assertTrue(mouseMotionEvents.isEmpty()); + mouseInput.onSDLEvent(event); + MouseMotionEvent backward = mouseMotionEvents.remove(); + assertEquals(-1, backward.getDX()); + assertEquals(-1, backward.getDY()); + assertTrue(mouseMotionEvents.isEmpty()); + } + } + + @Test + public void shouldDiscardFractionalMotionWhenRelativeModeEnds() throws Exception { + LwjglWindow context = mockRenderableContext(800, 600); + when(context.getWindowId()).thenReturn(7); + + try (MockedStatic sdlMouse = mockStatic(SDLMouse.class); + MemoryStack stack = MemoryStack.stackPush()) { + sdlMouse.when(() -> SDLMouse.SDL_GetWindowRelativeMouseMode(42L)).thenReturn(true); + SdlMouseInput mouseInput = new SdlMouseInput(context); + Queue mouseMotionEvents = getField(mouseInput, "mouseMotionEvents"); + SDL_Event event = SDL_Event.calloc(stack); + event.type(SDL_EVENT_MOUSE_MOTION); + event.motion().windowID(7); + event.motion().xrel(0.25f); + + mouseInput.setCursorVisible(false); + mouseInput.onSDLEvent(event); + mouseInput.setCursorVisible(true); + mouseMotionEvents.clear(); // Discard the cursor position sync event. + mouseInput.setCursorVisible(false); + + event.motion().xrel(0.75f); + mouseInput.onSDLEvent(event); + assertTrue(mouseMotionEvents.isEmpty()); + } + } + @Test public void shouldKeepVirtualCursorPositionForButtonsInRelativeMode() throws Exception { LwjglWindow context = mockRenderableContext(800, 600);