Skip to content

Commit 164d6e7

Browse files
authored
Restore default screen orientation autodetection (Card-Forge#9676)
Co-authored-by: tool4EvEr <tool4EvEr@>
1 parent 94e4540 commit 164d6e7

File tree

6 files changed

+71
-49
lines changed

6 files changed

+71
-49
lines changed

forge-gui-android/src/forge/app/Main.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,12 @@ private void displayMessage(View previousView, AndroidAdapter adapter, boolean e
348348
}
349349

350350
private void loadGame(final HWInfo hwInfo, final String title, final String steps, boolean isLandscape, AndroidAdapter adapter, boolean permissiongranted, boolean isTabletDevice, AndroidApplicationConfiguration config, boolean exception, String msg) {
351-
int totalRAM = hwInfo.getTotalRam();
352351
try {
353352
final Handler handler = new Handler();
354353
forgeLogo = findViewById(resId("id", "logo_id"));
355354
activeView = findViewById(resId("id", "mainview"));
356355
activeView.setBackgroundColor(Color.WHITE);
357-
forgeView = initializeForView(Forge.getApp(hwInfo, getAndroidClipboard(), adapter, ASSETS_DIR, false, !isLandscape, totalRAM, isTabletDevice, Build.VERSION.SDK_INT), config);
356+
forgeView = initializeForView(Forge.getApp(hwInfo, getAndroidClipboard(), adapter, ASSETS_DIR, false, !isLandscape, isTabletDevice, Build.VERSION.SDK_INT), config);
358357

359358
getAnimator(ObjectAnimator.ofFloat(forgeLogo, "alpha", 1f, 1f).setDuration(800), ObjectAnimator.ofObject(activeView, "backgroundColor", new ArgbEvaluator(), Color.WHITE, Color.BLACK).setDuration(1600), new AnimatorListenerAdapter() {
360359
@Override

forge-gui-ios/src/forge/ios/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected IOSApplication createApplication() {
3030
final IOSApplicationConfiguration config = new IOSApplicationConfiguration();
3131
config.useAccelerometer = false;
3232
config.useCompass = false;
33-
final ApplicationListener app = Forge.getApp(null, new IOSClipboard(), new IOSAdapter(), assetsDir, false, false, 0, false, 0);
33+
final ApplicationListener app = Forge.getApp(null, new IOSClipboard(), new IOSAdapter(), assetsDir, false, false, false, 0);
3434
final IOSApplication iosApp = new IOSApplication(app, config);
3535
return iosApp;
3636
}

forge-gui-mobile-dev/src/forge/app/GameLauncher.java

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package forge.app;
22

33
import com.badlogic.gdx.ApplicationListener;
4+
import com.badlogic.gdx.Graphics.DisplayMode;
45
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
56
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
67
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Clipboard;
@@ -33,7 +34,6 @@ public GameLauncher(final String versionString, final String[] args) {
3334
Configuration.STACK_SIZE.set(1024);
3435
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
3536
HWInfo hw = null;
36-
int totalRAM = 0;
3737
try {
3838
SystemInfo si = new SystemInfo();
3939
// Device Info
@@ -51,56 +51,64 @@ public GameLauncher(final String versionString, final String[] args) {
5151
os.setVersion(si.getOperatingSystem().getVersionInfo().getVersion());
5252
os.setBuild(si.getOperatingSystem().getVersionInfo().getBuildNumber());
5353
os.setRawDescription(si.getOperatingSystem() + " x" + si.getOperatingSystem().getBitness());
54-
totalRAM = Math.round(si.getHardware().getMemory().getTotal() / 1024f / 1024f);
5554
hw = new HWInfo(device, os, false);
5655
} catch (Exception e) {
5756
e.printStackTrace();
5857
}
5958

6059
// Retrieve command line parameters
61-
int windowHeight = 0;
62-
int windowWidth = 0;
63-
boolean isPortrait = false;
64-
boolean manualWindowSize = false;
60+
Integer widthArg = null;
61+
Integer heightArg = null;
62+
boolean portraitArg = false;
63+
boolean landscapeArg = false;
6564
for(String arg : args) {
66-
if(arg.startsWith("width=")) {
67-
windowWidth = Integer.parseInt(arg.substring(6));
68-
manualWindowSize = true;
69-
}
70-
if(arg.startsWith("height=")) {
71-
windowHeight = Integer.parseInt(arg.substring(7));
72-
manualWindowSize = true;
73-
}
74-
if(arg.equalsIgnoreCase("portrait")) {
75-
isPortrait = true;
76-
}
65+
if(arg.startsWith("width=")) widthArg = Integer.parseInt(arg.substring(6));
66+
else if(arg.startsWith("height=")) heightArg = Integer.parseInt(arg.substring(7));
67+
else if(arg.equalsIgnoreCase("portrait")) portraitArg = true;
68+
else if(arg.equalsIgnoreCase("landscape")) landscapeArg = true;
7769
}
7870

79-
// Check if the manually supplied window size indicates portrait mode
80-
if ((windowHeight > 0) && (windowWidth > 0)) {
81-
if (windowHeight > windowWidth) {
82-
isPortrait = true;
83-
}
84-
}
71+
boolean hasBothDims = widthArg != null && heightArg != null;
72+
// Only disable desktop auto-orientation when the user *really* overrides it
73+
boolean overrideOrientation = portraitArg || landscapeArg || hasBothDims;
74+
Forge.setDesktopAutoOrientation(!overrideOrientation);
75+
76+
// Determine desired portrait/landscape only if we are overriding orientation.
77+
boolean isPortrait = false;
78+
if (portraitArg) isPortrait = true;
79+
else if (landscapeArg) isPortrait = false;
80+
else if (hasBothDims) isPortrait = heightArg > widthArg;
8581

8682
ApplicationListener start = Forge.getApp(hw, new Lwjgl3Clipboard(), new Main.DesktopAdapter(switchOrientationFile),
87-
assetsDir, false, isPortrait, totalRAM, false, 0);
83+
assetsDir, false, isPortrait, false, 0);
84+
85+
// Initialize window size
86+
int windowWidth, windowHeight;
8887

89-
// If no manual window size is supplied, use the configured one (and adjust for portrait mode if needed)
90-
if (!manualWindowSize) {
88+
if (widthArg != null || heightArg != null) {
89+
float aspect = getPrimaryScreenAspect();
90+
91+
// If explicit portrait/landscape requested, coerce aspect direction
92+
if (portraitArg && aspect > 1f) aspect = 1f / aspect;
93+
if (landscapeArg && aspect < 1f) aspect = 1f / aspect;
94+
95+
if (widthArg != null && heightArg == null) {
96+
windowWidth = widthArg;
97+
windowHeight = Math.max(1, Math.round(windowWidth / aspect));
98+
} else if (heightArg != null && widthArg == null) {
99+
windowHeight = heightArg;
100+
windowWidth = Math.max(1, Math.round(windowHeight * aspect));
101+
} else { // both provided
102+
windowWidth = widthArg;
103+
windowHeight = heightArg;
104+
}
105+
} else {
91106
windowWidth = Config.instance().getSettingData().width;
92107
windowHeight = Config.instance().getSettingData().height;
93-
if (isPortrait && (windowHeight < windowWidth)) {
94-
// swap width/height
95-
int tmp = windowHeight;
96-
windowHeight = windowWidth;
97-
windowWidth = tmp;
98-
} else if (!isPortrait && (windowWidth < windowHeight)) {
99-
// swap width/height
100-
int tmp = windowHeight;
101-
windowHeight = windowWidth;
102-
windowWidth = tmp;
103-
}
108+
}
109+
// If user explicitly overrode orientation, normalize by swapping
110+
if (overrideOrientation && isPortrait == windowHeight < windowWidth) {
111+
int tmp = windowHeight; windowHeight = windowWidth; windowWidth = tmp;
104112
}
105113

106114
if (Config.instance().getSettingData().fullScreen) {
@@ -138,4 +146,10 @@ public void focusLost() {
138146

139147
new Lwjgl3Application(start, config);
140148
}
149+
150+
private static float getPrimaryScreenAspect() {
151+
DisplayMode dm = Lwjgl3ApplicationConfiguration.getDisplayMode();
152+
if (dm == null || dm.height == 0) return 16f / 9f; // sane fallback
153+
return (float) dm.width / (float) dm.height; // width/height
154+
}
141155
}

forge-gui-mobile/src/forge/Forge.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ public class Forge implements ApplicationListener {
128128
public static boolean forcedEnglishonCJKMissing = false;
129129
public static boolean createNewAdventureMap = false;
130130
private static Localizer localizer;
131+
private static boolean desktopAutoOrientation = true;
131132

132-
public static ApplicationListener getApp(HWInfo hwInfo, Clipboard clipboard0, IDeviceAdapter deviceAdapter0, String assetDir0, boolean propertyConfig, boolean androidOrientation, int totalRAM, boolean isTablet, int AndroidAPI) {
133+
public static ApplicationListener getApp(HWInfo hwInfo, Clipboard clipboard0, IDeviceAdapter deviceAdapter0, String assetDir0, boolean propertyConfig, boolean androidOrientation, boolean isTablet, int AndroidAPI) {
133134
if (app == null) {
134135
app = new Forge();
135136
if (GuiBase.getInterface() == null) {
@@ -140,11 +141,11 @@ public static ApplicationListener getApp(HWInfo hwInfo, Clipboard clipboard0, ID
140141
GuiBase.setInterface(new GuiMobile(assetDir0));
141142
GuiBase.enablePropertyConfig(propertyConfig);
142143
isPortraitMode = androidOrientation;
143-
totalDeviceRAM = totalRAM;
144144
isTabletDevice = isTablet;
145145
androidVersion = AndroidAPI;
146146
}
147147
if (hwInfo != null) {
148+
totalDeviceRAM = hwInfo.getTotalRam();
148149
Sentry.configureScope(ScopeType.GLOBAL, scope -> {
149150
scope.getContexts().setDevice(hwInfo.device());
150151
scope.getContexts().setOperatingSystem(hwInfo.os());
@@ -187,9 +188,6 @@ public void create() {
187188
frameRate = new FrameRate();
188189
animationBatch = new SpriteBatch();
189190
inputProcessor = new MainInputProcessor();
190-
//screenWidth and screenHeight should be set initially and only change upon restarting the app
191-
screenWidth = Gdx.app.getGraphics().getWidth();
192-
screenHeight = Gdx.app.getGraphics().getHeight();
193191

194192
Gdx.input.setInputProcessor(inputProcessor);
195193
/*
@@ -202,12 +200,20 @@ the app again (seems it doesnt dispose correctly...?!?)
202200
destroyThis = true; //Prevent back()
203201
if (Files.exists(Paths.get(ForgeConstants.DEFAULT_SKINS_DIR+ForgeConstants.ADV_TEXTURE_BG_FILE)))
204202
selector = getForgePreferences().getPref(FPref.UI_SELECTOR_MODE);
205-
boolean landscapeMode = !isPortraitMode;
203+
204+
//screenWidth and screenHeight should be set initially and only change upon restarting the app
205+
screenWidth = Gdx.app.getGraphics().getWidth();
206+
screenHeight = Gdx.app.getGraphics().getHeight();
207+
// Desktop default: auto-detect from initial window/backbuffer aspect ratio
208+
if (!GuiBase.isAndroid() && desktopAutoOrientation) {
209+
isPortraitMode = screenHeight > screenWidth;
210+
}
206211
//update landscape mode preference if it doesn't match what the app loaded as
207-
if (getForgePreferences().getPrefBoolean(FPref.UI_LANDSCAPE_MODE) != landscapeMode) {
208-
getForgePreferences().setPref(FPref.UI_LANDSCAPE_MODE, landscapeMode);
212+
if (getForgePreferences().getPrefBoolean(FPref.UI_LANDSCAPE_MODE) != isLandscapeMode()) {
213+
getForgePreferences().setPref(FPref.UI_LANDSCAPE_MODE, isLandscapeMode());
209214
getForgePreferences().save();
210215
}
216+
211217
String skinName;
212218
if (FileUtil.doesFileExist(ForgeConstants.MAIN_PREFS_FILE)) {
213219
skinName = getForgePreferences().getPref(FPref.UI_SKIN);
@@ -1653,4 +1659,8 @@ private void translateButtons(Controller controller, int buttonIndex, boolean ke
16531659
if (Controllers.getCurrent() != null)
16541660
System.out.println("Gamepad: " + Controllers.getCurrent().getName());
16551661
}
1662+
1663+
public static void setDesktopAutoOrientation(boolean auto) {
1664+
desktopAutoOrientation = auto;
1665+
}
16561666
}

forge-gui-mobile/src/forge/adventure/util/Config.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ static public Config instance() {
5656
}
5757

5858
private Config() {
59-
6059
String path = resPath();
6160
FilenameFilter planesFilter = (file, s) -> (!s.contains(".") && !s.equals(commonDirectoryName));
6261

File renamed without changes.

0 commit comments

Comments
 (0)