Skip to content

Commit

Permalink
Store and restore float param range
Browse files Browse the repository at this point in the history
  • Loading branch information
acidbubbles committed Oct 16, 2021
1 parent a80f97e commit 93849d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/AtomAnimations/Animatables/AnimatablesRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class AnimatablesRegistry : IDisposable

public IList<JSONStorableFloatRef> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -53,22 +65,18 @@ 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;
_lastAvailableCheck = Time.frameCount;
return false;
}

public bool TryBind(bool silent)
private bool TryBind(bool silent)
{
if (SuperController.singleton.isLoading) return false;
var storable = _atom.GetStorableByID(storableId);
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion src/AtomAnimations/Serialization/AtomAnimationSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 93849d0

Please sign in to comment.