From d24ed357e536dc4978ca79119c915f55303129bb Mon Sep 17 00:00:00 2001 From: Paul Wedeck Date: Sun, 22 Sep 2019 15:31:51 +0200 Subject: [PATCH] fix too small framebuffers when using native scaling, add jsettlers.swing.main wide font scaling --- .../go/graphics/android/AndroidTextDrawer.java | 2 +- .../main/java/go/graphics/swing/AreaContainer.java | 5 +++-- .../main/java/go/graphics/swing/GLContainer.java | 4 ++-- .../swing/contextcreator/BackendSelector.java | 6 ++++-- .../go/graphics/swing/opengl/LWJGLDrawContext.java | 14 ++++++++++---- .../go/graphics/swing/text/LWJGLTextDrawer.java | 4 ++-- .../java/go/graphics/text/AbstractTextDrawer.java | 4 ++-- .../java/jsettlers/main/swing/JSettlersFrame.java | 2 +- .../main/swing/lookandfeel/ui/UIDefaults.java | 4 +++- .../swing/menu/settingsmenu/SettingsMenuPanel.java | 7 ++++++- .../swing/menu/settingsmenu/SettingsSlider.java | 12 +++++++----- .../main/swing/settings/SettingsManager.java | 12 ++++++++++++ .../mapcreator/control/EditorControl.java | 2 +- 13 files changed, 54 insertions(+), 24 deletions(-) diff --git a/go.graphics.android/src/main/java/go/graphics/android/AndroidTextDrawer.java b/go.graphics.android/src/main/java/go/graphics/android/AndroidTextDrawer.java index 49ee24eb3f..e039597e89 100644 --- a/go.graphics.android/src/main/java/go/graphics/android/AndroidTextDrawer.java +++ b/go.graphics.android/src/main/java/go/graphics/android/AndroidTextDrawer.java @@ -11,7 +11,7 @@ public class AndroidTextDrawer extends AbstractTextDrawer { public AndroidTextDrawer(GLESDrawContext gl) { - super(gl); + super(gl, 0); } @Override diff --git a/go.graphics.swing/src/main/java/go/graphics/swing/AreaContainer.java b/go.graphics.swing/src/main/java/go/graphics/swing/AreaContainer.java index d21fc7e5af..83d37a022b 100644 --- a/go.graphics.swing/src/main/java/go/graphics/swing/AreaContainer.java +++ b/go.graphics.swing/src/main/java/go/graphics/swing/AreaContainer.java @@ -44,12 +44,13 @@ public class AreaContainer extends GLContainer implements RedrawListener { * The area to display */ public AreaContainer(Area area) { - this(area, EBackendType.DEFAULT, false); + this(area, EBackendType.DEFAULT, false, 0); } - public AreaContainer(Area area, EBackendType backend, boolean debug) { + public AreaContainer(Area area, EBackendType backend, boolean debug, float guiScale) { super(backend, new BorderLayout(), debug); this.area = area; + this.guiScale = guiScale; if(cc instanceof DrawmodeListener) { area.setDrawmodeListener((DrawmodeListener) cc); diff --git a/go.graphics.swing/src/main/java/go/graphics/swing/GLContainer.java b/go.graphics.swing/src/main/java/go/graphics/swing/GLContainer.java index 581fd17251..68ddc78279 100644 --- a/go.graphics.swing/src/main/java/go/graphics/swing/GLContainer.java +++ b/go.graphics.swing/src/main/java/go/graphics/swing/GLContainer.java @@ -1,7 +1,6 @@ package go.graphics.swing; import org.lwjgl.opengl.GL; -import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GLCapabilities; import java.awt.Component; @@ -24,6 +23,7 @@ public abstract class GLContainer extends JPanel implements GOEventHandlerProvid protected ContextCreator cc; protected LWJGLDrawContext context; private boolean debug; + protected float guiScale = 0; public GLContainer(EBackendType backend, LayoutManager layout, boolean debug) { setLayout(layout); @@ -62,7 +62,7 @@ public void wrapNewContext() { try { if(caps.OpenGL20) { - context = new LWJGLDrawContext(caps, debug); + context = new LWJGLDrawContext(caps, debug, guiScale); } else { errorGLVersion(); } diff --git a/go.graphics.swing/src/main/java/go/graphics/swing/contextcreator/BackendSelector.java b/go.graphics.swing/src/main/java/go/graphics/swing/contextcreator/BackendSelector.java index daee8d9ad1..dad82aca1f 100644 --- a/go.graphics.swing/src/main/java/go/graphics/swing/contextcreator/BackendSelector.java +++ b/go.graphics.swing/src/main/java/go/graphics/swing/contextcreator/BackendSelector.java @@ -36,7 +36,9 @@ public void actionPerformed(ActionEvent actionEvent) { super.actionPerformed(actionEvent); if(actionEvent.getActionCommand().equals("comboBoxChanged")) { - EBackendType bi = (EBackendType) getSelectedItem(); + Object item = getSelectedItem(); + if(item instanceof String) return; + EBackendType bi = (EBackendType) item; if (bi.platform != null && bi.platform != Platform.get()) { setSelectedItem(current_item); BackendSelector.this.hidePopup(); @@ -53,7 +55,7 @@ public BackendSelector() { addActionListener(this); - availableBackends().forEach(backend -> addItem(backend)); + availableBackends().forEach(this::addItem); } private static Stream availableBackends() { diff --git a/go.graphics.swing/src/main/java/go/graphics/swing/opengl/LWJGLDrawContext.java b/go.graphics.swing/src/main/java/go/graphics/swing/opengl/LWJGLDrawContext.java index 2a6b713979..97d9ef2ae4 100644 --- a/go.graphics.swing/src/main/java/go/graphics/swing/opengl/LWJGLDrawContext.java +++ b/go.graphics.swing/src/main/java/go/graphics/swing/opengl/LWJGLDrawContext.java @@ -37,7 +37,7 @@ @SuppressWarnings("WeakerAccess") public class LWJGLDrawContext extends GLDrawContext { - public LWJGLDrawContext(GLCapabilities glcaps, boolean debug) { + public LWJGLDrawContext(GLCapabilities glcaps, boolean debug, float guiScale) { this.glcaps = glcaps; if(debug) debugOutput = new LWJGLDebugOutput(this); @@ -52,7 +52,7 @@ public LWJGLDrawContext(GLCapabilities glcaps, boolean debug) { init(); - textDrawer = new LWJGLTextDrawer(this); + textDrawer = new LWJGLTextDrawer(this, guiScale); } private ArrayList shaders; @@ -179,10 +179,16 @@ public void setGlobalAttributes(float x, float y, float z, float sx, float sy, f } } - + protected float nativeScale = 0; + public void resize(int width, int height) { - GL11.glViewport(0, 0, width, height); + if(nativeScale == 0) { + int[] vp = new int[4]; + GL11.glGetIntegerv(GL11.GL_VIEWPORT, vp); + nativeScale = vp[2] / (float)width; + } + GL11.glViewport(0, 0, (int)(width*nativeScale), (int)(height*nativeScale)); mat.setOrtho(0, width, 0, height, -1, 1); mat.get(matBfr); diff --git a/go.graphics.swing/src/main/java/go/graphics/swing/text/LWJGLTextDrawer.java b/go.graphics.swing/src/main/java/go/graphics/swing/text/LWJGLTextDrawer.java index ed8477c0a3..ab232e31b0 100644 --- a/go.graphics.swing/src/main/java/go/graphics/swing/text/LWJGLTextDrawer.java +++ b/go.graphics.swing/src/main/java/go/graphics/swing/text/LWJGLTextDrawer.java @@ -36,8 +36,8 @@ public final class LWJGLTextDrawer extends AbstractTextDrawer * Creates a new text drawer. * */ - public LWJGLTextDrawer(LWJGLDrawContext drawContext) { - super(drawContext); + public LWJGLTextDrawer(LWJGLDrawContext drawContext, float guiScale) { + super(drawContext, guiScale); } @Override diff --git a/go.graphics/src/main/java/go/graphics/text/AbstractTextDrawer.java b/go.graphics/src/main/java/go/graphics/text/AbstractTextDrawer.java index c1d02d1e1a..65a422145e 100644 --- a/go.graphics/src/main/java/go/graphics/text/AbstractTextDrawer.java +++ b/go.graphics/src/main/java/go/graphics/text/AbstractTextDrawer.java @@ -76,9 +76,9 @@ public abstract class AbstractTextDrawer { * Creates a new text drawer. * */ - public AbstractTextDrawer(T drawContext) { + public AbstractTextDrawer(T drawContext, float guiScale) { this.drawContext = drawContext; - scalingFactor = calculateScalingFactor(); + scalingFactor = guiScale <= 0.51f ? calculateScalingFactor() : guiScale; int descent = init(); generateTexture(); diff --git a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/JSettlersFrame.java b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/JSettlersFrame.java index 54d054144f..92a6baa651 100644 --- a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/JSettlersFrame.java +++ b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/JSettlersFrame.java @@ -172,7 +172,7 @@ public void run() { } SwingUtilities.invokeLater(() -> { - setContentPane(areaContainer = new AreaContainer(area, SettingsManager.getInstance().getBackend(), SettingsManager.getInstance().isGraphicsDebug())); + setContentPane(areaContainer = new AreaContainer(area, SettingsManager.getInstance().getBackend(), SettingsManager.getInstance().isGraphicsDebug(), SettingsManager.getInstance().getGuiScale())); areaContainer.updateFPSLimit(fpsLimit); revalidate(); repaint(); diff --git a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/lookandfeel/ui/UIDefaults.java b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/lookandfeel/ui/UIDefaults.java index 1decfb42b0..dcc34ff42c 100644 --- a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/lookandfeel/ui/UIDefaults.java +++ b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/lookandfeel/ui/UIDefaults.java @@ -17,6 +17,8 @@ import java.awt.Color; import java.awt.Font; +import jsettlers.main.swing.settings.SettingsManager; + /** * Constant colors for L&F * @@ -41,7 +43,7 @@ private UIDefaults() { /** * Default font */ - public static final Font FONT = new Font("Sans", Font.BOLD, 14); + public static final Font FONT = new Font("Sans", Font.BOLD, (int)(14*SettingsManager.getInstance().getGuiScale())); /** * Default font diff --git a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsMenuPanel.java b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsMenuPanel.java index 64a0fdf459..8696cfb5df 100644 --- a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsMenuPanel.java +++ b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsMenuPanel.java @@ -47,6 +47,7 @@ public class SettingsMenuPanel extends JPanel { private final JTextField playerNameField = new JTextField(); private final SettingsSlider volumeSlider = new SettingsSlider("%", 0,100, null); private final SettingsSlider fpsLimitSlider = new SettingsSlider("fps", 0,240, "timerless redraw"); + private final SettingsSlider guiScaleSlider = new SettingsSlider("%", 50,400, "system default"); private final BackendSelector backendSelector = new BackendSelector(); /** @@ -77,6 +78,8 @@ public SettingsMenuPanel(MainMenuPanel mainMenuPanel) { addSetting("settings-fps-limit", fpsLimitSlider); addSetting("settings-backend", backendSelector); + + addSetting("settings-gui-scale", guiScaleSlider); initButton(); } @@ -111,7 +114,8 @@ private void initButton() { settingsManager.setUserName(playerNameField.getText()); settingsManager.setVolume(volumeSlider.getValue() / 100f); settingsManager.setFpsLimit(fpsLimitSlider.getValue()); - settingsManager.setBackend(backendSelector.getSelectedItem().toString()); + settingsManager.setBackend(backendSelector.getSelectedItem()+""); + settingsManager.setGuiScale(guiScaleSlider.getValue()/100f); mainMenuPanel.reset(); }); @@ -128,5 +132,6 @@ public void initializeValues() { volumeSlider.setValue((int) (settingsManager.getVolume() * 100)); fpsLimitSlider.setValue(settingsManager.getFpsLimit()); backendSelector.setSelectedItem(settingsManager.getBackend()); + guiScaleSlider.setValue(Math.round(settingsManager.getGuiScale()*100)); } } diff --git a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsSlider.java b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsSlider.java index e9871a718c..b3bea9ae54 100644 --- a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsSlider.java +++ b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/menu/settingsmenu/SettingsSlider.java @@ -29,13 +29,15 @@ public class SettingsSlider extends SettlersSlider { private static final long serialVersionUID = 1L; private String unit; - private String zeroString; + private String minString; + private int minValue; - public SettingsSlider(String unit, int min_value, int max_value, String zeroString) { + public SettingsSlider(String unit, int min_value, int max_value, String minString) { setStringPainted(true); this.unit = unit; - this.zeroString = zeroString; + this.minString = minString; + minValue = min_value; setMinimum(min_value); setMaximum(max_value); setValue(50); @@ -47,8 +49,8 @@ public SettingsSlider(String unit, int min_value, int max_value, String zeroStri @Override public void setValue(int n) { super.setValue(n); - if(n == 0 && zeroString != null) { - setString(zeroString); + if(n == minValue && minString != null) { + setString(minString); } else { setString(n + unit); } diff --git a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/settings/SettingsManager.java b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/settings/SettingsManager.java index 6394c9e21b..a67bce8a24 100644 --- a/jsettlers.main.swing/src/main/java/jsettlers/main/swing/settings/SettingsManager.java +++ b/jsettlers.main.swing/src/main/java/jsettlers/main/swing/settings/SettingsManager.java @@ -56,6 +56,7 @@ public class SettingsManager implements ISoundSettingsProvider { private static final String SETTING_FULL_SCREEN_MODE = "fullScreenMode"; private static final String SETTING_GRAPHICS_DEBUG = "debug-opengl"; + private static final String SETTINGS_GUI_SCALE = "gui-scale"; private static final String SETTING_CONTROL_ALL = "control-all"; private static final String SETTING_ACTIVATE_ALL_PLAYERS = "activate-all-players"; private static final String SETTING_ENABLE_CONSOLE_LOGGING = "console-output"; @@ -204,6 +205,8 @@ public void setVolume(float volume) { public void setFpsLimit(int fpsLimit) {set(SETTING_FPS_LIMIT,Integer.toString(fpsLimit));} + public void setGuiScale(float scale) {set(SETTINGS_GUI_SCALE, ""+scale);} + public void setBackend(String backend) {set(SETTING_BACKEND, backend);} public void setFullScreenMode(boolean fullScreenMode) { @@ -285,4 +288,13 @@ public void setUserName(String userName) { public boolean isGraphicsDebug() { return getOptional(SETTING_GRAPHICS_DEBUG); } + + public float getGuiScale() { + String guiScaleString = get(SETTINGS_GUI_SCALE); + try { + return Math.max(guiScaleString != null ? Float.parseFloat(guiScaleString) : 1, 0.5f); + } catch (NumberFormatException e) { + } + return 0.5f; + } } diff --git a/jsettlers.mapcreator/src/main/java/jsettlers/mapcreator/control/EditorControl.java b/jsettlers.mapcreator/src/main/java/jsettlers/mapcreator/control/EditorControl.java index bf1283e997..ac3dff0af0 100644 --- a/jsettlers.mapcreator/src/main/java/jsettlers/mapcreator/control/EditorControl.java +++ b/jsettlers.mapcreator/src/main/java/jsettlers/mapcreator/control/EditorControl.java @@ -283,7 +283,7 @@ public void buildMapEditingWindow() { Area area = new Area(); final Region region = new Region(Region.POSITION_CENTER); area.set(region); - displayPanel = new AreaContainer(area, SettingsManager.getInstance().getBackend(), SettingsManager.getInstance().isGraphicsDebug()); + displayPanel = new AreaContainer(area, SettingsManager.getInstance().getBackend(), SettingsManager.getInstance().isGraphicsDebug(), SettingsManager.getInstance().getGuiScale()); displayPanel.setMinimumSize(new Dimension(640, 480)); displayPanel.setFocusable(true); root.add(displayPanel, BorderLayout.CENTER);