ItemsLayer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline
  7. {
  8. enum Layer : byte
  9. {
  10. Clips,
  11. ClipHandles,
  12. Markers,
  13. MarkerHeaderTrack,
  14. MarkersOnHeader
  15. }
  16. struct LayerZOrder : IComparable<LayerZOrder>
  17. {
  18. Layer m_Layer;
  19. int m_ZOrder;
  20. public LayerZOrder(Layer layer, int zOrder)
  21. {
  22. m_Layer = layer;
  23. m_ZOrder = zOrder;
  24. }
  25. public int CompareTo(LayerZOrder other)
  26. {
  27. if (m_Layer == other.m_Layer)
  28. return m_ZOrder.CompareTo(other.m_ZOrder);
  29. return m_Layer.CompareTo(other.m_Layer);
  30. }
  31. public static LayerZOrder operator++(LayerZOrder x)
  32. {
  33. return new LayerZOrder(x.m_Layer, x.m_ZOrder + 1);
  34. }
  35. public LayerZOrder ChangeLayer(Layer layer)
  36. {
  37. return new LayerZOrder(layer, m_ZOrder);
  38. }
  39. }
  40. interface ILayerable
  41. {
  42. LayerZOrder zOrder { get; }
  43. }
  44. interface IZOrderProvider
  45. {
  46. LayerZOrder Next();
  47. }
  48. abstract class ItemsLayer : IZOrderProvider
  49. {
  50. // provide a buffer for time-based culling to allow for UI that extends slightly beyong the time (e.g. markers)
  51. // prevents popping of marker visibility.
  52. private const int kVisibilityBufferInPixels = 10;
  53. int m_PreviousLayerStateHash = -1;
  54. LayerZOrder m_LastZOrder;
  55. public LayerZOrder Next()
  56. {
  57. return m_LastZOrder++;
  58. }
  59. readonly List<TimelineItemGUI> m_Items = new List<TimelineItemGUI>();
  60. bool m_NeedSort = true;
  61. public virtual void Draw(Rect rect, WindowState state)
  62. {
  63. if (!m_Items.Any()) return;
  64. Sort();
  65. // buffer to prevent flickering of markers at boundaries
  66. var onePixelTime = state.PixelDeltaToDeltaTime(kVisibilityBufferInPixels);
  67. var visibleTime = state.timeAreaShownRange + new Vector2(-onePixelTime, onePixelTime);
  68. var layerViewStateHasChanged = GetLayerViewStateChanged(rect, state);
  69. foreach (var item in m_Items)
  70. {
  71. item.visible = item.end > visibleTime.x && item.start < visibleTime.y;
  72. if (!item.visible)
  73. continue;
  74. item.Draw(rect, layerViewStateHasChanged, state);
  75. }
  76. }
  77. public IEnumerable<TimelineItemGUI> items
  78. {
  79. get
  80. {
  81. return m_Items;
  82. }
  83. }
  84. protected void AddItem(TimelineItemGUI item)
  85. {
  86. m_Items.Add(item);
  87. m_NeedSort = true;
  88. }
  89. protected ItemsLayer(Layer layerOrder)
  90. {
  91. m_LastZOrder = new LayerZOrder(layerOrder, 0);
  92. }
  93. void Sort()
  94. {
  95. if (!m_NeedSort)
  96. return;
  97. m_Items.Sort((a, b) => a.zOrder.CompareTo(b.zOrder));
  98. m_NeedSort = false;
  99. }
  100. bool GetLayerViewStateChanged(Rect rect, WindowState state)
  101. {
  102. var layerStateHash = rect.GetHashCode().CombineHash(state.viewStateHash);
  103. var layerViewStateHasChanged = layerStateHash != m_PreviousLayerStateHash;
  104. if (Event.current.type == EventType.Layout && layerViewStateHasChanged)
  105. m_PreviousLayerStateHash = layerStateHash;
  106. return layerViewStateHasChanged;
  107. }
  108. }
  109. }