Scenario picker attempt 2: body heading instead of a second giant title, small left group labels, chrome Launch in the accent, tight Seed/Ambient block, capped brief, visible scrollbar track

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-10-22 21:07:15 +02:00
commit 585942259d
4 changed files with 122 additions and 19 deletions

View file

@ -56,7 +56,7 @@ public static class ScenarioPickerCapture
var (menu, list, detail) = Build();
menu.Dialog.gameObject.SetActive(false);
list.Title.text = MainMenu.TitleSubtitle("new game", "scenarios");
ScenarioPicker.Show(list, detail, AllGroups(), _ => { }, () => { });
ScenarioPicker.Show(list, detail, AllGroups(), _ => { }, () => { }, menu.Dialog.Confirm);
list.RefreshValues();
detail.RefreshValues();
@ -80,6 +80,7 @@ public static class ScenarioPickerCapture
foreach (var text in list.transform.GetComponentsInChildren<TMPro.TMP_Text>())
text.ForceMeshUpdate();
Canvas.ForceUpdateCanvases();
list.Content.GetComponentInParent<ScrollRect>().onValueChanged.Invoke(Vector2.zero);
camera.Render();
var previous = RenderTexture.active;

View file

@ -68,7 +68,7 @@ public static class ScenarioPickerVerify
private static Screen Build(bool oneGroup = false)
{
var (_, list, detail) = ScenarioPickerCapture.Build();
var (menu, list, detail) = ScenarioPickerCapture.Build();
// Build opens the scene afresh, which destroys the previous EventSystem.
var events = new GameObject("verify EventSystem").AddComponent<EventSystem>();
// Edit mode never calls OnEnable, which is what registers an EventSystem as current.
@ -83,11 +83,11 @@ public static class ScenarioPickerVerify
var groups = oneGroup
? new List<(string label, Scenario[] scenarios)> { ("modes", screen.A) }
: new List<(string label, Scenario[] scenarios)> { ("modes", screen.A), ("development", screen.B), ("test arenas", screen.C) };
screen.Picker = ScenarioPicker.Show(list, detail, groups, screen.Launched.Add, () => screen.Backs++);
screen.Picker = ScenarioPicker.Show(list, detail, groups, screen.Launched.Add, () => screen.Backs++, menu.Dialog.Confirm);
var buttons = list.GetComponentsInChildren<PropertyButton>(false).Where(button => !button.transform.IsChildOf(detail.transform)).Select(button => button.Button).ToArray();
screen.Rows = buttons.Take(buttons.Length - 1).ToArray();
screen.Back = buttons.Last();
screen.Launch = detail.GetComponentsInChildren<PropertyButton>(false).Single().Button;
screen.Launch = detail.GetComponentsInChildren<Button>(false).Single();
return screen;
}
@ -174,20 +174,20 @@ public static class ScenarioPickerVerify
var screen = Build();
var first = DetailText(screen);
Require(first.Contains("brief of Alpha") && first.Contains("7") && first.Contains("on"), $"Detail does not describe Alpha: {first}");
Require(screen.Detail.Title.text == MainMenu.TitleSubtitle("modes", "Alpha"), "Detail title is not the name and group.");
Require(first.Contains("modes") && first.Contains("Alpha"), "Detail heading is not the group and name.");
Move(screen, MoveDirection.Down);
Move(screen, MoveDirection.Down);
var third = DetailText(screen);
Require(third.Contains("brief of Gamma") && third.Contains("new each launch") && third.Contains("off"), $"Detail does not describe Gamma: {third}");
Require(screen.Detail.Title.text == MainMenu.TitleSubtitle("development", "Gamma"), "Detail title did not follow the selection.");
Require(screen.Detail.GetComponentsInChildren<PropertyButton>(false).Single().Button == screen.Launch, "Selecting rebuilt the Launch button.");
Require(third.Contains("development") && !third.Contains("Alpha"), "Detail heading did not follow the selection.");
Require(screen.Detail.GetComponentsInChildren<Button>(false).Single() == screen.Launch, "Selecting rebuilt the Launch button.");
}
private static void OneGroupHasNoSectionLabel()
{
var many = Build();
var one = Build(oneGroup: true);
Require(many.List.Children == 5 + 3 + 1, $"Three groups should list 5 rows, 3 section labels and Back, not {many.List.Children} children.");
Require(many.List.Children == 5 + 3 + 2 + 1, $"Three groups should list 5 rows, 3 section labels, 2 spacers and Back, not {many.List.Children} children.");
Require(one.List.Children == 2 + 1, $"One group should list its 2 rows and Back, not {one.List.Children} children.");
}
}

