EditModeInputHandler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.Timeline
  5. {
  6. class EditModeInputHandler
  7. {
  8. readonly MoveInputHandler m_MoveHandler;
  9. readonly TrimInputHandler m_TrimHandler;
  10. public EditModeInputHandler()
  11. {
  12. m_MoveHandler = new MoveInputHandler();
  13. m_TrimHandler = new TrimInputHandler();
  14. }
  15. public void ProcessMove(InputEvent action, double value)
  16. {
  17. if (TimelineWindow.instance != null && TimelineWindow.instance.state != null)
  18. ProcessInputAction(m_MoveHandler, action, value, TimelineWindow.instance.state);
  19. }
  20. public void ProcessTrim(InputEvent action, double value, bool stretch)
  21. {
  22. if (TimelineWindow.instance != null && TimelineWindow.instance.state != null)
  23. {
  24. m_TrimHandler.stretch = stretch;
  25. ProcessInputAction(m_TrimHandler, action, value, TimelineWindow.instance.state);
  26. }
  27. }
  28. public void SetValueForEdge(IEnumerable<ITimelineItem> items, AttractedEdge edge, double value)
  29. {
  30. if (TimelineWindow.instance != null && TimelineWindow.instance.state != null)
  31. MoveInputHandler.SetValueForEdge(items, edge, value, TimelineWindow.instance.state);
  32. }
  33. public void OnGUI(WindowState state, Event evt)
  34. {
  35. if (TimelineWindow.instance != null && TimelineWindow.instance.state != null)
  36. {
  37. m_MoveHandler.OnGUI(evt);
  38. m_TrimHandler.OnGUI(state);
  39. }
  40. }
  41. static void ProcessInputAction(IInputHandler handler, InputEvent action, double value, WindowState state)
  42. {
  43. var items = SelectionManager.SelectedItems();
  44. switch (action)
  45. {
  46. case InputEvent.None:
  47. return;
  48. case InputEvent.DragEnter:
  49. handler.OnEnterDrag(items, state);
  50. break;
  51. case InputEvent.Drag:
  52. handler.OnDrag(value, state);
  53. break;
  54. case InputEvent.DragExit:
  55. handler.OnExitDrag();
  56. break;
  57. case InputEvent.KeyboardInput:
  58. handler.OnSetValue(items, value, state);
  59. break;
  60. default:
  61. return;
  62. }
  63. }
  64. interface IInputHandler
  65. {
  66. void OnEnterDrag(IEnumerable<ITimelineItem> items, WindowState state);
  67. void OnDrag(double value, WindowState state);
  68. void OnExitDrag();
  69. void OnSetValue(IEnumerable<ITimelineItem> items, double value, WindowState state);
  70. }
  71. class TrimInputHandler : IInputHandler
  72. {
  73. bool isDragging { get; set; }
  74. public bool stretch { get; set; }
  75. IEnumerable<ITimelineItem> grabbedItems { get; set; }
  76. public void OnEnterDrag(IEnumerable<ITimelineItem> items, WindowState state)
  77. {
  78. grabbedItems = items.OfType<ITrimmable>().ToArray();
  79. foreach (var item in grabbedItems)
  80. {
  81. EditMode.BeginTrim(item, TrimEdge.End);
  82. }
  83. isDragging = true;
  84. }
  85. public void OnDrag(double endValue, WindowState state)
  86. {
  87. var trimValue = endValue;
  88. trimValue = TimelineWindow.instance.state.SnapToFrameIfRequired(trimValue);
  89. foreach (var item in grabbedItems)
  90. {
  91. EditMode.TrimEnd(item, trimValue, stretch);
  92. }
  93. state.UpdateRootPlayableDuration(state.editSequence.duration);
  94. }
  95. public void OnExitDrag()
  96. {
  97. isDragging = false;
  98. EditMode.FinishTrim();
  99. TimelineWindow.instance.Repaint();
  100. }
  101. public void OnSetValue(IEnumerable<ITimelineItem> items, double endValue, WindowState state)
  102. {
  103. foreach (var item in items.OfType<ITrimmable>())
  104. {
  105. EditMode.BeginTrim(item, TrimEdge.End);
  106. EditMode.TrimEnd(item, endValue, stretch);
  107. EditMode.FinishTrim();
  108. }
  109. state.UpdateRootPlayableDuration(state.editSequence.duration);
  110. }
  111. public void OnGUI(WindowState state)
  112. {
  113. if (!isDragging) return;
  114. foreach (var item in grabbedItems)
  115. {
  116. EditMode.DrawTrimGUI(state, item.gui, TrimEdge.End);
  117. }
  118. }
  119. }
  120. class MoveInputHandler : IInputHandler
  121. {
  122. MoveItemHandler m_MoveItemHandler;
  123. public void OnEnterDrag(IEnumerable<ITimelineItem> items, WindowState state)
  124. {
  125. if (items.Any())
  126. {
  127. m_MoveItemHandler = new MoveItemHandler(state);
  128. m_MoveItemHandler.Grab(items, items.First().parentTrack);
  129. }
  130. }
  131. public void OnDrag(double value, WindowState state)
  132. {
  133. if (m_MoveItemHandler == null) return;
  134. var startValue = value;
  135. startValue = state.SnapToFrameIfRequired(startValue);
  136. m_MoveItemHandler.OnAttractedEdge(null, ManipulateEdges.Both, AttractedEdge.None, startValue);
  137. }
  138. public void OnExitDrag()
  139. {
  140. if (m_MoveItemHandler == null) return;
  141. m_MoveItemHandler.Drop();
  142. m_MoveItemHandler = null;
  143. GUIUtility.ExitGUI();
  144. }
  145. public void OnSetValue(IEnumerable<ITimelineItem> items, double value, WindowState state)
  146. {
  147. if (!items.Any()) return;
  148. m_MoveItemHandler = new MoveItemHandler(state);
  149. m_MoveItemHandler.Grab(items, items.First().parentTrack);
  150. m_MoveItemHandler.OnAttractedEdge(null, ManipulateEdges.Both, AttractedEdge.None, value);
  151. m_MoveItemHandler.Drop();
  152. m_MoveItemHandler = null;
  153. }
  154. public void OnGUI(Event evt)
  155. {
  156. if (m_MoveItemHandler != null)
  157. m_MoveItemHandler.OnGUI(evt);
  158. }
  159. public static void SetValueForEdge(IEnumerable<ITimelineItem> items, AttractedEdge edge, double value, WindowState state)
  160. {
  161. var handler = new MoveItemHandler(state);
  162. foreach (var item in items)
  163. {
  164. handler.Grab(new[] {item}, item.parentTrack);
  165. handler.OnAttractedEdge(null, ManipulateEdges.Both, edge, value);
  166. handler.Drop();
  167. }
  168. }
  169. }
  170. }
  171. }