// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // #if UNITY_EDITOR using System.Runtime.CompilerServices; using UnityEditor; using UnityEngine; using Sequence = Animancer.AnimancerEvent.Sequence; namespace Animancer.Editor { /// [Editor-Only] Draws the Inspector GUI for a . /// https://kybernetik.com.au/animancer/api/Animancer.Editor/EventSequenceDrawer public class EventSequenceDrawer { /************************************************************************************************************************/ private static readonly ConditionalWeakTable SequenceToDrawer = new(); /// Returns a cached for the `events`. /// /// The cache uses a so it doesn't prevent the `events` /// from being garbage collected. /// public static EventSequenceDrawer Get(Sequence events) { if (events == null) return null; if (!SequenceToDrawer.TryGetValue(events, out var drawer)) SequenceToDrawer.Add(events, drawer = new()); return drawer; } /************************************************************************************************************************/ /// Calculates the number of vertical pixels required to draw the contents of the `events`. public float CalculateHeight(Sequence events) => AnimancerGUI.CalculateHeight(CalculateLineCount(events)); /// Calculates the number of lines required to draw the contents of the `events`. public int CalculateLineCount(Sequence events) { if (events == null) return 0; if (!IsExpanded) return 1; var count = 1; for (int i = 0; i < events.Count; i++) count += DelegateGUI.CalculateLineCount(events[i].callback); count += DelegateGUI.CalculateLineCount(events.EndEvent.callback); return count; } /************************************************************************************************************************/ /// Should the target's default be shown? public bool IsExpanded { get; set; } private static ConversionCache _EventNumberCache; private static float _LogButtonWidth = float.NaN; /************************************************************************************************************************/ /// Draws the GUI for the `events`. public void DoGUI(ref Rect area, Sequence events, GUIContent label) { using (var content = PooledGUIContent.Acquire(GetSummary(events))) DoGUI(ref area, events, label, content); } /// Draws the GUI for the `events`. public void DoGUI(ref Rect area, Sequence events, GUIContent label, GUIContent summary) { if (events == null) return; area.height = AnimancerGUI.LineHeight; var headerArea = area; const string LogLabel = "Log"; if (float.IsNaN(_LogButtonWidth)) _LogButtonWidth = EditorStyles.miniButton.CalculateWidth(LogLabel); var logArea = AnimancerGUI.StealFromRight(ref headerArea, _LogButtonWidth); if (GUI.Button(logArea, LogLabel, EditorStyles.miniButton)) Debug.Log(events.DeepToString()); IsExpanded = EditorGUI.Foldout(headerArea, IsExpanded, GUIContent.none, true); EditorGUI.LabelField(headerArea, label, summary); AnimancerGUI.NextVerticalArea(ref area); if (!IsExpanded) return; var enabled = GUI.enabled; GUI.enabled = false; EditorGUI.indentLevel++; for (int i = 0; i < events.Count; i++) { var name = events.GetName(i); if (string.IsNullOrEmpty(name)) { _EventNumberCache ??= new(index => $"Event {index}"); name = _EventNumberCache.Convert(i); } Draw(ref area, name, events[i]); } Draw(ref area, "End Event", events.EndEvent); EditorGUI.indentLevel--; GUI.enabled = enabled; } /************************************************************************************************************************/ private static readonly ConversionCache SummaryCache = new((count) => $"[{count}]"), EndSummaryCache = new((count) => $"[{count}] + End"); /// Returns a summary of the `events`. public static string GetSummary(Sequence events) { var cache = float.IsNaN(events.NormalizedEndTime) && AnimancerEvent.IsNullOrDummy(events.OnEnd) ? SummaryCache : EndSummaryCache; return cache.Convert(events.Count); } /************************************************************************************************************************/ private static ConversionCache _EventTimeCache; /// Draws the GUI for the `animancerEvent`. public static void Draw(ref Rect area, string name, AnimancerEvent animancerEvent) { _EventTimeCache ??= new((time) => float.IsNaN(time) ? "Time = Auto" : $"Time = {time.ToStringCached()}x"); var timeText = _EventTimeCache.Convert(animancerEvent.normalizedTime); using (var label = PooledGUIContent.Acquire(name)) using (var value = PooledGUIContent.Acquire(timeText)) DelegateGUI.DoGUI(ref area, label, animancerEvent.callback, value); } /************************************************************************************************************************/ } } #endif