// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // using System.Collections.Generic; namespace Animancer.TransitionLibraries { /// /// An and a dictionary to modify it based on the previous state. /// /// /// Documentation: /// /// Transition Libraries /// /// https://kybernetik.com.au/animancer/api/Animancer.TransitionLibraries/TransitionModifierGroup public class TransitionModifierGroup : ICloneable, ICopyable { /************************************************************************************************************************/ /// The index at which this group was added to its . public readonly int Index; /************************************************************************************************************************/ private ITransition _Transition; /// The target transition of this group. /// Can't be null. public ITransition Transition { get => _Transition; set { AnimancerUtilities.Assert( value != null, $"{nameof(TransitionModifierGroup)}.{nameof(Transition)} can't be null."); _Transition = value; } } /************************************************************************************************************************/ /// /// Custom s to use when playing the /// depending on the of the source state it is coming from. /// /// This is null by default until adds something. public Dictionary FromKeyToFadeDuration { get; set; } /************************************************************************************************************************/ /// Creates a new . public TransitionModifierGroup( int index, ITransition transition) { Index = index; Transition = transition; } /************************************************************************************************************************/ /// Sets the `fadeDuration` to use when transitioning from `from` to the . public void SetFadeDuration(object from, float fadeDuration) { FromKeyToFadeDuration ??= new(); FromKeyToFadeDuration[from] = fadeDuration; } /// Removes the fade duration modifier set for transitioning from `from` to the . public void ResetFadeDuration(object from) => FromKeyToFadeDuration?.Remove(from); /************************************************************************************************************************/ /// Returns the fade duration to use when transitioning from `from` to the . public float GetFadeDuration(object from) => FromKeyToFadeDuration != null && FromKeyToFadeDuration.TryGetValue(AnimancerUtilities.GetRootKey(from), out var fadeDuration) ? fadeDuration : Transition.FadeDuration; /************************************************************************************************************************/ /// public TransitionModifierGroup Clone(CloneContext context) { var clone = new TransitionModifierGroup(Index, null); clone.CopyFrom(this); return clone; } /************************************************************************************************************************/ /// public void CopyFrom(TransitionModifierGroup copyFrom, CloneContext context) { Transition = copyFrom.Transition; if (copyFrom.FromKeyToFadeDuration == null) { FromKeyToFadeDuration?.Clear(); } else { FromKeyToFadeDuration ??= new(); foreach (var item in copyFrom.FromKeyToFadeDuration) FromKeyToFadeDuration[item.Key] = item.Value; } } /************************************************************************************************************************/ /// Describes this object. public override string ToString() => $"{nameof(TransitionModifierGroup)}([{Index}] {AnimancerUtilities.ToStringOrNull(Transition)})"; /************************************************************************************************************************/ } }