Explorar el Código

GlfwMouseInput: scale mouse coords only if Retina AppSetting is true (#1746)

* GlfwMouseInput:  scale mouse coords only if Retina AppSetting is true

* GlfwMouseInput:  some minor code cleanup

* GlfwMouseInput:  check for scaled content in initCurrentMousePosition()
Stephen Gold hace 3 años
padre
commit
797bfd8b45

+ 26 - 16
jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java

@@ -134,23 +134,31 @@ public class GlfwMouseInput implements MouseInput {
     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 xDelta;
-        int yDelta;
-        int x = (int) Math.round(xpos * xScale[0]);
-        int y = (int) Math.round((currentHeight - ypos) * yScale[0]);
+        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);
+        }
 
-        xDelta = x - mouseX;
-        yDelta = y - mouseY;
+        int xDelta = x - mouseX;
+        int yDelta = y - mouseY;
         mouseX = x;
         mouseY = y;
 
@@ -249,12 +257,14 @@ public class GlfwMouseInput implements MouseInput {
         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]);
+        }
     }
 
     /**

+ 16 - 1
jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java

@@ -140,6 +140,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) {
 
@@ -229,7 +233,9 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
         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);
@@ -707,6 +713,15 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
         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;