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.

ui: staff-review fixes for the promo HUD

eyalgolanacd38126df4f
diff --git a/unity/Assets/Scripts/UI/EventCardPresenter.cs b/unity/Assets/Scripts/UI/EventCardPresenter.cs
index 81f67c5f7..e83a9e138 100644
--- a/unity/Assets/Scripts/UI/EventCardPresenter.cs
+++ b/unity/Assets/Scripts/UI/EventCardPresenter.cs
@@ -68,7 +68,7 @@ namespace Statecraft.Game.UI
/// <summary>The round sign in an option's left padding (Statecraft.uss .event-option), painted into the button so it
/// stays a leaf whose wrapped text Yoga measures (UiIcon.Paint).</summary>
- public const float SignRadius = 15f, SignLeft = 9f;
+ const float SignRadius = 15f, SignLeft = 9f;
static void PaintSign(MeshGenerationContext m) =>
UiIcon.PaintDisc(m, "chevron", new UnityEngine.Vector2(SignLeft + SignRadius, m.visualElement.layout.height / 2f), SignRadius);
diff --git a/unity/Assets/Scripts/UI/HudPresenter.cs b/unity/Assets/Scripts/UI/HudPresenter.cs
index 526f141be..1a276c6e5 100644
--- a/unity/Assets/Scripts/UI/HudPresenter.cs
+++ b/unity/Assets/Scripts/UI/HudPresenter.cs
@@ -43,8 +43,11 @@ namespace Statecraft.Game.UI
/// <summary>End Turn's hourglass, in the button's left padding (Statecraft.uss .end-turn), painted into the button so it
/// stays a leaf sized by its text (UiIcon.Paint).</summary>
- static void PaintHourglass(MeshGenerationContext m) =>
+ static void PaintHourglass(MeshGenerationContext m)
+ {
+ if (!m.visualElement.enabledInHierarchy) return; // no bright sign on a disabled End Turn (game over)
UiIcon.Paint(m, "hourglass", new UnityEngine.Rect(10f, (m.visualElement.layout.height - 22f) / 2f, 22f, 22f));
+ }
void Set(string name, string value) => root.Q<Label>(name).text = value;
diff --git a/unity/Assets/Scripts/UI/MinimapPresenter.cs b/unity/Assets/Scripts/UI/MinimapPresenter.cs
index 27ba4e804..8ae8c3c66 100644
--- a/unity/Assets/Scripts/UI/MinimapPresenter.cs
+++ b/unity/Assets/Scripts/UI/MinimapPresenter.cs
@@ -69,7 +69,7 @@ namespace Statecraft.Game.UI
var biome = new System.Collections.Generic.Dictionary<(int, int), string>();
foreach (var h in hexes.hexes) biome[(h.q, h.r)] = h.biome;
var b = hexes.bounds;
- var tex = new Texture2D(width, height, TextureFormat.RGB24, false) { name = "Minimap", filterMode = FilterMode.Bilinear, wrapMode = TextureWrapMode.Clamp };
+ var tex = new Texture2D(width, height, TextureFormat.RGB24, false) { name = "Minimap", filterMode = FilterMode.Point, wrapMode = TextureWrapMode.Clamp };
var pixels = new Color[width * height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
diff --git a/unity/Assets/Scripts/UI/ScreenNav.cs b/unity/Assets/Scripts/UI/ScreenNav.cs
index ff9531981..aa966ff33 100644
--- a/unity/Assets/Scripts/UI/ScreenNav.cs
+++ b/unity/Assets/Scripts/UI/ScreenNav.cs
@@ -15,6 +15,9 @@ namespace Statecraft.Game.UI
public const string OpenClass = "nav-open";
readonly VisualElement root;
readonly List<(Button, System.Action)> hooks = new List<(Button, System.Action)>();
+ // A pointer click leaves the button focused (UI Toolkit focuses on pointer-down and has no :focus-visible); blurring it
+ // keeps the focus ring for the keyboard only, so a screen just closed with the mouse does not still look selected.
+ static readonly EventCallback<ClickEvent> BlurOnClick = e => (e.currentTarget as VisualElement)?.Blur();
public ScreenNav(VisualElement root)
{
@@ -25,12 +28,13 @@ namespace Statecraft.Game.UI
var b = root.Q<Button>("nav-" + screen);
System.Action h = () => Toggle(screen);
b.clicked += h;
+ b.RegisterCallback(BlurOnClick);
hooks.Add((b, h));
}
Open(null);
}
- public void Dispose() { foreach (var (b, h) in hooks) b.clicked -= h; hooks.Clear(); }
+ public void Dispose() { foreach (var (b, h) in hooks) { b.clicked -= h; b.UnregisterCallback(BlurOnClick); } hooks.Clear(); }
/// <summary>The open screen's name, or null.</summary>
public string? Current { get; private set; }
diff --git a/unity/Assets/Scripts/UI/UiFrame.cs b/unity/Assets/Scripts/UI/UiFrame.cs
index b156173e8..d917be1a8 100644
--- a/unity/Assets/Scripts/UI/UiFrame.cs
+++ b/unity/Assets/Scripts/UI/UiFrame.cs
@@ -8,7 +8,8 @@ namespace Statecraft.Game.UI
/// edge, a thin gold rule inset inside the border, and a gold corner bracket with a small diamond at each corner. It is
/// an absolute overlay that fills its parent (Statecraft.uss ".ui-frame"), takes no clicks and lays nothing out, so a
/// panel's layout is the same with or without it. Inside a panel that clips (overflow: hidden) the shadow is clipped
- /// away and only the trim shows. In UXML: <c>&lt;Statecraft.Game.UI.UiFrame /&gt;</c> as a panel's first child.</summary>
+ /// away and only the trim shows. In UXML: <c>&lt;Statecraft.Game.UI.UiFrame /&gt;</c> as a panel's last child, so the trim is drawn over the content (the
+ /// top bar's shadow-only frame is its first child: it draws nothing inside).</summary>
[UxmlElement]
public partial class UiFrame : VisualElement
{
diff --git a/unity/Assets/Scripts/UI/UiIcon.cs b/unity/Assets/Scripts/UI/UiIcon.cs
index 7bfad5007..66ff08670 100644
--- a/unity/Assets/Scripts/UI/UiIcon.cs
+++ b/unity/Assets/Scripts/UI/UiIcon.cs
@@ -14,9 +14,7 @@ namespace Statecraft.Game.UI
[UxmlElement]
public partial class UiIcon : VisualElement
{
- public const string ClassName = "ui-icon";
-
- /// <summary>Every glyph this element can draw (UiIconTests checks each one draws).</summary>
+ /// <summary>Every glyph this element can draw (PromoHudTests.EveryGlyphDraws paints each one).</summary>
public static IReadOnlyCollection<string> Glyphs => Painters.Keys;
string glyph = "";
@@ -25,12 +23,8 @@ namespace Statecraft.Game.UI
[UxmlAttribute]
public string Glyph { get => glyph; set { glyph = value ?? ""; MarkDirtyRepaint(); } }
- public UiIcon() : this("") { }
-
- public UiIcon(string glyph)
+ public UiIcon()
{
- this.glyph = glyph;
- AddToClassList(ClassName);
pickingMode = PickingMode.Ignore;
generateVisualContent += Draw;
}
@@ -251,7 +245,7 @@ namespace Statecraft.Game.UI
}
// --- terrain signs for the info panel (drawn dark on the biome's own map colour) ---
- static readonly Color Ink = new Color32(14, 22, 38, 170), InkLight = new Color32(255, 250, 236, 150);
+ static readonly Color Ink = new Color32(14, 22, 38, 170), InkLight = new Color32(255, 250, 236, 220); // waves at >= 3:1 on sea and lake
static void Waves(Painter2D p, Func<float, float, Vector2> at, float s)
{
diff --git a/unity/Assets/Tests/EditMode/PromoHudTests.cs b/unity/Assets/Tests/EditMode/PromoHudTests.cs
index a1ac1d428..6f32bb133 100644
--- a/unity/Assets/Tests/EditMode/PromoHudTests.cs
+++ b/unity/Assets/Tests/EditMode/PromoHudTests.cs
@@ -132,7 +132,9 @@ public class PromoHudTests
[UnityTest]
public IEnumerator ANarrowWindowDropsTheMeterIcons()
{
- foreach (var (w, h, compact) in new[] { (1280, 1024, true), (1920, 1080, false), (1280, 720, false) })
+ // Logical widths (1080p reference, match 0.5): 1280x1024 ~1585, 1024x768 ~1662, 1024x1024 ~1440 (the compact bar's
+ // floor), 1366x768 and 1280x720 1920, 1920x1080 1920.
+ foreach (var (w, h, compact) in new[] { (1280, 1024, true), (1024, 768, true), (1024, 1024, true), (1920, 1080, false), (1280, 720, false), (1366, 768, false) })
{
var rt = new RenderTexture(w, h, 0);
var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));
@@ -150,7 +152,13 @@ public class PromoHudTests
// As in the game: the bar is laid out (the loading screen) before the HUD is made, and the window never resizes.
for (int i = 0; i < 20 && !(root.Q("top-bar").worldBound.width > 0); i++) yield return null;
hud = new HudPresenter(root, new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text), Text);
- yield return null; yield return null;
+ // The widest real date line: a three-digit turn and the longest month.
+ root.Q<Label>("turn").text = Text.Format("hud.turn", 120);
+ root.Q<Label>("date").text = Text.Format("hud.date", PlayerText.Date(new System.DateTime(1956, 9, 30)));
+ yield return null; yield return null; yield return null;
+ var badge = root.Q(className: "date-box").worldBound;
+ foreach (var l in root.Q(className: "date-box").Query<Label>().ToList())
+ Assert.LessOrEqual(l.worldBound.xMax, badge.xMax - 4f, $"{w}x{h}: '{l.text}' {l.worldBound} runs under the badge's trim {badge}");
var bar = root.Q("top-bar");
Assert.AreEqual(compact, bar.ClassListContains(HudPresenter.CompactClass), $"{w}x{h}: bar {bar.worldBound.width:0} px");
foreach (var icon in root.Q("meters").Query<UiIcon>().ToList())
@@ -162,4 +170,115 @@ public class PromoHudTests
finally { hud?.Dispose(); Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt); }
}
}
+
+ static void Near(Color e, Color a, string what) =>
+ Assert.That(Mathf.Abs(e.r - a.r) + Mathf.Abs(e.g - a.g) + Mathf.Abs(e.b - a.b), Is.LessThan(0.02f), $"{what}: expected {e} got {a}");
+
+ /// <summary>WCAG relative luminance of a colour composited over the navy behind the HUD.</summary>
+ static float Luminance(Color c)
+ {
+ var navy = new Color(9 / 255f, 14 / 255f, 26 / 255f);
+ float Ch(float v, float under) { var x = v * c.a + under * (1 - c.a); return x <= 0.04045f ? x / 12.92f : Mathf.Pow((x + 0.055f) / 1.055f, 2.4f); }
+ return 0.2126f * Ch(c.r, navy.r) + 0.7152f * Ch(c.g, navy.g) + 0.0722f * Ch(c.b, navy.b);
+ }
+
+ static float Contrast(Color a, Color b) { float x = Luminance(a), y = Luminance(b); return (Mathf.Max(x, y) + 0.05f) / (Mathf.Min(x, y) + 0.05f); }
+
+ /// <summary>Review I1: "open" and keyboard focus are separate signals, and a pointer click does not leave a screen
+ /// button focused, so a screen just closed with the mouse never looks open.</summary>
+ [UnityTest]
+ public IEnumerator FocusAndOpenAreDistinctAndAPointerClickClearsFocus()
+ {
+ using var ui = new UiTestKit();
+ var nav = new ScreenNav(ui.Root);
+ ui.Q<VisualElement>("root").RemoveFromClassList("loading"); // ready: a hidden (loading) button takes no focus
+ try
+ {
+ VisualElement Disc(string s) => ui.Q<Button>("nav-" + s).Q(className: "nav-disc");
+ nav.Toggle("budget");
+ var cabinet = ui.Q<Button>("nav-cabinet");
+ cabinet.Focus();
+ for (int i = 0; i < 10 && cabinet.focusController.focusedElement != cabinet; i++) yield return null;
+ yield return null; yield return null;
+ Assert.AreSame(cabinet, cabinet.focusController.focusedElement, "precondition: the keyboard focus is on Cabinet");
+ var open = Disc("budget").resolvedStyle; var focused = Disc("cabinet").resolvedStyle;
+ // Open: a gold-light rim on the raised navy disc. Focus: an ink rim on the warm focus fill, at least 2.5:1 apart.
+ Assert.GreaterOrEqual(Contrast(open.backgroundColor, focused.backgroundColor), 2.5f, "focus and open differ in fill");
+ Assert.Greater(Contrast(open.borderTopColor, focused.borderTopColor), 1.1f, "focus and open differ in rim colour");
+
+ // Close Budget with the mouse: the click blurs it, and its disc returns to the unlit rim.
+ var budget = ui.Q<Button>("nav-budget");
+ budget.Focus();
+ nav.Toggle("budget");
+ using (var e = ClickEvent.GetPooled()) { e.target = budget; budget.SendEvent(e); }
+ yield return null; yield return null;
+ Assert.AreNotSame(budget, budget.focusController.focusedElement, "a pointer click leaves no focus behind");
+ Assert.IsFalse(budget.ClassListContains(ScreenNav.OpenClass));
+ Near(new Color(196 / 255f, 160 / 255f, 92 / 255f), Disc("budget").resolvedStyle.borderTopColor, "the closed button's rim is unlit");
+ Assert.AreEqual(Disc("defence").resolvedStyle.borderTopWidth, Disc("budget").resolvedStyle.borderTopWidth, "the closed button looks like the others");
+ }
+ finally { nav.Dispose(); }
+ }
+
+ /// <summary>Review I3: keyboard focus changes End Turn, an event option and a screen button by at least 3:1 (WCAG 2.4.13).</summary>
+ [UnityTest]
+ public IEnumerator FocusChangesEachControlByAtLeastThreeToOne()
+ {
+ using var ui = new UiTestKit();
+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);
+ while (!g.State.PendingEvents.Contains("armistice_egypt")) g.Step(new Command[0]);
+ var card = new EventCardPresenter(ui.Root, new SimBridge(g, Text), Text, null);
+ ui.Q<VisualElement>("root").RemoveFromClassList("loading");
+ try
+ {
+ foreach (var (name, part) in new[] { ("end-turn", ""), ("option-sign", ""), ("nav-why", "nav-disc") })
+ {
+ var b = ui.Q<Button>(name);
+ VisualElement target = part == "" ? b : b.Q(className: part);
+ for (int i = 0; i < 5; i++) yield return null;
+ var before = target.resolvedStyle;
+ Color bgBefore = before.backgroundColor, rimBefore = before.borderTopColor;
+ b.Focus();
+ for (int i = 0; i < 10 && b.focusController.focusedElement != b; i++) yield return null;
+ yield return null; yield return null;
+ Assert.AreSame(b, b.focusController.focusedElement, name + ": focused");
+ var after = target.resolvedStyle;
+ var change = Mathf.Max(Contrast(bgBefore, after.backgroundColor), Contrast(rimBefore, after.borderTopColor));
+ Assert.GreaterOrEqual(change, 3f, $"{name}: focus changes it by {change:0.00}:1");
+ b.Blur();
+ }
+ }
+ finally { card.Dispose(); }
+ }
+
+ /// <summary>Review M1: every glyph paints without an error (each is drawn in a laid-out panel, and a second callback on
+ /// the same element proves the repaint ran).</summary>
+ [UnityTest]
+ public IEnumerator EveryGlyphDraws()
+ {
+ var rt = new RenderTexture(640, 360, 0);
+ var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));
+ ps.targetTexture = rt;
+ var go = new GameObject("GlyphTest");
+ var doc = go.AddComponent<UIDocument>();
+ doc.panelSettings = ps;
+ try
+ {
+ yield return null;
+ var drawn = new System.Collections.Generic.HashSet<string>();
+ foreach (var glyph in UiIcon.Glyphs)
+ {
+ var icon = new UiIcon { Glyph = glyph };
+ icon.style.width = 32; icon.style.height = 32;
+ var name = glyph;
+ icon.generateVisualContent += _ => drawn.Add(name);
+ doc.rootVisualElement.Add(icon);
+ }
+ doc.rootVisualElement.style.flexDirection = FlexDirection.Row; doc.rootVisualElement.style.flexWrap = Wrap.Wrap;
+ for (int i = 0; i < 30 && drawn.Count < UiIcon.Glyphs.Count; i++) { doc.rootVisualElement.MarkDirtyRepaint(); yield return null; }
+ CollectionAssert.AreEquivalent(UiIcon.Glyphs, drawn, "every glyph was painted");
+ LogAssert.NoUnexpectedReceived();
+ }
+ finally { Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt); }
+ }
}
diff --git a/unity/Assets/UI/Statecraft.uss b/unity/Assets/UI/Statecraft.uss
index 110dde7b3..0315fd972 100644
--- a/unity/Assets/UI/Statecraft.uss
+++ b/unity/Assets/UI/Statecraft.uss
@@ -13,6 +13,7 @@
--ink-dim: rgb(170, 164, 150);
--up: rgb(126, 196, 120);
--down: rgb(222, 120, 104);
+ --focus: rgb(120, 98, 58); /* keyboard focus fill: >= 3:1 against --navy-deep (WCAG 2.4.13) */
}
.root {
@@ -59,8 +60,10 @@
}
Button:hover { border-color: var(--gold); background-color: var(--navy-raised); }
/* Keyboard focus shows as a bright gold edge (navigation with Tab and the arrow keys). */
-Button:focus { border-color: var(--gold-light); }
+Button:focus { border-color: var(--gold-light); background-color: var(--focus); }
Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4); background-color: var(--navy-deep); }
+/* A screen button stays transparent around its disc in every state. */
+.nav-button:disabled, .nav-button:focus { background-color: rgba(0, 0, 0, 0); border-width: 0; }
/* Top bar: the date badge, the national indicators (icon, name, value and change), the round screen buttons, attention
and End Turn. */
@@ -76,7 +79,7 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);
.top-bar Label { padding-top: 0; padding-bottom: 0; margin-top: 0; margin-bottom: 0; }
/* The date badge: a raised, trimmed box at the left, as the promo's "1936 / Turn 1 / January 1936" plate. */
.date-box {
- width: 190px; flex-shrink: 1; min-width: 150px; height: 82px; justify-content: center;
+ flex-shrink: 0; min-width: 150px; height: 82px; /* sized to its widest line ("Turn 120 30 September 1956") */ justify-content: center;
padding: 0 12px 0 18px; margin-right: 10px;
background-color: var(--navy-raised); border-width: 1px; border-color: var(--gold-dim); border-radius: 2px;
}
@@ -109,14 +112,17 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);
}
.nav-button:hover { background-color: rgba(0, 0, 0, 0); color: var(--ink); }
.nav-button:hover .nav-disc { border-color: var(--gold); background-color: var(--navy-raised); }
-.nav-button:focus { color: var(--ink); }
-.nav-button:focus .nav-disc { border-color: var(--gold-light); }
-/* The open screen's button (ScreenNav). */
+/* Two separate signals: "open" (ScreenNav's nav-open) is a gold-light rim on a raised disc with a gold caption; keyboard
+ focus is an ink rim on the warm focus fill, which a pointer click clears (ScreenNav blurs the button). */
.nav-open { color: var(--gold-light); }
.nav-open .nav-disc { border-color: var(--gold-light); background-color: var(--navy-raised); }
+.nav-button:focus { color: var(--ink); }
+.nav-button:focus .nav-disc { border-color: var(--ink); background-color: var(--focus); }
/* A window too narrow for the icons (HudPresenter sets top-bar--compact below HudPresenter.CompactBelowPx): the meters
drop their icons and tighten, as the bar was before the icons. */
.top-bar--compact .meter-icon { display: none; }
+.top-bar--compact .date-box { padding: 0 10px 0 12px; margin-right: 6px; }
+.top-bar--compact .date-row Label { font-size: 16px; margin-right: 6px; }
.top-bar--compact .meter { padding: 0 8px; }
.top-bar--compact .nav-button { width: 62px; }
.top-bar--compact .nav-disc { left: 6px; }