CustomGUI.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using UnityEngine;
  4. namespace Animancer.Editor
  5. {
  6. /// <summary>[Editor-Only] Draws a custom GUI for an object.</summary>
  7. /// <remarks>
  8. /// Every non-abstract type implementing this interface must have at least one <see cref="CustomGUIAttribute"/>.
  9. /// </remarks>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/ICustomGUI
  11. ///
  12. public interface ICustomGUI
  13. {
  14. /************************************************************************************************************************/
  15. /// <summary>The optional label to draw in front of the field.</summary>
  16. GUIContent Label { get; set; }
  17. /// <summary>The target object for which this GUI will be drawn.</summary>
  18. object Value { get; set; }
  19. /// <summary>Draws the GUI for the <see cref="Value"/>.</summary>
  20. void DoGUI();
  21. /************************************************************************************************************************/
  22. }
  23. /// <summary>[Editor-Only] Draws a custom GUI for an object.</summary>
  24. /// <remarks>
  25. /// Every non-abstract type inheriting from this class must have at least one <see cref="CustomGUIAttribute"/>.
  26. /// </remarks>
  27. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/CustomGUI_1
  28. ///
  29. public abstract class CustomGUI<T> : ICustomGUI
  30. {
  31. /************************************************************************************************************************/
  32. /// <summary>The object for which this GUI will be drawn.</summary>
  33. public T Value { get; protected set; }
  34. /// <inheritdoc/>
  35. object ICustomGUI.Value
  36. {
  37. get => Value;
  38. set => Value = (T)value;
  39. }
  40. /************************************************************************************************************************/
  41. /// <inheritdoc/>
  42. public GUIContent Label { get; set; } = GUIContent.none;
  43. /************************************************************************************************************************/
  44. /// <inheritdoc/>
  45. public abstract void DoGUI();
  46. /************************************************************************************************************************/
  47. }
  48. /// <summary>[Editor-Only] Extension methods for <see cref="ICustomGUI"/>.</summary>
  49. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/CustomGUIExtensions
  50. ///
  51. public static class CustomGUIExtensions
  52. {
  53. /************************************************************************************************************************/
  54. /// <summary>Sets the <see cref="ICustomGUI.Label"/>.</summary>
  55. public static void SetLabel(
  56. this ICustomGUI customGUI,
  57. string text,
  58. string tooltip = null,
  59. Texture image = null)
  60. {
  61. var label = customGUI.Label;
  62. if (label == null || label == GUIContent.none)
  63. customGUI.Label = label = new(text);
  64. label.text = text;
  65. label.tooltip = tooltip;
  66. label.image = image;
  67. }
  68. /************************************************************************************************************************/
  69. }
  70. }
  71. #endif