Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion jme3-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MouseMotionEvent> 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());
}
}
80 changes: 80 additions & 0 deletions jme3-core/src/main/java/com/jme3/input/MouseMotionAccumulator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading
Loading