CustomFade.Delegate.cs 5.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using System;
  3. namespace Animancer
  4. {
  5. /// https://kybernetik.com.au/animancer/api/Animancer/CustomFade
  6. ///
  7. public partial class CustomFade
  8. {
  9. /************************************************************************************************************************/
  10. /// <summary>Modify the current fade to use the specified `calculateWeight` delegate.</summary>
  11. /// <example>See <see cref="CustomFade"/>.</example>
  12. /// <remarks>The `calculateWeight` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  13. public static void Apply(AnimancerComponent animancer, Func<float, float> calculateWeight)
  14. => Apply(animancer.States.Current, calculateWeight);
  15. /// <summary>Modify the current fade to use the specified `calculateWeight` delegate.</summary>
  16. /// <example>See <see cref="CustomFade"/>.</example>
  17. /// <remarks>The `calculateWeight` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  18. public static void Apply(AnimancerPlayable animancer, Func<float, float> calculateWeight)
  19. => Apply(animancer.States.Current, calculateWeight);
  20. /// <summary>Modify the current fade to use the specified `calculateWeight` delegate.</summary>
  21. /// <example>See <see cref="CustomFade"/>.</example>
  22. /// <remarks>The `calculateWeight` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  23. public static void Apply(AnimancerState state, Func<float, float> calculateWeight)
  24. => Delegate.Acquire(calculateWeight).Apply(state);
  25. /// <summary>Modify the current fade to use the specified `calculateWeight` delegate.</summary>
  26. /// <example>See <see cref="CustomFade"/>.</example>
  27. /// <remarks>The `calculateWeight` should follow the <see cref="OptionalWarning.CustomFadeBounds"/> guideline.</remarks>
  28. public static void Apply(AnimancerNode node, Func<float, float> calculateWeight)
  29. => Delegate.Acquire(calculateWeight).Apply(node);
  30. /************************************************************************************************************************/
  31. /// <summary>Modify the current fade to use the specified `function` to calculate the weight.</summary>
  32. /// <example>See <see cref="CustomFade"/>.</example>
  33. public static void Apply(AnimancerComponent animancer, Easing.Function function)
  34. => Apply(animancer.States.Current, function);
  35. /// <summary>Modify the current fade to use the specified `function` to calculate the weight.</summary>
  36. /// <example>See <see cref="CustomFade"/>.</example>
  37. public static void Apply(AnimancerPlayable animancer, Easing.Function function)
  38. => Apply(animancer.States.Current, function);
  39. /// <summary>Modify the current fade to use the specified `function` to calculate the weight.</summary>
  40. /// <example>See <see cref="CustomFade"/>.</example>
  41. public static void Apply(AnimancerState state, Easing.Function function)
  42. => Delegate.Acquire(function.GetDelegate()).Apply(state);
  43. /// <summary>Modify the current fade to use the specified `function` to calculate the weight.</summary>
  44. /// <example>See <see cref="CustomFade"/>.</example>
  45. public static void Apply(AnimancerNode node, Easing.Function function)
  46. => Delegate.Acquire(function.GetDelegate()).Apply(node);
  47. /************************************************************************************************************************/
  48. /// <summary>A <see cref="CustomFade"/> which uses a <see cref="Func{T, TResult}"/> to calculate the weight.</summary>
  49. /// <example>See <see cref="CustomFade"/>.</example>
  50. private class Delegate : CustomFade
  51. {
  52. /************************************************************************************************************************/
  53. private Func<float, float> _CalculateWeight;
  54. /************************************************************************************************************************/
  55. public static Delegate Acquire(Func<float, float> calculateWeight)
  56. {
  57. if (calculateWeight == null)
  58. {
  59. OptionalWarning.CustomFadeNotNull.Log($"{nameof(calculateWeight)} is null.");
  60. return null;
  61. }
  62. var fade = ObjectPool<Delegate>.Acquire();
  63. fade._CalculateWeight = calculateWeight;
  64. return fade;
  65. }
  66. /************************************************************************************************************************/
  67. protected override float CalculateWeight(float progress) => _CalculateWeight(progress);
  68. /************************************************************************************************************************/
  69. protected override void Release() => ObjectPool<Delegate>.Release(this);
  70. /************************************************************************************************************************/
  71. }
  72. }
  73. }