FContainerEditor.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using Flux;
  6. namespace FluxEditor
  7. {
  8. [Serializable]
  9. public class FContainerEditor : FEditorList<FTimelineEditor> {
  10. public const int CONTAINER_HEIGHT = 25;
  11. public FContainer Container { get { return (FContainer)Obj; } }
  12. private bool _isDragSelecting = false;
  13. private Vector2 _dragSelectingStartPos = Vector2.zero;
  14. private float _timelineHeaderWidth = 0;
  15. private float _pixelsPerFrame = 0;
  16. public void OnStop()
  17. {
  18. for( int i = 0; i != Editors.Count; ++i )
  19. Editors[i].OnStop();
  20. }
  21. public void UpdateTimelines( int frame, float time )
  22. {
  23. for( int i = 0; i != Editors.Count; ++i )
  24. {
  25. if( !Editors[i].Timeline.enabled )
  26. continue;
  27. Editors[i].UpdateTracks( frame, time );
  28. }
  29. }
  30. public override void Init( FObject obj, FEditor owner )
  31. {
  32. base.Init( obj, owner );
  33. Editors.Clear();
  34. List<FTimeline> timelines = Container.Timelines;
  35. for( int i = 0; i < timelines.Count; ++i )
  36. {
  37. FTimeline timeline = timelines[i];
  38. FTimelineEditor timelineEditor = SequenceEditor.GetEditor<FTimelineEditor>(timeline);
  39. timelineEditor.Init( timeline, this );
  40. Editors.Add( timelineEditor );
  41. }
  42. _icon = new GUIContent( FUtility.GetFluxTexture("Folder.png") );
  43. }
  44. public override FSequenceEditor SequenceEditor { get { return (FSequenceEditor)Owner; } }
  45. public override float HeaderHeight { get { return CONTAINER_HEIGHT; } }
  46. protected override string HeaderText { get { return Obj.name; } }
  47. protected override Color BackgroundColor { get { return Container.Color; } }
  48. protected override bool CanPaste( FObject obj )
  49. {
  50. // since Unity Objs can be "fake null"
  51. return obj != null && obj is FTimeline;
  52. }
  53. protected override void Paste( object obj )
  54. {
  55. if( !CanPaste(obj as FObject) )
  56. return;
  57. Undo.RecordObject( Container, string.Empty );
  58. FTimeline timeline = Instantiate<FTimeline>((FTimeline)obj);
  59. timeline.hideFlags = Container.hideFlags;
  60. Container.Add( timeline );
  61. Undo.RegisterCreatedObjectUndo( timeline.gameObject, "Paste Timeline " + ((FTimeline)obj).name );
  62. }
  63. protected override void Delete()
  64. {
  65. SequenceEditor.DestroyEditor( this );
  66. }
  67. public bool HasTimeline( FTimelineEditor timelineEditor )
  68. {
  69. foreach( FTimelineEditor t in Editors )
  70. {
  71. if( t == timelineEditor )
  72. return true;
  73. }
  74. return false;
  75. }
  76. private void StartDragSelecting( Vector2 mousePos )
  77. {
  78. _isDragSelecting = true;
  79. _dragSelectingStartPos = mousePos;
  80. }
  81. private void StopDragSelecting( Vector2 mousePos )
  82. {
  83. if( !_isDragSelecting )
  84. return;
  85. _isDragSelecting = false;
  86. FrameRange selectedRange = new FrameRange();
  87. bool isSelectingTimelines;
  88. Rect selectionRect = GetDragSelectionRect( _dragSelectingStartPos, mousePos, out selectedRange, out isSelectingTimelines );
  89. if( !Event.current.shift && !Event.current.control )
  90. SequenceEditor.DeselectAll();
  91. for( int i = 0; i != Editors.Count; ++i )
  92. {
  93. Rect timelineRect = Editors[i].Rect;
  94. if( timelineRect.yMin >= selectionRect.yMax )
  95. break;
  96. if( timelineRect.yMax <= selectionRect.yMin )
  97. continue;
  98. for( int j = 0; j != Editors[i].Editors.Count; ++j )
  99. {
  100. Rect trackRect = Editors[i].Editors[j].Rect;
  101. if( trackRect.yMin >= selectionRect.yMax )
  102. break;
  103. if( trackRect.yMax <= selectionRect.yMin )
  104. continue;
  105. if( Event.current.control )
  106. {
  107. Editors[i].Editors[j].DeselectEvents( selectedRange );
  108. }
  109. else
  110. {
  111. Editors[i].Editors[j].SelectEvents( selectedRange );
  112. }
  113. }
  114. }
  115. }
  116. private void OnDragSelecting( Vector2 mousePos )
  117. {
  118. if( !_isDragSelecting )
  119. return;
  120. if( Event.current.shift )
  121. {
  122. EditorGUIUtility.AddCursorRect( Rect, MouseCursor.ArrowPlus );
  123. }
  124. else if( Event.current.control )
  125. {
  126. EditorGUIUtility.AddCursorRect( Rect, MouseCursor.ArrowMinus );
  127. }
  128. FrameRange selectedRange;
  129. bool isSelectingTimelines;
  130. Rect selectionRect = GetDragSelectionRect( _dragSelectingStartPos, mousePos, out selectedRange, out isSelectingTimelines );
  131. if( selectionRect.width == 0 )
  132. selectionRect.width = 1;
  133. GUI.color = FGUI.GetSelectionColor();
  134. GUI.DrawTexture( selectionRect, EditorGUIUtility.whiteTexture );
  135. GUI.color = Color.white;
  136. }
  137. private Rect GetDragSelectionRect( Vector2 rawStartPos, Vector2 rawEndPos, out FrameRange selectedRange, out bool isSelectingTimelines )
  138. {
  139. int startFrame = GetFrameForX( rawStartPos.x );
  140. int endFrame = GetFrameForX( rawEndPos.x );
  141. if( startFrame > endFrame )
  142. {
  143. int temp = startFrame;
  144. startFrame = endFrame;
  145. endFrame = temp;
  146. }
  147. selectedRange = new FrameRange(startFrame, endFrame);
  148. Rect rect = new Rect();
  149. Vector2 startPos = new Vector2( GetXForFrame( startFrame ), rawStartPos.y );
  150. Vector2 endPos = new Vector2( GetXForFrame( endFrame ), rawEndPos.y );
  151. bool isStartOnHeader;
  152. bool isEndOnHeader;
  153. FTimelineEditor startTimeline = GetTimelineEditor( startPos, out isStartOnHeader );
  154. isSelectingTimelines = isStartOnHeader;
  155. if( startTimeline != null )
  156. {
  157. FTrackEditor startTrack = startTimeline.GetTrackEditor( startPos );
  158. FTrackEditor endTrack;
  159. FTimelineEditor endTimeline = GetTimelineEditor( endPos, out isEndOnHeader );
  160. if( endTimeline == null )
  161. {
  162. endTimeline = startTimeline;
  163. isEndOnHeader = isStartOnHeader;
  164. endTrack = startTrack;
  165. }
  166. else
  167. endTrack = endTimeline.GetTrackEditor( endPos );
  168. float xStart = Mathf.Min( startPos.x, endPos.x );
  169. float width = Mathf.Abs( startPos.x - endPos.x );
  170. float yStart;
  171. float height;
  172. if( startPos.y <= endPos.y )
  173. {
  174. yStart = isStartOnHeader ? startTimeline.Rect.yMin : startTrack.Rect.yMin;
  175. height = (isStartOnHeader ? endTimeline.Rect.yMax : (isEndOnHeader ? endTimeline.Rect.yMin + FTimelineEditor.HEADER_HEIGHT : endTrack.Rect.yMax)) - yStart;
  176. }
  177. else
  178. {
  179. yStart = isStartOnHeader || isEndOnHeader ? endTimeline.Rect.yMin : endTrack.Rect.yMin;
  180. height = (isStartOnHeader ? startTimeline.Rect.yMax : startTrack.Rect.yMax) - yStart;
  181. }
  182. rect.x = xStart;
  183. rect.y = yStart;
  184. rect.width = width;
  185. rect.height = height;
  186. }
  187. return rect;
  188. }
  189. public float GetXForFrame( int frame )
  190. {
  191. return _timelineHeaderWidth + frame * _pixelsPerFrame;
  192. }
  193. public int GetFrameForX( float x )
  194. {
  195. return Mathf.RoundToInt( ((x - _timelineHeaderWidth) / _pixelsPerFrame) );
  196. }
  197. private FTimelineEditor GetTimelineEditor( Vector2 pos, out bool isOnHeader )
  198. {
  199. for( int i = 0; i != Editors.Count; ++i )
  200. {
  201. if( Editors[i].Rect.Contains( pos ) )
  202. {
  203. isOnHeader = Editors[i].Rect.yMin + FTimelineEditor.HEADER_HEIGHT > pos.y;
  204. return Editors[i];
  205. }
  206. }
  207. isOnHeader = false;
  208. return null;
  209. }
  210. }
  211. }