ParametizedAnimancerStateDrawer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #if UNITY_EDITOR
  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="AnimancerState"/>.</summary>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/ParametizedAnimancerStateDrawer_1
  10. ///
  11. public abstract class ParametizedAnimancerStateDrawer<T> : AnimancerStateDrawer<T> where T : AnimancerState
  12. {
  13. /************************************************************************************************************************/
  14. /// <summary>The number of parameters being managed by the target state.</summary>
  15. public virtual int ParameterCount => 0;
  16. /// <summary>Returns the name of a parameter being managed by the target state.</summary>
  17. /// <exception cref="NotSupportedException">The target state doesn't manage any parameters.</exception>
  18. public virtual string GetParameterName(int index) => throw new NotSupportedException();
  19. /// <summary>Returns the type of a parameter being managed by the target state.</summary>
  20. /// <exception cref="NotSupportedException">The target state doesn't manage any parameters.</exception>
  21. public virtual AnimatorControllerParameterType GetParameterType(int index) => throw new NotSupportedException();
  22. /// <summary>Returns the value of a parameter being managed by the target state.</summary>
  23. /// <exception cref="NotSupportedException">The target state doesn't manage any parameters.</exception>
  24. public virtual object GetParameterValue(int index) => throw new NotSupportedException();
  25. /// <summary>Sets the value of a parameter being managed by the target state.</summary>
  26. /// <exception cref="NotSupportedException">The target state doesn't manage any parameters.</exception>
  27. public virtual void SetParameterValue(int index, object value) => throw new NotSupportedException();
  28. /************************************************************************************************************************/
  29. /// <summary>
  30. /// Creates a new <see cref="ParametizedAnimancerStateDrawer{T}"/> to manage the Inspector GUI for the `state`.
  31. /// </summary>
  32. protected ParametizedAnimancerStateDrawer(T state) : base(state) { }
  33. /************************************************************************************************************************/
  34. /// <inheritdoc/>
  35. protected override void DoDetailsGUI()
  36. {
  37. base.DoDetailsGUI();
  38. if (!IsExpanded)
  39. return;
  40. var count = ParameterCount;
  41. if (count <= 0)
  42. return;
  43. var labelWidth = EditorGUIUtility.labelWidth;
  44. EditorGUIUtility.labelWidth -= AnimancerGUI.IndentSize;
  45. for (int i = 0; i < count; i++)
  46. {
  47. var type = GetParameterType(i);
  48. if (type == 0)
  49. continue;
  50. var name = GetParameterName(i);
  51. var value = GetParameterValue(i);
  52. EditorGUI.BeginChangeCheck();
  53. var area = AnimancerGUI.LayoutSingleLineRect(AnimancerGUI.SpacingMode.Before);
  54. area = EditorGUI.IndentedRect(area);
  55. switch (type)
  56. {
  57. case AnimatorControllerParameterType.Float:
  58. value = EditorGUI.FloatField(area, name, (float)value);
  59. break;
  60. case AnimatorControllerParameterType.Int:
  61. value = EditorGUI.IntField(area, name, (int)value);
  62. break;
  63. case AnimatorControllerParameterType.Bool:
  64. value = EditorGUI.Toggle(area, name, (bool)value);
  65. break;
  66. case AnimatorControllerParameterType.Trigger:
  67. value = EditorGUI.Toggle(area, name, (bool)value, EditorStyles.radioButton);
  68. break;
  69. default:
  70. EditorGUI.LabelField(area, name, "Unsupported Type: " + type);
  71. break;
  72. }
  73. if (EditorGUI.EndChangeCheck())
  74. SetParameterValue(i, value);
  75. }
  76. EditorGUIUtility.labelWidth = labelWidth;
  77. }
  78. /************************************************************************************************************************/
  79. }
  80. }
  81. #endif