Skip to content

Commit

Permalink
fix too small framebuffers when using native scaling,
Browse files Browse the repository at this point in the history
add jsettlers.swing.main wide font scaling
  • Loading branch information
paulwedeck committed Sep 22, 2019
1 parent 26330b5 commit d24ed35
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class AndroidTextDrawer extends AbstractTextDrawer<GLESDrawContext> {

public AndroidTextDrawer(GLESDrawContext gl) {
super(gl);
super(gl, 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -62,7 +62,7 @@ public void wrapNewContext() {

try {
if(caps.OpenGL20) {
context = new LWJGLDrawContext(caps, debug);
context = new LWJGLDrawContext(caps, debug, guiScale);
} else {
errorGLVersion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -53,7 +55,7 @@ public BackendSelector() {

addActionListener(this);

availableBackends().forEach(backend -> addItem(backend));
availableBackends().forEach(this::addItem);
}

private static Stream<EBackendType> availableBackends() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -52,7 +52,7 @@ public LWJGLDrawContext(GLCapabilities glcaps, boolean debug) {

init();

textDrawer = new LWJGLTextDrawer(this);
textDrawer = new LWJGLTextDrawer(this, guiScale);
}

private ArrayList<ShaderProgram> shaders;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public final class LWJGLTextDrawer extends AbstractTextDrawer<LWJGLDrawContext>
* Creates a new text drawer.
*
*/
public LWJGLTextDrawer(LWJGLDrawContext drawContext) {
super(drawContext);
public LWJGLTextDrawer(LWJGLDrawContext drawContext, float guiScale) {
super(drawContext, guiScale);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public abstract class AbstractTextDrawer<T extends GLDrawContext> {
* 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.awt.Color;
import java.awt.Font;

import jsettlers.main.swing.settings.SettingsManager;

/**
* Constant colors for L&F
*
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand Down Expand Up @@ -77,6 +78,8 @@ public SettingsMenuPanel(MainMenuPanel mainMenuPanel) {
addSetting("settings-fps-limit", fpsLimitSlider);

addSetting("settings-backend", backendSelector);

addSetting("settings-gui-scale", guiScaleSlider);

initButton();
}
Expand Down Expand Up @@ -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();
});

Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d24ed35

Please sign in to comment.