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.

25.5 KB
Raw Blame
#nullable enable
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UIElements;

namespace Statecraft.Game.UI
{
    /// <summary>The side-menu buttons open the Cabinet, Budget, Defence, Why, Timeline, Encyclopedia, Objectives and Settings screens, one at a time; pressing the open
    /// screen's button again closes it. Every press toggles, synchronously, with no debounce: two presses in one frame (a
    /// double click, or a click plus Enter) open and close again, so the result depends only on the number of presses
    /// (HudTests.TwoPressesInOneFrameToggleTwice; playtest 1, item 11).</summary>
    public sealed class ScreenNav : System.IDisposable
    {
        public static readonly string[] Screens = { "cabinet", "budget", "defence", "why", "timeline", "encyclopedia", "objectives", "settings" };
        // Transient map confirmations expire even for a narrow screen that leaves the info panel visible.
        internal static event System.Action<VisualElement>? InfoIntentCovered;

        /// <summary>The class on the open screen's button (Statecraft.uss lights its ring).</summary>
        public const string OpenClass = "nav-open";
        /// <summary>The class on the "root" element while a wide screen (class <see cref="WideClass"/>: Cabinet, Defence, Timeline) is
        /// open: Statecraft.uss hides the info panel it covers, so its buttons take no focus and no clicks.</summary>
        public const string WideOpenClass = "wide-screen-open", WideClass = "screen-wide";
        /// <summary>The class on the "root" element while any screen is open: Statecraft.uss hides the decision layer under it,
        /// 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
        /// reach (#400). The start screen covers the top bar, the minimap column, the decision layer and the info panel; any
        /// open screen covers the decision layer (it sits where the card does), and in the two-row HUD also the minimap
        /// column and the info panel; a wide screen covers the info panel; the open decision card's backdrop
        /// (EventCardPresenter.OpenClass, with no start screen or screen over it: ModalClass, set here) covers the minimap
        /// column and the info panel, and so does the open capture dialog's (CaptureDialogPresenter.OpenClass, in the same
        /// layer); an End Turn outcome clip (OutcomePresenter.OpenClass) covers the top bar, the minimap
        /// column, the info panel and the decision layer. The turn report (TurnReportPresenter.OpenClass, #1037) covers them
        /// too, and is itself enabled only while it shows and no start screen or outcome clip covers it; when it comes out it
        /// takes the focus (OnUncovered on its element). Called wherever one of those classes changes (StartScreenPresenter,
        /// Open, HudPresenter's tier, EventCardPresenter, OutcomePresenter, TurnReportPresenter), so no caller re-enables a
        /// panel another still covers.</summary>
        public static void DisableCovered(VisualElement root)
        {
            var r = root.Q("root") ?? root;
            // The raid card on New game (RaidCardPresenter), and the raid flight before it (RaidFlightPresenter), cover the HUD
            // as the opening does.
            bool opening = r.ClassListContains(OpeningPresenter.OpenClass) || r.ClassListContains(RaidCardPresenter.OpenClass)
                || r.ClassListContains(RaidFlightPresenter.OpenClass);
            bool start = r.ClassListContains(StartScreenPresenter.OpenClass) || opening;
            r.Q("start-screen")?.SetEnabled(!opening);
            bool screen = r.ClassListContains(ScreenOpenClass);
            bool full = screen && r.ClassListContains(HudPresenter.TwoRowClass);
            bool fullWindow = screen && r.ClassListContains(HudPresenter.TightClass) && r.ClassListContains(HudPresenter.ThreeRowClass);
            bool outcome = r.ClassListContains(OutcomePresenter.OpenClass) || r.ClassListContains(AdvisorPresenter.OverlayOpenClass);   // an End Turn outcome clip covers all but itself
            bool end = r.ClassListContains(EndScreenPresenter.OpenClass);   // the end screen (#812) covers the HUD and the report
            bool report = r.ClassListContains(TurnReportPresenter.OpenClass) || end;   // the turn report covers the HUD under it
            bool modal = (r.ClassListContains(EventCardPresenter.OpenClass) || r.ClassListContains(CaptureDialogPresenter.OpenClass)) && !start && !screen && !outcome && !report;
            r.EnableInClassList(EventCardPresenter.ModalClass, modal);
            r.Q("top-bar")?.SetEnabled(!(start || outcome || report || fullWindow));
            r.Q("left-column")?.SetEnabled(!(start || outcome || report || fullWindow));   // the screens' buttons: usable wherever the top bar is
            r.Q(CommunityIconsPresenter.ColumnName)?.SetEnabled(!(start || outcome || report || fullWindow));   // Star, Report, Share (#1236): in the same lane
            r.Q("right-column")?.SetEnabled(!(start || full || modal || outcome || report));
            bool infoCovered = start || full || modal || outcome || report || r.ClassListContains(WideOpenClass);
            r.Q("info-panel")?.SetEnabled(!infoCovered);
            if (screen || infoCovered) InfoIntentCovered?.Invoke(r);
            // the map's keyboard stop (#41 B1b): out of reach exactly when the info panel is, so a pick is never refused (#210)
            r.Q("map-focus")?.SetEnabled(!infoCovered);
            var layer = r.Q("event-layer");
            if (layer != null)
            {
                bool was = layer.enabledSelf, now = !(start || screen || outcome || report);
                layer.SetEnabled(now);
                if (!was && now) (layer.userData as System.Action)?.Invoke();   // EventCardPresenter: the open card takes the focus
            }
            var panel = r.Q(TurnReportPresenter.ElementName);
            if (panel != null)
            {
                bool was = panel.enabledSelf, now = r.ClassListContains(TurnReportPresenter.OpenClass) && !(start || outcome || end);
                panel.SetEnabled(now);
                if (!was && now) (panel.userData as System.Action)?.Invoke();   // TurnReportPresenter: Close takes the focus
            }
            var over = r.Q(EndScreenPresenter.ElementName);
            if (over != null)
            {
                bool was = over.enabledSelf, now = end && !(start || outcome);
                over.SetEnabled(now);
                if (!was && now) (over.userData as System.Action)?.Invoke();   // EndScreenPresenter: Close takes the focus
            }
            // The advisors' popup (spec 4.2) hides under every layer above and comes back when they close: every layer calls
            // this method when its class changes, so the popup needs no class of its own here.
            (r.Q(AdvisorPresenter.LayerName)?.userData as System.Action)?.Invoke();
        }

