TimelineWindow_PlayRange.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Linq;
  2. using UnityEngine;
  3. namespace UnityEditor.Timeline
  4. {
  5. partial class TimelineWindow
  6. {
  7. TimeAreaItem m_PlayRangeEnd;
  8. TimeAreaItem m_PlayRangeStart;
  9. void PlayRangeGUI(TimelineItemArea area)
  10. {
  11. if (!currentMode.ShouldShowPlayRange(state) || treeView == null)
  12. return;
  13. if (state.masterSequence.asset != null && !state.masterSequence.asset.GetRootTracks().Any())
  14. return;
  15. // left Time Cursor
  16. if (m_PlayRangeStart == null || m_PlayRangeStart.style != styles.playTimeRangeStart)
  17. {
  18. m_PlayRangeStart = new TimeAreaItem(styles.playTimeRangeStart, OnTrackHeadMinSelectDrag);
  19. Vector2 offset = new Vector2(-2.0f, 0);
  20. m_PlayRangeStart.boundOffset = offset;
  21. }
  22. // right Time Cursor
  23. if (m_PlayRangeEnd == null || m_PlayRangeEnd.style != styles.playTimeRangeEnd)
  24. {
  25. m_PlayRangeEnd = new TimeAreaItem(styles.playTimeRangeEnd, OnTrackHeadMaxSelectDrag);
  26. Vector2 offset = new Vector2(2.0f, 0);
  27. m_PlayRangeEnd.boundOffset = offset;
  28. }
  29. if (area == TimelineItemArea.Header)
  30. DrawPlayRange(true, false);
  31. else if (area == TimelineItemArea.Lines)
  32. DrawPlayRange(false, true);
  33. }
  34. void DrawPlayRange(bool drawHeads, bool drawLines)
  35. {
  36. Rect timeCursorRect = state.timeAreaRect;
  37. timeCursorRect.height = clientArea.height;
  38. m_PlayRangeEnd.HandleManipulatorsEvents(state);
  39. m_PlayRangeStart.HandleManipulatorsEvents(state);
  40. // The first time a user enable the play range, we put the play range 75% around the current time...
  41. if (state.playRange == TimelineAssetViewModel.NoPlayRangeSet)
  42. {
  43. float minimumPlayRangeTime = 0.01f;
  44. float t0 = Mathf.Max(0.0f, state.PixelToTime(state.timeAreaRect.xMin));
  45. float t1 = Mathf.Min((float)state.masterSequence.duration, state.PixelToTime(state.timeAreaRect.xMax));
  46. if (Mathf.Abs(t1 - t0) <= minimumPlayRangeTime)
  47. {
  48. state.playRange = new Vector2(t0, t1);
  49. return;
  50. }
  51. float deltaT = (t1 - t0) * 0.25f / 2.0f;
  52. t0 += deltaT;
  53. t1 -= deltaT;
  54. if (t1 < t0)
  55. {
  56. float temp = t0;
  57. t0 = t1;
  58. t1 = temp;
  59. }
  60. if (Mathf.Abs(t1 - t0) < minimumPlayRangeTime)
  61. {
  62. if (t0 - minimumPlayRangeTime > 0.0f)
  63. t0 -= minimumPlayRangeTime;
  64. else if (t1 + minimumPlayRangeTime < state.masterSequence.duration)
  65. t1 += minimumPlayRangeTime;
  66. }
  67. state.playRange = new Vector2(t0, t1);
  68. }
  69. // Draw the head or the lines according to the parameters..
  70. m_PlayRangeStart.drawHead = drawHeads;
  71. m_PlayRangeStart.drawLine = drawLines;
  72. m_PlayRangeEnd.drawHead = drawHeads;
  73. m_PlayRangeEnd.drawLine = drawLines;
  74. var playRangeTime = state.playRange;
  75. m_PlayRangeStart.Draw(sequenceContentRect, state, playRangeTime.x);
  76. m_PlayRangeEnd.Draw(sequenceContentRect, state, playRangeTime.y);
  77. // Draw Time Range Box from Start to End...
  78. if (state.playRangeEnabled && m_PlayHead != null)
  79. {
  80. Rect rect =
  81. Rect.MinMaxRect(
  82. Mathf.Clamp(state.TimeToPixel(playRangeTime.x), state.timeAreaRect.xMin, state.timeAreaRect.xMax),
  83. m_PlayHead.bounds.yMax,
  84. Mathf.Clamp(state.TimeToPixel(playRangeTime.y), state.timeAreaRect.xMin, state.timeAreaRect.xMax),
  85. sequenceContentRect.height + state.timeAreaRect.height + timeCursorRect.y
  86. );
  87. EditorGUI.DrawRect(rect, DirectorStyles.Instance.customSkin.colorRange);
  88. rect.height = 3f;
  89. EditorGUI.DrawRect(rect, Color.white);
  90. }
  91. }
  92. void OnTrackHeadMinSelectDrag(double newTime)
  93. {
  94. Vector2 range = state.playRange;
  95. range.x = (float)newTime;
  96. state.playRange = range;
  97. m_PlayRangeStart.showTooltip = true;
  98. }
  99. void OnTrackHeadMaxSelectDrag(double newTime)
  100. {
  101. Vector2 range = state.playRange;
  102. range.y = (float)newTime;
  103. state.playRange = range;
  104. m_PlayRangeEnd.showTooltip = true;
  105. }
  106. }
  107. }