@@ -15,7 +15,7 @@ internal class AnimationPropertyContextualMenu
1515 {
1616 public static AnimationPropertyContextualMenu Instance = new AnimationPropertyContextualMenu ( ) ;
1717
18- private List < IAnimationContextualResponder > m_Responders = new List < IAnimationContextualResponder > ( ) ;
18+ IAnimationContextualResponder m_Responder ;
1919
2020 private static GUIContent addKeyContent = EditorGUIUtility . TextContent ( "Add Key" ) ;
2121 private static GUIContent updateKeyContent = EditorGUIUtility . TextContent ( "Update Key" ) ;
@@ -29,117 +29,140 @@ internal class AnimationPropertyContextualMenu
2929 public AnimationPropertyContextualMenu ( )
3030 {
3131 EditorApplication . contextualPropertyMenu += OnPropertyContextMenu ;
32+ MaterialEditor . contextualPropertyMenu += OnPropertyContextMenu ;
3233 }
3334
34- public void AddResponder ( IAnimationContextualResponder responder )
35+ public void SetResponder ( IAnimationContextualResponder responder )
3536 {
36- m_Responders . Add ( responder ) ;
37- }
38-
39- public void RemoveResponder ( IAnimationContextualResponder responder )
40- {
41- m_Responders . Remove ( responder ) ;
37+ m_Responder = responder ;
4238 }
4339
4440 void OnPropertyContextMenu ( GenericMenu menu , SerializedProperty property )
4541 {
46- if ( m_Responders . Count == 0 )
42+ if ( m_Responder == null )
4743 return ;
4844
49- bool isPropertyAnimatable = m_Responders . Exists ( responder => responder . IsAnimatable ( property ) ) ;
45+ PropertyModification [ ] modifications = AnimationWindowUtility . SerializedPropertyToPropertyModifications ( property ) ;
46+
47+ bool isPropertyAnimatable = m_Responder . IsAnimatable ( modifications ) ;
5048 if ( isPropertyAnimatable )
5149 {
52- bool isEditable = m_Responders . Exists ( responder => responder . IsEditable ( property ) ) ;
53- bool hasKey = isEditable && m_Responders . Exists ( responder => responder . KeyExists ( property ) ) ;
54- bool hasCandidate = isEditable && m_Responders . Exists ( responder => responder . CandidateExists ( property ) ) ;
55- bool hasCurve = isEditable && ( hasKey || m_Responders . Exists ( responder => responder . CurveExists ( property ) ) ) ;
50+ var targetObject = property . serializedObject . targetObject ;
51+ if ( m_Responder . IsEditable ( targetObject ) )
52+ OnPropertyContextMenu ( menu , modifications ) ;
53+ else
54+ OnDisabledPropertyContextMenu ( menu ) ;
55+ }
56+ }
5657
57- bool hasAnyCandidate = isEditable && m_Responders . Exists ( responder => responder . HasAnyCandidates ( ) ) ;
58- bool hasAnyCurve = isEditable && m_Responders . Exists ( responder => responder . HasAnyCurves ( ) ) ;
58+ void OnPropertyContextMenu ( GenericMenu menu , MaterialProperty property , Renderer targetObject )
59+ {
60+ if ( m_Responder == null )
61+ return ;
5962
60- // Important to pass a copy, the original can get Next called on it
61- // before the callback invoked
62- var propertyCopy = property . Copy ( ) ;
63+ if ( property . targets == null || property . targets . Length == 0 )
64+ return ;
6365
64- if ( isEditable )
65- {
66- menu . AddItem ( ( ( hasKey && hasCandidate ) ? updateKeyContent : addKeyContent ) , false , ( ) =>
67- {
68- m_Responders . ForEach ( responder => responder . AddKey ( propertyCopy ) ) ;
69- } ) ;
70- }
71- else
72- {
73- menu . AddDisabledItem ( addKeyContent ) ;
74- }
66+ PropertyModification [ ] modifications = MaterialAnimationUtility . MaterialPropertyToPropertyModifications ( property , targetObject ) ;
67+ if ( m_Responder . IsEditable ( targetObject ) )
68+ OnPropertyContextMenu ( menu , modifications ) ;
69+ else
70+ OnDisabledPropertyContextMenu ( menu ) ;
71+ }
7572
76- if ( hasKey )
77- {
78- menu . AddItem ( removeKeyContent , false , ( ) =>
79- {
80- m_Responders . ForEach ( responder => responder . RemoveKey ( propertyCopy ) ) ;
81- } ) ;
82- }
83- else
84- {
85- menu . AddDisabledItem ( removeKeyContent ) ;
86- }
73+ void OnPropertyContextMenu ( GenericMenu menu , PropertyModification [ ] modifications )
74+ {
75+ bool hasKey = m_Responder . KeyExists ( modifications ) ;
76+ bool hasCandidate = m_Responder . CandidateExists ( modifications ) ;
77+ bool hasCurve = ( hasKey || m_Responder . CurveExists ( modifications ) ) ;
8778
88- if ( hasCurve )
89- {
90- menu . AddItem ( removeCurveContent , false , ( ) =>
91- {
92- m_Responders . ForEach ( responder => responder . RemoveCurve ( propertyCopy ) ) ;
93- } ) ;
94- }
95- else
96- {
97- menu . AddDisabledItem ( removeCurveContent ) ;
98- }
79+ bool hasAnyCandidate = m_Responder . HasAnyCandidates ( ) ;
80+ bool hasAnyCurve = m_Responder . HasAnyCurves ( ) ;
9981
100- menu . AddSeparator ( string . Empty ) ;
101- if ( hasAnyCandidate )
82+ menu . AddItem ( ( ( hasKey && hasCandidate ) ? updateKeyContent : addKeyContent ) , false , ( ) =>
10283 {
103- menu . AddItem ( addCandidatesContent , false , ( ) =>
104- {
105- m_Responders . ForEach ( responder => responder . AddCandidateKeys ( ) ) ;
106- } ) ;
107- }
108- else
109- {
110- menu . AddDisabledItem ( addCandidatesContent ) ;
111- }
84+ m_Responder . AddKey ( modifications ) ;
85+ } ) ;
11286
113- if ( hasAnyCurve )
114- {
115- menu . AddItem ( addAnimatedContent , false , ( ) =>
116- {
117- m_Responders . ForEach ( responder => responder . AddAnimatedKeys ( ) ) ;
118- } ) ;
119- }
120- else
121- {
122- menu . AddDisabledItem ( addAnimatedContent ) ;
123- }
87+ if ( hasKey )
88+ {
89+ menu . AddItem ( removeKeyContent , false , ( ) =>
90+ {
91+ m_Responder . RemoveKey ( modifications ) ;
92+ } ) ;
93+ }
94+ else
95+ {
96+ menu . AddDisabledItem ( removeKeyContent ) ;
97+ }
12498
125- menu . AddSeparator ( string . Empty ) ;
126- if ( hasCurve )
127- {
128- menu . AddItem ( goToPreviousKeyContent , false , ( ) =>
129- {
130- m_Responders . ForEach ( responder => responder . GoToPreviousKeyframe ( propertyCopy ) ) ;
131- } ) ;
132- menu . AddItem ( goToNextKeyContent , false , ( ) =>
133- {
134- m_Responders . ForEach ( responder => responder . GoToNextKeyframe ( propertyCopy ) ) ;
135- } ) ;
136- }
137- else
138- {
139- menu . AddDisabledItem ( goToPreviousKeyContent ) ;
140- menu . AddDisabledItem ( goToNextKeyContent ) ;
141- }
99+ if ( hasCurve )
100+ {
101+ menu . AddItem ( removeCurveContent , false , ( ) =>
102+ {
103+ m_Responder . RemoveCurve ( modifications ) ;
104+ } ) ;
105+ }
106+ else
107+ {
108+ menu . AddDisabledItem ( removeCurveContent ) ;
109+ }
110+
111+ menu . AddSeparator ( string . Empty ) ;
112+ if ( hasAnyCandidate )
113+ {
114+ menu . AddItem ( addCandidatesContent , false , ( ) =>
115+ {
116+ m_Responder . AddCandidateKeys ( ) ;
117+ } ) ;
118+ }
119+ else
120+ {
121+ menu . AddDisabledItem ( addCandidatesContent ) ;
142122 }
123+
124+ if ( hasAnyCurve )
125+ {
126+ menu . AddItem ( addAnimatedContent , false , ( ) =>
127+ {
128+ m_Responder . AddAnimatedKeys ( ) ;
129+ } ) ;
130+ }
131+ else
132+ {
133+ menu . AddDisabledItem ( addAnimatedContent ) ;
134+ }
135+
136+ menu . AddSeparator ( string . Empty ) ;
137+ if ( hasCurve )
138+ {
139+ menu . AddItem ( goToPreviousKeyContent , false , ( ) =>
140+ {
141+ m_Responder . GoToPreviousKeyframe ( modifications ) ;
142+ } ) ;
143+ menu . AddItem ( goToNextKeyContent , false , ( ) =>
144+ {
145+ m_Responder . GoToNextKeyframe ( modifications ) ;
146+ } ) ;
147+ }
148+ else
149+ {
150+ menu . AddDisabledItem ( goToPreviousKeyContent ) ;
151+ menu . AddDisabledItem ( goToNextKeyContent ) ;
152+ }
153+ }
154+
155+ void OnDisabledPropertyContextMenu ( GenericMenu menu )
156+ {
157+ menu . AddDisabledItem ( addKeyContent ) ;
158+ menu . AddDisabledItem ( removeKeyContent ) ;
159+ menu . AddDisabledItem ( removeCurveContent ) ;
160+ menu . AddSeparator ( string . Empty ) ;
161+ menu . AddDisabledItem ( addCandidatesContent ) ;
162+ menu . AddDisabledItem ( addAnimatedContent ) ;
163+ menu . AddSeparator ( string . Empty ) ;
164+ menu . AddDisabledItem ( goToPreviousKeyContent ) ;
165+ menu . AddDisabledItem ( goToNextKeyContent ) ;
143166 }
144167 }
145168}
0 commit comments