TimelineSelection.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. static class SelectionManager
  8. {
  9. static IClipCurveEditorOwner currentInlineEditorCurve;
  10. public static void AddObject(Object obj)
  11. {
  12. if (obj == null)
  13. return;
  14. currentInlineEditorCurve = null;
  15. if (Selection.Contains(obj))
  16. return;
  17. Selection.Add(obj);
  18. }
  19. public static void Add(TrackAsset obj)
  20. {
  21. // this can indicate a deleted track (Unity 'null')
  22. if (obj == null)
  23. return;
  24. currentInlineEditorCurve = null;
  25. if (!Selection.Contains(obj))
  26. {
  27. WindowState state = null;
  28. if (TimelineWindow.instance != null)
  29. state = TimelineWindow.instance.state;
  30. if (!Selection.instanceIDs.Any() && state != null && state.editSequence.director != null)
  31. Selection.SetActiveObjectWithContext(obj, TimelineWindow.instance.state.editSequence.director);
  32. else
  33. Selection.Add(obj);
  34. }
  35. }
  36. public static void Add(TimelineClip item)
  37. {
  38. AddObject(EditorClipFactory.GetEditorClip(item));
  39. }
  40. public static void Add(IMarker marker)
  41. {
  42. var markerAsObject = marker as Object;
  43. if (markerAsObject != null)
  44. {
  45. if (!Selection.Contains(markerAsObject))
  46. {
  47. currentInlineEditorCurve = null;
  48. WindowState state = null;
  49. if (TimelineWindow.instance != null)
  50. state = TimelineWindow.instance.state;
  51. if (!Selection.instanceIDs.Any() && state != null && state.editSequence.director != null)
  52. Selection.SetActiveObjectWithContext(markerAsObject, TimelineWindow.instance.state.editSequence.director);
  53. else
  54. Selection.Add(markerAsObject);
  55. }
  56. }
  57. }
  58. public static void Add(ITimelineItem item)
  59. {
  60. var clipItem = item as ClipItem;
  61. if (clipItem != null)
  62. {
  63. Add(clipItem.clip);
  64. }
  65. else
  66. {
  67. var markerItem = item as MarkerItem;
  68. if (markerItem != null)
  69. {
  70. Add(markerItem.marker);
  71. }
  72. }
  73. }
  74. public static void SelectOnly(TrackAsset track)
  75. {
  76. RemoveTimelineSelection();
  77. Add(track);
  78. }
  79. public static void SelectOnly(TimelineClip clip)
  80. {
  81. RemoveTimelineSelection();
  82. Add(clip);
  83. }
  84. public static void SelectOnly(IMarker item)
  85. {
  86. RemoveTimelineSelection();
  87. Add(item);
  88. }
  89. public static void SelectOnly(ITimelineItem item)
  90. {
  91. var clipItem = item as ClipItem;
  92. if (clipItem != null)
  93. {
  94. SelectOnly(clipItem.clip);
  95. }
  96. else
  97. {
  98. var markerItem = item as MarkerItem;
  99. if (markerItem != null)
  100. {
  101. SelectOnly(markerItem.marker);
  102. }
  103. }
  104. }
  105. public static void SelectInlineCurveEditor(IClipCurveEditorOwner selection)
  106. {
  107. // case 1030681 - selecting an inline curve should remove clips and
  108. // tracks to prevent shortcuts being forwarded incorrectly
  109. if (selection != null && selection != currentInlineEditorCurve)
  110. {
  111. var track = selection.owner;
  112. var selectedTracks = SelectedTracks().ToArray();
  113. var selectedClips = SelectedClips().ToArray();
  114. foreach (var t in selectedTracks)
  115. {
  116. if (t != track)
  117. Remove(t);
  118. }
  119. foreach (var c in selectedClips)
  120. {
  121. if (c.parentTrack != track)
  122. Remove(c);
  123. }
  124. }
  125. currentInlineEditorCurve = selection;
  126. }
  127. public static IClipCurveEditorOwner GetCurrentInlineEditorCurve()
  128. {
  129. return currentInlineEditorCurve;
  130. }
  131. public static bool IsCurveEditorFocused(IClipCurveEditorOwner selection)
  132. {
  133. return selection == currentInlineEditorCurve;
  134. }
  135. public static bool Contains(TrackAsset item)
  136. {
  137. if (item == null)
  138. return false;
  139. return Selection.Contains(item);
  140. }
  141. public static bool Contains(TimelineClip item)
  142. {
  143. if (item == null)
  144. return false;
  145. return Selection.Contains(EditorClipFactory.GetEditorClip(item));
  146. }
  147. public static bool Contains(Object obj)
  148. {
  149. return Selection.Contains(obj);
  150. }
  151. public static bool Contains(IMarker marker)
  152. {
  153. var markerAsObject = marker as Object;
  154. return Selection.Contains(markerAsObject);
  155. }
  156. public static bool Contains(ITimelineItem item)
  157. {
  158. var clipItem = item as ClipItem;
  159. if (clipItem != null)
  160. {
  161. return Contains(clipItem.clip);
  162. }
  163. var markerItem = item as MarkerItem;
  164. return markerItem != null && Contains(markerItem.marker);
  165. }
  166. public static void Clear()
  167. {
  168. currentInlineEditorCurve = null;
  169. Selection.activeObject = null;
  170. }
  171. public static void UnSelectTracks()
  172. {
  173. var newSelection = Selection.objects.Where(x => !(x is TrackAsset));
  174. Selection.objects = newSelection.ToArray();
  175. }
  176. public static void Remove(TimelineClip item)
  177. {
  178. Remove(EditorClipFactory.GetEditorClip(item));
  179. }
  180. public static void Remove(IMarker marker)
  181. {
  182. var o = marker as Object;
  183. if (o != null)
  184. {
  185. Remove(o);
  186. }
  187. }
  188. static void Remove(Object item)
  189. {
  190. Selection.Remove(item);
  191. }
  192. public static void Remove(TrackAsset item)
  193. {
  194. Selection.Remove(item);
  195. }
  196. public static void Remove(ITimelineItem item)
  197. {
  198. var clipItem = item as ClipItem;
  199. if (clipItem != null)
  200. {
  201. Remove(clipItem.clip);
  202. }
  203. else
  204. {
  205. var markerItem = item as MarkerItem;
  206. if (markerItem != null)
  207. {
  208. Remove(markerItem.marker);
  209. }
  210. }
  211. }
  212. public static void RemoveTimelineSelection()
  213. {
  214. Selection.objects = Selection.objects.Where(s => !IsTimelineType(s)).ToArray();
  215. }
  216. public static void RemoveAllClips()
  217. {
  218. Selection.objects = Selection.objects.Where(s => !(s is EditorClip)).ToArray();
  219. }
  220. public static int Count()
  221. {
  222. return Selection.objects.Count(IsTimelineType);
  223. }
  224. public static IEnumerable<TimelineClipGUI> SelectedClipGUI()
  225. {
  226. foreach (var clip in SelectedClips())
  227. {
  228. var gui = ItemToItemGui.GetGuiForClip(clip);
  229. if (gui != null)
  230. yield return gui;
  231. }
  232. }
  233. public static IEnumerable<TimelineClip> SelectedClips()
  234. {
  235. return Selection.objects.OfType<EditorClip>().Select(x => x.clip);
  236. }
  237. public static IEnumerable<IMarker> SelectedMarkers()
  238. {
  239. return Selection.objects.OfType<IMarker>();
  240. }
  241. public static IEnumerable<TrackAsset> SelectedTracks()
  242. {
  243. return Selection.objects.OfType<TrackAsset>();
  244. }
  245. public static IEnumerable<T> SelectedItemOfType<T>()
  246. {
  247. if (typeof(T) == typeof(TimelineClip))
  248. return SelectedClips().Cast<T>();
  249. if (typeof(T).IsAssignableFrom(typeof(IMarker)))
  250. return SelectedMarkers().Cast<T>();
  251. return Enumerable.Empty<T>();
  252. }
  253. public static IEnumerable<TimelineTrackBaseGUI> SelectedTrackGUI()
  254. {
  255. var tracks = SelectedTracks();
  256. return TimelineWindow.instance.allTracks.Where(x => tracks.Contains(x.track));
  257. }
  258. static bool IsTimelineType(Object o)
  259. {
  260. return o is TrackAsset || o is EditorClip || o is IMarker;
  261. }
  262. public static IEnumerable<ITimelineItem> SelectedItems()
  263. {
  264. var list = new List<ITimelineItem>();
  265. foreach (var obj in Selection.objects)
  266. {
  267. var editorClip = obj as EditorClip;
  268. if (editorClip != null)
  269. {
  270. list.Add(new ClipItem(editorClip.clip));
  271. continue;
  272. }
  273. var marker = obj as IMarker;
  274. if (marker != null)
  275. {
  276. list.Add(new MarkerItem(marker));
  277. }
  278. }
  279. return list;
  280. }
  281. }
  282. }