using Godot; using System.Collections.Generic; using System.Linq; namespace TheGame { public partial class InstanceSelectionMenu : AcceptDialog { private SaveData _saveData; private VBoxContainer _instanceList; private Button _cancelButton; public override void _Ready() { ProcessMode = ProcessModeEnum.Always; Title = "Select Save Instance"; var vbox = new VBoxContainer(); // Header info var headerLabel = new Label(); headerLabel.Text = $"Game Seed: {_saveData?.GameSeed?.ToString() ?? "Unknown"}"; headerLabel.AddThemeStyleboxOverride("normal", new StyleBoxFlat()); vbox.AddChild(headerLabel); var separator = new HSeparator(); vbox.AddChild(separator); // Scroll container för instanser var scrollContainer = new ScrollContainer(); scrollContainer.CustomMinimumSize = new Vector2(0, 300); _instanceList = new VBoxContainer(); scrollContainer.AddChild(_instanceList); vbox.AddChild(scrollContainer); // Cancel button var buttonContainer = new HBoxContainer(); buttonContainer.Alignment = BoxContainer.AlignmentMode.End; _cancelButton = new Button(); _cancelButton.Text = "Cancel"; _cancelButton.Pressed += OnCancelPressed; buttonContainer.AddChild(_cancelButton); vbox.AddChild(buttonContainer); AddChild(vbox); PopulateInstanceList(); } public void Initialize(SaveData saveData) { _saveData = saveData; } private void PopulateInstanceList() { if (_saveData?.Instances == null) return; // Sortera instances efter save date (nyast först) var sortedInstances = _saveData.Instances.OrderByDescending(i => i.SaveDate).ToList(); foreach (var instance in sortedInstances) { var instanceButton = new Button(); instanceButton.CustomMinimumSize = new Vector2(0, 60); instanceButton.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill; // Skapa text med formatering var instanceText = $"{instance.DisplayName}\n"; instanceText += $"Game Time: {instance.GetFormattedGameTime()}\n"; instanceText += $"Saved: {instance.SaveDate:yyyy-MM-dd HH:mm:ss}"; instanceButton.Text = instanceText; instanceButton.Pressed += () => OnInstanceSelected(instance); _instanceList.AddChild(instanceButton); } } private void OnInstanceSelected(SaveInstance instance) { var saveManager = new SaveManager(); if (saveManager.LoadGame(_saveData, instance)) { GetTree().ChangeSceneToFile("res://scenes/Game.tscn"); } else { GD.PrintErr("Failed to load selected game instance"); // Visa error message till användaren var errorDialog = new AcceptDialog(); errorDialog.DialogText = "Failed to load the selected save instance."; errorDialog.Title = "Load Error"; GetTree().Root.AddChild(errorDialog); errorDialog.PopupCentered(); } QueueFree(); } private void OnCancelPressed() { QueueFree(); } public override void _Input(InputEvent @event) { if (@event is InputEventKey keyEvent && keyEvent.Pressed) { if (keyEvent.Keycode == Key.Escape) { OnCancelPressed(); GetViewport().SetInputAsHandled(); } } } } }