MarkerEditor.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using UnityEngine;
  2. using UnityEditor.Timeline;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. /// <summary>
  8. /// The flags that indicate the view status of a marker.
  9. /// </summary>
  10. [System.Flags]
  11. public enum MarkerUIStates
  12. {
  13. /// <summary>
  14. /// No extra state specified.
  15. /// </summary>
  16. None = 0,
  17. /// <summary>
  18. /// The marker is selected.
  19. /// </summary>
  20. Selected = 1 << 0,
  21. /// <summary>
  22. /// The marker is in a collapsed state.
  23. /// </summary>
  24. Collapsed = 1 << 1
  25. }
  26. /// <summary>
  27. /// The user-defined options for drawing a marker.
  28. /// </summary>
  29. public struct MarkerDrawOptions
  30. {
  31. /// <summary>
  32. /// The tooltip for the marker.
  33. /// </summary>
  34. public string tooltip { get; set; }
  35. /// <summary>
  36. /// Text that indicates if the marker should display an error.
  37. /// </summary>
  38. /// <remarks>
  39. /// If the error text is not empty or null, then the marker displays a warning. The error text is used as the tooltip.
  40. /// </remarks>
  41. public string errorText { get; set; }
  42. public override bool Equals(object obj)
  43. {
  44. if (!(obj is MarkerDrawOptions))
  45. return false;
  46. return Equals((MarkerDrawOptions)obj);
  47. }
  48. public bool Equals(MarkerDrawOptions other)
  49. {
  50. return errorText == other.errorText &&
  51. tooltip == other.tooltip;
  52. }
  53. public override int GetHashCode()
  54. {
  55. return HashUtility.CombineHash(
  56. errorText != null ? errorText.GetHashCode() : 0,
  57. tooltip != null ? tooltip.GetHashCode() : 0
  58. );
  59. }
  60. public static bool operator==(MarkerDrawOptions options1, MarkerDrawOptions options2)
  61. {
  62. return options1.Equals(options2);
  63. }
  64. public static bool operator!=(MarkerDrawOptions options1, MarkerDrawOptions options2)
  65. {
  66. return !options1.Equals(options2);
  67. }
  68. }
  69. /// <summary>
  70. /// The description of the on-screen area where the marker is drawn.
  71. /// </summary>
  72. public struct MarkerOverlayRegion
  73. {
  74. /// <summary>
  75. /// The area where the marker is being drawn.
  76. /// </summary>
  77. public Rect markerRegion { get; private set; }
  78. /// <summary>
  79. /// TThe area where the overlay is being drawn.
  80. /// </summary>
  81. public Rect timelineRegion { get; private set; }
  82. /// <summary>
  83. /// The start time of the visible region of the window.
  84. /// </summary>
  85. public double startTime { get; private set; }
  86. /// <summary>
  87. /// The end time of the visible region of the window.
  88. /// </summary>
  89. public double endTime { get; private set; }
  90. /// <summary>Constructor</summary>
  91. public MarkerOverlayRegion(Rect _markerRegion, Rect _timelineRegion, double _startTime, double _endTime)
  92. {
  93. markerRegion = _markerRegion;
  94. timelineRegion = _timelineRegion;
  95. startTime = _startTime;
  96. endTime = _endTime;
  97. }
  98. public override bool Equals(object obj)
  99. {
  100. if (!(obj is MarkerOverlayRegion))
  101. return false;
  102. return Equals((MarkerOverlayRegion)obj);
  103. }
  104. public bool Equals(MarkerOverlayRegion other)
  105. {
  106. return markerRegion == other.markerRegion &&
  107. timelineRegion == other.timelineRegion &&
  108. startTime == other.startTime &&
  109. endTime == other.endTime;
  110. }
  111. public override int GetHashCode()
  112. {
  113. return HashUtility.CombineHash(
  114. markerRegion.GetHashCode(),
  115. timelineRegion.GetHashCode(),
  116. startTime.GetHashCode(),
  117. endTime.GetHashCode()
  118. );
  119. }
  120. public static bool operator==(MarkerOverlayRegion region1, MarkerOverlayRegion region2)
  121. {
  122. return region1.Equals(region2);
  123. }
  124. public static bool operator!=(MarkerOverlayRegion region1, MarkerOverlayRegion region2)
  125. {
  126. return !region1.Equals(region2);
  127. }
  128. }
  129. /// <summary>
  130. /// Use this class to customize marker types in the TimelineEditor.
  131. /// </summary>
  132. public class MarkerEditor
  133. {
  134. internal readonly bool supportsDrawOverlay;
  135. /// <summary>
  136. /// Default constructor
  137. /// </summary>
  138. public MarkerEditor()
  139. {
  140. supportsDrawOverlay = TypeUtility.HasOverrideMethod(GetType(), nameof(DrawOverlay));
  141. }
  142. /// <summary>
  143. /// Implement this method to override the default options for drawing a marker.
  144. /// </summary>
  145. /// <param name="marker">The marker to draw.</param>
  146. /// <returns></returns>
  147. public virtual MarkerDrawOptions GetMarkerOptions(IMarker marker)
  148. {
  149. return new MarkerDrawOptions()
  150. {
  151. tooltip = string.Empty,
  152. errorText = string.Empty,
  153. };
  154. }
  155. /// <summary>
  156. /// Called when a marker is created.
  157. /// </summary>
  158. /// <param name="marker">The marker that is created.</param>
  159. /// <param name="clonedFrom">TThe source that the marker was copied from. This can be set to null if the marker is not a copy.</param>
  160. /// <remarks>
  161. /// The callback occurs before the marker is assigned to the track.
  162. /// </remarks>
  163. public virtual void OnCreate(IMarker marker, IMarker clonedFrom)
  164. {
  165. }
  166. /// <summary>
  167. /// Draws additional overlays for a marker.
  168. /// </summary>
  169. /// <param name="marker">The marker to draw.</param>
  170. /// <param name="uiState">The visual state of the marker.</param>
  171. /// <param name="region">The on-screen area where the marker is being drawn.</param>
  172. /// <remarks>
  173. /// Notes:
  174. /// * It is only called during TimelineWindow's Repaint step.
  175. /// * If there are multiple markers on top of each other, only the topmost marker receives the DrawOverlay call.
  176. /// </remarks>
  177. public virtual void DrawOverlay(IMarker marker, MarkerUIStates uiState, MarkerOverlayRegion region)
  178. {
  179. }
  180. }
  181. }