FAnimationTrackEditor.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System;
  6. using Flux;
  7. namespace FluxEditor
  8. {
  9. [FEditor(typeof(FAnimationTrack))]
  10. public class FAnimationTrackEditor : FTrackEditor {
  11. #region Debug tools
  12. private bool _syncWithAnimationWindow = false;
  13. private bool SyncWithAnimationWindow {
  14. get { return _syncWithAnimationWindow; }
  15. set {
  16. if( value )
  17. {
  18. SequenceEditor.OnUpdateEvent.AddListener( OnUpdate );
  19. foreach( FContainerEditor containerEditor in SequenceEditor.Editors )
  20. {
  21. List<FTimelineEditor> timelineEditors = containerEditor.Editors;
  22. foreach( FTimelineEditor timelineEditor in timelineEditors )
  23. {
  24. List<FTrackEditor> trackEditors = timelineEditor.Editors;
  25. foreach( FTrackEditor trackEditor in trackEditors )
  26. {
  27. if( trackEditor is FAnimationTrackEditor && ((FAnimationTrackEditor)trackEditor).SyncWithAnimationWindow )
  28. ((FAnimationTrackEditor)trackEditor).SyncWithAnimationWindow = false;
  29. }
  30. }
  31. }
  32. AnimationWindowProxy.OpenAnimationWindow();
  33. }
  34. else
  35. {
  36. if( TimelineEditor != null )
  37. SequenceEditor.OnUpdateEvent.RemoveListener( OnUpdate );
  38. if( AnimationMode.InAnimationMode() )
  39. AnimationMode.StopAnimationMode();
  40. }
  41. _syncWithAnimationWindow = value;
  42. if( TimelineEditor != null )
  43. SequenceEditor.Repaint();
  44. }
  45. }
  46. private bool _showKeyframes = false;
  47. public bool ShowKeyframes
  48. {
  49. get { return _showKeyframes; }
  50. set { _showKeyframes = value; }
  51. }
  52. private bool _showKeyframeTimes = false;
  53. public bool ShowKeyframeTimes
  54. {
  55. get { return _showKeyframeTimes; }
  56. set { _showKeyframeTimes = value; }
  57. }
  58. private bool _showTransformPath = false;
  59. public bool ShowTransformPath
  60. {
  61. get{ return _showTransformPath; }
  62. set{ _showTransformPath = value; }
  63. }
  64. #endregion
  65. protected override void OnEnable()
  66. {
  67. base.OnEnable();
  68. SceneView.onSceneGUIDelegate += OnSceneGUI;
  69. // was it syncing anim? hook it up again, because UnityEvent will
  70. // lose references on compile
  71. if( _syncWithAnimationWindow )
  72. {
  73. _syncWithAnimationWindow = false;
  74. // SyncWithAnimationWindow = true;
  75. }
  76. }
  77. private FEvent _previousEvent = null;
  78. private void OnUpdate()
  79. {
  80. if( !_syncWithAnimationWindow )
  81. return;
  82. if( Selection.activeTransform != Track.Owner )
  83. {
  84. Selection.activeTransform = Track.Owner;
  85. }
  86. if( !AnimationMode.InAnimationMode() )
  87. {
  88. AnimationWindowProxy.StartAnimationMode();
  89. }
  90. int animWindowFrame = AnimationWindowProxy.GetCurrentFrame();
  91. FEvent[] evts = new FEvent[2];
  92. int numEvts = Track.GetEventsAt( SequenceEditor.Sequence.CurrentFrame, evts );
  93. if( numEvts > 0 )
  94. {
  95. if( numEvts == 1 )
  96. {
  97. _previousEvent = evts[0];
  98. }
  99. else if( numEvts == 2 )
  100. {
  101. if( _previousEvent != evts[0] && _previousEvent != evts[1] )
  102. {
  103. _previousEvent = evts[1];
  104. }
  105. }
  106. FPlayAnimationEvent animEvt = (FPlayAnimationEvent)_previousEvent;
  107. if( animEvt.ControlsAnimation )
  108. {
  109. int normCurrentFrame = SequenceEditor.Sequence.CurrentFrame - animEvt.Start;
  110. if( AnimationWindowProxy.GetSelectedAnimationClip() != animEvt._animationClip )
  111. {
  112. AnimationWindowProxy.SelectAnimationClip( animEvt._animationClip );
  113. AnimationWindowProxy.SetCurrentFrame( normCurrentFrame, SequenceEditor.Sequence.CurrentTime - animEvt.StartTime );
  114. }
  115. if( animWindowFrame > animEvt.Length )
  116. {
  117. animWindowFrame = animEvt.Length;
  118. AnimationWindowProxy.SetCurrentFrame( animWindowFrame, animEvt.LengthTime );
  119. }
  120. if( animWindowFrame >= 0 && animWindowFrame != normCurrentFrame )
  121. {
  122. SequenceEditor.SetCurrentFrame( animEvt.Start + animWindowFrame );
  123. SequenceEditor.Repaint();
  124. }
  125. }
  126. }
  127. }
  128. protected override void OnDestroy()
  129. {
  130. base.OnDestroy();
  131. SceneView.onSceneGUIDelegate -= OnSceneGUI;
  132. if( SyncWithAnimationWindow )
  133. SyncWithAnimationWindow = false;
  134. }
  135. public override void Init( FObject obj, FEditor owner )
  136. {
  137. base.Init( obj, owner );
  138. FAnimationTrack animTrack = (FAnimationTrack)Obj;
  139. if( animTrack.Owner.GetComponent<Animator>() == null )
  140. {
  141. Animator animator = animTrack.Owner.gameObject.AddComponent<Animator>();
  142. Undo.RegisterCreatedObjectUndo( animator, string.Empty );
  143. }
  144. }
  145. public override void OnTrackChanged()
  146. {
  147. FAnimationTrackInspector.RebuildStateMachine( (FAnimationTrack)Track );
  148. }
  149. public override void Render( Rect rect, float headerWidth )
  150. {
  151. // bool isPreviewing = _track.IsPreviewing;
  152. base.Render( rect, headerWidth );
  153. // if( isPreviewing != _track.IsPreviewing )
  154. // {
  155. // if( Event.current.alt )
  156. // SyncWithAnimationWindow = true;
  157. // }
  158. switch( Event.current.type )
  159. {
  160. case EventType.DragUpdated:
  161. if( rect.Contains(Event.current.mousePosition ) )
  162. {
  163. int numAnimationsDragged = FAnimationEventInspector.NumAnimationsDragAndDrop( Track.Sequence.FrameRate );
  164. int frame = SequenceEditor.GetFrameForX( Event.current.mousePosition.x );
  165. DragAndDrop.visualMode = numAnimationsDragged > 0 && Track.CanAddAt(frame) ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
  166. Event.current.Use();
  167. }
  168. break;
  169. case EventType.DragPerform:
  170. if( rect.Contains(Event.current.mousePosition ) )
  171. {
  172. AnimationClip animClip = FAnimationEventInspector.GetAnimationClipDragAndDrop( Track.Sequence.FrameRate );
  173. if( animClip && Mathf.Approximately(animClip.frameRate, Track.Sequence.FrameRate) )
  174. {
  175. int frame = SequenceEditor.GetFrameForX( Event.current.mousePosition.x );
  176. int maxLength;
  177. if( Track.CanAddAt( frame, out maxLength ) )
  178. {
  179. FPlayAnimationEvent animEvt = FEvent.Create<FPlayAnimationEvent>( new FrameRange( frame, frame + Mathf.Min(maxLength, Mathf.RoundToInt(animClip.length*animClip.frameRate)) ) );
  180. Track.Add( animEvt );
  181. FAnimationEventInspector.SetAnimationClip( animEvt, animClip );
  182. DragAndDrop.AcceptDrag();
  183. }
  184. }
  185. Event.current.Use();
  186. }
  187. break;
  188. }
  189. }
  190. public override bool HasTools()
  191. {
  192. bool controlsAllAnimations = true;
  193. foreach( FAnimationEventEditor animEvtEditor in _eventEditors )
  194. {
  195. if( !((FPlayAnimationEvent)animEvtEditor.Evt).ControlsAnimation )
  196. {
  197. controlsAllAnimations = false;
  198. break;
  199. }
  200. }
  201. return controlsAllAnimations;
  202. }
  203. public override void OnToolsGUI ()
  204. {
  205. // bool canSyncWithAnimationWindow = true;
  206. // foreach( FAnimationEventEditor animEvtEditor in _eventEditors )
  207. // {
  208. // if(! ((FPlayAnimationEvent)animEvtEditor._evt).ControlsAnimation )
  209. // {
  210. // canSyncWithAnimationWindow = false;
  211. // break;
  212. // }
  213. // }
  214. //
  215. // if( canSyncWithAnimationWindow )
  216. {
  217. bool syncWithAnimationWindow = EditorGUILayout.Toggle( "Sync w/ Animation Window", SyncWithAnimationWindow );
  218. if( syncWithAnimationWindow != SyncWithAnimationWindow )
  219. SyncWithAnimationWindow = syncWithAnimationWindow;
  220. }
  221. bool showTransformPath = EditorGUILayout.Toggle( "Show Transform Path", ShowTransformPath );
  222. if( showTransformPath != ShowTransformPath )
  223. ShowTransformPath = showTransformPath;
  224. if( !ShowTransformPath )
  225. GUI.enabled = false;
  226. bool showKeyframes = EditorGUILayout.Toggle( "Show Keyframes", ShowKeyframes );
  227. if( showKeyframes != ShowKeyframes )
  228. ShowKeyframes = showKeyframes;
  229. bool showKeyframeTimes = EditorGUILayout.Toggle( "Show Keyframe Times", ShowKeyframeTimes );
  230. if( showKeyframeTimes != ShowKeyframeTimes )
  231. ShowKeyframeTimes = showKeyframeTimes;
  232. GUI.enabled = true;
  233. }
  234. protected override Color GetPreviewIconColor()
  235. {
  236. return _syncWithAnimationWindow ? Color.red : base.GetPreviewIconColor();
  237. }
  238. void OnSceneGUI( SceneView sceneView )
  239. {
  240. if( Track == null )
  241. return;
  242. for( int i = 0; i != _eventEditors.Count; ++i )
  243. {
  244. FAnimationEventEditor animEvtEditor = (FAnimationEventEditor)_eventEditors[i];
  245. FPlayAnimationEvent animEvt = (FPlayAnimationEvent)_eventEditors[i].Evt;
  246. if( animEvt._animationClip != null && Flux.FUtility.IsAnimationEditable(animEvt._animationClip) && ShowTransformPath /*_track.IsPreviewing*/ )
  247. {
  248. animEvtEditor.RenderTransformCurves( animEvt.Sequence.FrameRate );
  249. }
  250. }
  251. SceneView.RepaintAll();
  252. }
  253. private void RenderTransformPath( TransformCurves transformCurves, float length, float samplingDelta )
  254. {
  255. float t = 0;
  256. int numberSamples = Mathf.RoundToInt(length/samplingDelta)+1;
  257. float delta = length / numberSamples;
  258. Vector3[] pts = new Vector3[numberSamples];
  259. int index = 0;
  260. while( index < numberSamples )
  261. {
  262. pts[index++] = transformCurves.GetPosition( t );
  263. t += delta;
  264. }
  265. if( index != pts.Length )
  266. Debug.LogError("Number of samples doesn't match: " + (index+1) + " instead of " + pts.Length);
  267. Handles.DrawPolyLine( pts );
  268. }
  269. private void RenderTransformAnimation( TransformCurves transformCurves, float time )
  270. {
  271. Vector3 pos = transformCurves.GetPosition(time);
  272. Quaternion rot = transformCurves.GetRotation(time);
  273. Vector3 scale = transformCurves.GetScale(time);
  274. transformCurves.bone.localScale = scale;
  275. transformCurves.bone.localRotation = rot;
  276. transformCurves.bone.localPosition = pos;
  277. Handles.RectangleHandleCap(0, pos, rot, 0.1f, EventType.MouseDown );
  278. Handles.RectangleHandleCap(0, pos + rot*Vector3.forward, rot, 0.4f, EventType.MouseDown );
  279. }
  280. public override void UpdateEventsEditor (int frame, float time)
  281. {
  282. if( Track.RequiresEditorCache && !Track.HasCache && Track.CanCreateCache() )
  283. {
  284. OnToggle( true );
  285. }
  286. base.UpdateEventsEditor (frame, time);
  287. if( _syncWithAnimationWindow )
  288. {
  289. FEvent[] evts = new FEvent[2];
  290. int numEvts = Track.GetEventsAt( frame, evts );
  291. if( numEvts > 0 )
  292. {
  293. if( numEvts == 1 )
  294. {
  295. _previousEvent = evts[0];
  296. }
  297. else if( numEvts == 2 )
  298. {
  299. if( _previousEvent != evts[0] && _previousEvent != evts[1] )
  300. {
  301. _previousEvent = evts[1];
  302. }
  303. }
  304. FPlayAnimationEvent animEvt = (FPlayAnimationEvent)_previousEvent;
  305. if( animEvt._animationClip != AnimationWindowProxy.GetSelectedAnimationClip() )
  306. {
  307. AnimationWindowProxy.SelectAnimationClip( animEvt._animationClip );
  308. }
  309. AnimationWindowProxy.SetCurrentFrame( frame - animEvt.Start, time - animEvt.StartTime );
  310. }
  311. }
  312. }
  313. }
  314. }