using System.Collections.Generic;
using DevilLink2.Core;
using UnityEngine;
using UnityEngine.UI;
namespace DevilLink2.Presentation
{
/// Non-interactive overlay outside the board viewport. Stores only algorithm-returned paths.
[RequireComponent(typeof(CanvasRenderer))]
public sealed class BoardPathRenderer : MaskableGraphic
{
private sealed class Stroke
{
public GridPosition[] Points; public RectTransform Board; public BoardLayout Layout; public float Age;
}
private readonly List strokes = new List();
public int ActivePathCount => strokes.Count;
public const float DrawSeconds = .180f, FadeStartsAt = .220f, FadeSeconds = .260f, LineWidth = 3.5f;
protected override void Awake() { base.Awake(); raycastTarget = false; maskable = false; }
public void Show(IReadOnlyList path, RectTransform board, BoardLayout layout)
{
if (path == null || path.Count < 2 || !board) return;
var copy = new GridPosition[path.Count]; for (int i = 0; i < copy.Length; i++) copy[i] = path[i];
strokes.Add(new Stroke { Points = copy, Board = board, Layout = layout }); SetVerticesDirty();
}
public void Clear() { strokes.Clear(); SetVerticesDirty(); }
private void Update() { AdvanceVisuals(Time.unscaledDeltaTime); }
protected override void OnDisable() { Clear(); base.OnDisable(); }
public void AdvanceVisuals(float seconds)
{
if (float.IsNaN(seconds) || float.IsInfinity(seconds) || seconds < 0) return;
if (strokes.Count == 0) return;
for (int i = strokes.Count - 1; i >= 0; i--)
{ strokes[i].Age += seconds; if (!strokes[i].Board || strokes[i].Age >= FadeStartsAt + FadeSeconds) strokes.RemoveAt(i); }
SetVerticesDirty();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
foreach (var stroke in strokes)
{
if (!stroke.Board) continue;
var points = new Vector2[stroke.Points.Length]; float total = 0;
for (int i = 0; i < points.Length; i++)
{
var p = stroke.Points[i]; var offset = stroke.Layout.TilePosition(p.Row, p.Column);
var local = new Vector3(-stroke.Board.rect.width / 2 + offset.x + stroke.Layout.TileSize / 2,
stroke.Board.rect.height / 2 + offset.y - stroke.Layout.TileSize / 2, 0);
points[i] = rectTransform.InverseTransformPoint(stroke.Board.TransformPoint(local));
}
// End at the cell edges, leaving the accepted tiles' artwork unobscured.
float inset = stroke.Layout.TileSize * .5f * stroke.Board.lossyScale.x / Mathf.Max(.0001f, rectTransform.lossyScale.x);
points[0] = Vector2.MoveTowards(points[0], points[1], inset);
int last = points.Length - 1;
points[last] = Vector2.MoveTowards(points[last], points[last - 1], inset);
for (int i = 1; i < points.Length; i++) total += Vector2.Distance(points[i - 1], points[i]);
float remaining = total * Mathf.Clamp01(stroke.Age / DrawSeconds);
var tint = new Color(239f / 255, 214f / 255, 157f / 255,
1 - Mathf.Clamp01((stroke.Age - FadeStartsAt) / FadeSeconds));
for (int i = 1; i < points.Length && remaining > 0; i++)
{
float length = Vector2.Distance(points[i - 1], points[i]);
if (length <= 0) continue;
var end = Vector2.Lerp(points[i - 1], points[i], Mathf.Min(1, remaining / length));
AddSegment(vh, points[i - 1], end, tint); remaining -= length;
if (i < points.Length - 1 && remaining > 0)
{
// Fill the tiny corner seam without changing the algorithm's path points.
float half = LineWidth * .5f;
AddSegment(vh, points[i] - Vector2.right * half, points[i] + Vector2.right * half, tint);
}
}
}
}
private static void AddSegment(VertexHelper vh, Vector2 a, Vector2 b, Color tint)
{
var normal = new Vector2(-(b - a).y, (b - a).x).normalized * (LineWidth / 2);
int first = vh.currentVertCount;
vh.AddVert(a - normal, tint, Vector2.zero); vh.AddVert(a + normal, tint, Vector2.zero);
vh.AddVert(b + normal, tint, Vector2.zero); vh.AddVert(b - normal, tint, Vector2.zero);
vh.AddTriangle(first, first + 1, first + 2); vh.AddTriangle(first, first + 2, first + 3);
}
}
}