MixerTransitionDrawer.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR && UNITY_IMGUI
  3. using System;
  4. using UnityEditor;
  5. using UnityEditorInternal;
  6. using UnityEngine;
  7. using static Animancer.Editor.AnimancerGUI;
  8. namespace Animancer.Editor
  9. {
  10. /// <summary>[Editor-Only] Draws the Inspector GUI for a <see cref="MixerTransition{TMixer, TParameter}"/>.</summary>
  11. /// <remarks>
  12. /// <strong>Documentation:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/manual/transitions">
  14. /// Transitions</see> and
  15. /// <see href="https://kybernetik.com.au/animancer/docs/manual/blending/mixers">
  16. /// Mixers</see>
  17. /// </remarks>
  18. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/MixerTransitionDrawer
  19. public class MixerTransitionDrawer : ManualMixerTransitionDrawer
  20. {
  21. /************************************************************************************************************************/
  22. /// <summary>The number of horizontal pixels the "Threshold" label occupies.</summary>
  23. private readonly float ThresholdWidth;
  24. /************************************************************************************************************************/
  25. private static float _StandardThresholdWidth;
  26. /// <summary>
  27. /// The number of horizontal pixels the word "Threshold" occupies when drawn with the
  28. /// <see cref="EditorStyles.popup"/> style.
  29. /// </summary>
  30. protected static float StandardThresholdWidth
  31. {
  32. get
  33. {
  34. if (_StandardThresholdWidth == 0)
  35. _StandardThresholdWidth = AnimancerGUI.CalculateWidth(EditorStyles.popup, "Threshold");
  36. return _StandardThresholdWidth;
  37. }
  38. }
  39. /************************************************************************************************************************/
  40. /// <summary>
  41. /// Creates a new <see cref="MixerTransitionDrawer"/> using the default <see cref="StandardThresholdWidth"/>.
  42. /// </summary>
  43. public MixerTransitionDrawer()
  44. : this(StandardThresholdWidth)
  45. { }
  46. /// <summary>
  47. /// Creates a new <see cref="MixerTransitionDrawer"/> using a custom width for its threshold labels.
  48. /// </summary>
  49. protected MixerTransitionDrawer(float thresholdWidth)
  50. => ThresholdWidth = thresholdWidth;
  51. /************************************************************************************************************************/
  52. /// <summary>
  53. /// The serialized <see cref="MixerTransition{TMixer, TParameter}.Thresholds"/> of the
  54. /// <see cref="ManualMixerTransition.ManualMixerTransitionDrawer.CurrentProperty"/>.
  55. /// </summary>
  56. protected static SerializedProperty CurrentThresholds { get; private set; }
  57. /************************************************************************************************************************/
  58. /// <inheritdoc/>
  59. protected override void GatherSubProperties(SerializedProperty property)
  60. {
  61. base.GatherSubProperties(property);
  62. CurrentThresholds = property.FindPropertyRelative(MixerTransition2D.ThresholdsField);
  63. if (CurrentAnimations == null ||
  64. CurrentThresholds == null ||
  65. property.hasMultipleDifferentValues)
  66. return;
  67. var count = Math.Max(CurrentAnimations.arraySize, CurrentThresholds.arraySize);
  68. CurrentAnimations.arraySize = count;
  69. CurrentThresholds.arraySize = count;
  70. if (CurrentSpeeds != null &&
  71. CurrentSpeeds.arraySize != 0)
  72. CurrentSpeeds.arraySize = count;
  73. }
  74. /************************************************************************************************************************/
  75. /// <inheritdoc/>
  76. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  77. {
  78. var height = base.GetPropertyHeight(property, label);
  79. if (property.isExpanded)
  80. {
  81. if (CurrentThresholds != null)
  82. {
  83. height -= StandardSpacing +
  84. EditorGUI.GetPropertyHeight(CurrentThresholds, label);
  85. }
  86. }
  87. return height;
  88. }
  89. /************************************************************************************************************************/
  90. /// <inheritdoc/>
  91. protected override void DoChildPropertyGUI(
  92. ref Rect area,
  93. SerializedProperty rootProperty,
  94. SerializedProperty property,
  95. GUIContent label)
  96. {
  97. if (property.propertyPath.EndsWith($".{MixerTransition2D.ThresholdsField}"))
  98. return;
  99. base.DoChildPropertyGUI(ref area, rootProperty, property, label);
  100. }
  101. /************************************************************************************************************************/
  102. /// <summary>Splits the specified `area` into separate sections.</summary>
  103. protected void SplitListRect(
  104. Rect area,
  105. bool isHeader,
  106. out Rect animation,
  107. out Rect threshold,
  108. out Rect speed,
  109. out Rect sync)
  110. {
  111. SplitListRect(area, isHeader, out animation, out speed, out sync);
  112. if (TwoLineMode && !isHeader)
  113. {
  114. threshold = StealFromLeft(ref speed, ThresholdWidth, StandardSpacing);
  115. }
  116. else
  117. {
  118. threshold = animation;
  119. var xMin = threshold.xMin = EditorGUIUtility.labelWidth + IndentSize;
  120. animation.xMax = xMin - StandardSpacing;
  121. }
  122. }
  123. /************************************************************************************************************************/
  124. /// <inheritdoc/>
  125. protected override void DoChildListHeaderGUI(Rect area)
  126. {
  127. SplitListRect(
  128. area,
  129. true,
  130. out var animationArea,
  131. out var thresholdArea,
  132. out var speedArea,
  133. out var syncArea);
  134. DoAnimationHeaderGUI(animationArea);
  135. var attribute = AttributeCache<ThresholdLabelAttribute>.FindAttribute(CurrentThresholds);
  136. var text = attribute != null
  137. ? attribute.Label
  138. : "Threshold";
  139. using (var label = PooledGUIContent.Acquire(text,
  140. "The parameter values at which each child state will be fully active"))
  141. DoHeaderDropdownGUI(thresholdArea, CurrentThresholds, label, AddThresholdFunctionsToMenu);
  142. DoSpeedHeaderGUI(speedArea);
  143. DoSyncHeaderGUI(syncArea);
  144. }
  145. /************************************************************************************************************************/
  146. /// <inheritdoc/>
  147. protected override void DoElementGUI(
  148. Rect area,
  149. int index,
  150. SerializedProperty animation,
  151. SerializedProperty speed)
  152. {
  153. SplitListRect(
  154. area,
  155. false,
  156. out var animationArea,
  157. out var thresholdArea,
  158. out var speedArea,
  159. out var syncArea);
  160. DoAnimationField(animationArea, animation);
  161. DoThresholdGUI(thresholdArea, index);
  162. DoSpeedFieldGUI(speedArea, speed, index);
  163. DoSyncToggleGUI(syncArea, index);
  164. }
  165. /************************************************************************************************************************/
  166. /// <summary>Draws the GUI of the threshold at the specified `index`.</summary>
  167. protected virtual void DoThresholdGUI(Rect area, int index)
  168. {
  169. var threshold = CurrentThresholds.GetArrayElementAtIndex(index);
  170. EditorGUI.PropertyField(area, threshold, GUIContent.none);
  171. }
  172. /************************************************************************************************************************/
  173. /// <inheritdoc/>
  174. protected override void OnAddElement(int index)
  175. {
  176. base.OnAddElement(index);
  177. if (CurrentThresholds.arraySize > 0)
  178. CurrentThresholds.InsertArrayElementAtIndex(index);
  179. }
  180. /************************************************************************************************************************/
  181. /// <inheritdoc/>
  182. protected override void OnRemoveElement(ReorderableList list)
  183. {
  184. base.OnRemoveElement(list);
  185. Serialization.RemoveArrayElement(CurrentThresholds, list.index);
  186. }
  187. /************************************************************************************************************************/
  188. /// <inheritdoc/>
  189. protected override void ResizeList(int size)
  190. {
  191. base.ResizeList(size);
  192. CurrentThresholds.arraySize = size;
  193. }
  194. /************************************************************************************************************************/
  195. /// <inheritdoc/>
  196. protected override void OnReorderList(ReorderableList list, int oldIndex, int newIndex)
  197. {
  198. base.OnReorderList(list, oldIndex, newIndex);
  199. CurrentThresholds.MoveArrayElement(oldIndex, newIndex);
  200. }
  201. /************************************************************************************************************************/
  202. /// <summary>Adds functions to the `menu` relating to the thresholds.</summary>
  203. protected virtual void AddThresholdFunctionsToMenu(GenericMenu menu) { }
  204. /************************************************************************************************************************/
  205. }
  206. }
  207. #endif