2023-05-21 04:42:38 +00:00
|
|
|
namespace Pong.Scripts.Managers.UI.Menus;
|
|
|
|
|
|
|
|
public partial class Settings : BaseMenu
|
|
|
|
{
|
|
|
|
private SettingsManager _settings;
|
|
|
|
private SpinBox _scale;
|
2023-05-27 02:50:14 +00:00
|
|
|
private OptionButton _vSync;
|
2023-05-21 04:42:38 +00:00
|
|
|
private Button _saveButton;
|
|
|
|
|
|
|
|
private bool _scaleModified;
|
2023-05-27 02:50:14 +00:00
|
|
|
private bool _vSyncModified;
|
2023-05-21 04:42:38 +00:00
|
|
|
|
2023-05-27 02:50:14 +00:00
|
|
|
public override void _EnterTree()
|
2023-05-21 04:42:38 +00:00
|
|
|
{
|
2023-05-27 02:50:14 +00:00
|
|
|
base._EnterTree();
|
2023-05-21 04:42:38 +00:00
|
|
|
GetNodes();
|
|
|
|
|
|
|
|
_scale.Value = _settings.SettingsData.Scale;
|
2023-05-27 02:50:14 +00:00
|
|
|
_vSync.Selected = (int)_settings.SettingsData.VSync;
|
2023-05-21 04:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnBackButtonPressed()
|
|
|
|
{
|
|
|
|
GetTree().ChangeSceneToFile("res://Scenes/UI/Menus/MainMenu.tscn");
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:50:14 +00:00
|
|
|
/// <summary>
|
|
|
|
/// get references to the nodes required for the settings menu to function and bind to events.
|
|
|
|
/// </summary>
|
2023-05-21 04:42:38 +00:00
|
|
|
private void GetNodes()
|
|
|
|
{
|
2023-05-27 02:50:14 +00:00
|
|
|
using var mainContainer = GetNode<VBoxContainer>("ScrollContainer/InternalMainContainer");
|
|
|
|
|
2023-05-21 04:42:38 +00:00
|
|
|
_settings = GetNode<SettingsManager>("/root/SettingsManager");
|
2023-05-27 02:50:14 +00:00
|
|
|
_scale = mainContainer.GetNode<SpinBox>("ScaleContainer/Scale");
|
|
|
|
_vSync = mainContainer.GetNode<OptionButton>("VSyncContainer/VSyncOptions");
|
2023-05-21 04:42:38 +00:00
|
|
|
_saveButton = GetNode<Button>("SaveButton");
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:50:14 +00:00
|
|
|
/// <summary>
|
|
|
|
/// method fired every time the scale value is changed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="value">the new value</param>
|
2023-05-21 04:42:38 +00:00
|
|
|
private void ScaleChanged(double value)
|
|
|
|
{
|
|
|
|
_scaleModified = !value.Equals(_settings.SettingsData.Scale);
|
|
|
|
CheckIfModified();
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:50:14 +00:00
|
|
|
/// <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()
|
2023-05-21 04:42:38 +00:00
|
|
|
{
|
2023-05-27 02:50:14 +00:00
|
|
|
var isModified = _scaleModified || _vSyncModified;
|
2023-05-21 04:42:38 +00:00
|
|
|
_saveButton.Disabled = !isModified;
|
|
|
|
}
|
|
|
|
|
2023-05-27 02:50:14 +00:00
|
|
|
/// <summary>
|
|
|
|
/// saves the new settings to a json file and disables the save button if that operation happens successfully.
|
|
|
|
/// </summary>
|
2023-05-21 04:42:38 +00:00
|
|
|
private async void OnSaveButtonPressed()
|
|
|
|
{
|
2023-05-27 02:50:14 +00:00
|
|
|
if (!await _settings.SaveSettings(_settings.SettingsData with { Scale = _scale.Value, VSync = _vSync.Selected})) return;
|
2023-05-21 04:42:38 +00:00
|
|
|
_saveButton.Disabled = true;
|
2023-05-27 02:50:14 +00:00
|
|
|
_scaleModified = false;
|
|
|
|
_vSyncModified = false;
|
2023-05-21 04:42:38 +00:00
|
|
|
}
|
|
|
|
}
|