-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keyframes.cs
240 lines (198 loc) · 8.27 KB
/
Keyframes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace AV.Animations
{
[Serializable] public struct Keyframes<T> : ISerializationCallbackReceiver
{
[Serializable] public struct Frame
{
public float time;
public T value;
}
public Frame[] frames;
public int Count => frames.Length;
public ref Frame this[int i] => ref frames[i];
public void OnBeforeSerialize()
{
#if UNITY_EDITOR
// Do not sort frames by time while dragging!
if (KeyframesDrawer.Drag) return;
frames = frames?.OrderBy(x => x.time).ToArray();
#endif
}
public void OnAfterDeserialize() { }
public void Evaluate(float lerp, out T v1, out T v2, out float t)
{
v1 = v2 = default;
float t1 = 0, t2 = 0;
var count = frames.Length;
for (int i = 0; i < count; i++)
{
ref var f = ref frames[i];
if (lerp >= f.time) { t1 = f.time; v1 = f.value; }
}
for (int i = 0; i < count; i++)
{
ref var f = ref frames[i];
if (lerp <= f.time) { t2 = f.time; v2 = f.value; break; }
}
if (count > 0)
{
ref var f = ref frames[count - 1];
if (lerp >= f.time) { t2 = f.time; v2 = f.value; }
}
t = (lerp - t1) / (t2 - t1);
if (float.IsNaN(t))
t = 0;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(Keyframes<>))]
class KeyframesDrawer : PropertyDrawer
{
class Data
{
public int selected = -1;
public float itemHeight;
public float dragTime = -1;
public bool drag;
public bool mouseUp;
public SerializedProperty currFrame;
}
static Texture keyframeIcon = EditorGUIUtility.IconContent("blendKeySelected").image;
static Dictionary<string, Data> datas = new Dictionary<string, Data>();
internal static bool Drag;
internal static string FocusedProp;
public override void OnGUI(Rect r, SerializedProperty prop, GUIContent label)
{
var evt = Event.current;
var path = prop.propertyPath;
var mousePos = evt.mousePosition;
EditorGUI.BeginProperty(r, label, prop);
EditorGUI.LabelField(new Rect(r) { height = 18 }, label);
r.y += 20;
r.xMin += 3; r.xMax -= 5;
EditorGUI.DrawRect(new Rect(r) { height = 4 }, new Color(0, 0, 0, 0.3f));
r.xMin -= 5; r.xMax -= 5;
if (!datas.TryGetValue(path, out var data))
datas.Add(path, data = new Data());
var frames = P(prop, "frames");
var arraySize = frames.arraySize;
data.selected = Mathf.Clamp(data.selected, -1, arraySize - 1);
var tr = new Rect(r) { height = 16 }; tr.y -= 5; tr.xMin = 0; tr.xMax += 30;
if (tr.Contains(mousePos))
{
if (evt.clickCount == 2)
{
frames.InsertArrayElementAtIndex(arraySize);
P(frames, arraySize, "time").floatValue = (mousePos.x - r.x) / (r.xMax - r.x);
}
if (evt.type == EventType.MouseDrag){
data.drag = Drag = true;
FocusedProp = path;
}
}
if (data.mouseUp){
data.mouseUp = false;
// Find selected frame after OrderBy time
for (int i = 0; i < arraySize; i++)
if (Math.Abs(data.dragTime - P(frames, i, "time").floatValue) < 0.001f)
{
data.selected = i;
Debug.Log(i);
break;
}
}
for (int i = arraySize - 1; i >= 0; i--)
{
var frame = P(frames, i);
var time = P(frame, "time");
var kr = new Rect(r) { width = 19, height = 19 };
kr.x = Mathf.Lerp(r.x, r.xMax, time.floatValue);
kr.y -= 6;
if (kr.Contains(mousePos) && evt.button == 0 && evt.type == EventType.MouseDown)
{
data.selected = i;
FocusedProp = path;
Repaint(prop);
}
var selected = i == data.selected && FocusedProp == path;
if (selected && data.drag)
{
//float xMin = r.x + 16;
//data.dragTime = time.floatValue = Mathf.Clamp01((mousePos.x - xMin) / (r.xMax - xMin));
data.dragTime = time.floatValue = Mathf.Clamp01(evt.delta.x / Screen.width + time.floatValue);
prop.serializedObject.ApplyModifiedProperties();
Repaint(prop);
}
if (selected && evt.type == EventType.KeyDown &&
(evt.keyCode == KeyCode.Delete || evt.keyCode == KeyCode.Backspace))
{
data.selected = -1;
frames.DeleteArrayElementAtIndex(i);
}
GUI.color = selected ? new Color32(117, 173, 255, 255) : new Color32(175, 175, 175, 255);
GUI.Box(kr, keyframeIcon, EditorStyles.label);
GUI.color = Color.white;
}
// End drag
if (data.drag && evt.type == EventType.MouseUp)
{
data.drag = Drag = false;
data.mouseUp = true;
prop.serializedObject.ApplyModifiedProperties();
Repaint(prop);
}
// Draw selected frame data
if (data.selected != -1)
{
data.currFrame = P(frames, data.selected);
DrawFrame(r, data.currFrame, data);
}
EditorGUI.EndProperty();
}
void DrawFrame(Rect r, SerializedProperty frame, Data data)
{
var time = P(frame, "time");
var value = P(frame, "value");
r.y += 10; r.xMax += 10;
value.isExpanded = true;
EditorGUI.Slider(new Rect(r) { height = 18 }, time, 0, 1);
if (GUI.changed)
KeyframesDrawer.Drag = true;
var itemRect = new Rect(r);
itemRect.y += 20;
data.itemHeight = 0;
var iterator = value.Copy();
var enterChildren = iterator.hasVisibleChildren;
var endProperty = iterator.GetEndProperty();
while (iterator.NextVisible(enterChildren) && !SerializedProperty.EqualContents(iterator, endProperty))
{
data.itemHeight += itemRect.height = EditorGUI.GetPropertyHeight(iterator, false);
enterChildren = EditorGUI.PropertyField(itemRect, iterator, enterChildren);
itemRect.y += itemRect.height + 2;
}
}
static SerializedProperty P(SerializedProperty p, int i) => p.GetArrayElementAtIndex(i);
static SerializedProperty P(SerializedProperty p, string n) => p.FindPropertyRelative(n);
static SerializedProperty P(SerializedProperty p, int i, string n) => p.GetArrayElementAtIndex(i).FindPropertyRelative(n);
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{
if (!datas.TryGetValue(prop.propertyPath, out var data) || data.selected == -1 || data.currFrame == null)
return 36;
return 56 + data.itemHeight;
}
static void Repaint(SerializedProperty prop)
{
var obj = prop.serializedObject;
foreach (var i in ActiveEditorTracker.sharedTracker.activeEditors)
if (i.serializedObject == obj) { i.Repaint(); return; }
}
}
#endif
}