No Description
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.

ManipulationsTimeline.cs 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.ShortcutManagement;
  4. using UnityEditor.Timeline.Actions;
  5. using UnityEngine;
  6. namespace UnityEditor.Timeline
  7. {
  8. class TimelinePanManipulator : Manipulator
  9. {
  10. const float k_MaxPanSpeed = 50.0f;
  11. bool m_Active;
  12. protected override bool MouseDown(Event evt, WindowState state)
  13. {
  14. if ((evt.button == 2 && evt.modifiers == EventModifiers.None) ||
  15. (evt.button == 0 && evt.modifiers == EventModifiers.Alt))
  16. {
  17. TimelineCursors.SetCursor(TimelineCursors.CursorType.Pan);
  18. m_Active = true;
  19. return true;
  20. }
  21. return false;
  22. }
  23. protected override bool MouseUp(Event evt, WindowState state)
  24. {
  25. if (m_Active)
  26. {
  27. TimelineCursors.ClearCursor();
  28. state.editorWindow.Repaint();
  29. }
  30. return false;
  31. }
  32. protected override bool MouseDrag(Event evt, WindowState state)
  33. {
  34. // Note: Do not rely on evt.button here as some 3rd party automation
  35. // software does not properly set the button data during drag.
  36. if (!m_Active)
  37. return false;
  38. return Pan(evt, state);
  39. }
  40. protected override bool MouseWheel(Event evt, WindowState state)
  41. {
  42. if (Math.Abs(evt.delta.x) < 1e-5 || Math.Abs(evt.delta.x) <= Math.Abs(evt.delta.y))
  43. return false;
  44. TimelineZoomManipulator.InvalidateWheelZoom();
  45. var panEvent = new Event(evt);
  46. panEvent.delta = new Vector2(panEvent.delta.x * k_MaxPanSpeed * -1.0f, 0.0f);
  47. return Pan(panEvent, state);
  48. }
  49. static bool Pan(Event evt, WindowState state)
  50. {
  51. var cursorRect = TimelineWindow.instance.sequenceContentRect;
  52. cursorRect.xMax = TimelineWindow.instance.position.xMax;
  53. cursorRect.yMax = TimelineWindow.instance.position.yMax;
  54. if (state.GetWindow() != null && state.GetWindow().treeView != null)
  55. {
  56. var scroll = state.GetWindow().treeView.scrollPosition;
  57. scroll.y -= evt.delta.y;
  58. state.GetWindow().treeView.scrollPosition = scroll;
  59. state.OffsetTimeArea((int)evt.delta.x);
  60. return true;
  61. }
  62. return false;
  63. }
  64. }
  65. class TimelineZoomManipulator : Manipulator
  66. {
  67. Vector2 m_MouseDownPos = Vector2.zero;
  68. float m_FocalTime;
  69. float m_LastMouseMoveX = -1;
  70. bool m_WheelUsedLast;
  71. TimelineZoomManipulator() { }
  72. public static readonly TimelineZoomManipulator Instance = new TimelineZoomManipulator();
  73. internal void DoZoom(float zoomFactor)
  74. {
  75. var refRange = TimelineEditor.visibleTimeRange;
  76. DoZoom(zoomFactor, refRange, (refRange.x + refRange.y) / 2);
  77. // Force resetting the reference zoom after a Framing operation
  78. InvalidateWheelZoom();
  79. }
  80. static void DoZoom(float zoomFactor, Vector2 refRange, float focalTime)
  81. {
  82. const float kMinRange = 0.05f; // matches zoomable area.
  83. if (zoomFactor <= 0)
  84. return;
  85. var t = Mathf.Max(focalTime, refRange.x);
  86. var x = (refRange.x + t * (zoomFactor - 1)) / zoomFactor;
  87. var y = (refRange.y + t * (zoomFactor - 1)) / zoomFactor;
  88. var newRange = Mathf.Abs(x - y) < kMinRange ? refRange : new Vector2(
  89. Mathf.Max(x, -WindowConstants.timeAreaShownRangePadding),
  90. Mathf.Min(y, WindowState.kMaxShownTime));
  91. if (newRange != refRange)
  92. // Zoomable area does not protect 100% against crazy values
  93. TimelineEditor.visibleTimeRange = newRange;
  94. }
  95. internal static void InvalidateWheelZoom()
  96. {
  97. Instance.m_WheelUsedLast = false;
  98. }
  99. protected override bool MouseDown(Event evt, WindowState state)
  100. {
  101. m_MouseDownPos = evt.mousePosition;
  102. m_FocalTime = state.PixelToTime(m_MouseDownPos.x);
  103. return false;
  104. }
  105. protected override bool MouseWheel(Event evt, WindowState state)
  106. {
  107. if (Math.Abs(evt.delta.y) < 1e-5)
  108. return false;
  109. var zoomRect = TimelineWindow.instance.sequenceContentRect;
  110. zoomRect.yMax += TimelineWindow.instance.horizontalScrollbarHeight;
  111. if (!zoomRect.Contains(evt.mousePosition))
  112. return false;
  113. if (!m_WheelUsedLast || Mathf.Abs(m_LastMouseMoveX - evt.mousePosition.x) > 1.0f)
  114. {
  115. m_LastMouseMoveX = evt.mousePosition.x;
  116. m_FocalTime = state.PixelToTime(m_LastMouseMoveX);
  117. }
  118. var zoomFactor = -evt.delta.y * 0.02f + 1;
  119. DoZoom(zoomFactor, state.timeAreaShownRange, m_FocalTime);
  120. m_WheelUsedLast = true;
  121. return true;
  122. }
  123. protected override bool MouseDrag(Event evt, WindowState state)
  124. {
  125. // Fast zoom...
  126. if (evt.modifiers != EventModifiers.Alt || evt.button != 1) return false;
  127. var mouseMoveLength = Event.current.mousePosition - m_MouseDownPos;
  128. var delta = Math.Abs(mouseMoveLength.x) > Math.Abs(mouseMoveLength.y)
  129. ? mouseMoveLength.x
  130. : -mouseMoveLength.y;
  131. var zoomFactor = PixelToZoom(delta);
  132. DoZoom(zoomFactor, state.timeAreaShownRange, m_FocalTime);
  133. m_WheelUsedLast = false;
  134. return true;
  135. }
  136. static float PixelToZoom(float x)
  137. {
  138. const float pixel2Zoom = 1 / 300.0f;
  139. x *= pixel2Zoom;
  140. if (x < -0.75)
  141. {
  142. // Rational function that behaves like 1+x on [-0.75,inf) and asymptotically reaches zero on (-inf,-0.75]
  143. // The coefficients were obtained by the following constraints:
  144. //1) f(-0.75) = 0.25
  145. //2) f'(-0.75) = 1 C1 continuity
  146. //3) f(-3) = 0.001 (asymptotically zero)
  147. return 1 / (98.6667f + 268.444f * x + 189.63f * x * x);
  148. }
  149. return 1 + x;
  150. }
  151. }
  152. class TimelineShortcutManipulator : Manipulator
  153. {
  154. protected override bool ValidateCommand(Event evt, WindowState state)
  155. {
  156. return evt.commandName == EventCommandNames.Copy ||
  157. evt.commandName == EventCommandNames.Paste ||
  158. evt.commandName == EventCommandNames.Duplicate ||
  159. evt.commandName == EventCommandNames.SelectAll ||
  160. evt.commandName == EventCommandNames.Delete ||
  161. evt.commandName == EventCommandNames.SoftDelete ||
  162. evt.commandName == EventCommandNames.FrameSelected;
  163. }
  164. protected override bool ExecuteCommand(Event evt, WindowState state)
  165. {
  166. if (state.IsCurrentEditingASequencerTextField())
  167. return false;
  168. if (evt.commandName == EventCommandNames.SelectAll)
  169. {
  170. Invoker.InvokeWithSelected<SelectAllAction>();
  171. return true;
  172. }
  173. if (evt.commandName == EventCommandNames.SoftDelete)
  174. {
  175. Invoker.InvokeWithSelected<DeleteAction>();
  176. return true;
  177. }
  178. if (evt.commandName == EventCommandNames.FrameSelected)
  179. {
  180. Invoker.InvokeWithSelected<FrameSelectedAction>();
  181. return true;
  182. }
  183. return ActionManager.HandleShortcut(evt);
  184. }
  185. }
  186. class InlineCurvesShortcutManipulator : Manipulator
  187. {
  188. protected override bool ExecuteCommand(Event evt, WindowState state)
  189. {
  190. if (state.IsCurrentEditingASequencerTextField())
  191. return false;
  192. var inlineCurveEditor = SelectionManager.GetCurrentInlineEditorCurve();
  193. if (inlineCurveEditor == null || !inlineCurveEditor.inlineCurvesSelected)
  194. return false;
  195. if (evt.commandName != EventCommandNames.FrameSelected)
  196. return false;
  197. Invoker.InvokeWithSelected<FrameSelectedAction>();
  198. return true;
  199. }
  200. // CurveEditor uses an hardcoded shortcut to execute the FrameAll action, preventing the ShortcutManager from
  201. // ever picking it up. We have to hijack it to ensure our code is being run when framing inline curves.
  202. protected override bool KeyDown(Event evt, WindowState state)
  203. {
  204. var inlineCurveEditor = SelectionManager.GetCurrentInlineEditorCurve();
  205. if (inlineCurveEditor == null || !inlineCurveEditor.inlineCurvesSelected)
  206. return false;
  207. // Not conflicting with the hardcoded value
  208. if (evt.keyCode != KeyCode.A)
  209. return false;
  210. var combination = ShortcutManager.instance.GetShortcutBinding(Shortcuts.Timeline.frameAll)
  211. .keyCombinationSequence.ToList();
  212. var shortcutCombination = combination.First();
  213. var currentCombination = KeyCombination.FromKeyboardInput(evt);
  214. // User is not actually pressing the correct key combination for FrameAll
  215. if (combination.Count == 1 && shortcutCombination.Equals(currentCombination))
  216. Invoker.InvokeWithSelected<FrameAllAction>();
  217. return true;
  218. }
  219. }
  220. }