소스 검색

Fix Scaling issue #1745 (#1753)

Riccardo Balbo 3 년 전
부모
커밋
655a759716
2개의 변경된 파일25개의 추가작업 그리고 51개의 파일을 삭제
  1. 11 26
      jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java
  2. 14 25
      jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java

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

@@ -36,6 +36,7 @@ import com.jme3.input.MouseInput;
 import com.jme3.input.RawInputListener;
 import com.jme3.input.event.MouseButtonEvent;
 import com.jme3.input.event.MouseMotionEvent;
+import com.jme3.math.Vector2f;
 import com.jme3.system.lwjgl.LwjglWindow;
 import com.jme3.util.BufferUtils;
 import java.nio.ByteBuffer;
@@ -133,29 +134,18 @@ 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];
-
+    private final Vector2f inputScale = new Vector2f();
+  
     public GlfwMouseInput(final LwjglWindow context) {
         this.context = context;
         this.cursorVisible = true;
     }
 
     private void onCursorPos(final long window, final double xpos, final double ypos) {
-        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);
-        }
+        context.getWindowContentScale(inputScale);
+        int x = (int) Math.round(xpos * inputScale.x);
+        int y = (int) Math.round((currentHeight - ypos) * inputScale.y);
+      
 
         int xDelta = x - mouseX;
         int yDelta = y - mouseY;
@@ -252,19 +242,14 @@ public class GlfwMouseInput implements MouseInput {
         });
     }
 
+    
     private void initCurrentMousePosition(long window) {
         double[] x = new double[1];
         double[] y = new double[1];
         glfwGetCursorPos(window, x, y);
-
-        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]);
-        }
+        context.getWindowContentScale(inputScale);
+        mouseX = (int) Math.round(x[0] * inputScale.x);
+        mouseY = (int) Math.round((currentHeight - y[0]) * inputScale.y);     
     }
 
     /**

+ 14 - 25
jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglWindow.java

@@ -140,11 +140,11 @@ 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;
 
+    // temp variables used for glfw calls
+    private int width[] = new int[1];
+    private int height[] = new int[1];
+    
     public LwjglWindow(final JmeContext.Type type) {
 
         if (!SUPPORTED_TYPES.contains(type)) {
@@ -233,9 +233,7 @@ 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());
-
-        isScaledContent = settings.isUseRetinaFrameBuffer();
-        glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, isScaledContent ? GLFW_TRUE : GLFW_FALSE);
+        glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, settings.isUseRetinaFrameBuffer() ? GLFW_TRUE : GLFW_FALSE);
 
         if (settings.getBitsPerPixel() == 24) {
             glfwWindowHint(GLFW_RED_BITS, 8);
@@ -571,8 +569,6 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
         if (framesAfterContextStarted < 2) {
             framesAfterContextStarted++;
             if (framesAfterContextStarted == 2) {
-                int[] width = new int[1];
-                int[] height = new int[1];
                 glfwGetFramebufferSize(window, width, height);
 
                 if (settings.getWidth() != width[0] || settings.getHeight() != height[0]) {
@@ -713,15 +709,6 @@ 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;
@@ -759,13 +746,15 @@ public abstract class LwjglWindow extends LwjglContext implements Runnable {
      * @see <a href="https://www.glfw.org/docs/latest/window_guide.html#window_scale">Window content scale</a>
      */
     public Vector2f getWindowContentScale(Vector2f store) {
-        float[] xScale = new float[1];
-        float[] yScale = new float[1];
-        glfwGetWindowContentScale(window, xScale, yScale);
+        if (store == null) store = new Vector2f();
 
-        if (store != null) {
-            return store.set(xScale[0], yScale[0]);
-        }
-        return new Vector2f(xScale[0], yScale[0]);
+        glfwGetFramebufferSize(window, width, height);
+        store.set(width[0], height[0]);
+
+        glfwGetWindowSize(window, width, height);
+        store.x /= width[0];
+        store.y /= height[0];
+
+        return store;
     }
 }