A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Accessibility: Escape withdraws a New Game request armed from the top bar (Fixes #2521) (#2595)
Beri88a814058a10
diff --git a/changelog.d/2521-escape-new-game.md b/changelog.d/2521-escape-new-game.mdnew file mode 100644index 000000000..8687a161b--- /dev/null+++ b/changelog.d/2521-escape-new-game.md@@ -0,0 +1,3 @@+### Fixed+- Escape now withdraws a New Game request armed from the top bar when the start screen is closed, and takes the prompt+ off the toast; that Escape no longer also clears the map selection (accessibility audit L2, Fixes #2521).diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex d692bb706..45600ceca 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -94,6 +94,8 @@ namespace Statecraft.Game.UIGameSession? saveNoticeSession;AdvisorPresenter? advisor;CabinetPresenter? cabinet;+ /// <summary>The top bar's New Game (none in a smoke run), for Escape's withdrawal of its open request (#2340 L2).</summary>+ NewGamePresenter? newGame;EncyclopediaPresenter? encyclopedia;/// <summary>Tests supply an in-memory approval copy for Hebrew layout; the shipped review fields are unchanged.</summary>internal AdvisorCatalog? AdvisorCatalogForTests { get; set; }@@ -491,6 +493,7 @@ namespace Statecraft.Game.UIif (smoke == 0){newGame = Own(new NewGamePresenter(root, bridge, store, toast, text, GameStart.NewSeed));+ this.newGame = newGame;Own(GameStart.Autosave(bridge, store, text, toast.Append)); // appends: the turn's refusals stay visible}// Once the game is over, the sim's end report (#812); after the autosave, so the last turn is saved first. New Game@@ -969,7 +972,7 @@ namespace Statecraft.Game.UIfor (int i = owned.Count - 1; i >= 0; i--) owned[i].Dispose();owned.Clear();if (hoveredCity != null) { hoveredCity = null; HandCursor(false); } // the next Begin's pills start unlit- companion = null; minimap = null; unseenSkipped = null; labels = null; nav = null; start = null; opening = null; card = null; outcomes = null; report = null; end = null; advisor = null; cabinet = null; encyclopedia = null; feedback = null; share = null; info = null; rig = null; drawn = null; drawnSites = null; units = null; Regions = null; logCamera = false;+ companion = null; minimap = null; unseenSkipped = null; labels = null; nav = null; start = null; opening = null; card = null; outcomes = null; report = null; end = null; advisor = null; cabinet = null; newGame = null; encyclopedia = null; feedback = null; share = null; info = null; rig = null; drawn = null; drawnSites = null; units = null; Regions = null; logCamera = false;// A ?profile run cut short (a stopped coroutine never reaches its restore line) must not leave the terrain at the// profile's pixel error (Task 24 review M1). Nothing else changes it, so restoring is always right.if (map != null && map.Terrain != null) map.Terrain.heightmapPixelError = MapBuilder.TerrainPixelError;@@ -1171,6 +1174,15 @@ namespace Statecraft.Game.UIdone |= start?.Escape() == true;// Escape in the Cabinet's candidate list goes back to the seat, not out of the screen (#2340 M2).done |= cabinet?.Escape() == true || nav?.Close() == true;+ // With nothing above it to close, Escape withdraws a New Game request armed from the top bar too, the start+ // screen closed (#2340 L2, #2521), and is used up there, so it does not also clear the map selection. A+ // decision card or capture dialog open over it is above it (the top bar stays usable under them): that+ // Escape hides the card or dialog and the request stays armed (#2595 review).+ if (!done && newGame?.Armed == true)+ {+ done = capture?.Escape() == true || card?.Escape() == true || card?.CollapsedThisFrame == true;+ if (!done) { newGame.Cancel(); done = true; }+ }// A screen/card that consumed this Cancel must not also dismiss the map selection in this frame.if (!done && nav?.ClosedThisFrame != true && document.rootVisualElement.focusController?.focusedElement == null&& capture?.Escape() != true && card?.Escape() != true && card?.CollapsedThisFrame != true)diff --git a/unity/Assets/Tests/EditMode/GameSceneTests.cs b/unity/Assets/Tests/EditMode/GameSceneTests.csindex 0f86f92db..b70f8b995 100644--- a/unity/Assets/Tests/EditMode/GameSceneTests.cs+++ b/unity/Assets/Tests/EditMode/GameSceneTests.cs@@ -4,6 +4,7 @@ using System.Collections;using System.Collections.Generic;using System.IO;using System.Linq;+using System.Reflection;using NUnit.Framework;using Statecraft.Game;using Statecraft.Game.Boot;@@ -13,6 +14,7 @@ using Statecraft.Sim;using UnityEditor;using UnityEditor.SceneManagement;using UnityEngine;+using UnityEngine.InputSystem;using UnityEngine.TestTools;using UnityEngine.UIElements;using Object = UnityEngine.Object;@@ -1147,3 +1149,105 @@ public class GameSceneTests}}++/// <summary>#2340 L2 (#2521): with the start screen closed, Escape withdraws a New Game request armed from the top bar, and+/// the key is used up there, so the map selection under it stays. Drives GameRoot's own Escape poll (Update) with a key+/// pressed through the Input System test fixture, which is what makes wasPressedThisFrame read true in an EditMode test.</summary>+public class GameRootEscapeTests : SharedCameraActionsFixture+{+ string saveDir = "";+ static VisualElement Ui => Object.FindFirstObjectByType<UIDocument>().rootVisualElement;++ [TearDown]+ public void DiscardScene()+ {+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);+ if (Directory.Exists(saveDir)) Directory.Delete(saveDir, true);+ }++ /// <summary>Stands for a later frame: the card's Hide counts as an Escape's own only in the frame it happened+ /// (EventCardPresenter.CollapsedThisFrame), and the Input System fixture cannot press keys in a [UnityTest] that+ /// runs frames in edit mode.</summary>+ static void LaterFrame(GameRoot root) =>+ typeof(EventCardPresenter).GetField("collapsedAt", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(root.Card, -1);++ [Test]+ public void EscapeWithdrawsANewGameRequestArmedFromTheTopBar()+ {+ saveDir = Path.Combine(Path.GetTempPath(), "statecraft-scene-" + Guid.NewGuid().ToString("N"));+ var inputs = GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ Object.FindFirstObjectByType<MapScene>().Build(inputs.Map);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ root.Begin(inputs, 0); // no start screen+ GameSceneTests.HideTheOpeningCard(Ui);+ LaterFrame(root); // the Hide is not an Escape's+ var keyboard = InputSystem.AddDevice<Keyboard>();+ var bridge = root.Bridge!;+ var info = (InfoPanelPresenter)typeof(GameRoot).GetField("info", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(root)!;+ var home = bridge.Content.SettlementHexes.First(x => x.Id == "tel_aviv");+ info.Show(home.Q, home.R);+ UiTestKit.Press(Ui.Q<Button>("new-game"));+ var text = UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);+ Assert.AreEqual(text["newgame.confirm"], Ui.Q<Label>("toast").text, "precondition: the top bar armed the request");+ var newGame = (NewGamePresenter)typeof(GameRoot).GetField("newGame", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(root)!;+ void Escape()+ {+ (Ui.focusController?.focusedElement as VisualElement)?.Blur(); // Escape with no focus, as after a mouse press+ Press(keyboard.escapeKey);+ Assert.IsTrue(keyboard.escapeKey.wasPressedThisFrame, "precondition: Escape went down this frame");+ typeof(GameRoot).GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance)!.Invoke(root, null);+ Release(keyboard.escapeKey);+ }+ // The #2549 review note (comment 6636159055, an agent review): the open screen closes first (nav.Close); only+ // an Escape it declines withdraws the request.+ UiTestKit.Press(Ui.Q<Button>("nav-budget"));+ Assert.AreEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "precondition: a screen is open");+ Escape();+ Assert.AreNotEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "the first Escape closes the screen");+ Assert.IsTrue(newGame.Armed, "the first Escape is the screen's: the request is still armed");+ Escape();+ Assert.IsFalse(newGame.Armed, "the next Escape withdraws the request");+ Assert.AreEqual("", Ui.Q<Label>("toast").text, "the prompt is off the toast");+ Assert.AreEqual((home.Q, home.R), info.Selected, "the Escape was used up: the map selection stays");+ var session = bridge.Session;+ UiTestKit.Press(Ui.Q<Button>("new-game"));+ Assert.AreSame(session, bridge.Session, "withdrawn: the next press only arms the request again");+ }++ // The #2595 review: a decision card over an armed New Game is the top thing (the top bar stays usable under it), so+ // the first Escape hides the card and only the next one withdraws the request.+ [Test]+ public void EscapeHidesAnOpenDecisionCardBeforeItWithdrawsAnArmedNewGame()+ {+ saveDir = Path.Combine(Path.GetTempPath(), "statecraft-scene-" + Guid.NewGuid().ToString("N"));+ var inputs = GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ Object.FindFirstObjectByType<MapScene>().Build(inputs.Map);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ root.Begin(inputs, 0); // no start screen; the game opens on a card+ GameSceneTests.HideTheOpeningCard(Ui);+ var keyboard = InputSystem.AddDevice<Keyboard>();+ UiTestKit.Press(Ui.Q<Button>("event-reopen"));+ Assert.AreEqual(DisplayStyle.Flex, Ui.Q("event-card").style.display.value, "precondition: the decision card is open");+ UiTestKit.Press(Ui.Q<Button>("new-game"));+ var newGame = (NewGamePresenter)typeof(GameRoot).GetField("newGame", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(root)!;+ Assert.IsTrue(newGame.Armed, "precondition: the top bar armed the request under the card");+ void Escape()+ {+ (Ui.focusController?.focusedElement as VisualElement)?.Blur(); // Escape with no focus, as after a mouse press+ Press(keyboard.escapeKey);+ Assert.IsTrue(keyboard.escapeKey.wasPressedThisFrame, "precondition: Escape went down this frame");+ typeof(GameRoot).GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance)!.Invoke(root, null);+ Release(keyboard.escapeKey);+ }+ Escape();+ Assert.AreNotEqual(DisplayStyle.Flex, Ui.Q("event-card").style.display.value, "the first Escape hides the card");+ Assert.IsTrue(newGame.Armed, "the first Escape is the card's: the request is still armed");+ LaterFrame(root); // the next key press+ Escape();+ Assert.IsFalse(newGame.Armed, "the next Escape withdraws the request");+ }+}