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: the meter tip hides when the mouse leaves; a tapped tip closes on the next tap (Fixes #1108) (#1173)

Beribb1290154da4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d95e21292..8100e5cfb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -58,6 +58,8 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
geometry event (a per-frame poll); desktops and notch-free phones are unchanged.
With a side inset, raised meter captions reflow into the existing two-row bar when their measured widths no longer
fit; Security's level wraps with its caption, and the 12 px text floor is preserved.
+- A top-bar number's tip no longer stays open after the mouse leaves it: a mouse click no longer pins it (the mouse
+ hovers), and a tip pinned by a tap closes on the next tap anywhere, on the map or on another control (Fixes #1108).
### Added
- The sim's end report, `EndReport.For`, reads a finished game for the end screen: the outcome, the score, each
diff --git a/unity/Assets/Scripts/UI/MeterTip.cs b/unity/Assets/Scripts/UI/MeterTip.cs
index f8add9b2a..3e1c98916 100644
--- a/unity/Assets/Scripts/UI/MeterTip.cs
+++ b/unity/Assets/Scripts/UI/MeterTip.cs
@@ -9,10 +9,11 @@ namespace Statecraft.Game.UI
/// <summary>Each top-bar number explains itself (#1037): while the pointer is over a meter, or after a tap on it (a
/// touchscreen has no hover), a tip under the bar gives the meter in words (its tooltip, HudModel.Spoken: the value
/// and this turn's change) and the turn's main causes, which are the sim's own messages (HudModel.Causes). A second
- /// tap on the same meter closes it, a tap on another moves it, and End Turn closes it. It shows only while the meter
- /// can take the pointer, so a report or an outcome clip over the bar hides it. The tip is centred under the meter
- /// and kept inside the window. Nothing is computed here; the tip takes no clicks. Its two labels are the meter's words
- /// in the UI's language and the causes, which the sim gives in English (#55), so each takes its own direction.</summary>
+ /// tap on the same meter closes it, a tap on another moves it, and a tap anywhere else or End Turn closes it; a mouse
+ /// click pins nothing, as the mouse hovers (#1108). It shows only while the meter can take the pointer, so a report or
+ /// an outcome clip over the bar hides it. The tip is centred under the meter and kept inside the window. Nothing is
+ /// computed here; the tip takes no clicks. Its two labels are the meter's words in the UI's language and the causes,
+ /// which the sim gives in English (#55), so each takes its own direction.</summary>
public sealed class MeterTip : IDisposable
{
public const string TipName = "meter-tip";
@@ -23,6 +24,7 @@ namespace Statecraft.Game.UI
readonly List<VisualElement> meters = new List<VisualElement>();
readonly EventCallback<PointerEnterEvent> onEnter; readonly EventCallback<PointerLeaveEvent> onLeave;
readonly EventCallback<ClickEvent> onClick; readonly EventCallback<GeometryChangedEvent> onTipGeometry;
+ readonly EventCallback<PointerDownEvent> onPress; readonly VisualElement pressScope;
readonly Action<IReadOnlyList<Statecraft.Sim.Rejection>> onTurn;
VisualElement? hovered, pinned, shownFor;
@@ -32,7 +34,18 @@ namespace Statecraft.Game.UI
words = tip.Q<Label>("meter-tip-words"); causesLine = tip.Q<Label>("meter-tip-causes");
onEnter = e => { hovered = (VisualElement)e.currentTarget; Refresh(); };
onLeave = e => { if (hovered == e.currentTarget) hovered = null; Refresh(); };
- onClick = e => { var m = (VisualElement)e.currentTarget; pinned = pinned == m ? null : m; Refresh(); };
+ // #1108: a mouse click pinned the tip, so it outlived the pointer's leave; the mouse has hover, only a tap pins. A
+ // click still drops a tap's pin (a touchscreen laptop), as the press handler leaves presses on meters alone.
+ // (PointerType is spelled out: UnityEngine.PointerType has the same name.)
+ onClick = e =>
+ {
+ var m = (VisualElement)e.currentTarget;
+ pinned = 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).
+ onPress = e => { if (pinned == null || OnAMeter(e.target as VisualElement)) return; pinned = null; Refresh(); };
onTipGeometry = _ => Place();
onTurn = _ => { pinned = null; Refresh(); };
foreach (var m in meters)
@@ -41,10 +54,18 @@ namespace Statecraft.Game.UI
this.meters.Add(m);
}
tip.RegisterCallback(onTipGeometry);
+ pressScope = root.panel?.visualTree ?? root; // a HUD built off a panel (some tests) has no panel root
+ pressScope.RegisterCallback(onPress, TrickleDown.TrickleDown);
bridge.TurnEnded += onTurn;
Hide();
}
+ bool OnAMeter(VisualElement? e)
+ {
+ foreach (var m in meters) if (e != null && (m == e || m.Contains(e))) return true;
+ return false;
+ }
+
/// <summary>The meter the tip is shown for, or null.</summary>
public VisualElement? ShownFor => shownFor;
@@ -95,6 +116,7 @@ namespace Statecraft.Game.UI
foreach (var m in meters) { m.UnregisterCallback(onEnter); m.UnregisterCallback(onLeave); m.UnregisterCallback(onClick); }
meters.Clear();
tip.UnregisterCallback(onTipGeometry);
+ pressScope.UnregisterCallback(onPress, TrickleDown.TrickleDown);
bridge.TurnEnded -= onTurn;
hovered = pinned = null;
Hide();
diff --git a/unity/Assets/Tests/EditMode/MeterTipTests.cs b/unity/Assets/Tests/EditMode/MeterTipTests.cs
index d1fabe70e..a8ad392a9 100644
--- a/unity/Assets/Tests/EditMode/MeterTipTests.cs
+++ b/unity/Assets/Tests/EditMode/MeterTipTests.cs
@@ -22,6 +22,9 @@ public class MeterTipTests
static 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); }
+ 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;
@@ -95,6 +98,39 @@ public class MeterTipTests
Assert.IsEmpty(problems, string.Join("\n", problems));
}
+ /// <summary>#1108: a mouse has hover, so a click on a number does not pin its tip and leaving hides it; a tapped tip closes
+ /// on the next tap anywhere (the map, another control), while a tap on a number still moves or closes it.</summary>
+ [UnityTest]
+ public IEnumerator AClickedTipHidesOnLeaveAndATappedTipOnTheNextTapAnywhere()
+ {
+ var problems = new List<string>();
+ yield return Rendered(1280, 720, false, Check);
+
+ IEnumerator Check(VisualElement root, SimBridge bridge, UiText text)
+ {
+ var tip = root.Q(MeterTip.TipName);
+ VisualElement money = root.Q("meter-treasury"), security = root.Q("meter-security");
+ Enter(money); MouseClick(money); Leave(money);
+ if (Shown(tip)) problems.Add("a mouse click keeps the tip after the pointer leaves");
+ foreach (var (where, e) in new[] { ("the map", root.panel.visualTree), ("another control", root.Q("end-turn")) })
+ {
+ Tap(money);
+ if (!Shown(tip)) { problems.Add($"precondition: a tap shows the tip (before {where})"); continue; }
+ Press(e);
+ if (Shown(tip)) problems.Add($"a tap on {where} does not close a tapped tip");
+ }
+ Tap(money);
+ Press(security); Tap(security);
+ if (!Shown(tip) || !Words(root).StartsWith(security.tooltip)) problems.Add($"a tap on another number does not move the tip: '{Words(root)}'");
+ Press(security); Tap(security);
+ if (Shown(tip)) problems.Add("a second tap on the number does not close the tip");
+ Tap(money); Press(security); MouseClick(security);
+ if (Shown(tip)) problems.Add("a mouse click on a number does not drop a tap's pin (a touchscreen laptop)");
+ yield break;
+ }
+ Assert.IsEmpty(problems, string.Join("\n", problems));
+ }
+
/// <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)
{