222 lines
8.9 KiB
C#
222 lines
8.9 KiB
C#
using GazeLab.Combat;
|
|
using GazeLab.Data;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace GazeLab.EditorTools
|
|
{
|
|
[CustomEditor(typeof(WeaponDefinition))]
|
|
public sealed class WeaponDefinitionEditor : Editor
|
|
{
|
|
private int draggingHandleIndex = -2; // -2 = none, -1 = mountPivot, >= 0 = muzzle index
|
|
|
|
public override void OnInspectorGUI()
|
|
{
|
|
serializedObject.Update();
|
|
WeaponDefinition def = (WeaponDefinition)target;
|
|
|
|
EditorGUILayout.Space(4f);
|
|
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
|
|
EditorGUILayout.LabelField("Quick Alignment & Multi-Barrel Helpers", EditorStyles.boldLabel);
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("Auto-Distribute Barrels Vertically", GUILayout.Height(26f)))
|
|
{
|
|
Undo.RecordObject(def, "Auto-Distribute Barrels");
|
|
def.AutoDistributeMuzzleOffsets();
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
|
|
if (GUILayout.Button("Auto-Estimate Pivot & Length from Sprite", GUILayout.Height(26f)))
|
|
{
|
|
Undo.RecordObject(def, "Auto-Estimate Weapon Geometry");
|
|
AutoEstimateFromSprite(def);
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.EndVertical();
|
|
|
|
if (def.weaponSprite != null)
|
|
{
|
|
DrawInteractiveSpriteAlignmentPreview(def);
|
|
}
|
|
|
|
EditorGUILayout.Space(6f);
|
|
DrawDefaultInspector();
|
|
serializedObject.ApplyModifiedProperties();
|
|
}
|
|
|
|
private static void AutoEstimateFromSprite(WeaponDefinition def)
|
|
{
|
|
if (def.weaponSprite == null) return;
|
|
Bounds b = def.weaponSprite.bounds;
|
|
float w = b.size.x;
|
|
float h = b.size.y;
|
|
// Circular turret mount is typically ~28% from the left edge of the weapon sprite
|
|
def.mountPivotOffset = new Vector2(-w * 0.22f, 0f);
|
|
def.autoBarrelForwardLength = w * 0.70f;
|
|
def.autoBarrelTotalSpreadY = Mathf.Clamp(h * 0.36f, 0.2f, 2.5f);
|
|
def.AutoDistributeMuzzleOffsets();
|
|
}
|
|
|
|
private void DrawInteractiveSpriteAlignmentPreview(WeaponDefinition def)
|
|
{
|
|
EditorGUILayout.Space(4f);
|
|
EditorGUILayout.LabelField("Interactive Pivot (Cyan) & Barrel Muzzle (Red) Editor — Drag Points:", EditorStyles.boldLabel);
|
|
|
|
Rect previewBox = GUILayoutUtility.GetRect(320f, 190f, GUILayout.ExpandWidth(true));
|
|
EditorGUI.DrawRect(previewBox, new Color(0.08f, 0.11f, 0.16f, 1f));
|
|
|
|
Sprite sp = def.weaponSprite;
|
|
Texture2D tex = sp.texture;
|
|
if (tex == null) return;
|
|
|
|
Rect texRect = sp.textureRect;
|
|
float aspect = texRect.width / Mathf.Max(1f, texRect.height);
|
|
|
|
float pad = 18f;
|
|
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 spriteCenterGui = spriteDrawRect.center;
|
|
float guiPixelsPerUnitX = spriteDrawRect.width / ppuWidth;
|
|
float guiPixelsPerUnitY = spriteDrawRect.height / ppuHeight;
|
|
|
|
// Convert mountPivotOffset (relative to sprite center) to GUI position
|
|
Vector2 pivotGui = new Vector2(
|
|
spriteCenterGui.x + def.mountPivotOffset.x * guiPixelsPerUnitX,
|
|
spriteCenterGui.y - def.mountPivotOffset.y * guiPixelsPerUnitY);
|
|
|
|
// Handle mouse dragging inside the preview box
|
|
Event e = Event.current;
|
|
int count = def.muzzleOffsets != null ? Mathf.Min(def.barrelCount, def.muzzleOffsets.Length) : 0;
|
|
|
|
if (e.type == EventType.MouseDown && e.button == 0 && previewBox.Contains(e.mousePosition))
|
|
{
|
|
if (Vector2.Distance(e.mousePosition, pivotGui) <= 11f)
|
|
{
|
|
draggingHandleIndex = -1;
|
|
e.Use();
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector2 mGui = new Vector2(
|
|
pivotGui.x + def.muzzleOffsets[i].x * guiPixelsPerUnitX,
|
|
pivotGui.y - def.muzzleOffsets[i].y * guiPixelsPerUnitY);
|
|
if (Vector2.Distance(e.mousePosition, mGui) <= 10f)
|
|
{
|
|
draggingHandleIndex = i;
|
|
e.Use();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (e.type == EventType.MouseDrag && draggingHandleIndex >= -1)
|
|
{
|
|
Undo.RecordObject(def, "Drag Weapon Alignment Point");
|
|
if (draggingHandleIndex == -1)
|
|
{
|
|
def.mountPivotOffset = new Vector2(
|
|
(e.mousePosition.x - spriteCenterGui.x) / guiPixelsPerUnitX,
|
|
-(e.mousePosition.y - spriteCenterGui.y) / guiPixelsPerUnitY);
|
|
}
|
|
else if (def.muzzleOffsets != null && draggingHandleIndex < def.muzzleOffsets.Length)
|
|
{
|
|
def.muzzleOffsets[draggingHandleIndex] = new Vector2(
|
|
(e.mousePosition.x - pivotGui.x) / guiPixelsPerUnitX,
|
|
-(e.mousePosition.y - pivotGui.y) / guiPixelsPerUnitY);
|
|
}
|
|
EditorUtility.SetDirty(def);
|
|
e.Use();
|
|
}
|
|
else if (e.type == EventType.MouseUp)
|
|
{
|
|
draggingHandleIndex = -2;
|
|
}
|
|
|
|
// Draw Mount Pivot Crosshair (Cyan)
|
|
Handles.BeginGUI();
|
|
Handles.color = Color.cyan;
|
|
Handles.DrawWireDisc(pivotGui, Vector3.forward, 8f);
|
|
Handles.DrawLine(pivotGui + Vector2.left * 11f, pivotGui + Vector2.right * 11f);
|
|
Handles.DrawLine(pivotGui + Vector2.up * 11f, pivotGui + Vector2.down * 11f);
|
|
|
|
// Draw Muzzle Points (Red) & Barrel Vectors (Yellow)
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector2 mGui = new Vector2(
|
|
pivotGui.x + def.muzzleOffsets[i].x * guiPixelsPerUnitX,
|
|
pivotGui.y - def.muzzleOffsets[i].y * guiPixelsPerUnitY);
|
|
|
|
Handles.color = new Color(1f, 0.85f, 0.2f, 0.55f);
|
|
Handles.DrawDottedLine(pivotGui, mGui, 2f);
|
|
|
|
Handles.color = new Color(1f, 0.25f, 0.25f, 1f);
|
|
Handles.DrawSolidDisc(mGui, Vector3.forward, 5.5f);
|
|
|
|
Handles.color = Color.yellow;
|
|
Handles.DrawLine(mGui, mGui + Vector2.right * 18f);
|
|
}
|
|
Handles.EndGUI();
|
|
}
|
|
}
|
|
|
|
[CustomEditor(typeof(TurretController))]
|
|
public sealed class TurretControllerSceneEditor : Editor
|
|
{
|
|
private void OnSceneGUI()
|
|
{
|
|
TurretController turret = (TurretController)target;
|
|
if (turret == null || turret.weaponDefinition == null) return;
|
|
|
|
WeaponDefinition def = turret.weaponDefinition;
|
|
if (def.muzzleOffsets == null) return;
|
|
|
|
int count = Mathf.Min(def.barrelCount, def.muzzleOffsets.Length);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector3 worldMuzzle = turret.GetBarrelWorldPosition(i);
|
|
float size = HandleUtility.GetHandleSize(worldMuzzle) * 0.08f;
|
|
Handles.color = Color.red;
|
|
EditorGUI.BeginChangeCheck();
|
|
Vector3 newWorld = Handles.FreeMoveHandle(worldMuzzle, Quaternion.identity, size, Vector3.zero, Handles.SphereHandleCap);
|
|
if (EditorGUI.EndChangeCheck())
|
|
{
|
|
Undo.RecordObject(def, "Move Turret Barrel Muzzle");
|
|
Vector3 local = turret.transform.InverseTransformPoint(newWorld);
|
|
def.muzzleOffsets[i] = new Vector2(local.x, local.y);
|
|
EditorUtility.SetDirty(def);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|