TransitionModifierGroup.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System.Collections.Generic;
  3. namespace Animancer.TransitionLibraries
  4. {
  5. /// <summary>
  6. /// An <see cref="ITransition"/> and a dictionary to modify it based on the previous state.
  7. /// </summary>
  8. /// <remarks>
  9. /// <strong>Documentation:</strong>
  10. /// <see href="https://kybernetik.com.au/animancer/docs/manual/transitions/libraries">
  11. /// Transition Libraries</see>
  12. /// </remarks>
  13. /// https://kybernetik.com.au/animancer/api/Animancer.TransitionLibraries/TransitionModifierGroup
  14. public class TransitionModifierGroup :
  15. ICloneable<TransitionModifierGroup>,
  16. ICopyable<TransitionModifierGroup>
  17. {
  18. /************************************************************************************************************************/
  19. /// <summary>The index at which this group was added to its <see cref="TransitionLibrary"/>.</summary>
  20. public readonly int Index;
  21. /************************************************************************************************************************/
  22. private ITransition _Transition;
  23. /// <summary>The target transition of this group.</summary>
  24. /// <remarks>Can't be <c>null</c>.</remarks>
  25. public ITransition Transition
  26. {
  27. get => _Transition;
  28. set
  29. {
  30. AnimancerUtilities.Assert(
  31. value != null,
  32. $"{nameof(TransitionModifierGroup)}.{nameof(Transition)} can't be null.");
  33. _Transition = value;
  34. }
  35. }
  36. /************************************************************************************************************************/
  37. /// <summary>
  38. /// Custom <see cref="ITransition.FadeDuration"/>s to use when playing the <see cref="Transition"/>
  39. /// depending on the <see cref="IHasKey.Key"/> of the source state it is coming from.
  40. /// </summary>
  41. /// <remarks>This is <c>null</c> by default until <see cref="SetFadeDuration"/> adds something.</remarks>
  42. public Dictionary<object, float> FromKeyToFadeDuration { get; set; }
  43. /************************************************************************************************************************/
  44. /// <summary>Creates a new <see cref="TransitionModifierGroup"/>.</summary>
  45. public TransitionModifierGroup(
  46. int index,
  47. ITransition transition)
  48. {
  49. Index = index;
  50. Transition = transition;
  51. }
  52. /************************************************************************************************************************/
  53. /// <summary>Sets the `fadeDuration` to use when transitioning from `from` to the <see cref="Transition"/>.</summary>
  54. public void SetFadeDuration(object from, float fadeDuration)
  55. {
  56. FromKeyToFadeDuration ??= new();
  57. FromKeyToFadeDuration[from] = fadeDuration;
  58. }
  59. /// <summary>Removes the fade duration modifier set for transitioning from `from` to the <see cref="Transition"/>.</summary>
  60. public void ResetFadeDuration(object from)
  61. => FromKeyToFadeDuration?.Remove(from);
  62. /************************************************************************************************************************/
  63. /// <summary>Returns the fade duration to use when transitioning from `from` to the <see cref="Transition"/>.</summary>
  64. public float GetFadeDuration(object from)
  65. => FromKeyToFadeDuration != null
  66. && FromKeyToFadeDuration.TryGetValue(AnimancerUtilities.GetRootKey(from), out var fadeDuration)
  67. ? fadeDuration
  68. : Transition.FadeDuration;
  69. /************************************************************************************************************************/
  70. /// <inheritdoc/>
  71. public TransitionModifierGroup Clone(CloneContext context)
  72. {
  73. var clone = new TransitionModifierGroup(Index, null);
  74. clone.CopyFrom(this);
  75. return clone;
  76. }
  77. /************************************************************************************************************************/
  78. /// <inheritdoc/>
  79. public void CopyFrom(TransitionModifierGroup copyFrom, CloneContext context)
  80. {
  81. Transition = copyFrom.Transition;
  82. if (copyFrom.FromKeyToFadeDuration == null)
  83. {
  84. FromKeyToFadeDuration?.Clear();
  85. }
  86. else
  87. {
  88. FromKeyToFadeDuration ??= new();
  89. foreach (var item in copyFrom.FromKeyToFadeDuration)
  90. FromKeyToFadeDuration[item.Key] = item.Value;
  91. }
  92. }
  93. /************************************************************************************************************************/
  94. /// <summary>Describes this object.</summary>
  95. public override string ToString()
  96. => $"{nameof(TransitionModifierGroup)}([{Index}] {AnimancerUtilities.ToStringOrNull(Transition)})";
  97. /************************************************************************************************************************/
  98. }
  99. }