-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathKeyframeHandle.cs
More file actions
175 lines (148 loc) · 6.06 KB
/
KeyframeHandle.cs
File metadata and controls
175 lines (148 loc) · 6.06 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System;
namespace BetterHandles
{
public class KeyframeHandle : CustomHandle
{
public Color pointColor;
public Color tangentColor;
public Color wireColor = Color.green;
public float tangentHandleSpacing = .3f;
public float tangentHandleScale = .75f;
public Free2DMoveHandle pointHandle = new Free2DMoveHandle();
public Free2DMoveHandle tangentHandle = new Free2DMoveHandle();
readonly int mainHandleHash = "mainCurve2Dhandle".GetHashCode();
readonly int tangentHandleHash = "tangentCurve2Dhandle".GetHashCode();
AnimationCurve curve = null;
public enum TangentDirection
{
In,
Out,
}
public Keyframe DrawHandle(Vector2 zone, Keyframe keyframe, float size, bool rightEditable = true, bool leftEditable = true)
{
if (e.type == EventType.MouseDown)
{
//we add the context menu when right clicking on the point Handle:
if (HandleUtility.nearestControl == pointHandle.controlId && e.button == 1)
KeyframeContextMenu(keyframe);
}
return DrawKeyframeHandle(zone, keyframe, size, rightEditable, leftEditable);
}
public void SetCurve(AnimationCurve curve)
{
this.curve = curve;
}
Vector2 TangentToDirection(float radTangent)
{
if (float.IsInfinity(radTangent))
return new Vector2(0, -tangentHandleSpacing);
return (new Vector2(1f, radTangent)).normalized * tangentHandleSpacing;
}
float DirectionToTangent(Vector2 direction, TangentDirection tangentDirection)
{
if (tangentDirection == TangentDirection.In && direction.x > 0.0001f)
return float.PositiveInfinity;
if (tangentDirection == TangentDirection.Out && direction.x < -0.0001f)
return float.PositiveInfinity;
return direction.y / direction.x;
}
bool IsSelected(int controlId)
{
return (EditorGUIUtility.hotControl == controlId || EditorGUIUtility.keyboardControl == controlId);
}
Keyframe DrawKeyframeHandle(Vector2 zone, Keyframe keyframe, float size, bool rightEditable, bool leftEditable)
{
pointHandle.SetTransform(this);
tangentHandle.SetTransform(this);
int pointControlId = EditorGUIUtility.GetControlID(mainHandleHash, FocusType.Keyboard);
int inTangentControlId = EditorGUIUtility.GetControlID(tangentHandleHash, FocusType.Keyboard);
int outTangentControlId = EditorGUIUtility.GetControlID(tangentHandleHash, FocusType.Keyboard);
// Debug.Log("hotContorl: " + EditorGUIUtility.keyboardControl + ", " + outTangentControlId + ", " + inTangentControlId + ", selected: " + selected);
//point position
Vector2 keyframePosition = new Vector2(zone.x * keyframe.time, zone.y * keyframe.value);
//tangent positions:
Vector2 inTangentPosition = -TangentToDirection(keyframe.inTangent);
Vector2 outTangentPosition = TangentToDirection(keyframe.outTangent);
if (e.type == EventType.Repaint)
{
//tangent Wires:
HandlesMaterials.vertexColor.SetPass(0);
GL.Begin(GL.LINES);
{
GL.Color(wireColor);
if (rightEditable)
{
GL.Vertex(matrix.MultiplyPoint3x4(keyframePosition));
GL.Vertex(matrix.MultiplyPoint3x4(inTangentPosition + keyframePosition));
}
if (leftEditable)
{
GL.Vertex(matrix.MultiplyPoint3x4(keyframePosition));
GL.Vertex(matrix.MultiplyPoint3x4(outTangentPosition + keyframePosition));
}
}
GL.End();
}
//draw main point Handle
keyframePosition = pointHandle.DrawHandle(pointControlId, keyframePosition, size);
//draw tangents Handles
inTangentPosition += keyframePosition;
outTangentPosition += keyframePosition;
if (rightEditable)
{
inTangentPosition = tangentHandle.DrawHandle(inTangentControlId, inTangentPosition, size * tangentHandleScale);
keyframe.inTangent = DirectionToTangent(inTangentPosition - keyframePosition, TangentDirection.In);
}
if (leftEditable)
{
outTangentPosition = tangentHandle.DrawHandle(outTangentControlId, outTangentPosition, size * tangentHandleScale);
keyframe.outTangent = DirectionToTangent(outTangentPosition - keyframePosition, TangentDirection.Out);
}
//set back keyframe values
keyframe.time = keyframePosition.x / zone.x;
keyframe.value = keyframePosition.y / zone.y;
return keyframe;
}
void KeyframeContextMenu(Keyframe keyframe)
{
GenericMenu menu = new GenericMenu();
if (curve != null)
{
int keyframeIndex = curve.keys.ToList().FindIndex(k => k.Equal(keyframe)) - 1;
Action< bool, string, AnimationUtility.TangentMode > SetTangentModeMenu = (right, text, tangentMode) => {
menu.AddItem(new GUIContent(text), false, () => {
if (right)
AnimationUtility.SetKeyRightTangentMode(curve, keyframeIndex, tangentMode);
else
AnimationUtility.SetKeyLeftTangentMode(curve, keyframeIndex, tangentMode);
GUI.changed = true;
});
};
SetTangentModeMenu(false, "Left Tangent/Auto", AnimationUtility.TangentMode.Auto);
SetTangentModeMenu(false, "Left Tangent/ClampedAuto", AnimationUtility.TangentMode.ClampedAuto);
SetTangentModeMenu(false, "Left Tangent/Constant", AnimationUtility.TangentMode.Constant);
SetTangentModeMenu(false, "Left Tangent/Free", AnimationUtility.TangentMode.Free);
SetTangentModeMenu(false, "Left Tangent/Linear", AnimationUtility.TangentMode.Linear);
SetTangentModeMenu(true, "Right Tangent/Auto", AnimationUtility.TangentMode.Auto);
SetTangentModeMenu(true, "Right Tangent/ClampedAuto", AnimationUtility.TangentMode.ClampedAuto);
SetTangentModeMenu(true, "Right Tangent/Constant", AnimationUtility.TangentMode.Constant);
SetTangentModeMenu(true, "Right Tangent/Free", AnimationUtility.TangentMode.Free);
SetTangentModeMenu(true, "Right Tangent/Linear", AnimationUtility.TangentMode.Linear);
menu.AddItem(new GUIContent("remove"), false, () => {
GUI.changed = true;
if (keyframeIndex == -1)
keyframeIndex = curve.keys.Length - 1;
curve.RemoveKey(keyframeIndex);
});
}
else
menu.AddDisabledItem(new GUIContent("Curve not set for keyframe !"));
menu.ShowAsContext();
}
}
}