From 51b23a0c4c275dec0b4cf2e8240baa259e2237cd Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Sun, 16 Jan 2022 12:06:49 -0800 Subject: [PATCH 1/3] GlfwMouseInput: scale mouse coords only if Retina AppSetting is true --- .../com/jme3/input/lwjgl/GlfwMouseInput.java | 24 +++++++++++++------ .../com/jme3/system/lwjgl/LwjglWindow.java | 17 ++++++++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java index a868578910..369324528a 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java @@ -132,25 +132,35 @@ private static ByteBuffer transformCursorImage(final IntBuffer imageData, final private int mouseY; private int mouseWheel; private int currentHeight; - + private boolean cursorVisible; private boolean initialized; + /** + * temporary storage for GLFW queries + */ + private final float[] xScale = new float[1]; + private final float[] yScale = new float[1]; + public GlfwMouseInput(final LwjglWindow context) { this.context = context; this.cursorVisible = true; } private void onCursorPos(final long window, final double xpos, final double ypos) { - float[] xScale = new float[1]; - float[] yScale = new float[1]; - glfwGetWindowContentScale(window, xScale, yScale); + int x; + int y; + if (context.isScaledContent()) { + glfwGetWindowContentScale(window, xScale, yScale); + x = (int) Math.round(xpos * xScale[0]); + y = (int) Math.round((currentHeight - ypos) * yScale[0]); + } else { + x = (int) Math.round(xpos); + y = (int) Math.round(currentHeight - ypos); + } int xDelta; int yDelta; - int x = (int) Math.round(xpos * xScale[0]); - int y = (int) Math.round((currentHeight - ypos) * yScale[0]); - xDelta = x - mouseX; yDelta = y - mouseY; mouseX = x; diff --git a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java index 8f902acd3e..bfef4b99ba 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java @@ -147,6 +147,10 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable { protected boolean wasActive = false; protected boolean autoFlush = true; protected boolean allowSwapBuffers = false; + /** + * Set true if Retina/HiDPI frame buffer was enabled via AppSettings. + */ + private boolean isScaledContent = false; public LwjglWindow(final JmeContext.Type type) { @@ -254,7 +258,9 @@ public void invoke(int error, long description) { glfwWindowHint(GLFW_SAMPLES, settings.getSamples()); glfwWindowHint(GLFW_STEREO, settings.useStereo3D() ? GLFW_TRUE : GLFW_FALSE); glfwWindowHint(GLFW_REFRESH_RATE, settings.getFrequency()<=0?GLFW_DONT_CARE:settings.getFrequency()); - glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, settings.isUseRetinaFrameBuffer() ? GLFW_TRUE : GLFW_FALSE); + + isScaledContent = settings.isUseRetinaFrameBuffer(); + glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, isScaledContent ? GLFW_TRUE : GLFW_FALSE); if (settings.getBitsPerPixel() == 24) { glfwWindowHint(GLFW_RED_BITS, 8); @@ -729,6 +735,15 @@ public TouchInput getTouchInput() { return null; } + /** + * Test whether Retina/HiDPI frame buffer was enabled via AppSettings. + * + * @return true if enabled, otherwise false + */ + public boolean isScaledContent() { + return isScaledContent; + } + @Override public void setAutoFlushFrames(boolean enabled) { this.autoFlush = enabled; From d3780b7fd9fe403a9ddec5c705608a5dc225eabf Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Sun, 16 Jan 2022 12:29:04 -0800 Subject: [PATCH 2/3] GlfwMouseInput: some minor code cleanup --- .../main/java/com/jme3/input/lwjgl/GlfwMouseInput.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java index 369324528a..f74e96f437 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java @@ -132,7 +132,7 @@ private static ByteBuffer transformCursorImage(final IntBuffer imageData, final private int mouseY; private int mouseWheel; private int currentHeight; - + private boolean cursorVisible; private boolean initialized; @@ -159,10 +159,8 @@ private void onCursorPos(final long window, final double xpos, final double ypos y = (int) Math.round(currentHeight - ypos); } - int xDelta; - int yDelta; - xDelta = x - mouseX; - yDelta = y - mouseY; + int xDelta = x - mouseX; + int yDelta = y - mouseY; mouseX = x; mouseY = y; From 594d30cb724c1e64cb750c823f9b7bcf6848875a Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Sun, 16 Jan 2022 12:57:26 -0800 Subject: [PATCH 3/3] GlfwMouseInput: check for scaled content in initCurrentMousePosition() --- .../java/com/jme3/input/lwjgl/GlfwMouseInput.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java index f74e96f437..be903ec0b9 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java @@ -256,12 +256,14 @@ private void initCurrentMousePosition(long window) { double[] y = new double[1]; glfwGetCursorPos(window, x, y); - float[] xScale = new float[1]; - float[] yScale = new float[1]; - glfwGetWindowContentScale(window, xScale, yScale); - - mouseX = (int) Math.round(x[0] * xScale[0]); - mouseY = (int) Math.round((currentHeight - y[0]) * yScale[0]); + if (context.isScaledContent()) { + glfwGetWindowContentScale(window, xScale, yScale); + mouseX = (int) Math.round(x[0] * xScale[0]); + mouseY = (int) Math.round((currentHeight - y[0]) * yScale[0]); + } else { + mouseX = (int) Math.round(x[0]); + mouseY = (int) Math.round(currentHeight - y[0]); + } } /**