FSequenceWindowToolbar.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using UnityEngine;
  2. using UnityEditor;
  3. using Flux;
  4. namespace FluxEditor
  5. {
  6. public class FSequenceWindowToolbar
  7. {
  8. public const int ROW_HEIGHT = 20;
  9. public const int NUM_ROWS = 1;
  10. public const int SPACE = 5;
  11. public const int HEIGHT = ROW_HEIGHT * NUM_ROWS + SPACE*(NUM_ROWS+1);
  12. public const int BUTTON_WIDTH = 25;
  13. public const int STOP_BUTTON_WIDTH = 50;
  14. public const int PLAY_BUTTON_WIDTH = 50;
  15. public const int SPEED_LABEL_WIDTH = 40;
  16. public const int SPEED_SLIDER_WIDTH = 80;
  17. public const int FRAME_FIELD_WIDTH = 100;
  18. private FSequenceEditorWindow _window = null;
  19. private Rect _firstFrameButtonRect;
  20. private Rect _previousFrameButtonRect;
  21. private Rect _currentFrameFieldRect;
  22. private Rect _nextFrameButtonRect;
  23. private Rect _lastFrameButtonRect;
  24. // private Rect _playBackwardButtonRect;
  25. private Rect _stopButtonRect;
  26. private Rect _playForwardButtonRect;
  27. private Rect _speedLabelRect;
  28. private Rect _speedSliderRect;
  29. private Rect _speedValueRect;
  30. private GUIContent _firstFrame = null;
  31. private GUIContent _previousFrame = null;
  32. private GUIContent _nextFrame = null;
  33. private GUIContent _lastFrame = null;
  34. private GUIContent _playBackward = null;
  35. private GUIContent _playForward = null;
  36. private GUIContent _pause = null;
  37. private GUIContent _stop = null;
  38. private bool _showSpeedSlider;
  39. private GUIContent _speedLabel = new GUIContent("Speed", "Playback Speed, only for preview purposes");
  40. private float[] _speedValues = new float[]{ 0.1f, 0.25f, 0.5f, 0.75f, 1f, 1.25f, 1.5f, 1.75f, 2f };
  41. // private GUIContent[] _speedValueStrs = new GUIContent[]{ new GUIContent("Custom"), new GUIContent( "1/10x" ), new GUIContent( "1/4x" ), new GUIContent( "1/2x" ),
  42. // new GUIContent( "3/4x" ), new GUIContent( "1x" ), new GUIContent( "1.25x" ), new GUIContent( "1.5x" ), new GUIContent( "1.75x" ), new GUIContent( "2x" ) };
  43. // private int _speedIndex = 5;
  44. private bool _showViewRange;
  45. private GUIContent _viewRangeLabel = null;
  46. private GUIContent _viewRangeDash = null;
  47. private Rect _viewRangeLabelRect;
  48. private Rect _viewRangeDashRect;
  49. private Rect _viewRangeStartRect;
  50. private Rect _viewRangeEndRect;
  51. public FSequenceWindowToolbar( FSequenceEditorWindow window )
  52. {
  53. _window = window;
  54. _firstFrame = new GUIContent( FUtility.GetFluxTexture( "FirstFrame.png" ), "First Frame" );
  55. _previousFrame = new GUIContent( FUtility.GetFluxTexture( "PreviousFrame.png" ), "Previous Frame" );
  56. _nextFrame = new GUIContent( FUtility.GetFluxTexture( "NextFrame.png" ), "Next Frame" );
  57. _lastFrame = new GUIContent( FUtility.GetFluxTexture( "LastFrame.png" ), "Last Frame" );
  58. _playBackward = new GUIContent( FUtility.GetFluxTexture( "PlayBackward.png" ), "Play Backward" );
  59. _playForward = new GUIContent( FUtility.GetFluxTexture( "Play.png" ), "Left Click: Play Forward\nRight Click: Play Backwards" );
  60. _pause = new GUIContent( FUtility.GetFluxTexture( "Pause.png" ), "Pause" );
  61. _stop = new GUIContent( FUtility.GetFluxTexture( "Stop.png" ), "Stop" );
  62. _viewRangeLabel = new GUIContent( "View Range" );
  63. _viewRangeDash = new GUIContent( " - " );
  64. }
  65. public void RebuildLayout( Rect rect )
  66. {
  67. rect.xMin += SPACE;
  68. rect.yMin += SPACE;
  69. rect.xMax -= SPACE;
  70. rect.yMax -= SPACE;
  71. _firstFrameButtonRect = rect;
  72. _firstFrameButtonRect.width = BUTTON_WIDTH;
  73. _previousFrameButtonRect = rect;
  74. _previousFrameButtonRect.xMin = _firstFrameButtonRect.xMax;
  75. _previousFrameButtonRect.width = BUTTON_WIDTH;
  76. _currentFrameFieldRect = rect;
  77. _currentFrameFieldRect.xMin = _previousFrameButtonRect.xMax;
  78. _currentFrameFieldRect.width = FRAME_FIELD_WIDTH;
  79. _nextFrameButtonRect = rect;
  80. _nextFrameButtonRect.xMin = _currentFrameFieldRect.xMax;
  81. _nextFrameButtonRect.width = BUTTON_WIDTH;
  82. _lastFrameButtonRect = rect;
  83. _lastFrameButtonRect.xMin = _nextFrameButtonRect.xMax;
  84. _lastFrameButtonRect.width = BUTTON_WIDTH;
  85. // _playBackwardButtonRect = rect;
  86. // _playBackwardButtonRect.xMin = _lastFrameButtonRect.xMax + SPACE;
  87. // _playBackwardButtonRect.width = PLAY_BUTTON_WIDTH;
  88. //
  89. // _playForwardButtonRect = rect;
  90. // _playForwardButtonRect.xMin = _playBackwardButtonRect.xMax;
  91. // _playForwardButtonRect.width = PLAY_BUTTON_WIDTH;
  92. _stopButtonRect = rect;
  93. _stopButtonRect.xMin = _lastFrameButtonRect.xMax + SPACE;
  94. _stopButtonRect.width = STOP_BUTTON_WIDTH;
  95. _playForwardButtonRect = rect;
  96. _playForwardButtonRect.xMin = _stopButtonRect.xMax;
  97. _playForwardButtonRect.width = PLAY_BUTTON_WIDTH;
  98. float reminderWidth = rect.width - _playForwardButtonRect.xMax;// + SPACE;
  99. // float reminderWidth = rect.width - _speedValueRect.xMax/*_playForwardButtonRect.xMax*/ + SPACE;
  100. _viewRangeLabelRect = rect;
  101. _viewRangeLabelRect.width = EditorStyles.label.CalcSize( _viewRangeLabel ).x + SPACE;
  102. _viewRangeDashRect = rect;
  103. _viewRangeDashRect.width = EditorStyles.label.CalcSize( _viewRangeDash ).x;
  104. _viewRangeStartRect = rect;
  105. _viewRangeStartRect.width = FRAME_FIELD_WIDTH;
  106. _viewRangeEndRect = rect;
  107. _viewRangeEndRect.width = FRAME_FIELD_WIDTH;
  108. _viewRangeEndRect.x = rect.xMax - _viewRangeEndRect.width;
  109. _viewRangeDashRect.x = _viewRangeEndRect.xMin - _viewRangeDashRect.width;
  110. _viewRangeStartRect.x = _viewRangeDashRect.xMin - _viewRangeStartRect.width;
  111. _viewRangeLabelRect.x = _viewRangeStartRect.xMin - _viewRangeLabelRect.width;
  112. reminderWidth -= _viewRangeLabelRect.width + _viewRangeStartRect.width + _viewRangeDashRect.width + _viewRangeEndRect.width;
  113. _showViewRange = reminderWidth >= 0;
  114. if( _showViewRange )
  115. {
  116. _speedLabelRect = rect;
  117. _speedLabelRect.xMin = _playForwardButtonRect.xMax + SPACE;
  118. _speedLabelRect.width = SPEED_LABEL_WIDTH;
  119. _speedSliderRect = rect;
  120. _speedSliderRect.xMin = _speedLabelRect.xMax;
  121. _speedSliderRect.width = SPEED_SLIDER_WIDTH;
  122. _speedValueRect = rect;
  123. _speedValueRect.xMin = _speedSliderRect.xMax + SPACE;
  124. _speedValueRect.width = SPEED_LABEL_WIDTH;
  125. reminderWidth -= (_speedValueRect.xMax - _speedLabelRect.xMin);
  126. _showSpeedSlider = reminderWidth >= 0;
  127. if( _showSpeedSlider )
  128. {
  129. reminderWidth *= 0.5f;
  130. _speedLabelRect.x += reminderWidth;
  131. _speedSliderRect.x += reminderWidth;
  132. _speedValueRect.x += reminderWidth;
  133. }
  134. }
  135. else
  136. _showSpeedSlider = false;
  137. }
  138. // private float _speed = 1f;
  139. //
  140. // public float Speed
  141. // {
  142. // set
  143. // {
  144. // _speed = value;
  145. //
  146. // _speedIndex = 0;
  147. //// float distance = float.MaxValue;
  148. // for( int i = 1; i != _speedValues.Length; ++i )
  149. // {
  150. // if( Mathf.Approximately( _speed, _speedValues[i] ) )
  151. // {
  152. // _speedIndex = i;
  153. // break;
  154. // }
  155. //// if( Mathf.Abs( speed - _speedValues[i] ) < distance )
  156. //// {
  157. //// distance = Mathf.Abs( speed - _speedValues[i] );
  158. //// _speedIndex = i;
  159. //// }
  160. // }
  161. //
  162. // if( _speedIndex > 0 )
  163. // _window.GetSequenceEditor().GetSequence().Speed = _speedValues[_speedIndex] * (_window.GetSequenceEditor().IsPlayingForward ? 1f : -1f);
  164. // else
  165. // _speedValueStrs[0].text = _speed.ToString("0.00")+'x';
  166. // }
  167. // }
  168. public void OnGUI()
  169. {
  170. FSequence sequence = _window.GetSequenceEditor().Sequence;
  171. FrameRange viewRange = _window.GetSequenceEditor().ViewRange;
  172. GUI.backgroundColor = Color.white;
  173. GUI.contentColor = FGUI.GetTextColor();
  174. bool goToFirst = false;
  175. bool goToPrevious = false;
  176. bool goToNext = false;
  177. bool goToLast = false;
  178. if( Event.current.type == EventType.KeyDown )
  179. {
  180. if( Event.current.keyCode == KeyCode.Comma )
  181. {
  182. goToFirst = Event.current.shift;
  183. goToPrevious = !goToFirst;
  184. Event.current.Use();
  185. _window.Repaint();
  186. }
  187. if( Event.current.keyCode == KeyCode.Period )
  188. {
  189. goToLast = Event.current.shift;
  190. goToNext = !goToLast;
  191. Event.current.Use();
  192. _window.Repaint();
  193. }
  194. }
  195. if( GUI.Button( _firstFrameButtonRect, _firstFrame, EditorStyles.miniButtonLeft ) || goToFirst )
  196. GoToFrame( viewRange.Start );
  197. if( GUI.Button( _previousFrameButtonRect, _previousFrame, EditorStyles.miniButtonMid) || goToPrevious )
  198. GoToFrame( viewRange.Cull(sequence.CurrentFrame-1) );
  199. GUIStyle numberFieldStyle = new GUIStyle(EditorStyles.numberField);
  200. numberFieldStyle.alignment = TextAnchor.MiddleCenter;
  201. // Debug.Log("hot control: " + EditorGUIUtility.hotControl + " keyboard control: " + EditorGUIUtility.keyboardControl );
  202. if( sequence != null )
  203. {
  204. if( sequence.CurrentFrame < 0 )
  205. {
  206. EditorGUI.BeginChangeCheck();
  207. string frameStr = EditorGUI.TextField( _currentFrameFieldRect, string.Empty, numberFieldStyle );
  208. if( EditorGUI.EndChangeCheck() )
  209. {
  210. int newCurrentFrame = 0;
  211. int.TryParse( frameStr, out newCurrentFrame );
  212. newCurrentFrame = Mathf.Clamp( newCurrentFrame, 0, sequence.Length );
  213. _window.GetSequenceEditor().SetCurrentFrame( newCurrentFrame );
  214. }
  215. }
  216. else
  217. {
  218. EditorGUI.BeginChangeCheck();
  219. int newCurrentFrame = Mathf.Clamp( EditorGUI.IntField( _currentFrameFieldRect, sequence.CurrentFrame, numberFieldStyle ), 0, sequence.Length );
  220. if( EditorGUI.EndChangeCheck() )
  221. _window.GetSequenceEditor().SetCurrentFrame( newCurrentFrame );
  222. }
  223. }
  224. if( GUI.Button( _nextFrameButtonRect, _nextFrame, EditorStyles.miniButtonMid ) || goToNext )
  225. GoToFrame( viewRange.Cull( sequence.CurrentFrame+1 ) );
  226. if( GUI.Button( _lastFrameButtonRect, _lastFrame, EditorStyles.miniButtonRight) || goToLast )
  227. GoToFrame( viewRange.End );
  228. // if( GUI.Button( _playBackwardButtonRect, isPlaying ? _pause : _playBackward, EditorStyles.miniButtonLeft ) )
  229. // {
  230. // if( isPlaying )
  231. // Pause();
  232. // else
  233. // PlayBackwards();
  234. // }
  235. if( GUI.Button( _stopButtonRect, _stop, EditorStyles.miniButtonLeft ) )
  236. Stop();
  237. if( Event.current.type == EventType.MouseUp && _playForwardButtonRect.Contains( Event.current.mousePosition ) )
  238. {
  239. _window.GetSequenceEditor().IsPlayingForward = Event.current.button == 0;
  240. }
  241. bool isPlaying = Application.isPlaying ? _window.GetSequenceEditor().Sequence.IsPlaying : _window.GetSequenceEditor().IsPlaying;
  242. bool isPlayingForward = _window.GetSequenceEditor().IsPlayingForward;
  243. if( GUI.Button( _playForwardButtonRect, isPlaying ? _pause : (isPlayingForward ? _playForward : _playBackward), EditorStyles.miniButtonRight) )
  244. {
  245. if( isPlaying )
  246. Pause();
  247. else if( isPlayingForward )
  248. Play();
  249. else
  250. PlayBackwards();
  251. }
  252. if( _showSpeedSlider )
  253. {
  254. GUI.Label( _speedLabelRect, _speedLabel );
  255. float currentSpeed = Mathf.Abs(_window.GetSequenceEditor().Sequence.Speed);
  256. EditorGUI.BeginChangeCheck();
  257. float speed = GUI.HorizontalSlider( _speedSliderRect, currentSpeed, _speedValues[0], _speedValues[_speedValues.Length-1] );
  258. if( EditorGUI.EndChangeCheck() )
  259. {
  260. int speedIndex = 0;
  261. float distance = float.MaxValue;
  262. for( int i = 0; i != _speedValues.Length; ++i )
  263. {
  264. if( Mathf.Abs( speed - _speedValues[i] ) < distance )
  265. {
  266. distance = Mathf.Abs( speed - _speedValues[i] );
  267. speedIndex = i;
  268. }
  269. }
  270. // Speed = _speedValues[_speedIndex];
  271. _window.GetSequenceEditor().Sequence.Speed = _speedValues[speedIndex] * (isPlayingForward ? 1f : -1f);
  272. }
  273. // if( !Mathf.Approximately(speed, currentSpeed) )
  274. // {
  275. // _speedIndex = 0;
  276. // float distance = float.MaxValue;
  277. // for( int i = 0; i != _speedValues.Length; ++i )
  278. // {
  279. // if( Mathf.Abs( speed - _speedValues[i] ) < distance )
  280. // {
  281. // distance = Mathf.Abs( speed - _speedValues[i] );
  282. // _speedIndex = i;
  283. // }
  284. // }
  285. // _window.GetSequenceEditor().GetSequence().Speed = _speedValues[_speedIndex] * (isPlayingForward ? 1f : -1f);
  286. // }
  287. // GUI.Label( _speedValueRect, _speedValueStrs[_speedIndex] );
  288. EditorGUI.BeginChangeCheck();
  289. speed = Mathf.Abs(EditorGUI.FloatField( _speedValueRect, currentSpeed ));
  290. if( EditorGUI.EndChangeCheck() )
  291. {
  292. _window.GetSequenceEditor().Sequence.Speed = speed * (isPlayingForward ? 1f : -1f);
  293. }
  294. }
  295. if( _showViewRange )
  296. {
  297. EditorGUI.PrefixLabel( _viewRangeLabelRect, _viewRangeLabel );
  298. EditorGUI.BeginChangeCheck();
  299. viewRange.Start = EditorGUI.IntField( _viewRangeStartRect, viewRange.Start, numberFieldStyle );
  300. EditorGUI.PrefixLabel( _viewRangeDashRect, _viewRangeDash );
  301. viewRange.End = EditorGUI.IntField( _viewRangeEndRect, viewRange.End, numberFieldStyle );
  302. if( EditorGUI.EndChangeCheck() )
  303. _window.GetSequenceEditor().SetViewRange( viewRange );
  304. }
  305. }
  306. public void GoToFrame( int frame )
  307. {
  308. _window.GetSequenceEditor().SetCurrentFrame( frame );
  309. if( _window.IsPlaying )
  310. _window.Pause();
  311. FUtility.RepaintGameView();
  312. }
  313. public void Stop()
  314. {
  315. _window.Stop();
  316. }
  317. public void PlayBackwards()
  318. {
  319. _window.PlayBackwards( Event.current.shift );
  320. }
  321. public void Play()
  322. {
  323. _window.Play( Event.current.shift );
  324. }
  325. public void Pause()
  326. {
  327. _window.Pause();
  328. }
  329. // public static FrameRange FrameRangeField( Rect rect, FrameRange range )
  330. // {
  331. // float minWidthNumberField = 100;
  332. //
  333. // GUIContent label = new GUIContent( "View Range" );
  334. // GUIContent dash = new GUIContent( " - " );
  335. //
  336. // Vector2 labelSize = EditorStyles.label.CalcSize( label );
  337. // Vector2 dashSize = EditorStyles.label.CalcSize( dash );
  338. //
  339. // float minSize = minWidthNumberField + minWidthNumberField + dashSize.x;
  340. //
  341. // float extraSize = rect.width - minSize;
  342. //
  343. // labelSize.x = extraSize;
  344. //
  345. // Rect labelRect = rect;
  346. // labelRect.width = labelSize.x;
  347. //
  348. // EditorGUI.PrefixLabel( labelRect, label );
  349. //
  350. // rect.xMin = labelRect.xMax;
  351. //
  352. // Rect startRect = rect;
  353. // startRect.width = rect.width * 0.5f - dashSize.x * 0.5f;
  354. //
  355. // GUIStyle numberStyle = new GUIStyle( EditorStyles.numberField );
  356. // numberStyle.alignment = TextAnchor.MiddleCenter;
  357. //
  358. // EditorGUI.BeginChangeCheck();
  359. // range.start = EditorGUI.IntField( startRect, range.start, numberStyle );
  360. // if( EditorGUI.EndChangeCheck() )
  361. // range.start = Mathf.Min( range.start, range.end );
  362. //
  363. //
  364. // Rect dashRect = startRect;
  365. // dashRect.x = startRect.xMax;
  366. // dashRect.width = dashSize.x;
  367. //
  368. // GUI.Label( dashRect, dash );
  369. //
  370. // Rect endRect = startRect;
  371. // endRect.x = dashRect.xMax;
  372. // EditorGUI.BeginChangeCheck();
  373. // range.end = EditorGUI.IntField( endRect, range.end, numberStyle );
  374. // if( EditorGUI.EndChangeCheck() )
  375. // range.end = Mathf.Max( range.start, range.end );
  376. //
  377. // return range;
  378. // }
  379. }
  380. }