TrimItemModeMix.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. class TrimItemModeMix : ITrimItemMode, ITrimItemDrawer
  8. {
  9. ITrimmable m_Item;
  10. double m_Min;
  11. double m_Max;
  12. public void OnBeforeTrim(ITrimmable item, TrimEdge trimDirection)
  13. {
  14. m_Item = item;
  15. var sortedItems = ItemsUtils.GetItemsExcept(item.parentTrack, new[] {item})
  16. .OfType<ITrimmable>()
  17. .OrderBy(c => c.start);
  18. var itemStart = (DiscreteTime)item.start;
  19. var itemEnd = (DiscreteTime)item.end;
  20. var overlapped = sortedItems.LastOrDefault(c => (DiscreteTime)c.start == itemStart && (DiscreteTime)c.end == itemEnd);
  21. ITrimmable nextItem;
  22. ITrimmable prevItem;
  23. m_Min = 0.0;
  24. m_Max = double.PositiveInfinity;
  25. var blendableItem = item as IBlendable;
  26. if (blendableItem != null && blendableItem.supportsBlending)
  27. {
  28. if (trimDirection == TrimEdge.Start)
  29. {
  30. nextItem = sortedItems.FirstOrDefault(c => (DiscreteTime)c.start >= itemStart && (DiscreteTime)c.end > itemEnd);
  31. prevItem = sortedItems.LastOrDefault(c => (DiscreteTime)c.start <= itemStart && (DiscreteTime)c.end < itemEnd);
  32. if (prevItem != null)
  33. m_Min = prevItem.start + EditModeUtils.BlendDuration(prevItem, TrimEdge.Start);
  34. if (nextItem != null)
  35. m_Max = nextItem.start;
  36. }
  37. else
  38. {
  39. nextItem = sortedItems.FirstOrDefault(c => c != overlapped && (DiscreteTime)c.start >= itemStart && (DiscreteTime)c.end >= itemEnd);
  40. prevItem = sortedItems.LastOrDefault(c => c != overlapped && (DiscreteTime)c.start <= itemStart && (DiscreteTime)c.end <= itemEnd);
  41. if (prevItem != null)
  42. m_Min = prevItem.end;
  43. if (nextItem != null)
  44. m_Max = nextItem.end - EditModeUtils.BlendDuration(nextItem, TrimEdge.End);
  45. }
  46. }
  47. else
  48. {
  49. nextItem = sortedItems.FirstOrDefault(c => (DiscreteTime)c.start > itemStart);
  50. prevItem = sortedItems.LastOrDefault(c => (DiscreteTime)c.start < itemStart);
  51. if (prevItem != null)
  52. m_Min = prevItem.end;
  53. if (nextItem != null)
  54. m_Max = nextItem.start;
  55. }
  56. }
  57. public void TrimStart(ITrimmable item, double time)
  58. {
  59. time = Math.Min(Math.Max(time, m_Min), m_Max);
  60. item.SetStart(time);
  61. }
  62. public void TrimEnd(ITrimmable item, double time, bool affectTimeScale)
  63. {
  64. time = Math.Min(Math.Max(time, m_Min), m_Max);
  65. item.SetEnd(time, affectTimeScale);
  66. }
  67. public void DrawGUI(WindowState state, Rect bounds, Color color, TrimEdge edge)
  68. {
  69. if (EditModeUtils.HasBlends(m_Item, edge))
  70. {
  71. EditModeGUIUtils.DrawBoundsEdge(bounds, color, edge);
  72. var cursorType = (edge == TrimEdge.End)
  73. ? TimelineCursors.CursorType.MixRight
  74. : TimelineCursors.CursorType.MixLeft;
  75. TimelineCursors.SetCursor(cursorType);
  76. }
  77. else
  78. {
  79. TimelineCursors.ClearCursor();
  80. }
  81. }
  82. }
  83. }