TimeAreaAutoPanner.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using UnityEngine;
  2. namespace UnityEditor.Timeline
  3. {
  4. class TimeAreaAutoPanner
  5. {
  6. readonly WindowState m_State;
  7. readonly TimelineWindow m_Window;
  8. readonly Rect m_ViewRect;
  9. const float k_PixelDistanceToMaxSpeed = 100.0f;
  10. const float k_MaxPanSpeed = 30.0f;
  11. public TimeAreaAutoPanner(WindowState state)
  12. {
  13. m_State = state;
  14. m_Window = m_State.GetWindow();
  15. var shownRange = m_State.timeAreaShownRange;
  16. var trackViewBounds = m_Window.sequenceRect;
  17. m_ViewRect = Rect.MinMaxRect(m_State.TimeToPixel(shownRange.x), trackViewBounds.yMin,
  18. m_State.TimeToPixel(shownRange.y), trackViewBounds.yMax);
  19. }
  20. public void OnGUI(Event evt)
  21. {
  22. if (evt.type != EventType.Layout)
  23. return;
  24. var hFactor = 0.0f;
  25. var vFactor = 0.0f;
  26. bool horizontalPan = GetPanFactor(evt.mousePosition.x, m_ViewRect.xMin, m_ViewRect.xMax, out hFactor);
  27. bool verticalPan = GetPanFactor(evt.mousePosition.y, m_ViewRect.yMin, m_ViewRect.yMax, out vFactor);
  28. if (horizontalPan)
  29. {
  30. var translation = m_State.timeAreaTranslation;
  31. translation.x += hFactor * k_MaxPanSpeed;
  32. m_State.SetTimeAreaTransform(translation, m_State.timeAreaScale);
  33. }
  34. if (verticalPan)
  35. {
  36. var translation = m_Window.treeView.scrollPosition;
  37. translation.y -= vFactor * k_MaxPanSpeed;
  38. m_Window.treeView.scrollPosition = translation;
  39. }
  40. }
  41. static bool GetPanFactor(float v, float min, float max, out float factor)
  42. {
  43. factor = 0.0f;
  44. if (v < min)
  45. {
  46. factor = Mathf.Clamp01((min - v) / k_PixelDistanceToMaxSpeed);
  47. return true;
  48. }
  49. if (v > max)
  50. {
  51. factor = -Mathf.Clamp01((v - max) / k_PixelDistanceToMaxSpeed);
  52. return true;
  53. }
  54. return false;
  55. }
  56. }
  57. }