Нема описа
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.

RectangleTool.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.Timeline
  4. {
  5. abstract class RectangleTool
  6. {
  7. readonly struct TimelinePoint
  8. {
  9. readonly double m_Time;
  10. readonly float m_YPos;
  11. readonly float m_YScrollPos;
  12. readonly WindowState m_State;
  13. readonly TimelineTreeViewGUI m_TreeViewGUI;
  14. public TimelinePoint(WindowState state, Vector2 mousePosition)
  15. {
  16. m_State = state;
  17. m_TreeViewGUI = state.GetWindow().treeView;
  18. m_Time = m_State.PixelToTime(mousePosition.x);
  19. m_YPos = mousePosition.y;
  20. m_YScrollPos = m_TreeViewGUI.scrollPosition.y;
  21. }
  22. public Vector2 ToPixel()
  23. {
  24. return new Vector2(m_State.TimeToPixel(m_Time), m_YPos - (m_TreeViewGUI.scrollPosition.y - m_YScrollPos));
  25. }
  26. }
  27. const float k_HeaderSplitterOverlap = WindowConstants.headerSplitterWidth / 2;
  28. TimeAreaAutoPanner m_TimeAreaAutoPanner;
  29. TimelinePoint m_StartPoint;
  30. Vector2 m_EndPixel = Vector2.zero;
  31. Rect m_ActiveRect;
  32. protected abstract bool enableAutoPan { get; }
  33. protected abstract bool CanStartRectangle(Event evt);
  34. protected abstract bool OnFinish(Event evt, WindowState state, Rect rect);
  35. int m_Id;
  36. public void OnGUI(WindowState state, EventType rawType, Vector2 mousePosition)
  37. {
  38. if (m_Id == 0)
  39. m_Id = GUIUtility.GetPermanentControlID();
  40. if (state == null || state.GetWindow().treeView == null)
  41. return;
  42. var evt = Event.current;
  43. if (rawType == EventType.MouseDown || evt.type == EventType.MouseDown)
  44. {
  45. if (state.IsCurrentEditingASequencerTextField())
  46. return;
  47. m_ActiveRect = TimelineWindow.instance.sequenceContentRect;
  48. //remove the track header splitter overlap
  49. m_ActiveRect.x += k_HeaderSplitterOverlap;
  50. m_ActiveRect.width -= k_HeaderSplitterOverlap;
  51. if (!m_ActiveRect.Contains(mousePosition))
  52. return;
  53. if (!CanStartRectangle(evt))
  54. return;
  55. if (enableAutoPan)
  56. m_TimeAreaAutoPanner = new TimeAreaAutoPanner(state);
  57. m_StartPoint = new TimelinePoint(state, mousePosition);
  58. m_EndPixel = mousePosition;
  59. GUIUtility.hotControl = m_Id; //HACK: Because the treeView eats all the events, steal the hotControl if necessary...
  60. evt.Use();
  61. return;
  62. }
  63. switch (evt.GetTypeForControl(m_Id))
  64. {
  65. case EventType.KeyDown:
  66. {
  67. if (GUIUtility.hotControl == m_Id)
  68. {
  69. if (evt.keyCode == KeyCode.Escape)
  70. {
  71. m_TimeAreaAutoPanner = null;
  72. GUIUtility.hotControl = 0;
  73. evt.Use();
  74. }
  75. }
  76. return;
  77. }
  78. case EventType.MouseDrag:
  79. {
  80. if (GUIUtility.hotControl != m_Id)
  81. return;
  82. m_EndPixel = mousePosition;
  83. evt.Use();
  84. return;
  85. }
  86. case EventType.MouseUp:
  87. {
  88. if (GUIUtility.hotControl != m_Id)
  89. return;
  90. m_TimeAreaAutoPanner = null;
  91. var rect = CurrentRectangle();
  92. if (IsValidRect(rect))
  93. OnFinish(evt, state, rect);
  94. GUIUtility.hotControl = 0;
  95. evt.Use();
  96. return;
  97. }
  98. }
  99. if (GUIUtility.hotControl == m_Id)
  100. {
  101. if (evt.type == EventType.Repaint)
  102. {
  103. var r = CurrentRectangle();
  104. if (IsValidRect(r))
  105. {
  106. using (new GUIViewportScope(m_ActiveRect))
  107. {
  108. DrawRectangle(r);
  109. }
  110. }
  111. }
  112. if (m_TimeAreaAutoPanner != null)
  113. m_TimeAreaAutoPanner.OnGUI(evt);
  114. }
  115. }
  116. static void DrawRectangle(Rect rect)
  117. {
  118. EditorStyles.selectionRect.Draw(rect, GUIContent.none, false, false, false, false);
  119. }
  120. static bool IsValidRect(Rect rect)
  121. {
  122. return rect.width >= 1.0f && rect.height >= 1.0f;
  123. }
  124. Rect CurrentRectangle()
  125. {
  126. var startPixel = m_StartPoint.ToPixel();
  127. return Rect.MinMaxRect(
  128. Math.Min(startPixel.x, m_EndPixel.x),
  129. Math.Min(startPixel.y, m_EndPixel.y),
  130. Math.Max(startPixel.x, m_EndPixel.x),
  131. Math.Max(startPixel.y, m_EndPixel.y));
  132. }
  133. }
  134. }