Caps the automatic font size at no more than 25% larger than the current scaleFactor. In debug mode, resizing the application frame now updates the scaleFactor (if automatic). This behavior can only be enabled outside of the debug mode when all panels are automatically refreshed when the scalingFactor and/or font size changes.

This commit is contained in:
Stian Grenborgen 2024-01-21 10:08:57 +01:00
parent 697096c0da
commit 9657c8a65d
4 changed files with 61 additions and 18 deletions

View File

@ -277,6 +277,8 @@ public final class Canvas extends JDesktopPane {
// Internals // Internals
private void updateSize() { private void updateSize() {
freeColClient.getGUI().refreshScaleFactorIfNecessary();
Dimension size = getSize(); Dimension size = getSize();
if (oldSize.width != size.width || oldSize.height != size.height) { if (oldSize.width != size.width || oldSize.height != size.height) {
logger.info("Canvas resize from " + oldSize + " to " + size); logger.info("Canvas resize from " + oldSize + " to " + size);

View File

@ -2639,4 +2639,11 @@ public class GUI extends FreeColClientHolder {
public void emergencyPurge() { public void emergencyPurge() {
} }
/**
* Updates the scaleFactor, if necessary, after the window has been resized.
*/
public void refreshScaleFactorIfNecessary() {
}
} }

View File

@ -59,7 +59,6 @@ import net.sf.freecol.client.ClientOptions;
import net.sf.freecol.client.FreeColClient; import net.sf.freecol.client.FreeColClient;
import net.sf.freecol.client.control.MapTransform; import net.sf.freecol.client.control.MapTransform;
import net.sf.freecol.client.control.SoundController; import net.sf.freecol.client.control.SoundController;
import net.sf.freecol.client.gui.SwingGUI.PopupPosition;
import net.sf.freecol.client.gui.animation.Animation; import net.sf.freecol.client.gui.animation.Animation;
import net.sf.freecol.client.gui.animation.Animations; import net.sf.freecol.client.gui.animation.Animations;
// Special dialogs and panels // Special dialogs and panels
@ -73,7 +72,6 @@ import net.sf.freecol.client.gui.mapviewer.MapViewerState;
import net.sf.freecol.client.gui.mapviewer.TileViewer; import net.sf.freecol.client.gui.mapviewer.TileViewer;
import net.sf.freecol.client.gui.panel.ColonyPanel; import net.sf.freecol.client.gui.panel.ColonyPanel;
import net.sf.freecol.client.gui.panel.CornerMapControls; import net.sf.freecol.client.gui.panel.CornerMapControls;
import net.sf.freecol.client.gui.panel.ErrorPanel;
import net.sf.freecol.client.gui.panel.FreeColImageBorder; import net.sf.freecol.client.gui.panel.FreeColImageBorder;
import net.sf.freecol.client.gui.panel.FreeColPanel; import net.sf.freecol.client.gui.panel.FreeColPanel;
import net.sf.freecol.client.gui.panel.InformationPanel; import net.sf.freecol.client.gui.panel.InformationPanel;
@ -93,6 +91,7 @@ import net.sf.freecol.client.gui.plaf.FreeColToolTipUI;
import net.sf.freecol.common.FreeColException; import net.sf.freecol.common.FreeColException;
import net.sf.freecol.common.MemoryManager; import net.sf.freecol.common.MemoryManager;
import net.sf.freecol.common.debug.DebugUtils; import net.sf.freecol.common.debug.DebugUtils;
import net.sf.freecol.common.debug.FreeColDebugger;
import net.sf.freecol.common.i18n.Messages; import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.io.FreeColDataFile; import net.sf.freecol.common.io.FreeColDataFile;
import net.sf.freecol.common.metaserver.ServerInfo; import net.sf.freecol.common.metaserver.ServerInfo;
@ -2037,23 +2036,28 @@ public class SwingGUI extends GUI {
} }
final int displayScaling = getClientOptions().getInteger(ClientOptions.DISPLAY_SCALING); final int displayScaling = getClientOptions().getInteger(ClientOptions.DISPLAY_SCALING);
if (displayScaling == 0) { final float scaleFactor = determineScaleFactorUsingClientOptions(dpi);
int fontSize = (int) ((FontLibrary.DEFAULT_UNSCALED_MAIN_FONT_SIZE * dpi) / DEFAULT_DPI); final int fontSizeUsingScaling = (int) (FontLibrary.DEFAULT_UNSCALED_MAIN_FONT_SIZE * scaleFactor);
final int screenHeight = graphicsDevice.getDisplayMode().getHeight(); if (displayScaling != 0) {
if (screenHeight < 900) { logger.info("Font size based on manual display scaling: " + fontSizeUsingScaling + " (reported DPI: " + dpi + ")");
fontSize = Math.min(14, fontSize); return fontSizeUsingScaling;
} else if (screenHeight < 1050) {
fontSize = Math.min(18, fontSize);
}
logger.info("Automatic font size: " + fontSize + " (reported DPI: " + dpi + ", screen height: " + screenHeight + ")");
return fontSize;
} }
final int fontSize = (int) (FontLibrary.DEFAULT_UNSCALED_MAIN_FONT_SIZE * (displayScaling / 100f)); int fontSizeUsingDpi = (int) ((FontLibrary.DEFAULT_UNSCALED_MAIN_FONT_SIZE * dpi) / DEFAULT_DPI);
logger.info("Font size based on manual display scaling: " + fontSize + " (reported DPI: " + dpi + ")"); final int screenHeight = graphicsDevice.getDisplayMode().getHeight();
return fontSize; if (screenHeight < 900) {
fontSizeUsingDpi = Math.min(14, fontSizeUsingDpi);
} else if (screenHeight < 1050) {
fontSizeUsingDpi = Math.min(18, fontSizeUsingDpi);
}
if (fontSizeUsingDpi >= fontSizeUsingScaling * 0.25f) {
return fontSizeUsingScaling;
}
logger.info("Automatic font size: " + fontSizeUsingDpi + " (reported DPI: " + dpi + ", screen height: " + screenHeight + ")");
return fontSizeUsingDpi;
} }
private float determineScaleFactorUsingClientOptions(final int dpi) { private float determineScaleFactorUsingClientOptions(final int dpi) {
@ -2073,7 +2077,12 @@ public class SwingGUI extends GUI {
scaleFactor = 2F; scaleFactor = 2F;
} }
final int screenHeight = graphicsDevice.getDisplayMode().getHeight(); final int screenHeight;
if (FreeColDebugger.isAutomaticRescalingOnWindowResize()) {
screenHeight = (canvas != null && canvas.getParentFrame() != null) ? canvas.getParentFrame().getHeight() : graphicsDevice.getDisplayMode().getHeight();
} else {
screenHeight = graphicsDevice.getDisplayMode().getHeight();
}
if (screenHeight < 720) { if (screenHeight < 720) {
scaleFactor = 0.75F; scaleFactor = 0.75F;
} else if (screenHeight < 900) { } else if (screenHeight < 900) {
@ -2872,4 +2881,19 @@ public class SwingGUI extends GUI {
public boolean canGameChangingModsBeAdded() { public boolean canGameChangingModsBeAdded() {
return getFreeColClient().getGame() == null; return getFreeColClient().getGame() == null;
} }
@Override
public void refreshScaleFactorIfNecessary() {
if (!FreeColDebugger.isAutomaticRescalingOnWindowResize()) {
return;
}
final int dpi = Utils.determineDpi(graphicsDevice);
final float scaleFactor = determineScaleFactorUsingClientOptions(dpi);
if (Math.abs(getFixedImageLibrary().getScaleFactor() - scaleFactor) <= 0.1) {
return;
}
refreshGuiUsingClientOptions();
}
} }

View File

@ -114,6 +114,16 @@ public class FreeColDebugger {
public static boolean isInDebugMode() { public static boolean isInDebugMode() {
return FreeColDebugger.debugMode != 0; return FreeColDebugger.debugMode != 0;
} }
/**
* Checks if automatic changing of the scaleFactor on window resize
* has been enabled.
*
* @return {@code true} if any debug mode is enabled.
*/
public static boolean isAutomaticRescalingOnWindowResize() {
return FreeColDebugger.debugMode != 0;
}
/** /**
* Is a particular debug mode enabled in this game? * Is a particular debug mode enabled in this game?