Add UI scale option to options menu.

This commit is contained in:
Pieter-Jan Briers
2020-09-07 12:59:27 +02:00
parent b712abbec1
commit 03d6697323

View File

@@ -5,6 +5,7 @@ using Robust.Client.UserInterface.Controls;
using Robust.Shared.Interfaces.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
namespace Content.Client.UserInterface
{
@@ -12,13 +13,24 @@ namespace Content.Client.UserInterface
{
private sealed class GraphicsControl : Control
{
private static readonly float[] UIScaleOptions =
{
0f,
0.75f,
1f,
1.25f,
1.50f,
1.75f,
2f
};
private readonly IConfigurationManager _cfg;
private readonly Button ApplyButton;
private readonly CheckBox VSyncCheckBox;
private readonly CheckBox FullscreenCheckBox;
private readonly OptionButton LightingPresetOption;
private readonly OptionButton _uiScaleOption;
public GraphicsControl(IConfigurationManager cfg)
{
@@ -42,7 +54,7 @@ namespace Content.Client.UserInterface
LightingPresetOption.AddItem(Loc.GetString("High"));
LightingPresetOption.OnItemSelected += OnLightingQualityChanged;
var lightingHBox = new HBoxContainer
contents.AddChild(new HBoxContainer
{
Children =
{
@@ -50,8 +62,7 @@ namespace Content.Client.UserInterface
new Control {CustomMinimumSize = (4, 0)},
LightingPresetOption
}
};
contents.AddChild(lightingHBox);
});
ApplyButton = new Button
{
@@ -61,10 +72,24 @@ namespace Content.Client.UserInterface
var resourceCache = IoCManager.Resolve<IResourceCache>();
contents.AddChild(new Placeholder(resourceCache)
_uiScaleOption = new OptionButton();
_uiScaleOption.AddItem(Loc.GetString("Automatic ({0}%)", UserInterfaceManager.DefaultUIScale * 100));
_uiScaleOption.AddItem(Loc.GetString("75%"));
_uiScaleOption.AddItem(Loc.GetString("100%"));
_uiScaleOption.AddItem(Loc.GetString("125%"));
_uiScaleOption.AddItem(Loc.GetString("150%"));
_uiScaleOption.AddItem(Loc.GetString("175%"));
_uiScaleOption.AddItem(Loc.GetString("200%"));
_uiScaleOption.OnItemSelected += OnUIScaleChanged;
contents.AddChild(new HBoxContainer
{
SizeFlagsVertical = SizeFlags.FillExpand,
PlaceholderText = "UI Scaling"
Children =
{
new Label {Text = Loc.GetString("UI Scale:")},
new Control {CustomMinimumSize = (4, 0)},
_uiScaleOption
}
});
contents.AddChild(new Placeholder(resourceCache)
@@ -99,17 +124,24 @@ namespace Content.Client.UserInterface
VSyncCheckBox.Pressed = _cfg.GetCVar<bool>("display.vsync");
FullscreenCheckBox.Pressed = ConfigIsFullscreen;
LightingPresetOption.SelectId(GetConfigLightingQuality());
_uiScaleOption.SelectId(GetConfigUIScalePreset(ConfigUIScale));
AddChild(vBox);
}
private void OnUIScaleChanged(OptionButton.ItemSelectedEventArgs args)
{
_uiScaleOption.SelectId(args.Id);
UpdateApplyButton();
}
private void OnApplyButtonPressed(BaseButton.ButtonEventArgs args)
{
_cfg.SetCVar("display.vsync", VSyncCheckBox.Pressed);
SetConfigLightingQuality(LightingPresetOption.SelectedId);
_cfg.SetCVar("display.windowmode",
(int) (FullscreenCheckBox.Pressed ? WindowMode.Fullscreen : WindowMode.Windowed));
_cfg.SetCVar("display.uiScale", UIScaleOptions[_uiScaleOption.SelectedId]);
_cfg.SaveToFile();
UpdateApplyButton();
}
@@ -130,12 +162,15 @@ namespace Content.Client.UserInterface
var isVSyncSame = VSyncCheckBox.Pressed == _cfg.GetCVar<bool>("display.vsync");
var isFullscreenSame = FullscreenCheckBox.Pressed == ConfigIsFullscreen;
var isLightingQualitySame = LightingPresetOption.SelectedId == GetConfigLightingQuality();
ApplyButton.Disabled = isVSyncSame && isFullscreenSame && isLightingQualitySame;
var isUIScaleSame = MathHelper.CloseTo(UIScaleOptions[_uiScaleOption.SelectedId], ConfigUIScale);
ApplyButton.Disabled = isVSyncSame && isFullscreenSame && isLightingQualitySame && isUIScaleSame;
}
private bool ConfigIsFullscreen =>
_cfg.GetCVar<int>("display.windowmode") == (int) WindowMode.Fullscreen;
private float ConfigUIScale => _cfg.GetCVar<float>("display.uiScale");
private int GetConfigLightingQuality()
{
var val = _cfg.GetCVar<int>("display.lightmapdivider");
@@ -180,6 +215,19 @@ namespace Content.Client.UserInterface
break;
}
}
private static int GetConfigUIScalePreset(float value)
{
for (var i = 0; i < UIScaleOptions.Length; i++)
{
if (MathHelper.CloseTo(UIScaleOptions[i], value))
{
return i;
}
}
return 0;
}
}
}
}