A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
UI: PageUp, PageDown, Home and End scroll the open screen or the card text (a11y H3, refs #41) (#780)
Beri60495af1f4ab
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 78fae1d63..ad285ba1c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -810,6 +810,10 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIBHebrew waits for the native reviewer (#564). The chips also follow a coalition change in the middle of a government(`coalition_changes`, such as 25 May 1953), where before they kept their old colours. Presentation only: no goldenmoves.+- PageUp, PageDown, Home and End scroll the open screen, such as a long Why list or the Defence screen, or the open+ decision card's text when it runs long; those panels hold only text, so Tab never reached into them and the arrows+ pan the map (accessibility audit H3, refs #41). With the focus in another list on the screen, such as the Cabinet's+ candidates, the keys scroll that list. The arrows still pan the map. Presentation only: no golden moves.- Wheel zoom in the web build: one small wheel delta, such as a touchpad's half pixel, made every later frame zoom 27%,since Unity's player takes the smallest delta it has seen as one notch. The camera now counts the browser's wheelevents itself (`WheelInput.jslib`): a touchpad's small pixel deltas zoom by how far the fingers move (100 px to adiff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex 33f2dbb21..7fe0c51b1 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -474,6 +474,16 @@ namespace Statecraft.Game.UI// With nothing else to close, Escape on the open decision card is Hide (EventCardPresenter.Escape; the card itself// takes the Cancel event when it has the focus).if (UnityEngine.InputSystem.Keyboard.current?.escapeKey.wasPressedThisFrame == true) { bool done = outcomes?.SkipAll() == true || outcomes?.ClosedThisFrame == true; done |= start?.Escape() == true; done |= nav?.Close() == true; if (!done && document.rootVisualElement.focusController?.focusedElement == null) card?.Escape(); }+ // PageUp, PageDown, Home and End, with no modifier, scroll the open screen or the open card's text, with or without+ // focus (KeyScroll; accessibility audit H3), but not under an End Turn outcome clip, which covers them. The arrows+ // stay with the camera (NavigationGuard, B6).+ var keys = UnityEngine.InputSystem.Keyboard.current;+ if (keys != null && outcomes?.Showing == null && !(keys.ctrlKey.isPressed || keys.altKey.isPressed || keys.shiftKey.isPressed || keys.leftMetaKey.isPressed || keys.rightMetaKey.isPressed))+ {+ KeyScroll.Key? page = keys.pageUpKey.wasPressedThisFrame ? KeyScroll.Key.PageUp : keys.pageDownKey.wasPressedThisFrame ? KeyScroll.Key.PageDown+ : keys.homeKey.wasPressedThisFrame ? KeyScroll.Key.Home : keys.endKey.wasPressedThisFrame ? KeyScroll.Key.End : (KeyScroll.Key?)null;+ if (page != null) KeyScroll.Press(document.rootVisualElement, nav?.Current, card?.Shown != null && !card.Collapsed, page.Value);+ }SyncMinimap();if (!logCamera || rig == null || (rig.Distance == loggedDistance && rig.Yaw == loggedYaw)) return;loggedDistance = rig.Distance; loggedYaw = rig.Yaw;diff --git a/unity/Assets/Scripts/UI/KeyScroll.cs b/unity/Assets/Scripts/UI/KeyScroll.csnew file mode 100644index 000000000..e341ac460--- /dev/null+++ b/unity/Assets/Scripts/UI/KeyScroll.cs@@ -0,0 +1,53 @@+#nullable enable+using System.Collections.Generic;+using UnityEngine;+using UnityEngine.UIElements;++namespace Statecraft.Game.UI+{+ /// <summary>PageUp, PageDown, Home and End scroll the open screen, or the open decision card's text, from the keyboard+ /// (accessibility audit H3, #41). The Why list, the event text and the Defence body hold labels only, so Tab never+ /// reaches into them, and the arrows pan the camera and never move focus (NavigationGuard, ruling B6). GameRoot polls+ /// the keys beside its Escape poll, so they work with or without focus.</summary>+ public static class KeyScroll+ {+ public enum Key { PageUp, PageDown, Home, End }++ /// <summary>Each screen's main view (ScreenNav.Screens): what the keys scroll unless the focus is in another list+ /// on that screen, such as the Cabinet's candidates.</summary>+ static readonly Dictionary<string, string> MainViews = new Dictionary<string, string>+ {+ ["cabinet"] = "cabinet-seats", ["budget"] = "budget-rows", ["defence"] = "defence-body", ["why"] = "why-rows",+ };++ /// <summary>A page is the view's height less about one line of text, so the line at the edge stays in sight.</summary>+ const float Line = 24f;++ /// <summary>Scrolls the target view one page up or down, or to its top or bottom; false when there is no target or+ /// it is already there. The target: the ScrollView around the focused element if that is inside the open screen,+ /// else the open screen's main view, else (no screen open) the decision card's text while the card is open and not+ /// hidden. Nothing while the start screen shows.</summary>+ public static bool Press(VisualElement root, string? screen, bool cardOpen, Key key)+ {+ var view = Target(root, screen, cardOpen);+ if (view == null) return false;+ float high = view.verticalScroller.highValue, y = view.scrollOffset.y;+ if (float.IsNaN(high) || high <= 0f) return false; // everything fits: nothing to scroll+ float page = Mathf.Max(Line, view.contentViewport.layout.height - Line);+ float to = Mathf.Clamp(key switch { Key.PageUp => y - page, Key.PageDown => y + page, Key.Home => 0f, _ => high }, 0f, high);+ if (Mathf.Abs(to - y) < 0.5f) return false;+ view.scrollOffset = new Vector2(view.scrollOffset.x, to);+ return true;+ }++ static ScrollView? Target(VisualElement root, string? screen, bool cardOpen)+ {+ if ((root.Q("root") ?? root).ClassListContains(StartScreenPresenter.OpenClass)) return null;+ if (screen == null) return cardOpen ? root.Q<ScrollView>("event-scroll") : null;+ var open = root.Q<VisualElement>("screen-" + screen);+ var around = (root.panel?.focusController?.focusedElement as VisualElement)?.GetFirstAncestorOfType<ScrollView>();+ if (around != null && open != null && open.Contains(around)) return around;+ return MainViews.TryGetValue(screen, out var main) ? root.Q<ScrollView>(main) : null;+ }+ }+}diff --git a/unity/Assets/Scripts/UI/KeyScroll.cs.meta b/unity/Assets/Scripts/UI/KeyScroll.cs.metanew file mode 100644index 000000000..63ab5755a--- /dev/null+++ b/unity/Assets/Scripts/UI/KeyScroll.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 883e8069806109c63b79f98826b0e0fb\ No newline at end of filediff --git a/unity/Assets/Tests/EditMode/KeyScrollTests.cs b/unity/Assets/Tests/EditMode/KeyScrollTests.csnew file mode 100644index 000000000..2447b4e6d--- /dev/null+++ b/unity/Assets/Tests/EditMode/KeyScrollTests.cs@@ -0,0 +1,177 @@+#nullable enable+using System;+using System.Collections;+using System.Collections.Generic;+using System.Linq;+using NUnit.Framework;+using Statecraft.Game;+using Statecraft.Game.UI;+using Statecraft.Sim;+using UnityEditor;+using UnityEngine;+using UnityEngine.TestTools;+using UnityEngine.UIElements;+using Object = UnityEngine.Object;++/// <summary>Accessibility audit H3 (#41): PageUp, PageDown, Home and End scroll the open screen's list, or the open decision+/// card's text, from the keyboard (KeyScroll), since those panels hold labels that Tab never reaches and the arrows pan the+/// camera.</summary>+public class KeyScrollTests+{+ static readonly Content C = ContentLoader.Load(Paths.Content);+ static UiText Text => UiText.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(UiPaths.Strings).text);++ /// <summary>A real panel in the playtest window (1568x752) with the game's UXML.</summary>+ sealed class Rig : IDisposable+ {+ public readonly VisualElement Root;+ readonly GameObject go; readonly PanelSettings ps; readonly RenderTexture rt;+ readonly List<IDisposable> owned = new List<IDisposable>();++ public Rig()+ {+ rt = new RenderTexture(1568, 752, 0);+ ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));+ ps.targetTexture = rt;+ go = new GameObject("KeyScrollTest");+ go.SetActive(false);+ var doc = go.AddComponent<UIDocument>();+ doc.panelSettings = ps;+ doc.visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(UiPaths.GameUxml);+ go.SetActive(true);+ Root = doc.rootVisualElement;+ Root.Q("root")?.RemoveFromClassList("loading");+ }++ public T Own<T>(T d) where T : IDisposable { owned.Add(d); return d; }++ public void Dispose()+ {+ for (int i = owned.Count - 1; i >= 0; i--) owned[i].Dispose();+ Object.DestroyImmediate(go); Object.DestroyImmediate(ps); rt.Release(); Object.DestroyImmediate(rt);+ }+ }++ static IEnumerator Frames(int n) { for (int i = 0; i < n; i++) yield return null; }++ /// <summary>Holds a view to <paramref name="height"/> px, so its content overflows whatever the window.</summary>+ static void Shorten(ScrollView view, float height)+ {+ view.style.height = height; view.style.minHeight = height; view.style.maxHeight = height; view.style.flexGrow = 0;+ }++ /// <summary>PageDown moves one view less a line, End reaches the bottom, PageUp moves back and Home reaches the top; at+ /// the top PageUp does nothing. The focus never moves.</summary>+ static void PagesThrough(VisualElement root, ScrollView view, string? screen, bool cardOpen, string where)+ {+ var focused = root.panel.focusController.focusedElement;+ float high = view.verticalScroller.highValue, page = Mathf.Max(24f, view.contentViewport.layout.height - 24f);+ Assert.Greater(high, page, where + ": precondition, the view overflows by more than a page");+ Assert.AreEqual(0f, view.scrollOffset.y, 0.5f, where + ": precondition, at the top");+ Assert.IsTrue(KeyScroll.Press(root, screen, cardOpen, KeyScroll.Key.PageDown), where + ": PageDown scrolls");+ Assert.AreEqual(Mathf.Min(page, high), view.scrollOffset.y, 0.5f, where + ": PageDown moves one view less a line");+ Assert.IsTrue(KeyScroll.Press(root, screen, cardOpen, KeyScroll.Key.End), where + ": End scrolls");+ Assert.AreEqual(high, view.scrollOffset.y, 0.5f, where + ": End reaches the bottom");+ Assert.IsFalse(KeyScroll.Press(root, screen, cardOpen, KeyScroll.Key.PageDown), where + ": PageDown at the bottom");+ Assert.IsTrue(KeyScroll.Press(root, screen, cardOpen, KeyScroll.Key.PageUp), where + ": PageUp scrolls");+ Assert.AreEqual(high - page, view.scrollOffset.y, 0.5f, where + ": PageUp moves one view less a line");+ Assert.IsTrue(KeyScroll.Press(root, screen, cardOpen, KeyScroll.Key.Home), where + ": Home scrolls");+ Assert.AreEqual(0f, view.scrollOffset.y, 0.5f, where + ": Home reaches the top");+ Assert.IsFalse(KeyScroll.Press(root, screen, cardOpen, KeyScroll.Key.PageUp), where + ": PageUp at the top");+ Assert.AreSame(focused, root.panel.focusController.focusedElement, where + ": the keys move no focus");+ }++ [UnityTest]+ public IEnumerator ThePageKeysScrollTheWhyListAndTheDefenceBody()+ {+ using var r = new Rig();+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);+ var nav = r.Own(new ScreenNav(r.Root));+ r.Own(new WhyPresenter(r.Root, bridge, Text));+ r.Own(new DefencePresenter(r.Root, bridge, Text));+ bridge.Queue(new SetMobilization(1.0));+ bridge.EndTurn();+ foreach (var (screen, view) in new[] { ("why", "why-rows"), ("defence", "defence-body") })+ {+ var v = r.Root.Q<ScrollView>(view);+ Shorten(v, 80f);+ nav.Open(screen);+ yield return Frames(4);+ PagesThrough(r.Root, v, screen, false, screen);+ }+ }++ [UnityTest]+ public IEnumerator FocusInTheCandidateListScrollsThatListNotTheCards()+ {+ using var r = new Rig();+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);+ while (g.State.Date < new DateTime(1950, 2, 1)) g.Step(HistoricalAI.Commands(C, g.State));+ var nav = r.Own(new ScreenNav(r.Root));+ var cabinet = r.Own(new CabinetPresenter(r.Root, new SimBridge(g, Text), Text, null));+ nav.Open("cabinet");+ cabinet.Select("finance_minister");+ var candidates = r.Root.Q<ScrollView>("cabinet-candidates");+ var seats = r.Root.Q<ScrollView>("cabinet-seats");+ Shorten(candidates, 60f);+ yield return Frames(4);+ Assert.Greater(seats.verticalScroller.highValue, 0f, "precondition: the cards overflow too");+ var first = candidates.contentContainer.Query<Button>().ToList().First(b => b.canGrabFocus);+ first.Focus();+ yield return Frames(2);+ Assert.AreSame(first, r.Root.panel.focusController.focusedElement, "precondition: a candidate has the focus");+ PagesThrough(r.Root, candidates, "cabinet", false, "candidates");+ Assert.AreEqual(0f, seats.scrollOffset.y, 0.5f, "the cards did not move");++ first.Blur();+ yield return Frames(2);+ Assert.IsTrue(KeyScroll.Press(r.Root, "cabinet", false, KeyScroll.Key.PageDown), "no focus in a list: the cards scroll");+ Assert.Greater(seats.scrollOffset.y, 0f, "the cards moved");+ }++ [UnityTest]+ public IEnumerator TheCardsTextScrollsOnlyWhileTheCardIsOpenAndNoScreenCoversIt()+ {+ using var r = new Rig();+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);+ while (!g.State.PendingEvents.Contains("armistice_egypt")) g.Step(new Command[0]);+ var bridge = new SimBridge(g, Text);+ var nav = r.Own(new ScreenNav(r.Root));+ r.Own(new WhyPresenter(r.Root, bridge, Text));+ var card = r.Own(new EventCardPresenter(r.Root, bridge, Text, null));+ var text = r.Root.Q<ScrollView>("event-scroll");+ Shorten(text, 16f);+ yield return Frames(4);+ Assert.IsNotNull(card.Shown, "precondition: the card is open");+ Assert.IsFalse(card.Collapsed, "precondition: the card is not hidden");+ Assert.Greater(text.worldBound.height, 0f, "precondition: the card's text is laid out");+ Assert.IsFalse(KeyScroll.Press(r.Root, null, false, KeyScroll.Key.PageDown), "a hidden card's text does not scroll");+ Assert.AreEqual(0f, text.scrollOffset.y, 0.5f, "hidden card");+ nav.Open("why");+ yield return Frames(2);+ KeyScroll.Press(r.Root, "why", true, KeyScroll.Key.PageDown);+ Assert.AreEqual(0f, text.scrollOffset.y, 0.5f, "an open screen takes the keys, not the card under it");+ nav.Open(null);+ yield return Frames(4);+ PagesThrough(r.Root, text, null, true, "event text");+ }++ [UnityTest]+ public IEnumerator NothingScrollsUnderTheStartScreen()+ {+ using var r = new Rig();+ var bridge = new SimBridge(GameSession.New(C, GoldenRun.Seed, Mode.Historical), Text);+ var nav = r.Own(new ScreenNav(r.Root));+ r.Own(new WhyPresenter(r.Root, bridge, Text));+ bridge.EndTurn();+ var rows = r.Root.Q<ScrollView>("why-rows");+ Shorten(rows, 40f);+ nav.Open("why");+ r.Root.Q("root").AddToClassList(StartScreenPresenter.OpenClass);+ yield return Frames(4);+ Assert.Greater(rows.verticalScroller.highValue, 0f, "precondition: the list overflows");+ foreach (KeyScroll.Key key in Enum.GetValues(typeof(KeyScroll.Key)))+ Assert.IsFalse(KeyScroll.Press(r.Root, "why", false, key), key + " under the start screen");+ Assert.AreEqual(0f, rows.scrollOffset.y, 0.5f, "the list did not move");+ }+}diff --git a/unity/Assets/Tests/EditMode/KeyScrollTests.cs.meta b/unity/Assets/Tests/EditMode/KeyScrollTests.cs.metanew file mode 100644index 000000000..a7cde8841--- /dev/null+++ b/unity/Assets/Tests/EditMode/KeyScrollTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 8ababce6f20d7028399e556f0e9bfc93\ No newline at end of filediff --git a/unity/Assets/Tests/EditMode/PromoHudTests.cs b/unity/Assets/Tests/EditMode/PromoHudTests.csindex 70551d302..28f880ba2 100644--- a/unity/Assets/Tests/EditMode/PromoHudTests.cs+++ b/unity/Assets/Tests/EditMode/PromoHudTests.cs@@ -460,7 +460,8 @@ public class PromoHudTestsyield return null;Assert.AreSame(start, start.focusController.focusedElement, $"{name}: a {d} move left focus where it was");}- foreach (var key in new[] { KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.Q, KeyCode.E })+ foreach (var key in new[] { KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.Q, KeyCode.E,+ KeyCode.PageUp, KeyCode.PageDown, KeyCode.Home, KeyCode.End }){using (var e = KeyDownEvent.GetPooled((char)0, key, EventModifiers.None)) { e.target = start; start.SendEvent(e); }yield return null;