AnimancerEventInvokerEditor.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR && UNITY_IMGUI
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace Animancer.Editor
  6. {
  7. /// <summary>[Editor-Only] A custom Inspector for <see cref="AnimancerEvent.Invoker"/>s.</summary>
  8. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/AnimancerEventInvokerEditor
  9. ///
  10. [CustomEditor(typeof(AnimancerEvent.Invoker), true), CanEditMultipleObjects]
  11. public class AnimancerEventInvokerEditor : UnityEditor.Editor
  12. {
  13. /************************************************************************************************************************/
  14. private readonly Field[]
  15. Fields = new Field[100];
  16. private struct Field
  17. {
  18. public FastObjectField component;
  19. public FastObjectField state;
  20. }
  21. /************************************************************************************************************************/
  22. /// <inheritdoc/>
  23. public override void OnInspectorGUI()
  24. {
  25. if (target is Behaviour behaviour &&
  26. !behaviour.enabled)
  27. {
  28. EditorGUILayout.HelpBox(
  29. "This component is disabled so it won't invoke any events.",
  30. MessageType.Warning);
  31. }
  32. int index = 0;
  33. var isLayoutEvent = Event.current.type == EventType.Layout;
  34. var enumerator = AnimancerEvent.Invoker.EnumerateInvocationQueue();
  35. while (enumerator.MoveNext())
  36. {
  37. if (index < Fields.Length)
  38. {
  39. var invocation = enumerator.Current;
  40. if (invocation.State == null)
  41. {
  42. GUILayout.Label("State is Null");
  43. return;
  44. }
  45. ref var field = ref Fields[index];
  46. var area = AnimancerGUI.LayoutSingleLineRect();
  47. var labelArea = AnimancerGUI.StealFromLeft(ref area, EditorGUIUtility.labelWidth);
  48. labelArea = EditorGUI.IndentedRect(labelArea);
  49. if (isLayoutEvent)
  50. {
  51. field.component.SetValue(invocation.State.Graph?.Component);
  52. field.state.SetValue(invocation.State, invocation.State.GetPath());
  53. }
  54. field.component.Draw(labelArea);
  55. field.state.Draw(area);
  56. EditorGUI.indentLevel++;
  57. EditorGUILayout.LabelField("Event Name", invocation.Name);
  58. EditorGUILayout.LabelField("Normalized Time", invocation.Event.normalizedTime.ToString());
  59. NamedEventDictionaryDrawer.DoEventGUI("Direct Callback", invocation.Event.callback);
  60. NamedEventDictionaryDrawer.DoEventGUI("Bound Callback", invocation.GetBoundCallback());
  61. EditorGUI.indentLevel--;
  62. }
  63. index++;
  64. }
  65. if (index > Fields.Length)
  66. GUILayout.Label($"And {index - Fields.Length} more events.");
  67. Repaint();
  68. }
  69. /************************************************************************************************************************/
  70. }
  71. }
  72. #endif