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.

UI: the map zooms from the keyboard, = and - and the numpad's + and - (a11y B1a, refs #41) (#890)

Beri6c56f22d7cd7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a67242799..ffc6bc6df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,10 @@ adds a line under **Unreleased**; CI's `Changelog` job checks this (see `CONTRIB
exist on the date reads its named successor's multiplier. In the historical run every build and absorption moves:
the reference player builds Mefalsim on 15 February 1951 (was 15 March 1950), the camps are never started, and every
golden is re-pinned.
+- The map zooms from the keyboard (a11y B1a, refs #41): hold = or - (or the numpad's + and -) to zoom in or out at
+ six wheel notches a second, so 6 to 250 km takes about six seconds. Like keyboard pan, the keys work while the
+ pointer rests over a panel, and only without Ctrl, Alt or Cmd, so Ctrl/Cmd with + and - stays the browser's own
+ zoom. A new `ZoomKeys` action in the Camera map; the wheel's `Zoom` keeps its one binding.
### Fixed
- Fractional site and city-project carry is hashed as IEEE bits on every runtime, avoiding different decimal spellings on .NET and Unity Mono; gameplay progress is unchanged.
diff --git a/unity/Assets/InputSystem_Actions.inputactions b/unity/Assets/InputSystem_Actions.inputactions
index 5287270ff..06fcaffc4 100644
--- a/unity/Assets/InputSystem_Actions.inputactions
+++ b/unity/Assets/InputSystem_Actions.inputactions
@@ -923,6 +923,15 @@
"interactions": "",
"initialStateCheck": true
},
+ {
+ "name": "ZoomKeys",
+ "type": "Value",
+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c06",
+ "expectedControlType": "Axis",
+ "processors": "",
+ "interactions": "",
+ "initialStateCheck": true
+ },
{
"name": "Orbit",
"type": "Value",
@@ -1088,6 +1097,72 @@
"isComposite": false,
"isPartOfComposite": false
},
+ {
+ "name": "PlusMinus",
+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c50",
+ "path": "1DAxis",
+ "interactions": "",
+ "processors": "",
+ "groups": "",
+ "action": "ZoomKeys",
+ "isComposite": true,
+ "isPartOfComposite": false
+ },
+ {
+ "name": "negative",
+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c51",
+ "path": "<Keyboard>/minus",
+ "interactions": "",
+ "processors": "",
+ "groups": ";Keyboard&Mouse",
+ "action": "ZoomKeys",
+ "isComposite": false,
+ "isPartOfComposite": true
+ },
+ {
+ "name": "positive",
+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c52",
+ "path": "<Keyboard>/equals",
+ "interactions": "",
+ "processors": "",
+ "groups": ";Keyboard&Mouse",
+ "action": "ZoomKeys",
+ "isComposite": false,
+ "isPartOfComposite": true
+ },
+ {
+ "name": "Numpad",
+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c53",
+ "path": "1DAxis",
+ "interactions": "",
+ "processors": "",
+ "groups": "",
+ "action": "ZoomKeys",
+ "isComposite": true,
+ "isPartOfComposite": false
+ },
+ {
+ "name": "negative",
+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c54",
+ "path": "<Keyboard>/numpadMinus",
+ "interactions": "",
+ "processors": "",
+ "groups": ";Keyboard&Mouse",
+ "action": "ZoomKeys",
+ "isComposite": false,
+ "isPartOfComposite": true
+ },
+ {
+ "name": "positive",
+ "id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c55",
+ "path": "<Keyboard>/numpadPlus",
+ "interactions": "",
+ "processors": "",
+ "groups": ";Keyboard&Mouse",
+ "action": "ZoomKeys",
+ "isComposite": false,
+ "isPartOfComposite": true
+ },
{
"name": "QE",
"id": "5b1f0c3e-7a8d-4c51-9e2b-3a6d4f8e1c40",
diff --git a/unity/Assets/Scripts/Map/CameraController.cs b/unity/Assets/Scripts/Map/CameraController.cs
index 8f7ebd32b..d861794a2 100644
--- a/unity/Assets/Scripts/Map/CameraController.cs
+++ b/unity/Assets/Scripts/Map/CameraController.cs
@@ -10,6 +10,9 @@ namespace Statecraft.Game.Map
public struct CameraInput
{
public Vector2 Pan, DragPan; public float Zoom, Orbit;
+ /// <summary>The zoom keys (a11y B1a: = and -, the numpad's + and -) in [-1, 1], positive = in. Step scales it by
+ /// ZoomKeysPerSecond and the frame time, as keyboard pan is, and like keyboard pan it is not dropped over the UI.</summary>
+ public float KeyZoom;
/// <summary>True while the pointer is over the UI: the camera then ignores pointer input (wheel zoom, drag).</summary>
public bool OverUi;
/// <summary>True when the current right-button drag started over the UI (latched on button-down): that drag never pans the map.</summary>
@@ -34,6 +37,9 @@ namespace Statecraft.Game.Map
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 wheel notch
+ /// <summary>Wheel notches a second while a zoom key is held (a11y B1a, option 1 of the keyboard plan): the closest to
+ /// the farthest zoom (6 to 250 km, 35 notches) in about six seconds, held like pan and orbit, not pressed 35 times.</summary>
+ public const float ZoomKeysPerSecond = 6f;
public const float OrbitDegreesPerSecond = 60f;
public const float MaxFrameSeconds = 0.1f; // a stalled frame (tab in the background) must not fling the camera
/// <summary>The least height above the ground beneath it the camera keeps, as a share of its height above the
@@ -73,7 +79,7 @@ namespace Statecraft.Game.Map
public float GroundTop = ShadowFit.GroundY;
CameraRig rig = null!;
- InputAction? pan, dragPan, zoom, orbit;
+ InputAction? pan, dragPan, zoom, zoomKeys, orbit;
void Awake() => rig = GetComponent<CameraRig>();
@@ -84,7 +90,7 @@ namespace Statecraft.Game.Map
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);
+ zoom = map.FindAction("Zoom", true); zoomKeys = map.FindAction("ZoomKeys", true); orbit = map.FindAction("Orbit", true);
map.Enable();
}
@@ -92,9 +98,14 @@ namespace Statecraft.Game.Map
bool dragFromUi, dragWasDown;
- void Update()
+ void Update() => Tick(Time.unscaledDeltaTime);
+
+ /// <summary>One frame from the live actions, <paramref name="dt"/> seconds long. Test seam (internal; the tests see it
+ /// through InternalsVisibleTo): an EditMode test has no frame clock, so it drives the real key path with a fixed
+ /// frame time (#890).</summary>
+ internal void Tick(float dt)
{
- if (pan == null || dragPan == null || zoom == null || orbit == null) return;
+ if (pan == null || dragPan == null || zoom == null || zoomKeys == null || orbit == null) return;
Vector2? pointer = Pointer.current != null ? Pointer.current.position.ReadValue() : (Vector2?)null;
var down = Mouse.current != null && Mouse.current.rightButton.isPressed;
var drag = dragPan.ReadValue<Vector2>();
@@ -117,8 +128,9 @@ namespace Statecraft.Game.Map
break;
}
}
- Step(rig, Gather(pan.ReadValue<Vector2>(), drag, zoomRaw, orbit.ReadValue<float>(), down, pointer),
- ClampDt(Time.unscaledDeltaTime), Bounds, Ground, GroundTop);
+ var modifier = ModifierHeld(Keyboard.current);
+ Step(rig, Gather(pan.ReadValue<Vector2>(), drag, zoomRaw, orbit.ReadValue<float>(), down, pointer, zoomKeys.ReadValue<float>(), modifier),
+ ClampDt(dt), Bounds, Ground, GroundTop);
}
/// <summary>The first two touches down (2 stands for two or more), or fewer.</summary>
@@ -147,6 +159,11 @@ namespace Statecraft.Game.Map
return (now - was) / PixelsPerNotch;
}
+ /// <summary>Ctrl, Alt or Cmd (either side) held: the zoom keys then belong to the browser or the system. Shift is not
+ /// a modifier here, since + is Shift and = on a US keyboard.</summary>
+ public static bool ModifierHeld(Keyboard? keys) =>
+ keys != null && (keys.ctrlKey.isPressed || keys.altKey.isPressed || keys.leftMetaKey.isPressed || keys.rightMetaKey.isPressed);
+
/// <summary>Fits the rig's current pose with no input (review B-m3: a minimap jump goes through the same view fit and
/// ground clearance as panning, so no frame shows the map's frame or the inside of a hill).</summary>
public void Settle() => Step(GetComponent<CameraRig>(), default, 0f, Bounds, Ground, GroundTop);
@@ -207,8 +224,11 @@ namespace Statecraft.Game.Map
public static float ClampDt(float dt) => Mathf.Min(dt, MaxFrameSeconds);
/// <summary>Builds one frame of input from raw action values. The wheel is checked against the UI at the pointer every frame;
- /// the drag is checked once, when the button goes down, and that answer holds until it is released. No pointer means not over the UI.</summary>
- public CameraInput Gather(Vector2 panValue, Vector2 dragValue, float zoomRaw, float orbitValue, bool dragDown, Vector2? pointer)
+ /// the drag is checked once, when the button goes down, and that answer holds until it is released. No pointer means not over the UI.
+ /// The zoom keys count only with no Ctrl, Alt or Cmd held (<paramref name="modifierHeld"/>), so Ctrl/Cmd with + and - stays the
+ /// browser's page zoom; both composites held at once (= and the numpad's +) still count as one key.</summary>
+ public CameraInput Gather(Vector2 panValue, Vector2 dragValue, float zoomRaw, float orbitValue, bool dragDown, Vector2? pointer,
+ float zoomKeys = 0f, bool modifierHeld = false)
{
var over = pointer.HasValue && IsOverUi != null && IsOverUi(pointer.Value);
if (dragDown && !dragWasDown) dragFromUi = over;
@@ -220,6 +240,7 @@ namespace Statecraft.Game.Map
// Notches: on desktop the Input System's default scroll behaviour (UniformAcrossAllPlatforms) gives 1 per
// notch; in WebGL WheelOr counts the browser's events (#43). Clamp bursts.
Zoom = Mathf.Clamp(zoomRaw, -3f, 3f),
+ KeyZoom = modifierHeld ? 0f : Mathf.Clamp(zoomKeys, -1f, 1f),
Orbit = orbitValue, OverUi = over, DragFromUi = dragFromUi,
};
}
@@ -229,7 +250,8 @@ namespace Statecraft.Game.Map
public static void Step(CameraRig rig, CameraInput input, float dt, Rect bounds, Func<Vector2, float>? ground = null,
float groundTop = ShadowFit.GroundY)
{
- var zoom = input.OverUi ? 0f : input.Zoom;
+ // The wheel is the UI's while the pointer is over it; the zoom keys are not (plan amendment A4, as keyboard pan).
+ var zoom = (input.OverUi ? 0f : input.Zoom) + input.KeyZoom * ZoomKeysPerSecond * dt;
var drag = input.DragFromUi ? Vector2.zero : input.DragPan;
rig.Yaw = Mathf.Repeat(rig.Yaw + input.Orbit * OrbitDegreesPerSecond * dt, 360f);
// (1 - step)^zoom: one notch in is exactly ZoomPerStep closer, and n notches out undo n notches in (Task 24 review M3).
diff --git a/unity/Assets/Tests/EditMode/CameraInputTests.cs b/unity/Assets/Tests/EditMode/CameraInputTests.cs
index ebda2e24a..2af052997 100644
--- a/unity/Assets/Tests/EditMode/CameraInputTests.cs
+++ b/unity/Assets/Tests/EditMode/CameraInputTests.cs
@@ -27,10 +27,15 @@ public class CameraInputTests
{
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);
+ foreach (var name in new[] { "Pan", "DragPan", "Zoom", "ZoomKeys", "Orbit" }) Assert.IsNotNull(map.FindAction(name), name);
Assert.AreEqual(8, map.FindAction("Pan").bindings.Count(b => b.isPartOfComposite && b.path.StartsWith("<Keyboard>/")));
Assert.AreEqual(3, map.FindAction("DragPan").bindings.Count);
Assert.AreEqual(1, map.FindAction("Zoom").bindings.Count);
+ // a11y B1a: = and -, and the numpad's + and -, as two 1D composites; the wheel's Zoom keeps its one binding.
+ CollectionAssert.AreEquivalent(new[] { "<Keyboard>/equals", "<Keyboard>/minus", "<Keyboard>/numpadPlus", "<Keyboard>/numpadMinus" },
+ map.FindAction("ZoomKeys").bindings.Where(b => b.isPartOfComposite).Select(b => b.path).ToArray());
+ CollectionAssert.AreEquivalent(new[] { "<Keyboard>/equals", "<Keyboard>/numpadPlus" }, // positive = in, as the wheel
+ map.FindAction("ZoomKeys").bindings.Where(b => b.isPartOfComposite && b.name == "positive").Select(b => b.path).ToArray());
Assert.AreEqual(3, map.FindAction("Orbit").bindings.Count);
var added = new System.Collections.Generic.List<InputDevice>();
if (Keyboard.current == null) added.Add(InputSystem.AddDevice<Keyboard>());
@@ -121,6 +126,76 @@ public class CameraInputTests
finally { Object.DestroyImmediate(rig.gameObject); }
}
+ /// <summary>a11y B1a: the zoom keys move the camera as keyboard pan does, also while the pointer rests over a panel,
+ /// at ZoomKeysPerSecond wheel notches a second held, so the closest to the farthest zoom takes about six seconds.</summary>
+ [Test]
+ public void ZoomKeysWorkOverTheUiAtSixNotchesASecond()
+ {
+ var rig = NewRig();
+ try
+ {
+ CameraController.Step(rig, new CameraInput { KeyZoom = 1, OverUi = true }, 1f, Map);
+ Assert.AreEqual(100000f * Mathf.Pow(1f - CameraController.ZoomPerStep, CameraController.ZoomKeysPerSecond), rig.Distance, 1f);
+ CameraController.Step(rig, new CameraInput { KeyZoom = -1, OverUi = true }, 1f, Map);
+ Assert.AreEqual(100000f, rig.Distance, 1f, "a second out undoes a second in");
+ Assert.AreEqual(6f, CameraController.ZoomKeysPerSecond, "the plan's rate (B1a option 1)");
+ }
+ finally { Object.DestroyImmediate(rig.gameObject); }
+ }
+
+ [Test]
+ public void ZoomKeysTwoHalfStepsEqualOneFullStep()
+ {
+ var a = NewRig(); var b = NewRig();
+ try
+ {
+ var inp = new CameraInput { KeyZoom = 1 };
+ CameraController.Step(a, inp, 0.1f, Map);
+ CameraController.Step(b, inp, 0.05f, Map); CameraController.Step(b, inp, 0.05f, Map);
+ Assert.AreEqual(a.Distance, b.Distance, 1e-1);
+ Assert.Less(a.Distance, 100000f);
+ }
+ finally { Object.DestroyImmediate(a.gameObject); Object.DestroyImmediate(b.gameObject); }
+ }
+
+ /// <summary>a11y B1a: only an unmodified press zooms the map, so Ctrl or Cmd with + and - stays the browser's own page
+ /// zoom (and Alt combinations stay the system's). Shift is allowed: + is Shift and = on a US keyboard.</summary>
+ [Test]
+ public void ZoomKeysCountOnlyWithoutCtrlAltOrCmd()
+ {
+ var c = NewController(out var go);
+ try
+ {
+ Assert.AreEqual(1f, c.Gather(Vector2.zero, Vector2.zero, 0, 0, false, null, 1f, false).KeyZoom);
+ Assert.AreEqual(-1f, c.Gather(Vector2.zero, Vector2.zero, 0, 0, false, null, -1f, false).KeyZoom);
+ Assert.AreEqual(0f, c.Gather(Vector2.zero, Vector2.zero, 0, 0, false, null, 1f, true).KeyZoom);
+ Assert.AreEqual(1f, c.Gather(Vector2.zero, Vector2.zero, 0, 0, false, null, 2f, false).KeyZoom, "both composites held count once");
+ Assert.AreEqual(0f, c.Gather(Vector2.zero, Vector2.zero, 0, 0, false, null).KeyZoom, "no keys, no key zoom");
+ }
+ finally { Object.DestroyImmediate(go); }
+ }
+
+ /// <summary>The modifier read Update feeds Gather: each of Ctrl, Alt and Cmd on either side counts, Shift does not.</summary>
+ [Test]
+ public void CtrlAltAndCmdAreModifiersAndShiftIsNot()
+ {
+ var keyboard = InputSystem.AddDevice<Keyboard>();
+ try
+ {
+ Assert.IsFalse(CameraController.ModifierHeld(null));
+ foreach (var (key, want) in new[] { (Key.None, false), (Key.LeftShift, false), (Key.RightShift, false),
+ (Key.LeftCtrl, true), (Key.RightCtrl, true), (Key.LeftAlt, true), (Key.RightAlt, true), (Key.LeftMeta, true), (Key.RightMeta, true) })
+ {
+ var state = key == Key.None ? new UnityEngine.InputSystem.LowLevel.KeyboardState(Key.Equals)
+ : new UnityEngine.InputSystem.LowLevel.KeyboardState(key, Key.Equals);
+ InputSystem.QueueStateEvent(keyboard, state); InputSystem.Update();
+ Assert.IsTrue(keyboard.equalsKey.isPressed, key + ": precondition, = is down");
+ Assert.AreEqual(want, CameraController.ModifierHeld(keyboard), key.ToString());
+ }
+ }
+ finally { InputSystem.RemoveDevice(keyboard); }
+ }
+
[Test]
public void MaxDistanceClampsAndYawWraps()
{
@@ -575,3 +650,46 @@ public class CameraInputTests
finally { Object.DestroyImmediate(go); }
}
}
+
+/// <summary>The zoom keys through Unity's own input path (#890, owner's ask): a virtual keyboard from the Input System's
+/// test fixture, the project-wide Camera actions, and CameraController's frame (Tick, which Update calls), so a key that
+/// stops being read or a modifier that stops being passed on goes red. Edit mode sends no Awake/OnEnable/OnDisable, so
+/// the test sends them.</summary>
+public class CameraZoomKeysThroughTheInputSystemTests : InputTestFixture
+{
+ static void Send(CameraController c, string message) =>
+ typeof(CameraController).GetMethod(message, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
+ .Invoke(c, null);
+
+ [Test]
+ public void HeldZoomKeysMoveTheCameraAndCtrlStopsThem()
+ {
+ InputSystem.actions = AssetDatabase.LoadAssetAtPath<InputActionAsset>("Assets/InputSystem_Actions.inputactions");
+ var keyboard = InputSystem.AddDevice<Keyboard>();
+ var go = new GameObject("KeyZoomTest", typeof(Camera));
+ var rig = go.AddComponent<CameraRig>();
+ rig.LookAt = new Vector3(500000, 0, 500000); rig.Distance = 100000f;
+ var c = go.AddComponent<CameraController>();
+ c.Bounds = new Rect(0, 0, 1000000, 1000000); // far larger than the view: no view clamp here
+ Send(c, "Awake"); Send(c, "OnEnable");
+ try
+ {
+ float AfterOneSecond(float from) => from * Mathf.Pow(1f - CameraController.ZoomPerStep, CameraController.ZoomKeysPerSecond);
+
+ Press(keyboard.equalsKey);
+ for (int i = 0; i < 10; i++) c.Tick(0.1f);
+ Assert.AreEqual(AfterOneSecond(100000f), rig.Distance, 1f, "= held for a second: six notches in");
+
+ Press(keyboard.leftCtrlKey);
+ var held = rig.Distance;
+ c.Tick(0.1f);
+ Assert.AreEqual(held, rig.Distance, 1e-3, "Ctrl with = is the browser's zoom, not the map's");
+ Release(keyboard.leftCtrlKey); Release(keyboard.equalsKey);
+
+ Press(keyboard.numpadMinusKey);
+ for (int i = 0; i < 10; i++) c.Tick(0.1f);
+ Assert.AreEqual(100000f, rig.Distance, 1f, "the numpad's - held for a second undoes it");
+ }
+ finally { Send(c, "OnDisable"); Object.DestroyImmediate(go); }
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/PromoHudTests.cs b/unity/Assets/Tests/EditMode/PromoHudTests.cs
index 86a7a5f86..69afc03ac 100644
--- a/unity/Assets/Tests/EditMode/PromoHudTests.cs
+++ b/unity/Assets/Tests/EditMode/PromoHudTests.cs
@@ -489,7 +489,8 @@ public class PromoHudTests
Assert.AreSame(start, start.focusController.focusedElement, $"{name}: a {d} move left focus where it was");
}
foreach (var key in new[] { KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.Q, KeyCode.E,
- KeyCode.PageUp, KeyCode.PageDown, KeyCode.Home, KeyCode.End })
+ KeyCode.PageUp, KeyCode.PageDown, KeyCode.Home, KeyCode.End,
+ KeyCode.Equals, KeyCode.Minus, KeyCode.KeypadPlus, KeyCode.KeypadMinus })
{
using (var e = KeyDownEvent.GetPooled((char)0, key, EventModifiers.None)) { e.target = start; start.SendEvent(e); }
yield return null;