202 lines
8.1 KiB
C#
202 lines
8.1 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace GazeLab.UI
|
|
{
|
|
public enum ConduitVisualState
|
|
{
|
|
Locked = 0,
|
|
Unlockable = 1,
|
|
Purchased = 2,
|
|
HighlightedPath = 3
|
|
}
|
|
|
|
public struct ConduitEdgeData
|
|
{
|
|
public Vector2 startNormalized;
|
|
public Vector2 endNormalized;
|
|
public ConduitVisualState state;
|
|
public Color trackAccentColor;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Custom uGUI MaskableGraphic that renders anti-aliased sci-fi circuit conduits,
|
|
/// outer glow halos, junction dots, and directional chevrons in a single draw call.
|
|
/// Uses normalized (0..1) rect coordinates so it is 100% immune to first-frame layout delays.
|
|
/// </summary>
|
|
[RequireComponent(typeof(CanvasRenderer))]
|
|
public sealed class TechTreeConduitGraphic : MaskableGraphic
|
|
{
|
|
private readonly List<ConduitEdgeData> edges = new List<ConduitEdgeData>();
|
|
private readonly List<float> horizontalTrackLanesY = new List<float> { 0.85f, 0.63f, 0.36f, 0.14f };
|
|
|
|
public void SetEdges(List<ConduitEdgeData> newEdges)
|
|
{
|
|
edges.Clear();
|
|
if (newEdges != null)
|
|
{
|
|
edges.AddRange(newEdges);
|
|
}
|
|
SetVerticesDirty();
|
|
}
|
|
|
|
protected override void OnPopulateMesh(VertexHelper vh)
|
|
{
|
|
vh.Clear();
|
|
Rect r = rectTransform.rect;
|
|
if (r.width <= 2f || r.height <= 2f) return;
|
|
|
|
// 1. Subtle horizontal background track guide lines
|
|
Color guideCol = new Color(0.14f, 0.24f, 0.38f, 0.22f);
|
|
for (int i = 0; i < horizontalTrackLanesY.Count; i++)
|
|
{
|
|
float y = r.yMin + horizontalTrackLanesY[i] * r.height;
|
|
Vector2 pL = new Vector2(r.xMin + r.width * 0.16f, y);
|
|
Vector2 pR = new Vector2(r.xMin + r.width * 0.96f, y);
|
|
AddLineSegment(vh, pL, pR, 1.5f, guideCol);
|
|
}
|
|
|
|
// 2. Render locked/normal edges first, then active/highlighted edges on top
|
|
for (int pass = 0; pass < 2; pass++)
|
|
{
|
|
for (int i = 0; i < edges.Count; i++)
|
|
{
|
|
ConduitEdgeData e = edges[i];
|
|
bool isForeground = e.state == ConduitVisualState.Purchased || e.state == ConduitVisualState.HighlightedPath || e.state == ConduitVisualState.Unlockable;
|
|
if ((pass == 0 && isForeground) || (pass == 1 && !isForeground))
|
|
continue;
|
|
|
|
Vector2 p1 = new Vector2(r.xMin + e.startNormalized.x * r.width, r.yMin + e.startNormalized.y * r.height);
|
|
Vector2 p2 = new Vector2(r.xMin + e.endNormalized.x * r.width, r.yMin + e.endNormalized.y * r.height);
|
|
|
|
DrawCircuitConduit(vh, p1, p2, e.state, e.trackAccentColor);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void DrawCircuitConduit(VertexHelper vh, Vector2 p1, Vector2 p2, ConduitVisualState state, Color trackAccent)
|
|
{
|
|
Color coreColor;
|
|
Color glowColor;
|
|
float thickness;
|
|
float glowThickness;
|
|
|
|
switch (state)
|
|
{
|
|
case ConduitVisualState.Purchased:
|
|
coreColor = new Color(0.38f, 0.96f, 1.0f, 0.95f);
|
|
glowColor = new Color(0.20f, 0.80f, 1.0f, 0.28f);
|
|
thickness = 3.4f;
|
|
glowThickness = 9.0f;
|
|
break;
|
|
|
|
case ConduitVisualState.HighlightedPath:
|
|
coreColor = new Color(1.0f, 0.80f, 0.24f, 0.98f);
|
|
glowColor = new Color(1.0f, 0.65f, 0.15f, 0.34f);
|
|
thickness = 3.8f;
|
|
glowThickness = 10.5f;
|
|
break;
|
|
|
|
case ConduitVisualState.Unlockable:
|
|
coreColor = Color.Lerp(trackAccent, new Color(0.55f, 0.95f, 1f, 1f), 0.45f);
|
|
coreColor.a = 0.88f;
|
|
glowColor = new Color(coreColor.r, coreColor.g, coreColor.b, 0.22f);
|
|
thickness = 2.8f;
|
|
glowThickness = 7.5f;
|
|
break;
|
|
|
|
default: // Locked
|
|
coreColor = new Color(0.22f, 0.30f, 0.44f, 0.52f);
|
|
glowColor = Color.clear;
|
|
thickness = 2.0f;
|
|
glowThickness = 0f;
|
|
break;
|
|
}
|
|
|
|
bool isStraightHorizontal = Mathf.Abs(p1.y - p2.y) < 3.5f;
|
|
if (isStraightHorizontal)
|
|
{
|
|
if (glowThickness > 0.1f)
|
|
AddLineSegment(vh, p1, p2, glowThickness, glowColor);
|
|
AddLineSegment(vh, p1, p2, thickness, coreColor);
|
|
|
|
Vector2 mid = (p1 + p2) * 0.5f;
|
|
Vector2 dir = (p2 - p1).normalized;
|
|
AddDirectionalChevron(vh, mid, dir, thickness * 2.2f, coreColor);
|
|
}
|
|
else
|
|
{
|
|
// Clean 3-segment sci-fi circuit trace with a 45% horizontal lead-out
|
|
float midX = Mathf.Lerp(p1.x, p2.x, 0.46f);
|
|
Vector2 cornerA = new Vector2(midX, p1.y);
|
|
Vector2 cornerB = new Vector2(midX, p2.y);
|
|
|
|
if (glowThickness > 0.1f)
|
|
{
|
|
AddLineSegment(vh, p1, cornerA, glowThickness, glowColor);
|
|
AddLineSegment(vh, cornerA, cornerB, glowThickness, glowColor);
|
|
AddLineSegment(vh, cornerB, p2, glowThickness, glowColor);
|
|
}
|
|
|
|
AddLineSegment(vh, p1, cornerA, thickness, coreColor);
|
|
AddLineSegment(vh, cornerA, cornerB, thickness, coreColor);
|
|
AddLineSegment(vh, cornerB, p2, thickness, coreColor);
|
|
|
|
// Junction elbows
|
|
AddDiamondDot(vh, cornerA, thickness * 1.15f, coreColor);
|
|
AddDiamondDot(vh, cornerB, thickness * 1.15f, coreColor);
|
|
|
|
// Directional chevron on the final horizontal approach into child node
|
|
Vector2 finalSegMid = Vector2.Lerp(cornerB, p2, 0.45f);
|
|
Vector2 finalDir = (p2 - cornerB).normalized;
|
|
AddDirectionalChevron(vh, finalSegMid, finalDir, thickness * 2.2f, coreColor);
|
|
}
|
|
}
|
|
|
|
private static void AddLineSegment(VertexHelper vh, Vector2 a, Vector2 b, float thickness, Color color)
|
|
{
|
|
Vector2 diff = b - a;
|
|
float len = diff.magnitude;
|
|
if (len < 0.01f) return;
|
|
|
|
Vector2 normal = new Vector2(-diff.y / len, diff.x / len) * (thickness * 0.5f);
|
|
int idx = vh.currentVertCount;
|
|
|
|
vh.AddVert(a - normal, color, Vector2.zero);
|
|
vh.AddVert(a + normal, color, Vector2.zero);
|
|
vh.AddVert(b + normal, color, Vector2.zero);
|
|
vh.AddVert(b - normal, color, Vector2.zero);
|
|
|
|
vh.AddTriangle(idx, idx + 1, idx + 2);
|
|
vh.AddTriangle(idx, idx + 2, idx + 3);
|
|
}
|
|
|
|
private static void AddDiamondDot(VertexHelper vh, Vector2 center, float radius, Color color)
|
|
{
|
|
int idx = vh.currentVertCount;
|
|
vh.AddVert(center + new Vector2(0f, radius), color, Vector2.zero);
|
|
vh.AddVert(center + new Vector2(radius, 0f), color, Vector2.zero);
|
|
vh.AddVert(center + new Vector2(0f, -radius), color, Vector2.zero);
|
|
vh.AddVert(center + new Vector2(-radius, 0f), color, Vector2.zero);
|
|
|
|
vh.AddTriangle(idx, idx + 1, idx + 2);
|
|
vh.AddTriangle(idx, idx + 2, idx + 3);
|
|
}
|
|
|
|
private static void AddDirectionalChevron(VertexHelper vh, Vector2 tipPos, Vector2 dir, float size, Color color)
|
|
{
|
|
if (dir.sqrMagnitude < 0.001f) dir = Vector2.right;
|
|
Vector2 perp = new Vector2(-dir.y, dir.x);
|
|
|
|
Vector2 tip = tipPos + dir * (size * 0.55f);
|
|
Vector2 wingTop = tipPos - dir * (size * 0.55f) + perp * (size * 0.65f);
|
|
Vector2 wingBot = tipPos - dir * (size * 0.55f) - perp * (size * 0.65f);
|
|
|
|
AddLineSegment(vh, wingTop, tip, size * 0.34f, color);
|
|
AddLineSegment(vh, wingBot, tip, size * 0.34f, color);
|
|
}
|
|
}
|
|
}
|
|
|