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:
parent
09de7e5eb2
commit
585942259d
4 changed files with 122 additions and 19 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue