Clipboard.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. class Clipboard
  8. {
  9. class ExposedReferenceTable : IExposedPropertyTable
  10. {
  11. Dictionary<PropertyName, Object> m_ReferenceTable = new Dictionary<PropertyName, Object>();
  12. public void SetReferenceValue(PropertyName id, Object value)
  13. {
  14. m_ReferenceTable[id] = value;
  15. }
  16. public Object GetReferenceValue(PropertyName id, out bool idValid)
  17. {
  18. Object reference;
  19. idValid = m_ReferenceTable.TryGetValue(id, out reference);
  20. return reference;
  21. }
  22. public void ClearReferenceValue(PropertyName id)
  23. {
  24. m_ReferenceTable.Remove(id);
  25. }
  26. public void Clear()
  27. {
  28. m_ReferenceTable.Clear();
  29. }
  30. }
  31. public struct ClipboardTrackEntry
  32. {
  33. public TrackAsset item;
  34. public TrackAsset parent;
  35. }
  36. static readonly int kListInitialSize = 10;
  37. readonly List<ItemsPerTrack> m_ItemsData = new List<ItemsPerTrack>(kListInitialSize);
  38. readonly List<ClipboardTrackEntry> m_trackData = new List<ClipboardTrackEntry>(kListInitialSize);
  39. TimelineAsset rootTimeline;
  40. public readonly IExposedPropertyTable exposedPropertyTable = new ExposedReferenceTable();
  41. public Clipboard()
  42. {
  43. rootTimeline = CreateTimeline();
  44. EditorApplication.playModeStateChanged += OnPlayModeChanged;
  45. }
  46. public void CopyItems(IEnumerable<ITimelineItem> items)
  47. {
  48. using (new TimelineUndo.DisableUndoGuard(true))
  49. {
  50. var itemsByParent = items.ToLookup(i => i.parentTrack);
  51. foreach (var itemsGroup in itemsByParent)
  52. {
  53. var parent = itemsGroup.Key;
  54. var itemsList = new List<ITimelineItem>();
  55. foreach (var item in itemsGroup)
  56. {
  57. if (item is ClipItem)
  58. itemsList.Add(CopyItem((ClipItem)item));
  59. else if (item is MarkerItem)
  60. itemsList.Add(CopyItem((MarkerItem)item));
  61. }
  62. m_ItemsData.Add(new ItemsPerTrack(parent, itemsList));
  63. }
  64. }
  65. }
  66. ClipItem CopyItem(ClipItem clipItem)
  67. {
  68. var newClip = TimelineHelpers.Clone(clipItem.clip, TimelineWindow.instance.state.editSequence.director, exposedPropertyTable, rootTimeline);
  69. return new ClipItem(newClip);
  70. }
  71. static MarkerItem CopyItem(MarkerItem markerItem)
  72. {
  73. var markerObject = markerItem.marker as Object;
  74. if (markerObject != null)
  75. {
  76. var newMarker = Object.Instantiate(markerObject);
  77. newMarker.name = markerObject.name;
  78. return new MarkerItem((IMarker)newMarker);
  79. }
  80. return null;
  81. }
  82. public void CopyTracks(IEnumerable<TrackAsset> tracks)
  83. {
  84. using (new TimelineUndo.DisableUndoGuard(true))
  85. {
  86. foreach (var track in TrackExtensions.FilterTracks(tracks))
  87. {
  88. var newTrack = track.Duplicate(TimelineEditor.inspectedDirector, TimelineEditor.clipboard.exposedPropertyTable, rootTimeline);
  89. m_trackData.Add(new ClipboardTrackEntry {item = newTrack, parent = track.parent as TrackAsset});
  90. }
  91. }
  92. }
  93. public IEnumerable<ClipboardTrackEntry> GetTracks()
  94. {
  95. return m_trackData;
  96. }
  97. public IEnumerable<ItemsPerTrack> GetCopiedItems()
  98. {
  99. return m_ItemsData;
  100. }
  101. public void Clear()
  102. {
  103. m_ItemsData.Clear();
  104. m_trackData.Clear();
  105. rootTimeline = CreateTimeline();
  106. ((ExposedReferenceTable)exposedPropertyTable).Clear();
  107. }
  108. private void OnPlayModeChanged(PlayModeStateChange state)
  109. {
  110. if (state == PlayModeStateChange.EnteredEditMode || state == PlayModeStateChange.EnteredPlayMode)
  111. Clear();
  112. }
  113. static TimelineAsset CreateTimeline()
  114. {
  115. var timeline = ScriptableObject.CreateInstance<TimelineAsset>();
  116. timeline.hideFlags |= HideFlags.DontSave;
  117. timeline.name = "Clipboard";
  118. return timeline;
  119. }
  120. }
  121. }