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.

fix(input): every click counts once, at its own position (playtest 2, P2-4)

eyalgolan57edd4192ef1
diff --git a/unity/Assets/Scripts/Boot/InputConfig.cs b/unity/Assets/Scripts/Boot/InputConfig.cs
new file mode 100644
index 000000000..3293b6025
--- /dev/null
+++ b/unity/Assets/Scripts/Boot/InputConfig.cs
@@ -0,0 +1,18 @@
+#nullable enable
+using UnityEngine;
+using UnityEngine.InputSystem;
+
+namespace Statecraft.Game.Boot
+{
+ /// <summary>Input System settings the game needs (playtest 2, P2-4). Redundant-event merging is turned off: with it on,
+ /// a burst of mouse events in one frame (a slow frame, a quick double click, a click and then another click elsewhere)
+ /// can merge a button release into the move that follows it (FastMouse.MergeForward; its skip rule protects only the
+ /// event right after a button change). The release then reaches the UI at the next click's position, so the first
+ /// button's click is cancelled: a screen toggle or an option is lost while the next click goes through. With merging
+ /// off every press and release keeps its own position; the cost is one action update per mouse event.</summary>
+ public static class InputConfig
+ {
+ [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
+ public static void Apply() => InputSystem.settings.disableRedundantEventsMerging = true;
+ }
+}
diff --git a/unity/Assets/Scripts/Boot/InputConfig.cs.meta b/unity/Assets/Scripts/Boot/InputConfig.cs.meta
new file mode 100644
index 000000000..c612637ee
--- /dev/null
+++ b/unity/Assets/Scripts/Boot/InputConfig.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 0ef26ee7b0d0d40c693f26917c31bb81
\ No newline at end of file
diff --git a/unity/Assets/Scripts/Map/MapPointer.cs b/unity/Assets/Scripts/Map/MapPointer.cs
index 1bd07b2d7..9d317466f 100644
--- a/unity/Assets/Scripts/Map/MapPointer.cs
+++ b/unity/Assets/Scripts/Map/MapPointer.cs
@@ -1,5 +1,6 @@
#nullable enable
using System;
+using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
@@ -13,18 +14,48 @@ namespace Statecraft.Game.Map
public Func<Vector2, bool>? IsOverUi;
public event Action<int, int>? HexClicked;
- void Update()
+ // Every press, at the position of its own input event (InputConfig keeps a release from merging into the next move),
+ // queued and evaluated in order in Update: several presses in one slow frame are each checked where they happened,
+ // never all at the frame's last pointer position (playtest 2, P2-4).
+ readonly Queue<Vector2> presses = new Queue<Vector2>();
+ InputAction? press;
+
+ void OnEnable()
+ {
+ press = new InputAction("map-press", InputActionType.Button, "<Mouse>/leftButton");
+ press.performed += OnPressed;
+ press.Enable();
+ }
+
+ void OnDisable()
+ {
+ if (press != null) { press.performed -= OnPressed; press.Dispose(); press = null; }
+ presses.Clear();
+ }
+
+ void OnPressed(InputAction.CallbackContext ctx)
+ {
+ if (ctx.control.device is Pointer p) OnPress(p.position.ReadValue()); // read during the event: its own position
+ }
+
+ /// <summary>Queues a press at <paramref name="at"/> (screen pixels, y up).</summary>
+ internal void OnPress(Vector2 at) => presses.Enqueue(at);
+
+ /// <summary>Handles every queued press, in order, each at its own position.</summary>
+ internal void ProcessPresses()
{
- var mouse = Mouse.current;
- if (mouse == null || !mouse.leftButton.wasPressedThisFrame) return;
- HandleClick(mouse.position.ReadValue());
+ while (presses.Count > 0) HandleClick(presses.Dequeue());
}
+ void Update() => ProcessPresses();
+
/// <summary>A left click at <paramref name="pos"/> (screen pixels): ignored over the UI (the same IsOverUi the camera
/// uses), otherwise the hex under the pointer is raised as HexClicked. Returns whether a hex was picked.</summary>
public bool HandleClick(Vector2 pos)
{
- if (IsOverUi != null && IsOverUi(pos)) return false; // the UI is asked first, whatever else is missing
+ var overUi = IsOverUi != null && IsOverUi(pos);
+ Boot.DiagLog.NavLine(FormattableString.Invariant($"map click at ({pos.x:0}, {pos.y:0}) overUi={overUi}"));
+ if (overUi) return false; // the UI is asked first, whatever else is missing
if (Map == null || Map.Terrain == null) return false;
var cam = Camera.main;
var collider = Map.Terrain.GetComponent<TerrainCollider>();
diff --git a/unity/Assets/Tests/EditMode/InputMergingTests.cs b/unity/Assets/Tests/EditMode/InputMergingTests.cs
new file mode 100644
index 000000000..1512be947
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/InputMergingTests.cs
@@ -0,0 +1,66 @@
+#nullable enable
+using System.Collections.Generic;
+using NUnit.Framework;
+using Statecraft.Game.Boot;
+using UnityEngine;
+using UnityEngine.InputSystem;
+using UnityEngine.InputSystem.LowLevel;
+
+/// <summary>Playtest 2, P2-4, root cause (reproduced headed: same-frame CDP clicks under ?navlog, raw Input System events
+/// logged): when a move, a click and a second click land in one update, the Input System's redundant-event merging
+/// (FastMouse.MergeForward; the skip rule protects only the event right after a button change) merges the first click's
+/// release into the move that follows it. The release then reaches the UI at the second click's position, the button
+/// cancels its click, and the toggle is lost while the second click goes through. InputConfig turns merging off.</summary>
+public class InputMergingTests
+{
+ [Test]
+ public void AReleaseInABurstKeepsItsOwnPosition()
+ {
+ var old = InputSystem.settings.disableRedundantEventsMerging;
+ // By layout name, as a real mouse is created: the precompiled FastMouse, the device that merges events.
+ var mouse = (Mouse)InputSystem.AddDevice("Mouse", "P24Mouse");
+ Assert.AreEqual("FastMouse", mouse.GetType().Name, "a merging mouse, as browsers and desktops get");
+ var seen = new List<(float left, Vector2 at)>();
+ System.Action<InputEventPtr, InputDevice> listen = (e, d) =>
+ {
+ if (d == mouse && e.IsA<StateEvent>()) seen.Add((mouse.leftButton.ReadValueFromEvent(e), mouse.position.ReadValueFromEvent(e)));
+ };
+ InputSystem.onEvent += listen; // the events that survive merging, as the actions and the UI then see them
+ try
+ {
+ InputConfig.Apply();
+ Vector2 start = new Vector2(100, 100), why = new Vector2(1908, 1151), map = new Vector2(1630, 317);
+ void State(Vector2 at, bool down)
+ {
+ var s = new MouseState { position = at };
+ InputSystem.QueueStateEvent(mouse, down ? s.WithButton(MouseButton.Left) : s);
+ }
+ State(start, false); InputSystem.Update();
+ seen.Clear();
+ // The browser's burst: move to Why, click, move to the map, click, all in one frame.
+ State(why, false); State(why, true); State(why, false); State(map, false); State(map, true); State(map, false);
+ InputSystem.Update();
+ Assert.AreEqual(6, seen.Count, "every event survives: " + string.Join(", ", seen));
+ Assert.AreEqual((0f, why), seen[2], "the first release is where it happened, on Why");
+ }
+ finally
+ {
+ InputSystem.onEvent -= listen;
+ InputSystem.RemoveDevice(mouse);
+ InputSystem.settings.disableRedundantEventsMerging = old;
+ }
+ }
+
+ [Test]
+ public void TheGameTurnsMergingOff()
+ {
+ var old = InputSystem.settings.disableRedundantEventsMerging;
+ try
+ {
+ InputSystem.settings.disableRedundantEventsMerging = false;
+ InputConfig.Apply();
+ Assert.IsTrue(InputSystem.settings.disableRedundantEventsMerging);
+ }
+ finally { InputSystem.settings.disableRedundantEventsMerging = old; }
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/InputMergingTests.cs.meta b/unity/Assets/Tests/EditMode/InputMergingTests.cs.meta
new file mode 100644
index 000000000..086be52e9
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/InputMergingTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: d14d2e6ca000d4a3eba87ce7b69abfa2
\ No newline at end of file
diff --git a/unity/Assets/Tests/EditMode/MapPressTests.cs b/unity/Assets/Tests/EditMode/MapPressTests.cs
new file mode 100644
index 000000000..e777982d9
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/MapPressTests.cs
@@ -0,0 +1,31 @@
+#nullable enable
+using System.Collections.Generic;
+using NUnit.Framework;
+using Statecraft.Game.Map;
+using UnityEngine;
+using UnityEngine.InputSystem;
+
+/// <summary>Playtest 2, P2-4: map clicks under same-frame input.</summary>
+public class MapPressTests
+{
+ /// <summary>MapPointer evaluates every press at its own position, in order, even when several land in one frame.</summary>
+ [Test]
+ public void EveryMapPressIsCheckedAtItsOwnPosition()
+ {
+ var go = new GameObject("P24Pointer");
+ try
+ {
+ var pointer = go.AddComponent<MapPointer>();
+ var asked = new List<Vector2>();
+ pointer.IsOverUi = p => { asked.Add(p); return p.y > 1000; }; // the top of the screen is UI
+ pointer.OnPress(new Vector2(1908, 1151));
+ pointer.OnPress(new Vector2(1630, 317));
+ pointer.ProcessPresses();
+ CollectionAssert.AreEqual(new[] { new Vector2(1908, 1151), new Vector2(1630, 317) }, asked, "both presses, each where it happened");
+ asked.Clear();
+ pointer.ProcessPresses();
+ Assert.IsEmpty(asked, "a press is handled once");
+ }
+ finally { Object.DestroyImmediate(go); }
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/MapPressTests.cs.meta b/unity/Assets/Tests/EditMode/MapPressTests.cs.meta
new file mode 100644
index 000000000..53d38a8c9
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/MapPressTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 39a2ca9572b5844f798057673b275250
\ No newline at end of file