EditModeUtils.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. static class EditModeUtils
  8. {
  9. public static void Delete(IEnumerable<ITimelineItem> items)
  10. {
  11. if (items == null)
  12. return;
  13. foreach (var item in items)
  14. item.Delete();
  15. }
  16. public static void SetStart(IEnumerable<ITimelineItem> items, double time)
  17. {
  18. var offset = time - items.Min(c => c.start);
  19. foreach (var item in items)
  20. item.start += offset;
  21. }
  22. public static void SetParentTrack(IEnumerable<ITimelineItem> items, TrackAsset parentTrack)
  23. {
  24. foreach (var item in items)
  25. {
  26. if (item.parentTrack == parentTrack)
  27. continue;
  28. item.parentTrack = parentTrack;
  29. var clipGUI = item.gui as TimelineClipGUI;
  30. if (clipGUI != null)
  31. {
  32. clipGUI.clipCurveEditor = null;
  33. }
  34. }
  35. }
  36. public static ITimelineItem GetFirstIntersectedItem(IEnumerable<ITimelineItem> items, double time)
  37. {
  38. return items.FirstOrDefault(c => Intersects(time, c.start, c.end));
  39. }
  40. static bool Intersects(double time, double start, double end)
  41. {
  42. var discreteTime = (DiscreteTime)time;
  43. return discreteTime > (DiscreteTime)start && discreteTime < (DiscreteTime)end;
  44. }
  45. public static bool Overlaps(ITimelineItem item, double from, double to)
  46. {
  47. var discreteFrom = (DiscreteTime)from;
  48. var discreteTo = (DiscreteTime)to;
  49. var discreteStart = (DiscreteTime)item.start;
  50. if (discreteStart >= discreteFrom && discreteStart < discreteTo)
  51. return true;
  52. var discreteEnd = (DiscreteTime)item.end;
  53. if (discreteEnd > discreteFrom && discreteEnd <= discreteTo)
  54. return true;
  55. return false;
  56. }
  57. public static bool IsItemWithinRange(ITimelineItem item, double from, double to)
  58. {
  59. return (DiscreteTime)item.start >= (DiscreteTime)from && (DiscreteTime)item.end <= (DiscreteTime)to;
  60. }
  61. public static bool IsRangeWithinItem(double from, double to, ITimelineItem item)
  62. {
  63. return (DiscreteTime)from >= (DiscreteTime)item.start && (DiscreteTime)to <= (DiscreteTime)item.end;
  64. }
  65. public static bool Contains(double from, double to, ITimelineItem item)
  66. {
  67. return (DiscreteTime)from < (DiscreteTime)item.start && (DiscreteTime)to > (DiscreteTime)item.end;
  68. }
  69. public static bool HasBlends(ITimelineItem item, TrimEdge edge)
  70. {
  71. var blendable = item as IBlendable;
  72. if (blendable == null) return false;
  73. return edge == TrimEdge.Start && blendable.hasLeftBlend || edge == TrimEdge.End && blendable.hasRightBlend;
  74. }
  75. public static double BlendDuration(ITimelineItem item, TrimEdge edge)
  76. {
  77. var blendable = item as IBlendable;
  78. if (blendable == null) return 0.0;
  79. return edge == TrimEdge.Start ? blendable.leftBlendDuration : blendable.rightBlendDuration;
  80. }
  81. public static bool IsInfiniteTrack(TrackAsset track)
  82. {
  83. var aTrack = track as AnimationTrack;
  84. return aTrack != null && aTrack.CanConvertToClipMode();
  85. }
  86. public static void GetInfiniteClipBoundaries(TrackAsset track, out double start, out double end)
  87. {
  88. var info = AnimationClipCurveCache.Instance.GetCurveInfo(((AnimationTrack)track).infiniteClip);
  89. if (info.keyTimes.Length > 0)
  90. {
  91. start = info.keyTimes.Min();
  92. end = info.keyTimes.Max();
  93. }
  94. else
  95. {
  96. start = end = 0.0f;
  97. }
  98. }
  99. }
  100. }