123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- using System;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Animations;
- namespace Animancer
- {
-
-
-
-
-
-
-
-
- public abstract class MixerState<TParameter> : ManualMixerState
- {
-
- #region Properties
-
-
- private TParameter[] _Thresholds = Array.Empty<TParameter>();
-
- private TParameter _Parameter;
-
-
-
-
-
-
- public TParameter Parameter
- {
- get => _Parameter;
- set
- {
- #if UNITY_ASSERTIONS
- var error = GetParameterError(value);
- if (error != null)
- throw new ArgumentOutOfRangeException(nameof(value), error);
- #endif
- _Parameter = value;
- WeightsAreDirty = true;
- RequireUpdate();
- }
- }
-
-
-
-
- public abstract string GetParameterError(TParameter parameter);
-
- #endregion
-
- #region Thresholds
-
-
-
-
-
- public bool HasThresholds => _Thresholds.Length >= ChildCount;
-
-
-
-
- public TParameter GetThreshold(int index) => _Thresholds[index];
-
-
-
-
- public void SetThreshold(int index, TParameter threshold)
- {
- _Thresholds[index] = threshold;
- OnThresholdsChanged();
- }
-
-
-
-
-
-
-
- public void SetThresholds(params TParameter[] thresholds)
- {
- if (thresholds.Length != ChildCount)
- throw new ArgumentOutOfRangeException(nameof(thresholds),
- $"Threshold count ({thresholds.Length}) doesn't match child count ({ChildCount}).");
- _Thresholds = thresholds;
- OnThresholdsChanged();
- }
-
-
-
-
-
- public bool ValidateThresholdCount()
- {
- var count = ChildCount;
- if (_Thresholds.Length != count)
- {
- _Thresholds = new TParameter[count];
- return true;
- }
- else return false;
- }
-
-
-
-
-
- public virtual void OnThresholdsChanged()
- {
- WeightsAreDirty = true;
- RequireUpdate();
- }
-
-
-
-
-
- public void CalculateThresholds(Func<AnimancerState, TParameter> calculate)
- {
- ValidateThresholdCount();
- for (int i = ChildCount - 1; i >= 0; i--)
- {
- var state = GetChild(i);
- if (state == null)
- continue;
- _Thresholds[i] = calculate(state);
- }
- OnThresholdsChanged();
- }
-
-
-
-
-
- public override void RecreatePlayable()
- {
- base.RecreatePlayable();
- WeightsAreDirty = true;
- RequireUpdate();
- }
-
- #endregion
-
- #region Initialisation
-
-
- public override void Initialize(int portCount)
- {
- base.Initialize(portCount);
- _Thresholds = new TParameter[portCount];
- OnThresholdsChanged();
- }
-
-
-
-
-
-
-
-
-
- public void Initialize(AnimationClip[] clips, TParameter[] thresholds)
- {
- Initialize(clips);
- _Thresholds = thresholds;
- OnThresholdsChanged();
- }
-
-
-
-
- public void Initialize(AnimationClip[] clips, Func<AnimancerState, TParameter> calculateThreshold)
- {
- Initialize(clips);
- CalculateThresholds(calculateThreshold);
- }
-
-
-
-
-
-
- public ClipState CreateChild(int index, AnimationClip clip, TParameter threshold)
- {
- SetThreshold(index, threshold);
- return CreateChild(index, clip);
- }
-
-
-
-
- public AnimancerState CreateChild(int index, Animancer.ITransition transition, TParameter threshold)
- {
- SetThreshold(index, threshold);
- return CreateChild(index, transition);
- }
-
-
- public void SetChild(int index, AnimancerState state, TParameter threshold)
- {
- SetChild(index, state);
- SetThreshold(index, threshold);
- }
-
- #endregion
-
- #region Descriptions
-
-
- public override string GetDisplayKey(AnimancerState state) => $"[{state.Index}] {_Thresholds[state.Index]}";
-
-
- protected override void AppendDetails(StringBuilder text, string separator)
- {
- text.Append(separator);
- text.Append($"{nameof(Parameter)}: ");
- AppendParameter(text, Parameter);
- text.Append(separator).Append("Thresholds: ");
- for (int i = 0; i < _Thresholds.Length; i++)
- {
- if (i > 0)
- text.Append(", ");
- AppendParameter(text, _Thresholds[i]);
- }
- base.AppendDetails(text, separator);
- }
-
-
- public virtual void AppendParameter(StringBuilder description, TParameter parameter)
- {
- description.Append(parameter);
- }
-
- #endregion
-
- }
- }
|