Files
beyond/Assets/Scripts/Editor/AnimSequencerInspector.cs
2024-11-20 15:21:28 +01:00

217 lines
7.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace Beyond
{
[CustomEditor(typeof(AnimSequencer))]
public class AnimSequencerInspector : Editor
{
AnimSequencer helper;
private Quaternion handleRotation;
private const float handleSize = 0.04f;
private const float pickSize = 0.06f;
const int maxSeg = 100;
const int minSeg = 10;
Vector3[] mSegmentPoints = new Vector3[60];
private void DrawSegments(int idx, int numSegments)
{
int i = 0;
mSegmentPoints[i++] = helper.GetWorldPos(idx);
float step = 1.0f / (float)numSegments;
for (float j = step; j < 1f; j += step)
{
mSegmentPoints[i++] = helper.GetInterpolated(idx, j);
}
//Handles.DrawLine(v1, helper[idx + 1]);
mSegmentPoints[i++] = helper.GetWorldPos(idx + 1);
Handles.DrawAAPolyLine(2.0f, i, mSegmentPoints);
}
private void OnSceneGUI()
{
helper = (AnimSequencer)target;
handleRotation = Tools.pivotRotation == PivotRotation.Local ?
helper.transform.rotation : Quaternion.identity;
for (int i = 0; i < helper.Count - 1; i++)
{
float screenSize = (Camera.current.WorldToViewportPoint(helper.GetWorldPos(i)).WithZ(0f) - Camera.current.WorldToViewportPoint(helper.GetWorldPos(i + 1)).WithZ(0f)).magnitude;
int numSegs = (int)(screenSize * 40f);
if (numSegs < 1) numSegs = 1;
if (numSegs > 50) numSegs = 50;
DrawSegments(i, numSegs);
}
for (int i = 0; i < helper.mSeqPoints.Length; i++)
{
ShowPoint(i);
}
}
private Vector3 ShowPoint(int id)
{
AnimSequencer.SeqPoint p = helper.mSeqPoints[id];
Transform trans = helper.transform;
// Vector3 point = trans.TransformPoint(p.pos);
Vector3 point = p.pos;
Quaternion rot = p.rot;
//rot = helper.transform.rotation;
float size = HandleUtility.GetHandleSize(point) * 2f;
if (helper.mSeqPoints[id].type == AnimSequencer.SeqPoint.PointType.STOP)
{
Handles.color = Color.red;
}
else
{
//Handles.color = Color.white;
Handles.color = Color.gray;
}
if (Handles.Button(point, handleRotation, size * handleSize, size * pickSize, Handles.DotHandleCap))
{
helper.selectedIndex = id;
Repaint();
}
GUIStyle style = new GUIStyle();
style.fontSize = 20;
Font f = style.font;
Handles.Label(point, "" + id, style);
//Handles.BeginGUI(new Rect(Screen.width - 100, Screen.height - 80, 90, 50));
if (id == helper.selectedIndex)
{
EditorGUI.BeginChangeCheck();
//EditorGUI.
if (Tools.current == Tool.Move)
{
point = Handles.DoPositionHandle(point, handleRotation);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(helper, "Move Point");
EditorUtility.SetDirty(helper);
// p.pos = trans.InverseTransformPoint(point);
p.pos = point;
helper.UpdatePointsList();
}
}
else if (Tools.current == Tool.Rotate)
{
//rot = Handles.DoRotationHandle(rot, Vector3.zero);
rot = Handles.DoRotationHandle(rot, point);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(helper, "Rotate Point");
EditorUtility.SetDirty(helper);
p.rot = rot;
helper.UpdatePointsList();
//helper.transform.rotation = rot;
}
}
}
return point;
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
helper = target as AnimSequencer;
int numPoints = helper.mSeqPoints.Length;
GUILayout.Label("Operacje na punktach:");
GUILayout.BeginVertical();
for (int i = 0; i < numPoints; i++)
{
if (helper.selectedIndex == i)
GUILayout.BeginHorizontal(EditorStyles.helpBox);
else
GUILayout.BeginHorizontal();
//GUILayout.Label("" + i + ", Type: " + helper.mSeqPoints[i].type.ToString() + ", Trigger: " + helper.mAnimTriggers[helper.mSeqPoints[i].animTriggerIdx]);
if (GUILayout.Button("" + i))
{
helper.selectedIndex = i;
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
if (helper.selectedIndex > -1 && helper.selectedIndex < helper.mSeqPoints.Length)
{
GUILayout.BeginHorizontal();
if (helper.selectedIndex > 0 && GUILayout.Button("UP"))
{
helper.MoveSelectedUp();
}
if (helper.selectedIndex < numPoints - 1 && GUILayout.Button("DOWN"))
{
helper.MoveSelectedDown();
}
GUILayout.EndHorizontal();
AnimSequencer.SeqPoint selPoint = helper.mSeqPoints[helper.selectedIndex];
GUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.BeginHorizontal();
GUILayout.Label("Type: ");
int typeNum = (int)helper.mSeqPoints[helper.selectedIndex].type;
typeNum = EditorGUILayout.Popup(typeNum, Enum.GetNames(typeof(AnimSequencer.SeqPoint.PointType)));
helper.SetPointType(helper.selectedIndex, (AnimSequencer.SeqPoint.PointType)Enum.ToObject(typeof(AnimSequencer.SeqPoint.PointType), typeNum));
if (typeNum == (int)AnimSequencer.SeqPoint.PointType.STOP)
{
SerializedProperty sprop = serializedObject.FindProperty("mSeqPoints");
sprop = sprop.GetArrayElementAtIndex(helper.selectedIndex);
sprop = sprop.FindPropertyRelative("onReached");
// EditorGUIUtility.LookLikeControls();
EditorGUILayout.PropertyField(sprop);
serializedObject.ApplyModifiedProperties();
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
for (int i = 0; i < numPoints; i++)
{
}
GUILayout.BeginHorizontal();
if (GUILayout.Button("Add Point"))
{
Undo.RecordObject(helper, "Add Point");
helper.AddSeqPoint();
EditorUtility.SetDirty(helper);
}
if (helper.selectedIndex > -1)
{
if (GUILayout.Button("Remove Point"))
{
Undo.RecordObject(helper, "Remove Point");
helper.RemoveSelected();
EditorUtility.SetDirty(helper);
}
}
GUILayout.EndHorizontal();
}
}
}