A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
UI: an X on every side-menu screen closes it (a phone has no Escape) (#1027)
agamrafaeli-instincte9bada93d4a7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b760a512f..5729d15ed 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,11 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB## [Unreleased]+### Fixed+- The Cabinet, Budget, Defence, Why, How to play and Settings screens each gain an X in the header that closes+ them (the same close as Escape): a phone has no Escape key, and re-tapping the lit rail button was the only way+ out (v0.1.1 playtest feedback).+## [0.1.2] - 2026-10-19### Addeddiff --git a/unity/Assets/Scripts/UI/ScreenNav.cs b/unity/Assets/Scripts/UI/ScreenNav.csindex 834a8c280..f7fd9278f 100644--- a/unity/Assets/Scripts/UI/ScreenNav.cs+++ b/unity/Assets/Scripts/UI/ScreenNav.cs@@ -76,7 +76,9 @@ namespace Statecraft.Game.UIscreen.style.display.keyword == StyleKeyword.Undefined && screen.style.display.value == DisplayStyle.Flex;readonly VisualElement root;readonly List<(Button, System.Action)> hooks = new List<(Button, System.Action)>();+ readonly List<Button> closes = new List<Button>();readonly EventCallback<NavigationCancelEvent> onCancel;+ readonly EventCallback<GeometryChangedEvent> onGeometry;public ScreenNav(VisualElement root){@@ -86,6 +88,8 @@ namespace Statecraft.Game.UI// no element has the focus (CloseOnEscape).onCancel = e => { if (Close()) e.StopPropagation(); };root.RegisterCallback(onCancel, TrickleDown.TrickleDown);+ onGeometry = e => FitCloseButtons();+ root.RegisterCallback(onGeometry);foreach (var s in Screens){var screen = s;@@ -93,15 +97,67 @@ namespace Statecraft.Game.UISystem.Action h = () => Toggle(screen);b.clicked += h;hooks.Add((b, h));++ // an X on the screen itself closes it: a phone has no Escape, and re-tapping the lit rail button is not+ // discoverable (playtest v0.1.1). The same Close() as Escape, so the focus return is identical.+ var header = root.Q("screen-" + screen)?.Q(className: "screen-header");+ if (header == null) continue;+ var x = new Button(() => Close()) { name = "screen-close-" + screen, text = "X" };+ x.AddToClassList("screen-close");+ header.Add(x);+ closes.Add(x);}Open(null);+ FitCloseButtons();+ }++ /// <summary>The X's hit area: 44 screen px at every window size (#1035's ruling), so in panel units it grows as+ /// the panel shrinks (<paramref name="scale"/> is screen px per panel px; the 1280x720 window scales about 2/3,+ /// so 44 panel px would render as about 29 px there). On the web a screen pixel is a CSS pixel: the canvas is+ /// drawn in device pixels, so the target is <paramref name="dpr"/> device px per CSS px (HudPresenter.cs's text+ /// floor counts the same way, DevicePixelRatio.jslib). Off the web <paramref name="dpr"/> is 1.</summary>+ internal static float CloseTargetPanelPx(float scale, float dpr = 1f) =>+ scale > 0f ? 44f * (dpr > 0f ? dpr : 1f) / scale : 44f;++#if UNITY_WEBGL && !UNITY_EDITOR+ // The panel scales by the canvas's device pixels, so a screen pixel here is a CSS pixel: the device's pixels / the+ // browser's devicePixelRatio (DevicePixelRatio.jslib; the same import as HudPresenter's text floor).+ [System.Runtime.InteropServices.DllImport("__Internal")] static extern float StatecraftDevicePixelRatio();+ static float DevicePixelRatio() { var r = StatecraftDevicePixelRatio(); return r > 0f ? r : 1f; }+#else+ static float DevicePixelRatio() => 1f;+#endif++ void FitCloseButtons()+ {+ var px = CloseTargetPanelPx(root.panel?.scaledPixelsPerPoint ?? 1f, DevicePixelRatio());+ var r = new StyleLength(new Length(px / 2f));+ foreach (var x in closes)+ {+ x.style.width = px; x.style.height = px;+ x.style.borderTopLeftRadius = r; x.style.borderTopRightRadius = r;+ x.style.borderBottomLeftRadius = r; x.style.borderBottomRightRadius = r;++ // Pad both sides of the header here, not in the stylesheet: a grown X (66 panel px at 1280x720, 89 at+ // 390x844) would cover the title's end under the old fixed 52 px, and one inline rule serves both+ // languages. Also grow the header so the X (top 6 px + px) fits inside it, with its 4 px bottom+ // padding and 1 px border.+ var header = x.parent;+ if (header == null) continue;+ header.style.paddingLeft = new StyleLength(px + 8f);+ header.style.paddingRight = new StyleLength(px + 8f);+ header.style.minHeight = new StyleLength(px + 6f + 5f);+ }}public void Dispose(){foreach (var (b, h) in hooks) b.clicked -= h;hooks.Clear();+ foreach (var x in closes) x.RemoveFromHierarchy();+ closes.Clear();root.UnregisterCallback(onCancel, TrickleDown.TrickleDown);+ root.UnregisterCallback(onGeometry);var r = root.Q("root") ?? root;r.RemoveFromClassList(WideOpenClass); r.RemoveFromClassList(ScreenOpenClass);DisableCovered(root);diff --git a/unity/Assets/Tests/EditMode/UiFoundationTests.cs b/unity/Assets/Tests/EditMode/UiFoundationTests.csindex 4ba0bb9b5..799bb75a9 100644--- a/unity/Assets/Tests/EditMode/UiFoundationTests.cs+++ b/unity/Assets/Tests/EditMode/UiFoundationTests.cs@@ -19,6 +19,139 @@ public class UiFoundationTestsAssert.IsNotNull(ps.themeStyleSheet, "theme");}+ /// <summary>Playtest v0.1.1: every screen has an X in its header that closes it - a phone has no Escape, and+ /// re-tapping the lit rail button is not discoverable.</summary>+ [Test]+ public void EveryScreenHasACloseButtonThatClosesIt()+ {+ using var ui = new UiTestKit();+ using var nav = new ScreenNav(ui.Root);+ foreach (var s in ScreenNav.Screens)+ {+ nav.Toggle(s);+ Assert.AreEqual(s, nav.Current, s + " open");+ var x = ui.Q<Button>("screen-close-" + s);+ x.Focus();+ UiTestKit.Press(x); // Press(Button) is static; only Press(string) is an instance method+ Assert.IsNull(nav.Current, s + ": the X closes the screen, like Escape");+ Assert.AreEqual(DisplayStyle.None, ui.Q<VisualElement>("screen-" + s).style.display.value, s + " hidden");+ Assert.AreSame(ui.Q<Button>("nav-" + s), ui.Root.panel.focusController.focusedElement,+ s + ": the focus goes back to the screen's rail button, as after Escape (#1035)");+ }+ }++ /// <summary>#1035's ruling: the X's hit area is 44 screen px at every window size, so in panel units it grows as+ /// the panel shrinks. On the web a screen pixel is a CSS pixel, so the device-pixel ratio multiplies the target+ /// (44 device px was about 15 CSS px on a DPR-3 phone; the ruling's own phone figure is in CSS px).</summary>+ [Test]+ public void TheCloseButtonsHitAreaIs44ScreenPixelsAtEveryScale()+ {+ Assert.AreEqual(44f, ScreenNav.CloseTargetPanelPx(1f), 1e-3f);+ Assert.AreEqual(66f, ScreenNav.CloseTargetPanelPx(2f / 3f), 1e-2f); // 1280x720's scale+ Assert.AreEqual(88f, ScreenNav.CloseTargetPanelPx(0.5f), 1e-3f); // a small phone window's scale+ Assert.AreEqual(89.4f, ScreenNav.CloseTargetPanelPx(1.477f, 3f), 1e-1f); // a 390x844 phone at DPR 3: 44 CSS px+ Assert.AreEqual(132f, ScreenNav.CloseTargetPanelPx(2f / 3f, 2f), 1e-2f); // a DPR-2 laptop at 1280x720+ Assert.AreEqual(44f, ScreenNav.CloseTargetPanelPx(0f), "no scale yet: the USS size stands");+ using var ui = new UiTestKit();+ using var nav = new ScreenNav(ui.Root);+ var scale = ui.Root.panel.scaledPixelsPerPoint;+ foreach (var s in ScreenNav.Screens)+ {+ var w = ui.Q<Button>("screen-close-" + s).style.width.value.value; // StyleLength -> Length -> float, panel px+ Assert.AreEqual(44f, w * scale, 1e-2f, s + ": panel px x the panel's scale = 44 screen px");+ }+ }++ /// <summary>#1035: the X keeps clear of the screen's title in both languages - in Hebrew it sits on the left, and the+ /// header's padding keeps the title away from it. (The Hebrew overlap this guards was measured on all five screens.)</summary>+ [UnityTest]+ public IEnumerator TheCloseButtonKeepsClearOfTheTitleInBothLanguages()+ {+ foreach (var he in new[] { false, true })+ {+ using var ui = new UiTestKit();+ using var nav = new ScreenNav(ui.Root);+ if (he) ui.Q<VisualElement>("root").AddToClassList(UiLanguage.HebrewClass);+ var lang = he ? "HE" : "EN";+ foreach (var s in ScreenNav.Screens)+ {+ nav.Open(s);+ yield return null; // as in the window-size test: a reopened screen lays out on the next frame+ var x = ui.Q<Button>("screen-close-" + s);+ var title = ui.Q<VisualElement>("screen-" + s).Q(className: "title");+ Assert.IsNotNull(title, s + " has a title");+ for (int i = 0; i < 10 && (float.IsNaN(x.worldBound.width) || float.IsNaN(title.worldBound.width)); i++) yield return null;+ Assert.IsFalse(x.worldBound.Overlaps(title.worldBound), $"{lang} {s}: the X is clear of the title");+ }+ }+ }++ /// <summary>#1035, at the ruling's own window sizes (1280x720 and a 390x844 phone), in EN and HE: the X renders+ /// at least 44 screen px, keeps clear of the title (the header's padding grows with it), and fits inside its+ /// header. The rig drives real window sizes: a copy of the PanelSettings with a RenderTexture target, as+ /// DecisionCardTests.Rig does. To see it red, remove the header padding/min-height lines in FitCloseButtons:+ /// at 1280x720 the 66 px X overlaps every title ("EN cabinet: the X is clear of the title").</summary>+ [UnityTest]+ public IEnumerator TheCloseButtonFitsItsHeaderAndMissesTheTitleAtTheRulingsWindowSizes()+ {+ foreach (var (w, h) in new[] { (1280, 720), (390, 844) })+ foreach (var he in new[] { false, true })+ {+ using var rig = new WindowRig(w, h, he);+ var lang = he ? "HE" : "EN";+ var scale = rig.Root.panel.scaledPixelsPerPoint;+ foreach (var s in ScreenNav.Screens)+ {+ rig.Nav.Open(s);+ yield return null; // a screen that was display:none lays out on the next frame; its X's bounds are 0, not NaN, until then+ var x = rig.Root.Q<Button>("screen-close-" + s);+ var header = x.parent;+ var title = rig.Root.Q<VisualElement>("screen-" + s).Q(className: "title");+ for (int i = 0; i < 10 && (float.IsNaN(x.worldBound.width) || float.IsNaN(title.worldBound.width)); i++) yield return null;+ Assert.GreaterOrEqual(x.worldBound.width * scale, 44f - 1e-1f, $"{w}x{h} {lang} {s}: the X renders at least 44 screen px");+ Assert.IsFalse(x.worldBound.Overlaps(title.worldBound), $"{w}x{h} {lang} {s}: the X is clear of the title");+ Assert.LessOrEqual(x.worldBound.yMax, header.worldBound.yMax + 1e-1f, $"{w}x{h} {lang} {s}: the X fits inside its header");+ Assert.AreEqual(he, x.worldBound.center.x < header.worldBound.center.x, $"{w}x{h} {lang} {s}: the X sits on the left in Hebrew, the right in English");+ }+ }+ }++ /// <summary>The game's UI on a panel whose PanelSettings copy targets a RenderTexture of a real window size (the+ /// DecisionCardTests.Rig pattern), so scaledPixelsPerPoint is the window's true scale.</summary>+ sealed class WindowRig : System.IDisposable+ {+ public readonly VisualElement Root; public readonly ScreenNav Nav;+ readonly GameObject go; readonly PanelSettings ps; readonly RenderTexture rt; readonly HudPresenter hud;++ public WindowRig(int w, int h, bool hebrew)+ {+ rt = new RenderTexture(w, h, 0);+ ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));+ ps.targetTexture = rt;+ go = new GameObject("UiFoundationWindowTest");+ go.SetActive(false);+ var doc = go.AddComponent<UIDocument>();+ doc.panelSettings = ps;+ doc.visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(UiPaths.GameUxml);+ go.SetActive(true);+ Root = doc.rootVisualElement;+ Root.Q("root").RemoveFromClassList("loading");+ if (hebrew) Root.Q("root").AddToClassList(UiLanguage.HebrewClass);+ // The narrow-window classes (hud-narrow/-two-row/-three-row) only come from a live HudPresenter; without+ // one a small window keeps the desktop .screen insets and the screens' widths go negative.+ var text = UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(hebrew ? UiPaths.StringsHebrew : UiPaths.Strings).text);+ hud = new HudPresenter(Root, new SimBridge(Statecraft.Sim.GameSession.New(Statecraft.Sim.ContentLoader.Load(Statecraft.Game.Paths.Content),+ Statecraft.Sim.GoldenRun.Seed, Statecraft.Sim.Mode.Historical), text), text);+ Nav = new ScreenNav(Root);+ }++ public void Dispose()+ {+ Nav.Dispose(); hud.Dispose();+ Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt);+ }+ }+[Test]public void ASubmittedButtonFiresClickedSynchronously(){diff --git a/unity/Assets/UI/Statecraft.uss b/unity/Assets/UI/Statecraft.ussindex 4230f8bf8..f53f82cb3 100644--- a/unity/Assets/UI/Statecraft.uss+++ b/unity/Assets/UI/Statecraft.uss@@ -390,12 +390,23 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);/* Full screens (Cabinet, Budget, Defence, Why, Settings) open in the centre, one at a time. *//* Hidden until ScreenNav opens one (never visible while loading; playtest 2, P2-1). Right edge clear of the 480px column. */.screen { display: none; position: absolute; left: 552px; right: 512px; top: 112px; bottom: 16px; padding: 18px; background-color: rgb(11, 18, 32); } /* opaque: a reading screen, and the map labels must not show through */-.screen-header { flex-direction: column; align-items: stretch; border-bottom-width: 1px; border-color: var(--gold-dim); margin-bottom: 10px; padding-bottom: 4px; }+.screen-header { flex-direction: column; align-items: stretch; border-bottom-width: 1px; border-color: var(--gold-dim); margin-bottom: 10px; padding-bottom: 4px; } /* the X's side padding is set inline by ScreenNav.FitCloseButtons (it grows with the X) *//* Screen titles and section names in gold, letter-spaced, as the promo's "DEFENSE" and "LEADERSHIP". */.screen-header > .title { color: var(--gold-light); letter-spacing: 1px; }/* The subtitle sits under the title, the screen's full width, and wraps in a narrow window (#211). A column, not awrapping row: a shrinking, wrapping label in a row re-measured itself every frame (PromoScreensTests idle test). */.screen-header > .muted { white-space: normal; }++/* An X on an open screen (ScreenNav adds one to every screen-header): a phone has no Escape (playtest v0.1.1). */+.screen-close {+ position: absolute; top: 6px; right: 8px;+ width: 44px; height: 44px; margin: 0; padding: 0;+ background-color: rgba(0, 0, 0, 0); border-width: 1px; border-color: var(--gold-dim); border-radius: 22px;+ color: var(--gold-light); font-size: 20px; -unity-font-style: bold; -unity-text-align: middle-center;+}+.screen-close:hover, .screen-close:focus { border-color: var(--gold-light); }+/* Hebrew mirrors: the X sits on the left, and the header's free side moves with it (#1035). */+.lang-he .screen-close { right: auto; left: 8px; }.screen .meter-name { color: var(--gold); letter-spacing: 2px; font-size: 16px; margin-top: 8px; }.row { flex-direction: row; align-items: center; }.grow { flex-grow: 1; }