Add options menu persistence

This commit is contained in:
2025-10-30 23:59:05 -07:00
parent dc3c458414
commit 9fc875eda5
18 changed files with 333 additions and 26 deletions

View File

@@ -0,0 +1,109 @@
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Chickensoft.Serialization;
using Godot;
namespace Zennysoft.Game.Ma;
[Meta(typeof(IAutoNode))]
public partial class OptionsMenu : Control
{
public override void _Notification(int what) => this.Notify(what);
[Node] public OptionButton ResolutionOptions { get; set; }
[Node] public HSlider MasterVolumeSlider { get; set; }
[Node] public HSlider MusicVolumeSlider { get; set; }
[Node] public HSlider SFXVolumeSlider { get; set; }
[Node] public Button SaveAndExitButton { get; set; }
public OptionsData OptionsData;
private int _masterBusIndex;
private int _musicBusIndex;
private int _sfxBusIndex;
private readonly DisplayServer.WindowMode[] _windowModes = [DisplayServer.WindowMode.Windowed, DisplayServer.WindowMode.Maximized, DisplayServer.WindowMode.Fullscreen, DisplayServer.WindowMode.ExclusiveFullscreen];
[Signal] public delegate void OptionsMenuExitedEventHandler();
public void OnReady()
{
ResolutionOptions.AddItem("Windowed");
ResolutionOptions.AddItem("Maximized");
ResolutionOptions.AddItem("Fullscreen");
ResolutionOptions.AddItem("Exclusive Fullscreen");
ResolutionOptions.Select(0);
OptionsData = new OptionsData()
{
MasterVolumeLevel = MasterVolumeSlider.Value,
MusicVolumeLevel = MusicVolumeSlider.Value,
SFXVolumeLevel = SFXVolumeSlider.Value,
ScreenResolution = ResolutionOptions.GetSelectedId()
};
MasterVolumeSlider.ValueChanged += MasterVolumeSlider_Changed;
MusicVolumeSlider.ValueChanged += MusicVolumeSlider_Changed;
SFXVolumeSlider.ValueChanged += SFXVolumeSlider_Changed;
ResolutionOptions.ItemSelected += ResolutionOptions_ItemSelected;
SaveAndExitButton.ButtonUp += SaveAndExitButton_ButtonUp;
_masterBusIndex = AudioServer.GetBusIndex("Master");
_musicBusIndex = AudioServer.GetBusIndex("MUSIC");
_sfxBusIndex = AudioServer.GetBusIndex("SFX");
}
private void ResolutionOptions_ItemSelected(long index)
{
var resolutionIndex = ResolutionOptions.GetSelectedId();
OptionsData.ScreenResolution = resolutionIndex;
DisplayServer.WindowSetMode(_windowModes[resolutionIndex]);
}
public void Load(OptionsData optionsData)
{
MasterVolumeSlider.Value = optionsData.MasterVolumeLevel;
MusicVolumeSlider.Value = optionsData.MusicVolumeLevel;
SFXVolumeSlider.Value = optionsData.SFXVolumeLevel;
ResolutionOptions.Select(optionsData.ScreenResolution);
DisplayServer.WindowSetMode(_windowModes[optionsData.ScreenResolution]);
}
private void SaveAndExitButton_ButtonUp() => EmitSignal(SignalName.OptionsMenuExited);
private void MasterVolumeSlider_Changed(double valueChanged)
{
OptionsData.MasterVolumeLevel = valueChanged;
AudioServer.SetBusVolumeDb(_masterBusIndex, Mathf.LinearToDb((float)valueChanged));
}
private void MusicVolumeSlider_Changed(double valueChanged)
{
OptionsData.MusicVolumeLevel = valueChanged;
AudioServer.SetBusVolumeDb(_musicBusIndex, Mathf.LinearToDb((float)valueChanged));
}
private void SFXVolumeSlider_Changed(double valueChanged)
{
OptionsData.SFXVolumeLevel = valueChanged;
AudioServer.SetBusVolumeDb(_sfxBusIndex, Mathf.LinearToDb((float)valueChanged));
}
}
[Meta, Id("options_data")]
public partial class OptionsData : Node
{
[Save("MasterVolume")]
public required double MasterVolumeLevel { get; set; }
[Save("MusicVolume")]
public required double MusicVolumeLevel { get; set; }
[Save("SFXVolume")]
public required double SFXVolumeLevel { get; set; }
[Save("ScreenResolution")]
public required int ScreenResolution { get; set; }
}

