ManipulationsTimeline.cs 10 KB

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