Files
TheGame/scripts/LoadGameMenu.cs
Björn Blomberg 74062a37d6 Add localization support and refactor menus
- 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.
2025-10-17 14:22:21 +02:00

133 lines
4.9 KiB
C#

using Godot;
using System.Collections.Generic;
namespace TheGame
{
public partial class LoadGameMenu : Control
{
private VBoxContainer _saveList;
private SaveManager _saveManager;
private Label _titleLabel;
private Button _backButton;
public override void _Ready()
{
// Lägg till i localized_ui gruppen
AddToGroup("localized_ui");
_saveList = GetNode<VBoxContainer>("VBoxContainer/ScrollContainer/SaveList");
_titleLabel = GetNode<Label>("VBoxContainer/TitleLabel");
_backButton = GetNode<Button>("VBoxContainer/BackButton");
_saveManager = new SaveManager();
UpdateLocalization();
PopulateSaveList();
}
public void UpdateLocalization()
{
var loc = LocalizationManager.Instance;
_titleLabel.Text = loc.GetText("load_game_title");
_backButton.Text = loc.GetText("load_game_back");
}
public override void _Notification(int what)
{
if (what == NotificationWMCloseRequest)
{
// I load game menyn kan vi avsluta direkt
GetTree().Quit();
}
}
private void PopulateSaveList()
{
// Rensa tidigare innehåll
foreach (Node child in _saveList.GetChildren())
{
child.QueueFree();
}
var savedGames = _saveManager.GetSavedGames();
if (savedGames.Count == 0)
{
var noSavesLabel = new Label();
var loc = LocalizationManager.Instance;
noSavesLabel.Text = loc.GetText("load_game_no_saves");
noSavesLabel.HorizontalAlignment = HorizontalAlignment.Center;
_saveList.AddChild(noSavesLabel);
return;
}
foreach (var saveData in savedGames)
{
var gameContainer = new VBoxContainer();
gameContainer.AddThemeConstantOverride("separation", 5);
// Header med seed info
var seedLabel = new Label();
seedLabel.Text = $"Game: {saveData.GameSeed.ToString()}";
seedLabel.AddThemeStyleboxOverride("normal", new StyleBoxFlat());
gameContainer.AddChild(seedLabel);
// Continue button (senaste instansen)
var latestInstance = saveData.GetLatestInstance();
if (latestInstance != null)
{
var continueButton = new Button();
continueButton.Text = $"Continue: {latestInstance.DisplayName}\nTime: {latestInstance.GetFormattedGameTime()}\nSaved: {latestInstance.SaveDate:yyyy-MM-dd HH:mm}";
continueButton.Pressed += () => LoadGame(saveData, latestInstance);
gameContainer.AddChild(continueButton);
}
// Load Previous button (om det finns fler instanser)
if (saveData.Instances.Count > 1)
{
var loadPreviousButton = new Button();
loadPreviousButton.Text = $"Load Previous ({saveData.Instances.Count - 1} other saves)";
loadPreviousButton.Pressed += () => ShowInstanceSelection(saveData);
gameContainer.AddChild(loadPreviousButton);
}
// Separator mellan games
var separator = new HSeparator();
gameContainer.AddChild(separator);
_saveList.AddChild(gameContainer);
}
}
private void LoadGame(SaveData saveData, SaveInstance instance = null)
{
if (_saveManager.LoadGame(saveData, instance))
{
GetTree().ChangeSceneToFile("res://scenes/Game.tscn");
}
else
{
GD.PrintErr("Failed to load selected game");
// Visa error message
var errorDialog = new AcceptDialog();
errorDialog.DialogText = "Failed to load the selected save.";
errorDialog.Title = "Load Error";
GetTree().Root.AddChild(errorDialog);
errorDialog.PopupCentered();
}
}
private void ShowInstanceSelection(SaveData saveData)
{
var instanceMenu = new InstanceSelectionMenu();
instanceMenu.Initialize(saveData);
GetTree().Root.AddChild(instanceMenu);
instanceMenu.PopupCentered(new Vector2I(500, 400));
}
private void _on_back_button_pressed()
{
GetTree().ChangeSceneToFile("res://scenes/MainMenu.tscn");
}
}
}