From dab5f3658d697363f80447bb1af5391362e79f74 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 18 Jun 2026 18:11:35 +0200 Subject: [PATCH] deprecate legacy reshape() and improve backward compatibility --- .../com/jme3/system/android/OGLESContext.java | 3 + .../java/com/jme3/app/LegacyApplication.java | 6 +- .../java/com/jme3/system/SystemListener.java | 8 +- .../app/LegacyApplicationReshapeTest.java | 123 ++++++++++++++++++ .../com/jme3/system/ios/IGLESContext.java | 3 + .../com/jme3/system/lwjgl/LwjglCanvas.java | 1 + .../com/jme3/system/lwjgl/LwjglDisplay.java | 2 + .../com/jme3/system/lwjgl/LwjglCanvas.java | 1 + .../com/jme3/system/lwjgl/LwjglWindow.java | 1 + 9 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 jme3-core/src/test/java/com/jme3/app/LegacyApplicationReshapeTest.java diff --git a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java index 138519144f..4bb91e0dc3 100644 --- a/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java +++ b/jme3-android/src/main/java/com/jme3/system/android/OGLESContext.java @@ -465,6 +465,7 @@ public void onSurfaceChanged(GL10 gl, int width, int height) { if (renderable.get()) { logger.log(Level.FINE, "App already initialized, calling reshape"); listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); + listener.reshape(logicalWidth, logicalHeight); } } @@ -511,6 +512,7 @@ private void applyDisplayScaleModeIfNeeded() { updateDisplayScaleMetrics(); if (renderable.get()) { listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); + listener.reshape(logicalWidth, logicalHeight); } } @@ -529,6 +531,7 @@ public void onDrawFrame(GL10 gl) { listener.initialize(); if (framebufferWidth > 0 && framebufferHeight > 0) { listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); + listener.reshape(logicalWidth, logicalHeight); } renderable.set(true); } diff --git a/jme3-core/src/main/java/com/jme3/app/LegacyApplication.java b/jme3-core/src/main/java/com/jme3/app/LegacyApplication.java index d0fbde9fbd..21e865638f 100644 --- a/jme3-core/src/main/java/com/jme3/app/LegacyApplication.java +++ b/jme3-core/src/main/java/com/jme3/app/LegacyApplication.java @@ -590,10 +590,14 @@ public void startCanvas(boolean waitFor) { /** * Internal use only. + * + * @deprecated Display size changes are reported through + * {@link #reshape(int, int, int, int)}. Use this new method instead. + * This one is kept only for backward compatibility. */ @Override + @Deprecated public void reshape(int w, int h) { - reshape(w, h, w, h); } @Override diff --git a/jme3-core/src/main/java/com/jme3/system/SystemListener.java b/jme3-core/src/main/java/com/jme3/system/SystemListener.java index fe445f8ff3..6a7aba63d3 100644 --- a/jme3-core/src/main/java/com/jme3/system/SystemListener.java +++ b/jme3-core/src/main/java/com/jme3/system/SystemListener.java @@ -48,8 +48,13 @@ public interface SystemListener { * Called to notify the application that the resolution has changed. * @param width the new logical width of the display (≥0) * @param height the new logical height of the display (≥0) + * + * @deprecated Implement {@link #reshape(int, int, int, int)} instead. + * This one is kept only for backward compatibility. */ - public void reshape(int width, int height); + @Deprecated + public default void reshape(int width, int height) { + } /** * Called to notify the application that logical application size and @@ -61,7 +66,6 @@ public interface SystemListener { * @param framebufferHeight the physical framebuffer height in pixels */ public default void reshape(int logicalWidth, int logicalHeight, int framebufferWidth, int framebufferHeight) { - reshape(logicalWidth, logicalHeight); } /** diff --git a/jme3-core/src/test/java/com/jme3/app/LegacyApplicationReshapeTest.java b/jme3-core/src/test/java/com/jme3/app/LegacyApplicationReshapeTest.java new file mode 100644 index 0000000000..a5d5181f14 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/app/LegacyApplicationReshapeTest.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2009-2022 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; 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.app; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class LegacyApplicationReshapeTest { + + @Test + void fourArgumentReshapeDoesNotCallLegacyOverride() { + LegacyReshapeApplication application = new LegacyReshapeApplication(); + + application.reshape(320, 240, 640, 480); + + assertEquals(0, application.legacyReshapeCalls); + } + + @Test + void directLegacyReshapeCallInvokesLegacyOverrideOnly() { + LegacyReshapeApplication application = new LegacyReshapeApplication(); + + application.reshape(320, 240); + + assertEquals(1, application.legacyReshapeCalls); + assertEquals(320, application.width); + assertEquals(240, application.height); + } + + @Test + void fourArgumentReshapeWithModernOverrideDoesNotCallLegacyOverride() { + ModernReshapeApplication application = new ModernReshapeApplication(); + + application.reshape(320, 240, 640, 480); + + assertEquals(1, application.modernReshapeCalls); + assertEquals(0, application.legacyReshapeCalls); + } + + @Test + void directLegacyReshapeCallDoesNotReachModernOverride() { + ModernOnlyReshapeApplication application = new ModernOnlyReshapeApplication(); + + application.reshape(320, 240); + + assertEquals(0, application.modernReshapeCalls); + } + + private static class LegacyReshapeApplication extends LegacyApplication { + + protected int legacyReshapeCalls; + private int width; + private int height; + + @Override + public void reshape(int width, int height) { + legacyReshapeCalls++; + this.width = width; + this.height = height; + super.reshape(width, height); + } + } + + private static final class ModernReshapeApplication extends LegacyReshapeApplication { + + private int modernReshapeCalls; + + @Override + public void reshape(int logicalWidth, int logicalHeight, int framebufferWidth, int framebufferHeight) { + modernReshapeCalls++; + super.reshape(logicalWidth, logicalHeight, framebufferWidth, framebufferHeight); + } + } + + private static final class ModernOnlyReshapeApplication extends LegacyApplication { + + private int modernReshapeCalls; + private int logicalWidth; + private int logicalHeight; + private int framebufferWidth; + private int framebufferHeight; + + @Override + public void reshape(int logicalWidth, int logicalHeight, int framebufferWidth, int framebufferHeight) { + modernReshapeCalls++; + this.logicalWidth = logicalWidth; + this.logicalHeight = logicalHeight; + this.framebufferWidth = framebufferWidth; + this.framebufferHeight = framebufferHeight; + super.reshape(logicalWidth, logicalHeight, framebufferWidth, framebufferHeight); + } + } +} 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 18794bcb87..5e7448f75e 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 @@ -260,6 +260,7 @@ public void create(boolean waitFor) { renderable.set(true); if (framebufferWidth > 0 && framebufferHeight > 0) { listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); + listener.reshape(logicalWidth, logicalHeight); } if (waitFor) { @@ -319,6 +320,7 @@ public void resizeFramebuffer(int width, int height) { logger.log(Level.FINE, "iOS framebuffer resized, width: {0} height: {1}", new Object[]{framebufferWidth, framebufferHeight}); listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); + listener.reshape(logicalWidth, logicalHeight); } } } @@ -364,6 +366,7 @@ private void applyDisplayScaleModeIfNeeded() { linearFrameBufferDirty = true; if (renderable.get() && listener != null) { listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); + listener.reshape(logicalWidth, logicalHeight); } } diff --git a/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java b/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java index 33e8a3a70d..06e7b2541a 100644 --- a/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java +++ b/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java @@ -210,6 +210,7 @@ protected void runLoop() { width = newWidth; height = newHeight; if (listener != null) { + listener.reshape(width, height, width, height); listener.reshape(width, height); } } diff --git a/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglDisplay.java b/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglDisplay.java index 21558a7c33..8d839d3921 100644 --- a/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglDisplay.java +++ b/jme3-lwjgl/src/main/java/com/jme3/system/lwjgl/LwjglDisplay.java @@ -226,6 +226,7 @@ public void runLoop() { } catch (LWJGLException ex) { logger.log(Level.SEVERE, "Failed to set display settings!", ex); } + listener.reshape(settings.getWidth(), settings.getHeight(), settings.getWidth(), settings.getHeight()); listener.reshape(settings.getWidth(), settings.getHeight()); if (renderable.get()) { reinitContext(); @@ -237,6 +238,7 @@ public void runLoop() { int newWidth = Display.getWidth(); int newHeight = Display.getHeight(); settings.setResolution(newWidth, newHeight); + listener.reshape(newWidth, newHeight, newWidth, newHeight); listener.reshape(newWidth, newHeight); } diff --git a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java index 3e286dd112..6f2d81f427 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/system/lwjgl/LwjglCanvas.java @@ -525,6 +525,7 @@ public void run() { if (needResize.getAndSet(false)) { settings.setResolution(framebufferWidth, framebufferHeight); listener.reshape(framebufferWidth, framebufferHeight, framebufferWidth, framebufferHeight); + listener.reshape(framebufferWidth, framebufferHeight); } synchronized (lock) { 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 13144debbb..1396e8682d 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 @@ -579,6 +579,7 @@ private void updateSizes(boolean notifyListener) { || framebufferWidth != oldFramebufferWidth || framebufferHeight != oldFramebufferHeight) { settings.setResolution(logicalWidth, logicalHeight); listener.reshape(logicalWidth, logicalHeight, getRenderFramebufferWidth(), getRenderFramebufferHeight()); + listener.reshape(logicalWidth, logicalHeight); oldLogicalWidth = logicalWidth; oldLogicalHeight = logicalHeight; oldFramebufferWidth = framebufferWidth;