- Implemented LocalizationManager for handling multiple languages. - Updated LoadGameMenu, MainMenu, PauseMenu, and SettingsMenu to support localization. - Added InstanceSelectionMenu for selecting game instances. - Refactored SaveManager to handle new SaveInstance structure. - Created SaveDialog for saving games with user-defined names. - Enhanced SaveData to manage multiple save instances. - Added error handling and logging for save/load operations. - Updated UI elements to reflect localized text.
288 lines
11 KiB
C#
288 lines
11 KiB
C#
using Godot;
|
|
using System.Collections.Generic;
|
|
|
|
namespace TheGame
|
|
{
|
|
public partial class SettingsMenu : Control
|
|
{
|
|
// UI References
|
|
private Label _titleLabel;
|
|
private TabContainer _tabContainer;
|
|
|
|
// General Tab
|
|
private Label _languageLabel;
|
|
private OptionButton _languageOption;
|
|
|
|
// Video Tab
|
|
private Label _fullscreenLabel;
|
|
private CheckBox _fullscreenCheck;
|
|
|
|
// Audio Tab
|
|
private Label _masterLabel;
|
|
private HSlider _masterSlider;
|
|
private Label _backgroundLabel;
|
|
private HSlider _backgroundSlider;
|
|
private Label _effectsLabel;
|
|
private HSlider _effectsSlider;
|
|
private Label _radioLabel;
|
|
private HSlider _radioSlider;
|
|
|
|
// Buttons
|
|
private Button _applyButton;
|
|
private Button _cancelButton;
|
|
private Button _backButton;
|
|
|
|
// Settings
|
|
private GameSettings _settings;
|
|
private GameSettings _originalSettings;
|
|
|
|
public override void _Ready()
|
|
{
|
|
// Lägg till i localized_ui gruppen för språkuppdateringar
|
|
AddToGroup("localized_ui");
|
|
|
|
// Sätt process mode baserat på om spelet är pausat eller inte
|
|
if (GetTree().Paused)
|
|
{
|
|
ProcessMode = ProcessModeEnum.WhenPaused;
|
|
}
|
|
else
|
|
{
|
|
ProcessMode = ProcessModeEnum.Always;
|
|
}
|
|
|
|
// Hämta UI referenser
|
|
GetUIReferences();
|
|
|
|
// Ladda nuvarande inställningar
|
|
_settings = GameSettings.Instance;
|
|
_originalSettings = new GameSettings
|
|
{
|
|
Fullscreen = _settings.Fullscreen,
|
|
MasterVolume = _settings.MasterVolume,
|
|
BackgroundVolume = _settings.BackgroundVolume,
|
|
EffectsVolume = _settings.EffectsVolume,
|
|
RadioVolume = _settings.RadioVolume,
|
|
Language = _settings.Language
|
|
};
|
|
|
|
// Sätt process mode på alla UI element så de fungerar när pausat
|
|
SetProcessModeRecursive(this, ProcessModeEnum.WhenPaused);
|
|
|
|
// Initiera UI
|
|
InitializeLanguageOptions();
|
|
LoadCurrentSettings();
|
|
UpdateLocalization();
|
|
}
|
|
|
|
public override void _Notification(int what)
|
|
{
|
|
if (what == NotificationWMCloseRequest)
|
|
{
|
|
// Settings menyn är en popup, så vi stänger bara den själv
|
|
_on_cancel_button_pressed();
|
|
}
|
|
}
|
|
|
|
private void SetProcessModeRecursive(Node node, ProcessModeEnum mode)
|
|
{
|
|
if (node is Control control)
|
|
{
|
|
control.ProcessMode = mode;
|
|
}
|
|
|
|
foreach (Node child in node.GetChildren())
|
|
{
|
|
SetProcessModeRecursive(child, mode);
|
|
}
|
|
}
|
|
|
|
private void GetUIReferences()
|
|
{
|
|
_titleLabel = GetNode<Label>("MainContainer/TitleLabel");
|
|
_tabContainer = GetNode<TabContainer>("MainContainer/TabContainer");
|
|
|
|
// General
|
|
_languageLabel = GetNode<Label>("MainContainer/TabContainer/General/GeneralVBox/LanguageContainer/LanguageLabel");
|
|
_languageOption = GetNode<OptionButton>("MainContainer/TabContainer/General/GeneralVBox/LanguageContainer/LanguageOption");
|
|
|
|
// Video
|
|
_fullscreenLabel = GetNode<Label>("MainContainer/TabContainer/Video/VideoVBox/FullscreenContainer/FullscreenLabel");
|
|
_fullscreenCheck = GetNode<CheckBox>("MainContainer/TabContainer/Video/VideoVBox/FullscreenContainer/FullscreenCheck");
|
|
|
|
// Audio
|
|
_masterLabel = GetNode<Label>("MainContainer/TabContainer/Audio/AudioVBox/MasterContainer/MasterLabel");
|
|
_masterSlider = GetNode<HSlider>("MainContainer/TabContainer/Audio/AudioVBox/MasterContainer/MasterSlider");
|
|
_backgroundLabel = GetNode<Label>("MainContainer/TabContainer/Audio/AudioVBox/BackgroundContainer/BackgroundLabel");
|
|
_backgroundSlider = GetNode<HSlider>("MainContainer/TabContainer/Audio/AudioVBox/BackgroundContainer/BackgroundSlider");
|
|
_effectsLabel = GetNode<Label>("MainContainer/TabContainer/Audio/AudioVBox/EffectsContainer/EffectsLabel");
|
|
_effectsSlider = GetNode<HSlider>("MainContainer/TabContainer/Audio/AudioVBox/EffectsContainer/EffectsSlider");
|
|
_radioLabel = GetNode<Label>("MainContainer/TabContainer/Audio/AudioVBox/RadioContainer/RadioLabel");
|
|
_radioSlider = GetNode<HSlider>("MainContainer/TabContainer/Audio/AudioVBox/RadioContainer/RadioSlider");
|
|
|
|
// Buttons
|
|
_applyButton = GetNode<Button>("MainContainer/ButtonContainer/ApplyButton");
|
|
_cancelButton = GetNode<Button>("MainContainer/ButtonContainer/CancelButton");
|
|
_backButton = GetNode<Button>("MainContainer/ButtonContainer/BackButton");
|
|
}
|
|
|
|
private void InitializeLanguageOptions()
|
|
{
|
|
_languageOption.Clear();
|
|
|
|
var availableLanguages = LocalizationManager.Instance.GetAvailableLanguages();
|
|
var languageNames = new Dictionary<string, string>
|
|
{
|
|
{"eng", "English"},
|
|
{"sve", "Svenska"}
|
|
};
|
|
|
|
foreach (string lang in availableLanguages)
|
|
{
|
|
string displayName = languageNames.ContainsKey(lang) ? languageNames[lang] : lang.ToUpper();
|
|
_languageOption.AddItem(displayName);
|
|
_languageOption.SetItemMetadata(_languageOption.GetItemCount() - 1, lang);
|
|
}
|
|
}
|
|
|
|
private void LoadCurrentSettings()
|
|
{
|
|
// Language
|
|
for (int i = 0; i < _languageOption.GetItemCount(); i++)
|
|
{
|
|
if (_languageOption.GetItemMetadata(i).AsString() == _settings.Language)
|
|
{
|
|
_languageOption.Selected = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Video
|
|
_fullscreenCheck.ButtonPressed = _settings.Fullscreen;
|
|
|
|
// Audio
|
|
_masterSlider.Value = _settings.MasterVolume;
|
|
_backgroundSlider.Value = _settings.BackgroundVolume;
|
|
_effectsSlider.Value = _settings.EffectsVolume;
|
|
_radioSlider.Value = _settings.RadioVolume;
|
|
|
|
UpdateVolumeLabels();
|
|
}
|
|
|
|
private void UpdateVolumeLabels()
|
|
{
|
|
var loc = LocalizationManager.Instance;
|
|
_masterLabel.Text = $"{loc.GetText("settings_master_volume")}: {(int)_masterSlider.Value}";
|
|
_backgroundLabel.Text = $"{loc.GetText("settings_background_volume")}: {(int)_backgroundSlider.Value}";
|
|
_effectsLabel.Text = $"{loc.GetText("settings_effects_volume")}: {(int)_effectsSlider.Value}";
|
|
_radioLabel.Text = $"{loc.GetText("settings_radio_volume")}: {(int)_radioSlider.Value}";
|
|
}
|
|
|
|
public void UpdateLocalization()
|
|
{
|
|
var loc = LocalizationManager.Instance;
|
|
|
|
_titleLabel.Text = loc.GetText("settings_title");
|
|
|
|
// Tab names
|
|
_tabContainer.SetTabTitle(0, loc.GetText("settings_general"));
|
|
_tabContainer.SetTabTitle(1, loc.GetText("settings_video"));
|
|
_tabContainer.SetTabTitle(2, loc.GetText("settings_audio"));
|
|
|
|
// General
|
|
_languageLabel.Text = loc.GetText("settings_language") + ":";
|
|
|
|
// Video
|
|
_fullscreenLabel.Text = loc.GetText("settings_fullscreen") + ":";
|
|
|
|
// Audio
|
|
UpdateVolumeLabels();
|
|
|
|
// Buttons
|
|
_applyButton.Text = loc.GetText("settings_apply");
|
|
_cancelButton.Text = loc.GetText("settings_cancel");
|
|
_backButton.Text = loc.GetText("settings_back");
|
|
}
|
|
|
|
// Signal handlers
|
|
private void _on_language_option_item_selected(int index)
|
|
{
|
|
string selectedLanguage = _languageOption.GetItemMetadata(index).AsString();
|
|
_settings.Language = selectedLanguage;
|
|
|
|
// Tillämpa språkändring direkt
|
|
LocalizationManager.Instance.SetLanguage(selectedLanguage);
|
|
}
|
|
|
|
private void _on_fullscreen_check_toggled(bool buttonPressed)
|
|
{
|
|
_settings.Fullscreen = buttonPressed;
|
|
}
|
|
|
|
private void _on_master_slider_value_changed(double value)
|
|
{
|
|
_settings.MasterVolume = (int)value;
|
|
UpdateVolumeLabels();
|
|
}
|
|
|
|
private void _on_background_slider_value_changed(double value)
|
|
{
|
|
_settings.BackgroundVolume = (int)value;
|
|
UpdateVolumeLabels();
|
|
}
|
|
|
|
private void _on_effects_slider_value_changed(double value)
|
|
{
|
|
_settings.EffectsVolume = (int)value;
|
|
UpdateVolumeLabels();
|
|
}
|
|
|
|
private void _on_radio_slider_value_changed(double value)
|
|
{
|
|
_settings.RadioVolume = (int)value;
|
|
UpdateVolumeLabels();
|
|
}
|
|
|
|
private void _on_apply_button_pressed()
|
|
{
|
|
_settings.ApplyAllSettings();
|
|
GoBack();
|
|
}
|
|
|
|
private void _on_cancel_button_pressed()
|
|
{
|
|
// Återställ inställningar
|
|
_settings.Fullscreen = _originalSettings.Fullscreen;
|
|
_settings.MasterVolume = _originalSettings.MasterVolume;
|
|
_settings.BackgroundVolume = _originalSettings.BackgroundVolume;
|
|
_settings.EffectsVolume = _originalSettings.EffectsVolume;
|
|
_settings.RadioVolume = _originalSettings.RadioVolume;
|
|
_settings.Language = _originalSettings.Language;
|
|
|
|
// Återställ språk
|
|
LocalizationManager.Instance.SetLanguage(_originalSettings.Language);
|
|
|
|
GoBack();
|
|
}
|
|
|
|
private void _on_back_button_pressed()
|
|
{
|
|
_on_apply_button_pressed(); // Spara ändringar när man går tillbaka
|
|
}
|
|
|
|
private void GoBack()
|
|
{
|
|
// Kontrollera varifrån vi kom
|
|
if (GetTree().Paused)
|
|
{
|
|
// Vi kom från pausmenyn - ta bara bort settings window
|
|
QueueFree();
|
|
}
|
|
else
|
|
{
|
|
// Vi kom från huvudmenyn
|
|
GetTree().ChangeSceneToFile("res://scenes/MainMenu.tscn");
|
|
}
|
|
}
|
|
}
|
|
} |