EaseClip.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Text;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. class EaseClip : Manipulator
  8. {
  9. bool m_IsCaptured;
  10. bool m_UndoSaved;
  11. TimelineClipHandle m_EaseClipHandler;
  12. ManipulateEdges m_Edges;
  13. TimelineClip m_Clip;
  14. StringBuilder m_OverlayText = new StringBuilder("");
  15. double m_OriginalValue;
  16. public static readonly string EaseInClipText = L10n.Tr("Ease In Clip");
  17. public static readonly string EaseOutClipText = L10n.Tr("Ease Out Clip");
  18. public static readonly string EaseInText = L10n.Tr("Ease In");
  19. public static readonly string EaseOutText = L10n.Tr("Ease Out");
  20. public static readonly string DurationFrameText = L10n.Tr(" Duration {0:0.00;-0.00} frames ");
  21. public static readonly string DurationSecText = L10n.Tr(" Duration {0:0.00;-0.00} s ");
  22. public static readonly string DeltaFrameText = L10n.Tr("({0:+0.00;-0.00} frames)");
  23. public static readonly string DeltaSecText = L10n.Tr("({0:+0.00;-0.00} s)");
  24. protected override bool MouseDown(Event evt, WindowState state)
  25. {
  26. if (evt.modifiers != ManipulatorsUtils.actionModifier)
  27. return false;
  28. return MouseDownInternal(evt, state, PickerUtils.PickedLayerableOfType<TimelineClipHandle>());
  29. }
  30. protected bool MouseDownInternal(Event evt, WindowState state, TimelineClipHandle handle)
  31. {
  32. if (handle == null)
  33. return false;
  34. if (handle.clipGUI.clip != null && !handle.clipGUI.clip.clipCaps.HasAny(ClipCaps.Blending))
  35. return false;
  36. m_Edges = ManipulateEdges.Right;
  37. if (handle.trimDirection == TrimEdge.Start)
  38. m_Edges = ManipulateEdges.Left;
  39. if (m_Edges == ManipulateEdges.Left && handle.clipGUI.clip.hasBlendIn || m_Edges == ManipulateEdges.Right && handle.clipGUI.clip.hasBlendOut)
  40. return false;
  41. m_IsCaptured = true;
  42. m_UndoSaved = false;
  43. m_EaseClipHandler = handle;
  44. m_Clip = handle.clipGUI.clip;
  45. m_OriginalValue = m_Edges == ManipulateEdges.Left ? m_Clip.easeInDuration : m_Clip.easeOutDuration;
  46. // Change cursor only when OnGUI Process (not in test)
  47. if (GUIUtility.guiDepth > 0)
  48. TimelineCursors.SetCursor(m_Edges == ManipulateEdges.Left ? TimelineCursors.CursorType.MixRight : TimelineCursors.CursorType.MixLeft);
  49. state.AddCaptured(this);
  50. return true;
  51. }
  52. protected override bool MouseUp(Event evt, WindowState state)
  53. {
  54. if (!m_IsCaptured)
  55. return false;
  56. m_IsCaptured = false;
  57. m_UndoSaved = false;
  58. state.captured.Clear();
  59. // Clear cursor only when OnGUI Process (not in test)
  60. if (GUIUtility.guiDepth > 0)
  61. TimelineCursors.ClearCursor();
  62. return true;
  63. }
  64. protected override bool MouseDrag(Event evt, WindowState state)
  65. {
  66. if (!m_IsCaptured)
  67. return false;
  68. if (!m_UndoSaved)
  69. {
  70. var uiClip = m_EaseClipHandler.clipGUI;
  71. string undoName = m_Edges == ManipulateEdges.Left ? EaseInClipText : EaseOutClipText;
  72. TimelineUndo.PushUndo(uiClip.clip.parentTrack, undoName);
  73. m_UndoSaved = true;
  74. }
  75. double d = state.PixelDeltaToDeltaTime(evt.delta.x);
  76. if (m_Edges == ManipulateEdges.Left)
  77. {
  78. m_Clip.easeInDuration = Math.Max(0, m_Clip.easeInDuration + d);
  79. }
  80. else if (m_Edges == ManipulateEdges.Right)
  81. {
  82. m_Clip.easeOutDuration = Math.Max(0, m_Clip.easeOutDuration - d);
  83. }
  84. RefreshOverlayStrings(m_EaseClipHandler, state);
  85. return true;
  86. }
  87. public override void Overlay(Event evt, WindowState state)
  88. {
  89. if (!m_IsCaptured)
  90. return;
  91. if (m_OverlayText.Length > 0)
  92. {
  93. int stringLength = m_OverlayText.Length;
  94. var r = new Rect(evt.mousePosition.x - (stringLength / 2.0f),
  95. m_EaseClipHandler.clipGUI.rect.yMax,
  96. stringLength, 20);
  97. GUI.Label(r, m_OverlayText.ToString(), TimelineWindow.styles.tinyFont);
  98. }
  99. }
  100. void RefreshOverlayStrings(TimelineClipHandle handle, WindowState state)
  101. {
  102. m_OverlayText.Length = 0;
  103. m_OverlayText.Append(m_Edges == ManipulateEdges.Left ? EaseInText : EaseOutText);
  104. double easeDuration = m_Edges == ManipulateEdges.Left ? m_Clip.easeInDuration : m_Clip.easeOutDuration;
  105. double deltaDuration = easeDuration - m_OriginalValue;
  106. bool hasDurationDelta = Math.Abs(deltaDuration) > double.Epsilon;
  107. if (state.timeInFrames)
  108. {
  109. easeDuration *= state.editSequence.frameRate;
  110. deltaDuration *= state.editSequence.frameRate;
  111. m_OverlayText.AppendFormat(DurationFrameText, easeDuration);
  112. if (hasDurationDelta)
  113. {
  114. m_OverlayText.AppendFormat(DeltaFrameText, deltaDuration);
  115. }
  116. }
  117. else
  118. {
  119. m_OverlayText.AppendFormat(DurationSecText, easeDuration);
  120. if (hasDurationDelta)
  121. {
  122. m_OverlayText.AppendFormat(DeltaSecText, deltaDuration);
  123. }
  124. }
  125. }
  126. }
  127. }