diff --git a/Assets/Scripts/UI/MainMenu.cs b/Assets/Scripts/UI/MainMenu.cs index 88600e516..e7aba3357 100644 --- a/Assets/Scripts/UI/MainMenu.cs +++ b/Assets/Scripts/UI/MainMenu.cs @@ -7,7 +7,9 @@ using System.Threading; using System.Threading.Tasks; using TMPro; using UniRx; +using UniRx.Triggers; using UnityEngine; +using UnityEngine.EventSystems; using UnityEngine.SceneManagement; using UnityEngine.UI; using static CultMath.math; @@ -139,36 +141,74 @@ public class MainMenu : MonoBehaviour _nextMenu.panel.AddButton("Credits", () => { - ShowCredits(); - Fade(true); + if (ShowCredits()) Fade(true); }); _nextMenu.panel.AddButton("Quit", Application.Quit); } - // The third-party attributions the build must show, one button per entry with its notice in the detail pane. The - // list is Credits.All; this only renders it. - private void ShowCredits() + // The third-party attributions the build must show, one button per entry; the selected entry's name, licence and one + // row per notice line fill the detail pane. The list is Credits.All; this only renders it. The screen follows the + // New Game screen: list rows are out of automatic navigation, the focused Back button takes Up and Down to step the + // selection and Cancel to leave, and a click on empty space does not drop that focus. + // False when the list could not be built and a dialog says why; the menu stays where it was. + private bool ShowCredits() { - var credits = Credits.All(Resources.Load(Credits.NounProjectResource).text); + var attribution = Resources.Load(Credits.NounProjectResource); + if (attribution == null) + { + Refuse("Credits are unavailable", $"{Credits.NounProjectResource} is missing from this build."); + return false; + } + IReadOnlyList credits; + try { credits = Credits.All(attribution.text); } + catch (InvalidOperationException error) + { + Refuse("Credits are unavailable", error.Message); + return false; + } Next("credits"); - _nextMenu.detail.gameObject.SetActive(true); - _nextMenu.detail.Title.text = _nextMenu.panel.Title.text; - _nextMenu.detail.Title.color = Color.clear; - var selected = 0; - _nextMenu.detail.AddProperty(() => credits[selected].Name); - _nextMenu.detail.AddProperty(() => credits[selected].Licence); - _nextMenu.detail.AddProperty(() => credits[selected].Notice); + var list = _nextMenu.panel; + var detail = _nextMenu.detail; + detail.gameObject.SetActive(true); + var focus = new FocusHold(); + var selected = -1; + Button back = null; + void Leave() + { + FocusHold.Focus(null); + focus.Release(); + ShowMain(); + Fade(false); + } + void Select(int index) + { + selected = Mathf.Clamp(index, 0, credits.Count - 1); + var credit = credits[selected]; + detail.Clear(); + detail.Title.text = list.Title.text; + detail.Title.color = Color.clear; + detail.AddProperty(() => credit.Name); + detail.AddProperty(() => credit.Licence); + detail.AddSpacer(); + foreach (var line in credit.Notice.Split('\n')) detail.AddProperty(() => line); + FocusHold.Focus(back.gameObject); + } for (var i = 0; i < credits.Count; i++) { var index = i; - _nextMenu.panel.AddButton(credits[i].Name, () => selected = index); + list.AddButton(credits[i].Name, () => Select(index)).Button.navigation = new Navigation { mode = Navigation.Mode.None }; } - _nextMenu.panel.AddButton("Back", - () => - { - ShowMain(); - Fade(false); - }); + back = list.AddButton("Back", Leave).Button; + back.navigation = new Navigation { mode = Navigation.Mode.None }; + back.OnMoveAsObservable().Subscribe(move => + { + if (move.moveDir == MoveDirection.Up) Select(selected - 1); + else if (move.moveDir == MoveDirection.Down) Select(selected + 1); + }); + back.OnCancelAsObservable().Subscribe(_ => Leave()); + focus.Hold(); + Select(0); + return true; } // New Game: every way to set up a run, each with its brief. A release build lists only the modes; the other ways diff --git a/Assets/Scripts/UI/ScenarioPicker.cs b/Assets/Scripts/UI/ScenarioPicker.cs index e0cd93134..f5f3a1ca4 100644 --- a/Assets/Scripts/UI/ScenarioPicker.cs +++ b/Assets/Scripts/UI/ScenarioPicker.cs @@ -30,7 +30,7 @@ public sealed class ScenarioPicker private static readonly Color Accent = new Color(1, .4458f, .05f); private int _selected; private bool _launched; - private bool _restoreDeselect; + private readonly FocusHold _focus = new FocusHold(); private ScenarioPicker(PropertiesPanel list, PropertiesPanel detail, Action launch, Action back) { @@ -115,7 +115,7 @@ public sealed class ScenarioPicker picker._launchButton.OnCancelAsObservable().Subscribe(_ => picker.Back()); picker.StyleScrollbars(); - picker.HoldFocus(); + picker._focus.Hold(); picker.Select(0); // Layout is not live until the frame after Show; edit mode has no frames. if (Application.isPlaying) Observable.NextFrame().Subscribe(_ => picker.FitViewport()); @@ -229,7 +229,7 @@ public sealed class ScenarioPicker _rows[i].colors = colors; _rows[i].targetGraphic.color = i == _selected ? Accent : _rowFills[i]; } - Focus(_launchButton.gameObject); + FocusHold.Focus(_launchButton.gameObject); Reveal((RectTransform) _rows[_selected].transform); } @@ -253,36 +253,42 @@ public sealed class ScenarioPicker { if (_launched) return; _launched = true; - Focus(null); - ReleaseFocus(); + FocusHold.Focus(null); + _focus.Release(); _launch(Selected); } // Leaves the screen; the same path for the Back button and Cancel. private void Back() { - Focus(null); - ReleaseFocus(); + FocusHold.Focus(null); + _focus.Release(); _back(); } +} - // While the picker is up, a click on empty space must not drop focus from Launch, or the arrows and Enter go dead. - // The input module's flag is scene-wide, so the picker takes it for its own lifetime and gives it back on leaving. - private void HoldFocus() +// Keeps keyboard and gamepad focus on one control while a menu screen is up. While held, a click on empty space must +// not drop focus, or the arrows and Enter go dead. The input module's flag is scene-wide, so the screen takes it for its +// own lifetime and gives it back on leaving. The New Game and Credits screens both use it. +public sealed class FocusHold +{ + private bool _restoreDeselect; + + public void Hold() { if (EventSystem.current == null || !(EventSystem.current.currentInputModule is InputSystemUIInputModule module) || !module.deselectOnBackgroundClick) return; module.deselectOnBackgroundClick = false; _restoreDeselect = true; } - private void ReleaseFocus() + public void Release() { if (!_restoreDeselect) return; _restoreDeselect = false; if (EventSystem.current != null && EventSystem.current.currentInputModule is InputSystemUIInputModule module) module.deselectOnBackgroundClick = true; } - private static void Focus(GameObject target) + public static void Focus(GameObject target) { if (EventSystem.current != null) EventSystem.current.SetSelectedGameObject(target); } diff --git a/tests/Aetheria.Shared.Tests/CreditsTests.cs b/tests/Aetheria.Shared.Tests/CreditsTests.cs index b346f1fca..ed322bef4 100644 --- a/tests/Aetheria.Shared.Tests/CreditsTests.cs +++ b/tests/Aetheria.Shared.Tests/CreditsTests.cs @@ -190,12 +190,15 @@ namespace Aetheria.Shared.Tests { var menu = Regex.Replace(Read("Assets/Scripts/UI/MainMenu.cs"), @"/\*.*?\*/|//[^\n]*", "", RegexOptions.Singleline); var main = menu.Substring(menu.IndexOf("private void ShowMain()", StringComparison.Ordinal)); - main = main.Substring(0, main.IndexOf("private void ShowCredits()", StringComparison.Ordinal)); + main = main.Substring(0, main.IndexOf("private bool ShowCredits()", StringComparison.Ordinal)); Assert.Contains("AddButton(\"Credits\"", main); Assert.Contains("ShowCredits()", main); - var credits = menu.Substring(menu.IndexOf("private void ShowCredits()", StringComparison.Ordinal)); + var credits = menu.Substring(menu.IndexOf("private bool ShowCredits()", StringComparison.Ordinal)); credits = credits.Substring(0, credits.IndexOf("private void ShowScenarios()", StringComparison.Ordinal)); Assert.Contains("Credits.All(", credits); + // A build missing the attribution file refuses with a dialog instead of throwing in the click handler. + Assert.Contains("attribution == null", credits); + Assert.Contains("Refuse(", credits); } } }