EditableFieldDrawer.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. // FlexiMotion // https://kybernetik.com.au/flexi-motion // Copyright 2023 Kybernetik //
  3. #if UNITY_EDITOR
  4. using System;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using static Animancer.Editor.AnimancerGUI;
  8. namespace Animancer.Editor
  9. // namespace FlexiMotion.Editor
  10. {
  11. /// <summary>[Editor-Only] A <see cref="PropertyDrawer"/> which adds an "Edit" button to a field.</summary>
  12. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/EditableFieldDrawer
  13. /// https://kybernetik.com.au/flexi-motion/api/FlexiMotion.Editor/EditableFieldDrawer
  14. public abstract class EditableFieldDrawer : PropertyDrawer
  15. {
  16. /************************************************************************************************************************/
  17. /// <summary>The method to call when the "Edit" button is clicked.</summary>
  18. /// <remarks>Set this in a custom editor before drawing the attributed field then clear it afterwards.</remarks>
  19. public static event Action<SerializedProperty> OnEdit;
  20. /************************************************************************************************************************/
  21. /// <inheritdoc/>
  22. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  23. => EditorGUI.GetPropertyHeight(property, label);
  24. /************************************************************************************************************************/
  25. /// <inheritdoc/>
  26. public override void OnGUI(Rect area, SerializedProperty property, GUIContent label)
  27. {
  28. DrawEditableArea(area, property);
  29. EditorGUI.PropertyField(area, property, label, true);
  30. }
  31. /************************************************************************************************************************/
  32. private static GUIStyle
  33. _LeftAlignedButtonStyle;
  34. private static readonly GUIContent
  35. EditContent = new();
  36. private void DrawEditableArea(Rect area, SerializedProperty property)
  37. {
  38. if (property.hasMultipleDifferentValues)
  39. return;
  40. var label = EditContent;
  41. label.text = null;
  42. label.tooltip = null;
  43. GetEditButtonLabel(property, label);
  44. if (!string.IsNullOrEmpty(label.text))
  45. {
  46. area.xMin += EditorGUIUtility.labelWidth + StandardSpacing;
  47. area.height = LineHeight;
  48. if (OnEdit != null)
  49. {
  50. _LeftAlignedButtonStyle ??= new GUIStyle(EditorStyles.miniButton)
  51. {
  52. alignment = TextAnchor.MiddleLeft,
  53. padding = EditorStyles.miniPullDown.padding,
  54. };
  55. if (GUI.Button(area, label, _LeftAlignedButtonStyle))
  56. OnEdit(property);
  57. }
  58. else
  59. {
  60. GUI.Label(area, label);
  61. }
  62. }
  63. }
  64. /************************************************************************************************************************/
  65. /// <summary>Sets the `label` for the "Edit" button.</summary>
  66. public abstract void GetEditButtonLabel(SerializedProperty property, GUIContent label);
  67. /************************************************************************************************************************/
  68. }
  69. }
  70. #endif