FTimelineEditor.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.AnimatedValues;
  4. using UnityEditorInternal;
  5. using System;
  6. using System.Collections.Generic;
  7. using Flux;
  8. namespace FluxEditor
  9. {
  10. public class FTimelineEditor : FEditorList<FTrackEditor>
  11. {
  12. public const int HEADER_HEIGHT = 25;
  13. public FContainerEditor ContainerEditor { get { return (FContainerEditor)Owner; } }
  14. public FTimeline Timeline { get { return (FTimeline)Obj; } }
  15. public bool _showHeader = true;
  16. public bool _showTracks = true;
  17. public override void Init( FObject obj, FEditor owner )
  18. {
  19. base.Init( obj, owner );
  20. if( Timeline.Owner == null )
  21. Timeline.Awake();
  22. Editors.Clear();
  23. List<FTrack> tracks = Timeline.Tracks;
  24. for( int i = 0; i < tracks.Count; ++i )
  25. {
  26. FTrack track = tracks[i];
  27. FTrackEditor trackEditor = ContainerEditor.SequenceEditor.GetEditor<FTrackEditor>(track);
  28. trackEditor.Init( track, this );
  29. Editors.Add( trackEditor );
  30. }
  31. _icon = new GUIContent(FUtility.GetFluxTexture( "Plus.png" ));
  32. }
  33. public void OnStop()
  34. {
  35. for( int i = 0; i != Editors.Count; ++i )
  36. Editors[i].OnStop();
  37. }
  38. public override float Height {
  39. get {
  40. float headerHeight = _showHeader ? HEADER_HEIGHT : 0;
  41. float tracksHeight = 0;
  42. foreach( FTrackEditor trackEditor in Editors )
  43. tracksHeight += trackEditor.Height;
  44. tracksHeight *= ShowPercentage;
  45. return headerHeight + tracksHeight;
  46. }
  47. }
  48. public override float HeaderHeight {
  49. get {
  50. return HEADER_HEIGHT;
  51. }
  52. }
  53. protected override string HeaderText {
  54. get {
  55. return Timeline.Owner != null ? Timeline.Owner.name : Timeline.name + " (Missing)";//Obj.Owner.name;
  56. }
  57. }
  58. protected override bool IconOnLeft {
  59. get {
  60. return false;
  61. }
  62. }
  63. public FTrackEditor GetTrackEditor( Vector2 pos )
  64. {
  65. if( !_showTracks
  66. || !Rect.Contains( pos )
  67. || (_showHeader && pos.y < Rect.yMin + HEADER_HEIGHT) ) return null;
  68. for( int i = 0; i != Editors.Count; ++i )
  69. {
  70. if( Editors[i].Rect.Contains( pos ) )
  71. return Editors[i];
  72. }
  73. return null; // shouldn't happen
  74. }
  75. protected override Color BackgroundColor {
  76. get {
  77. return FGUI.GetTimelineColor();
  78. }
  79. }
  80. protected override bool CanPaste(FObject obj)
  81. {
  82. // since Unity Objs can be "fake null"
  83. return obj != null && obj is FTrack;
  84. }
  85. protected override void Paste(object obj)
  86. {
  87. if( !CanPaste(obj as FObject) )
  88. return;
  89. Undo.RecordObject( Timeline, string.Empty );
  90. FTrack track = Instantiate<FTrack>((FTrack)obj);
  91. track.hideFlags = Timeline.hideFlags;
  92. Timeline.Add( track );
  93. Undo.RegisterCreatedObjectUndo( track.gameObject, "Paste Track " + ((FTrack)obj).name );
  94. }
  95. protected override void Delete()
  96. {
  97. SequenceEditor.DestroyEditor( this );
  98. }
  99. protected override void OnHeaderInput (Rect labelRect, Rect iconRect)
  100. {
  101. if( Event.current.type == EventType.MouseDown && Event.current.clickCount > 1 && labelRect.Contains( Event.current.mousePosition ) )
  102. {
  103. Selection.activeTransform = Timeline.Owner;
  104. Event.current.Use();
  105. }
  106. base.OnHeaderInput(labelRect, iconRect);
  107. if( Event.current.type == EventType.MouseDown && iconRect.Contains(Event.current.mousePosition) )
  108. {
  109. ShowAddTrackMenu();
  110. }
  111. }
  112. private void ShowAddTrackMenu()
  113. {
  114. Event.current.Use();
  115. GenericMenu menu = new GenericMenu();
  116. System.Reflection.Assembly fluxAssembly = typeof(FEvent).Assembly;
  117. Type[] types = typeof(FEvent).Assembly.GetTypes();
  118. if( fluxAssembly.GetName().Name != "Assembly-CSharp" )
  119. {
  120. // if we are in the flux trial, basically allow to get the types in the project assembly
  121. ArrayUtility.AddRange<Type>( ref types, System.Reflection.Assembly.Load("Assembly-CSharp").GetTypes() );
  122. }
  123. List<KeyValuePair<Type, FEventAttribute>> validTypeList = new List<KeyValuePair<Type, FEventAttribute>>();
  124. foreach( Type t in types )
  125. {
  126. if( !typeof(FEvent).IsAssignableFrom( t ) )
  127. continue;
  128. object[] attributes = t.GetCustomAttributes(typeof(FEventAttribute), false);
  129. if( attributes.Length == 0 || ((FEventAttribute)attributes[0]).menu == null )
  130. continue;
  131. validTypeList.Add( new KeyValuePair<Type, FEventAttribute>(t, (FEventAttribute)attributes[0]) );
  132. }
  133. validTypeList.Sort( delegate(KeyValuePair<Type, FEventAttribute> x, KeyValuePair<Type, FEventAttribute> y)
  134. {
  135. return x.Value.menu.CompareTo( y.Value.menu );
  136. });
  137. foreach( KeyValuePair<Type, FEventAttribute> kvp in validTypeList )
  138. {
  139. menu.AddItem( new GUIContent(kvp.Value.menu), false, AddTrackMenu, kvp );
  140. }
  141. menu.ShowAsContext();
  142. }
  143. protected override void PopulateContextMenu( GenericMenu menu )
  144. {
  145. base.PopulateContextMenu( menu );
  146. if( Selection.activeTransform != null && Selection.activeTransform != Timeline.Owner )
  147. menu.AddItem( new GUIContent("Change Owner to " + Selection.activeTransform.name), false, ChangeOwner, Selection.activeTransform );
  148. else
  149. menu.AddDisabledItem( new GUIContent( "Change Owner") );
  150. if( CanPaste( FSequenceEditor.CopyObject ) )
  151. {
  152. menu.AddItem( new GUIContent("Paste " + FSequenceEditor.CopyObject.name), false, Paste, FSequenceEditor.CopyObject );
  153. }
  154. menu.AddSeparator(null);
  155. }
  156. void AddTrackMenu( object param )
  157. {
  158. KeyValuePair<Type, FEventAttribute> kvp = (KeyValuePair<Type, FEventAttribute>)param;
  159. Undo.RecordObjects( new UnityEngine.Object[]{Timeline, this}, "add Track" );
  160. FTrack track = (FTrack)typeof(FTimeline).GetMethod("Add", new Type[]{typeof(FrameRange)}).MakeGenericMethod( kvp.Key ).Invoke( Timeline, new object[]{SequenceEditor.ViewRange} );
  161. string evtName = track.gameObject.name;
  162. int nameStart = 0;
  163. int nameEnd = evtName.Length;
  164. if( nameEnd > 2 && evtName[0] == 'F' && char.IsUpper(evtName[1]) )
  165. nameStart = 1;
  166. if( evtName.EndsWith("Event") )
  167. nameEnd = evtName.Length - "Event".Length;
  168. evtName = evtName.Substring( nameStart, nameEnd - nameStart );
  169. track.gameObject.name = ObjectNames.NicifyVariableName( evtName );
  170. if( !Timeline.Sequence.IsStopped )
  171. track.Init();
  172. SequenceEditor.Refresh();
  173. Undo.RegisterCreatedObjectUndo( track.gameObject, string.Empty );
  174. SequenceEditor.SelectExclusive( SequenceEditor.GetEditor<FEventEditor>( track.GetEvent(0) ) );
  175. }
  176. void ChangeOwner( object newOwnerTransform )
  177. {
  178. Transform newOwner = (Transform)newOwnerTransform;
  179. Undo.RecordObject( Timeline, "Change Timeline Owner" );
  180. Timeline.SetOwner( newOwner );
  181. if( !SequenceEditor.Sequence.IsStopped )
  182. Timeline.Init();
  183. }
  184. void DuplicateTimeline()
  185. {
  186. UnityEngine.Object[] objsToSave = new UnityEngine.Object[]{ SequenceEditor, Timeline.Container };
  187. Undo.RecordObjects( objsToSave, string.Empty );
  188. GameObject duplicateTimeline = (GameObject)Instantiate( Timeline.gameObject );
  189. duplicateTimeline.name = Timeline.gameObject.name;
  190. Undo.SetTransformParent( duplicateTimeline.transform, Timeline.Container.transform, string.Empty );
  191. Undo.RegisterCreatedObjectUndo( duplicateTimeline, "duplicate Timeline" );
  192. if( !SequenceEditor.Sequence.IsStopped )
  193. duplicateTimeline.GetComponent<FTimeline>().Init();
  194. }
  195. void DeleteTimeline()
  196. {
  197. UnityEngine.Object[] objsToSave = new UnityEngine.Object[]{ Timeline.Container, Timeline };
  198. Undo.RegisterCompleteObjectUndo( objsToSave, string.Empty );
  199. OnDelete();
  200. Undo.SetTransformParent( Timeline.transform, null, string.Empty );
  201. Timeline.Container.Remove( Timeline );
  202. Undo.DestroyObjectImmediate( Timeline.gameObject );
  203. }
  204. public override FSequenceEditor SequenceEditor { get { return ContainerEditor != null ? ContainerEditor.SequenceEditor : null; } }
  205. public void UpdateTracks( int frame, float time )
  206. {
  207. for( int i = 0; i != Editors.Count; ++i )
  208. {
  209. if( !Editors[i].Track.enabled ) continue;
  210. Editors[i].UpdateEventsEditor( frame, time );
  211. }
  212. }
  213. }
  214. }