        /// <summary>Asks to be told, synchronously, whenever the decision layer comes out from under the start screen, a
        /// screen or an outcome clip (DisableCovered enables it again); null stops it.</summary>
        public static void OnUncovered(VisualElement layer, System.Action? action) => layer.userData = action;

        /// <summary>A second listener on the decision layer, beside the one <see cref="OnUncovered"/> set (the capture dialog
        /// shares the event card's layer): added after it, and taken away again with <see cref="NotOnUncovered"/>.</summary>
        public static void AlsoOnUncovered(VisualElement layer, System.Action action) => layer.userData = (layer.userData as System.Action) + action;

        public static void NotOnUncovered(VisualElement layer, System.Action action) => layer.userData = (layer.userData as System.Action) - action;

        /// <summary>Asks to be told, synchronously, whenever <paramref name="screen"/> is opened (a presenter that skips
        /// 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), 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.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>();
        readonly HoverTip? closeTips;
        readonly EventCallback<NavigationCancelEvent> onCancel;
        readonly EventCallback<GeometryChangedEvent> onGeometry;
        readonly PressSource presses;

        /// <summary>A layer over the screens that takes Escape first (GameRoot: the report panel, #1169, and the share panel, #1170): true when it used the
        /// key, and the open screen under it stays open. This handler trickles down from the root, so it runs before the layer's own.</summary>
        public System.Func<bool>? TakesEscape;

        public ScreenNav(VisualElement root) : this(root, null) { }

        public ScreenNav(VisualElement root, UiText? text)
        {
            this.root = root;
            // Escape (the UI map's Cancel, from any focused element or the panel) closes the open screen. NavigationGuard
            // handles only NavigationMoveEvent, so the two never meet. GameRoot also polls the Escape key for the case where
            // no element has the focus (CloseOnEscape).
            onCancel = e => { if (TakesEscape?.Invoke() == true || Close()) e.StopPropagation(); };
            root.RegisterCallback(onCancel, TrickleDown.TrickleDown);
            onGeometry = e => FitCloseButtons();
            root.RegisterCallback(onGeometry);
            presses = new PressSource(root);
            foreach (var s in Screens)
            {
                var screen = s;
                var b = root.Q<Button>("nav-" + screen);
                System.Action h = () => Toggle(screen);
                b.clicked += h;
                hooks.Add((b, h));

                // an X on the screen itself closes it: a phone has no Escape, and re-tapping the lit rail button is not
                // discoverable (playtest v0.1.1). The same Close as Escape: pressed from the keyboard, the focus returns as
                // after Escape; pressed with the pointer it moves nowhere (PointerBlur's rule), else the rail button would
                // light up and show its tip (#1323).
                var header = root.Q("screen-" + screen)?.Q(className: "screen-header");
                if (header == null) continue;
                var x = new Button(() => Close(handFocus: presses.ByKeyboard))
                {
                    name = "screen-close-" + screen,
                    text = "X",
                    // HudPresenter has already localized the rail button. Keep the visible X, but give the icon button
                    // the screen-specific name HoverTip and the Web companion read (#2566).
                    tooltip = text == null ? b.text : text.Format("screen.close", b.text),
                };
                x.AddToClassList("screen-close");
                header.Add(x);
                closes.Add(x);
            }
            if (closes.Count > 0 && root.Q<Label>(HoverTip.TipName) != null)
            {
                var uiRoot = root.Q("root") ?? root;
                closeTips = new HoverTip(root, closes,
                    () => uiRoot.ClassListContains(UiLanguage.HebrewClass) ? HoverTip.Side.Right : HoverTip.Side.Left);
            }
            Open(null);
            FitCloseButtons();
        }

