A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Top bar: the camera's top inset counts the safe area above the bar (Fixes #2572) (#2575)
Eyal Golanac1e5a79ba7e
diff --git a/changelog.d/2572-top-bar-inset.md b/changelog.d/2572-top-bar-inset.mdnew file mode 100644index 000000000..fd01bcd39--- /dev/null+++ b/changelog.d/2572-top-bar-inset.md@@ -0,0 +1,4 @@+### Fixed++- On phones with a top safe area, the map is now framed below both the notch and the top bar instead of partly behind+ the bar (#2572).diff --git a/unity/Assets/Scripts/UI/TopBarInset.cs b/unity/Assets/Scripts/UI/TopBarInset.csindex f07530373..cfe5a2f78 100644--- a/unity/Assets/Scripts/UI/TopBarInset.cs+++ b/unity/Assets/Scripts/UI/TopBarInset.cs@@ -1,4 +1,5 @@#nullable enable+using System.Collections.Generic;using Statecraft.Game.Map;using UnityEngine;using UnityEngine.UIElements;@@ -6,18 +7,26 @@ using UnityEngine.UIElements;namespace Statecraft.Game.UI{/// <summary>Tells the camera what share of the window the top bar covers (CameraRig.TopInset), so the map is framed in- /// the area below the bar and labels near the top of the view are not hidden under it (playtest 1, item 3). Follows+ /// the area below the bar and labels near the top of the view are not hidden under it (playtest 1, item 3). The share+ /// runs from the window's top to the bar's bottom edge, so a phone's safe area above the bar counts too (#2572). Follows/// the bar and the window as they change size; disposing gives the camera the whole screen back.</summary>public sealed class TopBarInset : System.IDisposable{readonly VisualElement root, bar; readonly CameraRig rig;+ /// <summary>The bar and every element up to <see cref="root"/>: SafeAreaFit pads the root, which moves #root (and the+ /// bar inside it) without changing the root's or the bar's own layout, so their events alone would miss it (#2572).</summary>+ readonly List<VisualElement> watched = new List<VisualElement>();public TopBarInset(VisualElement root, CameraRig rig){this.root = root; this.rig = rig;bar = root.Q<VisualElement>("top-bar");- bar.RegisterCallback<GeometryChangedEvent>(OnGeometry);- root.RegisterCallback<GeometryChangedEvent>(OnGeometry);+ for (var e = bar; e != null; e = e.parent)+ {+ watched.Add(e);+ if (e == root) break;+ }+ foreach (var e in watched) e.RegisterCallback<GeometryChangedEvent>(OnGeometry);Update();}@@ -27,16 +36,19 @@ namespace Statecraft.Game.UIpublic void Update(){if (rig == null) return; // the camera went first (scene teardown)- var window = root.panel?.visualTree.worldBound.height ?? float.NaN;+ var panel = root.panel?.visualTree;+ var window = panel?.worldBound.height ?? float.NaN;var height = bar.worldBound.height;- rig.TopInset = window > 0 && !float.IsNaN(height) && height > 0 ? Mathf.Clamp01(height / window) : 0f;+ var covered = bar.worldBound.yMax - (panel?.worldBound.yMin ?? float.NaN);+ rig.TopInset = window > 0 && !float.IsNaN(height) && height > 0 && !float.IsNaN(covered) && covered > 0+ ? Mathf.Clamp01(covered / window) : 0f;rig.ApplyProjection();}public void Dispose(){- bar.UnregisterCallback<GeometryChangedEvent>(OnGeometry);- root.UnregisterCallback<GeometryChangedEvent>(OnGeometry);+ foreach (var e in watched) e.UnregisterCallback<GeometryChangedEvent>(OnGeometry);+ watched.Clear();if (rig == null) return;rig.TopInset = 0f;rig.ApplyProjection();diff --git a/unity/Assets/Tests/EditMode/MapLabelTests.cs b/unity/Assets/Tests/EditMode/MapLabelTests.csindex ce1999cd1..75bd607f7 100644--- a/unity/Assets/Tests/EditMode/MapLabelTests.cs+++ b/unity/Assets/Tests/EditMode/MapLabelTests.cs@@ -577,6 +577,45 @@ public class MapLabelTestslabels.Refresh(force: true); yield return null;}+ /// <summary>#2572: the camera inset includes the safe area above the top bar, while a zero inset keeps the top bar's+ /// old share of the window. Built in GameRoot's order (TopBarInset, then SafeAreaFit) and never told to update: the+ /// padding SafeAreaFit puts on the root must reach the camera through the layout's own events.</summary>+ [UnityTest]+ public IEnumerator TheCameraInsetIncludesTheSafeAreaAboveTheTopBar()+ {+ var hd = new HdPanel(cam);+ var safeInsets = (l: 0f, r: 0f, t: 0f, b: 0f);+ var inset = new TopBarInset(hd.Root, rig);+ var safeArea = new SafeAreaFit(hd.Root, () => safeInsets);+ try+ {+ var bar = hd.Root.Q<VisualElement>("top-bar");+ for (int i = 0; i < 20 && (float.IsNaN(bar.worldBound.height) || bar.worldBound.height <= 0); i++) yield return null;+ yield return null; yield return null;+ var panelBounds = hd.Root.panel.visualTree.worldBound;+ var window = panelBounds.height;+ Assert.AreEqual(panelBounds.yMin, bar.worldBound.yMin, 1e-3f, "with no safe area the bar starts at the panel top");+ Assert.AreEqual(bar.worldBound.height / window, rig.TopInset, 1e-3f,+ "with no safe-area inset the bar starts at the panel top, so its bottom edge is its height");++ const float safeTop = 47f;+ safeInsets = (0f, 0f, safeTop, 0f); // what SafeAreaFit's per-frame poll reads next+ safeArea.Apply(0f, 0f, safeTop, 0f); // and what it then writes: the root's padding, as on a notched phone+ var expected = 0f;+ for (int i = 0; i < 20; i++)+ {+ EditorApplication.QueuePlayerLoopUpdate();+ yield return null;+ expected = (bar.worldBound.yMax - panelBounds.yMin) / window;+ if (bar.worldBound.yMin - panelBounds.yMin >= safeTop - 1f && Mathf.Abs(rig.TopInset - expected) < 1e-3f) break;+ }+ Assert.AreEqual(safeTop, bar.worldBound.yMin - panelBounds.yMin, 1f, "precondition: the padding moved the bar down");+ Assert.AreEqual((safeTop + bar.worldBound.height) / window, rig.TopInset, 1e-3f,+ "the camera leaves room for the safe area and the top bar, with no call to Update");+ }+ finally { safeArea.Dispose(); inset.Dispose(); hd.Dispose(); }+ }+/// <summary>Independent of the presenter's own projection call: the Tel Aviv pill sits centred over its kit and just/// above it (issue #214): its bottom at least LiftPx and at most 30 px above the top of the kit's drawn buildings (every/// vertex projected with Camera.WorldToScreenPoint and the panel's scale; the presenter's columns are a little looser). Also on a 2880x1620 target (panel scale 1.5, Retina-like) with the game's top-bar