Credits screen: one row per notice line, refuse a missing attribution file, follow the New Game focus handling

ShowCredits renders the selected credit as property rows instead of one multi-line label, refuses with a dialog when
the attribution TextAsset is missing or malformed, takes list rows out of automatic navigation, steps with Up/Down on
the focused Back button and leaves on Cancel. ScenarioPicker's focus hold moves to FocusHold so both screens share it.
The menu wiring test now strips comments before matching.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Nibu 2026-11-12 12:48:52 +00:00
commit 390b4c7a45
3 changed files with 83 additions and 34 deletions

View file

@ -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<TextAsset>(Credits.NounProjectResource).text);
var attribution = Resources.Load<TextAsset>(Credits.NounProjectResource);
if (attribution == null)
{
Refuse("Credits are unavailable", $"{Credits.NounProjectResource} is missing from this build.");
return false;
}
IReadOnlyList<Credit> 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

View file

@ -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<Scenario> 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);
}