DirectionalClipTransitionDrawer.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /// <inheritdoc/>
  8. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/DirectionalClipTransitionDrawer
  9. [CustomPropertyDrawer(typeof(DirectionalClipTransition), true)]
  10. public class DirectionalClipTransitionDrawer : TransitionDrawer
  11. {
  12. /************************************************************************************************************************/
  13. /// <summary>Creates a new <see cref="DirectionalClipTransitionDrawer"/>.</summary>
  14. public DirectionalClipTransitionDrawer()
  15. : base(DirectionalClipTransition.AnimationSetField)
  16. { }
  17. /************************************************************************************************************************/
  18. /// <inheritdoc/>
  19. protected override void DoChildPropertyGUI(
  20. ref Rect area,
  21. SerializedProperty rootProperty,
  22. SerializedProperty property,
  23. GUIContent label)
  24. {
  25. var width = area.width;
  26. var path = property.propertyPath;
  27. if (path.EndsWith($".{ClipTransition.ClipFieldName}"))
  28. {
  29. if (property.objectReferenceValue != null)
  30. {
  31. var removeArea = AnimancerGUI.StealFromRight(
  32. ref area, AnimancerGUI.LineHeight, AnimancerGUI.StandardSpacing);
  33. var removeContent = AnimancerIcons.ClearIcon(
  34. $"A {nameof(DirectionalClipTransition)}" +
  35. $" will get its Clip from the Animation Set at runtime" +
  36. $" so the Clip might as well be null until then.");
  37. if (GUI.Button(removeArea, removeContent, AnimancerGUI.NoPaddingButtonStyle))
  38. property.objectReferenceValue = null;
  39. }
  40. if (Context.Transition is DirectionalClipTransition directionalClipTransition &&
  41. directionalClipTransition.AnimationSet != null)
  42. {
  43. var dropdownArea = AnimancerGUI.StealFromRight(
  44. ref area, area.height, AnimancerGUI.StandardSpacing);
  45. if (GUI.Button(dropdownArea, GUIContent.none, EditorStyles.popup))
  46. PickAnimation(property, directionalClipTransition);
  47. }
  48. }
  49. base.DoChildPropertyGUI(ref area, rootProperty, property, label);
  50. area.width = width;
  51. }
  52. /************************************************************************************************************************/
  53. /// <summary>Shows a context menu to choose an <see cref="AnimationClip"/> from the `source`.</summary>
  54. private void PickAnimation(SerializedProperty property, object source)
  55. {
  56. var menu = new GenericMenu();
  57. using (SetPool<AnimationClip>.Instance.Acquire(out var clips))
  58. {
  59. clips.GatherFromSource(source);
  60. if (clips.Count == 0)
  61. return;
  62. property = property.Copy();
  63. foreach (var clip in clips)
  64. {
  65. menu.AddPropertyModifierFunction(property, clip.name, true, modify =>
  66. {
  67. modify.objectReferenceValue = clip;
  68. });
  69. }
  70. }
  71. menu.ShowAsContext();
  72. }
  73. /************************************************************************************************************************/
  74. }
  75. }
  76. #endif