        /// <summary>The X's hit area: 44 screen px at every window size (#1035's ruling), so in panel units it grows as
        /// the panel shrinks (<paramref name="scale"/> is screen px per panel px; the 1280x720 window scales about 2/3,
        /// so 44 panel px would render as about 29 px there). On the web a screen pixel is a CSS pixel: the canvas is
        /// drawn in device pixels, so the target is <paramref name="dpr"/> device px per CSS px (HudPresenter.cs's text
        /// floor counts the same way, DevicePixelRatio.jslib). Off the web <paramref name="dpr"/> is 1.</summary>
        internal static float CloseTargetPanelPx(float scale, float dpr = 1f) =>
            scale > 0f ? 44f * (dpr > 0f ? dpr : 1f) / scale : 44f;

#if UNITY_WEBGL && !UNITY_EDITOR
        // The panel scales by the canvas's device pixels, so a screen pixel here is a CSS pixel: the device's pixels / the
        // browser's devicePixelRatio (DevicePixelRatio.jslib; the same import as HudPresenter's text floor).
        [System.Runtime.InteropServices.DllImport("__Internal")] static extern float StatecraftDevicePixelRatio();
        static float DevicePixelRatio() { var r = StatecraftDevicePixelRatio(); return r > 0f ? r : 1f; }
#else
        static float DevicePixelRatio() => 1f;
#endif

        void FitCloseButtons()
        {
            var px = CloseTargetPanelPx(root.panel?.scaledPixelsPerPoint ?? 1f, DevicePixelRatio());
            var r = new StyleLength(new Length(px / 2f));
            foreach (var x in closes)
            {
                x.style.width = px; x.style.height = px;
                x.style.borderTopLeftRadius = r; x.style.borderTopRightRadius = r;
                x.style.borderBottomLeftRadius = r; x.style.borderBottomRightRadius = r;

                // Pad both sides of the header here, not in the stylesheet: a grown X (66 panel px at 1280x720, 89 at
                // 390x844) would cover the title's end under the old fixed 52 px, and one inline rule serves both
                // languages. Also grow the header so the X (top 6 px + px) fits inside it, with its 4 px bottom
                // padding and 1 px border.
                var header = x.parent;
                if (header == null) continue;
                header.style.paddingLeft = new StyleLength(px + 8f);
                header.style.paddingRight = new StyleLength(px + 8f);
                header.style.minHeight = new StyleLength(px + 6f + 5f);
            }
        }

        public void Dispose()
        {
            foreach (var (b, h) in hooks) b.clicked -= h;
            hooks.Clear();
            closeTips?.Dispose();
            foreach (var x in closes) x.RemoveFromHierarchy();
            closes.Clear();
            root.UnregisterCallback(onCancel, TrickleDown.TrickleDown);
            root.UnregisterCallback(onGeometry);
            presses.Dispose();
            var r = root.Q("root") ?? root;
            r.RemoveFromClassList(WideOpenClass); r.RemoveFromClassList(ScreenOpenClass);
            DisableCovered(root);
        }

        /// <summary>Closes the open screen (Escape); false when none was open. When the focus was on the screen's contents it
        /// goes to the screen's top-bar button, so the next Tab carries on from there (#41 M1); an open decision card
        /// takes it instead (EventCardPresenter, when its cover closes).</summary>
        public bool Close() => Close(handFocus: true);
        int closedFrame = -1;
        public bool ClosedThisFrame => closedFrame == UnityEngine.Time.frameCount;

