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.

wip(ui): top bar/stage layout, cabinet list body, map tests, item 3 tests (red, stubs)

eyalgolan2e5faff026a3
diff --git a/unity/Assets/Scripts/Map/CameraRig.cs b/unity/Assets/Scripts/Map/CameraRig.cs
index 84fee9cab..f718ab7d7 100644
--- a/unity/Assets/Scripts/Map/CameraRig.cs
+++ b/unity/Assets/Scripts/Map/CameraRig.cs
@@ -11,6 +11,7 @@ namespace Statecraft.Game.Map
{
public const float Pitch = 50f, Fov = 35f;
public Vector3 LookAt; public float Distance = 120000f; public float Yaw;
+ public float TopInset;
DepthOfField _dof;
/// <summary>The global post-processing volume Init creates. Its runtime VolumeProfile must be owned by the caller
@@ -20,6 +21,10 @@ namespace Statecraft.Game.Map
public void Init()
{
var cam = GetComponent<Camera>(); cam.fieldOfView = Fov; cam.farClipPlane = 1_000_000f; cam.nearClipPlane = 50f;
+ // Beyond the terrain the view shows open sea, not a grey void (playtest 1, item 9). At this pitch the top of the
+ // view still looks down, so the sky never shows and a solid clear in the sea biome colour is the whole fix.
+ cam.clearFlags = CameraClearFlags.SolidColor;
+ cam.backgroundColor = MapBuilder.BiomeColours["sea"];
cam.GetUniversalAdditionalCameraData().renderPostProcessing = true;
var vol = new GameObject("PostFX").AddComponent<Volume>(); vol.isGlobal = true;
vol.profile = ScriptableObject.CreateInstance<VolumeProfile>();
diff --git a/unity/Assets/Scripts/UI/TopBarInset.cs b/unity/Assets/Scripts/UI/TopBarInset.cs
new file mode 100644
index 000000000..e4ee73a11
--- /dev/null
+++ b/unity/Assets/Scripts/UI/TopBarInset.cs
@@ -0,0 +1,12 @@
+#nullable enable
+using Statecraft.Game.Map;
+using UnityEngine.UIElements;
+
+namespace Statecraft.Game.UI
+{
+ public sealed class TopBarInset : System.IDisposable
+ {
+ public TopBarInset(VisualElement root, CameraRig rig) { }
+ public void Dispose() { }
+ }
+}
diff --git a/unity/Assets/Scripts/UI/TopBarInset.cs.meta b/unity/Assets/Scripts/UI/TopBarInset.cs.meta
new file mode 100644
index 000000000..247c5c175
--- /dev/null
+++ b/unity/Assets/Scripts/UI/TopBarInset.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: ac698e5a8ade54e929ffd380c0895747
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/SettlementTests.cs b/unity/Assets/Tests/EditMode/SettlementTests.cs
index e9461b0ea..abbb1c73a 100644
--- a/unity/Assets/Tests/EditMode/SettlementTests.cs
+++ b/unity/Assets/Tests/EditMode/SettlementTests.cs
@@ -160,6 +160,81 @@ public class MapSceneTests
Assert.AreEqual(1, Object.FindObjectsByType<CameraRig>(FindObjectsSortMode.None).Length, "camera rigs");
}
+ /// <summary>Playtest 1, item 9: beyond the terrain the view shows sea, not a grey void. The diorama pitch never shows
+ /// the sky, so the camera clears to the sea biome colour.</summary>
+ [Test]
+ public void TheCameraClearsToTheSeaColour()
+ {
+ var map = NewMap(); map.Build();
+ var cam = Camera.main;
+ Assert.AreEqual(CameraClearFlags.SolidColor, cam.clearFlags);
+ Assert.AreEqual(MapBuilder.BiomeColours["sea"], cam.backgroundColor);
+ Assert.Less(CameraRig.Pitch - CameraRig.Fov / 2, 90f);
+ Assert.Greater(CameraRig.Pitch - CameraRig.Fov / 2, 0f, "the top of the view looks down: no sky to show");
+ }
+
+ /// <summary>Playtest 1, item 3: with the top bar covering the top <c>TopInset</c> of the screen, the look-at point is
+ /// framed in the middle of the map area below the bar, not in the middle of the whole screen.</summary>
+ [Test]
+ public void TheLookAtPointIsCentredBelowTheTopBar()
+ {
+ var map = NewMap(); map.Build();
+ var cam = Camera.main;
+ var rig = cam.GetComponent<CameraRig>();
+ Assert.AreEqual(0.5f, cam.WorldToViewportPoint(rig.LookAt).y, 1e-3f, "no inset: the screen's centre");
+ rig.TopInset = 0.1f; rig.Apply();
+ var v = cam.WorldToViewportPoint(rig.LookAt);
+ Assert.AreEqual(0.45f, v.y, 1e-3f, "the middle of the 90% below the bar (viewport y is up)");
+ Assert.AreEqual(0.5f, v.x, 1e-3f);
+ rig.TopInset = 0f; rig.Apply();
+ Assert.AreEqual(0.5f, cam.WorldToViewportPoint(rig.LookAt).y, 1e-3f, "the inset is undone");
+ }
+
+ /// <summary>Playtest 1, item 10: the selected hex gets an outline in the highlight colour, draped on the terrain like
+ /// the hex overlay (HexMath corners), drawn with the editor-made overlay material (no runtime material).</summary>
+ [Test]
+ public void SelectingAHexOutlinesItOnTheTerrain()
+ {
+ var map = NewMap(); map.Build();
+ var overlay = Load(OverlayMatPath);
+ float size = map.Hexes.hex_size_km * 1000f;
+ bool Inland(HexJson h) // whole hex on the terrain, so no corner is clamped to the map's edge
+ {
+ var w = map.Frame.ToWorld(h.lon, h.lat, 0);
+ return w.x > size && w.x < map.Frame.WidthM - size && w.z > size && w.z < map.Frame.LengthM - size;
+ }
+ var land = System.Array.FindAll(map.Hexes.hexes, h => h.land && h.biome == "hills" && Inland(h));
+ Assert.Greater(land.Length, 2);
+ foreach (var h in new[] { land[0], land[land.Length / 2] })
+ {
+ map.Select(h.q, h.r);
+ var sel = map.Terrain.transform.Find(MapScene.SelectionName);
+ Assert.IsNotNull(sel, "the selection outline exists");
+ var mr = sel.GetComponent<MeshRenderer>();
+ Assert.IsTrue(mr.enabled, "the outline is shown");
+ Assert.AreSame(overlay, mr.sharedMaterial, "the editor-made overlay material, not an instance");
+ var block = new MaterialPropertyBlock(); mr.GetPropertyBlock(block);
+ var colour = block.GetColor(Shader.PropertyToID("_BaseColor"));
+ Assert.Less(Mathf.Abs(colour.r - MapScene.SelectionColour.r) + Mathf.Abs(colour.g - MapScene.SelectionColour.g)
+ + Mathf.Abs(colour.b - MapScene.SelectionColour.b), 1e-3f, $"highlight colour, got {colour}");
+ var verts = sel.GetComponent<MeshFilter>().sharedMesh.vertices;
+ var centre = map.Frame.ToWorld(h.lon, h.lat, 0);
+ var c2 = new Vector2(centre.x, centre.z);
+ for (int i = 0; i < 6; i++)
+ {
+ var corner = HexMath.Corner(c2, size, i);
+ Assert.IsTrue(System.Array.Exists(verts, v => (new Vector2(v.x, v.z) - corner).magnitude < 1f), $"corner {i} of ({h.q}, {h.r})");
+ }
+ foreach (var v in verts)
+ {
+ Assert.LessOrEqual((new Vector2(v.x, v.z) - c2).magnitude, size + 1f, "inside the hex");
+ Assert.Greater(v.y, map.Terrain.SampleHeight(v) + MapBuilder.OverlayClearanceM - 1f, "draped above the terrain");
+ }
+ }
+ map.Select(-9999, -9999);
+ Assert.IsFalse(map.Terrain.transform.Find(MapScene.SelectionName).GetComponent<MeshRenderer>().enabled, "no hex there: no outline");
+ }
+
[Test]
public void BuildWithoutMaterialsNamesTheMissingField()
{
diff --git a/unity/Assets/Tests/EditMode/UiLayoutTests.cs b/unity/Assets/Tests/EditMode/UiLayoutTests.cs
index 28ca04733..8c1d73085 100644
--- a/unity/Assets/Tests/EditMode/UiLayoutTests.cs
+++ b/unity/Assets/Tests/EditMode/UiLayoutTests.cs
@@ -1,4 +1,8 @@
+#nullable enable
using System.Collections;
+using System.Linq;
+using Statecraft.Game;
+using Statecraft.Sim;
using NUnit.Framework;
using Statecraft.Game.UI;
using UnityEditor;
@@ -17,7 +21,7 @@ public class UiLayoutTests
[UnityTest]
public IEnumerator NothingOverlapsAt1080pAnd720p()
{
- foreach (var (w, h) in new[] { (1920, 1080), (1280, 720), (1280, 1024) })
+ foreach (var (w, h) in new[] { (1920, 1080), (1280, 720), (1280, 1024), (1531, 784), (2560, 1080), (1920, 900) })
{
var rt = new RenderTexture(w, h, 0);
var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));
@@ -61,4 +65,125 @@ public class UiLayoutTests
finally { Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt); }
}
}
+
+ static readonly Content C = ContentLoader.Load(Paths.Content);
+ 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
+ /// (1531x784, about 1.95) and 21:9.</summary>
+ static readonly (int w, int h)[] Windows = { (1531, 784), (2560, 1080), (1920, 1080), (1280, 1024), (1920, 900) };
+
+ /// <summary>The real Game.uxml on the real PanelSettings (and so the real theme), rendered at <paramref name="w"/> x
+ /// <paramref name="h"/>, with the HUD and the Cabinet screen filled from the opening of the historical game.</summary>
+ static IEnumerator Filled(int w, int h, string? screen, System.Action<VisualElement> check)
+ {
+ var rt = new RenderTexture(w, h, 0);
+ var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));
+ ps.targetTexture = rt;
+ var go = new GameObject("LayoutTest");
+ 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 bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);
+ var hud = new HudPresenter(root, bridge, Text);
+ var nav = new ScreenNav(root);
+ var cabinet = new CabinetPresenter(root, bridge, Text, null);
+ nav.Open(screen);
+ try
+ {
+ for (int i = 0; i < 20 && (float.IsNaN(root.Q("minimap").worldBound.width) || root.Q("year").worldBound.height <= 0); i++) yield return null;
+ yield return null; // one more pass, so text measured after the first layout settles
+ check(root);
+ }
+ finally
+ {
+ cabinet.Dispose(); nav.Dispose(); hud.Dispose();
+ Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt);
+ }
+ }
+
+ static bool Inside(Rect inner, Rect outer) =>
+ inner.xMin >= outer.xMin - 0.5f && inner.xMax <= outer.xMax + 0.5f && inner.yMin >= outer.yMin - 0.5f && inner.yMax <= outer.yMax + 0.5f;
+
+ /// <summary>Playtest 1, item 1: every top-bar label (the year, the date, the security level) lies inside the bar,
+ /// clear of the window's top edge, and nothing else starts above the bar's bottom edge.</summary>
+ [UnityTest]
+ public IEnumerator TheTopBarHoldsItsContentInWideAndShortWindows()
+ {
+ foreach (var (w, h) in Windows)
+ yield return Filled(w, h, null, root =>
+ {
+ var bar = root.Q("top-bar").worldBound;
+ Assert.GreaterOrEqual(bar.yMin, -0.5f, $"{w}x{h}: the top bar starts above the window");
+ var labels = root.Q("top-bar").Query<Label>().ToList().Where(l => !string.IsNullOrEmpty(l.text)).ToList();
+ Assert.Greater(labels.Count, 10, "the HUD filled the bar");
+ foreach (var l in labels)
+ {
+ Assert.IsTrue(Inside(l.worldBound, bar), $"{w}x{h}: '{l.text}' {l.worldBound} is outside the top bar {bar}");
+ Assert.GreaterOrEqual(l.worldBound.yMin, bar.yMin + 2f, $"{w}x{h}: '{l.text}' sits flush against the top edge");
+ }
+ foreach (var name in new[] { "event-card", "screen-cabinet", "screen-budget", "screen-defence", "screen-why", "toast" })
+ if (root.Q(name).resolvedStyle.display != DisplayStyle.None)
+ Assert.GreaterOrEqual(root.Q(name).worldBound.yMin, bar.yMax - 0.5f, $"{w}x{h}: {name} starts under the top bar");
+ });
+ }
+
+ /// <summary>Playtest 1, item 2: the Cabinet lineup is a list inside the screen panel, below its header, filling the
+ /// panel; rows that do not fit scroll instead of spilling over the top bar.</summary>
+ [UnityTest]
+ public IEnumerator TheCabinetListStaysInsideItsPanel()
+ {
+ foreach (var (w, h) in Windows)
+ yield return Filled(w, h, "cabinet", root =>
+ {
+ var screen = root.Q("screen-cabinet").worldBound;
+ var header = root.Q("screen-cabinet").Q(className: "screen-header").worldBound;
+ var list = root.Q<ScrollView>("cabinet-seats");
+ var view = list.contentViewport.worldBound;
+ Assert.IsTrue(Inside(header, screen), $"{w}x{h}: the header {header} is outside the screen {screen}");
+ Assert.IsTrue(Inside(view, screen), $"{w}x{h}: the list {view} is outside the screen {screen}");
+ Assert.GreaterOrEqual(view.yMin, header.yMax - 0.5f, $"{w}x{h}: the list starts above the screen header");
+ Assert.GreaterOrEqual(view.yMax, root.Q("cabinet-status").worldBound.yMin - 16f, $"{w}x{h}: the list leaves the panel empty below it");
+ var rows = list.Query<Button>(className: "seat").ToList();
+ Assert.Greater(rows.Count, 5, "the lineup was rendered");
+ foreach (var r in rows)
+ {
+ Assert.GreaterOrEqual(r.worldBound.yMin, view.yMin - 0.5f, $"{w}x{h}: {r.name} sits above the list");
+ Assert.GreaterOrEqual(r.worldBound.xMin, screen.xMin - 0.5f, $"{w}x{h}: {r.name} leaves the panel on the left");
+ Assert.LessOrEqual(r.worldBound.xMax, screen.xMax + 0.5f, $"{w}x{h}: {r.name} leaves the panel on the right");
+ if (r.worldBound.yMax > view.yMax + 0.5f)
+ Assert.Greater(list.verticalScroller.highValue, 0f, $"{w}x{h}: {r.name} runs past the panel and the list does not scroll");
+ }
+ });
+ }
+
+ /// <summary>Playtest 1, item 3: the camera learns how much of the window the top bar covers, at every window shape,
+ /// so the map is framed below it; disposing gives the whole screen back.</summary>
+ [UnityTest]
+ public IEnumerator TheCameraIsToldWhatTheTopBarCovers()
+ {
+ var camGo = new GameObject("InsetCamera");
+ camGo.AddComponent<Camera>();
+ var rig = camGo.AddComponent<Statecraft.Game.Map.CameraRig>();
+ try
+ {
+ foreach (var (w, h) in Windows)
+ {
+ yield return Filled(w, h, null, root =>
+ {
+ var inset = new TopBarInset(root, rig);
+ var bar = root.Q("top-bar").worldBound.height;
+ var screen = root.panel.visualTree.worldBound.height;
+ Assert.Greater(bar, 90f, $"{w}x{h}: the bar is laid out");
+ Assert.AreEqual(bar / screen, rig.TopInset, 1e-3f, $"{w}x{h}: the inset is the bar's share of the window");
+ inset.Dispose();
+ Assert.AreEqual(0f, rig.TopInset, $"{w}x{h}: disposing gives the screen back");
+ });
+ }
+ }
+ finally { Object.DestroyImmediate(camGo); }
+ }
}
diff --git a/unity/Assets/UI/Game.uxml b/unity/Assets/UI/Game.uxml
index a2f55d7f2..db23846c2 100644
--- a/unity/Assets/UI/Game.uxml
+++ b/unity/Assets/UI/Game.uxml
@@ -3,8 +3,7 @@
<ui:VisualElement name="root" class="root" picking-mode="Ignore">
<ui:VisualElement name="top-bar" class="panel top-bar">
<ui:VisualElement class="date-box">
- <ui:Label name="year" class="year" />
- <ui:Label name="turn" class="muted" />
+ <ui:VisualElement class="year-row"><ui:Label name="year" class="year" /><ui:Label name="turn" class="muted" /></ui:VisualElement>
<ui:Label name="date" class="muted" />
</ui:VisualElement>
<ui:VisualElement name="meters" class="meters">
@@ -46,6 +45,9 @@
<ui:Button name="end-turn" text="End Turn" class="end-turn" />
</ui:VisualElement>
</ui:VisualElement>
+ <!-- Everything below the top bar. The bar's height follows its content; the panels are placed in the stage, so none
+ of them can start under the bar whatever the window's shape (playtest 1, item 1). -->
+ <ui:VisualElement name="stage" class="stage" picking-mode="Ignore">
<ui:VisualElement name="event-card" class="panel event-card">
<ui:VisualElement name="event-image" class="event-image" />
<ui:Label name="event-draft" text="DRAFT" class="draft" />
@@ -73,7 +75,7 @@
</ui:VisualElement>
<ui:VisualElement name="screen-cabinet" class="panel screen">
<ui:VisualElement class="screen-header"><ui:Label name="cabinet-government" class="title" /><ui:Label name="cabinet-coalition" class="muted" /></ui:VisualElement>
- <ui:VisualElement class="row grow">
+ <ui:VisualElement class="screen-body">
<ui:ScrollView name="cabinet-seats" class="grow" />
<ui:VisualElement name="cabinet-pick" class="grow">
<ui:Label name="cabinet-pick-title" class="meter-name" />
@@ -122,5 +124,6 @@
<ui:ScrollView name="why-rows" class="grow" />
</ui:VisualElement>
<ui:Label name="toast" class="panel toast" />
+ </ui:VisualElement>
</ui:VisualElement>
</ui:UXML>
diff --git a/unity/Assets/UI/Statecraft.uss b/unity/Assets/UI/Statecraft.uss
index 6dd50f90e..11d999d66 100644
--- a/unity/Assets/UI/Statecraft.uss
+++ b/unity/Assets/UI/Statecraft.uss
@@ -51,13 +51,17 @@ Button:hover { border-color: var(--gold); }
Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4); }
/* Top bar: date box, national indicators with value and change, screen buttons, attention and End turn. */
+/* Its height follows its content (at least 96px), so no label leaves it in a wide or short window; the stage below
+ starts where it ends. */
.top-bar {
- position: absolute; left: 0; right: 0; top: 0; height: 96px;
+ flex-shrink: 0; min-height: 96px;
flex-direction: row; align-items: center;
- border-width: 0 0 1px 0; border-radius: 0; padding: 0 16px;
+ border-width: 0 0 1px 0; border-radius: 0; padding: 6px 16px;
}
+.stage { flex-grow: 1; }
.date-box { width: 190px; flex-shrink: 1; min-width: 150px; border-right-width: 1px; border-color: var(--gold-dim); padding-right: 12px; }
-.year { -unity-font-definition: url("project://database/Assets/UI/Fonts/EBGaramond-SemiBold.ttf"); font-size: 36px; }
+.year-row { flex-direction: row; align-items: flex-end; justify-content: space-between; }
+.year { -unity-font-definition: url("project://database/Assets/UI/Fonts/EBGaramond-SemiBold.ttf"); font-size: 32px; }
.meters { flex-direction: row; flex-grow: 1; }
.meter { min-width: 130px; flex-shrink: 1; padding: 0 8px; border-right-width: 1px; border-color: var(--gold-dim); }
.meter-name { font-size: 18px; color: var(--ink-dim); white-space: normal; } /* wraps in a narrow meter */
@@ -70,7 +74,7 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);
.end-turn { -unity-font-definition: url("project://database/Assets/UI/Fonts/EBGaramond-SemiBold.ttf"); font-size: 22px; border-color: var(--gold); }
/* Event card at the right, info panel at the bottom-left, minimap at the bottom-right. */
-.event-card { position: absolute; right: 16px; top: 112px; width: 420px; max-height: 560px; overflow: hidden; }
+.event-card { position: absolute; right: 16px; top: 16px; width: 420px; max-height: 560px; overflow: hidden; }
.event-image { height: 200px; margin-bottom: 8px; -unity-background-scale-mode: scale-and-crop; background-color: var(--navy-deep); }
.event-title { -unity-text-align: middle-center; margin-bottom: 6px; }
.event-text { white-space: normal; margin-bottom: 8px; }
@@ -81,14 +85,16 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);
.minimap-view { position: absolute; border-color: var(--ink); border-width: 1px; }
/* Full screens (Cabinet, Budget, Defence, Why) open in the centre, one at a time. */
-.screen { position: absolute; left: 552px; right: 452px; top: 112px; bottom: 16px; }
+.screen { position: absolute; left: 552px; right: 452px; top: 16px; bottom: 16px; }
.screen-header { flex-direction: row; justify-content: space-between; align-items: center; border-bottom-width: 1px; border-color: var(--gold-dim); margin-bottom: 8px; }
.row { flex-direction: row; align-items: center; }
+/* A screen's lists fill the space under its header and scroll inside it (playtest 1, item 2). */
+.screen-body { flex-direction: row; align-items: stretch; flex-grow: 1; flex-shrink: 1; flex-basis: 0; min-height: 0; }
.rung { flex-direction: row; padding: 4px 8px; border-left-width: 3px; border-color: rgba(0, 0, 0, 0); }
.rung-current { border-color: var(--gold); background-color: rgba(196, 160, 92, 0.15); }
.grow { flex-grow: 1; }
-.toast { position: absolute; left: 30%; right: 30%; top: 112px; -unity-text-align: middle-center; white-space: normal; }
+.toast { position: absolute; left: 30%; right: 30%; top: 16px; -unity-text-align: middle-center; white-space: normal; }
.why-group { -unity-font-definition: url("project://database/Assets/UI/Fonts/EBGaramond-SemiBold.ttf"); color: var(--gold); margin-top: 8px; }
.why-line { white-space: normal; padding-left: 12px; }