A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
fix: MapPointer ticks before the camera, and tests that drive its real frame (Refs #1661) (#1675)
EllaSharakanski01cb287b1190
diff --git a/changelog.d/1661-mappointer-tick.md b/changelog.d/1661-mappointer-tick.mdnew file mode 100644index 000000000..ce2eccedc--- /dev/null+++ b/changelog.d/1661-mappointer-tick.md@@ -0,0 +1,11 @@+### Fixed++- A one-finger pan lifted just after it crossed the slop is no longer lost (#1661 item 5). `MapPointer` now declares+ `[DefaultExecutionOrder(-100)]`, so its frame runs before `CameraController`'s and sets the press's `moved` before the+ camera asks whether the press may still be a tap. With no order declared, Unity was free to tick the camera first,+ which read that answer from the frame before, kept the drag in `HoldDrag` and then dropped it as a click's.++### Changed++- `MapPointer` gains an internal `Tick` test seam, as `CameraController` has had since #890, so an EditMode test can+ drive the real `Update` path instead of the handlers beneath it (#1661 item 6). No behaviour change.diff --git a/unity/Assets/Scripts/Map/MapPointer.cs b/unity/Assets/Scripts/Map/MapPointer.csindex 2b605da3f..c7e4b8170 100644--- a/unity/Assets/Scripts/Map/MapPointer.cs+++ b/unity/Assets/Scripts/Map/MapPointer.cs@@ -43,6 +43,15 @@ namespace Statecraft.Game.Map/// A right click (press and release inside the same slop) raises HexOrdered instead, selecting nothing. One TapPress per/// gesture decides both a click and the camera's pan (RightTapCandidate, TouchTapCandidate): the camera pans only once/// the press has left the slop, so a press is a click or a pan, never both (#1654 review).</summary>+ /// <remarks>Runs before <see cref="CameraController"/> (#1661 item 5). The camera asks this pointer whether a press+ /// may still be a tap. For a finger that answer is <see cref="TouchTapCandidate"/>, which only this component's own+ /// frame moves; <see cref="RightTapCandidate"/> is moved by the right button's action callbacks+ /// (<see cref="RightStep"/>) instead, so it does not depend on the order. With no order declared, Unity was free to+ /// tick the camera first, which then read the touch answer from the frame before: a one-finger drag lifted just+ /// after crossing the slop had its delta kept by CameraController.HoldDrag and then dropped, so the pan was lost.+ /// The order is the cause, so the order is what is fixed; HoldDrag's contract (a press is a click or a pan, never+ /// both, #1654) is left alone.</remarks>+ [DefaultExecutionOrder(-100)]public sealed class MapPointer : MonoBehaviour{public MapScene? Map;@@ -121,7 +130,12 @@ namespace Statecraft.Game.Mapwhile (rightClicks.Count > 0) HandleRightClick(rightClicks.Dequeue());}- void Update()+ void Update() => Tick();++ /// <summary>One frame of this pointer: the queued presses, then the primary finger's tap rule. Test seam (internal;+ /// the tests see it through InternalsVisibleTo), as CameraController.Tick is (#890): an EditMode test has no player+ /// loop, so it drives the real Update path instead of the handlers underneath it (#1661 item 6).</summary>+ internal void Tick(){ProcessPresses();var ts = Touchscreen.current;diff --git a/unity/Assets/Tests/EditMode/MapPointerTickTests.cs b/unity/Assets/Tests/EditMode/MapPointerTickTests.csnew file mode 100644index 000000000..d8fb05f60--- /dev/null+++ b/unity/Assets/Tests/EditMode/MapPointerTickTests.cs@@ -0,0 +1,176 @@+#nullable enable+using System.Collections.Generic;+using NUnit.Framework;+using Statecraft.Game.Map;+using UnityEngine;+using UnityEditor;+using UnityEngine.InputSystem;++/// <summary>#1661 items 4, 5 and 6: the real <c>MapPointer.Update</c> path, driven through its test seam+/// (<c>MapPointer.Tick</c>, as <c>CameraController.Tick</c> is, #890). The other fixtures reach+/// <c>ProcessPresses</c> and the handlers underneath <c>Update</c>, so nothing drove the frame itself (item 6).+/// <para>One thing to know when reading these: the fixture's <c>BeginTouch</c>, <c>MoveTouch</c> and <c>EndTouch</c>+/// default to <c>queueEventOnly: false</c>, so each call runs its own <c>InputSystem.Update</c>. A gesture written as+/// three plain calls therefore spans three input updates, and the press edge is gone before <c>Tick</c> runs. To put a+/// whole gesture inside one input update, every call but the last passes <c>queueEventOnly: true</c>.</para></summary>+public class MapPointerTickTests : InputTestFixture+{+ static void Send(Object o, string message) =>+ o.GetType().GetMethod(message, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.Invoke(o, null);++ sealed class Rig : System.IDisposable+ {+ public readonly GameObject Go;+ public readonly CameraRig View;+ public readonly CameraController Controller;+ public readonly MapPointer Pointer;+ /// <summary>Every position the pointer asked the UI about: one per click, order or tap it acted on.</summary>+ public readonly List<Vector2> Asked = new List<Vector2>();+ public Rig()+ {+ Go = new GameObject("MapPointerTick", typeof(UnityEngine.Camera));+ View = Go.AddComponent<CameraRig>();+ View.LookAt = new Vector3(500000, 0, 500000); View.Distance = 100000f;+ Controller = Go.AddComponent<CameraController>();+ Controller.Bounds = new Rect(0, 0, 1000000, 1000000);+ Pointer = Go.AddComponent<MapPointer>();+ var asked = Asked;+ Pointer.IsOverUi = p => { asked.Add(p); return true; }; // true: the decision is recorded, no map needed+ var pointer = Pointer;+ Controller.HoldsMouseDrag = () => pointer.RightTapCandidate; // as GameRoot wires them+ Controller.HoldsTouchDrag = () => pointer.TouchTapCandidate;+ Send(Controller, "Awake"); Send(Controller, "OnEnable"); Send(Pointer, "OnEnable");+ }+ /// <summary>One frame, in the order the attribute now guarantees: the pointer, then the camera.</summary>+ public void Frame() { Pointer.Tick(); Controller.Tick(0.016f); }+ /// <summary>A tap is decided at the end of the pointer's frame and only <em>enqueued</em> (OnPress), while+ /// ProcessPresses already ran at the top of that same frame, so a tap takes effect on the frame after the finger+ /// lifts. A mouse click does not pay this, since it enqueues from the action callback outside Tick.</summary>+ public void SettleFrame() { Frame(); }+ public void Dispose() { Send(Pointer, "OnDisable"); Send(Controller, "OnDisable"); Object.DestroyImmediate(Go); }+ }++ Mouse mouse = null!;++ [SetUp]+ public void Devices()+ {+ InputSystem.actions = AssetDatabase.LoadAssetAtPath<InputActionAsset>("Assets/InputSystem_Actions.inputactions");+ mouse = InputSystem.AddDevice<Mouse>();+ InputSystem.AddDevice<Touchscreen>();+ }++ // --- item 6: the frame itself -------------------------------------------------------------------------------++ /// <summary>The frame handles a queued press, at its own position, exactly once.</summary>+ [Test]+ public void TheFrameHandlesAQueuedPressOnceThroughTheRealPath()+ {+ using var rig = new Rig();+ rig.Pointer.OnPress(new Vector2(800, 450));+ rig.Frame();+ CollectionAssert.AreEqual(new[] { new Vector2(800, 450) }, rig.Asked, "the frame handled the press at its own position");+ rig.Frame();+ Assert.AreEqual(1, rig.Asked.Count, "a press is handled once, not again on the next frame");+ }++ /// <summary>A tap spanning frames selects where the finger landed, driven through the frame.</summary>+ [Test]+ public void ATapOverTwoFramesSelectsWhereTheFingerLanded()+ {+ using var rig = new Rig();+ var at = new Vector2(600, 300);+ BeginTouch(1, at);+ rig.Frame();+ Assert.IsEmpty(rig.Asked, "nothing while the finger is down");+ EndTouch(1, at + new Vector2(2, -1));+ rig.Frame();+ Assert.IsEmpty(rig.Asked, "the tap is decided on the release frame but only queued");+ rig.SettleFrame();+ CollectionAssert.AreEqual(new[] { at }, rig.Asked, "a 2 px wiggle is a tap, at where the finger landed");+ }++ // --- item 4: a whole gesture inside one frame --------------------------------------------------------------+ // The mouse half of item 4 ("no pan") is deliberately absent. A test asserting that a right press, a 300 px move+ // and a release inside one frame leaves LookAt alone passes on its own and fails in the full suite, where the+ // camera had panned to (503876.60, 0, 499869.60). So "it does not pan" is not a stable property of the system here,+ // and a test asserting it would be order-dependent. Reported on #1661 instead of pinned; the no-order half is+ // already covered by RightClickOrPanTests.ARightDragOutAndBackInsideOneFrameIsNoClick.++ /// <summary>Item 4's touch half, pinning what happens **today**: a flick whose press, travel and release all land+ /// in one input update comes back as a <em>tap</em>, and at the release point rather than where the finger landed.+ /// <para>Why. <c>Tick</c> samples the touch once per frame, and <c>TapPress.Step</c> sets <c>start</c> from that+ /// same sample on the press edge, so <c>position - start</c> is zero and <c>moved</c> never latches however far the+ /// finger travelled. The 200 px of travel is invisible, and the one position the frame saw is the last one.</para>+ /// <para>The warming frame matters: <c>ButtonControl.wasPressedThisFrame</c> takes an old path on its first call+ /// ever and can miss a fast press (ButtonControl.cs:287-293, and its own remarks). Without it this test passed for+ /// the wrong reason, with the gesture not seen at all. In the game <c>Tick</c> queries every frame, so the control+ /// is warm, and this is what a real one-update flick would do. Reported on #1661; the fix would mean reading the+ /// touch's travel or phase rather than one sampled position, which changes the tap rule.</para></summary>+ [Test]+ public void AFlickInsideOneInputUpdateIsATapAtTheReleasePointToday()+ {+ using var rig = new Rig();+ var start = rig.View.LookAt;+ var from = new Vector2(400, 400);+ rig.Frame(); // warms press.wasPressedThisFrame: its first call ever takes ButtonControl's old path+ BeginTouch(1, from, queueEventOnly: true);+ MoveTouch(1, from + new Vector2(200, 0), queueEventOnly: true);+ EndTouch(1, from + new Vector2(200, 0));+ rig.Frame();+ rig.SettleFrame();+ CollectionAssert.AreEqual(new[] { from + new Vector2(200, 0) }, rig.Asked,+ "today a one-update flick taps, and at the release point: the travel is invisible to the slop");+ Assert.AreEqual(start, rig.View.LookAt, "and it does not pan");+ }++ /// <summary>The zero-travel control, and the reason the test above is a finding rather than an artefact: the same+ /// one-update press and release with the finger still selects where it was pressed, which is correct. So the+ /// difference between the two is the 200 px of travel being lost, not the gesture being dropped.</summary>+ [Test]+ public void AStillPressInsideOneInputUpdateSelectsWhereItWasPressed()+ {+ using var rig = new Rig();+ var start = rig.View.LookAt;+ var at = new Vector2(500, 350);+ rig.Frame(); // same warming as above+ BeginTouch(1, at, queueEventOnly: true);+ EndTouch(1, at);+ rig.Frame();+ rig.SettleFrame();+ CollectionAssert.AreEqual(new[] { at }, rig.Asked, "a still press and release in one update selects, correctly");+ Assert.AreEqual(start, rig.View.LookAt, "and a tap never pans");+ }++ // --- item 5: the order, now declared ------------------------------------------------------------------------++ /// <summary>Item 5's fix, pinned at its cause: the pointer's frame must run before the camera's, because the camera+ /// asks it whether a press may still be a tap and only the pointer's frame moves that state.</summary>+ [Test]+ public void ThePointerTicksBeforeTheCamera()+ {+ int Order<T>() => System.Attribute.GetCustomAttribute(typeof(T), typeof(DefaultExecutionOrder)) is DefaultExecutionOrder o ? o.order : 0;+ Assert.Less(Order<MapPointer>(), Order<CameraController>(),+ "MapPointer must tick first, or a drag lifted just after crossing the slop loses its pan (#1661 item 5)");+ }++ /// <summary>Item 5, the behaviour the order protects: a one-finger drag that crosses the slop and is lifted on the+ /// very next frame still pans. In that order the pointer sets moved first, so HoldDrag replays the kept delta+ /// instead of dropping it as a click's.</summary>+ [Test]+ public void APanLiftedRightAfterCrossingTheSlopIsNotLost()+ {+ using var rig = new Rig();+ var start = rig.View.LookAt;+ var from = new Vector2(700, 400);+ BeginTouch(1, from);+ rig.Frame();+ MoveTouch(1, from + new Vector2(TapPress.SlopPixels + 40, 0));+ rig.Frame();+ EndTouch(1, from + new Vector2(TapPress.SlopPixels + 40, 0));+ rig.Frame();+ rig.SettleFrame();+ Assert.AreNotEqual(start, rig.View.LookAt, "the pan is not lost when the finger lifts right after the slop");+ Assert.IsEmpty(rig.Asked, "a drag past the slop never taps");+ }+}diff --git a/unity/Assets/Tests/EditMode/MapPointerTickTests.cs.meta b/unity/Assets/Tests/EditMode/MapPointerTickTests.cs.metanew file mode 100644index 000000000..be9d8a12f--- /dev/null+++ b/unity/Assets/Tests/EditMode/MapPointerTickTests.cs.meta@@ -0,0 +1,11 @@+fileFormatVersion: 2+guid: 4235e4ebcb124bb791f6b620e8118fb3+MonoImporter:+ externalObjects: {}+ serializedVersion: 2+ defaultReferences: []+ executionOrder: 0+ icon: {instanceID: 0}+ userData:+ assetBundleName:+ assetBundleVariant: