NamedEventDictionaryDrawer.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR && UNITY_IMGUI
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Animancer.Editor
  7. {
  8. /// <summary>[Editor-Only] Draws the Inspector GUI for an <see cref="NamedEventDictionary"/>.</summary>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/NamedEventDictionaryDrawer
  10. ///
  11. public static class NamedEventDictionaryDrawer
  12. {
  13. /************************************************************************************************************************/
  14. private const string
  15. KeyPrefix = AnimancerGraphDrawer.KeyPrefix;
  16. private static readonly BoolPref
  17. AreEventsExpanded = new(KeyPrefix + nameof(AreEventsExpanded), false);
  18. /************************************************************************************************************************/
  19. /// <summary>Draws the <see cref="AnimancerGraph.Events"/>.</summary>
  20. public static void DoEventsGUI(AnimancerGraph graph)
  21. {
  22. if (!graph.HasEvents)
  23. return;
  24. EditorGUI.indentLevel++;
  25. var events = graph.Events;
  26. AreEventsExpanded.Value = AnimancerGUI.DoLabelFoldoutFieldGUI(
  27. "Events",
  28. events.Count.ToStringCached(),
  29. AreEventsExpanded);
  30. if (AreEventsExpanded)
  31. {
  32. EditorGUI.indentLevel++;
  33. var sortedEvents = ListPool.Acquire<StringReference>();
  34. sortedEvents.AddRange(events.Keys);
  35. sortedEvents.Sort();
  36. foreach (var item in sortedEvents)
  37. DoEventGUI(item, events[item]);
  38. ListPool.Release(sortedEvents);
  39. EditorGUI.indentLevel--;
  40. }
  41. EditorGUI.indentLevel--;
  42. }
  43. /************************************************************************************************************************/
  44. /// <summary>Draws an event.</summary>
  45. public static void DoEventGUI(string name, Action action)
  46. {
  47. var gui = CustomGUIFactory.GetOrCreateForObject(action);
  48. if (gui == null)
  49. {
  50. EditorGUILayout.LabelField(name, action.ToStringDetailed());
  51. return;
  52. }
  53. gui.SetLabel(name);
  54. gui.DoGUI();
  55. }
  56. /************************************************************************************************************************/
  57. }
  58. }
  59. #endif