add a vsync option to the settings

i added a vsync option to the settings that lets you pick a vsync option that maps to the engines vsync enum. so you can either enable, disable it, turn it to adapt mode, or mailbox mode.
This commit is contained in:
Fries 2023-05-26 19:50:14 -07:00
parent 8072cf4961
commit 9fdf16c557
7 changed files with 95 additions and 31 deletions

View file

@ -3,9 +3,11 @@
[ext_resource type="FontFile" uid="uid://dq8ojwul8884x" path="res://Fonts/AtkinsonHyperlegible-Regular.ttf" id="1_aadm8"]
[resource]
default_font = ExtResource("1_aadm8")
Button/font_sizes/font_size = 32
Button/fonts/font = ExtResource("1_aadm8")
Label/font_sizes/font_size = 32
Label/fonts/font = ExtResource("1_aadm8")
LineEdit/font_sizes/font_size = 32
LineEdit/fonts/font = ExtResource("1_aadm8")
PopupMenu/font_sizes/font_size = 32

View file

@ -34,8 +34,7 @@ size_flags_horizontal = 4
[node name="Label" type="Label" parent="ScrollContainer/InternalMainContainer/ScaleContainer"]
layout_mode = 2
size_flags_horizontal = 4
text = "Scale
"
text = "Scale"
[node name="Scale" type="SpinBox" parent="ScrollContainer/InternalMainContainer/ScaleContainer"]
layout_mode = 2
@ -45,6 +44,30 @@ step = 0.01
value = 1.0
alignment = 1
[node name="VSyncContainer" type="VBoxContainer" parent="ScrollContainer/InternalMainContainer"]
layout_mode = 2
size_flags_horizontal = 4
alignment = 1
[node name="Label" type="Label" parent="ScrollContainer/InternalMainContainer/VSyncContainer"]
layout_mode = 2
text = "VSync"
horizontal_alignment = 1
[node name="VSyncOptions" type="OptionButton" parent="ScrollContainer/InternalMainContainer/VSyncContainer"]
layout_mode = 2
item_count = 4
selected = 1
fit_to_longest_item = false
popup/item_0/text = "Disabled"
popup/item_0/id = 0
popup/item_1/text = "Enabled"
popup/item_1/id = 1
popup/item_2/text = "Adaptive"
popup/item_2/id = 2
popup/item_3/text = "Mailbox"
popup/item_3/id = 3
[node name="SaveButton" type="Button" parent="."]
layout_mode = 2
disabled = true
@ -59,5 +82,7 @@ theme_override_font_sizes/font_size = 32
text = "Back
"
[connection signal="value_changed" from="ScrollContainer/InternalMainContainer/ScaleContainer/Scale" to="." method="ScaleChanged"]
[connection signal="item_selected" from="ScrollContainer/InternalMainContainer/VSyncContainer/VSyncOptions" to="." method="VSyncChanged"]
[connection signal="pressed" from="SaveButton" to="." method="OnSaveButtonPressed"]
[connection signal="pressed" from="BackButton" to="." method="OnBackButtonPressed"]

View file

@ -3,7 +3,7 @@ namespace Pong.Scripts.Data;
/// <summary>
/// a record class that represents the game area.
/// </summary>
public record class GameArea
public record GameArea
{
/// <summary>
/// the X coordinate of the GameArea wrapped in a struct that makes operating on it easier. this is divided
@ -28,7 +28,6 @@ public record class GameArea
return new Vector2(normalizedX, normalizedY);
}
/// <summary>
/// take a normalized position and convert it back into a globalized position.
/// </summary>

View file

@ -10,8 +10,14 @@ public struct Settings
/// </summary>
public double Scale;
/// <summary>
/// options for VSync. 0 is Disabled, 1 is Enabled, 2 is Adaptive, 3 is Mailbox. These match the <see cref="DisplayServer.VSyncMode"/> enum on
/// the <see cref="DisplayServer"/>.
/// </summary>
public long VSync;
/// <summary>
/// the default values of settings.
/// </summary>
public static Settings Default => new() { Scale = 1.0 };
public static Settings Default => new() { Scale = 1.0, VSync = (long)DisplayServer.VSyncMode.Enabled };
}

View file

