A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Map: a fast pinch is a pinch, not a pan or a tap (Fixes #1792) (#1795)
EllaSharakanskia88bd5a35aad
diff --git a/changelog.d/1792-pinch-one-update.md b/changelog.d/1792-pinch-one-update.mdnew file mode 100644index 000000000..5fe45fe7e--- /dev/null+++ b/changelog.d/1792-pinch-one-update.md@@ -0,0 +1,4 @@+### Fixed++- A very fast pinch no longer pans the map or selects a hex: a second finger that touches and lifts between two+ input samples is now counted.diff --git a/unity/Assets/Scripts/Map/CameraController.cs b/unity/Assets/Scripts/Map/CameraController.csindex 553d3b4af..d522f860c 100644--- a/unity/Assets/Scripts/Map/CameraController.cs+++ b/unity/Assets/Scripts/Map/CameraController.cs@@ -148,15 +148,27 @@ namespace Statecraft.Game.Mapvar ts = Touchscreen.current;if (ts != null){+ // A second finger that pressed and lifted inside this update is already up when FingersDown samples, so+ // a pinch would read as a one-finger drag and pan the map (#1792). Count the presses as well, and when+ // two fingers touched, this frame is neither a drag nor a zoom: there is no live second finger to+ // measure a pinch against, and guessing one from a finger that is gone would be worse than waiting.+ var touched = 0;+ foreach (var t in ts.touches)+ if (DownThisUpdate(t)) touched++;switch (FingersDown(ts, out var a, out var b)){// one finger drags the map; its press observer latches the UI origin- case 1:+ case 1 when touched < 2:drag += HoldsTouchDrag != null ? touchDrag.Take(HoldsTouchDrag()) : a.delta.ReadValue();fromUi |= touchDrag.FromUi;down = true;pointer = a.position.ReadValue();break;+ // one finger left, but two touched this update: a pinch, so neither a drag nor a zoom, and the+ // travel the press observer recorded must not arrive as a late pan on the next frame (#1795 review)+ case 1:+ touchDrag.Discard();+ break;// a pinch zooms by how far the fingers move; the UI check follows the first fingercase 2:touchDrag.Discard();@@ -209,7 +221,20 @@ namespace Statecraft.Game.Map}}- /// <summary>The first two touches down (2 stands for two or more), or fewer.</summary>+ /// <summary>Whether a finger was down at any point in this input update: pressed now, or pressed during the+ /// update even though it has already lifted. <c>isPressed</c> alone is level state read once a frame, so a+ /// finger whose whole press and release arrive between two samples is invisible to it and a pinch replays as a+ /// one-finger pan or lets the thumb select (#1792). The press edge survives that, which is also how+ /// <c>MapPointer.Tick</c> reads the primary finger's own press.</summary>+ /// <remarks>Same class as #1661's one-update flick (#1777): a gesture that opens and closes between two+ /// samples cannot be seen by level state, only by its edges.</remarks>+ public static bool DownThisUpdate(UnityEngine.InputSystem.Controls.TouchControl t) =>+ t.press.isPressed || t.press.wasPressedThisFrame;++ /// <summary>The first two touches down <em>now</em> (2 stands for two or more), or fewer. Level state on+ /// purpose: a finger that has already lifted contributes no ongoing drag, so counting it here would turn a+ /// one-update flick into a pan. Whether a finger merely touched during the update is a separate question,+ /// asked by <see cref="DownThisUpdate"/> at the call site (#1792).</summary>static int FingersDown(Touchscreen ts, out UnityEngine.InputSystem.Controls.TouchControl a,out UnityEngine.InputSystem.Controls.TouchControl b){diff --git a/unity/Assets/Scripts/Map/MapPointer.cs b/unity/Assets/Scripts/Map/MapPointer.csindex 39d4a6857..e853b82e1 100644--- a/unity/Assets/Scripts/Map/MapPointer.cs+++ b/unity/Assets/Scripts/Map/MapPointer.cs@@ -180,10 +180,10 @@ namespace Statecraft.Game.Mapvar t = ts.primaryTouch;var pressed = 0;foreach (var touch in ts.touches)- if (touch.press.isPressed) pressed++;+ if (CameraController.DownThisUpdate(touch)) pressed++; // counts a finger that already lifted this update (#1792)// any other finger down cancels the tap (TapPress): a releasing pinch's thumb is inside the slop, yet must not selectvar tapAt = tap.Step(t.press.wasPressedThisFrame, t.press.wasReleasedThisFrame,- t.position.ReadValue(), pressed > (t.press.isPressed ? 1 : 0), t.delta.ReadValue());+ t.position.ReadValue(), pressed > (CameraController.DownThisUpdate(t) ? 1 : 0), t.delta.ReadValue());if (tapAt.HasValue) OnPress(tapAt.Value); // a tap selects where the finger landed, like a click}diff --git a/unity/Assets/Tests/EditMode/MapPointerTickTests.cs b/unity/Assets/Tests/EditMode/MapPointerTickTests.csindex fa6f1edfe..705c7b930 100644--- a/unity/Assets/Tests/EditMode/MapPointerTickTests.cs+++ b/unity/Assets/Tests/EditMode/MapPointerTickTests.cs@@ -197,4 +197,85 @@ public class MapPointerTickTests : InputTestFixtureAssert.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");}++ // --- #1792: a second finger whose whole press lands inside one input update ---------------------------------++ /// <summary>#1792: a pinch whose second finger presses, travels and lifts inside one input update must still cancel+ /// the thumb's tap. This is <c>MapPointer.cs</c>'s own promise - "a releasing pinch's thumb is inside the slop, yet+ /// must not select" - and before the fix the thumb selected at (400, 400).+ /// <para>Why it broke. <c>Tick</c> counted the fingers from level state (<c>if (touch.press.isPressed)</c>), so a+ /// finger already up when the frame sampled was never counted: <c>otherFingerDown</c> stayed false,+ /// <c>TapPress.pinched</c> never latched, and the still thumb ended as a clean tap. The count now asks+ /// <c>CameraController.DownThisUpdate</c>, which also takes the press edge. Same class as #1661's one-update flick+ /// (#1777): a gesture that opens and closes between two samples is invisible to level state.</para></summary>+ [Test]+ public void APinchWhoseSecondFingerLivesInsideOneUpdateRefusesTheThumbsTap()+ {+ using var rig = new Rig();+ var thumb = new Vector2(400, 400);+ rig.Frame(); // warms press.wasPressedThisFrame (see the flick test above)+ BeginTouch(1, thumb);+ rig.Frame();+ Assert.IsEmpty(rig.Asked, "nothing while the thumb alone is down");+ // the second finger's whole life - press, 200 px of travel, release - inside one input update+ BeginTouch(2, new Vector2(700, 400), queueEventOnly: true);+ MoveTouch(2, new Vector2(900, 400), queueEventOnly: true);+ EndTouch(2, new Vector2(900, 400));+ rig.Frame();+ EndTouch(1, thumb);+ rig.Frame();+ rig.SettleFrame();+ Assert.IsEmpty(rig.Asked, "the thumb of a pinch must not select, however briefly the other finger was down");+ }++ /// <summary>#1792's other half, the one the issue names: the same invisible second finger, but with the thumb+ /// <em>moving</em>. The camera's own finger count skipped on <c>!t.press.isPressed</c> too, so it saw one finger,+ /// took <c>case 1</c> and panned by the thumb's delta instead of taking <c>case 2</c> and zooming - the map's+ /// centre moved 16 800 world units before the fix. A pinch must not replay as a one-finger pan.</summary>+ [Test]+ public void APinchWhoseSecondFingerLivesInsideOneUpdateDoesNotPan()+ {+ using var rig = new Rig();+ var start = rig.View.LookAt;+ var thumb = new Vector2(400, 400);+ rig.Frame();+ BeginTouch(1, thumb);+ rig.Frame();+ // all four events in ONE input update: every call but the last passes queueEventOnly, or it flushes its own+ // update and the thumb's drag would land in a later one, where there really is only one finger+ BeginTouch(2, new Vector2(700, 400), queueEventOnly: true);+ MoveTouch(2, new Vector2(900, 400), queueEventOnly: true);+ EndTouch(2, new Vector2(900, 400), queueEventOnly: true);+ MoveTouch(1, thumb + new Vector2(0, -TapPress.SlopPixels - 40)); // the thumb drags well past the slop+ rig.Frame();+ Assert.AreEqual(start, rig.View.LookAt, "a pinch must not pan the map, however briefly the other finger was down");+ // ...and it must not arrive a frame late either (#1795 review). Skipping case 1 for one frame is enough today,+ // because the drag is read from the finger's delta and that delta is gone next frame. On the #1661 camera lane+ // the drag comes from travel recorded at the events, so a deferred pan would land here and a single assertion+ // on the pinch frame alone would not notice.+ rig.Frame();+ Assert.AreEqual(start, rig.View.LookAt, "and the pan does not arrive on the frame after the pinch either");+ }++ /// <summary>The control, and the reason the test above is a finding rather than an artefact: the same gesture with+ /// the second finger down long enough to be sampled does latch the pinch, and the thumb selects nothing. So the+ /// difference between the two is the second finger being invisible to the sample, not the thumb being special.</summary>+ [Test]+ public void APinchWhoseSecondFingerSpansFramesCorrectlyRefusesTheThumbsTap()+ {+ using var rig = new Rig();+ var thumb = new Vector2(400, 400);+ rig.Frame();+ BeginTouch(1, thumb);+ rig.Frame();+ BeginTouch(2, new Vector2(700, 400));+ rig.Frame(); // sampled with both fingers down: otherFingerDown is true here+ EndTouch(2, new Vector2(900, 400));+ rig.Frame();+ EndTouch(1, thumb);+ rig.Frame();+ rig.SettleFrame();+ Assert.IsEmpty(rig.Asked, "a pinch's thumb selects nothing when the second finger was sampled");+ }+}