A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Minimap: Borders/Labels buttons start from the map's switches after a restart or language switch (#2231)
Eyal Golanc2947ef41c0f
diff --git a/changelog.d/2227-minimap-toggles.md b/changelog.d/2227-minimap-toggles.mdnew file mode 100644index 000000000..444c35527--- /dev/null+++ b/changelog.d/2227-minimap-toggles.md@@ -0,0 +1,5 @@+### Fixed++- After a language switch, New Game or a load, the minimap's Borders and Labels buttons keep what the map shows. A+ layer the player switched off stays off with its button dimmed, and one press brings it back; before, the first press+ did nothing and the border caption showed over hidden borders.diff --git a/unity/Assets/Scripts/Map/MapBorders.cs b/unity/Assets/Scripts/Map/MapBorders.csindex 80d78f517..9c61c9797 100644--- a/unity/Assets/Scripts/Map/MapBorders.cs+++ b/unity/Assets/Scripts/Map/MapBorders.cs@@ -212,6 +212,9 @@ namespace Statecraft.Game.Mapstatic bool hasLayer, toggleOn = true;static bool noEarlyOut;+ /// <summary>Test seam (internal): the "borders" switch as last set (on at every build), drawn or not (#2227).</summary>+ internal static bool ToggleOn => toggleOn;+/// <summary>Test seam (_ScBorders.z): true draws every pixel the long way, with no early-out, so a test can prove the/// early-out exact (the two renders must match pixel for pixel).</summary>internal static bool NoEarlyOut { get => noEarlyOut; set { noEarlyOut = value; Push(); } }diff --git a/unity/Assets/Scripts/UI/GameRoot.cs b/unity/Assets/Scripts/UI/GameRoot.csindex 603b532ee..a7a4b87b8 100644--- a/unity/Assets/Scripts/UI/GameRoot.cs+++ b/unity/Assets/Scripts/UI/GameRoot.cs@@ -62,6 +62,8 @@ namespace Statecraft.Game.UIinternal CaptureDialogPresenter? Capture => capture;/// <summary>Tests: the settlement name pills of the last Begin.</summary>internal MapLabelsPresenter? Labels => labels;+ /// <summary>Tests: the minimap of the last Begin.</summary>+ internal MinimapPresenter? Minimap => minimap;string? hoveredCity; // the city HoverCity lit (#1063)OutcomePresenter? outcomes;TurnReportPresenter? report;@@ -501,6 +503,7 @@ namespace Statecraft.Game.UIcameraController.Settle(); SyncMinimap(); labels?.Refresh();};mini.BordersChanged += ShowLayerLabel;+ ShowLayerLabel(); // #2227: the caption was set before this minimap was made; a restart keeps borders the player hidAction onChanged = RenderPlacementsIfChanged;bridge.Changed += onChanged;// One console line per turn, so a browser run (reload, Continue) can be checked against the hash.diff --git a/unity/Assets/Scripts/UI/MinimapPresenter.cs b/unity/Assets/Scripts/UI/MinimapPresenter.csindex 4308e830d..75faa59e8 100644--- a/unity/Assets/Scripts/UI/MinimapPresenter.cs+++ b/unity/Assets/Scripts/UI/MinimapPresenter.cs@@ -55,6 +55,10 @@ namespace Statecraft.Game.UIframe.RegisterCallback<GeometryChangedEvent>(OnBoxGeometry);overview = box.Q<Button>("minimap-overview"); labels = box.Q<Button>("minimap-labels"); borders = box.Q<Button>("minimap-borders");overview.clicked += OnOverview; labels.clicked += OnLabels; borders.clicked += OnBorders;+ // #2227: GameRoot.Begin (New Game, a load, the language switch) makes a new presenter over the same document and+ // the same map, whose switches keep what the last press set; a button dims only together with its switch+ // (Switch), so the dim says what the map shows and the new presenter starts from it, keeping the player's choice.+ LabelsShown = !labels.ClassListContains(OffClass); BordersShown = !borders.ClassListContains(OffClass);if (text != null){overview.tooltip = text["minimap.overview"]; labels.tooltip = text["minimap.labels"]; borders.tooltip = text["minimap.borders"];diff --git a/unity/Assets/Tests/EditMode/MinimapRestartTests.cs b/unity/Assets/Tests/EditMode/MinimapRestartTests.csnew file mode 100644index 000000000..825fb2d3f--- /dev/null+++ b/unity/Assets/Tests/EditMode/MinimapRestartTests.cs@@ -0,0 +1,106 @@+#nullable enable+using System;+using System.Collections;+using System.IO;+using System.Linq;+using NUnit.Framework;+using Statecraft.Game;+using Statecraft.Game.Map;+using Statecraft.Game.UI;+using UnityEditor.SceneManagement;+using UnityEngine;+using UnityEngine.TestTools;+using UnityEngine.UIElements;+using Object = UnityEngine.Object;++/// <summary>#2227: GameRoot.Begin (New Game, a load, the language switch) makes a new MinimapPresenter over the same+/// document and the same map, which is built once at boot. Its Borders and Labels buttons read "on" while the map's+/// "borders" and "labels" switches stayed off from the last press, so the next press "turned off" something already off.+/// Same harness as ScreenEntryTests.</summary>+public class MinimapRestartTests+{+ string saveDir = "";+ RenderTexture? rt; PanelSettings? ps;+ VisualElement ui = null!;+ GameRoot root = null!;++ [SetUp] public void TempSaves() => saveDir = Path.Combine(Path.GetTempPath(), "statecraft-minimaprestart-" + 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 Frames(int n) { for (int i = 0; i < n; i++) yield return null; }++ IEnumerator Boot()+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var doc = Object.FindFirstObjectByType<UIDocument>();+ rt = new RenderTexture(1280, 720, 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.Begin(MapFocusTests.Inputs, 0);+ ui = doc.rootVisualElement;+ for (int i = 0; i < 30 && !(ui.Q("top-bar").worldBound.height > 0); i++) yield return null;+ yield return Frames(3);+ GameSceneTests.HideTheOpeningCard(ui);+ yield return Frames(2);+ }++ /// <summary>The "labels" switch's real state: MapLabelsPresenter's switch hides the names' container inline.</summary>+ bool LabelsOn => ui.Q(MapLabelsPresenter.ContainerName).style.display.value != DisplayStyle.None;++ bool CaptionShown => ui.Q<Label>("map-layer").style.display.value == DisplayStyle.Flex;++ /// <summary>Borders and Labels pressed off, then the language switch's Begin (GameRoot.SwitchLanguage: the same game,+ /// kept): each button of the new minimap says what its switch really does, the player's choice is kept, and one press+ /// brings the layer back.</summary>+ [UnityTest]+ public IEnumerator AfterARestartTheMinimapButtonsMatchTheMapsSwitches()+ {+ yield return Boot();+ try+ {+ Assert.IsTrue(VisualToggles.Names.Contains(MapBorders.ToggleName) && VisualToggles.Names.Contains(MinimapPresenter.LabelsToggle), "precondition: the map's switches");+ var first = root.Minimap ?? throw new AssertionException("precondition: Begin made a minimap");+ UiTestKit.Press(ui.Q<Button>("minimap-borders"));+ UiTestKit.Press(ui.Q<Button>("minimap-labels"));+ Assert.IsFalse(MapBorders.ToggleOn, "precondition: the borders switch is off");+ Assert.IsFalse(LabelsOn, "precondition: the labels switch is off");+ Assert.IsFalse(first.BordersShown || first.LabelsShown, "precondition: the first minimap's buttons read off");++ root.Language = UiLanguage.Hebrew; // as SwitchLanguage does: a new language over this game+ root.Begin(MapFocusTests.Inputs, 0, keep: root.Bridge);+ GameSceneTests.HideTheOpeningCard(ui);+ yield return Frames(3);+ var mini = root.Minimap ?? throw new AssertionException("precondition: the restart made a minimap");+ Assert.AreNotSame(first, mini, "precondition: Begin made a new minimap");++ Assert.AreEqual(MapBorders.ToggleOn, mini.BordersShown, "the Borders button says what the borders switch does");+ Assert.AreEqual(LabelsOn, mini.LabelsShown, "the Labels button says what the labels switch does");+ Assert.IsFalse(MapBorders.ToggleOn || LabelsOn, "the restart keeps the player's choice: both layers stay off");+ Assert.AreEqual(root.Regions!.Shown && MapBorders.ToggleOn, CaptionShown, "the border caption follows the borders");++ UiTestKit.Press(ui.Q<Button>("minimap-borders"));+ UiTestKit.Press(ui.Q<Button>("minimap-labels"));+ Assert.IsTrue(MapBorders.ToggleOn && mini.BordersShown, "one press brings the borders back");+ Assert.IsTrue(LabelsOn && mini.LabelsShown, "one press brings the names back");+ Assert.AreEqual(root.Regions!.Shown, CaptionShown, "and the border caption with them");+ }+ finally+ {+ // VisualToggles and MapBorders are static: leave both switches on for the next test.+ if (VisualToggles.Names.Contains(MapBorders.ToggleName)) VisualToggles.Set(MapBorders.ToggleName, true);+ if (VisualToggles.Names.Contains(MinimapPresenter.LabelsToggle)) VisualToggles.Set(MinimapPresenter.LabelsToggle, true);+ }+ }+}diff --git a/unity/Assets/Tests/EditMode/MinimapRestartTests.cs.meta b/unity/Assets/Tests/EditMode/MinimapRestartTests.cs.metanew file mode 100644index 000000000..89d474749--- /dev/null+++ b/unity/Assets/Tests/EditMode/MinimapRestartTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 4c961e9983a94ac185b50e71f9ef0f84\ No newline at end of file