diff --git a/src/AtomAnimations/Animatables/AnimatablesRegistry.cs b/src/AtomAnimations/Animatables/AnimatablesRegistry.cs index 90b52fec..2496612f 100644 --- a/src/AtomAnimations/Animatables/AnimatablesRegistry.cs +++ b/src/AtomAnimations/Animatables/AnimatablesRegistry.cs @@ -15,11 +15,11 @@ public class AnimatablesRegistry : IDisposable public IList storableFloats => _storableFloats; - public JSONStorableFloatRef GetOrCreateStorableFloat(Atom atom, string storableId, string floatParamName) + public JSONStorableFloatRef GetOrCreateStorableFloat(Atom atom, string storableId, string floatParamName, float? assignMinValueOnBound = null, float? assignMaxValueOnBound = null) { var t = _storableFloats.FirstOrDefault(x => x.Targets(storableId, floatParamName)); if (t != null) return t; - t = new JSONStorableFloatRef(atom, storableId, floatParamName); + t = new JSONStorableFloatRef(atom, storableId, floatParamName, assignMinValueOnBound, assignMaxValueOnBound); _storableFloats.Add(t); RegisterAnimatableRef(t); return t; diff --git a/src/AtomAnimations/Animatables/JSONStorableFloats/JSONStorableFloatRef.cs b/src/AtomAnimations/Animatables/JSONStorableFloats/JSONStorableFloatRef.cs index 0fba2eea..9b0e0151 100644 --- a/src/AtomAnimations/Animatables/JSONStorableFloats/JSONStorableFloatRef.cs +++ b/src/AtomAnimations/Animatables/JSONStorableFloats/JSONStorableFloatRef.cs @@ -10,16 +10,28 @@ public class JSONStorableFloatRef : AnimatableRefBase private bool _available; private readonly Atom _atom; private int _lastAvailableCheck; + public float? assignMinValueOnBound {get; private set; } + public float? assignMaxValueOnBound {get; private set; } public readonly string storableId; public JSONStorable storable { get; private set; } public string floatParamName; public JSONStorableFloat floatParam { get; private set; } - public JSONStorableFloatRef(Atom atom, string storableId, string floatParamName) + public JSONStorableFloatRef(Atom atom, string storableId, string floatParamName, float? assignMinValueOnBound = null, float? assignMaxValueOnBound = null) { _atom = atom; this.storableId = storableId; this.floatParamName = floatParamName; + if (assignMinValueOnBound == 0 && assignMaxValueOnBound == 0) + { + this.assignMinValueOnBound = null; + this.assignMaxValueOnBound = null; + } + else + { + this.assignMinValueOnBound = assignMinValueOnBound; + this.assignMaxValueOnBound = assignMaxValueOnBound; + } } public JSONStorableFloatRef(JSONStorable storable, JSONStorableFloat floatParam) @@ -53,14 +65,10 @@ public bool EnsureAvailable(bool silent = true) { if (_available) { - if (storable == null) - { - _available = false; - storable = null; - floatParam = null; - return false; - } - return true; + if (storable != null) return true; + _available = false; + storable = null; + floatParam = null; } if (Time.frameCount == _lastAvailableCheck) return false; if (TryBind(silent)) return true; @@ -68,7 +76,7 @@ public bool EnsureAvailable(bool silent = true) return false; } - public bool TryBind(bool silent) + private bool TryBind(bool silent) { if (SuperController.singleton.isLoading) return false; var storable = _atom.GetStorableByID(storableId); @@ -102,6 +110,16 @@ public bool TryBind(bool silent) this.floatParam = floatParam; // May be replaced (might use alt name) floatParamName = floatParam.name; + if (assignMinValueOnBound != null) + { + floatParam.min = assignMinValueOnBound.Value; + assignMinValueOnBound = null; + } + if (assignMaxValueOnBound != null) + { + floatParam.max = assignMaxValueOnBound.Value; + assignMaxValueOnBound = null; + } _available = true; return true; } diff --git a/src/AtomAnimations/Serialization/AtomAnimationSerializer.cs b/src/AtomAnimations/Serialization/AtomAnimationSerializer.cs index 46105d3d..b95d5727 100644 --- a/src/AtomAnimations/Serialization/AtomAnimationSerializer.cs +++ b/src/AtomAnimations/Serialization/AtomAnimationSerializer.cs @@ -163,7 +163,13 @@ private void DeserializeClip(AtomAnimationClip clip, JSONClass clipJSON, Animata { var storableId = paramJSON["Storable"].Value; var floatParamName = paramJSON["Name"].Value; - var floatParamRef = targetsRegistry.GetOrCreateStorableFloat(_atom, storableId, floatParamName); + var floatParamRef = targetsRegistry.GetOrCreateStorableFloat( + _atom, + storableId, + floatParamName, + paramJSON.HasKey("Min") ? (float?)paramJSON["Min"].AsFloat : null, + paramJSON.HasKey("Max") ? (float?)paramJSON["Max"].AsFloat : null + ); var target = new JSONStorableFloatAnimationTarget(floatParamRef); var dirty = false; DeserializeCurve(target.value, paramJSON["Value"], ref dirty); @@ -462,6 +468,10 @@ private static void SerializeClip(AtomAnimationClip clip, JSONClass clipJSON) { "Name", target.animatableRef.floatParamName }, { "Value", SerializeCurve(target.value) } }; + var min = target.animatableRef.floatParam?.min ?? target.animatableRef.assignMinValueOnBound; + if (min != null) paramJSON["Min"] = min.Value.ToString(CultureInfo.InvariantCulture); + var max = target.animatableRef.floatParam?.max ?? target.animatableRef.assignMaxValueOnBound; + if (max != null) paramJSON["Max"] = max.Value.ToString(CultureInfo.InvariantCulture); floatParamsJSON.Add(paramJSON); } if(floatParamsJSON.Count > 0)