252 lines
10 KiB
C#
252 lines
10 KiB
C#
using System.Collections.Generic;
|
|
using GazeLab.Data;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace GazeLab.EditorTools
|
|
{
|
|
[CustomEditor(typeof(StationLevelDefinition))]
|
|
public sealed class StationLevelDefinitionEditor : Editor
|
|
{
|
|
private int draggingHardpointIndex = -1; // -1 = none, >= 0 = hardpoint index
|
|
|
|
public override void OnInspectorGUI()
|
|
{
|
|
serializedObject.Update();
|
|
StationLevelDefinition def = (StationLevelDefinition)target;
|
|
|
|
EditorGUILayout.Space(4f);
|
|
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
|
|
EditorGUILayout.LabelField("Station Hardpoint Positioning & Symmetry Tools", EditorStyles.boldLabel);
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("Add Hardpoint (+)", GUILayout.Height(24f)))
|
|
{
|
|
Undo.RecordObject(def, "Add Hardpoint");
|
|
AddHardpoint(def);
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
|
|
if (GUILayout.Button("Remove Last (-)", GUILayout.Height(24f)))
|
|
{
|
|
if (def.turretHardpoints != null && def.turretHardpoints.Length > 1)
|
|
{
|
|
Undo.RecordObject(def, "Remove Hardpoint");
|
|
RemoveLastHardpoint(def);
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
}
|
|
|
|
if (GUILayout.Button("Mirror / Symmetrize (X)", GUILayout.Height(24f)))
|
|
{
|
|
Undo.RecordObject(def, "Symmetrize Hardpoints");
|
|
SymmetrizeHardpoints(def);
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("Distribute Arc (Top)", GUILayout.Height(22f)))
|
|
{
|
|
Undo.RecordObject(def, "Distribute Arc Top");
|
|
DistributeArc(def, 0.45f, 0.85f);
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
if (GUILayout.Button("Distribute Perimeter Ring", GUILayout.Height(22f)))
|
|
{
|
|
Undo.RecordObject(def, "Distribute Ring");
|
|
DistributeRing(def, 0.65f);
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.EndVertical();
|
|
|
|
if (def.stationSprite != null)
|
|
{
|
|
DrawInteractiveHardpointPreview(def);
|
|
}
|
|
|
|
EditorGUILayout.Space(6f);
|
|
DrawDefaultInspector();
|
|
serializedObject.ApplyModifiedProperties();
|
|
}
|
|
|
|
private static void AddHardpoint(StationLevelDefinition def)
|
|
{
|
|
List<Vector2> list = def.turretHardpoints != null
|
|
? new List<Vector2>(def.turretHardpoints)
|
|
: new List<Vector2>();
|
|
|
|
float offset = list.Count > 0 ? (list.Count % 2 == 1 ? 0.35f : -0.35f) * Mathf.Ceil(list.Count / 2f) : 0f;
|
|
list.Add(new Vector2(offset, 0.35f));
|
|
def.turretHardpoints = list.ToArray();
|
|
}
|
|
|
|
private static void RemoveLastHardpoint(StationLevelDefinition def)
|
|
{
|
|
if (def.turretHardpoints == null || def.turretHardpoints.Length <= 1) return;
|
|
List<Vector2> list = new List<Vector2>(def.turretHardpoints);
|
|
list.RemoveAt(list.Count - 1);
|
|
def.turretHardpoints = list.ToArray();
|
|
}
|
|
|
|
private static void SymmetrizeHardpoints(StationLevelDefinition def)
|
|
{
|
|
if (def.turretHardpoints == null || def.turretHardpoints.Length < 2) return;
|
|
// Primary slot 0 stays centered
|
|
def.turretHardpoints[0].x = 0f;
|
|
|
|
// Pair subsequent slots: odd slot is positive X, next even slot is exact mirrored -X
|
|
for (int i = 1; i < def.turretHardpoints.Length; i += 2)
|
|
{
|
|
if (i + 1 < def.turretHardpoints.Length)
|
|
{
|
|
float avgX = (Mathf.Abs(def.turretHardpoints[i].x) + Mathf.Abs(def.turretHardpoints[i + 1].x)) * 0.5f;
|
|
float avgY = (def.turretHardpoints[i].y + def.turretHardpoints[i + 1].y) * 0.5f;
|
|
def.turretHardpoints[i] = new Vector2(avgX, avgY);
|
|
def.turretHardpoints[i + 1] = new Vector2(-avgX, avgY);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void DistributeArc(StationLevelDefinition def, float radius, float arcAngleDeg)
|
|
{
|
|
if (def.turretHardpoints == null || def.turretHardpoints.Length == 0) return;
|
|
int n = def.turretHardpoints.Length;
|
|
if (n == 1)
|
|
{
|
|
def.turretHardpoints[0] = new Vector2(0f, radius);
|
|
return;
|
|
}
|
|
|
|
float halfArc = arcAngleDeg * 0.5f;
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
float t = (float)i / (n - 1);
|
|
float angle = Mathf.Lerp(90f + halfArc * 0.5f, 90f - halfArc * 0.5f, t) * Mathf.Deg2Rad;
|
|
def.turretHardpoints[i] = new Vector2(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius);
|
|
}
|
|
}
|
|
|
|
private static void DistributeRing(StationLevelDefinition def, float radius)
|
|
{
|
|
if (def.turretHardpoints == null || def.turretHardpoints.Length == 0) return;
|
|
int n = def.turretHardpoints.Length;
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
float angle = (90f + (360f / n) * i) * Mathf.Deg2Rad;
|
|
def.turretHardpoints[i] = new Vector2(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius);
|
|
}
|
|
}
|
|
|
|
private void DrawInteractiveHardpointPreview(StationLevelDefinition def)
|
|
{
|
|
EditorGUILayout.Space(4f);
|
|
EditorGUILayout.LabelField("Station Sprite & Turret Hardpoints (Click & Drag Pins):", EditorStyles.boldLabel);
|
|
|
|
Rect previewBox = GUILayoutUtility.GetRect(320f, 220f, GUILayout.ExpandWidth(true));
|
|
EditorGUI.DrawRect(previewBox, new Color(0.06f, 0.09f, 0.14f, 1f));
|
|
|
|
Sprite sp = def.stationSprite;
|
|
Texture2D tex = sp.texture;
|
|
if (tex == null) return;
|
|
|
|
Rect texRect = sp.textureRect;
|
|
float aspect = texRect.width / Mathf.Max(1f, texRect.height);
|
|
|
|
float pad = 20f;
|
|
float availW = previewBox.width - pad * 2f;
|
|
float availH = previewBox.height - pad * 2f;
|
|
float drawW = availW;
|
|
float drawH = drawW / aspect;
|
|
if (drawH > availH)
|
|
{
|
|
drawH = availH;
|
|
drawW = drawH * aspect;
|
|
}
|
|
|
|
Rect spriteDrawRect = new Rect(
|
|
previewBox.x + (previewBox.width - drawW) * 0.5f,
|
|
previewBox.y + (previewBox.height - drawH) * 0.5f,
|
|
drawW,
|
|
drawH);
|
|
|
|
Rect uvRect = new Rect(
|
|
texRect.x / tex.width,
|
|
texRect.y / tex.height,
|
|
texRect.width / tex.width,
|
|
texRect.height / tex.height);
|
|
|
|
GUI.DrawTextureWithTexCoords(spriteDrawRect, tex, uvRect, true);
|
|
|
|
Bounds bounds = sp.bounds;
|
|
float ppuWidth = Mathf.Max(0.01f, bounds.size.x);
|
|
float ppuHeight = Mathf.Max(0.01f, bounds.size.y);
|
|
|
|
Vector2 centerGui = spriteDrawRect.center;
|
|
float guiPixelsPerUnitX = spriteDrawRect.width / ppuWidth;
|
|
float guiPixelsPerUnitY = spriteDrawRect.height / ppuHeight;
|
|
|
|
int count = def.turretHardpoints != null ? def.turretHardpoints.Length : 0;
|
|
Event e = Event.current;
|
|
|
|
if (e.type == EventType.MouseDown && e.button == 0 && previewBox.Contains(e.mousePosition))
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector2 pGui = new Vector2(
|
|
centerGui.x + def.turretHardpoints[i].x * guiPixelsPerUnitX,
|
|
centerGui.y - def.turretHardpoints[i].y * guiPixelsPerUnitY);
|
|
if (Vector2.Distance(e.mousePosition, pGui) <= 12f)
|
|
{
|
|
draggingHardpointIndex = i;
|
|
e.Use();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if (e.type == EventType.MouseDrag && draggingHardpointIndex >= 0 && draggingHardpointIndex < count)
|
|
{
|
|
Undo.RecordObject(def, "Drag Station Turret Hardpoint");
|
|
def.turretHardpoints[draggingHardpointIndex] = new Vector2(
|
|
(e.mousePosition.x - centerGui.x) / guiPixelsPerUnitX,
|
|
-(e.mousePosition.y - centerGui.y) / guiPixelsPerUnitY);
|
|
EditorUtility.SetDirty(def);
|
|
e.Use();
|
|
}
|
|
else if (e.type == EventType.MouseUp)
|
|
{
|
|
draggingHardpointIndex = -1;
|
|
}
|
|
|
|
// Draw Station Center Origin Mark
|
|
Handles.BeginGUI();
|
|
Handles.color = new Color(1f, 1f, 1f, 0.35f);
|
|
Handles.DrawDottedLine(centerGui + Vector2.left * 16f, centerGui + Vector2.right * 16f, 2f);
|
|
Handles.DrawDottedLine(centerGui + Vector2.up * 16f, centerGui + Vector2.down * 16f, 2f);
|
|
|
|
// Draw Hardpoint Pins
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector2 pGui = new Vector2(
|
|
centerGui.x + def.turretHardpoints[i].x * guiPixelsPerUnitX,
|
|
centerGui.y - def.turretHardpoints[i].y * guiPixelsPerUnitY);
|
|
|
|
bool isPrimary = (i == 0);
|
|
Color pinCol = isPrimary ? new Color(0.2f, 0.95f, 1f) : new Color(1f, 0.75f, 0.2f);
|
|
Handles.color = pinCol;
|
|
Handles.DrawSolidDisc(pGui, Vector3.forward, 6.5f);
|
|
Handles.color = Color.black;
|
|
Handles.DrawWireDisc(pGui, Vector3.forward, 6.5f);
|
|
|
|
string label = isPrimary ? $"Slot 1 (P)" : $"Slot {i + 1}";
|
|
GUI.color = pinCol;
|
|
GUI.Label(new Rect(pGui.x + 8f, pGui.y - 9f, 90f, 20f), label, EditorStyles.miniBoldLabel);
|
|
}
|
|
Handles.EndGUI();
|
|
GUI.color = Color.white;
|
|
}
|
|
}
|
|
}
|
|
|