Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

HUD: disable a panel a root class hides, so its buttons take no focus (fixes #400) (#409)

Eyal Golanf93fd5ef4d7e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bdd66ad3a..35b41df55 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -284,6 +284,10 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
It decodes to the same bytes, so the terrain is unchanged (#44).
### Fixed
+- In the two-row HUD (a window narrower than about square), a button in the event column or the info panel hidden under an
+ open screen could still take the keyboard focus once the 1950 build buttons were live. The covered panels are now
+ disabled as well as hidden, also when the window narrows under an open screen, and a panel hidden by the start screen
+ or a wide screen is not re-enabled by a tier change or a screen closing under it (#400).
- The map's far tilt-shift band no longer blurs the far towns into a smear at the play zoom (#390). Since the map drew
faster (#320), the quality fallback no longer turns the tilt-shift off, and the far band showed in full. On the owner's
ruling it is now gently soft at the gameplay zoom, as on promo 03, and in full only at the overview. Presentation only.
diff --git a/unity/Assets/Scripts/UI/HudPresenter.cs b/unity/Assets/Scripts/UI/HudPresenter.cs
index 54a5e92fb..9027c4f70 100644
--- a/unity/Assets/Scripts/UI/HudPresenter.cs
+++ b/unity/Assets/Scripts/UI/HudPresenter.cs
@@ -77,6 +77,7 @@ namespace Statecraft.Game.UI
foreach (var m in meters) m.UnregisterCallback<GeometryChangedEvent>(OnMeterGeometry);
bar.RemoveFromClassList(RoomyClass);
bar.RemoveFromClassList(CompactClass); hudRoot.RemoveFromClassList(NarrowClass); hudRoot.RemoveFromClassList(TwoRowClass); hudRoot.RemoveFromClassList(ThreeRowClass);
+ ScreenNav.DisableCovered(hudRoot);
}
// The bar's width does not depend on the class, so this settles in one pass.
@@ -118,6 +119,7 @@ namespace Statecraft.Game.UI
hudRoot.EnableInClassList(NarrowClass, width < NarrowBelowPx);
hudRoot.EnableInClassList(TwoRowClass, width < TwoRowBelowPx);
hudRoot.EnableInClassList(ThreeRowClass, width < ThreeRowBelowPx);
+ ScreenNav.DisableCovered(hudRoot); // two rows or one: what an open screen covers changes with it (#400)
}
/// <summary>End Turn's hourglass, in the button's left padding (Statecraft.uss .end-turn), painted into the button so it
diff --git a/unity/Assets/Scripts/UI/ScreenNav.cs b/unity/Assets/Scripts/UI/ScreenNav.cs
index 564adb63e..973350e44 100644
--- a/unity/Assets/Scripts/UI/ScreenNav.cs
+++ b/unity/Assets/Scripts/UI/ScreenNav.cs
@@ -20,6 +20,22 @@ namespace Statecraft.Game.UI
/// the full width, so Statecraft.uss hides the event column and the info panel under it (#210).</summary>
public const string ScreenOpenClass = "screen-open";
+ /// <summary>Disables each HUD panel that a class on the "root" element hides, and enables it again when none does: a
+ /// child of an undisplayed element can still take the keyboard focus, so hiding alone leaves its buttons in Tab's
+ /// reach (#400). The start screen covers the top bar, the event column and the info panel; in the two-row HUD any
+ /// open screen covers the event column and the info panel; a wide screen covers the info panel. Called wherever
+ /// one of those classes changes (StartScreenPresenter, Open, HudPresenter's tier), so no caller re-enables a panel
+ /// another still covers.</summary>
+ public static void DisableCovered(VisualElement root)
+ {
+ var r = root.Q("root") ?? root;
+ bool start = r.ClassListContains(StartScreenPresenter.OpenClass);
+ bool full = r.ClassListContains(ScreenOpenClass) && r.ClassListContains(HudPresenter.TwoRowClass);
+ r.Q("top-bar")?.SetEnabled(!start);
+ r.Q("right-column")?.SetEnabled(!(start || full));
+ r.Q("info-panel")?.SetEnabled(!(start || full || r.ClassListContains(WideOpenClass)));
+ }
+
/// <summary>Asks to be told, synchronously, whenever <paramref name="screen"/> is opened (a presenter that skips
/// rendering while its screen is hidden catches up then).</summary>
public static void OnOpened(VisualElement screen, System.Action action) => screen.userData = action;
@@ -57,6 +73,7 @@ namespace Statecraft.Game.UI
root.UnregisterCallback(onCancel, TrickleDown.TrickleDown);
var r = root.Q("root") ?? root;
r.RemoveFromClassList(WideOpenClass); r.RemoveFromClassList(ScreenOpenClass);
+ DisableCovered(root);
}
/// <summary>Closes the open screen (Escape); false when none was open.</summary>
@@ -89,6 +106,7 @@ namespace Statecraft.Game.UI
}
(root.Q("root") ?? root).EnableInClassList(WideOpenClass, wide);
(root.Q("root") ?? root).EnableInClassList(ScreenOpenClass, screen != null);
+ DisableCovered(root);
if (screen != null) (root.Q<VisualElement>("screen-" + screen).userData as System.Action)?.Invoke();
}
}
diff --git a/unity/Assets/Scripts/UI/StartScreenPresenter.cs b/unity/Assets/Scripts/UI/StartScreenPresenter.cs
index a98143bfe..24c471cd1 100644
--- a/unity/Assets/Scripts/UI/StartScreenPresenter.cs
+++ b/unity/Assets/Scripts/UI/StartScreenPresenter.cs
@@ -28,9 +28,6 @@ namespace Statecraft.Game.UI
public const float ArtAspect = 16f / 9f;
/// <summary>The URL flag (WebGL) or command-line flag (desktop, "-nostart") that skips the screen by hand.</summary>
public const string NoStartFlag = "nostart";
- /// <summary>The HUD regions under the screen: hidden by the stylesheet and disabled while it shows, so no key reaches
- /// them (a hidden button can still report that it takes focus until the next layout pass).</summary>
- public static readonly string[] Hud = { "top-bar", "right-column", "info-panel" };
/// <summary>Whether the screen is skipped: the automated runs (the golden check, the smoke run, the shot, frame and
/// pixel-error profiles, the unit showcase and the border proof, all of which drive or measure the map straight away),
@@ -88,7 +85,7 @@ namespace Statecraft.Game.UI
{
continueButton.style.display = Resumed ? DisplayStyle.Flex : DisplayStyle.None;
hudRoot.AddToClassList(OpenClass);
- foreach (var name in Hud) hudRoot.Q(name)?.SetEnabled(false);
+ ScreenNav.DisableCovered(hudRoot); // the HUD under it: hidden by the stylesheet, disabled so no key reaches it
Fit();
// Focus once the styles have resolved (a button still hidden by the stylesheet cannot take it).
var first = Resumed ? continueButton : newGameButton;
@@ -103,7 +100,7 @@ namespace Statecraft.Game.UI
{
if (!IsOpen) return;
hudRoot.RemoveFromClassList(OpenClass);
- foreach (var name in Hud) hudRoot.Q(name)?.SetEnabled(true);
+ ScreenNav.DisableCovered(hudRoot);
var focused = screen.focusController?.focusedElement as VisualElement;
if (focused != null && screen.Contains(focused)) focused.Blur();
shownChanged?.Invoke(false);
diff --git a/unity/Assets/Tests/EditMode/StartScreenTests.cs b/unity/Assets/Tests/EditMode/StartScreenTests.cs
index 68670683c..427732a36 100644
--- a/unity/Assets/Tests/EditMode/StartScreenTests.cs
+++ b/unity/Assets/Tests/EditMode/StartScreenTests.cs
@@ -165,6 +165,7 @@ public class StartScreenTests
}
public void Press(string name) => UiTestKit.Press(Q<Button>(name));
+
public void Dispose() { Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt); }
}
@@ -325,6 +326,56 @@ public class StartScreenTests
Assert.IsTrue(rig.Ui.Q<Button>("end-turn").canGrabFocus, "the HUD is back");
}
+ /// <summary>#400: a turn-1 save keeps the build buttons disabled anyway, so the test above cannot see them. With a save
+ /// from after August 1950, a selected hex and an event waiting, the build buttons and the event's options are live, and
+ /// still none of them takes the focus under the start screen.
+ /// Built in GameRoot's order: the HUD, ScreenNav, the info panel and the event card, then the screen.</summary>
+ [UnityTest]
+ public IEnumerator ASaveFrom1950KeepsTheLiveHudOutOfReach()
+ {
+ var g = GameSession.New(C, 9, Mode.Historical);
+ while (g.State.Date < new DateTime(1950, 8, 1) || g.State.PendingEvents.Count == 0) g.Step(HistoricalAI.Commands(C, g.State));
+ var store = TempStore();
+ store.WriteAutosave(g.Save.Write());
+ using var ui = new RenderedUi();
+ var bridge = new SimBridge(GameStart.Begin(C, store, () => 4UL, new List<string>(), Text, out var resumed), Text);
+ var owned = new List<IDisposable>();
+ try
+ {
+ ui.Root.Q("root").RemoveFromClassList("loading");
+ owned.Add(new HudPresenter(ui.Root, bridge, Text));
+ owned.Add(new ScreenNav(ui.Root));
+ var info = new InfoPanelPresenter(ui.Root, bridge, Text, C.SettlementHexes.ToDictionary(x => x.Id, x => x.Id)); owned.Add(info);
+ owned.Add(new EventCardPresenter(ui.Root, bridge, Text, null));
+ var toast = new ToastPresenter(ui.Root, bridge, Text); owned.Add(toast);
+ var newGame = new NewGamePresenter(ui.Root, bridge, store, toast, Text, () => 5UL); owned.Add(newGame);
+ var tlv = C.SettlementHexes.Single(x => x.Id == "tel_aviv");
+ info.Show(tlv.Q, tlv.R);
+ var start = new StartScreenPresenter(ui.Root, newGame, resumed); owned.Add(start);
+ start.Show();
+ for (int i = 0; i < 4; i++) yield return null;
+ Assert.IsTrue(resumed, "precondition: the 1950 save is continued");
+ var build = ui.Q<Button>("build-camp");
+ Assert.IsTrue(build.enabledSelf && build.style.display.value == DisplayStyle.Flex, "precondition: build-camp is live: " + ui.Q<Label>("info-reason").text);
+ Assert.IsNotNull(ui.Root.Q("event-options").Q<Button>(), "precondition: an event's options are shown");
+ void NoneReachable(string where)
+ {
+ foreach (var region in new[] { "top-bar", "right-column", "info-panel" })
+ foreach (var b in ui.Root.Q(region).Query<Button>().ToList())
+ {
+ Assert.IsFalse(b.canGrabFocus, $"{where}: {region} {b.name} cannot take the focus under the start screen");
+ b.Focus();
+ Assert.AreNotSame(b, ui.Root.focusController?.focusedElement, $"{where}: {region} {b.name} is not focused");
+ }
+ }
+ NoneReachable("under the start screen");
+ ui.Press("start-continue");
+ yield return null; yield return null;
+ Assert.IsTrue(build.canGrabFocus, "the build button is back once the screen closes");
+ }
+ finally { for (int i = owned.Count - 1; i >= 0; i--) owned[i].Dispose(); }
+ }
+
// --- Layout ------------------------------------------------------------------------------------------------------
/// <summary>The painting is a native render of at least 1920x1080 at 16:9 (StartScreenPresenter.ArtAspect), imported at
diff --git a/unity/Assets/Tests/EditMode/UiLayoutTests.cs b/unity/Assets/Tests/EditMode/UiLayoutTests.cs
index 25f266038..3802c790a 100644
--- a/unity/Assets/Tests/EditMode/UiLayoutTests.cs
+++ b/unity/Assets/Tests/EditMode/UiLayoutTests.cs
@@ -128,6 +128,90 @@ public class UiLayoutTests
}
finally { nav.Dispose(); info.Dispose(); Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt); }
}
+
+ /// <summary>#400: in the two-row HUD every open screen takes the full width, and the stylesheet hides the event column and
+ /// the info panel under it (.hud-two-row.screen-open). In August 1950, with a hex selected and an event waiting, their
+ /// buttons are live, and none may take the focus while hidden: when Budget opens in two rows, and when a window already
+ /// showing Budget beside the panels narrows into two rows (the tier changes under the open screen). Widening again, or
+ /// closing the screen, brings them back.</summary>
+ [UnityTest]
+ public IEnumerator TheTwoRowHudKeepsWhatAScreenCoversOutOfReach()
+ {
+ var rt = new RenderTexture(1920, 1080, 0);
+ var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));
+ ps.targetTexture = rt;
+ var go = new GameObject("TwoRowTest");
+ go.SetActive(false);
+ var doc = go.AddComponent<UIDocument>();
+ doc.panelSettings = ps;
+ doc.visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(UiPaths.GameUxml);
+ go.SetActive(true);
+ var root = doc.rootVisualElement;
+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ while (g.State.Date < new System.DateTime(1950, 8, 1) || g.State.PendingEvents.Count == 0) g.Step(HistoricalAI.Commands(C, g.State));
+ var bridge = new SimBridge(g, Text);
+ var owned = new List<System.IDisposable>();
+ void Resize(int w, int h) { var old = rt; rt = new RenderTexture(w, h, 0); ps.targetTexture = rt; old.Release(); Object.DestroyImmediate(old); }
+ try
+ {
+ root.Q("root").RemoveFromClassList("loading");
+ owned.Add(new HudPresenter(root, bridge, Text));
+ var nav = new ScreenNav(root); owned.Add(nav);
+ var info = new InfoPanelPresenter(root, bridge, Text, C.SettlementHexes.ToDictionary(x => x.Id, x => x.Id)); owned.Add(info);
+ owned.Add(new EventCardPresenter(root, bridge, Text, null));
+ var tlv = C.SettlementHexes.Single(x => x.Id == "tel_aviv");
+ info.Show(tlv.Q, tlv.R);
+ for (int i = 0; i < 4; i++) yield return null;
+ var build = root.Q<Button>("build-camp");
+ Assert.IsTrue(build.enabledSelf && build.style.display.value == DisplayStyle.Flex, "precondition: build-camp is live: " + root.Q<Label>("info-reason").text);
+ Assert.IsNotNull(root.Q("event-options").Q<Button>(), "precondition: an event's options are shown");
+ var covered = new[] { "right-column", "info-panel" };
+ List<Button> Buttons() => covered.SelectMany(r => root.Q(r).Query<Button>().ToList()).ToList();
+ void Reach(bool expected, string where)
+ {
+ // In reach: the two buttons known to be shown. Out of reach: every button in the covered panels.
+ foreach (var b in expected ? new[] { build, root.Q("event-options").Q<Button>() }.ToList() : Buttons())
+ {
+ Assert.AreEqual(expected, b.canGrabFocus, $"{where}: {b.name} can take the focus");
+ if (expected) continue;
+ b.Focus();
+ Assert.AreNotSame(b, b.focusController?.focusedElement, $"{where}: {b.name} is not focused");
+ }
+ }
+
+ // Budget, a narrow screen, at 1080p: the panels stay beside it, in reach.
+ nav.Open("budget");
+ for (int i = 0; i < 3; i++) yield return null;
+ Assert.IsFalse(root.Q("root").ClassListContains(HudPresenter.TwoRowClass), "precondition: one row at 1080p");
+ Reach(true, "1080p, Budget open");
+
+ // The window narrows under the open screen: two rows, the screen full width, the panels hidden and out of reach.
+ Resize(960, 1080);
+ for (int i = 0; i < 4; i++) yield return null;
+ Assert.IsTrue(root.Q("root").ClassListContains(HudPresenter.TwoRowClass), "precondition: two rows at 960x1080");
+ foreach (var r in covered) Assert.AreEqual(DisplayStyle.None, root.Q(r).resolvedStyle.display, r + " is hidden");
+ Reach(false, "narrowed with Budget open");
+
+ // Closed, then Budget opened again in two rows.
+ nav.Open(null);
+ for (int i = 0; i < 3; i++) yield return null;
+ Reach(true, "two rows, no screen");
+ nav.Open("budget");
+ for (int i = 0; i < 3; i++) yield return null;
+ Reach(false, "two rows, Budget opened");
+
+ // Widened again with Budget open: one row, the panels back.
+ Resize(1920, 1080);
+ for (int i = 0; i < 4; i++) yield return null;
+ Assert.IsFalse(root.Q("root").ClassListContains(HudPresenter.TwoRowClass), "precondition: one row again");
+ Reach(true, "widened with Budget open");
+ }
+ finally
+ {
+ for (int i = owned.Count - 1; i >= 0; i--) owned[i].Dispose();
+ Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt);
+ }
+ }
static UiText Text => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);
/// <summary>The window sizes a player is likely to have: 16:9, 5:4, the playtest's wide-and-short browser window