View File

@@ -0,0 +1 @@
uid://cjxmdvhixcj6e

View File

@@ -0,0 +1,134 @@
[gd_scene load_steps=5 format=3 uid="uid://drkl3btdy6uxj"]
[ext_resource type="Script" uid="uid://cjxmdvhixcj6e" path="res://src/options/OptionsMenu.cs" id="1_jli36"]
[sub_resource type="StyleBoxLine" id="StyleBoxLine_jli36"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_utd4g"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_1egkf"]
bg_color = Color(2.5028e-06, 0.712708, 0.445629, 1)
[node name="OptionsMenu" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_jli36")
[node name="ColorRect" type="ColorRect" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0.137255, 0.121569, 0.12549, 1)
[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 0
theme_override_constants/margin_left = 100
theme_override_constants/margin_top = 100
theme_override_constants/margin_right = 100
theme_override_constants/margin_bottom = 100
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
layout_mode = 2
size_flags_horizontal = 0
mouse_filter = 0
[node name="MasterVolume" type="HBoxContainer" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="VolumeLabel" type="Label" parent="MarginContainer/VBoxContainer/MasterVolume"]
custom_minimum_size = Vector2(125, 0)
layout_mode = 2
text = "Master Volume"
horizontal_alignment = 2
[node name="MasterVolumeSlider" type="HSlider" parent="MarginContainer/VBoxContainer/MasterVolume"]
unique_name_in_owner = true
custom_minimum_size = Vector2(300, 0)
layout_mode = 2
size_flags_vertical = 1
theme_override_styles/slider = SubResource("StyleBoxLine_jli36")
theme_override_styles/grabber_area = SubResource("StyleBoxFlat_utd4g")
theme_override_styles/grabber_area_highlight = SubResource("StyleBoxFlat_1egkf")
max_value = 1.0
step = 0.001
value = 1.0
[node name="MusicVolume" type="HBoxContainer" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="VolumeLabel" type="Label" parent="MarginContainer/VBoxContainer/MusicVolume"]
custom_minimum_size = Vector2(125, 0)
layout_mode = 2
text = "Music Volume"
horizontal_alignment = 2
[node name="MusicVolumeSlider" type="HSlider" parent="MarginContainer/VBoxContainer/MusicVolume"]
unique_name_in_owner = true
custom_minimum_size = Vector2(300, 0)
layout_mode = 2
size_flags_vertical = 1
theme_override_styles/slider = SubResource("StyleBoxLine_jli36")
theme_override_styles/grabber_area = SubResource("StyleBoxFlat_utd4g")
theme_override_styles/grabber_area_highlight = SubResource("StyleBoxFlat_1egkf")
max_value = 1.0
step = 0.001
value = 1.0
[node name="SFXVolume" type="HBoxContainer" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="VolumeLabel" type="Label" parent="MarginContainer/VBoxContainer/SFXVolume"]
custom_minimum_size = Vector2(125, 0)
layout_mode = 2
text = "SFX Volume"
horizontal_alignment = 2
[node name="SFXVolumeSlider" type="HSlider" parent="MarginContainer/VBoxContainer/SFXVolume"]
unique_name_in_owner = true
custom_minimum_size = Vector2(300, 0)
layout_mode = 2
size_flags_vertical = 1
theme_override_styles/slider = SubResource("StyleBoxLine_jli36")
theme_override_styles/grabber_area = SubResource("StyleBoxFlat_utd4g")
theme_override_styles/grabber_area_highlight = SubResource("StyleBoxFlat_1egkf")
max_value = 1.0
step = 0.001
value = 1.0
[node name="Resolution" type="HBoxContainer" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
[node name="ResolutionLabel" type="Label" parent="MarginContainer/VBoxContainer/Resolution"]
custom_minimum_size = Vector2(125, 0)
layout_mode = 2
text = "Resolution: "
horizontal_alignment = 2
[node name="ResolutionOptions" type="OptionButton" parent="MarginContainer/VBoxContainer/Resolution"]
unique_name_in_owner = true
custom_minimum_size = Vector2(200, 0)
layout_mode = 2
flat = true
[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
size_flags_vertical = 10
[node name="SaveAndExitButton" type="Button" parent="MarginContainer/VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(150, 75)
layout_mode = 2
text = "Save and Exit"