Files
linuxdistro-roguelite/Assets/Scripts/UI/TutorialSpotlight.cs
T

150 lines
8.3 KiB
C#

using System;
using System.Linq;
using KernelPanic.Combat;
using KernelPanic.Run;
using UnityEngine;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
/// <summary>Four pointer-blocking shades leave only the highlighted action exposed.</summary>
internal sealed class TutorialSpotlight : IDisposable
{
private readonly VisualElement _root, _overlay, _outline, _panel;
private readonly VisualElement[] _shades = new VisualElement[4];
private readonly Label _title, _copy, _progress;
private readonly Button _next;
private readonly IVisualElementScheduledItem _placement;
private TutorialGuide _guide;
private VisualElement _anchor;
private int _index = -1;
private bool _disposed;
public VisualElement Anchor => _anchor;
public TutorialSpotlight(VisualElement overlay, Action next)
{
_overlay = overlay; _root = overlay.parent ?? overlay;
_overlay.Clear(); _overlay.AddToClassList("guided-overlay");
_overlay.pickingMode = PickingMode.Ignore;
_root.AddToClassList("tutorial-guided");
for (int i = 0; i < _shades.Length; i++)
{ _shades[i] = new VisualElement(); _shades[i].AddToClassList("tutorial-shade"); _overlay.Add(_shades[i]); }
_outline = new VisualElement { name = "TutorialHighlight", pickingMode = PickingMode.Ignore };
_outline.AddToClassList("tutorial-highlight"); _overlay.Add(_outline);
_panel = new VisualElement { name = "TutorialCallout" };
_panel.AddToClassList("tutorial-callout");
_progress = TerminalLayout.BodyText("", "TutorialStepCount"); _panel.Add(_progress);
_title = TerminalLayout.Heading("", "TutorialStepTitle"); _panel.Add(_title);
var scroll = new ScrollView(ScrollViewMode.Vertical) { name = "TutorialStepScroll" };
scroll.style.flexShrink = 1; scroll.style.minHeight = 0;
scroll.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
_copy = TerminalLayout.BodyText("", "TutorialInstruction"); scroll.Add(_copy); _panel.Add(scroll);
_next = new Button(next) { name = "TutorialNext", text = "Next" };
_next.AddToClassList("primary-action"); _panel.Add(_next); _overlay.Add(_panel);
_root.RegisterCallback<NavigationMoveEvent>(TrapFocus, TrickleDown.TrickleDown);
_placement = _overlay.schedule.Execute(Place).Every(33);
}
public void Show(TutorialGuide guide, CombatManager combat)
{
_guide = guide; _overlay.style.display = DisplayStyle.Flex;
_root.EnableInClassList("tutorial-card-focus", guide.Step.Anchor == "card");
_anchor = ResolveAnchor();
_progress.text = $"Wave {combat.RunConfig.TutorialLesson}/{TutorialCampaign.LessonCount} / step {guide.Index + 1} of {guide.Count}";
_title.text = guide.Step.Title; _copy.text = guide.Step.Copy;
_next.style.display = guide.Reading ? DisplayStyle.Flex : DisplayStyle.None;
_next.SetEnabled(combat.TutorialInputReady);
if (_index != guide.Index)
{
_index = guide.Index;
_panel.Q<ScrollView>().scrollOffset = Vector2.zero;
_overlay.schedule.Execute(() =>
{
if (_disposed) return;
_anchor = ResolveAnchor();
for (var ancestor = _anchor?.parent; ancestor != null; ancestor = ancestor.parent)
if (ancestor is ScrollView scroll) scroll.ScrollTo(_anchor);
FocusAction(); Place();
});
}
Place();
}
private VisualElement ResolveAnchor()
{
if (_guide?.Step == null) return null;
string key = _guide.Step.Anchor;
if (key == "card") return _root.Query<Button>(className: "hand-card").ToList().FirstOrDefault(button => button.userData == _guide.Card);
if (key is "zombie" or "zombie-intent" or "enemy" || key.StartsWith("enemy:", StringComparison.Ordinal))
{
string id = key.StartsWith("enemy:", StringComparison.Ordinal) ? key.Substring(6) : null;
var enemy = _root.Query<Button>(className: "enemy-card").ToList().FirstOrDefault(button => button.userData is EnemyInstance instance
&& (id != null ? instance.ArchetypeId == id : ReferenceEquals(instance, _guide.Enemy)));
return key == "zombie-intent" ? enemy?.Q("ProcessIntent") : enemy;
}
if (key == "enemies") return _root.Q(className: "enemy-row");
return _root.Q(key);
}
private void FocusAction()
{
if (_guide?.Reading == true) _next.Focus();
else if (_anchor is Button button && button.enabledInHierarchy) button.Focus();
}
private void TrapFocus(NavigationMoveEvent evt)
{
if (_guide?.Active != true || evt.direction is not (NavigationMoveEvent.Direction.Next or NavigationMoveEvent.Direction.Previous)) return;
_root.panel?.focusController.IgnoreEvent(evt); evt.StopImmediatePropagation(); FocusAction();
}
private void Place()
{
if (_guide?.Active != true || _overlay.panel == null) return;
_anchor = ResolveAnchor();
float w = _overlay.resolvedStyle.width, h = _overlay.resolvedStyle.height;
if (float.IsNaN(w) || w < 1 || h < 1) return;
Rect hole = new(w / 2, h / 2, 0, 0);
if (_anchor != null)
{
Rect bounds = _anchor.worldBound;
for (var parent = _anchor.parent; parent != null; parent = parent.parent)
if (parent is ScrollView scroll) bounds = Intersection(bounds, scroll.contentViewport.worldBound);
Vector2 min = _overlay.WorldToLocal(bounds.min), max = _overlay.WorldToLocal(bounds.max);
hole = Rect.MinMaxRect(Mathf.Clamp(min.x - 3, 0, w), Mathf.Clamp(min.y - 3, 0, h),
Mathf.Clamp(max.x + 3, 0, w), Mathf.Clamp(max.y + 3, 0, h));
}
Position(_outline, hole);
Position(_shades[0], new Rect(0, 0, w, hole.yMin));
Position(_shades[1], new Rect(0, hole.yMax, w, h - hole.yMax));
Position(_shades[2], new Rect(0, hole.yMin, hole.xMin, hole.height));
Position(_shades[3], new Rect(hole.xMax, hole.yMin, w - hole.xMax, hole.height));
Rect[] spaces = {
new(10, 10, w - 20, Mathf.Max(0, hole.yMin - 20)),
new(10, hole.yMax + 10, w - 20, Mathf.Max(0, h - hole.yMax - 20)),
new(10, 10, Mathf.Max(0, hole.xMin - 20), h - 20),
new(hole.xMax + 10, 10, Mathf.Max(0, w - hole.xMax - 20), h - 20)
};
var space = spaces.Where(rect => rect.width >= 180 && rect.height >= 95)
.OrderByDescending(rect => Mathf.Min(rect.width, 370) * Mathf.Min(rect.height, 240)).FirstOrDefault();
if (space.width <= 0) space = new Rect(10, 10, w - 20, Mathf.Min(180, h - 20));
float width = Mathf.Min(370, space.width), height = Mathf.Min(240, space.height);
Position(_panel, new Rect(space.center.x - width / 2, space.center.y - height / 2, width, height));
}
private static Rect Intersection(Rect a, Rect b) => Rect.MinMaxRect(Mathf.Max(a.xMin, b.xMin), Mathf.Max(a.yMin, b.yMin),
Mathf.Max(Mathf.Max(a.xMin, b.xMin), Mathf.Min(a.xMax, b.xMax)), Mathf.Max(Mathf.Max(a.yMin, b.yMin), Mathf.Min(a.yMax, b.yMax)));
private static void Position(VisualElement element, Rect rect)
{ element.style.left = rect.x; element.style.top = rect.y; element.style.width = Mathf.Max(0, rect.width); element.style.height = Mathf.Max(0, rect.height); }
public void Dispose()
{
_disposed = true;
_placement.Pause(); _root.UnregisterCallback<NavigationMoveEvent>(TrapFocus, TrickleDown.TrickleDown);
_root.RemoveFromClassList("tutorial-guided"); _overlay.RemoveFromClassList("guided-overlay");
_root.RemoveFromClassList("tutorial-card-focus");
_overlay.pickingMode = PickingMode.Position; _overlay.Clear();
}
}
}