ITransitionDetailed.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer
  4. {
  5. /// <summary>An <see cref="ITransition"/> with some additional details (mainly for the Unity Editor GUI).</summary>
  6. /// <remarks>
  7. /// <strong>Documentation:</strong>
  8. /// <see href="https://kybernetik.com.au/animancer/docs/manual/transitions">
  9. /// Transitions</see>
  10. /// </remarks>
  11. /// https://kybernetik.com.au/animancer/api/Animancer/ITransitionDetailed
  12. ///
  13. public interface ITransitionDetailed : ITransition
  14. {
  15. /************************************************************************************************************************/
  16. /// <summary>Can this transition create a valid <see cref="AnimancerState"/>?</summary>
  17. bool IsValid { get; }
  18. /// <summary>What will the value of <see cref="AnimancerState.IsLooping"/> be for the created state?</summary>
  19. bool IsLooping { get; }
  20. /// <summary>The <see cref="AnimancerState.NormalizedTime"/> to start the animation at.</summary>
  21. /// <remarks><see cref="float.NaN"/> allows the animation to continue from its current time.</remarks>
  22. float NormalizedStartTime { get; set; }
  23. /// <summary>The maximum amount of time the animation is expected to take (in seconds).</summary>
  24. /// <remarks>The actual duration can vary in states like <see cref="ManualMixerState"/>.</remarks>
  25. float MaximumDuration { get; }
  26. /// <summary>The <see cref="AnimancerNodeBase.Speed"/> to play the animation at.</summary>
  27. float Speed { get; set; }
  28. /************************************************************************************************************************/
  29. }
  30. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerUtilities
  31. public static partial class AnimancerUtilities
  32. {
  33. /************************************************************************************************************************/
  34. /// <summary>
  35. /// Is the `transition` not null and <see cref="ITransitionDetailed.IsValid"/>?
  36. /// </summary>
  37. public static bool IsValid(this ITransitionDetailed transition)
  38. => transition != null
  39. && transition.IsValid;
  40. /************************************************************************************************************************/
  41. /// <summary>Returns the <see cref="ITransitionDetailed.IsValid"/> with support for <see cref="IWrapper"/>.</summary>
  42. public static bool IsValid(this ITransition transition)
  43. {
  44. if (transition == null)
  45. return false;
  46. if (TryGetWrappedObject(transition, out ITransitionDetailed detailed))
  47. return detailed.IsValid;
  48. return true;
  49. }
  50. /************************************************************************************************************************/
  51. /// <summary>
  52. /// Returns the <see cref="ITransition.FadeDuration"/>
  53. /// or <see cref="float.NaN"/> if it's <c>null</c> or throws an exception.
  54. /// </summary>
  55. public static float TryGetFadeDuration(this ITransition transition)
  56. {
  57. if (transition == null)
  58. return float.NaN;
  59. try
  60. {
  61. return transition.FadeDuration;
  62. }
  63. catch
  64. {
  65. return float.NaN;
  66. }
  67. }
  68. /************************************************************************************************************************/
  69. /// <summary>Outputs the <see cref="Motion.isLooping"/> or <see cref="ITransitionDetailed.IsLooping"/>.</summary>
  70. /// <remarks>Returns false if the `motionOrTransition` is null or an unsupported type.</remarks>
  71. public static bool TryGetIsLooping(object motionOrTransition, out bool isLooping)
  72. {
  73. if (motionOrTransition is Motion motion)
  74. {
  75. if (motion != null)
  76. {
  77. isLooping = motion.isLooping;
  78. return true;
  79. }
  80. }
  81. else if (TryGetWrappedObject(motionOrTransition, out ITransitionDetailed transition))
  82. {
  83. isLooping = transition.IsLooping;
  84. return true;
  85. }
  86. isLooping = false;
  87. return false;
  88. }
  89. /************************************************************************************************************************/
  90. /// <summary>Outputs the <see cref="AnimationClip.length"/> or <see cref="ITransitionDetailed.MaximumDuration"/>.</summary>
  91. /// <remarks>Returns false if the `motionOrTransition` is null or an unsupported type.</remarks>
  92. public static bool TryGetLength(object motionOrTransition, out float length)
  93. {
  94. if (motionOrTransition is AnimationClip clip)
  95. {
  96. if (clip != null)
  97. {
  98. length = clip.length;
  99. return true;
  100. }
  101. }
  102. else if (TryGetWrappedObject(motionOrTransition, out ITransitionDetailed transition))
  103. {
  104. length = transition.MaximumDuration;
  105. return true;
  106. }
  107. length = 0;
  108. return false;
  109. }
  110. /************************************************************************************************************************/
  111. /// <summary>
  112. /// Tries to calculate the amount of time (in seconds)
  113. /// from the <see cref="ITransitionDetailed.NormalizedStartTime"/>
  114. /// too the <see cref="AnimancerEvent.Sequence.NormalizedEndTime"/>,
  115. /// including the <see cref="ITransitionDetailed.Speed"/>
  116. /// </summary>
  117. public static bool TryCalculateDuration(object transition, out float duration)
  118. {
  119. if (!TryGetWrappedObject(transition, out ITransitionDetailed detailed))
  120. {
  121. duration = 0;
  122. return false;
  123. }
  124. var speed = detailed.Speed;
  125. duration = detailed.MaximumDuration;
  126. var normalizedStartTime = detailed.NormalizedStartTime;
  127. if (!float.IsNaN(normalizedStartTime))
  128. duration *= 1 - normalizedStartTime;
  129. if (TryGetWrappedObject(transition, out ITransitionWithEvents events))
  130. {
  131. var normalizedEndTime = events.Events.NormalizedEndTime;
  132. if (!float.IsNaN(normalizedEndTime))
  133. duration *= normalizedEndTime;
  134. }
  135. if (speed.IsFinite() && speed != 0)
  136. duration /= speed;
  137. return true;
  138. }
  139. /************************************************************************************************************************/
  140. }
  141. }