Pong/Scripts/Managers/UI/Menus/Settings.cs

81 lines
2.3 KiB
C#
Raw Normal View History

namespace Pong.Scripts.Managers.UI.Menus;
public partial class Settings : BaseMenu
{
private SettingsManager _settings;
private SpinBox _scale;
private OptionButton _vSync;
private Button _saveButton;
private bool _scaleModified;
private bool _vSyncModified;
public override void _EnterTree()
{
base._EnterTree();
GetNodes();
_scale.Value = _settings.SettingsData.Scale;
_vSync.Selected = (int)_settings.SettingsData.VSync;
}
private void OnBackButtonPressed()
{
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()
{
using var mainContainer = GetNode<VBoxContainer>("ScrollContainer/InternalMainContainer");
_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();
}
/// <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)
{
_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, VSync = _vSync.Selected})) return;
_saveButton.Disabled = true;
_scaleModified = false;
_vSyncModified = false;
}
}