        /// <param name="handFocus">False when the screen's X was pressed with the pointer: the focus is not handed on.</param>
        bool Close(bool handFocus)
        {
            if (Current == null) return false;
            var screen = root.Q<VisualElement>("screen-" + Current); var button = root.Q<Button>("nav-" + Current);
            var focused = root.panel?.focusController?.focusedElement as VisualElement;
            var inside = focused != null && (focused == screen || screen.Contains(focused));
            Open(null); closedFrame = UnityEngine.Time.frameCount;
            if (handFocus && inside && !(root.Q("root") ?? root).ClassListContains(EventCardPresenter.ModalClass)) FocusReturn.To(button);
            return true;
        }

        /// <summary>The top-bar button of the screen that holds <paramref name="e"/>, or null when it is in no screen: where
        /// <see cref="Close"/> hands a focus that was on the screen's contents. A presenter that closes the screens from a
        /// press inside one (the Encyclopedia's "Show the opening advice again") asks this rather than the focus controller:
        /// a focus change made while UI Toolkit is still dispatching that press is applied only after it, so focusedElement
        /// still names the control in the closed screen (#3028).</summary>
        public static Button? ButtonOfScreenAround(VisualElement root, VisualElement? e)
        {
            for (var v = e; v != null; v = v.parent)
                foreach (var s in Screens)
                    if (v.name == "screen-" + s) return root.Q<Button>("nav-" + s);
            return null;
        }

        /// <summary>The open screen's name, or null.</summary>
        public string? Current { get; private set; }
        /// <summary>Raised with <see cref="Current"/> each time a screen opens or closes (TurnReportPresenter: a report that
        /// waited for the enemy's moves opens once the player closes the screen they opened meanwhile, #2202).</summary>
        public event System.Action<string?>? Changed;

        public void Toggle(string screen)
        {
            Boot.DiagLog.NavLine($"toggle {screen}: {Current ?? "none"} -> {(Current == screen ? "none" : screen)}");
            Open(Current == screen ? null : screen);
        }

        public void Open(string? screen)
        {
            Current = screen;
            var wide = false;
            foreach (var s in Screens)
            {
                var element = root.Q<VisualElement>("screen-" + s);
                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);
            }
            (root.Q("root") ?? root).EnableInClassList(WideOpenClass, wide);
            (root.Q("root") ?? root).EnableInClassList(ScreenOpenClass, screen != null);
            DisableCovered(root);
            if (screen != null) (root.Q<VisualElement>("screen-" + screen).userData as System.Action)?.Invoke();
            // After the OnOpened action, which is what builds a screen's rows (CabinetPresenter, DefencePresenter):
            // before it there is nothing in the screen to hand the focus to.
            if (screen != null && presses.ByKeyboard) HandFocusTo(root.Q<VisualElement>("screen-" + screen));
            Changed?.Invoke(screen);
        }

        /// <summary>Gives the keyboard focus to the screen just opened (accessibility audit, #41, the keyboard-only
        /// turn's last "Still open" item). Without it the focus stayed on the rail button that opened the screen, and
        /// since a screen's content sits after the whole HUD in the Tab order, reaching it meant walking the rail, New
        /// Game, End Turn, the minimap's three buttons and star, report and share first: 16 stops for the Cabinet and
        /// the Budget, 14 for Defence. Close is already the other half of this, handing the focus back to the rail
        /// button, so open was the asymmetric one.
        ///
        /// The screen's own X is first in its order, because the header is its first child, so the focus goes to the
        /// first control of the screen's content instead and leaves the X one Shift+Tab back; Escape and the X itself
        /// are unchanged. A screen with no other control that can take the focus takes its X, which is better than
        /// leaving the focus outside the screen the player just opened.</summary>
        void HandFocusTo(VisualElement screen)
        {
            var x = closes.FirstOrDefault(b => screen.Contains(b));
            VisualElement? first = null;
            foreach (var e in screen.Query<VisualElement>().Build())
            {
                if (e == screen || e == x || !FocusReturn.CanTakeFocus(e)) continue;
                // Not a scroll bar: Defence's defence-body puts its scroller before any control of its own, and a bar is
                // no use from the keyboard (#41 K3, which took the Cabinet's bars out of the Tab order, and K6, which
                // took every other one out too). The skip stays: it is what keeps the landing control a real control
                // even while a bar is on screen.
                if (e is Scroller || e.GetFirstAncestorOfType<Scroller>() != null) continue;
                first = e;
                break;
            }
            FocusReturn.To(first ?? x);
        }
    }
}