EditModeReplaceUtils.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine.Timeline;
  4. namespace UnityEditor.Timeline
  5. {
  6. static class EditModeReplaceUtils
  7. {
  8. public static void Insert(IEnumerable<ItemsPerTrack> itemsGroups)
  9. {
  10. foreach (var itemsGroup in itemsGroups)
  11. {
  12. Insert(itemsGroup.targetTrack, itemsGroup.items);
  13. }
  14. }
  15. static void Insert(TrackAsset track, IEnumerable<ITimelineItem> items)
  16. {
  17. if (track == null) return;
  18. var orderedItems = ItemsUtils.GetItemsExcept(track, items)
  19. .OfType<ITrimmable>()
  20. .OrderBy(i => i.start).ToArray();
  21. foreach (var item in items.OfType<ITrimmable>())
  22. {
  23. var from = item.start;
  24. var to = item.end;
  25. var overlappedItems = orderedItems.Where(i => EditModeUtils.Overlaps(i, from, to));
  26. foreach (var overlappedItem in overlappedItems)
  27. {
  28. if (EditModeUtils.IsItemWithinRange(overlappedItem, from, to))
  29. {
  30. overlappedItem.Delete();
  31. }
  32. else
  33. {
  34. if (overlappedItem.start >= from)
  35. overlappedItem.TrimStart(to);
  36. else
  37. overlappedItem.TrimEnd(from);
  38. }
  39. }
  40. var includingItems = orderedItems.Where(c => c.start<from && c.end> to);
  41. foreach (var includingItem in includingItems)
  42. {
  43. var newItem = includingItem.CloneTo(track, includingItem.start) as ITrimmable;
  44. includingItem.TrimStart(to);
  45. if (newItem != null)
  46. newItem.SetEnd(from, false);
  47. }
  48. }
  49. }
  50. }
  51. }