View file

@ -154,7 +154,7 @@ public class MainMenu : MonoBehaviour
{
ShowMain();
Fade(false);
});
}, Dialog.Confirm);
}
// The only way into a new run: generates the scenario's galaxy off the main thread and enters the game, where

View file

@ -2,7 +2,9 @@ using System;
using System.Collections.Generic;
using UniRx;
using UniRx.Triggers;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
@ -22,6 +24,8 @@ public sealed class ScenarioPicker
private readonly Action<Scenario> _launch;
private readonly Action _back;
private Button _launchButton;
private ScrollRect _scroll;
private UnityAction<Vector2> _fit;
private static readonly Color Accent = new Color(1, .4458f, .05f);
private int _selected;
@ -50,14 +54,27 @@ public sealed class ScenarioPicker
detail.gameObject.SetActive(false);
}
// Builds both panes for the groups, shows the detail pane, selects the first scenario and focuses Launch.
private static readonly Color Muted = new Color(1, 1, 1, .55f);
// Builds both panes for the groups, shows the detail pane, selects the first scenario and focuses Launch. The
// launchChrome is a button of the menu's own styling, cloned for Launch.
public static ScenarioPicker Show(PropertiesPanel list, PropertiesPanel detail,
IReadOnlyList<(string label, Scenario[] scenarios)> groups, Action<Scenario> launch, Action back)
IReadOnlyList<(string label, Scenario[] scenarios)> groups, Action<Scenario> launch, Action back, Button launchChrome)
{
var picker = new ScenarioPicker(list, detail, launch, back);
foreach (var group in groups)
{
if (groups.Count > 1) list.AddSection(group.label);
if (groups.Count > 1)
{
if (picker._scenarios.Count > 0) list.AddSpacer();
var header = list.AddSection(group.label);
header.GetComponent<LayoutElement>().preferredHeight = 26;
var text = header.GetComponentInChildren<TMP_Text>();
text.fontSize = 14;
text.fontStyle = FontStyles.SmallCaps;
text.alignment = TextAlignmentOptions.Left;
text.color = Muted;
}
foreach (var scenario in group.scenarios)
{
var index = picker._scenarios.Count;
@ -74,10 +91,18 @@ public sealed class ScenarioPicker
detail.Clear();
detail.gameObject.SetActive(true);
detail.AddProperty(() => picker.Selected.Brief);
detail.AddProperty("Seed", () => picker.Selected.Seed == 0 ? "new each launch" : picker.Selected.Seed.ToString());
detail.AddProperty("Ambient", () => picker.Selected.Ambient ? "on" : "off");
picker._launchButton = detail.AddButton("Launch", picker.Activate).Button;
// The pane's own title is an invisible copy of the page title, so both panes stack their content from the same
// line; the scenario is headed by its group label and name in the body.
detail.Title.text = list.Title.text;
detail.Title.color = Color.clear;
Style(detail.AddProperty(() => $"<smallcaps>{picker._groupLabels[picker._selected]}"), 14, Muted);
Style(detail.AddProperty(() => picker.Selected.Name), 30, Color.white);
detail.AddProperty(() => $"<margin-right=20%>{picker.Selected.Brief}");
detail.AddSpacer();
detail.AddProperty(() => $"<color=#ffffff8c>Seed</color><pos=22%>{(picker.Selected.Seed == 0 ? "new each launch" : picker.Selected.Seed.ToString())}");
detail.AddProperty(() => $"<color=#ffffff8c>Ambient</color><pos=22%>{(picker.Selected.Ambient ? "on" : "off")}");
detail.AddSpacer();
picker._launchButton = AddLaunch(detail, launchChrome, picker.Activate);
picker.Track(picker._launchButton);
picker._launchButton.OnMoveAsObservable().Subscribe(move =>
{
@ -86,17 +111,95 @@ public sealed class ScenarioPicker
});
picker._launchButton.OnCancelAsObservable().Subscribe(_ => picker.Back());
picker.StyleScrollbars();
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());
return picker;
}
private static void Style(Property property, float size, Color color)
{
property.Label.enableAutoSizing = false;
property.Label.fontSize = size;
property.Label.color = color;
}
// Launch is a clone of the menu's own chrome button, filled with the accent so it reads as the default action. The
// clone lives under a spacer row, so the pane's Clear destroys it.
private static Button AddLaunch(PropertiesPanel detail, Button chrome, Action onClick)
{
var holder = detail.AddSpacer();
holder.GetComponent<LayoutElement>().preferredHeight = 40;
var button = UnityEngine.Object.Instantiate(chrome, holder, false);
var rect = (RectTransform) button.transform;
rect.anchorMin = Vector2.zero;
rect.anchorMax = new Vector2(0, 1);
rect.pivot = new Vector2(0, .5f);
rect.anchoredPosition = Vector2.zero;
rect.sizeDelta = new Vector2(220, 0);
button.gameObject.SetActive(true);
button.onClick.RemoveAllListeners();
button.onClick.AddListener(() => onClick());
button.targetGraphic.color = Accent;
button.GetComponentInChildren<TMP_Text>().text = "Launch";
return button;
}
// The detail pane needs no scrollbar; the list's gets a visible track, so its handle reads as a scrollbar rather
// than a bar that stops partway.
private void StyleScrollbars()
{
var detailScroll = ScrollOf(_detail);
if (detailScroll != null && detailScroll.verticalScrollbar != null) detailScroll.verticalScrollbar.gameObject.SetActive(false);
_scroll = ScrollOf(_list);
if (_scroll == null) return;
if (_scroll.verticalScrollbar != null)
{
if (!_scroll.verticalScrollbar.TryGetComponent<Image>(out var track)) track = _scroll.verticalScrollbar.gameObject.AddComponent<Image>();
track.color = new Color(Accent.r, Accent.g, Accent.b, .2f);
}
_scroll.onValueChanged.RemoveAllListeners();
_fit = _ => FitViewport();
_scroll.onValueChanged.AddListener(_fit);
}
private static ScrollRect ScrollOf(PropertiesPanel panel) => (panel.Content ?? panel.transform).GetComponentInParent<ScrollRect>();
// Hides every list entry the viewport only partly shows, so the list ends on a whole row instead of slicing text.
private void FitViewport()
{
if (_scroll == null || _scroll.content == null) return;
if (_rows[0] == null)
{
_scroll.onValueChanged.RemoveListener(_fit);
return;
}
var view = ViewOf();
var viewBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(_scroll.content, view);
// The mask can sit tighter than the viewport rect, so the scroll rect also bounds the visible span.
var outer = RectTransformUtility.CalculateRelativeRectTransformBounds(_scroll.content, (RectTransform) _scroll.transform);
var low = Mathf.Max(viewBounds.min.y, outer.min.y);
var high = Mathf.Min(viewBounds.max.y, outer.max.y);
foreach (RectTransform entry in _list.Content ?? _list.transform)
{
var bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(_scroll.content, entry);
var whole = bounds.min.y >= low - .5f && bounds.max.y <= high + .5f;
if (!entry.TryGetComponent<CanvasGroup>(out var group)) group = entry.gameObject.AddComponent<CanvasGroup>();
group.alpha = whole ? 1 : 0;
group.blocksRaycasts = whole;
}
}
private RectTransform ViewOf() => _scroll.viewport != null ? _scroll.viewport : (RectTransform) _scroll.transform;
// Takes a button out of Unity's navigation so a click never moves focus and Move never walks automatic navigation.
private void Track(Button button) => button.navigation = new Navigation { mode = Navigation.Mode.None };
// Moves the selection one row, stopping at either end.
private void Step(int delta) => Select(_selected + delta);
// Makes one row the selection: clamps the index, redraws the highlight and title, and returns focus to Launch.
// Makes one row the selection: clamps the index, redraws the highlight, and returns focus to Launch.
private void Select(int index)
{
_selected = Mathf.Clamp(index, 0, _scenarios.Count - 1);
@ -108,7 +211,6 @@ public sealed class ScenarioPicker
_rows[i].colors = colors;
_rows[i].targetGraphic.color = i == _selected ? Accent : _rowFills[i];
}
_detail.Title.text = MainMenu.TitleSubtitle(_groupLabels[_selected], Selected.Name);
Focus(_launchButton.gameObject);
Reveal((RectTransform) _rows[_selected].transform);
}
@ -116,7 +218,7 @@ public sealed class ScenarioPicker
// Scrolls the list pane just far enough that the selected row is inside its viewport.
private void Reveal(RectTransform row)
{
var scroll = (_list.Content ?? _list.transform).GetComponentInParent<ScrollRect>();
var scroll = _scroll;
if (scroll == null || scroll.content == null) return;
var view = scroll.viewport != null ? scroll.viewport : (RectTransform) scroll.transform;
var rowBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(scroll.content, row);