fixt pop up box about unsaved porgrass

This commit is contained in:
Björn Blomberg
2025-10-17 15:58:42 +02:00
parent ed4ce28921
commit 099110cdb7
6 changed files with 101 additions and 20 deletions

View File

@@ -100,6 +100,9 @@ namespace TheGame
public override void _Input(InputEvent @event)
{
if (!IsInsideTree())
return;
if (@event.IsActionPressed("ui_cancel")) // ESC key
{
TogglePause();

View File

@@ -225,16 +225,24 @@ namespace TheGame
public string GetText(string key)
{
string text = null;
// Försök hämta från nuvarande språk
if (_currentTranslations != null && _currentTranslations.ContainsKey(key))
{
return _currentTranslations[key];
text = _currentTranslations[key];
}
// Fallback till engelska
else if (_fallbackTranslations != null && _fallbackTranslations.ContainsKey(key))
{
text = _fallbackTranslations[key];
}
// Fallback till engelska
if (_fallbackTranslations != null && _fallbackTranslations.ContainsKey(key))
// Konvertera escape-sekvenser om vi hittade text
if (text != null)
{
return _fallbackTranslations[key];
text = text.Replace("\\n", "\n").Replace("\\t", "\t");
return text;
}
// Om inte ens engelska finns, returnera nyckeln

View File

@@ -271,6 +271,9 @@ namespace TheGame
public override void _Input(InputEvent @event)
{
if (!IsInsideTree())
return;
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Enter || keyEvent.Keycode == Key.KpEnter)
@@ -287,13 +290,19 @@ namespace TheGame
OnSaveConfirmed();
}
}
GetViewport().SetInputAsHandled();
if (IsInsideTree())
{
GetViewport().SetInputAsHandled();
}
}
else if (keyEvent.Keycode == Key.Escape)
{
Hide();
QueueFree();
GetViewport().SetInputAsHandled();
if (IsInsideTree())
{
GetViewport().SetInputAsHandled();
}
}
}
}

View File

@@ -3,7 +3,7 @@ using System;
namespace TheGame
{
public partial class ExitConfirmationDialog : AcceptDialog
public partial class ExitConfirmationDialog : Window
{
private Action _onConfirm;
private Button _yesButton;
@@ -15,6 +15,9 @@ namespace TheGame
// Lägg till i localized_ui gruppen
AddToGroup("localized_ui");
// Dölj dialogen initialt
Hide();
// Skapa dialog layout
SetupDialog();
UpdateLocalization();
@@ -27,29 +30,46 @@ namespace TheGame
Unresizable = false;
ProcessMode = ProcessModeEnum.WhenPaused;
// Ta bort standard OK knapp
GetOkButton().QueueFree();
// 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;
vbox.AddChild(_messageLabel);
_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);
vbox.AddChild(spacer);
content.AddChild(spacer);
// Skapa knappar
var buttonContainer = new HBoxContainer();
buttonContainer.Alignment = BoxContainer.AlignmentMode.Center;
vbox.AddChild(buttonContainer);
content.AddChild(buttonContainer);
_yesButton = new Button();
_yesButton.FocusMode = Control.FocusModeEnum.All;
_yesButton.CustomMinimumSize = new Vector2(80, 35);
_yesButton.Pressed += OnYesPressed;
buttonContainer.AddChild(_yesButton);
@@ -59,11 +79,14 @@ namespace TheGame
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 minimum storlek för dialog
Size = new Vector2I(400, 200);
// Sätt lämplig storlek för dialog
MinSize = new Vector2I(380, 200);
Size = new Vector2I(420, 220);
}
public void UpdateLocalization()
@@ -78,28 +101,51 @@ namespace TheGame
public void ShowDialog(Action onConfirm)
{
_onConfirm = onConfirm;
PopupCentered();
_noButton.GrabFocus(); // Fokusera på "Nej" som säkrare alternativ
// 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();
GetViewport().SetInputAsHandled();
if (IsInsideTree())
{
GetViewport().SetInputAsHandled();
}
}
}
}

View File

@@ -81,19 +81,28 @@ namespace TheGame
public override void _Input(InputEvent @event)
{
if (!IsInsideTree())
return;
// Se till att input fungerar när spelet är pausat
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Enter || keyEvent.Keycode == Key.KpEnter)
{
OnSaveConfirmed();
GetViewport().SetInputAsHandled();
if (IsInsideTree())
{
GetViewport().SetInputAsHandled();
}
}
else if (keyEvent.Keycode == Key.Escape)
{
Hide();
QueueFree();
GetViewport().SetInputAsHandled();
if (IsInsideTree())
{
GetViewport().SetInputAsHandled();
}
}
}
}

View File

@@ -108,12 +108,18 @@ namespace TheGame
public override void _Input(InputEvent @event)
{
if (!IsInsideTree())
return;
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
if (keyEvent.Keycode == Key.Escape)
{
OnCancelPressed();
GetViewport().SetInputAsHandled();
if (IsInsideTree())
{
GetViewport().SetInputAsHandled();
}
}
}
}