Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EaseClip.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 DurationText = L10n.Tr("Duration: ");
  21. protected override bool MouseDown(Event evt, WindowState state)
  22. {
  23. if (evt.modifiers != ManipulatorsUtils.actionModifier)
  24. return false;
  25. return MouseDownInternal(evt, state, PickerUtils.TopmostPickedItem() as TimelineClipHandle);
  26. }
  27. protected bool MouseDownInternal(Event evt, WindowState state, TimelineClipHandle handle)
  28. {
  29. if (handle == null)
  30. return false;
  31. if (handle.clipGUI.clip != null && !handle.clipGUI.clip.clipCaps.HasAny(ClipCaps.Blending))
  32. return false;
  33. m_Edges = ManipulateEdges.Right;
  34. if (handle.trimDirection == TrimEdge.Start)
  35. m_Edges = ManipulateEdges.Left;
  36. if (m_Edges == ManipulateEdges.Left && handle.clipGUI.clip.hasBlendIn || m_Edges == ManipulateEdges.Right && handle.clipGUI.clip.hasBlendOut)
  37. return false;
  38. m_IsCaptured = true;
  39. m_UndoSaved = false;
  40. m_EaseClipHandler = handle;
  41. m_Clip = handle.clipGUI.clip;
  42. m_OriginalValue = m_Edges == ManipulateEdges.Left ? m_Clip.easeInDuration : m_Clip.easeOutDuration;
  43. // Change cursor only when OnGUI Process (not in test)
  44. if (GUIUtility.guiDepth > 0)
  45. TimelineCursors.SetCursor(m_Edges == ManipulateEdges.Left ? TimelineCursors.CursorType.MixRight : TimelineCursors.CursorType.MixLeft);
  46. state.AddCaptured(this);
  47. return true;
  48. }
  49. protected override bool MouseUp(Event evt, WindowState state)
  50. {
  51. if (!m_IsCaptured)
  52. return false;
  53. m_IsCaptured = false;
  54. m_UndoSaved = false;
  55. state.captured.Clear();
  56. // Clear cursor only when OnGUI Process (not in test)
  57. if (GUIUtility.guiDepth > 0)
  58. TimelineCursors.ClearCursor();
  59. return true;
  60. }
  61. protected override bool MouseDrag(Event evt, WindowState state)
  62. {
  63. if (!m_IsCaptured)
  64. return false;
  65. if (!m_UndoSaved)
  66. {
  67. var uiClip = m_EaseClipHandler.clipGUI;
  68. string undoName = m_Edges == ManipulateEdges.Left ? EaseInClipText : EaseOutClipText;
  69. UndoExtensions.RegisterClip(uiClip.clip, undoName);
  70. m_UndoSaved = true;
  71. }
  72. double d = state.PixelDeltaToDeltaTime(evt.delta.x);
  73. var duration = m_Clip.duration;
  74. var easeInDurationLimit = duration - m_Clip.easeOutDuration;
  75. var easeOutDurationLimit = duration - m_Clip.easeInDuration;
  76. if (m_Edges == ManipulateEdges.Left)
  77. {
  78. m_Clip.easeInDuration = Math.Min(easeInDurationLimit, Math.Max(0, m_Clip.easeInDuration + d));
  79. }
  80. else if (m_Edges == ManipulateEdges.Right)
  81. {
  82. m_Clip.easeOutDuration = Math.Min(easeOutDurationLimit, 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. var easeDuration = m_Edges == ManipulateEdges.Left ? m_Clip.easeInDuration : m_Clip.easeOutDuration;
  105. var deltaDuration = easeDuration - m_OriginalValue;
  106. // round to frame so we don't show partial time codes due to no frame snapping
  107. if (state.timeFormat == TimeFormat.Timecode)
  108. {
  109. easeDuration = TimeUtility.RoundToFrame(easeDuration, state.referenceSequence.frameRate);
  110. deltaDuration = TimeUtility.RoundToFrame(deltaDuration, state.referenceSequence.frameRate);
  111. }
  112. m_OverlayText.Append(DurationText);
  113. m_OverlayText.Append(state.timeFormat.ToTimeStringWithDelta(easeDuration, state.referenceSequence.frameRate, deltaDuration));
  114. }
  115. }
  116. }