MoveItemModeRipple.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace UnityEditor.Timeline
  7. {
  8. class MoveItemModeRipple : IMoveItemMode, IMoveItemDrawer
  9. {
  10. const float k_SnapToEdgeDistance = 30.0f;
  11. class PrevItemInfo
  12. {
  13. public ITimelineItem item;
  14. public ITimelineItem firstSelectedItem;
  15. public bool blending;
  16. public PrevItemInfo(ITimelineItem item, ITimelineItem firstSelectedItem)
  17. {
  18. this.item = item;
  19. this.firstSelectedItem = firstSelectedItem;
  20. blending = item != null && item.end > firstSelectedItem.start;
  21. }
  22. }
  23. readonly Dictionary<Object, List<ITimelineItem>> m_NextItems = new Dictionary<Object, List<ITimelineItem>>();
  24. readonly Dictionary<Object, PrevItemInfo> m_PreviousItem = new Dictionary<Object, PrevItemInfo>();
  25. double m_PreviousEnd;
  26. bool m_TrackLocked;
  27. bool m_Detached;
  28. public void OnTrackDetach(IEnumerable<ItemsPerTrack> itemsGroups)
  29. {
  30. if (m_TrackLocked)
  31. return;
  32. if (m_Detached)
  33. return;
  34. if (itemsGroups.Any(x => x.markers.Any()))
  35. return;
  36. // Ripple can either remove or not clips when detaching them from their track.
  37. // Keep it off for now. TODO: add clutch key to toggle this feature?
  38. //EditModeRippleUtils.Remove(manipulatedClipsList);
  39. StartDetachMode(itemsGroups);
  40. }
  41. public void HandleTrackSwitch(IEnumerable<ItemsPerTrack> itemsGroups)
  42. {
  43. // Nothing
  44. }
  45. public bool AllowTrackSwitch()
  46. {
  47. return !m_TrackLocked;
  48. }
  49. public double AdjustStartTime(WindowState state, ItemsPerTrack itemsGroup, double time)
  50. {
  51. var track = itemsGroup.targetTrack;
  52. if (track == null)
  53. return time;
  54. double start;
  55. double end;
  56. if (EditModeUtils.IsInfiniteTrack(track))
  57. {
  58. EditModeUtils.GetInfiniteClipBoundaries(track, out start, out end);
  59. }
  60. else
  61. {
  62. var siblings = ItemsUtils.GetItemsExcept(track, itemsGroup.items);
  63. var firstIntersectedItem = EditModeUtils.GetFirstIntersectedItem(siblings, time);
  64. if (firstIntersectedItem == null)
  65. return time;
  66. start = firstIntersectedItem.start;
  67. end = firstIntersectedItem.end;
  68. }
  69. var closestTime = Math.Abs(time - start) < Math.Abs(time - end) ? start : end;
  70. var pixelTime = state.TimeToPixel(time);
  71. var pixelClosestTime = state.TimeToPixel(closestTime);
  72. if (Math.Abs(pixelTime - pixelClosestTime) < k_SnapToEdgeDistance)
  73. return closestTime;
  74. return time;
  75. }
  76. void StartDetachMode(IEnumerable<ItemsPerTrack> itemsGroups)
  77. {
  78. m_Detached = true;
  79. foreach (var itemsGroup in itemsGroups)
  80. EditModeUtils.SetParentTrack(itemsGroup.items, null);
  81. }
  82. public void OnModeClutchEnter(IEnumerable<ItemsPerTrack> itemsGroups)
  83. {
  84. StartDetachMode(itemsGroups);
  85. m_TrackLocked = false;
  86. }
  87. public void OnModeClutchExit(IEnumerable<ItemsPerTrack> itemsGroups)
  88. {
  89. m_Detached = false;
  90. m_TrackLocked = false;
  91. }
  92. public void BeginMove(IEnumerable<ItemsPerTrack> itemsGroups)
  93. {
  94. m_NextItems.Clear();
  95. m_PreviousItem.Clear();
  96. var itemTypes = ItemsUtils.GetItemTypes(itemsGroups).ToList();
  97. foreach (var itemsGroup in itemsGroups)
  98. {
  99. //can only ripple items of the same type as those selected
  100. var sortedSelectedItems = itemsGroup.items.OrderBy(i => i.start).ToList();
  101. var siblings = itemsGroup.targetTrack.GetItemsExcept(itemsGroup.items);
  102. var sortedSiblingsToRipple = siblings.Where(i => itemTypes.Contains(i.GetType())).OrderBy(i => i.start).ToList();
  103. var start = sortedSelectedItems.First().start;
  104. m_NextItems.Add(itemsGroup.targetTrack, sortedSiblingsToRipple.Where(i => i.start > start).ToList());
  105. m_PreviousItem.Add(itemsGroup.targetTrack, CalculatePrevItemInfo(sortedSelectedItems, sortedSiblingsToRipple, itemTypes));
  106. }
  107. m_PreviousEnd = itemsGroups.Max(m => m.items.Max(c => c.end));
  108. }
  109. public void UpdateMove(IEnumerable<ItemsPerTrack> itemsGroups)
  110. {
  111. if (m_Detached)
  112. return;
  113. m_TrackLocked = true;
  114. var overlap = 0.0;
  115. foreach (var itemsGroup in itemsGroups)
  116. {
  117. var track = itemsGroup.targetTrack;
  118. if (track == null) continue;
  119. var prevItemInfo = m_PreviousItem[track];
  120. if (prevItemInfo.item != null)
  121. {
  122. var prevItem = prevItemInfo.item;
  123. var firstItem = prevItemInfo.firstSelectedItem;
  124. if (prevItemInfo.blending)
  125. prevItemInfo.blending = prevItem.end > firstItem.start;
  126. if (prevItemInfo.blending)
  127. {
  128. var b = EditModeUtils.BlendDuration(firstItem, TrimEdge.End);
  129. overlap = Math.Max(overlap, Math.Max(prevItem.start, prevItem.end - firstItem.end + firstItem.start + b) - firstItem.start);
  130. }
  131. else
  132. {
  133. overlap = Math.Max(overlap, prevItem.end - firstItem.start);
  134. }
  135. }
  136. }
  137. if (overlap > 0)
  138. {
  139. foreach (var itemsGroup in itemsGroups)
  140. {
  141. foreach (var item in itemsGroup.items)
  142. item.start += overlap;
  143. }
  144. }
  145. var newEnd = itemsGroups.Max(m => m.items.Max(c => c.end));
  146. var offset = newEnd - m_PreviousEnd;
  147. m_PreviousEnd = newEnd;
  148. foreach (var itemsGroup in itemsGroups)
  149. {
  150. foreach (var item in m_NextItems[itemsGroup.targetTrack])
  151. item.start += offset;
  152. }
  153. }
  154. static PrevItemInfo CalculatePrevItemInfo(List<ITimelineItem> orderedSelection, List<ITimelineItem> orderedSiblings, IEnumerable<Type> itemTypes)
  155. {
  156. ITimelineItem previousItem = null;
  157. ITimelineItem firstSelectedItem = null;
  158. var gap = double.PositiveInfinity;
  159. foreach (var type in itemTypes)
  160. {
  161. var firstSelectedItemOfType = orderedSelection.FirstOrDefault(i => i.GetType() == type);
  162. if (firstSelectedItemOfType == null) continue;
  163. var previousItemOfType = orderedSiblings.LastOrDefault(i => i.GetType() == type && i.start < firstSelectedItemOfType.start);
  164. if (previousItemOfType == null) continue;
  165. var currentGap = firstSelectedItemOfType.start - previousItemOfType.end;
  166. if (currentGap < gap)
  167. {
  168. gap = currentGap;
  169. firstSelectedItem = firstSelectedItemOfType;
  170. previousItem = previousItemOfType;
  171. }
  172. }
  173. return new PrevItemInfo(previousItem, firstSelectedItem);
  174. }
  175. public bool ValidateMove(ItemsPerTrack itemsGroup)
  176. {
  177. return true;
  178. }
  179. public void FinishMove(IEnumerable<ItemsPerTrack> itemsGroups)
  180. {
  181. if (m_Detached)
  182. EditModeRippleUtils.Insert(itemsGroups);
  183. m_Detached = false;
  184. m_TrackLocked = false;
  185. }
  186. public void DrawGUI(WindowState state, IEnumerable<MovingItems> movingItems, Color color)
  187. {
  188. if (m_Detached)
  189. {
  190. var xMin = float.MaxValue;
  191. var xMax = float.MinValue;
  192. foreach (var grabbedItems in movingItems)
  193. {
  194. xMin = Math.Min(xMin, grabbedItems.onTrackItemsBounds.Min(b => b.xMin)); // TODO Cache this?
  195. xMax = Math.Max(xMax, grabbedItems.onTrackItemsBounds.Max(b => b.xMax));
  196. }
  197. foreach (var grabbedItems in movingItems)
  198. {
  199. var bounds = Rect.MinMaxRect(xMin, grabbedItems.onTrackItemsBounds[0].yMin,
  200. xMax, grabbedItems.onTrackItemsBounds[0].yMax);
  201. EditModeGUIUtils.DrawOverlayRect(bounds, new Color(1.0f, 1.0f, 1.0f, 0.5f));
  202. EditModeGUIUtils.DrawBoundsEdge(bounds, color, TrimEdge.Start);
  203. }
  204. }
  205. else
  206. {
  207. foreach (var grabbedItems in movingItems)
  208. {
  209. var bounds = Rect.MinMaxRect(grabbedItems.onTrackItemsBounds.Min(b => b.xMin), grabbedItems.onTrackItemsBounds[0].yMin,
  210. grabbedItems.onTrackItemsBounds.Max(b => b.xMax), grabbedItems.onTrackItemsBounds[0].yMax);
  211. EditModeGUIUtils.DrawBoundsEdge(bounds, color, TrimEdge.Start);
  212. }
  213. }
  214. TimelineCursors.SetCursor(TimelineCursors.CursorType.Ripple);
  215. }
  216. }
  217. }