TimelineWindow_Duration.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Timeline;
  4. namespace UnityEditor.Timeline
  5. {
  6. partial class TimelineWindow
  7. {
  8. TimeAreaItem m_TimelineDuration;
  9. void DurationGUI(TimelineItemArea area, double duration)
  10. {
  11. // don't show the duration if the time area is not visible for some other reason.
  12. if (!currentMode.ShouldShowTimeArea(state))
  13. return;
  14. bool headerMode = area == TimelineItemArea.Header;
  15. if (state.IsEditingASubTimeline())
  16. {
  17. if (headerMode)
  18. HighlightTimeAreaRange(state.editSequence.GetEvaluableRange(), DirectorStyles.Instance.customSkin.colorSubSequenceDurationLine);
  19. return;
  20. }
  21. // don't show the duration if there's none.
  22. if (state.editSequence.asset.durationMode == TimelineAsset.DurationMode.BasedOnClips && duration <= 0.0f)
  23. return;
  24. if (m_TimelineDuration == null || m_TimelineDuration.style != styles.endmarker)
  25. {
  26. m_TimelineDuration = new TimeAreaItem(styles.endmarker, OnTrackDurationDrag)
  27. {
  28. tooltip = "End of sequence marker",
  29. boundOffset = new Vector2(0.0f, -DirectorStyles.kDurationGuiThickness)
  30. };
  31. }
  32. DrawDuration(headerMode, !headerMode, duration);
  33. }
  34. void DrawDuration(bool drawhead, bool drawline, double duration)
  35. {
  36. if (state.TimeIsInRange((float)duration))
  37. {
  38. // Set the colors based on the mode
  39. Color lineColor = DirectorStyles.Instance.customSkin.colorEndmarker;
  40. Color headColor = Color.white;
  41. bool canMoveHead = !EditorApplication.isPlaying && state.editSequence.asset.durationMode == TimelineAsset.DurationMode.FixedLength;
  42. if (canMoveHead)
  43. {
  44. if (Event.current.type == EventType.MouseDown)
  45. {
  46. if (m_TimelineDuration.bounds.Contains(Event.current.mousePosition))
  47. {
  48. if (m_PlayHead != null && m_PlayHead.bounds.Contains(Event.current.mousePosition))
  49. {
  50. // ignore duration markers if the mouse is over the TimeCursor.
  51. canMoveHead = false;
  52. }
  53. }
  54. }
  55. }
  56. else
  57. {
  58. lineColor.a *= 0.66f;
  59. headColor = DirectorStyles.Instance.customSkin.colorDuration;
  60. }
  61. if (canMoveHead)
  62. m_TimelineDuration.HandleManipulatorsEvents(state);
  63. m_TimelineDuration.lineColor = lineColor;
  64. m_TimelineDuration.headColor = headColor;
  65. m_TimelineDuration.drawHead = drawhead;
  66. m_TimelineDuration.drawLine = drawline;
  67. m_TimelineDuration.canMoveHead = canMoveHead;
  68. // Draw the TimeAreaItem
  69. // Rect trackheadRect = treeviewBounds;
  70. //trackheadRect.height = clientArea.height;
  71. m_TimelineDuration.Draw(sequenceRect, state, duration);
  72. }
  73. // Draw Blue line in timeline indicating the duration...
  74. if (state.editSequence.asset != null && drawhead)
  75. {
  76. HighlightTimeAreaRange(state.editSequence.GetEvaluableRange(), DirectorStyles.Instance.customSkin.colorDurationLine);
  77. }
  78. }
  79. void HighlightTimeAreaRange(Range range, Color lineColor)
  80. {
  81. if (range.length <= 0.0 || !state.RangeIsVisible(range)) return;
  82. Rect lineRect = Rect.MinMaxRect(
  83. Math.Max(state.TimeToPixel(range.start), state.timeAreaRect.xMin),
  84. state.timeAreaRect.y - DirectorStyles.kDurationGuiThickness + state.timeAreaRect.height,
  85. Math.Min(state.TimeToPixel(range.end), state.timeAreaRect.xMax),
  86. state.timeAreaRect.y + state.timeAreaRect.height);
  87. EditorGUI.DrawRect(lineRect, lineColor);
  88. }
  89. // Drag handler for the gui
  90. void OnTrackDurationDrag(double newTime)
  91. {
  92. if (state.editSequence.asset.durationMode == TimelineAsset.DurationMode.FixedLength && !state.editSequence.isReadOnly)
  93. {
  94. // this is the first call to the drag
  95. if (m_TimelineDuration.firstDrag)
  96. {
  97. UndoExtensions.RegisterTimeline(state.editSequence.asset, "Change Duration");
  98. }
  99. state.editSequence.asset.fixedDuration = newTime;
  100. // when setting a new length, modify the duration of the timeline playable directly instead of
  101. // rebuilding the whole graph
  102. state.UpdateRootPlayableDuration(newTime);
  103. }
  104. m_TimelineDuration.showTooltip = true;
  105. }
  106. }
  107. }