PlaybackScroller.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using UnityEngine;
  2. namespace UnityEditor.Timeline
  3. {
  4. /// <summary>
  5. /// Scrolling mode during playback for the timeline window.
  6. /// </summary>
  7. public enum PlaybackScrollMode
  8. {
  9. /// <summary>
  10. /// Timeline window doesn't change while the playhead is leaving the window.
  11. /// </summary>
  12. None,
  13. /// <summary>
  14. /// Timeline window pans its content when the playhead arrive at the right of the window (like a paging scrolling).
  15. /// </summary>
  16. Pan,
  17. /// <summary>
  18. /// Timeline window move the content as the playhead moves.
  19. /// When the playhead reach the middle of the window, it stays there and the content scroll behind it.
  20. /// </summary>
  21. Smooth
  22. }
  23. static class PlaybackScroller
  24. {
  25. public static void AutoScroll(WindowState state)
  26. {
  27. if (Event.current.type != EventType.Layout)
  28. return;
  29. switch (state.autoScrollMode)
  30. {
  31. case PlaybackScrollMode.Pan:
  32. DoPanScroll(state);
  33. break;
  34. case PlaybackScrollMode.Smooth:
  35. DoSmoothScroll(state);
  36. break;
  37. }
  38. }
  39. static void DoSmoothScroll(WindowState state)
  40. {
  41. if (state.playing)
  42. state.SetPlayHeadToMiddle();
  43. state.UpdateLastFrameTime();
  44. }
  45. static void DoPanScroll(WindowState state)
  46. {
  47. if (!state.playing)
  48. return;
  49. var paddingDeltaTime = state.PixelDeltaToDeltaTime(WindowConstants.autoPanPaddingInPixels);
  50. var showRange = state.timeAreaShownRange;
  51. var rightBoundForPan = showRange.y - paddingDeltaTime;
  52. if (state.editSequence.time > rightBoundForPan)
  53. {
  54. var leftBoundForPan = showRange.x + paddingDeltaTime;
  55. var delta = rightBoundForPan - leftBoundForPan;
  56. state.SetTimeAreaShownRange(showRange.x + delta, showRange.y + delta);
  57. }
  58. }
  59. }
  60. }