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 first-turn guide's steps follow what is on screen (fixes #1323) (#1331)
Eyal Golanfd8d34242b33
diff --git a/changelog.d/1323.md b/changelog.d/1323.mdnew file mode 100644index 000000000..d1d0647b1--- /dev/null+++ b/changelog.d/1323.md@@ -0,0 +1,6 @@+### Fixed+- The first-turn guide's steps now match what is on screen (owner's report on v0.1.4, #1323). Answering the decision card+ on the event step moves the guide on to Objectives with the focus on Next, instead of leaving "An event card" up with+ no card. With no card to explain, the event step is left out and the guide counts five steps. The unit card the move+ step opens is put away when the guide moves on. On the city steps the panel shows only after the info panel is laid+ out, so it no longer jumps from the bottom to the top.diff --git a/unity/Assets/Scripts/UI/EventCardPresenter.cs b/unity/Assets/Scripts/UI/EventCardPresenter.csindex 065ac66c0..2991f6883 100644--- a/unity/Assets/Scripts/UI/EventCardPresenter.cs+++ b/unity/Assets/Scripts/UI/EventCardPresenter.cs@@ -488,9 +488,12 @@ namespace Statecraft.Game.UIvoid Choose(GameEvent e, EventOption o){// The bridge raises Refused (the toast shows it) or Changed (the toast clears and Render shows the next event,- // which takes the focus). Answered from the keyboard with no event left, the focus goes to End Turn (#41 M1).+ // which takes the focus). Answered from the keyboard with no event left, the focus goes to End Turn (#41 M1),+ // unless a control outside the card took it meanwhile (the first-turn guide's Next, as it moves on, #1323).bridge.Queue(new ChooseEventOption(e.Id, o.Id));- if (Shown == null && presses.ByKeyboard) FocusReturn.To(hudRoot.Q<Button>("end-turn"));+ var focused = hudRoot.focusController?.focusedElement as VisualElement;+ bool kept = focused != null && focused.panel != null && !layer.Contains(focused);+ if (Shown == null && presses.ByKeyboard && !kept) FocusReturn.To(hudRoot.Q<Button>("end-turn"));}}}diff --git a/unity/Assets/Scripts/UI/FirstTurnGuidePresenter.cs b/unity/Assets/Scripts/UI/FirstTurnGuidePresenter.csindex 0b7f10d93..e91da5f26 100644--- a/unity/Assets/Scripts/UI/FirstTurnGuidePresenter.cs+++ b/unity/Assets/Scripts/UI/FirstTurnGuidePresenter.cs@@ -20,9 +20,11 @@ namespace Statecraft.Game.UI/// the "seen" line in the settings file, SettingsStore), never the sim, the queued orders or the save. Steps 2 and 3/// show a city with a training facility (<see cref="CityHex"/>) in the info panel, step 4 a unit that can move/// (<see cref="UnitToShow"/>) with its card's Move button, and the selection the guide found is put back when it- /// closes; the decision card is hidden ("Decision waiting") except on step 5 and put back as it was- /// when the guide closes. The guide takes no pointer outside its panel, so the HUD stays usable under it (the browser- /// checks press End Turn with it open); End Turn closes it as the turn advances.</para></summary>+ /// moves on to a step that does not explain it, or closes; the decision card is hidden ("Decision waiting") except on+ /// step 5 and put back as it was when the guide closes. Step 5 is left out while there is no card to explain (the+ /// guide then counts five steps), and answering the card on it moves the guide on (owner's report on v0.1.4, #1323).+ /// The guide takes no pointer outside its panel, so the HUD stays usable under it (the browser checks press End Turn+ /// with it open); End Turn closes it as the turn advances.</para></summary>public sealed class FirstTurnGuidePresenter : IDisposable{/// <summary>On the "root" element while the guide shows (Statecraft.uss shows its layer).</summary>@@ -54,6 +56,7 @@ namespace Statecraft.Game.UIreadonly SimBridge? bridge;readonly Action onNext, onSkip, onReopen;readonly Action<IReadOnlyList<Rejection>> onTurn;+ readonly Action onChanged;readonly IVisualElementScheduledItem follow;readonly (VisualElement element, float px)[] floored;/// <summary>The scale the text floor was last applied for (0: not yet).</summary>@@ -66,6 +69,10 @@ namespace Statecraft.Game.UIint openTurn;/// <summary>Puts back the selection the guide found, once it has shown its city or unit (null: it has not).</summary>Action? putBack;+ /// <summary>The event step has shown since the guide opened, so the count keeps it after its card is answered.</summary>+ bool eventShown;+ /// <summary>The frame a step was shown on: the panel is placed, and shown, only once that frame's layout has run.</summary>+ int shownFrame = -1;/// <param name="store">The settings file, where the guide notes that it has shown (null: it never shows by itself).</param>/// <param name="card">The decision card, hidden except on the event step (null: none).</param>@@ -94,7 +101,8 @@ namespace Statecraft.Game.UIonNext = Next; onSkip = Close; onReopen = Open;next.clicked += onNext; skip.clicked += onSkip; reopenButton.clicked += onReopen;onTurn = _ => TurnEnded();- if (bridge != null) bridge.TurnEnded += onTurn;+ onChanged = GameChanged;+ if (bridge != null) { bridge.TurnEnded += onTurn; bridge.Changed += onChanged; }// The targets move with the layout (a window resized, the info panel re-rendered after an order), so the ring// follows them while the guide shows.follow = layer.schedule.Execute(Place).Every(100);@@ -134,6 +142,7 @@ namespace Statecraft.Game.UI}hudRoot.AddToClassList(OpenClass);follow.Resume();+ eventShown = false;Show(0);}@@ -141,7 +150,28 @@ namespace Statecraft.Game.UIpublic void Next(){if (!IsOpen) return;- if (Step + 1 >= Steps.Length) Close(); else Show(Step + 1);+ var next = NextStep(Step);+ if (next < 0) Close(); else Show(next);+ }++ /// <summary>Whether a step is in the tour now: every step but the event card's, which is left out while there is no+ /// card to explain (answered before the guide reached it; owner's report on v0.1.4).</summary>+ bool InTour(int step) => step != EventStep || card?.Shown != null;++ /// <summary>The step after <paramref name="step"/> in the tour, or -1 after the last.</summary>+ int NextStep(int step)+ {+ for (int s = step + 1; s < Steps.Length; s++) if (InTour(s)) return s;+ return -1;+ }++ /// <summary>The game changed under the open guide (an order queued, the card answered): on the event step, once the+ /// card it explains has closed with no other card to show, the guide goes on to the next step, as Next would, and Next+ /// takes the focus back from the answer the player pressed.</summary>+ void GameChanged()+ {+ if (disposed || !IsOpen || Step != EventStep || card?.Shown != null) return;+ Next();}/// <summary>Escape: skips the guide. True when it was open.</summary>@@ -190,10 +220,22 @@ namespace Statecraft.Game.UI{Step = step;var key = Steps[step];- count.text = text.Format("guide.step", step + 1, Steps.Length);+ if (step == EventStep) eventShown = true;+ // Counted as the player meets them: the event step counts while it has a card or once it has shown.+ bool Counted(int s) => s == EventStep ? eventShown || InTour(s) : true;+ int number = 0, total = 0;+ for (int s = 0; s < Steps.Length; s++) if (Counted(s)) { total++; if (s <= step) number++; }+ count.text = text.Format("guide.step", number, total);title.text = text["guide." + key + ".title"];words.text = text["guide." + key + ".text"];- next.text = text[step + 1 == Steps.Length ? "guide.done" : "guide.next"];+ next.text = text[NextStep(step) < 0 ? "guide.done" : "guide.next"];+ // A step that does not explain the city or the unit puts away the one an earlier step showed (as Close does), so+ // no stale panel stays under the event card or Objectives.+ if (key != "city" && key != "training" && key != "move")+ {+ var back = putBack; putBack = null;+ back?.Invoke();+ }// The card shows only on its own step: elsewhere its backdrop would dim the parts the other steps point at.if (card?.Shown != null){@@ -205,10 +247,30 @@ namespace Statecraft.Game.UIputBack ??= keepSelection?.Invoke(); // what the guide found, before it first shows its city or unitif (key == "move") showUnit?.Invoke(); else showCity?.Invoke();}- Place();+ // The panel is placed once this frame's layout has run (the city or unit just shown has its size only then), and+ // transparent until then, so it never shows for a frame in the half its target is about to take (steps 2 and 3).+ shownFrame = Frame();+ if (shownFrame < 0) Place(); // EditMode: laid out on demand+ else+ {+ dock.style.opacity = 0f; // transparent, not hidden: a hidden Next could not take the focus below+ ring.style.display = DisplayStyle.None; // nor the ring round the last step's target+ layer.schedule.Execute(PlaceWhenLaidOut);+ }FocusReturn.To(next); // Enter goes on to the next step; again once styles resolve, as the layer may only now show}+ /// <summary>The frame count in play (the layout runs once a frame, after the scheduler), or -1 in EditMode, where the+ /// tests lay panels out on demand and Place runs at once.</summary>+ static int Frame() => Application.isPlaying ? Time.frameCount : -1;++ void PlaceWhenLaidOut()+ {+ if (disposed || !IsOpen) return;+ if (shownFrame >= 0 && Frame() == shownFrame) { layer.schedule.Execute(PlaceWhenLaidOut); return; }+ Place();+ }+/// <summary>The elements a step points at, as the HUD has them now (the ring goes round the shown ones): Attention in/// the top bar; the city's housing, jobs and workers rows in the info panel (GddHexPanel); its training rows; the unit/// card's Move button (#1265; it shows only while the card's unit can move); the card's options, or "Decision@@ -255,6 +317,9 @@ namespace Statecraft.Game.UIinternal void Place(){if (!IsOpen) return;+ if (shownFrame >= 0 && Frame() == shownFrame) return; // not before the step's layout (PlaceWhenLaidOut)+ shownFrame = -1;+ dock.style.opacity = StyleKeyword.Null;ApplyTextFloor();var area = TargetArea();ring.style.display = area == null ? DisplayStyle.None : DisplayStyle.Flex;@@ -324,7 +389,7 @@ namespace Statecraft.Game.UIStep = -1;putBack = null; // a game going away: nothing to put backhudRoot.RemoveFromClassList(OpenClass);- if (bridge != null) bridge.TurnEnded -= onTurn;+ if (bridge != null) { bridge.TurnEnded -= onTurn; bridge.Changed -= onChanged; }next.clicked -= onNext; skip.clicked -= onSkip; reopenButton.clicked -= onReopen;}}diff --git a/unity/Assets/Tests/EditMode/FirstTurnGuideTests.cs b/unity/Assets/Tests/EditMode/FirstTurnGuideTests.csindex e1f3a79c8..73aaa48a1 100644--- a/unity/Assets/Tests/EditMode/FirstTurnGuideTests.cs+++ b/unity/Assets/Tests/EditMode/FirstTurnGuideTests.cs@@ -379,6 +379,107 @@ public class FirstTurnGuideTestsAssert.IsNull(info.Selected, "and no hex");}+ // --- Each step with what it explains (owner's report on v0.1.4) ---------------------------------------------------++ /// <summary>The first card's first option, pressed as a click would.</summary>+ static void AnswerTheCard() => UiTestKit.Press(Ui.Q("event-options").Query<Button>().First());++ /// <summary>The owner's report on v0.1.4 ("they open for a split second after the user closes the screen they were+ /// supposed to explain"), path A: the player answers the card while the event step explains it. The card closes, so the+ /// guide goes on to the next step at once, with Next focused, instead of explaining a card that is gone.</summary>+ [Test]+ public void AnsweringTheCardOnItsStepGoesOnToTheNext()+ {+ var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));+ game.Begin(Inputs, 0, startScreen: true);+ UiTestKit.Press(Ui.Q<Button>("start-new-game"));+ var guide = game.Guide!; var card = game.Card!;+ for (int i = 0; i < FirstTurnGuidePresenter.EventStep; i++) UiTestKit.Press(Ui.Q<Button>("guide-next"));+ Assert.AreEqual(FirstTurnGuidePresenter.EventStep, guide.Step, "precondition: the event step");+ Assert.IsTrue(card.Shown != null && !card.Collapsed, "precondition: its card open");+ AnswerTheCard();+ Assert.IsNull(card.Shown, "precondition: the opening turn has one card, now answered");+ Assert.IsTrue(guide.IsOpen);+ Assert.AreEqual(Array.IndexOf(FirstTurnGuidePresenter.Steps, "objectives"), guide.Step, "the guide goes on to Objectives");+ Assert.AreEqual(En.Format("guide.step", 6, 6), Ui.Q<Label>("guide-count").text, "the count the player was shown stays");+ Assert.AreEqual(En["guide.objectives.title"], Ui.Q<Label>("guide-title").text);+ Assert.AreSame(Ui.Q<Button>("guide-next"), Ui.Q("root").focusController.focusedElement, "Next has the focus again");+ }++ /// <summary>Path B: the card was answered before the guide reaches its event step (a returning player opening it from How+ /// to play, or one who answered it under the guide): there is no card to explain, so the step is left out and the+ /// guide counts five steps.</summary>+ [Test]+ public void WithNoCardTheEventStepIsLeftOut()+ {+ var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));+ game.Begin(Inputs, 0, startScreen: true);+ UiTestKit.Press(Ui.Q<Button>("start-new-game"));+ var guide = game.Guide!; var card = game.Card!;+ guide.Close();+ AnswerTheCard();+ Assert.IsNull(card.Shown, "precondition: no card left this turn");+ UiTestKit.Press(Ui.Q<Button>("nav-how-to-play")); UiTestKit.Press(Ui.Q<Button>("how-to-play-guide"));+ Assert.IsTrue(guide.IsOpen);+ var seen = new List<string>();+ for (int shown = 1; guide.IsOpen; shown++)+ {+ seen.Add(FirstTurnGuidePresenter.Steps[guide.Step]);+ Assert.AreEqual(En.Format("guide.step", shown, 5), Ui.Q<Label>("guide-count").text, $"step {shown} of five");+ Assert.AreEqual(En[shown == 5 ? "guide.done" : "guide.next"], Ui.Q<Button>("guide-next").text);+ Assert.LessOrEqual(shown, 5, "five steps, then Done");+ UiTestKit.Press(Ui.Q<Button>("guide-next"));+ }+ CollectionAssert.AreEqual(new[] { "attention", "city", "training", "move", "objectives" }, seen);+ }++ /// <summary>The city or unit the guide shows for steps 2 to 4 is put away when the guide moves on to a step that does not+ /// explain it (the event card, Objectives), with or without a card, so no stale panel stays under the later steps.</summary>+ [Test]+ public void LeavingTheMoveStepPutsItsUnitCardAway([Values(false, true)] bool cardAnswered)+ {+ var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));+ game.Begin(Inputs, 0, startScreen: true);+ UiTestKit.Press(Ui.Q<Button>("start-new-game"));+ var guide = game.Guide!; var info = game.Info!; var units = game.Units!;+ if (cardAnswered) { guide.Close(); AnswerTheCard(); guide.Open(); }+ var move = Array.IndexOf(FirstTurnGuidePresenter.Steps, "move");+ for (int i = 0; i < move; i++) UiTestKit.Press(Ui.Q<Button>("guide-next"));+ Assert.IsTrue(info.ShowsUnit && units.Selected != null, "precondition: the move step shows a unit's card");+ UiTestKit.Press(Ui.Q<Button>("guide-next"));+ Assert.AreEqual(cardAnswered ? "objectives" : "event", FirstTurnGuidePresenter.Steps[guide.Step]);+ Assert.IsFalse(info.ShowsUnit, "the unit's card is put away");+ Assert.IsNull(units.Selected, "and its unit unselected, as the guide found it");+ Assert.IsNull(info.Selected, "with no hex shown either");+ }++ /// <summary>A closed or disposed guide takes no notice of the game changing (it listens to SimBridge.Changed only while it+ /// lives, and acts on it only while it is open).</summary>+ [Test]+ public void AClosedGuideIgnoresTheGameChanging()+ {+ var game = OpenScene(new SaveStore(TempDir()), new SettingsStore(TempDir()));+ game.Begin(Inputs, 0, startScreen: true);+ UiTestKit.Press(Ui.Q<Button>("start-new-game"));+ var guide = game.Guide!;+ for (int i = 0; i < FirstTurnGuidePresenter.EventStep; i++) UiTestKit.Press(Ui.Q<Button>("guide-next"));+ UiTestKit.Press(Ui.Q<Button>("guide-skip"));+ AnswerTheCard();+ Assert.IsFalse(guide.IsOpen, "closed, it stays closed");+ Assert.AreEqual(-1, guide.Step);+ Assert.IsFalse(Ui.Q("root").ClassListContains(FirstTurnGuidePresenter.OpenClass));++ using var rig = new Rig(En);+ var bridge = rig.Bridge;+ var lone = new FirstTurnGuidePresenter(rig.Ui.Root, En, null, rig.Card, bridge: bridge);+ lone.Dispose();+ rig.Guide.Open(); // another guide open on the same HUD (the root's class)+ for (int i = 0; i < FirstTurnGuidePresenter.EventStep; i++) rig.Guide.Next();+ UiTestKit.Press(rig.Ui.Root.Q("event-options").Query<Button>().First());+ Assert.IsNull(rig.Card.Shown, "precondition: the card answered");+ Assert.AreEqual(-1, lone.Step, "the disposed guide was not called");+ }+// --- Presentation only ------------------------------------------------------------------------------------------/// <summary>The whole guide, by itself, through How to play, through every step, Skip and Escape: the state hash, the