123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
- using System;
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- using UnityEditorInternal;
- #endif
- namespace Animancer
- {
-
-
- [Serializable]
- public abstract class MixerTransition<TMixer, TParameter> : ManualMixerTransition<TMixer>
- , ICopyable<MixerTransition<TMixer, TParameter>>
- where TMixer : MixerState<TParameter>
- {
-
- [SerializeField]
- private TParameter[] _Thresholds;
-
-
-
- public ref TParameter[] Thresholds => ref _Thresholds;
-
- public const string ThresholdsField = nameof(_Thresholds);
-
- [SerializeField]
- private TParameter _DefaultParameter;
-
-
-
- public ref TParameter DefaultParameter => ref _DefaultParameter;
-
- public const string DefaultParameterField = nameof(_DefaultParameter);
-
-
- public override void InitializeState()
- {
- base.InitializeState();
- State.SetThresholds(_Thresholds);
- State.Parameter = _DefaultParameter;
- }
-
-
- public virtual void CopyFrom(MixerTransition<TMixer, TParameter> copyFrom)
- {
- CopyFrom((ManualMixerTransition<TMixer>)copyFrom);
- if (copyFrom == null)
- {
- _DefaultParameter = default;
- _Thresholds = default;
- return;
- }
- _DefaultParameter = copyFrom._DefaultParameter;
- AnimancerUtilities.CopyExactArray(copyFrom._Thresholds, ref _Thresholds);
- }
-
- }
-
- #if UNITY_EDITOR
-
-
-
-
-
-
-
- public class MixerTransitionDrawer : ManualMixerTransition.Drawer
- {
-
-
- private readonly float ThresholdWidth;
-
- private static float _StandardThresholdWidth;
-
-
-
-
- protected static float StandardThresholdWidth
- {
- get
- {
- if (_StandardThresholdWidth == 0)
- _StandardThresholdWidth = Editor.AnimancerGUI.CalculateWidth(EditorStyles.popup, "Threshold");
- return _StandardThresholdWidth;
- }
- }
-
-
-
-
- public MixerTransitionDrawer() : this(StandardThresholdWidth) { }
-
-
-
- protected MixerTransitionDrawer(float thresholdWidth) => ThresholdWidth = thresholdWidth;
-
-
-
-
-
- protected static SerializedProperty CurrentThresholds { get; private set; }
-
-
- protected override void GatherSubProperties(SerializedProperty property)
- {
- base.GatherSubProperties(property);
- CurrentThresholds = property.FindPropertyRelative(MixerTransition2D.ThresholdsField);
- if (CurrentAnimations == null ||
- CurrentThresholds == null ||
- property.hasMultipleDifferentValues)
- return;
- var count = Math.Max(CurrentAnimations.arraySize, CurrentThresholds.arraySize);
- CurrentAnimations.arraySize = count;
- CurrentThresholds.arraySize = count;
- if (CurrentSpeeds != null &&
- CurrentSpeeds.arraySize != 0)
- CurrentSpeeds.arraySize = count;
- }
-
-
- public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
- {
- var height = base.GetPropertyHeight(property, label);
- if (property.isExpanded)
- {
- if (CurrentThresholds != null)
- height -= Editor.AnimancerGUI.StandardSpacing + EditorGUI.GetPropertyHeight(CurrentThresholds, label);
- }
- return height;
- }
-
-
- protected override void DoChildPropertyGUI(ref Rect area, SerializedProperty rootProperty, SerializedProperty property, GUIContent label)
- {
- if (property.propertyPath.EndsWith($".{MixerTransition2D.ThresholdsField}"))
- return;
- base.DoChildPropertyGUI(ref area, rootProperty, property, label);
- }
-
-
- protected void SplitListRect(Rect area, bool isHeader, out Rect animation, out Rect threshold, out Rect speed, out Rect sync)
- {
- SplitListRect(area, isHeader, out animation, out speed, out sync);
- threshold = animation;
- var xMin = threshold.xMin = EditorGUIUtility.labelWidth + Editor.AnimancerGUI.IndentSize;
- animation.xMax = xMin - Editor.AnimancerGUI.StandardSpacing;
- }
-
-
- protected override void DoChildListHeaderGUI(Rect area)
- {
- SplitListRect(area, true, out var animationArea, out var thresholdArea, out var speedArea, out var syncArea);
- DoAnimationHeaderGUI(animationArea);
- var attribute = Editor.AttributeCache<ThresholdLabelAttribute>.FindAttribute(CurrentThresholds);
- var text = attribute != null ? attribute.Label : "Threshold";
- using (ObjectPool.Disposable.AcquireContent(out var label, text,
- "The parameter values at which each child state will be fully active"))
- DoHeaderDropdownGUI(thresholdArea, CurrentThresholds, label, AddThresholdFunctionsToMenu);
- DoSpeedHeaderGUI(speedArea);
- DoSyncHeaderGUI(syncArea);
- }
-
-
- protected override void DoElementGUI(Rect area, int index,
- SerializedProperty clip, SerializedProperty speed)
- {
- SplitListRect(area, false, out var animationArea, out var thresholdArea, out var speedArea, out var syncArea);
- DoElementGUI(animationArea, speedArea, syncArea, index, clip, speed);
- DoThresholdGUI(thresholdArea, index);
- }
-
-
- protected virtual void DoThresholdGUI(Rect area, int index)
- {
- var threshold = CurrentThresholds.GetArrayElementAtIndex(index);
- EditorGUI.PropertyField(area, threshold, GUIContent.none);
- }
-
-
- protected override void OnAddElement(int index)
- {
- base.OnAddElement(index);
- if (CurrentThresholds.arraySize > 0)
- CurrentThresholds.InsertArrayElementAtIndex(index);
- }
-
-
- protected override void OnRemoveElement(ReorderableList list)
- {
- base.OnRemoveElement(list);
- Editor.Serialization.RemoveArrayElement(CurrentThresholds, list.index);
- }
-
-
- protected override void ResizeList(int size)
- {
- base.ResizeList(size);
- CurrentThresholds.arraySize = size;
- }
-
-
- protected override void OnReorderList(ReorderableList list, int oldIndex, int newIndex)
- {
- base.OnReorderList(list, oldIndex, newIndex);
- CurrentThresholds.MoveArrayElement(oldIndex, newIndex);
- }
-
-
- protected virtual void AddThresholdFunctionsToMenu(GenericMenu menu) { }
-
- }
- #endif
- }
|