A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
UI: a tapped meter tip closes on a tap on the bare map (Fixes #1184) (#1204)
Berif463de8604f6
diff --git a/changelog.d/1184.md b/changelog.d/1184.mdnew file mode 100644index 000000000..ea2f935d0--- /dev/null+++ b/changelog.d/1184.md@@ -0,0 +1,4 @@+### Fixed+- A tapped top-bar meter tip closes on a tap on the bare map too (#1184, follow-up to #1173): such a tap never reaches+ the UI panel, so MeterTip hears the press on the device itself and closes the tip when no element is under it. A tap+ on the UI and the mouse's hover work as before.diff --git a/unity/Assets/Scripts/UI/MeterTip.cs b/unity/Assets/Scripts/UI/MeterTip.csindex 3e1c98916..9390f9bbe 100644--- a/unity/Assets/Scripts/UI/MeterTip.cs+++ b/unity/Assets/Scripts/UI/MeterTip.cs@@ -25,6 +25,7 @@ namespace Statecraft.Game.UIreadonly EventCallback<PointerEnterEvent> onEnter; readonly EventCallback<PointerLeaveEvent> onLeave;readonly EventCallback<ClickEvent> onClick; readonly EventCallback<GeometryChangedEvent> onTipGeometry;readonly EventCallback<PointerDownEvent> onPress; readonly VisualElement pressScope;+ readonly UnityEngine.InputSystem.InputAction mapPress;readonly Action<IReadOnlyList<Statecraft.Sim.Rejection>> onTurn;VisualElement? hovered, pinned, shownFor;@@ -43,8 +44,8 @@ namespace Statecraft.Game.UIpinned = e.pointerType == UnityEngine.UIElements.PointerType.mouse ? null : pinned == m ? null : m;Refresh();};- // A tapped tip closes on the next tap anywhere: the panel sends a press with nothing under it (the map) to its root,- // so the press is heard there before any control takes it. A press on a meter is left to its click (move, close).+ // A tapped tip closes on the next tap anywhere: a press on the UI is heard at the panel's root before any control+ // takes it. A press on a meter is left to its click (move, close).onPress = e => { if (pinned == null || OnAMeter(e.target as VisualElement)) return; pinned = null; Refresh(); };onTipGeometry = _ => Place();onTurn = _ => { pinned = null; Refresh(); };@@ -56,10 +57,23 @@ namespace Statecraft.Game.UItip.RegisterCallback(onTipGeometry);pressScope = root.panel?.visualTree ?? root; // a HUD built off a panel (some tests) has no panel rootpressScope.RegisterCallback(onPress, TrickleDown.TrickleDown);+ // #1184: a press with no element under it (the map) never reaches the panel, as the event system sends a pointer+ // event only to a panel that picks an element there. The device's own press closes the tip then; a press on the UI+ // stays the panel's (onPress, the meters' clicks), or a second tap on the meter would close and reopen it.+ mapPress = new UnityEngine.InputSystem.InputAction("meter-tip-map-press", UnityEngine.InputSystem.InputActionType.Button, "<Pointer>/press");+ mapPress.performed += OnMapPress;+ mapPress.Enable();bridge.TurnEnded += onTurn;Hide();}+ void OnMapPress(UnityEngine.InputSystem.InputAction.CallbackContext ctx)+ {+ if (pinned == null || !(ctx.control.device is UnityEngine.InputSystem.Pointer p)) return;+ if (UiPointer.IsOverUi(tip.panel, p.position.ReadValue(), Screen.height)) return;+ pinned = null; Refresh();+ }+bool OnAMeter(VisualElement? e){foreach (var m in meters) if (e != null && (m == e || m.Contains(e))) return true;@@ -117,6 +131,7 @@ namespace Statecraft.Game.UImeters.Clear();tip.UnregisterCallback(onTipGeometry);pressScope.UnregisterCallback(onPress, TrickleDown.TrickleDown);+ mapPress.performed -= OnMapPress; mapPress.Dispose();bridge.TurnEnded -= onTurn;hovered = pinned = null;Hide();diff --git a/unity/Assets/Tests/EditMode/MeterTipTests.cs b/unity/Assets/Tests/EditMode/MeterTipTests.csindex a8ad392a9..14873cd13 100644--- a/unity/Assets/Tests/EditMode/MeterTipTests.cs+++ b/unity/Assets/Tests/EditMode/MeterTipTests.cs@@ -21,13 +21,13 @@ public class MeterTipTestsstatic void Enter(VisualElement e) { using var ev = PointerEnterEvent.GetPooled(new Event { type = EventType.MouseMove, mousePosition = e.worldBound.center }); ev.target = e; e.SendEvent(ev); }static void Leave(VisualElement e) { using var ev = PointerLeaveEvent.GetPooled(new Event { type = EventType.MouseMove, mousePosition = Vector2.zero }); ev.target = e; e.SendEvent(ev); }- static void Tap(VisualElement e) { using var ev = ClickEvent.GetPooled(); ev.target = e; e.SendEvent(ev); }+ internal static void Tap(VisualElement e) { using var ev = ClickEvent.GetPooled(); ev.target = e; e.SendEvent(ev); }static void MouseClick(VisualElement e) { using var ev = ClickEvent.GetPooled(new Event { type = EventType.MouseUp, mousePosition = e.worldBound.center }); ev.target = e; e.SendEvent(ev); }/// <summary>A finger going down on <paramref name="e"/>; on the map, with nothing under it, the panel sends it to its root.</summary>static void Press(VisualElement e) { using var ev = PointerDownEvent.GetPooled(new Touch { fingerId = 0, phase = TouchPhase.Began }); ev.target = e; e.SendEvent(ev); }/// <summary>Shown only when MeterTip set it so (an unset inline display reads as Flex, the default).</summary>- static bool Shown(VisualElement tip) => tip.style.display.keyword == StyleKeyword.Undefined && tip.style.display.value == DisplayStyle.Flex;+ internal static bool Shown(VisualElement tip) => tip.style.display.keyword == StyleKeyword.Undefined && tip.style.display.value == DisplayStyle.Flex;static string Words(VisualElement root) => root.Q<Label>("meter-tip-words").text;static string CausesLine(VisualElement root) => root.Q<Label>("meter-tip-causes").text;@@ -132,7 +132,7 @@ public class MeterTipTests}/// <summary>Renders Game.uxml at w x h with the HUD after one turn, then runs <paramref name="check"/>, which may wait frames.</summary>- static IEnumerator Rendered(int w, int h, bool hebrew, System.Func<VisualElement, SimBridge, UiText, IEnumerator> check)+ internal static IEnumerator Rendered(int w, int h, bool hebrew, System.Func<VisualElement, SimBridge, UiText, IEnumerator> check){var rt = new RenderTexture(w, h, 0);var ps = Object.Instantiate(AssetDatabase.LoadAssetAtPath<PanelSettings>(UiPaths.Panel));@@ -162,3 +162,42 @@ public class MeterTipTests}}}++/// <summary>#1184: a tap on the bare map never reaches the panel (the event system sends a pointer event only to a panel+/// that has an element under it), so MeterTip's press on the panel root does not hear it. A tapped tip still closes on it,+/// through the touchscreen itself; a tap on the UI is left to the panel (a second tap on the meter must not reopen it).+/// InputTestFixture runs the devices in edit mode; the edit-mode panel gets no pointer events at all, as the map's press.</summary>+public class MeterTipBareMapTests : UnityEngine.InputSystem.InputTestFixture+{+ [UnityTest]+ public IEnumerator ATappedTipClosesOnATapOnTheBareMap()+ {+ var touch = UnityEngine.InputSystem.InputSystem.AddDevice<UnityEngine.InputSystem.Touchscreen>();+ var problems = new List<string>();+ yield return MeterTipTests.Rendered(390, 844, false, Check);++ IEnumerator Check(VisualElement root, SimBridge bridge, UiText text)+ {+ var tip = root.Q(MeterTip.TipName);+ var money = root.Q("meter-treasury");+ // screen points (y up) with and without an element under them, by the same test as the map's clicks (UiPointer)+ Vector2? bare = null, onUi = null;+ for (int y = 0; y < Screen.height; y += 10)+ for (int x = 0; x < Screen.width; x += 10)+ {+ var p = new Vector2(x, y);+ if (UiPointer.IsOverUi(root.panel, p, Screen.height)) onUi ??= p; else bare ??= p;+ }+ if (bare == null || onUi == null) { problems.Add($"precondition: the {Screen.width}x{Screen.height} screen has bare map ({bare}) and UI ({onUi})"); yield break; }+ int id = 1;+ void TapAt(Vector2 at) { BeginTouch(id, at); EndTouch(id, at); id++; }+ MeterTipTests.Tap(money);+ if (!MeterTipTests.Shown(tip)) problems.Add("precondition: a tap pins the tip");+ TapAt(onUi.Value);+ if (!MeterTipTests.Shown(tip)) problems.Add("a touch on the UI closed the tip itself: the panel's press (or the meter's click) decides that");+ TapAt(bare.Value);+ if (MeterTipTests.Shown(tip)) problems.Add("a tap on the bare map does not close a tapped tip");+ }+ Assert.IsEmpty(problems, string.Join("\n", problems));+ }+}