Files
TheGame/scripts/ExitConfirmationDialog.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

106 lines
3.2 KiB
C#

using Godot;
using System;
namespace TheGame
{
public partial class ExitConfirmationDialog : AcceptDialog
{
private Action _onConfirm;
private Button _yesButton;
private Button _noButton;
private Label _messageLabel;
public override void _Ready()
{
// Lägg till i localized_ui gruppen
AddToGroup("localized_ui");
// Skapa dialog layout
SetupDialog();
UpdateLocalization();
}
private void SetupDialog()
{
// Sätt dialog egenskaper
Title = "";
Unresizable = false;
ProcessMode = ProcessModeEnum.WhenPaused;
// Ta bort standard OK knapp
GetOkButton().QueueFree();
// Skapa innehåll
var vbox = new VBoxContainer();
AddChild(vbox);
_messageLabel = new Label();
_messageLabel.HorizontalAlignment = HorizontalAlignment.Center;
_messageLabel.AutowrapMode = TextServer.AutowrapMode.WordSmart;
vbox.AddChild(_messageLabel);
// Lägg till mellanrum
var spacer = new Control();
spacer.CustomMinimumSize = new Vector2(0, 20);
vbox.AddChild(spacer);
// Skapa knappar
var buttonContainer = new HBoxContainer();
buttonContainer.Alignment = BoxContainer.AlignmentMode.Center;
vbox.AddChild(buttonContainer);
_yesButton = new Button();
_yesButton.Pressed += OnYesPressed;
buttonContainer.AddChild(_yesButton);
// Lägg till mellanrum mellan knappar
var buttonSpacer = new Control();
buttonSpacer.CustomMinimumSize = new Vector2(20, 0);
buttonContainer.AddChild(buttonSpacer);
_noButton = new Button();
_noButton.Pressed += OnNoPressed;
buttonContainer.AddChild(_noButton);
// Sätt minimum storlek för dialog
Size = new Vector2I(400, 200);
}
public void UpdateLocalization()
{
var loc = LocalizationManager.Instance;
Title = loc.GetText("exit_warning_title");
_messageLabel.Text = loc.GetText("exit_warning_message");
_yesButton.Text = loc.GetText("yes");
_noButton.Text = loc.GetText("no");
}
public void ShowDialog(Action onConfirm)
{
_onConfirm = onConfirm;
PopupCentered();
_noButton.GrabFocus(); // Fokusera på "Nej" som säkrare alternativ
}
private void OnYesPressed()
{
Hide();
_onConfirm?.Invoke();
}
private void OnNoPressed()
{
Hide();
}
public override void _Input(InputEvent @event)
{
// Hantera ESC för att stänga dialog (samma som "Nej")
if (@event.IsActionPressed("ui_cancel") && Visible)
{
OnNoPressed();
GetViewport().SetInputAsHandled();
}
}
}
}