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: windows scale up and fade in when they open, scale down when they close (Fixes #1106) (#1175)

Beri3e89768f143e
diff --git a/changelog.d/ui-window-transitions-1106.md b/changelog.d/ui-window-transitions-1106.md
new file mode 100644
index 000000000..07cf85591
--- /dev/null
+++ b/changelog.d/ui-window-transitions-1106.md
@@ -0,0 +1,5 @@
+- Windows open and close with motion (#1106, play-test of v0.1.2): every side-menu screen and the decision card scale
+ up and fade in over 0.5 s when they open and scale down over 0.3 s when they close. USS transitions only, switched by
+ ScreenNav.Show; a closing window is out of reach while it shrinks and leaves the layout when the shrink ends, also when
+ it is closed while it still grows. Play mode only: an EditMode panel does not tick, so the layout tests still see
+ windows at full size.
diff --git a/unity/Assets/Scripts/UI/EventCardPresenter.cs b/unity/Assets/Scripts/UI/EventCardPresenter.cs
index 752db6175..e4e883e69 100644
--- a/unity/Assets/Scripts/UI/EventCardPresenter.cs
+++ b/unity/Assets/Scripts/UI/EventCardPresenter.cs
@@ -198,7 +198,7 @@ namespace Statecraft.Game.UI
if (Shown == null) collapsedFor = null;
if (bridge.Session != session) { session = bridge.Session; collapsedFor = null; focusedFor = null; }
var shown = !hiddenInCapture && Shown != null && !Collapsed;
- card.style.display = shown ? DisplayStyle.Flex : DisplayStyle.None;
+ ScreenNav.Show(card, shown); // #1106: grows in, shrinks away
card.SetEnabled(shown); // a child of an undisplayed element can still take the focus (#400): a collapsed card's options must not
reopen.style.display = !hiddenInCapture && Collapsed ? DisplayStyle.Flex : DisplayStyle.None;
hudRoot.EnableInClassList(OpenClass, shown);
diff --git a/unity/Assets/Scripts/UI/ScreenNav.cs b/unity/Assets/Scripts/UI/ScreenNav.cs
index 50cc00600..077da2010 100644
--- a/unity/Assets/Scripts/UI/ScreenNav.cs
+++ b/unity/Assets/Scripts/UI/ScreenNav.cs
@@ -20,6 +20,9 @@ namespace Statecraft.Game.UI
/// and in the two-row (narrow) HUD, where every screen takes the full width, the minimap column and the info panel
/// too (#210).</summary>
public const string ScreenOpenClass = "screen-open";
+ /// <summary>#1106: the classes on a window (a side-menu screen, the decision card) while it opens and while it
+ /// closes (Statecraft.uss holds the transitions).</summary>
+ public const string OpeningClass = "window-opening", ClosingClass = "window-closing";
/// <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
@@ -74,9 +77,78 @@ namespace Statecraft.Game.UI
/// rendering while its screen is hidden catches up then).</summary>
public static void OnOpened(VisualElement screen, System.Action action) => screen.userData = action;
- /// <summary>True when <paramref name="screen"/> is open (ScreenNav shows it inline; the stylesheet hides it).</summary>
+ /// <summary>True when <paramref name="screen"/> is open (ScreenNav shows it inline; the stylesheet hides it), and not
+ /// shrinking away (<see cref="Show"/>).</summary>
public static bool IsOpen(VisualElement screen) =>
- screen.style.display.keyword == StyleKeyword.Undefined && screen.style.display.value == DisplayStyle.Flex;
+ screen.style.display.keyword == StyleKeyword.Undefined && screen.style.display.value == DisplayStyle.Flex
+ && !screen.ClassListContains(ClosingClass);
+
+ /// <summary>Test seam: whether windows move. Only in play mode: an EditMode panel does not tick (EventImageCatalog's
+ /// note), so there a window shows and hides at once and the layout tests measure it at full size; HudTests turns
+ /// the motion on to see the classes switch.</summary>
+ internal static System.Func<bool> Moves = () => UnityEngine.Application.isPlaying;
+
+ /// <summary>Shows or hides a window (a side-menu screen, the decision card) with #1106's motion, all of it USS
+ /// transitions: a hidden window opens in OpeningClass (small and transparent) for one layout, then grows and fades
+ /// in to its normal style over 0.5 s; Unity runs no transition on an element coming out of display: none, hence the
+ /// start class. A shown window closes in ClosingClass, disabled so nothing in it takes a click or the focus while it
+ /// shrinks over 0.3 s, and leaves the layout when that scale transition ends. Opened again while it shrinks, it grows back
+ /// from where it is; closed before its first layout (two presses in one frame), it just hides.</summary>
+ public static void Show(VisualElement window, bool shown)
+ {
+ if (!Moves()) { window.style.display = shown ? DisplayStyle.Flex : DisplayStyle.None; return; }
+ bool inView = window.style.display.keyword == StyleKeyword.Undefined && window.style.display.value == DisplayStyle.Flex;
+ if (shown)
+ {
+ window.SetEnabled(true);
+ window.RemoveFromClassList(ClosingClass); // grows back, if it was shrinking
+ if (inView) return;
+ window.AddToClassList(OpeningClass);
+ window.RegisterCallback<GeometryChangedEvent>(EndOpening);
+ window.style.display = DisplayStyle.Flex;
+ return;
+ }
+ if (!inView || window.ClassListContains(OpeningClass)) { Hide(window); return; }
+ if (window.ClassListContains(ClosingClass)) return;
+ window.AddToClassList(ClosingClass);
+ window.SetEnabled(false);
+ window.RegisterCallback<TransitionEndEvent>(EndClosing);
+ // a safety net for a shrink that never reports its end (an ancestor hidden meanwhile, a panel that stops):
+ // the window must not stay in the layout, disabled. A net left from an earlier close can cut a quick
+ // close-reopen-close's shrink short; it never leaves a window stuck.
+ window.schedule.Execute(() => { if (window.ClassListContains(ClosingClass)) Hide(window); }).StartingIn(ClosingNetMs);
+ }
+
+ /// <summary>The safety net's delay: the 0.3 s shrink of .window-closing, plus a margin.</summary>
+ internal const long ClosingNetMs = 400;
+
+ static void Hide(VisualElement window)
+ {
+ window.UnregisterCallback<GeometryChangedEvent>(EndOpening);
+ window.UnregisterCallback<TransitionEndEvent>(EndClosing);
+ window.RemoveFromClassList(OpeningClass);
+ window.RemoveFromClassList(ClosingClass);
+ window.style.display = DisplayStyle.None;
+ }
+
+ /// <summary>The window's first layout out of display: none: its start state is resolved, so it can grow.</summary>
+ static void EndOpening(GeometryChangedEvent e)
+ {
+ var window = (VisualElement)e.currentTarget;
+ window.UnregisterCallback<GeometryChangedEvent>(EndOpening);
+ window.RemoveFromClassList(OpeningClass);
+ }
+
+ /// <summary>The shrink ended: the window leaves the layout, unless it was opened again meanwhile. Only the scale
+ /// counts: closed within 0.5 s of opening, the grow's opacity transition ends during the shrink. A cancelled
+ /// transition does not count: closing mid-grow cancels the grow's scale, and a shrink that stops is the safety
+ /// net's (Show).</summary>
+ static void EndClosing(TransitionEndEvent e) => EndClosing((VisualElement)e.currentTarget, e.target, e.AffectsProperty(Scale));
+ static readonly StylePropertyName Scale = new StylePropertyName("scale");
+ static void EndClosing(VisualElement window, IEventHandler target, bool scale)
+ {
+ if (scale && target == window && window.ClassListContains(ClosingClass)) Hide(window);
+ }
readonly VisualElement root;
readonly List<(Button, System.Action)> hooks = new List<(Button, System.Action)>();
readonly List<Button> closes = new List<Button>();
@@ -196,7 +268,7 @@ namespace Statecraft.Game.UI
foreach (var s in Screens)
{
var element = root.Q<VisualElement>("screen-" + s);
- element.style.display = s == screen ? DisplayStyle.Flex : DisplayStyle.None;
+ Show(element, s == screen); // #1106: grows in, shrinks away
root.Q<Button>("nav-" + s).EnableInClassList(OpenClass, s == screen); // the open screen's button is lit
if (s == screen) wide = element.ClassListContains(WideClass);
}
diff --git a/unity/Assets/Tests/EditMode/HudTests.cs b/unity/Assets/Tests/EditMode/HudTests.cs
index c0b017993..86d2dd883 100644
--- a/unity/Assets/Tests/EditMode/HudTests.cs
+++ b/unity/Assets/Tests/EditMode/HudTests.cs
@@ -249,6 +249,113 @@ public class HudTests
Assert.AreEqual(DisplayStyle.Flex, ui.Q<Label>("toast").style.display.value);
}
+ /// <summary>ScreenNav's motion as the test run found it (off in edit mode), put back after every test: an assertion
+ /// inside a nested coroutine skips the outer one's finally.</summary>
+ static readonly System.Func<bool> PlayModeMotion = ScreenNav.Moves;
+ [TearDown] public void StillWindows() => ScreenNav.Moves = PlayModeMotion;
+
+ /// <summary>Yields until <paramref name="done"/> holds, at most 3 s: an edit-mode panel updates its styles and layout
+ /// only now and then, not on every yield.</summary>
+ static IEnumerator Until(System.Func<bool> done)
+ {
+ var t0 = Time.realtimeSinceStartup;
+ while (!done() && Time.realtimeSinceStartup - t0 < 3f) yield return null;
+ }
+
+ /// <summary>#1106: a window (a side-menu screen, the decision card) opens in ScreenNav.OpeningClass, small and
+ /// transparent, for one layout, then grows and fades in over 0.5 s; on close it shrinks over 0.3 s (ClosingClass), out
+ /// of reach, and leaves the layout when that transition ends. An EditMode panel does not run the transitions' clock,
+ /// so the test checks the classes, the resolved transitions, and sends the transitions' ends itself.</summary>
+ static IEnumerator OpensAndCloses(VisualElement window, System.Action open, System.Action close, string what)
+ {
+ open();
+ Assert.AreEqual(DisplayStyle.Flex, window.style.display.value, what + " shows at once");
+ Assert.IsTrue(window.ClassListContains(ScreenNav.OpeningClass), what + " starts small and transparent");
+ Assert.IsTrue(ScreenNav.IsOpen(window), what + " is open");
+ yield return Until(() => !window.ClassListContains(ScreenNav.OpeningClass));
+ Assert.IsFalse(window.ClassListContains(ScreenNav.OpeningClass), what + ": the start state lasts one layout, then it grows");
+ yield return Until(() => window.resolvedStyle.transitionDuration.First().value > 0.4f);
+ Assert.AreEqual(0.5f, window.resolvedStyle.transitionDuration.First().value, 1e-4, what + ": scale-up and fade-in, 0.5 s");
+ CollectionAssert.AreEquivalent(new[] { "scale", "opacity" }, window.resolvedStyle.transitionProperty.Select(p => p.ToString()), what);
+ close();
+ Assert.IsTrue(window.ClassListContains(ScreenNav.ClosingClass), what + " shrinks");
+ Assert.IsFalse(ScreenNav.IsOpen(window), what + " counts as closed while it shrinks");
+ Assert.IsFalse(window.enabledSelf, what + ": out of reach while it shrinks (#400)");
+ Assert.AreEqual(DisplayStyle.Flex, window.style.display.value, what + " stays in view while it shrinks");
+ yield return Until(() => window.resolvedStyle.transitionDuration.First().value < 0.4f);
+ Assert.AreEqual(0.3f, window.resolvedStyle.transitionDuration.First().value, 1e-4, what + ": scale-down, 0.3 s");
+ Assert.AreEqual("scale", window.resolvedStyle.transitionProperty.Single().ToString(), what + ": a scale-down, no fade");
+ End(window, "opacity"); // the grow's fade, when it closes within 0.5 s of opening
+ Assert.AreEqual(DisplayStyle.Flex, window.style.display.value, what + ": only the end of the shrink hides it");
+ open(); // opened again while it shrinks: it grows back from there
+ Assert.IsFalse(window.ClassListContains(ScreenNav.ClosingClass), what + " grows back");
+ Assert.IsFalse(window.ClassListContains(ScreenNav.OpeningClass), what + ": from where it is, not from the start state");
+ Assert.IsTrue(window.enabledSelf && ScreenNav.IsOpen(window), what + " is open and in reach again");
+ End(window, "scale"); // the interrupted shrink's end, late: ignored
+ Assert.AreEqual(DisplayStyle.Flex, window.style.display.value, what + " stays open");
+ close();
+ End(window, "scale");
+ Assert.AreEqual(DisplayStyle.None, window.style.display.value, what + " leaves the layout when the shrink ends");
+ Assert.IsFalse(window.ClassListContains(ScreenNav.ClosingClass), what);
+ open(); close(); // closed before its first layout (two presses in one frame)
+ Assert.AreEqual(DisplayStyle.None, window.style.display.value, what + ": nothing to shrink from, it hides at once");
+ Assert.IsFalse(window.ClassListContains(ScreenNav.OpeningClass) || window.ClassListContains(ScreenNav.ClosingClass), what);
+ }
+
+ /// <summary>Sends the end of a transition of <paramref name="property"/> on <paramref name="window"/>, as the panel does.</summary>
+ static void End(VisualElement window, string property)
+ {
+ using var e = TransitionEndEvent.GetPooled(new StylePropertyName(property), 0.3);
+ e.target = window; window.SendEvent(e);
+ }
+
+ [UnityTest]
+ public IEnumerator WindowsScaleUpAndFadeInWhenTheyOpenAndScaleDownWhenTheyClose()
+ {
+ ScreenNav.Moves = () => true; // play mode's motion; StillWindows puts it back (an EditMode panel ticks only now and then: Until)
+ using var ui = new UiTestKit();
+ using var nav = new ScreenNav(ui.Root);
+ foreach (var s in ScreenNav.Screens)
+ yield return OpensAndCloses(ui.Q<VisualElement>("screen-" + s), () => ui.Press("nav-" + s), () => ui.Press("nav-" + s), s);
+ using var card = new EventCardPresenter(ui.Root, NewBridge(), Text, null); // a new game opens on a decision
+ var element = ui.Q<VisualElement>("event-card");
+ card.Collapse();
+ yield return Until(() => element.style.display.value == DisplayStyle.None);
+ yield return OpensAndCloses(element, card.Reopen, card.Collapse, "the decision card");
+ }
+
+ /// <summary>#1175 review: a window closed while it still grows (within 0.5 s of opening) retargets the scale
+ /// transition, and the panel sends a TransitionCancelEvent for the grow. That must not hide it: it shrinks, and only the
+ /// shrink's end or ScreenNav's safety net (ClosingNetMs) takes it out of the layout.</summary>
+ [UnityTest]
+ public IEnumerator AWindowClosedWhileItGrowsStillShrinks()
+ {
+ ScreenNav.Moves = () => true; // StillWindows puts it back
+ using var ui = new UiTestKit();
+ using var nav = new ScreenNav(ui.Root);
+ var screen = ScreenNav.Screens.First();
+ var window = ui.Q<VisualElement>("screen-" + screen);
+ ui.Press("nav-" + screen);
+ yield return Until(() => !window.ClassListContains(ScreenNav.OpeningClass));
+ ui.Press("nav-" + screen); // closed mid-grow
+ var closedAt = Time.realtimeSinceStartup;
+ using (var e = TransitionCancelEvent.GetPooled(new StylePropertyName("scale"), 0.2))
+ {
+ e.target = window; window.SendEvent(e); // the grow, cancelled by the shrink
+ }
+ Assert.AreEqual(DisplayStyle.Flex, window.style.display.value, "the cancelled grow does not hide it: it shrinks");
+ Assert.IsTrue(window.ClassListContains(ScreenNav.ClosingClass), "still shrinking");
+ // a quiet EditMode panel runs its scheduler (the safety net) only on player-loop requests (SafeAreaTests)
+ while (window.style.display.value != DisplayStyle.None && Time.realtimeSinceStartup - closedAt < 3f)
+ {
+ EditorApplication.QueuePlayerLoopUpdate();
+ yield return null;
+ }
+ Assert.AreEqual(DisplayStyle.None, window.style.display.value, "the safety net hides it when no shrink end comes");
+ Assert.GreaterOrEqual(Time.realtimeSinceStartup - closedAt, ScreenNav.ClosingNetMs / 1000f - 0.05f,
+ "not before ClosingNetMs");
+ }
+
[Test]
public void NavigationOpensOneScreenAtATime()
{
diff --git a/unity/Assets/UI/Statecraft.uss b/unity/Assets/UI/Statecraft.uss
index 787fecd00..7759c35d4 100644
--- a/unity/Assets/UI/Statecraft.uss
+++ b/unity/Assets/UI/Statecraft.uss
@@ -403,6 +403,15 @@ Button:disabled { color: var(--ink-dim); border-color: rgba(120, 120, 120, 0.4);
/* Full screens (Cabinet, Budget, Defence, Why, Settings) open in the centre, one at a time. */
/* Hidden until ScreenNav opens one (never visible while loading; playtest 2, P2-1). Right edge clear of the 480px column. */
.screen { display: none; position: absolute; left: 552px; right: 512px; top: 112px; bottom: 16px; padding: 18px; background-color: rgb(11, 18, 32); } /* opaque: a reading screen, and the map labels must not show through */
+/* #1106 (play-test of v0.1.2): a window, every side-menu screen and the decision card, grows and fades in over 0.5 s when
+ it opens and shrinks over 0.3 s when it closes. ScreenNav.Show switches the classes: .window-opening is the start state
+ for one layout (no transition, as Unity runs none out of display: none), .window-closing the shrink (the window leaves
+ the layout when it ends). Play mode only: an EditMode panel does not tick (ScreenNav.Moves). */
+.screen, .event-card { transition-property: scale, opacity; transition-duration: 0.5s, 0.5s; transition-timing-function: ease-out-cubic, ease-out-cubic; }
+.screen.window-opening, .event-card.window-opening { scale: 0.8 0.8; opacity: 0; transition-duration: 0s, 0s; }
+.screen.window-closing, .event-card.window-closing { scale: 0 0; transition-property: scale; transition-duration: 0.3s; transition-timing-function: ease-in-cubic; }
+/* The shrinking window is disabled (out of reach); the theme's half opacity for disabled elements would make the shrink a fade. */
+.screen.window-closing.unity-disabled, .event-card.window-closing.unity-disabled, .window-closing .unity-disabled { opacity: 1; }
.screen-header { flex-direction: column; align-items: stretch; border-bottom-width: 1px; border-color: var(--gold-dim); margin-bottom: 10px; padding-bottom: 4px; } /* the X's side padding is set inline by ScreenNav.FitCloseButtons (it grows with the X) */
/* Screen titles and section names in gold, letter-spaced, as the promo's "DEFENSE" and "LEADERSHIP". */
.screen-header > .title { color: var(--gold-light); letter-spacing: 1px; }