internal static Keyframe <T> NewInstance(JsonObject json, LottieComposition composition, float scale, IAnimatableValueFactory <T> valueFactory) { PointF cp1 = null; PointF cp2 = null; float startFrame = 0; var startValue = default(T); var endValue = default(T); IInterpolator interpolator = null; if (json.ContainsKey("t")) { startFrame = (float)json.GetNamedNumber("t", 0); var startValueJson = json.GetNamedArray("s", null); if (startValueJson != null) { startValue = valueFactory.ValueFromObject(startValueJson, scale); } var endValueJson = json.GetNamedArray("e", null); if (endValueJson != null) { endValue = valueFactory.ValueFromObject(endValueJson, scale); } var cp1Json = json.GetNamedObject("o", null); var cp2Json = json.GetNamedObject("i", null); if (cp1Json != null && cp2Json != null) { cp1 = JsonUtils.PointFromJsonObject(cp1Json, scale); cp2 = JsonUtils.PointFromJsonObject(cp2Json, scale); } var hold = (int)json.GetNamedNumber("h", 0) == 1; if (hold) { endValue = startValue; // TODO: create a HoldInterpolator so progress changes don't invalidate. interpolator = LinearInterpolator; } else if (cp1 != null) { cp1 = new PointF(MiscUtils.Clamp(cp1.X, -scale, scale), MiscUtils.Clamp(cp1.Y, -MaxCpValue, MaxCpValue)); cp2 = new PointF(MiscUtils.Clamp(cp2.X, -scale, scale), MiscUtils.Clamp(cp2.Y, -MaxCpValue, MaxCpValue)); interpolator = new PathInterpolator(cp1.X / scale, cp1.Y / scale, cp2.X / scale, cp2.Y / scale); } else { interpolator = LinearInterpolator; } } else { startValue = valueFactory.ValueFromObject(json, scale); endValue = startValue; } return(new Keyframe <T>(composition, startValue, endValue, interpolator, startFrame, null)); }
public PointF ValueFromObject(IJsonValue @object, float scale) { if (@object.ValueType == JsonValueType.Array) { return(JsonUtils.PointFromJsonArray(@object.GetArray(), scale)); } if (@object.ValueType == JsonValueType.Object) { return(JsonUtils.PointFromJsonObject(@object.GetObject(), scale)); } throw new System.ArgumentException("Unable to parse point from " + @object); }
internal static Keyframe <T> NewInstance(JsonObject json, LottieComposition composition, float scale, IAnimatableValueFactory <T> valueFactory) { Vector2? cp1 = null; Vector2? cp2 = null; float startFrame = 0; var startValue = default(T); var endValue = default(T); IInterpolator interpolator = null; if (json.ContainsKey("t")) { startFrame = (float)json.GetNamedNumber("t", 0); if (json.TryGetValue("s", out var startValueJson)) { startValue = valueFactory.ValueFromObject(startValueJson, scale); } if (json.TryGetValue("e", out var endValueJson)) { endValue = valueFactory.ValueFromObject(endValueJson, scale); } var cp1Json = json.GetNamedObject("o", null); var cp2Json = json.GetNamedObject("i", null); if (cp1Json != null && cp2Json != null) { cp1 = JsonUtils.PointFromJsonObject(cp1Json, scale); cp2 = JsonUtils.PointFromJsonObject(cp2Json, scale); } var hold = (int)json.GetNamedNumber("h", 0) == 1; if (hold) { endValue = startValue; // TODO: create a HoldInterpolator so progress changes don't invalidate. interpolator = LinearInterpolator; } else if (cp1 != null) { cp1 = new Vector2(MiscUtils.Clamp(cp1.Value.X, -scale, scale), MiscUtils.Clamp(cp1.Value.Y, -MaxCpValue, MaxCpValue)); cp2 = new Vector2(MiscUtils.Clamp(cp2.Value.X, -scale, scale), MiscUtils.Clamp(cp2.Value.Y, -MaxCpValue, MaxCpValue)); int hash = Utils.HashFor(cp1.Value.X, cp1.Value.Y, cp2.Value.X, cp2.Value.Y); if (PathInterpolatorCache.TryGetValue(hash, out var interpolatorRef) == false || interpolatorRef.TryGetTarget(out interpolator) == false) { interpolator = new PathInterpolator(cp1.Value.X / scale, cp1.Value.Y / scale, cp2.Value.X / scale, cp2.Value.Y / scale); PathInterpolatorCache[hash] = new WeakReference <IInterpolator>(interpolator); } } else { interpolator = LinearInterpolator; } } else { startValue = valueFactory.ValueFromObject(json, scale); endValue = startValue; } return(new Keyframe <T>(composition, startValue, endValue, interpolator, startFrame, null)); }