using Godot; using System; namespace TheGame { public partial class ExitConfirmationDialog : Window { 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"); // Dölj dialogen initialt Hide(); // Skapa dialog layout SetupDialog(); UpdateLocalization(); } private void SetupDialog() { // Sätt dialog egenskaper Title = ""; Unresizable = false; ProcessMode = ProcessModeEnum.WhenPaused; // Konfigurera window egenskaper Mode = ModeEnum.Windowed; SetFlag(Flags.Popup, true); // Skapa innehåll var vbox = new VBoxContainer(); vbox.AddThemeConstantOverride("separation", 10); AddChild(vbox); // Lägg till padding var margin = new MarginContainer(); margin.AddThemeConstantOverride("margin_left", 25); margin.AddThemeConstantOverride("margin_right", 25); margin.AddThemeConstantOverride("margin_top", 25); margin.AddThemeConstantOverride("margin_bottom", 25); vbox.AddChild(margin); var content = new VBoxContainer(); margin.AddChild(content); _messageLabel = new Label(); _messageLabel.HorizontalAlignment = HorizontalAlignment.Center; _messageLabel.VerticalAlignment = VerticalAlignment.Center; _messageLabel.AutowrapMode = TextServer.AutowrapMode.WordSmart; _messageLabel.CustomMinimumSize = new Vector2(300, 80); // Säkerställ tillräcklig höjd för text content.AddChild(_messageLabel); // Lägg till mellanrum var spacer = new Control(); spacer.CustomMinimumSize = new Vector2(0, 20); content.AddChild(spacer); // Skapa knappar var buttonContainer = new HBoxContainer(); buttonContainer.Alignment = BoxContainer.AlignmentMode.Center; content.AddChild(buttonContainer); _yesButton = new Button(); _yesButton.FocusMode = Control.FocusModeEnum.All; _yesButton.CustomMinimumSize = new Vector2(80, 35); _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.FocusMode = Control.FocusModeEnum.All; _noButton.CustomMinimumSize = new Vector2(80, 35); _noButton.Pressed += OnNoPressed; buttonContainer.AddChild(_noButton); // Sätt lämplig storlek för dialog MinSize = new Vector2I(380, 200); Size = new Vector2I(420, 220); } 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; // Använd PopupCentered med explicit storlek PopupCentered(new Vector2I(420, 220)); // Skjut upp fokusanropet till nästa frame CallDeferred(nameof(FocusNoButton)); } private void FocusNoButton() { // Sätt fokus på "Nej"-knappen if (_noButton != null && IsInsideTree() && _noButton.IsInsideTree()) { _noButton.GrabFocus(); } } private void OnYesPressed() { Hide(); _onConfirm?.Invoke(); // Återställ onConfirm för nästa användning _onConfirm = null; } private void OnNoPressed() { Hide(); // Återställ onConfirm för nästa användning _onConfirm = null; } public override void _Input(InputEvent @event) { if (!IsInsideTree()) return; // Hantera ESC för att stänga dialog (samma som "Nej") if (@event.IsActionPressed("ui_cancel") && Visible) { OnNoPressed(); if (IsInsideTree()) { GetViewport().SetInputAsHandled(); } } } } }