You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.8 KiB
101 lines
2.8 KiB
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Savegame;
|
|
|
|
/// <summary>
|
|
/// Tracks important window settings and communicates with the <see cref="SettingsSaveController"/> to save/load them.
|
|
/// </summary>
|
|
public partial class WindowSettingsSync : Node
|
|
{
|
|
private Window window;
|
|
|
|
public override void _Ready()
|
|
{
|
|
window = GetWindow();
|
|
window.SizeChanged += SaveWindowSize;
|
|
|
|
SyncSettings();
|
|
SettingsSaveController.Instance.OnSettingsReloaded += SyncSettings;
|
|
}
|
|
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
SaveWindowPosition();
|
|
SaveWindowBorderless();
|
|
SaveWindowSize();
|
|
SettingsSaveController.Instance.SaveSettings();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries to get previous settings from settings-savefile, if available.
|
|
/// </summary>
|
|
public void SyncSettings()
|
|
{
|
|
if (!SettingsSaveController.Instance.LoadedData)
|
|
{
|
|
SaveWindowPosition();
|
|
SaveWindowSize();
|
|
SaveWindowBorderless();
|
|
return;
|
|
}
|
|
|
|
SettingsData? settingsData = SettingsSaveController.Instance.settings;
|
|
if (settingsData != null)
|
|
{
|
|
window.Position = new Vector2I(settingsData.windowPositionX, settingsData.windowPositionY);
|
|
ValidateWindowPosition();
|
|
|
|
window.Size = new Vector2I(settingsData.windowSizeX, settingsData.windowSizeY);
|
|
window.Borderless = settingsData.windowBorderless;
|
|
}
|
|
}
|
|
|
|
private void ValidateWindowPosition()
|
|
{
|
|
bool validWindowPosition = false;
|
|
foreach (Rect2I displayRect in DisplayServer.GetDisplayCutouts())
|
|
{
|
|
if (displayRect.HasPoint(window.Position))
|
|
{
|
|
validWindowPosition = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!validWindowPosition)
|
|
{
|
|
window.MoveToCenter();
|
|
SaveWindowPosition();
|
|
}
|
|
}
|
|
|
|
private void SaveWindowPosition()
|
|
{
|
|
SettingsData? settingsData = SettingsSaveController.Instance.settings;
|
|
|
|
if (settingsData != null)
|
|
{
|
|
settingsData.windowPositionX = window.Position.X;
|
|
settingsData.windowPositionY = window.Position.Y;
|
|
}
|
|
}
|
|
|
|
private void SaveWindowSize()
|
|
{
|
|
SettingsData? settingsData = SettingsSaveController.Instance.settings;
|
|
if (settingsData != null)
|
|
{
|
|
settingsData.windowSizeX = window.Size.X;
|
|
settingsData.windowSizeY = window.Size.Y;
|
|
}
|
|
}
|
|
|
|
private void SaveWindowBorderless()
|
|
{
|
|
SettingsData? settingsData = SettingsSaveController.Instance.settings;
|
|
if (settingsData != null)
|
|
settingsData.windowBorderless = window.Borderless;
|
|
}
|
|
|
|
} |