LinearMixerTransitionDrawer.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace Animancer.Editor
  8. {
  9. /// <inheritdoc/>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/LinearMixerTransitionDrawer
  11. [CustomPropertyDrawer(typeof(LinearMixerTransition), true)]
  12. public class LinearMixerTransitionDrawer : MixerTransitionDrawer
  13. {
  14. /************************************************************************************************************************/
  15. private static GUIContent _SortingErrorContent;
  16. private static GUIStyle _SortingErrorStyle;
  17. /// <inheritdoc/>
  18. protected override void DoThresholdGUI(Rect area, int index)
  19. {
  20. var color = GUI.color;
  21. var iconArea = default(Rect);
  22. if (index > 0)
  23. {
  24. var previousThreshold = CurrentThresholds.GetArrayElementAtIndex(index - 1);
  25. var currentThreshold = CurrentThresholds.GetArrayElementAtIndex(index);
  26. if (previousThreshold.floatValue >= currentThreshold.floatValue)
  27. {
  28. iconArea = AnimancerGUI.StealFromRight(
  29. ref area,
  30. area.height,
  31. AnimancerGUI.StandardSpacing);
  32. GUI.color = AnimancerGUI.ErrorFieldColor;
  33. }
  34. }
  35. base.DoThresholdGUI(area, index);
  36. if (iconArea != default)
  37. {
  38. _SortingErrorContent ??= new(AnimancerIcons.Error)
  39. {
  40. tooltip =
  41. "Linear Mixer Thresholds must always be unique" +
  42. " and sorted in ascending order (click to sort)"
  43. };
  44. _SortingErrorStyle ??= new(GUI.skin.label)
  45. {
  46. padding = new(),
  47. };
  48. if (GUI.Button(iconArea, _SortingErrorContent, _SortingErrorStyle))
  49. {
  50. AnimancerGUI.Deselect();
  51. Serialization.RecordUndo(Context.Property);
  52. ((LinearMixerTransition)Context.Transition).SortByThresholds();
  53. }
  54. }
  55. GUI.color = color;
  56. }
  57. /************************************************************************************************************************/
  58. /// <inheritdoc/>
  59. protected override void AddThresholdFunctionsToMenu(GenericMenu menu)
  60. {
  61. const string EvenlySpaced = "Evenly Spaced";
  62. var count = CurrentThresholds.arraySize;
  63. if (count <= 1)
  64. {
  65. menu.AddDisabledItem(new(EvenlySpaced));
  66. }
  67. else
  68. {
  69. var first = CurrentThresholds.GetArrayElementAtIndex(0).floatValue;
  70. var last = CurrentThresholds.GetArrayElementAtIndex(count - 1).floatValue;
  71. if (last == first)
  72. last++;
  73. AddPropertyModifierFunction(menu, $"{EvenlySpaced} ({first} to {last})", _ =>
  74. {
  75. for (int i = 0; i < count; i++)
  76. {
  77. CurrentThresholds.GetArrayElementAtIndex(i).floatValue =
  78. Mathf.Lerp(first, last, i / (float)(count - 1));
  79. }
  80. });
  81. }
  82. AddCalculateThresholdsFunction(menu, "From Speed",
  83. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity)
  84. ? velocity.magnitude
  85. : float.NaN);
  86. AddCalculateThresholdsFunction(menu, "From Velocity X",
  87. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity)
  88. ? velocity.x
  89. : float.NaN);
  90. AddCalculateThresholdsFunction(menu, "From Velocity Y",
  91. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity)
  92. ? velocity.y
  93. : float.NaN);
  94. AddCalculateThresholdsFunction(menu, "From Velocity Z",
  95. (state, threshold) => AnimancerUtilities.TryGetAverageVelocity(state, out var velocity)
  96. ? velocity.z
  97. : float.NaN);
  98. AddCalculateThresholdsFunction(menu, "From Angular Speed (Rad)",
  99. (state, threshold) => AnimancerUtilities.TryGetAverageAngularSpeed(state, out var speed)
  100. ? speed
  101. : float.NaN);
  102. AddCalculateThresholdsFunction(menu, "From Angular Speed (Deg)",
  103. (state, threshold) => AnimancerUtilities.TryGetAverageAngularSpeed(state, out var speed)
  104. ? speed * Mathf.Rad2Deg
  105. : float.NaN);
  106. }
  107. /************************************************************************************************************************/
  108. private void AddCalculateThresholdsFunction(
  109. GenericMenu menu,
  110. string label,
  111. Func<Object, float, float> calculateThreshold)
  112. {
  113. AddPropertyModifierFunction(menu, label, (property) =>
  114. {
  115. var count = CurrentAnimations.arraySize;
  116. for (int i = 0; i < count; i++)
  117. {
  118. var state = CurrentAnimations.GetArrayElementAtIndex(i).objectReferenceValue;
  119. if (state == null)
  120. continue;
  121. var threshold = CurrentThresholds.GetArrayElementAtIndex(i);
  122. var value = calculateThreshold(state, threshold.floatValue);
  123. if (!float.IsNaN(value))
  124. threshold.floatValue = value;
  125. }
  126. });
  127. }
  128. /************************************************************************************************************************/
  129. }
  130. }
  131. #endif