forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationContextualPropertyMenu.cs
More file actions
181 lines (157 loc) · 6.46 KB
/
Copy pathAnimationContextualPropertyMenu.cs
File metadata and controls
181 lines (157 loc) · 6.46 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
176
177
178
179
180
181
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;
namespace UnityEditorInternal
{
internal class AnimationPropertyContextualMenu
{
public static AnimationPropertyContextualMenu Instance = new AnimationPropertyContextualMenu();
IAnimationContextualResponder m_Responder;
private static GUIContent addKeyContent = EditorGUIUtility.TrTextContent("Add Key");
private static GUIContent updateKeyContent = EditorGUIUtility.TrTextContent("Update Key");
private static GUIContent removeKeyContent = EditorGUIUtility.TrTextContent("Remove Key");
private static GUIContent removeCurveContent = EditorGUIUtility.TrTextContent("Remove All Keys");
private static GUIContent goToPreviousKeyContent = EditorGUIUtility.TrTextContent("Go to Previous Key");
private static GUIContent goToNextKeyContent = EditorGUIUtility.TrTextContent("Go to Next Key");
private static GUIContent addCandidatesContent = EditorGUIUtility.TrTextContent("Key All Modified");
private static GUIContent addAnimatedContent = EditorGUIUtility.TrTextContent("Key All Animated");
public AnimationPropertyContextualMenu()
{
EditorApplication.contextualPropertyMenu += OnPropertyContextMenu;
MaterialEditor.contextualPropertyMenu += OnPropertyContextMenu;
}
public void SetResponder(IAnimationContextualResponder responder)
{
m_Responder = responder;
}
public bool IsResponder(IAnimationContextualResponder responder)
{
return responder == m_Responder;
}
void OnPropertyContextMenu(GenericMenu menu, SerializedProperty property)
{
if (m_Responder == null)
return;
PropertyModification[] modifications = AnimationWindowUtility.SerializedPropertyToPropertyModifications(property);
bool isPropertyAnimatable = m_Responder.IsAnimatable(modifications);
if (isPropertyAnimatable)
{
var targetObject = property.serializedObject.targetObject;
if (m_Responder.IsEditable(targetObject))
OnPropertyContextMenu(menu, modifications);
else
OnDisabledPropertyContextMenu(menu);
}
}
void OnPropertyContextMenu(GenericMenu menu, MaterialProperty property, Renderer[] renderers)
{
if (m_Responder == null)
return;
if (property.targets == null || property.targets.Length == 0)
return;
if (renderers == null || renderers.Length == 0)
return;
var modifications = new List<PropertyModification>();
foreach (Renderer renderer in renderers)
{
modifications.AddRange(MaterialAnimationUtility.MaterialPropertyToPropertyModifications(property, renderer));
}
if (m_Responder.IsEditable(renderers[0]))
OnPropertyContextMenu(menu, modifications.ToArray());
else
OnDisabledPropertyContextMenu(menu);
}
void OnPropertyContextMenu(GenericMenu menu, PropertyModification[] modifications)
{
bool hasKey = m_Responder.KeyExists(modifications);
bool hasCandidate = m_Responder.CandidateExists(modifications);
bool hasCurve = (hasKey || m_Responder.CurveExists(modifications));
bool hasAnyCandidate = m_Responder.HasAnyCandidates();
bool hasAnyCurve = m_Responder.HasAnyCurves();
menu.AddItem(((hasKey && hasCandidate) ? updateKeyContent : addKeyContent), false, () =>
{
m_Responder.AddKey(modifications);
});
if (hasKey)
{
menu.AddItem(removeKeyContent, false, () =>
{
m_Responder.RemoveKey(modifications);
});
}
else
{
menu.AddDisabledItem(removeKeyContent);
}
if (hasCurve)
{
menu.AddItem(removeCurveContent, false, () =>
{
m_Responder.RemoveCurve(modifications);
});
}
else
{
menu.AddDisabledItem(removeCurveContent);
}
menu.AddSeparator(string.Empty);
if (hasAnyCandidate)
{
menu.AddItem(addCandidatesContent, false, () =>
{
m_Responder.AddCandidateKeys();
});
}
else
{
menu.AddDisabledItem(addCandidatesContent);
}
if (hasAnyCurve)
{
menu.AddItem(addAnimatedContent, false, () =>
{
m_Responder.AddAnimatedKeys();
});
}
else
{
menu.AddDisabledItem(addAnimatedContent);
}
menu.AddSeparator(string.Empty);
if (hasCurve)
{
menu.AddItem(goToPreviousKeyContent, false, () =>
{
m_Responder.GoToPreviousKeyframe(modifications);
});
menu.AddItem(goToNextKeyContent, false, () =>
{
m_Responder.GoToNextKeyframe(modifications);
});
}
else
{
menu.AddDisabledItem(goToPreviousKeyContent);
menu.AddDisabledItem(goToNextKeyContent);
}
}
void OnDisabledPropertyContextMenu(GenericMenu menu)
{
menu.AddDisabledItem(addKeyContent);
menu.AddDisabledItem(removeKeyContent);
menu.AddDisabledItem(removeCurveContent);
menu.AddSeparator(string.Empty);
menu.AddDisabledItem(addCandidatesContent);
menu.AddDisabledItem(addAnimatedContent);
menu.AddSeparator(string.Empty);
menu.AddDisabledItem(goToPreviousKeyContent);
menu.AddDisabledItem(goToNextKeyContent);
}
}
}