A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Escape: one press closes one layer, the topmost (Fixes #2609) (#2699)
Beri4df93e5a2ef7
diff --git a/changelog.d/2609-escape-one-layer.md b/changelog.d/2609-escape-one-layer.mdnew file mode 100644index 000000000..3ef6fa27c--- /dev/null+++ b/changelog.d/2609-escape-one-layer.md@@ -0,0 +1,3 @@+### Fixed+- Closing the advisor's full-screen clip no longer also closes the screen under it, and Escape on the start screen no+ longer closes an ended game's end report hidden under it (Fixes #2609).diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex 7249b1d99..fc654949e 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -1211,11 +1211,15 @@ namespace Statecraft.Game.UI{bool done = outcomes?.SkipAll() == true || outcomes?.ClosedThisFrame == true;if (!done) done = advisor?.CloseOverlay() == true || advisor?.OverlayClosedThisFrame == true;+ // One press closes one layer, the topmost (#2609): each step below runs only while nothing above took it. The+ // start screen covers an ended save's end screen and everything else under it: open, it takes the press,+ // withdrawing an armed New Game or doing nothing.+ if (!done && start?.IsOpen == true) { start.Escape(); done = true; }if (!done) done = end?.Close() == true || end?.ClosedThisFrame == true;if (!done) done = report?.Close() == true || report?.ClosedThisFrame == true;- done |= 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;+ // Escape in the Cabinet's candidate list goes back to the seat, not out of the screen (#2340 M2). A screen the+ // focused element's Cancel already closed this frame took the press too (ClosedThisFrame), as for end and report.+ if (!done) done = cabinet?.Escape() == true || nav?.Close() == true || nav?.ClosedThisFrame == 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): thatdiff --git a/unity/Assets/Tests/EditMode/AdvisorMediaTests.cs b/unity/Assets/Tests/EditMode/AdvisorMediaTests.csindex 01ee81985..becea3beb 100644--- a/unity/Assets/Tests/EditMode/AdvisorMediaTests.cs+++ b/unity/Assets/Tests/EditMode/AdvisorMediaTests.cs@@ -16,7 +16,7 @@ using UnityEngine.UIElements;/// narration's position; one press or a covering layer stops both; the card's own loop is never stopped by the advisor.</summary>public class AdvisorMediaTests{- sealed class FakePicture : AdvisorMedia.IPicture+ internal sealed class FakePicture : AdvisorMedia.IPicture{public bool Ready { get; set; }public double Time { get; set; }@@ -29,7 +29,7 @@ public class AdvisorMediaTestspublic void Seek(double t) { Seeks.Add(t); Time = Seekable ? t : 0; }}- sealed class FakeOutput : DeclarationRecording.IOutput+ internal sealed class FakeOutput : DeclarationRecording.IOutput{public bool Decoded = true; public bool IsPlaying { get; set; } public float Position { get; set; }public bool Played, Stopped;@@ -40,7 +40,7 @@ public class AdvisorMediaTests}sealed class Done : DeclarationRecording.IRequest { public ulong Bytes => 0; public void Abort() { } }- static DeclarationRecording.Fetch Immediate => (url, done) => { done(AudioClip.Create("n", 4410, 1, 44100, false), null); return new Done(); };+ internal static DeclarationRecording.Fetch Immediate => (url, done) => { done(AudioClip.Create("n", 4410, 1, 44100, false), null); return new Done(); };static readonly AdvisorCatalog.Media Row = new AdvisorCatalog.Media{diff --git a/unity/Assets/Tests/EditMode/GameRootEscapeLayerTests.cs b/unity/Assets/Tests/EditMode/GameRootEscapeLayerTests.csnew file mode 100644index 000000000..a7064eee8--- /dev/null+++ b/unity/Assets/Tests/EditMode/GameRootEscapeLayerTests.cs@@ -0,0 +1,166 @@+#nullable enable+using System;+using System.Collections.Generic;+using System.IO;+using System.Reflection;+using NUnit.Framework;+using Statecraft.Game;+using Statecraft.Game.Boot;+using Statecraft.Game.Map;+using Statecraft.Game.UI;+using Statecraft.Sim;+using UnityEditor;+using UnityEditor.SceneManagement;+using UnityEngine;+using UnityEngine.InputSystem;+using UnityEngine.UIElements;+using Object = UnityEngine.Object;++/// <summary>#2609: one Escape closes one layer, the topmost visible one. 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 GameRootEscapeLayerTests : SharedCameraActionsFixture+{+ string saveDir = "";+ Keyboard keyboard = null!;+ static VisualElement Ui => Object.FindFirstObjectByType<UIDocument>().rootVisualElement;+ static VisualElement HudRoot => Ui.Q("root");++ [TearDown]+ public void DiscardScene()+ {+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);+ if (Directory.Exists(saveDir)) Directory.Delete(saveDir, true);+ }++ GameRoot Open(GameInputs inputs, bool startScreen)+ {+ 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, startScreen);+ keyboard = InputSystem.AddDevice<Keyboard>();+ return root;+ }++ void Escape(GameRoot root)+ {+ (Ui.focusController?.focusedElement as VisualElement)?.Blur(); // Escape with no focus: only GameRoot's poll takes it+ 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);+ }++ [SetUp]+ public void NewSaveDir() => saveDir = Path.Combine(Path.GetTempPath(), "statecraft-escape-" + Guid.NewGuid().ToString("N"));++ /// <summary>The advisor's full-screen media overlay disables the screens under it but leaves ScreenNav.Current as it+ /// was: the Escape that closes the overlay must not close the side screen under it too.</summary>+ [Test]+ public void EscapeClosesTheMediaOverlayAndLeavesTheScreenUnderIt()+ {+ var root = Open(GameInputs.FromDisk(Paths.Content, Paths.StreamingMap), startScreen: false);+ GameSceneTests.HideTheOpeningCard(Ui); // the card is a hard cover: no media plays under it+ // A clip with a picture, so it takes the overlay; its label is any shipped key, as the advisor reads it from the game's text.+ var row = new AdvisorCatalog.Media+ {+ Picture = "x.mp4", Narration = new Dictionary<string, string> { ["en"] = "x.en.wav" },+ Cues = new Dictionary<string, List<double>> { ["en"] = new List<double> { 0.0 } }, Label = "report.close",+ };+ var text = UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text + "\nadvisor.x.cue.1: \"First.\"\n");+ var advisor = root.Advisor!;+ advisor.Media = new AdvisorMedia(text, new AdvisorMediaTests.FakePicture(), null, AdvisorMediaTests.Immediate, new AdvisorMediaTests.FakeOutput());+ advisor.ForceInline = false;+ UiTestKit.Press(Ui.Q<Button>("nav-budget"));+ Assert.AreEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "precondition: a side screen is open");+ advisor.WatchOverlay(row, "advisor.x");+ Assert.IsTrue(HudRoot.ClassListContains(AdvisorPresenter.OverlayOpenClass), "precondition: the overlay is over it");++ Escape(root);+ Assert.IsFalse(HudRoot.ClassListContains(AdvisorPresenter.OverlayOpenClass), "the first Escape closes the overlay");+ Assert.AreEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "and only the overlay: the screen stays");+ // A later frame: the overlay's close counts as this press's only in the frame it happened (OverlayClosedThisFrame),+ // and an EditMode test cannot advance Time.frameCount between two presses.+ typeof(AdvisorPresenter).GetField("overlayClosedAt", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(advisor, -1);+ Escape(root);+ Assert.AreNotEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "the next Escape closes the screen");+ }++ /// <summary>The turn report is over everything under it: the Escape that closes it must not close a side screen under it+ /// too. A report opens with the screens closed (TurnReportPresenter.Show), so the test puts the report's class over an+ /// open screen by hand, as SelectThenClickMoveTests does.</summary>+ [Test]+ public void EscapeClosesTheReportAndLeavesTheScreenUnderIt()+ {+ var root = Open(GameInputs.FromDisk(Paths.Content, Paths.StreamingMap), startScreen: false);+ GameSceneTests.HideTheOpeningCard(Ui);+ UiTestKit.Press(Ui.Q<Button>("nav-budget"));+ Assert.AreEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "precondition: a side screen is open");+ HudRoot.AddToClassList(TurnReportPresenter.OpenClass);++ Escape(root);+ Assert.IsFalse(HudRoot.ClassListContains(TurnReportPresenter.OpenClass), "the Escape closes the report");+ Assert.AreEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "and only the report: the screen stays");+ }++ /// <summary>A key press reaches the focused element as the UI Cancel event, which closes the open screen (ScreenNav), before+ /// GameRoot polls the same Escape. That poll finds the screen closed this frame (ScreenNav.ClosedThisFrame), so the press+ /// was the screen's: it closes nothing else and leaves a New Game armed from the top bar armed.</summary>+ [Test]+ public void EscapeAFocusedElementUsedToCloseTheScreenLeavesAnArmedNewGame()+ {+ var root = Open(GameInputs.FromDisk(Paths.Content, Paths.StreamingMap), startScreen: false);+ GameSceneTests.HideTheOpeningCard(Ui);+ // The Hide is not this press's: CollapsedThisFrame would otherwise claim it in the single EditMode frame.+ typeof(EventCardPresenter).GetField("collapsedAt", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(root.Card, -1);+ var text = UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);+ UiTestKit.Press(Ui.Q<Button>("new-game"));+ 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)!;+ UiTestKit.Press(Ui.Q<Button>("nav-budget"));+ Assert.AreEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "precondition: a side screen is open");++ var at = Ui.Q<Button>("nav-budget");+ using (var e = NavigationCancelEvent.GetPooled()) { e.target = at; at.SendEvent(e); }+ Assert.AreNotEqual(DisplayStyle.Flex, Ui.Q("screen-budget").style.display.value, "precondition: the Cancel closed the screen");+ Press(keyboard.escapeKey); // the same press, as GameRoot's poll reads it+ 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);+ Assert.IsTrue(newGame.Armed, "the press was the screen's: the request is still armed");+ Assert.AreEqual(text["newgame.confirm"], Ui.Q<Label>("toast").text, "and its prompt stays on the toast");+ }++ /// <summary>An ended autosave opens the end screen before the start screen goes up over it. Escape on the start screen+ /// is the start screen's: it withdraws an armed New Game, and never closes the hidden end screen under it.</summary>+ [Test]+ public void EscapeOnTheStartScreenLeavesTheEndScreenUnderIt()+ {+ var inputs = GameInputs.FromDisk(Paths.Content, Paths.StreamingMap);+ var ended = GameSession.New(inputs.Content, 9, Mode.Historical);+ for (int turn = 0; turn < 1000 && ended.State.GameOver == null; turn++) ended.Step(Array.Empty<Command>());+ Assert.IsNotNull(ended.State.GameOver, "precondition: a game left alone ends");+ new SaveStore(saveDir).WriteAutosave(ended.Save.Write());+ var root = Open(inputs, startScreen: true);+ Assert.IsTrue(HudRoot.ClassListContains(StartScreenPresenter.OpenClass), "precondition: the start screen is up");+ Assert.IsTrue(HudRoot.ClassListContains(EndScreenPresenter.OpenClass), "precondition: the ended save's end screen is under it");+ var text = UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);++ Escape(root); // nothing armed: the start screen has nothing to close+ Assert.IsTrue(HudRoot.ClassListContains(StartScreenPresenter.OpenClass), "the start screen stays");+ Assert.IsTrue(HudRoot.ClassListContains(EndScreenPresenter.OpenClass), "the end screen under it is not closed");++ UiTestKit.Press(Ui.Q<Button>("start-new-game")); // a resumed game: the first press arms the request+ Assert.AreEqual(text["newgame.confirm"], Ui.Q<Label>("toast").text, "precondition: New Game is armed");+ Escape(root);+ Assert.AreEqual("", Ui.Q<Label>("toast").text, "the Escape withdraws the request");+ Assert.IsTrue(HudRoot.ClassListContains(StartScreenPresenter.OpenClass), "the start screen stays");+ Assert.IsTrue(HudRoot.ClassListContains(EndScreenPresenter.OpenClass), "and only the request: the end screen stays");++ UiTestKit.Press(Ui.Q<Button>("start-continue"));+ Assert.IsTrue(HudRoot.ClassListContains(EndScreenPresenter.OpenClass), "Continue finds the end report");+ Escape(root);+ Assert.IsFalse(HudRoot.ClassListContains(EndScreenPresenter.OpenClass), "now Escape closes the end screen");+ }+}diff --git a/unity/Assets/Tests/EditMode/GameRootEscapeLayerTests.cs.meta b/unity/Assets/Tests/EditMode/GameRootEscapeLayerTests.cs.metanew file mode 100644index 000000000..107838df7--- /dev/null+++ b/unity/Assets/Tests/EditMode/GameRootEscapeLayerTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 52a3e738d1f5cbfecb7d321704b6d5dd\ No newline at end of filediff --git a/unity/Assets/Tests/EditMode/GameSceneTests.cs b/unity/Assets/Tests/EditMode/GameSceneTests.csindex 3d0f03e30..8bb5ea51a 100644--- a/unity/Assets/Tests/EditMode/GameSceneTests.cs+++ b/unity/Assets/Tests/EditMode/GameSceneTests.cs@@ -1286,6 +1286,10 @@ public class GameRootEscapeTests : SharedCameraActionsFixtureEscape();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");+ // The next key press is a later frame: the screen closed on the first press, and ScreenNav.ClosedThisFrame+ // would still claim this one in the single EditMode frame (#2609).+ var nav = typeof(GameRoot).GetField("nav", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(root)!;+ typeof(ScreenNav).GetField("closedFrame", BindingFlags.NonPublic | BindingFlags.Instance)!.SetValue(nav, -1);Escape();Assert.IsFalse(newGame.Armed, "the next Escape withdraws the request");Assert.AreEqual("", Ui.Q<Label>("toast").text, "the prompt is off the toast");