A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
feat(unity): Input System camera (pan, zoom, orbit) that ignores input over the UI
eyalgolan2048df9d7ab0
diff --git a/unity/Assets/InputSystem_Actions.inputactions b/unity/Assets/InputSystem_Actions.inputactionsindex 1a12cb91b..50e6cb854 100644--- a/unity/Assets/InputSystem_Actions.inputactions+++ b/unity/Assets/InputSystem_Actions.inputactions@@ -990,6 +990,237 @@"isPartOfComposite": false}]+ },+ {+ "name": "Camera",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c01",+ "actions": [+ {+ "name": "Pan",+ "type": "Value",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c02",+ "expectedControlType": "Vector2",+ "processors": "",+ "interactions": "",+ "initialStateCheck": true+ },+ {+ "name": "DragPan",+ "type": "Value",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c03",+ "expectedControlType": "Vector2",+ "processors": "",+ "interactions": "",+ "initialStateCheck": true+ },+ {+ "name": "Zoom",+ "type": "Value",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c04",+ "expectedControlType": "Axis",+ "processors": "",+ "interactions": "",+ "initialStateCheck": true+ },+ {+ "name": "Orbit",+ "type": "Value",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c05",+ "expectedControlType": "Axis",+ "processors": "",+ "interactions": "",+ "initialStateCheck": true+ }+ ],+ "bindings": [+ {+ "name": "WASD",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c10",+ "path": "2DVector",+ "interactions": "",+ "processors": "",+ "groups": "",+ "action": "Pan",+ "isComposite": true,+ "isPartOfComposite": false+ },+ {+ "name": "up",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c11",+ "path": "<Keyboard>/w",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "down",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c12",+ "path": "<Keyboard>/s",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "left",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c13",+ "path": "<Keyboard>/a",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "right",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c14",+ "path": "<Keyboard>/d",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "Arrows",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c15",+ "path": "2DVector",+ "interactions": "",+ "processors": "",+ "groups": "",+ "action": "Pan",+ "isComposite": true,+ "isPartOfComposite": false+ },+ {+ "name": "up",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c16",+ "path": "<Keyboard>/upArrow",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "down",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c17",+ "path": "<Keyboard>/downArrow",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "left",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c18",+ "path": "<Keyboard>/leftArrow",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "right",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c19",+ "path": "<Keyboard>/rightArrow",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Pan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "RightDrag",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c20",+ "path": "OneModifier",+ "interactions": "",+ "processors": "",+ "groups": "",+ "action": "DragPan",+ "isComposite": true,+ "isPartOfComposite": false+ },+ {+ "name": "modifier",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c21",+ "path": "<Mouse>/rightButton",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "DragPan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "binding",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c22",+ "path": "<Mouse>/delta",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "DragPan",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c30",+ "path": "<Mouse>/scroll/y",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Zoom",+ "isComposite": false,+ "isPartOfComposite": false+ },+ {+ "name": "QE",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c40",+ "path": "1DAxis",+ "interactions": "",+ "processors": "",+ "groups": "",+ "action": "Orbit",+ "isComposite": true,+ "isPartOfComposite": false+ },+ {+ "name": "negative",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c41",+ "path": "<Keyboard>/q",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Orbit",+ "isComposite": false,+ "isPartOfComposite": true+ },+ {+ "name": "positive",+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c42",+ "path": "<Keyboard>/e",+ "interactions": "",+ "processors": "",+ "groups": ";Keyboard&Mouse",+ "action": "Orbit",+ "isComposite": false,+ "isPartOfComposite": true+ }+ ]}],"controlSchemes": [@@ -1054,4 +1285,4 @@]}]-}\ No newline at end of file+}diff --git a/unity/Assets/Scripts/Map/CameraController.cs b/unity/Assets/Scripts/Map/CameraController.csnew file mode 100644index 000000000..3fe23d235--- /dev/null+++ b/unity/Assets/Scripts/Map/CameraController.cs@@ -0,0 +1,79 @@+#nullable enable+using System;+using UnityEngine;+using UnityEngine.InputSystem;++namespace Statecraft.Game.Map+{+ /// <summary>One frame of camera input, in the units the actions give: Pan in [-1, 1] per axis (keys), DragPan in+ /// screen pixels (right-button drag), Zoom in scroll units (positive = in), Orbit in [-1, 1] (Q/E).</summary>+ public struct CameraInput+ {+ public Vector2 Pan, DragPan; public float Zoom, Orbit;+ /// <summary>True while the pointer is over the UI: the camera then ignores pointer input (wheel zoom, drag).</summary>+ public bool OverUi;+ }++ /// <summary>Pans, zooms and orbits the diorama camera from the project-wide "Camera" action map (Input System only).+ /// While the pointer is over the UI (IsOverUi, wired by the scene) the wheel and the drag belong to the UI; keyboard pan+ /// and orbit still move the camera (plan amendment A4). Pan speed scales with+ /// distance, zoom is clamped to [MinDistance, MaxDistance], and the look-at point stays on the map.</summary>+ [RequireComponent(typeof(CameraRig))]+ public sealed class CameraController : MonoBehaviour+ {+ public const float MinDistance = 20000f, MaxDistance = 250000f;+ public const float PanPerSecond = 0.8f; // distances per second at full key press+ public const float DragPanPerPixel = 0.0012f; // distances per pixel of drag+ public const float ZoomPerStep = 0.1f; // fraction of the distance per scroll notch (scroll units are normalised to notches)+ public const float OrbitDegreesPerSecond = 60f;++ /// <summary>Answers "is the pointer over the UI?" for a screen position (y up). Null = never.</summary>+ public Func<Vector2, bool>? IsOverUi;+ /// <summary>The map rectangle on the ground (x, z), in world metres; the look-at point is clamped to it.</summary>+ public Rect Bounds = new Rect(0, 0, float.MaxValue, float.MaxValue);++ CameraRig rig = null!;+ InputAction? pan, dragPan, zoom, orbit;++ void Awake() => rig = GetComponent<CameraRig>();++ void OnEnable()+ {+ var map = InputSystem.actions?.FindActionMap("Camera", throwIfNotFound: true);+ if (map == null) return;+ pan = map.FindAction("Pan", true); dragPan = map.FindAction("DragPan", true);+ zoom = map.FindAction("Zoom", true); orbit = map.FindAction("Orbit", true);+ map.Enable();+ }++ void OnDisable() => InputSystem.actions?.FindActionMap("Camera")?.Disable();++ void Update()+ {+ if (pan == null || dragPan == null || zoom == null || orbit == null) return;+ var pointer = Pointer.current?.position.ReadValue() ?? Vector2.zero;+ Step(rig, new CameraInput+ {+ Pan = pan.ReadValue<Vector2>(), DragPan = dragPan.ReadValue<Vector2>(),+ Zoom = Mathf.Clamp(zoom.ReadValue<float>() / 120f, -3f, 3f), // browsers report 100-120 per notch; clamp bursts+ Orbit = orbit.ReadValue<float>(),+ OverUi = IsOverUi != null && IsOverUi(pointer),+ }, Time.unscaledDeltaTime, Bounds);+ }++ /// <summary>Applies one frame of input to the rig and calls rig.Apply(). Pure apart from the rig, so it is testable.</summary>+ public static void Step(CameraRig rig, CameraInput input, float dt, Rect bounds)+ {+ var zoom = input.OverUi ? 0f : input.Zoom;+ var drag = input.OverUi ? Vector2.zero : input.DragPan;+ rig.Yaw = Mathf.Repeat(rig.Yaw + input.Orbit * OrbitDegreesPerSecond * dt, 360f);+ rig.Distance = Mathf.Clamp(rig.Distance * (1f - zoom * ZoomPerStep), MinDistance, MaxDistance);+ var yaw = Quaternion.Euler(0, rig.Yaw, 0);+ var move = input.Pan * (PanPerSecond * dt) - drag * DragPanPerPixel;+ var delta = yaw * new Vector3(move.x, 0, move.y) * rig.Distance;+ var p = rig.LookAt + delta;+ rig.LookAt = new Vector3(Mathf.Clamp(p.x, bounds.xMin, bounds.xMax), p.y, Mathf.Clamp(p.z, bounds.yMin, bounds.yMax));+ rig.Apply();+ }+ }+}diff --git a/unity/Assets/Scripts/Map/CameraController.cs.meta b/unity/Assets/Scripts/Map/CameraController.cs.metanew file mode 100644index 000000000..4512b9c2f--- /dev/null+++ b/unity/Assets/Scripts/Map/CameraController.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 882f31929dee34cf59c6f172c5f87054\ No newline at end of filediff --git a/unity/Assets/Scripts/Map/CameraRig.cs b/unity/Assets/Scripts/Map/CameraRig.csindex 5a2d5e06f..84fee9cab 100644--- a/unity/Assets/Scripts/Map/CameraRig.cs+++ b/unity/Assets/Scripts/Map/CameraRig.cs@@ -5,11 +5,12 @@ using UnityEngine.Rendering.Universal;namespace Statecraft.Game.Map{/// <summary>Diorama camera: fixed 50-degree pitch, 35-degree FOV, Gaussian far blur beyond the look-at point (tilt-shift look).+ /// Yaw turns the view around the look-at point (0 = looking north); CameraController drives LookAt, Distance and Yaw./// Gaussian, not Bokeh: physical Bokeh DoF keeps everything sharp at 100 km focus distances.</summary>public sealed class CameraRig : MonoBehaviour{public const float Pitch = 50f, Fov = 35f;- public Vector3 LookAt; public float Distance = 120000f;+ public Vector3 LookAt; public float Distance = 120000f; public float Yaw;DepthOfField _dof;/// <summary>The global post-processing volume Init creates. Its runtime VolumeProfile must be owned by the caller@@ -30,7 +31,7 @@ namespace Statecraft.Game.Mappublic void Apply(){- transform.rotation = Quaternion.Euler(Pitch, 0, 0);+ transform.rotation = Quaternion.Euler(Pitch, Yaw, 0);transform.position = LookAt - transform.forward * Distance;if (_dof != null) { _dof.gaussianStart.Override(Distance * 1.05f); _dof.gaussianEnd.Override(Distance * 1.8f); }}diff --git a/unity/Assets/Scripts/Statecraft.Game.asmdef b/unity/Assets/Scripts/Statecraft.Game.asmdefindex 8cf508c47..14a785d58 100644--- a/unity/Assets/Scripts/Statecraft.Game.asmdef+++ b/unity/Assets/Scripts/Statecraft.Game.asmdef@@ -1 +1 @@-{ "name": "Statecraft.Game", "references": ["Unity.RenderPipelines.Universal.Runtime", "Unity.RenderPipelines.Core.Runtime"], "overrideReferences": true, "precompiledReferences": ["Statecraft.Sim.dll", "YamlDotNet.dll"], "autoReferenced": true }+{ "name": "Statecraft.Game", "references": ["Unity.RenderPipelines.Universal.Runtime", "Unity.RenderPipelines.Core.Runtime", "Unity.InputSystem"], "overrideReferences": true, "precompiledReferences": ["Statecraft.Sim.dll", "YamlDotNet.dll"], "autoReferenced": true }diff --git a/unity/Assets/Tests/EditMode/CameraInputTests.cs b/unity/Assets/Tests/EditMode/CameraInputTests.csnew file mode 100644index 000000000..5d04e4517--- /dev/null+++ b/unity/Assets/Tests/EditMode/CameraInputTests.cs@@ -0,0 +1,84 @@+using NUnit.Framework;+using Statecraft.Game.Map;+using UnityEditor;+using UnityEngine;+using UnityEngine.InputSystem;++public class CameraInputTests+{+ static CameraRig NewRig()+ {+ var go = new GameObject("RigTest", typeof(Camera));+ var rig = go.AddComponent<CameraRig>();+ rig.LookAt = new Vector3(50000, 0, 50000); rig.Distance = 100000f;+ return rig;+ }++ static readonly Rect Map = new Rect(0, 0, 160000, 430000);++ [Test]+ public void TheCameraActionMapIsInTheProjectWideAsset()+ {+ var asset = AssetDatabase.LoadAssetAtPath<InputActionAsset>("Assets/InputSystem_Actions.inputactions");+ var map = asset.FindActionMap("Camera", true);+ foreach (var name in new[] { "Pan", "DragPan", "Zoom", "Orbit" }) Assert.IsNotNull(map.FindAction(name), name);+ Assert.IsNotNull(asset.FindActionMap("UI", true).FindAction("Click"), "UI Toolkit needs the UI map's actions");+ }++ [Test]+ public void PanMovesTheLookAtNorthAtYawZero()+ {+ var rig = NewRig();+ try+ {+ CameraController.Step(rig, new CameraInput { Pan = Vector2.up }, 0.5f, Map);+ Assert.AreEqual(50000f, rig.LookAt.x, 1e-2);+ Assert.AreEqual(50000f + 0.4f * 100000f, rig.LookAt.z, 1e-1); // 0.8 distances per second for half a second+ }+ finally { Object.DestroyImmediate(rig.gameObject); }+ }++ [Test]+ public void ZoomIsClampedAndOrbitTurnsTheView()+ {+ var rig = NewRig();+ try+ {+ for (int i = 0; i < 100; i++) CameraController.Step(rig, new CameraInput { Zoom = 3 }, 0.016f, Map);+ Assert.AreEqual(CameraController.MinDistance, rig.Distance, 1e-3);+ CameraController.Step(rig, new CameraInput { Orbit = 1 }, 0.5f, Map);+ Assert.AreEqual(30f, rig.Yaw, 1e-3);+ Assert.AreEqual(30f, rig.transform.eulerAngles.y, 1e-3);+ CameraController.Step(rig, new CameraInput { Pan = Vector2.left }, 100f, Map);+ Assert.AreEqual(0f, rig.LookAt.x, 1e-3); // clamped to the map edge+ }+ finally { Object.DestroyImmediate(rig.gameObject); }+ }++ [Test]+ public void PointerInputIsIgnoredOverTheUi()+ {+ var rig = NewRig();+ try+ {+ var before = (rig.LookAt, rig.Distance, rig.Yaw);+ CameraController.Step(rig, new CameraInput { Zoom = 1, DragPan = Vector2.one * 50, OverUi = true }, 1f, Map);+ Assert.AreEqual(before, (rig.LookAt, rig.Distance, rig.Yaw));+ }+ finally { Object.DestroyImmediate(rig.gameObject); }+ }++ [Test]+ public void KeysStillPanAndOrbitWhileThePointerIsOverTheUi()+ {+ var rig = NewRig();+ try+ {+ CameraController.Step(rig, new CameraInput { Pan = Vector2.up, Orbit = 1, Zoom = 1, OverUi = true }, 0.5f, Map);+ Assert.AreEqual(100000f, rig.Distance, 1e-3); // the wheel was over the UI+ Assert.AreEqual(30f, rig.Yaw, 1e-3);+ Assert.Greater(rig.LookAt.z, 50000f);+ }+ finally { Object.DestroyImmediate(rig.gameObject); }+ }+}diff --git a/unity/Assets/Tests/EditMode/CameraInputTests.cs.meta b/unity/Assets/Tests/EditMode/CameraInputTests.cs.metanew file mode 100644index 000000000..a04c7c579--- /dev/null+++ b/unity/Assets/Tests/EditMode/CameraInputTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: cf28af8270c57403886afd1b7ea90c6d\ No newline at end of filediff --git a/unity/Assets/Tests/EditMode/Statecraft.Tests.EditMode.asmdef b/unity/Assets/Tests/EditMode/Statecraft.Tests.EditMode.asmdefindex 3bfb49794..58def7824 100644--- a/unity/Assets/Tests/EditMode/Statecraft.Tests.EditMode.asmdef+++ b/unity/Assets/Tests/EditMode/Statecraft.Tests.EditMode.asmdef@@ -1 +1 @@-{ "name": "Statecraft.Tests.EditMode", "references": ["Statecraft.Game", "Unity.RenderPipelines.Core.Runtime", "Unity.RenderPipelines.Universal.Runtime", "UnityEngine.TestRunner", "UnityEditor.TestRunner"], "includePlatforms": ["Editor"], "overrideReferences": true, "precompiledReferences": ["nunit.framework.dll", "Statecraft.Sim.dll", "YamlDotNet.dll"], "defineConstraints": ["UNITY_INCLUDE_TESTS"] }+{ "name": "Statecraft.Tests.EditMode", "references": ["Statecraft.Game", "Unity.RenderPipelines.Core.Runtime", "Unity.RenderPipelines.Universal.Runtime", "Unity.InputSystem", "UnityEngine.TestRunner", "UnityEditor.TestRunner"], "includePlatforms": ["Editor"], "overrideReferences": true, "precompiledReferences": ["nunit.framework.dll", "Statecraft.Sim.dll", "YamlDotNet.dll"], "defineConstraints": ["UNITY_INCLUDE_TESTS"] }