Vector2MixerState.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace Animancer
  6. {
  7. /// <summary>[Pro-Only]
  8. /// An <see cref="AnimancerState"/> which blends an array of other states together
  9. /// based on a two dimensional parameter and thresholds.
  10. /// </summary>
  11. /// <remarks>
  12. /// <strong>Documentation:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/manual/blending/mixers">
  14. /// Mixers</see>
  15. /// </remarks>
  16. /// https://kybernetik.com.au/animancer/api/Animancer/Vector2MixerState
  17. ///
  18. public abstract class Vector2MixerState : MixerState<Vector2>,
  19. ICopyable<Vector2MixerState>
  20. {
  21. /************************************************************************************************************************/
  22. /// <summary><see cref="MixerState{TParameter}.Parameter"/>.x.</summary>
  23. public float ParameterX
  24. {
  25. get => Parameter.x;
  26. set => Parameter = new(value, Parameter.y);
  27. }
  28. /// <summary><see cref="MixerState{TParameter}.Parameter"/>.y.</summary>
  29. public float ParameterY
  30. {
  31. get => Parameter.y;
  32. set => Parameter = new(Parameter.x, value);
  33. }
  34. /************************************************************************************************************************/
  35. #region Parameter Binding
  36. /************************************************************************************************************************/
  37. private NodeParameter<float> _ParameterBindingX;
  38. /// <summary>
  39. /// If set, this will be used as a key in the <see cref="ParameterDictionary"/> so any
  40. /// changes to that parameter will automatically set the <see cref="ParameterX"/>.
  41. /// </summary>
  42. public StringReference ParameterNameX
  43. {
  44. get => _ParameterBindingX.Key;
  45. set
  46. {
  47. if (_ParameterBindingX.SetKeyCheckNeedsInitialize(value))
  48. _ParameterBindingX.Initialize(this, parameter => ParameterX = parameter);
  49. }
  50. }
  51. /************************************************************************************************************************/
  52. private NodeParameter<float> _ParameterBindingY;
  53. /// <summary>
  54. /// If set, this will be used as a key in the <see cref="ParameterDictionary"/> so any
  55. /// changes to that parameter will automatically set the <see cref="ParameterY"/>.
  56. /// </summary>
  57. public StringReference ParameterNameY
  58. {
  59. get => _ParameterBindingY.Key;
  60. set
  61. {
  62. if (_ParameterBindingY.SetKeyCheckNeedsInitialize(value))
  63. _ParameterBindingY.Initialize(this, parameter => ParameterY = parameter);
  64. }
  65. }
  66. /************************************************************************************************************************/
  67. /// <inheritdoc/>
  68. public override void SetGraph(AnimancerGraph graph)
  69. {
  70. if (Graph == graph)
  71. return;
  72. _ParameterBindingX.UnBindIfInitialized();
  73. _ParameterBindingY.UnBindIfInitialized();
  74. base.SetGraph(graph);
  75. _ParameterBindingX.BindIfInitialized();
  76. _ParameterBindingY.BindIfInitialized();
  77. }
  78. /************************************************************************************************************************/
  79. /// <inheritdoc/>
  80. public override void Destroy()
  81. {
  82. base.Destroy();
  83. _ParameterBindingX.UnBindIfInitialized();
  84. _ParameterBindingY.UnBindIfInitialized();
  85. }
  86. /************************************************************************************************************************/
  87. /// <inheritdoc/>
  88. public sealed override void CopyFrom(MixerState<Vector2> copyFrom, CloneContext context)
  89. => this.CopyFromBase(copyFrom, context);
  90. /// <inheritdoc/>
  91. public virtual void CopyFrom(Vector2MixerState copyFrom, CloneContext context)
  92. {
  93. base.CopyFrom(copyFrom, context);
  94. ParameterNameX = copyFrom.ParameterNameX;
  95. ParameterNameY = copyFrom.ParameterNameY;
  96. }
  97. /************************************************************************************************************************/
  98. #endregion
  99. /************************************************************************************************************************/
  100. /// <summary>Gets the lowest and highest threshold values on each axis.</summary>
  101. public void GetThresholdBounds(out Vector2 min, out Vector2 max)
  102. {
  103. var i = ChildCount - 1;
  104. min = max = GetThreshold(i);
  105. i--;
  106. for (; i >= 0; i--)
  107. {
  108. var threshold = GetThreshold(i);
  109. if (min.x > threshold.x)
  110. min.x = threshold.x;
  111. if (min.y > threshold.y)
  112. min.y = threshold.y;
  113. if (max.x < threshold.x)
  114. max.x = threshold.x;
  115. if (max.y < threshold.y)
  116. max.y = threshold.y;
  117. }
  118. }
  119. /// <inheritdoc/>
  120. public override Vector2 NormalizedParameter
  121. {
  122. get
  123. {
  124. GetThresholdBounds(out var min, out var max);
  125. var value = Parameter;
  126. return new(
  127. AnimancerUtilities.InverseLerpUnclamped(min.x, max.x, value.x),
  128. AnimancerUtilities.InverseLerpUnclamped(min.y, max.y, value.y));
  129. }
  130. set
  131. {
  132. GetThresholdBounds(out var min, out var max);
  133. Parameter = new(
  134. Mathf.LerpUnclamped(min.x, max.x, value.x),
  135. Mathf.LerpUnclamped(min.y, max.y, value.y));
  136. }
  137. }
  138. /************************************************************************************************************************/
  139. /// <inheritdoc/>
  140. public override string GetParameterError(Vector2 value)
  141. => value.IsFinite()
  142. ? null
  143. : $"value.x and value.y {Strings.MustBeFinite}";
  144. /************************************************************************************************************************/
  145. /// <inheritdoc/>
  146. public override void AppendParameter(StringBuilder text, Vector2 parameter)
  147. {
  148. text.Append('(')
  149. .Append(parameter.x)
  150. .Append(", ")
  151. .Append(parameter.y)
  152. .Append(')');
  153. }
  154. /************************************************************************************************************************/
  155. #region Inspector
  156. /************************************************************************************************************************/
  157. /// <inheritdoc/>
  158. public override void GetParameters(List<StateParameterDetails> parameters)
  159. {
  160. parameters.Add(new(
  161. "Parameter X",
  162. ParameterNameX,
  163. AnimatorControllerParameterType.Float,
  164. ParameterX));
  165. parameters.Add(new(
  166. "Parameter Y",
  167. ParameterNameY,
  168. AnimatorControllerParameterType.Float,
  169. ParameterY));
  170. }
  171. /// <inheritdoc/>
  172. public override void SetParameters(List<StateParameterDetails> parameters)
  173. {
  174. var parameter = parameters[0];
  175. ParameterNameX = parameter.name;
  176. ParameterX = (float)parameter.value;
  177. parameter = parameters[1];
  178. ParameterNameY = parameter.name;
  179. ParameterY = (float)parameter.value;
  180. }
  181. /************************************************************************************************************************/
  182. #endregion
  183. /************************************************************************************************************************/
  184. }
  185. }