A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
ui: promo-sized meters when the bar has room; New Game caption fits
eyalgolana83a050d34b9
diff --git a/unity/Assets/Scripts/UI/HudPresenter.cs b/unity/Assets/Scripts/UI/HudPresenter.csindex 2ed9067e7..cb11248a7 100644--- a/unity/Assets/Scripts/UI/HudPresenter.cs+++ b/unity/Assets/Scripts/UI/HudPresenter.cs@@ -1,4 +1,5 @@#nullable enable+using System.Linq;using UnityEngine.UIElements;namespace Statecraft.Game.UI@@ -8,7 +9,8 @@ namespace Statecraft.Game.UI{readonly VisualElement root; readonly SimBridge bridge; readonly UiText text;readonly Button endTurn; readonly System.Action onEndTurn;- readonly VisualElement bar, hudRoot;+ readonly VisualElement bar, hudRoot, nav;+ readonly System.Collections.Generic.List<VisualElement> meters;// Three tiers by the bar's logical width (1080p reference, match 0.5: logical width = sqrt(aspect x 1920 x 1080)).// Each threshold sits well clear of a common window shape, so a small window change does not flip a tier:@@ -31,6 +33,17 @@ namespace Statecraft.Game.UI/// below (Statecraft.uss .hud-two-row).</summary>public const float TwoRowBelowPx = 1500f;public const string TwoRowClass = "hud-two-row";+ /// <summary>On the one-row bar, when the meters leave enough room, the bar gets RoomyClass: promo-sized icons (38 px)+ /// and values (25 px) (Statecraft.uss .top-bar--roomy). It turns on only when the measured spare room covers the+ /// predicted growth (RoomyGrowth) plus SpareShare, and off when the spare room falls below SpareShare, so it never+ /// flips back and forth.</summary>+ public const string RoomyClass = "top-bar--roomy";+ /// <summary>The share of the meters' width kept free after them (room for the browser's text measuring wider than the+ /// editor's; PromoHudTests.EveryWindowShapeFitsTheBarAfterATurn asserts it).</summary>+ public const float SpareShare = 0.05f;++ /// <summary>The meters' predicted growth when roomy: six icons 30 -> 38 px, values 22 -> 25 px (about 14% wider).</summary>+ public static float RoomyGrowth(float valueWidths) => 6 * 8f + 0.14f * valueWidths;public HudPresenter(VisualElement root, SimBridge bridge, UiText text){@@ -42,6 +55,9 @@ namespace Statecraft.Game.UIbar = root.Q<VisualElement>("top-bar");hudRoot = root.Q<VisualElement>("root");bar.RegisterCallback<GeometryChangedEvent>(OnBarGeometry);+ meters = root.Q<VisualElement>("meters").Children().ToList();+ nav = root.Q<VisualElement>("nav");+ foreach (var m in meters) m.RegisterCallback<GeometryChangedEvent>(OnMeterGeometry);SetCompact(bar.layout.width); // the bar may already be laid out (the loading screen shows it) and never resizebridge.Changed += Render;Render();@@ -52,12 +68,36 @@ namespace Statecraft.Game.UI{bridge.Changed -= Render; endTurn.clicked -= onEndTurn; endTurn.generateVisualContent -= PaintHourglass;bar.UnregisterCallback<GeometryChangedEvent>(OnBarGeometry);+ foreach (var m in meters) m.UnregisterCallback<GeometryChangedEvent>(OnMeterGeometry);+ bar.RemoveFromClassList(RoomyClass);bar.RemoveFromClassList(CompactClass); hudRoot.RemoveFromClassList(NarrowClass); hudRoot.RemoveFromClassList(TwoRowClass);}// The bar's width does not depend on the class, so this settles in one pass.void OnBarGeometry(GeometryChangedEvent e) => SetCompact(e.newRect.width);+ void OnMeterGeometry(GeometryChangedEvent e) => UpdateRoomy();++ /// <summary>Roomy only on the one-row bar with icons; see RoomyClass.</summary>+ void UpdateRoomy()+ {+ if (meters.Count == 0) return;+ var oneRow = !hudRoot.ClassListContains(NarrowClass) && !bar.ClassListContains(CompactClass);+ // Panel coordinates (logical px), so the meters and the screen buttons compare directly.+ float first = meters[0].worldBound.xMin, last = meters[meters.Count - 1].worldBound.xMax, limit = nav.worldBound.xMin;+ if (!oneRow || float.IsNaN(first) || float.IsNaN(limit)) { bar.RemoveFromClassList(RoomyClass); return; }+ var used = last - first; var spare = limit - last;+ if (bar.ClassListContains(RoomyClass))+ {+ if (spare < SpareShare * used) bar.RemoveFromClassList(RoomyClass);+ }+ else+ {+ var values = meters.Sum(m => m.Q<Label>(className: "meter-value")?.worldBound.width ?? 0f);+ if (spare - RoomyGrowth(values) >= SpareShare * used + 8f) bar.AddToClassList(RoomyClass);+ }+ }+void SetCompact(float width){if (!(width > 0)) return; // NaN before layout: wait for itdiff --git a/unity/Assets/Tests/EditMode/PromoHudTests.cs b/unity/Assets/Tests/EditMode/PromoHudTests.csindex 3e6d3da09..43f04ebe2 100644--- a/unity/Assets/Tests/EditMode/PromoHudTests.cs+++ b/unity/Assets/Tests/EditMode/PromoHudTests.cs@@ -164,6 +164,16 @@ public class PromoHudTestsvar bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);hud = new HudPresenter(root, bridge, Text);bridge.EndTurn(); // real changes on every meter that moved+ for (int i = 0; i < 6; i++) yield return null;+ if (w == 1920 && h == 1080)+ {+ // Review P1: with typical (turn-1) values at 16:9 the bar has room for the promo's sizes.+ Assert.IsTrue(root.Q("top-bar").ClassListContains(HudPresenter.RoomyClass), "1920x1080: roomy with typical values");+ foreach (var icon in root.Q("meters").Query<UiIcon>().ToList())+ Assert.GreaterOrEqual(icon.worldBound.width, 36f, $"1920x1080: the {icon.Glyph} icon is promo-sized");+ foreach (var v in root.Q("meters").Query<Label>(className: "meter-value").ToList())+ Assert.GreaterOrEqual(v.resolvedStyle.fontSize, 24f, $"1920x1080: '{v.text}' is promo-sized");+ }// The widest realistic changes, formatted as HudModel formats them, and the widest date line.foreach (var (name, delta) in new[] { ("treasury", PlayerText.MoneyChange(C, "treasury", -12.4)), ("immigration", Text.Format("hud.delta_people", 12345.0)),("security", "-10%"), ("support", "-10%"), ("standing", "+10%"), ("fx", PlayerText.MoneyChange(C, "fx_reserves", 12.5)) })@@ -175,7 +185,7 @@ public class PromoHudTestsroot.Q<Label>("security-level").text = Text.Format("hud.level", 5);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)));- for (int i = 0; i < 3; i++) yield return null;+ for (int i = 0; i < 6; i++) yield return null; // the roomy bar steps back down if it mustvar bar = root.Q("top-bar"); var barRect = bar.worldBound;var where = $"{w}x{h} (bar {barRect.width:0} px)";Assert.AreEqual(!icons, bar.ClassListContains(HudPresenter.CompactClass), where + ": compact (no icons)");@@ -215,6 +225,12 @@ public class PromoHudTestsvar used = meters[meters.Count - 1].worldBound.xMax - meters[0].worldBound.xMin;var limit = twoRow ? barRect.xMax - 14f : root.Q("nav").worldBound.xMin;Assert.GreaterOrEqual(limit - meters[meters.Count - 1].worldBound.xMax, 0.05f * used, $"{where}: the meters use {used:0} of {limit - meters[0].worldBound.xMin:0} px");+ // Review P2: every screen button's caption fits its button.+ foreach (var nb in root.Q("nav").Query<Button>().ToList())+ {+ var natural = nb.MeasureTextSize(nb.text, 0, VisualElement.MeasureMode.Undefined, 0, VisualElement.MeasureMode.Undefined);+ Assert.LessOrEqual(natural.x, nb.contentRect.width + 0.5f, $"{where}: the caption '{nb.text}' ({natural.x:0} px) overflows its button ({nb.contentRect.width:0} px)");+ }foreach (var e in new[] { root.Q("nav"), root.Q(className: "turn-box"), badge })Assert.LessOrEqual(e.worldBound.yMax, barRect.yMax + 0.5f, $"{where}: {e.name}{string.Join(".", e.GetClasses())} leaves the bar");Assert.GreaterOrEqual(root.Q("right-column").worldBound.yMin, barRect.yMax, where + ": the event column starts under the bar");diff --git a/unity/Assets/UI/Statecraft.uss b/unity/Assets/UI/Statecraft.ussindex 58a79e70a..8c6c7d0dc 100644--- a/unity/Assets/UI/Statecraft.uss+++ b/unity/Assets/UI/Statecraft.uss@@ -94,6 +94,9 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);}/* The icon gives way first in a narrow window (down to 26px), so the caption keeps its line. */.meter-icon { width: 30px; height: 30px; flex-shrink: 1; min-width: 24px; margin-right: 5px; }+/* With room to spare (HudPresenter sets top-bar--roomy on the one-row bar), the promo's sizes. */+.top-bar--roomy .meter-icon { width: 38px; height: 38px; margin-right: 7px; }+.top-bar--roomy .meter-value { font-size: 25px; }.meter-body { flex-shrink: 1; flex-grow: 1; min-width: 0; }.meter-name { font-size: 16px; color: var(--ink); white-space: normal; max-width: 120px; } /* a long caption wraps rather than widen its meter */#security-level { font-size: 14px; color: var(--ink-dim); }@@ -111,6 +114,9 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);position: absolute; top: 2px; left: 5px; width: 50px; height: 50px; padding: 11px;border-radius: 25px; border-width: 1.5px; border-color: var(--gold-dim); background-color: var(--navy-deep);}+/* "New Game" is the longest caption: its button is wider, its disc centred (PromoHudTests: every caption fits). */+#new-game { width: 66px; }+#new-game .nav-disc { left: 8px; }.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); }/* Two separate signals: "open" (ScreenNav's nav-open) is a gold-light rim on a raised disc with a gold caption; keyboard@@ -138,7 +144,7 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);.hud-two-row .top-bar { height: 216px; padding-top: 6px; padding-bottom: 4px; flex-wrap: wrap; justify-content: space-between; align-content: space-around; }.hud-two-row .meters { width: 78%; flex-grow: 1; }.hud-two-row .right-column, .hud-two-row .screen, .hud-two-row .toast { top: 232px; }-.turn-box { width: 156px; flex-shrink: 1; min-width: 150px; align-items: flex-end; }+.turn-box { width: 150px; flex-shrink: 1; min-width: 150px; align-items: flex-end; }#attention { font-size: 16px; }.end-turn {-unity-font-definition: url("project://database/Assets/UI/Fonts/EBGaramond-SemiBold.ttf"); font-size: 22px;