A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix: a lit hex seen through the unit card takes the click (Refs #1617) (#1628)
Eyal Golan31ef9ebdbf5e
diff --git a/changelog.d/1617.md b/changelog.d/1617.mdnew file mode 100644index 000000000..2287e13d0--- /dev/null+++ b/changelog.d/1617.md@@ -0,0 +1,5 @@+### Fixed+- A click on a hex seen through the unit card while Move or Attack is picking now does what the same click on the map+ does: a lit hex gives the move or the attack, and an unlit hex with a route gives the go-to. Before, the card took the+ click and nothing happened. An unlit hex with no route stays blocked, the card's buttons keep their clicks, and with no+ pick open the card blocks the map as before (#1617, player report).diff --git a/unity/Assets/Scripts/Map/MapPointer.cs b/unity/Assets/Scripts/Map/MapPointer.csindex 97ba08a1f..a987e470f 100644--- a/unity/Assets/Scripts/Map/MapPointer.cs+++ b/unity/Assets/Scripts/Map/MapPointer.cs@@ -124,6 +124,10 @@ namespace Statecraft.Game.Map/// the hex of the terrain under the pointer. GameRoot also asks it for the hover.</summary>public (int q, int r)? HexAt(Vector2 pos) => HexAt(pos, IsOverUi != null && IsOverUi(pos), true);+ /// <summary>The hex a click at <paramref name="pos"/> would pick were no UI there (HexAt without the UI check).+ /// GameRoot asks it for a click on the unit card over a lit hex (#1617).</summary>+ public (int q, int r)? HexUnder(Vector2 pos) => HexAt(pos, false, true);+(int q, int r)? HexAt(Vector2 pos, bool overUi, bool pills){if (overUi) return null; // the UI is asked first, whatever else is missingdiff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex 0e3d00bca..dd2e81305 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -404,7 +404,12 @@ namespace Statecraft.Game.UIcameraController.Ground = CameraController.GroundOf(terrain);cameraController.GroundTop = CameraController.GroundTopOf(terrain);pointer.Map = map;- pointer.IsOverUi = overUi;+ // #1617: the lit hexes of a Move or Attack pick show through the unit card, so the map pointer also takes a click+ // on the card's text over one of them (InfoPanelPresenter.TakesClickThrough). The camera does not: no drag or wheel+ // through the card.+ Func<Vector2, bool> pointerOverUi = p => outcomes?.HoldsThePointer == true+ || (UiPointer.PickAt(root.panel, p, Screen.height) is { } picked && !info.TakesClickThrough(picked, () => pointer.HexUnder(p)));+ pointer.IsOverUi = pointerOverUi;Action<int, int> onHex = (q, r) =>{OnHexClicked(map, unitLayer, info, q, r, UnityEngine.InputSystem.Pointer.current is UnityEngine.InputSystem.Touchscreen);@@ -472,7 +477,7 @@ namespace Statecraft.Game.UIbridge.TurnEnded -= onTurn;pointer.HexClicked -= onHex;if (pointer.PillHex == pillHex) pointer.PillHex = null;- if (pointer.IsOverUi == overUi) pointer.IsOverUi = null;+ if (pointer.IsOverUi == pointerOverUi) pointer.IsOverUi = null;if (cameraController.IsOverUi == overUi) cameraController.IsOverUi = null;if (pointer.Map == map) pointer.Map = null;cameraController.Bounds = oldBounds;diff --git a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs b/unity/Assets/Scripts/UI/InfoPanelPresenter.csindex 9532f8c9c..151b01ec5 100644--- a/unity/Assets/Scripts/UI/InfoPanelPresenter.cs+++ b/unity/Assets/Scripts/UI/InfoPanelPresenter.cs@@ -42,6 +42,9 @@ namespace Statecraft.Game.UI/// load ends the pick, as the hexes change), and whether it is the attack's.</summary>(int unit, HashSet<(int q, int r)> hexes, GameSession session, int turn, bool attack, (int q, int r)? touchRoute)? pick;(int q, int r)? shownRoute;+ /// <summary>TakesClickThrough's last answer, by hex: the pointer asks it every frame it hovers the card, and a go-to's+ /// route is a whole-map search. Forgotten when the pick ends and on every render (an order or a new state).</summary>+ ((int q, int r) hex, bool takes)? clickThrough;/// <summary>A sim unit's name on screen, for the attack's targets (GameRoot: its token's card title); null, or no/// name, gives the hex's coordinates.</summary>readonly System.Func<int, string?>? unitName;@@ -295,6 +298,7 @@ namespace Statecraft.Game.UIif (pick == null) return;pick = null;shownRoute = null;+ clickThrough = null;showReachable?.Invoke(System.Array.Empty<(int q, int r)>());showRoute?.Invoke(System.Array.Empty<(int q, int r, int turn)>());}@@ -326,6 +330,22 @@ namespace Statecraft.Game.UIshowRoute?.Invoke(route);}+ /// <summary>#1617 (player report): the lit hexes of an open Move or Attack pick show through this card (its navy is+ /// translucent), so a click on the card over one of them is meant for the map. True when a pick is open,+ /// <paramref name="picked"/> (the UI element under the pointer) is this card or inside it but not inside one of its+ /// buttons, and the hex under the pointer (<paramref name="hexUnder"/>, asked only then) is one PickTarget takes: lit,+ /// or, for Move, an unlit hex with a route (go-to, #1398).</summary>+ public bool TakesClickThrough(VisualElement? picked, System.Func<(int q, int r)?> hexUnder)+ {+ if (!(pick is { } p) || picked == null || (picked != panel && !panel.Contains(picked))) return false;+ for (var e = picked; e != panel; e = e.parent) if (e is Button) return false;+ if (!(hexUnder() is { } h)) return false;+ if (clickThrough is { } known && known.hex == h) return known.takes;+ var takes = p.hexes.Contains(h) || RouteTo(p, h.q, h.r).Count > 0; // RouteTo: PickTarget's go-to rule+ clickThrough = (h, takes);+ return takes;+ }+/// <summary>A map click while the player picks a Move's or an Attack's hex (GameRoot.OnHexClicked asks first): a lit/// hex gives the Move or attack; an unlit hex with a route gives go-to. Touch previews that route on the first tap and/// confirms it on a second tap of the same hex. A hex with no target ends the pick and selects as usual.</summary>@@ -484,6 +504,7 @@ namespace Statecraft.Game.UIpublic void Render(){+ clickThrough = null;RenderLines();panel.EnableInClassList(GddClass, gdd.style.display.value == DisplayStyle.Flex);// A fact row shows only when its line has something to say (an empty row would leave a lone sign).diff --git a/unity/Assets/Scripts/UI/UiPointer.cs b/unity/Assets/Scripts/UI/UiPointer.csindex 33f1eb11a..0d49e29d6 100644--- a/unity/Assets/Scripts/UI/UiPointer.cs+++ b/unity/Assets/Scripts/UI/UiPointer.cs@@ -12,7 +12,11 @@ namespace Statecraft.Game.UIpublic static Vector2 ToPanel(IPanel panel, Vector2 screenYUp, float screenHeight) =>RuntimePanelUtils.ScreenToPanel(panel, new Vector2(screenYUp.x, screenHeight - screenYUp.y));+ /// <summary>The UI element under a screen point (y-up), or null where the map shows through.</summary>+ public static VisualElement? PickAt(IPanel? panel, Vector2 screenYUp, float screenHeight) =>+ panel?.Pick(ToPanel(panel, screenYUp, screenHeight));+public static bool IsOverUi(IPanel? panel, Vector2 screenYUp, float screenHeight) =>- panel != null && panel.Pick(ToPanel(panel, screenYUp, screenHeight)) != null;+ PickAt(panel, screenYUp, screenHeight) != null;}}diff --git a/unity/Assets/Tests/EditMode/GameSceneTests.cs b/unity/Assets/Tests/EditMode/GameSceneTests.csindex 0c09dc516..03d765629 100644--- a/unity/Assets/Tests/EditMode/GameSceneTests.cs+++ b/unity/Assets/Tests/EditMode/GameSceneTests.cs@@ -325,6 +325,84 @@ public class GameSceneTestsAssert.AreEqual(ground.z, b.center.z, 100f);}+ /// <summary>#1617 (player report on v0.2.1, a Retina MacBook): after Move the lit hexes show through the unit card (its+ /// navy is translucent), but a click on one there hit the card and nothing happened. While a Move pick is open, a click+ /// on the card's text over a lit hex reaches the map and queues the Move. The card's buttons keep their clicks, the card+ /// still holds the drag and the wheel, and with no pick open the card blocks the map as before.</summary>+ [UnityTest]+ public IEnumerator ALitHexSeenThroughTheUnitCardTakesTheClick()+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var doc = Object.FindFirstObjectByType<UIDocument>();+ var cam = Camera.main!;+ var rt = new RenderTexture(1920, 1080, 0);+ var camRt = new RenderTexture(1920, 1080, 24); // the camera fills the panel, as in the game (MapLabelsPresenter)+ var ps = Object.Instantiate(doc.panelSettings);+ ps.targetTexture = rt;+ try+ {+ doc.panelSettings = ps;+ cam.targetTexture = camRt;+ var map = Object.FindFirstObjectByType<MapScene>();+ map.Build(Inputs.Map);+ var root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ root.Begin(Inputs, 0);+ var ui = doc.rootVisualElement;+ HideTheOpeningCard(ui);+ var bridge = root.Bridge!; var c = bridge.Content;+ var layer = root.Units!;+ var golani = layer.Tokens.Single(p => p.Token.Unit.Id == "golani");+ var id = UnitCard.SimUnit(c, bridge.State, golani.Token.Unit)!.Value;+ GameRoot.OnHexClicked(map, layer, root.Info!, golani.Q, golani.R);+ Assert.IsTrue(root.Info!.ShowsUnit, "precondition: Golani's card");+ var move = ui.Q<Button>("info-move");+ UiTestKit.Press(move);+ // The lit hex farthest from every unit (a token takes a click for its own hex) and every settlement (a city's+ // name pill does), so the click can only be the land's.+ int Room((int q, int r) h) => bridge.State.Units.Values.Select(u => MapActions.Distance(u.Q, u.R, h.q, h.r))+ .Concat(c.SettlementHexes.Select(x => MapActions.Distance(x.Q, x.R, h.q, h.r))).Min();+ var lit = Movement.Reachable(c, bridge.State, id).Keys.Select(k => (k.Q, k.R)).OrderByDescending(Room).First();+ Assert.GreaterOrEqual(Room(lit), 1, "precondition: no unit or settlement on the lit hex");++ // Lay the card over the lit hex: the probe is the card's title, over the hex's middle.+ var card = ui.Q("info-panel");+ var panel = ui.panel; float h = Screen.height;+ var probe = AimAt(lit.Item1, lit.Item2);+ var at = UiPointer.ToPanel(panel, probe, h);+ card.style.left = at.x - 60f; card.style.top = at.y - 24f; card.style.bottom = StyleKeyword.Auto;+ for (int i = 0; i < 30 && !card.worldBound.Contains(at); i++) yield return null;+ yield return null;+ probe = AimAt(lit.Item1, lit.Item2);+ var picked = panel.Pick(at);+ Assert.IsTrue(picked != null && (picked == card || card.Contains(picked)), $"precondition: the probe is on the card ({picked?.name})");+ Assert.IsNull(picked!.GetFirstOfType<Button>(), $"precondition: the probe is on the card's text, not a button ({picked.name})");+ var pointer = Object.FindFirstObjectByType<MapPointer>();+ var camera = cam.GetComponent<CameraController>();++ // The card's buttons keep their clicks while the pick is open, wherever the lit hexes are.+ Vector2 o = UiPointer.ToPanel(panel, Vector2.zero, h), ex = UiPointer.ToPanel(panel, Vector2.right * 100, h) - o, ey = UiPointer.ToPanel(panel, Vector2.up * 100, h) - o;+ Vector2 ScreenFor(Vector2 p) => new Vector2((p - o).x / ex.x * 100, (p - o).y / ey.y * 100);+ Assert.IsTrue(pointer.IsOverUi!(ScreenFor(move.worldBound.center)), "the Move button keeps its click during the pick");++ Assert.IsFalse(pointer.IsOverUi!(probe), "a lit hex seen through the card takes the click");+ Assert.IsTrue(camera.IsOverUi!(probe), "the card still holds the drag and the wheel");+ Assert.IsTrue(pointer.HandleClick(probe), "the click picks the lit hex");+ Assert.AreEqual(lit, (bridge.State.Units[id].Q, bridge.State.Units[id].R), "the click moved Golani to the lit hex at once (#1398)");+ Assert.IsTrue(root.Info.ShowsUnit, "Golani's card stays");+ // A move with points left opens the pick again (#1398): end it, and the card blocks the map again.+ if (ui.Q<Label>("info-settlements").text.Contains(Text["unit.move_pick"])) UiTestKit.Press(move);+ Assert.IsTrue(pointer.IsOverUi!(probe), "with no pick open the card blocks the map again");+ }+ finally+ {+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);+ if (ps != null) Object.DestroyImmediate(ps);+ if (rt != null) { rt.Release(); Object.DestroyImmediate(rt); }+ if (camRt != null) { camRt.Release(); Object.DestroyImmediate(camRt); }+ }+ }+/// <summary>Since the GDD cutover the game opens on its first card, One Army (idf_founding, window from 15 May 1948 per/// #567: the order it asks about was signed on 26 May, before the next monthly turn), and its photograph covers the map. A/// player hides it to use the map; so do these tests.</summary>@@ -337,11 +415,16 @@ public class GameSceneTests/// <summary>Points the main camera straight down at Tel Aviv and returns Tel Aviv's screen position.</summary>static Vector2 AimAtTelAviv()+ {+ var tlv = Inputs.Content.SettlementHexes.Single(s => s.Id == "tel_aviv");+ return AimAt(tlv.Q, tlv.R);+ }++ /// <summary>Points the main camera straight down at the hex (q, r) and returns its screen position.</summary>+ static Vector2 AimAt(int q, int r){var map = Object.FindFirstObjectByType<MapScene>();- var c = Inputs.Content;- var tlv = c.SettlementHexes.Single(s => s.Id == "tel_aviv");- var hex = c.HexAt(tlv.Q, tlv.R)!;+ var hex = Inputs.Content.HexAt(q, r)!;var ground = map.Frame.ToWorld(hex.Lon, hex.Lat, 0);var cam = Camera.main!;cam.transform.position = new Vector3(ground.x, 50_000f, ground.z);diff --git a/unity/Assets/Tests/EditMode/UnitOrdersTests.cs b/unity/Assets/Tests/EditMode/UnitOrdersTests.csindex 1bacc46d6..19c501be6 100644--- a/unity/Assets/Tests/EditMode/UnitOrdersTests.cs+++ b/unity/Assets/Tests/EditMode/UnitOrdersTests.cs@@ -176,6 +176,51 @@ public class UnitOrdersTestsinfo.Dispose();}+ /// <summary>#1617: while a Move pick is open, a click on the unit card is the map's only where it falls on the card's+ /// text (not a button, not another panel) over a hex PickTarget takes: a lit hex, or an unlit hex with a go-to route+ /// (#1398); an unlit hex with no route stays blocked. The hex is asked only then. With no pick open, never.</summary>+ [Test]+ public void OnlyAHexTheMovePickTakesUnderTheCardsTextTakesAClickThroughTheCard()+ {+ using var ui = new UiTestKit();+ var g = GameSession.New(C, GoldenRun.Seed, Mode.Historical);+ var bridge = new SimBridge(g, Text);+ var golani = UnitRoster.FromState(Units, C, g.State, _ => true, (row, a, q, r) => a).Single(t => t.Unit.Id == "golani");+ PlacedToken? shown = new PlacedToken(golani, new AnchorSpot(0, 0, 0, 0, false));+ var info = new InfoPanelPresenter(ui.Root, bridge, Text, new Dictionary<string, string>(), selectedUnit: () => shown);+ info.ShowUnit();+ var reach = Movement.Reachable(C, g.State, UnitCard.SimUnit(C, g.State, golani.Unit)!.Value).Keys.ToList();+ (int q, int r)? lit = (reach[0].Q, reach[0].R);+ var id = UnitCard.SimUnit(C, g.State, golani.Unit)!.Value;+ var tlv = C.SettlementHexes.Single(t => t.Id == "tel_aviv");+ (int q, int r)? routable = (tlv.Q, tlv.R);+ Assert.IsFalse(reach.Contains((tlv.Q, tlv.R)), "precondition: Tel Aviv is out of Golani's reach this turn");+ Assert.IsNotEmpty(Movement.Route(C, g.State, id, tlv.Q, tlv.R, bridge.Spent), "precondition: a go-to route reaches Tel Aviv");+ var none = C.Hexes.First(h => !h.Land); // the sea: no route goes there+ Assert.IsEmpty(Movement.Route(C, g.State, id, none.Q, none.R, bridge.Spent), "precondition: no go-to route reaches the sea");+ (int q, int r)? unroutable = (none.Q, none.R);+ var title = ui.Q<Label>("info-title"); var move = ui.Q<Button>("info-move");+ var asked = 0;+ System.Func<(int q, int r)?> At((int q, int r)? hex) => () => { asked++; return hex; };+ Assert.IsFalse(info.TakesClickThrough(title, At(lit)), "no pick open");+ Assert.AreEqual(0, asked, "the hex is not asked with no pick open");+ UiTestKit.Press(move);+ Assert.IsTrue(info.TakesClickThrough(title, At(lit)), "the card's text over a lit hex");+ Assert.IsTrue(info.TakesClickThrough(ui.Q<VisualElement>("info-panel"), At(lit)), "the card's own background over a lit hex");+ Assert.IsTrue(info.TakesClickThrough(title, At(routable)), "the card's text over an unlit hex with a go-to route");+ Assert.IsFalse(info.TakesClickThrough(title, At(unroutable)), "the card's text over an unlit hex with no route");+ Assert.IsFalse(info.TakesClickThrough(title, At(null)), "the card's text off the map");+ asked = 0;+ Assert.IsFalse(info.TakesClickThrough(move, At(lit)), "a button keeps its click over a lit hex");+ Assert.IsFalse(info.TakesClickThrough(move, At(routable)), "and over a hex with a go-to route");+ Assert.IsFalse(info.TakesClickThrough(ui.Q<VisualElement>("top-bar"), At(lit)), "another panel keeps its click");+ Assert.IsFalse(info.TakesClickThrough(null, At(lit)), "no UI: the map has the click anyway");+ Assert.AreEqual(0, asked, "the hex is asked only for the card's text");+ UiTestKit.Press(move); // a second press ends the pick+ Assert.IsFalse(info.TakesClickThrough(title, At(lit)), "the pick has ended");+ info.Dispose();+ }+/// <summary>Slice 2 (#754): for every unit with a sim unit, Israel's and the enemy's, the card shows Fortify and Hold each/// only where the sim's Orders.Check takes it, and where it does not, on Israel's units the sim's reason, whatever it is/// (so a refusal the sim adds later, such as artillery's hold, shows with no change here); an enemy's card gives no reason