@ -7,32 +7,30 @@ public partial class BaseMenu : VBoxContainer
public override void _EnterTree()
{
_settingsManager = GetNode<SettingsManager>("/root/SettingsManager");
_settingsManager.SettingsChanged += AdaptUiToGameResolution;
_settingsManager.SettingsChanged += SettingsChanged;
GetTree().Root.SizeChanged += AdaptUiToGameResolution;
AdaptUiToGameResolution();
EnterTree();
}
public override void _ExitTree()
{
GetTree().Root.SizeChanged -= AdaptUiToGameResolution;
_settingsManager.SettingsChanged -= AdaptUiToGameResolution;
ExitTree();
_settingsManager.SettingsChanged -= SettingsChanged;
}
protected virtual void EnterTree()
{
}
protected virtual void ExitTree()
/// <summary>
/// method that fires when the settings get changed.
/// </summary>
private void SettingsChanged()
{
AdaptUiToGameResolution();
DisplayServer.WindowSetVsyncMode((DisplayServer.VSyncMode)_settingsManager.SettingsData.VSync);
}
/// <summary>
/// change the size of the UI container to the current resolution of the game.
/// </summary>
protected void AdaptUiToGameResolution()
private void AdaptUiToGameResolution()
{
var scale = _settingsManager.SettingsData.Scale;
Size = (Vector2)DisplayServer.WindowGetSize() / scale;

View file

@ -4,15 +4,19 @@ public partial class Settings : BaseMenu
{
private SettingsManager _settings;
private SpinBox _scale;
private OptionButton _vSync;
private Button _saveButton;
private bool _scaleModified;
private bool _vSyncModified;
protected override void EnterTree()
public override void _EnterTree()
{
base._EnterTree();
GetNodes();
_scale.Value = _settings.SettingsData.Scale;
_vSync.Selected = (int)_settings.SettingsData.VSync;
}
private void OnBackButtonPressed()
@ -20,31 +24,57 @@ public partial class Settings : BaseMenu
GetTree().ChangeSceneToFile("res://Scenes/UI/Menus/MainMenu.tscn");
}
/// <summary>
/// get references to the nodes required for the settings menu to function and bind to events.
/// </summary>
private void GetNodes()
{
_settings = GetNode<SettingsManager>("/root/SettingsManager");
_scale = GetNode<SpinBox>("ScrollContainer/InternalMainContainer/ScaleContainer/Scale");
_saveButton = GetNode<Button>("SaveButton");
using var mainContainer = GetNode<VBoxContainer>("ScrollContainer/InternalMainContainer");
_scale.ValueChanged += ScaleChanged;
_settings = GetNode<SettingsManager>("/root/SettingsManager");
_scale = mainContainer.GetNode<SpinBox>("ScaleContainer/Scale");
_vSync = mainContainer.GetNode<OptionButton>("VSyncContainer/VSyncOptions");
_saveButton = GetNode<Button>("SaveButton");
}
/// <summary>
/// method fired every time the scale value is changed.
/// </summary>
/// <param name="value">the new value</param>
private void ScaleChanged(double value)
{
_scaleModified = !value.Equals(_settings.SettingsData.Scale);
CheckIfModified();
}
private bool CheckIfModified()
/// <summary>
/// method fired every time the VSync value is changed.
/// </summary>
/// <param name="index">the index of the VSync options box.</param>
private void VSyncChanged(long index)
{
var isModified = _scaleModified;
_saveButton.Disabled = !isModified;
return isModified;
_vSyncModified = index != _settings.SettingsData.VSync;
CheckIfModified();
}
/// <summary>
/// changes the isModified bool to true if either of the modified booleans are true and enables the save button if
/// the isModified bool is true.
/// </summary>
private void CheckIfModified()
{
var isModified = _scaleModified || _vSyncModified;
_saveButton.Disabled = !isModified;
}
/// <summary>
/// saves the new settings to a json file and disables the save button if that operation happens successfully.
/// </summary>
private async void OnSaveButtonPressed()
{
if (!await _settings.SaveSettings(_settings.SettingsData with { Scale = _scale.Value })) return;
if (!await _settings.SaveSettings(_settings.SettingsData with { Scale = _scale.Value, VSync = _vSync.Selected})) return;
_saveButton.Disabled = true;
_scaleModified = false;
_vSyncModified = false;
}
}

View file

@ -57,6 +57,10 @@ scene_reload={
]
}
[physics]
2d/run_on_separate_thread=true
[rendering]
renderer/rendering_method="gl_compatibility"