A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Camera: keys off under a screen that covers the map; New Game opens on the opening view (Fixes #2712) (#2836)
Beri39f8768ff9ad
diff --git a/changelog.d/2712.md b/changelog.d/2712.mdnew file mode 100644index 000000000..39d0962ce--- /dev/null+++ b/changelog.d/2712.md@@ -0,0 +1,5 @@+### Fixed++- On a narrow window, where an open screen covers the whole map, the camera keys no longer move the hidden map;+ beside a desktop screen they still do. A New Game opens on the opening view again instead of where the last game's+ camera was left.diff --git a/unity/Assets/Scripts/Map/CameraController.cs b/unity/Assets/Scripts/Map/CameraController.csindex a272c3ce2..670f23c88 100644--- a/unity/Assets/Scripts/Map/CameraController.cs+++ b/unity/Assets/Scripts/Map/CameraController.cs@@ -90,6 +90,11 @@ namespace Statecraft.Game.Map/// screen's rows (NavigationGuard.Step, accessibility audit L4; the owner's answer on #2340, Q27: "do the recommended/// for all") and do not pan. Left, Right and WASD still pan. Null: the arrows always pan (B6).</summary>public Func<bool>? ScreenTakesUpDown;+ /// <summary>True while an open screen covers the whole map (GameRoot: a screen in the two-row or tight three-row HUD,+ /// #2712): keyboard pan, orbit and the zoom keys are then off, as under the start screen, since they would move a map+ /// the player cannot see. A partial desktop screen leaves the map beside it, so there the keys stay (plan amendment+ /// A4). The wheel and the drag need no gate: over the screen they are the UI's already (IsOverUi). Null: never.</summary>+ public Func<bool>? ScreenCoversMap;/// <summary>The trackpad pinch since the last frame, as the summed NSEvent magnification (positive = the fingers/// spread). macOS sends a pinch as magnify events, which Unity's player passes to neither the Input System nor the/// wheel (the owner found zooming did nothing in the macOS player, 27 Oct 2026), so TrackpadPinch.mm collects them@@ -229,9 +234,11 @@ namespace Statecraft.Game.Map}}var modifier = ModifierHeld(Keyboard.current);- var keyPan = pan.ReadValue<Vector2>();- if (ScreenTakesUpDown?.Invoke() == true) keyPan = WithoutUpDownArrows(keyPan, Keyboard.current);- var input = Gather(keyPan, drag, zoomRaw, orbit.ReadValue<float>(), down, pointer, zoomKeys.ReadValue<float>(), modifier);+ var keysOff = ScreenCoversMap?.Invoke() == true;+ var keyPan = keysOff ? Vector2.zero : pan.ReadValue<Vector2>();+ if (!keysOff && ScreenTakesUpDown?.Invoke() == true) keyPan = WithoutUpDownArrows(keyPan, Keyboard.current);+ var input = Gather(keyPan, drag, zoomRaw, keysOff ? 0f : orbit.ReadValue<float>(), down, pointer,+ keysOff ? 0f : zoomKeys.ReadValue<float>(), modifier);if (down) input.DragFromUi = fromUi; // latched at the actual press, even between camera frames// Classify and filter the current gesture before draining earlier, already-approved motion.// A new UI press owns only its own drag; OverUi must still block its wheel/magnify zoom.diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex 77bf85959..c8f9219b5 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -563,6 +563,11 @@ namespace Statecraft.Game.UI// Audit L4: while a screen is open, the Up and Down arrows are its rows' (NavigationGuard.Step), not the camera's.Func<bool> screenTakesUpDown = () => nav?.Current != null;cameraController.ScreenTakesUpDown = screenTakesUpDown;+ // #2712: in the two-row HUD (the tight three-row one too) an open screen covers the whole map, so the camera keys+ // are off while it shows, as under the start screen; a desktop screen leaves the map beside it (plan amendment A4).+ var hudRoot = root.Q("root") ?? root;+ Func<bool> screenCoversMap = () => nav?.Current != null && hudRoot.ClassListContains(HudPresenter.TwoRowClass);+ cameraController.ScreenCoversMap = screenCoversMap;Action<int, int, bool> onHex = (q, r, cityPill) =>{OnHexClicked(map, unitLayer, info, q, r, UnityEngine.InputSystem.Pointer.current is UnityEngine.InputSystem.Touchscreen, cityPill);@@ -630,6 +635,7 @@ namespace Statecraft.Game.UIif (cameraController.HoldsMouseDrag == holdsMouseDrag) cameraController.HoldsMouseDrag = null;if (cameraController.HoldsTouchDrag == holdsTouchDrag) cameraController.HoldsTouchDrag = null;if (cameraController.ScreenTakesUpDown == screenTakesUpDown) cameraController.ScreenTakesUpDown = null;+ if (cameraController.ScreenCoversMap == screenCoversMap) cameraController.ScreenCoversMap = null;if (pointer.Map == map) { pointer.Map = null; pointer.View = null; }cameraController.Bounds = oldBounds;cameraController.Ground = oldGround; cameraController.GroundTop = oldTop;@@ -772,10 +778,22 @@ namespace Statecraft.Game.UI// is no opening, so that route only plays the opening.Action freshGame = () => { if (opening != null) opening.Show(replay: false); else advisor?.NewGame(); };Action freshOpened = () => opening?.Show(replay: false);+ // #2712: a New Game keeps the map, so the camera goes back to the opening view here, as MapScene.Build sets it+ // at boot: Tel Aviv and Jaffa in frame at the start zoom.+ Action openingView = () =>+ {+ var cam = cameraController.GetComponent<Camera>();+ camRig.LookAt = CameraRig.OpeningLookAt(map.Frame, cam.aspect,+ map.Settlements.Single(s => s.Id == "tel_aviv"), map.Settlements.Single(s => s.Id == "jaffa"));+ camRig.Distance = CameraRig.StartDistance;+ cameraController.Settle(); SyncMinimap(); labels?.Refresh();+ };+ newGame.Started += openingView;newGame.Started += freshGame;if (start != null) start.FreshGameOpened += freshOpened;owned.Add(new Undo(() =>{+ newGame.Started -= openingView;newGame.Started -= freshGame;if (start != null) start.FreshGameOpened -= freshOpened;}));diff --git a/unity/Assets/Tests/EditMode/CameraScreenCoverTests.cs b/unity/Assets/Tests/EditMode/CameraScreenCoverTests.csnew file mode 100644index 000000000..881f20928--- /dev/null+++ b/unity/Assets/Tests/EditMode/CameraScreenCoverTests.cs@@ -0,0 +1,135 @@+#nullable enable+using System;+using System.Collections;+using System.IO;+using NUnit.Framework;+using Statecraft.Game;+using Statecraft.Game.Map;+using Statecraft.Game.UI;+using UnityEditor;+using UnityEditor.SceneManagement;+using UnityEngine;+using UnityEngine.InputSystem;+using UnityEngine.InputSystem.LowLevel;+using UnityEngine.TestTools;+using UnityEngine.UIElements;+using Object = UnityEngine.Object;++/// <summary>#2712, through GameRoot on Map.unity: (1) in the two-row HUD a side screen covers the whole map, so the camera+/// keys do not move the hidden map while one is open; in the one-row desktop layout the screen is a window beside the map+/// and the keys still move it (plan amendment A4). (2) New Game starts from the opening view, as a fresh boot does.</summary>+public class CameraScreenCoverTests : SharedCameraActionsFixture+{+ string saveDir = "";+ RenderTexture? rt; PanelSettings? ps;+ VisualElement ui = null!;+ GameRoot root = null!;++ [SetUp] public void TempSaves() => saveDir = Path.Combine(Path.GetTempPath(), "statecraft-cover-" + Guid.NewGuid().ToString("N"));++ [TearDown]+ public void DiscardScene()+ {+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);+ if (ps != null) Object.DestroyImmediate(ps);+ if (rt != null) { rt.Release(); Object.DestroyImmediate(rt); }+ if (Directory.Exists(saveDir)) Directory.Delete(saveDir, true);+ }++ IEnumerator Boot(int width, int height)+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var doc = Object.FindFirstObjectByType<UIDocument>();+ rt = new RenderTexture(width, height, 0);+ ps = Object.Instantiate(doc.panelSettings);+ ps.targetTexture = rt;+ doc.panelSettings = ps;+ Object.FindFirstObjectByType<MapScene>().Build(MapFocusTests.Inputs.Map);+ root = Object.FindFirstObjectByType<GameRoot>();+ root.Store = new SaveStore(saveDir);+ root.Language = UiLanguage.English;+ root.Begin(MapFocusTests.Inputs, 0);+ ui = doc.rootVisualElement;+ for (int i = 0; i < 30 && !(ui.Q("top-bar").worldBound.height > 0); i++) yield return null;+ for (int i = 0; i < 5; i++) yield return null;+ }++ static void Send(MonoBehaviour c, string message) =>+ c.GetType().GetMethod(message, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.Invoke(c, null);++ /// <summary>Item 1: with the Budget open, D (pan), = (zoom in) and Q (orbit) held for half a second leave the camera where+ /// it is in the two-row HUD and in the three-row one, which also carries the two-row class, and move it in the one-row+ /// one. The panel scales with the screen (1920x1080 reference, match 0.5; HudPresenter's tier note), so at a height of+ /// 1080 the logical width is width / ((width/1920 + 1) / 2): 600 is about 914 (three rows), 960 about 1280 (plain two+ /// rows; 1280 itself would be about 1536, one row), 1920 stays 1920 (one row). With the screen closed, every layout pans.</summary>+ [UnityTest]+ public IEnumerator TheCameraKeysStopOnlyUnderAScreenThatCoversTheMap([Values(600, 960, 1920)] int width)+ {+ InputSystem.actions = AssetDatabase.LoadAssetAtPath<InputActionAsset>("Assets/InputSystem_Actions.inputactions");+ var keyboard = InputSystem.AddDevice<Keyboard>();+ yield return Boot(width, 1080);+ bool twoRow = width < 1400;+ Assert.AreEqual(twoRow, ui.Q("root").ClassListContains(HudPresenter.TwoRowClass), "precondition: the HUD tier");+ Assert.AreEqual(width < 960, ui.Q("root").ClassListContains(HudPresenter.ThreeRowClass), "precondition: the HUD tier (three rows)");+ var cam = Object.FindFirstObjectByType<CameraController>();+ var rig = cam.GetComponent<CameraRig>();+ Send(cam, "Awake"); Send(cam, "OnEnable");+ cam.SetFixedNorth(false); // a gdd-0.3 game holds north (R-CAMERA-001), which zeroes Q/E anyway; free it so the orbit gate is what is seen+ try+ {+ (Vector3 moved, float zoomed, float turned) Hold(Key k)+ {+ var from = rig.LookAt; var distance = rig.Distance; var yaw = rig.Yaw;+ InputSystem.QueueStateEvent(keyboard, new KeyboardState(k)); InputSystem.Update();+ for (int i = 0; i < 5; i++) cam.Tick(0.1f);+ InputSystem.QueueStateEvent(keyboard, new KeyboardState()); InputSystem.Update();+ cam.Tick(0.1f);+ return (rig.LookAt - from, rig.Distance - distance, Mathf.Abs(Mathf.DeltaAngle(yaw, rig.Yaw)));+ }+ UiTestKit.Press(ui.Q<Button>("nav-budget"));+ for (int i = 0; i < 5; i++) yield return null;+ Assert.IsTrue(ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass), "precondition: the Budget is open");+ var pan = Hold(Key.D).moved.magnitude;+ var zoom = Mathf.Abs(Hold(Key.Equals).zoomed);+ var turn = Hold(Key.Q).turned;+ if (twoRow)+ {+ Assert.AreEqual(0f, pan, "two rows, screen open: D does not pan the hidden map");+ Assert.AreEqual(0f, zoom, "two rows, screen open: = does not zoom the hidden map");+ Assert.AreEqual(0f, turn, "two rows, screen open: Q does not orbit the hidden map");+ }+ else+ {+ Assert.Greater(pan, 1f, "one row, screen open: D still pans (A4)");+ Assert.Greater(zoom, 1f, "one row, screen open: = still zooms (A4)");+ Assert.Greater(turn, 1f, "one row, screen open: Q still orbits (A4)");+ }+ UiTestKit.Press(ui.Q<Button>("screen-close-budget")); // in three rows the screen is a full window: the top bar is off+ for (int i = 0; i < 5; i++) yield return null;+ Assert.IsFalse(ui.Q("root").ClassListContains(ScreenNav.ScreenOpenClass), "precondition: the Budget is closed");+ Assert.Greater(Hold(Key.D).moved.magnitude, 1f, "no screen open: D pans");+ }+ finally { Send(cam, "OnDisable"); }+ }++ /// <summary>Item 2: the player pans and zooms away, then presses New Game (twice, as the top bar asks): the camera is+ /// back at the opening view the boot gave, its look-at and its zoom (CameraRig.StartDistance).</summary>+ [UnityTest]+ public IEnumerator NewGameStartsFromTheOpeningView()+ {+ yield return Boot(1920, 1080);+ var rig = Object.FindFirstObjectByType<CameraController>().GetComponent<CameraRig>();+ var opening = rig.LookAt;+ Assert.AreEqual(CameraRig.StartDistance, rig.Distance, "precondition: the boot opens at the start zoom");+ rig.LookAt = opening + new Vector3(60000f, 0f, 80000f);+ rig.Distance = 120000f;+ rig.Apply();+ var session = root.Bridge!.Session;+ UiTestKit.Press(ui.Q<Button>("new-game"));+ UiTestKit.Press(ui.Q<Button>("new-game"));+ Assert.AreNotSame(session, root.Bridge!.Session, "precondition: New Game replaced the session");+ Assert.AreEqual(opening.x, rig.LookAt.x, 1f, "New Game: the opening look-at (x)");+ Assert.AreEqual(opening.z, rig.LookAt.z, 1f, "New Game: the opening look-at (z)");+ Assert.AreEqual(CameraRig.StartDistance, rig.Distance, 1f, "New Game: the opening zoom");+ }+}diff --git a/unity/Assets/Tests/EditMode/CameraScreenCoverTests.cs.meta b/unity/Assets/Tests/EditMode/CameraScreenCoverTests.cs.metanew file mode 100644index 000000000..1b7cba4e8--- /dev/null+++ b/unity/Assets/Tests/EditMode/CameraScreenCoverTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 0a5f17c730b0ddcf18f8e2fdc03aa29a\ No newline at end of file