forked from Yoooi0/vam-curve-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONStorableAnimationCurve.cs
115 lines (95 loc) · 4.11 KB
/
JSONStorableAnimationCurve.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using SimpleJSON;
using UnityEngine;
namespace CurveEditor
{
public class JSONStorableAnimationCurve : JSONStorableParam, IStorableAnimationCurve
{
public delegate void AnimationCurveUpdatedCallback(AnimationCurve val);
private AnimationCurve _val;
private Keyframe[] _defaultVal = new[]
{
new Keyframe(0, 0),
new Keyframe(1, 1)
};
public AnimationCurve val
{
get { return _val; }
set { _val = value; NotifyUpdated(); }
}
private readonly AnimationCurveUpdatedCallback _updatedCallbackFunction;
public JSONStorableAnimationCurve(string paramName)
: this(paramName, new AnimationCurve(), null) { }
public JSONStorableAnimationCurve(string paramName, AnimationCurveUpdatedCallback callback)
: this(paramName, new AnimationCurve(), callback) { }
public JSONStorableAnimationCurve(string paramName, AnimationCurve curve)
: this(paramName, curve, null) { }
public JSONStorableAnimationCurve(string paramName, AnimationCurve curve, AnimationCurveUpdatedCallback callback)
{
name = paramName;
val = curve;
_updatedCallbackFunction = callback;
}
public void NotifyUpdated() => _updatedCallbackFunction?.Invoke(val);
public override bool StoreJSON(JSONClass jc, bool includePhysical = true, bool includeAppearance = true, bool forceStore = false)
{
var jcCurve = new JSONClass();
var jcKeyframes = new JSONArray();
for (var i = 0; i < val.length; i++)
{
var keyframe = val[i];
var jcKeyframe = new JSONClass();
jcKeyframe["time"].AsFloat = keyframe.time;
jcKeyframe["value"].AsFloat = keyframe.value;
jcKeyframe["inTangent"].AsFloat = keyframe.inTangent;
jcKeyframe["outTangent"].AsFloat = keyframe.outTangent;
jcKeyframe["inWeight"].AsFloat = keyframe.inWeight;
jcKeyframe["outWeight"].AsFloat = keyframe.outWeight;
jcKeyframe["weightedMode"].AsInt = (int)keyframe.weightedMode;
jcKeyframes.Add(jcKeyframe);
}
jcCurve["keyframes"] = jcKeyframes;
jc[name] = jcCurve;
return true;
}
public override void RestoreFromJSON(JSONClass jc, bool restorePhysical = true, bool restoreAppearance = true, bool setMissingToDefault = true)
{
if (jc[name] != null)
{
while (val.length > 0)
val.RemoveKey(0);
var jcCurve = jc[name];
var jcKeyframes = jcCurve["keyframes"].AsArray;
for (var i = 0; i < jcKeyframes.Count; i++)
{
var jcKeyframe = jcKeyframes[i];
var key = new Keyframe(
jcKeyframe["time"].AsFloat,
jcKeyframe["value"].AsFloat,
jcKeyframe["inTangent"].AsFloat,
jcKeyframe["outTangent"].AsFloat,
jcKeyframe["inWeight"].AsFloat,
jcKeyframe["outWeight"].AsFloat)
{
weightedMode = (WeightedMode)jcKeyframe["weightedMode"].AsInt
};
val.AddKey(key);
}
}
else if (setMissingToDefault)
{
SetValToDefault();
}
NotifyUpdated();
}
public override void LateRestoreFromJSON(JSONClass jc, bool restorePhysical = true, bool restoreAppearance = true, bool setMissingToDefault = true)
=> RestoreFromJSON(jc, restorePhysical, restoreAppearance, setMissingToDefault);
public override void SetValToDefault()
{
while (val.length > 0)
val.RemoveKey(0);
val.keys = _defaultVal;
NotifyUpdated();
}
public override void SetDefaultFromCurrent() => _defaultVal = val.keys;
}
}