EventSequenceDrawer.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System.Runtime.CompilerServices;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using Sequence = Animancer.AnimancerEvent.Sequence;
  7. namespace Animancer.Editor
  8. {
  9. /// <summary>[Editor-Only] Draws the Inspector GUI for a <see cref="Sequence"/>.</summary>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/EventSequenceDrawer
  11. public class EventSequenceDrawer
  12. {
  13. /************************************************************************************************************************/
  14. private static readonly ConditionalWeakTable<Sequence, EventSequenceDrawer>
  15. SequenceToDrawer = new();
  16. /// <summary>Returns a cached <see cref="EventSequenceDrawer"/> for the `events`.</summary>
  17. /// <remarks>
  18. /// The cache uses a <see cref="ConditionalWeakTable{TKey, TValue}"/> so it doesn't prevent the `events`
  19. /// from being garbage collected.
  20. /// </remarks>
  21. public static EventSequenceDrawer Get(Sequence events)
  22. {
  23. if (events == null)
  24. return null;
  25. if (!SequenceToDrawer.TryGetValue(events, out var drawer))
  26. SequenceToDrawer.Add(events, drawer = new());
  27. return drawer;
  28. }
  29. /************************************************************************************************************************/
  30. /// <summary>Calculates the number of vertical pixels required to draw the contents of the `events`.</summary>
  31. public float CalculateHeight(Sequence events)
  32. => AnimancerGUI.CalculateHeight(CalculateLineCount(events));
  33. /// <summary>Calculates the number of lines required to draw the contents of the `events`.</summary>
  34. public int CalculateLineCount(Sequence events)
  35. {
  36. if (events == null)
  37. return 0;
  38. if (!IsExpanded)
  39. return 1;
  40. var count = 1;
  41. for (int i = 0; i < events.Count; i++)
  42. count += DelegateGUI.CalculateLineCount(events[i].callback);
  43. count += DelegateGUI.CalculateLineCount(events.EndEvent.callback);
  44. return count;
  45. }
  46. /************************************************************************************************************************/
  47. /// <summary>Should the target's default be shown?</summary>
  48. public bool IsExpanded { get; set; }
  49. private static ConversionCache<int, string> _EventNumberCache;
  50. private static float _LogButtonWidth = float.NaN;
  51. /************************************************************************************************************************/
  52. /// <summary>Draws the GUI for the `events`.</summary>
  53. public void DoGUI(ref Rect area, Sequence events, GUIContent label)
  54. {
  55. using (var content = PooledGUIContent.Acquire(GetSummary(events)))
  56. DoGUI(ref area, events, label, content);
  57. }
  58. /// <summary>Draws the GUI for the `events`.</summary>
  59. public void DoGUI(ref Rect area, Sequence events, GUIContent label, GUIContent summary)
  60. {
  61. if (events == null)
  62. return;
  63. area.height = AnimancerGUI.LineHeight;
  64. var headerArea = area;
  65. const string LogLabel = "Log";
  66. if (float.IsNaN(_LogButtonWidth))
  67. _LogButtonWidth = EditorStyles.miniButton.CalculateWidth(LogLabel);
  68. var logArea = AnimancerGUI.StealFromRight(ref headerArea, _LogButtonWidth);
  69. if (GUI.Button(logArea, LogLabel, EditorStyles.miniButton))
  70. Debug.Log(events.DeepToString());
  71. IsExpanded = EditorGUI.Foldout(headerArea, IsExpanded, GUIContent.none, true);
  72. EditorGUI.LabelField(headerArea, label, summary);
  73. AnimancerGUI.NextVerticalArea(ref area);
  74. if (!IsExpanded)
  75. return;
  76. var enabled = GUI.enabled;
  77. GUI.enabled = false;
  78. EditorGUI.indentLevel++;
  79. for (int i = 0; i < events.Count; i++)
  80. {
  81. var name = events.GetName(i);
  82. if (string.IsNullOrEmpty(name))
  83. {
  84. _EventNumberCache ??= new(index => $"Event {index}");
  85. name = _EventNumberCache.Convert(i);
  86. }
  87. Draw(ref area, name, events[i]);
  88. }
  89. Draw(ref area, "End Event", events.EndEvent);
  90. EditorGUI.indentLevel--;
  91. GUI.enabled = enabled;
  92. }
  93. /************************************************************************************************************************/
  94. private static readonly ConversionCache<int, string>
  95. SummaryCache = new((count) => $"[{count}]"),
  96. EndSummaryCache = new((count) => $"[{count}] + End");
  97. /// <summary>Returns a summary of the `events`.</summary>
  98. public static string GetSummary(Sequence events)
  99. {
  100. var cache = float.IsNaN(events.NormalizedEndTime) && AnimancerEvent.IsNullOrDummy(events.OnEnd)
  101. ? SummaryCache
  102. : EndSummaryCache;
  103. return cache.Convert(events.Count);
  104. }
  105. /************************************************************************************************************************/
  106. private static ConversionCache<float, string> _EventTimeCache;
  107. /// <summary>Draws the GUI for the `animancerEvent`.</summary>
  108. public static void Draw(ref Rect area, string name, AnimancerEvent animancerEvent)
  109. {
  110. _EventTimeCache ??= new((time)
  111. => float.IsNaN(time) ? "Time = Auto" : $"Time = {time.ToStringCached()}x");
  112. var timeText = _EventTimeCache.Convert(animancerEvent.normalizedTime);
  113. using (var label = PooledGUIContent.Acquire(name))
  114. using (var value = PooledGUIContent.Acquire(timeText))
  115. DelegateGUI.DoGUI(ref area, label, animancerEvent.callback, value);
  116. }
  117. /************************************************************************************************************************/
  118. }
  119. }
  120. #endif