Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
@@ -0,0 +1,5 @@
#include "NodeProperty.hpp"
namespace lottie {
}
@@ -0,0 +1,55 @@
#ifndef NodeProperty_hpp
#define NodeProperty_hpp
#include "Lottie/Public/Primitives/AnyValue.hpp"
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/Protocols/AnyNodeProperty.hpp"
#include "Lottie/Public/DynamicProperties/AnyValueProvider.hpp"
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/ValueContainer.hpp"
namespace lottie {
/// A node property that holds a reference to a T ValueProvider and a T ValueContainer.
template<typename T>
class NodeProperty: public AnyNodeProperty {
public:
NodeProperty(std::shared_ptr<ValueProvider<T>> provider) :
_valueProvider(provider),
//_originalValueProvider(provider),
_typedContainer(provider->value(0.0)) {
_typedContainer.setNeedsUpdate();
}
public:
virtual AnyValue::Type valueType() const override {
return AnyValueType<T>::type();
}
virtual T value() {
return _typedContainer.outputValue();
}
virtual bool needsUpdate(double frame) const override {
return _typedContainer.needsUpdate() || _valueProvider->hasUpdate(frame);
}
virtual void setProvider(std::shared_ptr<AnyValueProvider> provider) override {
/*if (provider->valueType() != valueType()) {
return;
}
_valueProvider = provider;
_typedContainer.setNeedsUpdate();*/
}
virtual void update(double frame) override {
_typedContainer.setValue(_valueProvider->value(frame), frame);
}
private:
ValueContainer<T> _typedContainer;
std::shared_ptr<ValueProvider<T>> _valueProvider;
//std::shared_ptr<AnyValueProvider> _originalValueProvider;
};
}
#endif /* NodeProperty_hpp */
@@ -0,0 +1,55 @@
//
// NodeProperty.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/30/19.
//
import CoreGraphics
import Foundation
/// A node property that holds a reference to a T ValueProvider and a T ValueContainer.
class NodeProperty<T>: AnyNodeProperty {
// MARK: Lifecycle
init(provider: AnyValueProvider) {
valueProvider = provider
originalValueProvider = valueProvider
typedContainer = ValueContainer<T>(provider.value(frame: 0) as! T)
typedContainer.setNeedsUpdate()
}
// MARK: Internal
var valueProvider: AnyValueProvider
var originalValueProvider: AnyValueProvider
var valueType: Any.Type { T.self }
var value: T {
typedContainer.outputValue
}
var valueContainer: AnyValueContainer {
typedContainer
}
func needsUpdate(frame: CGFloat) -> Bool {
valueContainer.needsUpdate || valueProvider.hasUpdate(frame: frame)
}
func setProvider(provider: AnyValueProvider) {
guard provider.valueType == valueType else { return }
valueProvider = provider
valueContainer.setNeedsUpdate()
}
func update(frame: CGFloat) {
typedContainer.setValue(valueProvider.value(frame: frame), forFrame: frame)
}
// MARK: Fileprivate
fileprivate var typedContainer: ValueContainer<T>
}
@@ -0,0 +1,5 @@
#include "AnyNodeProperty.hpp"
namespace lottie {
}
@@ -0,0 +1,33 @@
#ifndef AnyNodeProperty_hpp
#define AnyNodeProperty_hpp
#include "Lottie/Public/Primitives/AnyValue.hpp"
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/Protocols/AnyValueContainer.hpp"
#include "Lottie/Public/DynamicProperties/AnyValueProvider.hpp"
#include <memory>
namespace lottie {
/// A property of a node. The node property holds a provider and a container
class AnyNodeProperty {
public:
virtual ~AnyNodeProperty() = default;
public:
/// Returns true if the property needs to recompute its stored value
virtual bool needsUpdate(double frame) const = 0;
/// Updates the property for the frame
virtual void update(double frame) = 0;
/// The Type of the value provider
virtual AnyValue::Type valueType() const = 0;
/// Sets the value provider for the property.
virtual void setProvider(std::shared_ptr<AnyValueProvider> provider) = 0;
};
}
#endif /* AnyNodeProperty_hpp */
@@ -0,0 +1,50 @@
//
// AnyNodeProperty.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/30/19.
//
import CoreGraphics
import Foundation
// MARK: - AnyNodeProperty
/// A property of a node. The node property holds a provider and a container
protocol AnyNodeProperty {
/// Returns true if the property needs to recompute its stored value
func needsUpdate(frame: CGFloat) -> Bool
/// Updates the property for the frame
func update(frame: CGFloat)
/// The stored value container for the property
var valueContainer: AnyValueContainer { get }
/// The value provider for the property
var valueProvider: AnyValueProvider { get }
/// The original value provider for the property
var originalValueProvider: AnyValueProvider { get }
/// The Type of the value provider
var valueType: Any.Type { get }
/// Sets the value provider for the property.
func setProvider(provider: AnyValueProvider)
}
extension AnyNodeProperty {
/// Returns the most recently computed value for the keypath, returns nil if property wasn't found
func getValueOfType<T>() -> T? {
valueContainer.value as? T
}
/// Returns the most recently computed value for the keypath, returns nil if property wasn't found
func getValue() -> Any? {
valueContainer.value
}
}
@@ -0,0 +1,5 @@
#include "AnyValueContainer.hpp"
namespace lottie {
}
@@ -0,0 +1,25 @@
#ifndef AnyValueContainer_hpp
#define AnyValueContainer_hpp
#include "Lottie/Public/Primitives/AnyValue.hpp"
namespace lottie {
class AnyValueContainer {
public:
/// The stored value of the container
virtual AnyValue value() const = 0;
/// Notifies the provider that it should update its container
virtual void setNeedsUpdate() = 0;
/// When true the container needs to have its value updated by its provider
virtual bool needsUpdate() const = 0;
/// The frame time of the last provided update
virtual double lastUpdateFrame() const = 0;
};
}
#endif /* AnyValueContainer_hpp */
@@ -0,0 +1,26 @@
//
// AnyValueContainer.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/30/19.
//
import CoreGraphics
import Foundation
/// The container for the value of a property.
protocol AnyValueContainer: AnyObject {
/// The stored value of the container
var value: Any { get }
/// Notifies the provider that it should update its container
func setNeedsUpdate()
/// When true the container needs to have its value updated by its provider
var needsUpdate: Bool { get }
/// The frame time of the last provided update
var lastUpdateFrame: CGFloat { get }
}
@@ -0,0 +1,13 @@
#ifndef HasRenderUpdates_hpp
#define HasRenderUpdates_hpp
namespace lottie {
class HasRenderUpdates {
public:
virtual bool hasRenderUpdates(double forFrame) = 0;
};
}
#endif /* HasRenderUpdates_hpp */
@@ -0,0 +1,14 @@
#ifndef HasUpdate_hpp
#define HasUpdate_hpp
namespace lottie {
class HasUpdate {
public:
/// The last frame in which this node was updated.
virtual bool hasUpdate() = 0;
};
}
#endif /* HasUpdate_hpp */
@@ -0,0 +1,5 @@
#include "KeypathSearchable.hpp"
namespace lottie {
}
@@ -0,0 +1,36 @@
#ifndef KeypathSearchable_hpp
#define KeypathSearchable_hpp
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/Protocols/AnyNodeProperty.hpp"
#include "Lottie/Public/Primitives/CALayer.hpp"
#include <string>
#include <vector>
#include <map>
#include <memory>
namespace lottie {
class KeypathSearchable;
class HasChildKeypaths {
public:
/// Children Keypaths
virtual std::vector<std::shared_ptr<KeypathSearchable>> const &childKeypaths() const = 0;
};
/// Protocol that provides keypath search functionality. Returns all node properties associated with a keypath.
class KeypathSearchable: virtual public HasChildKeypaths {
public:
/// The name of the Keypath
virtual std::string keypathName() const = 0;
/// A list of properties belonging to the keypath.
virtual std::map<std::string, std::shared_ptr<AnyNodeProperty>> keypathProperties() const = 0;
virtual std::shared_ptr<CALayer> keypathLayer() const = 0;
};
}
#endif /* KeypathSearchable_hpp */
@@ -0,0 +1,24 @@
//
// KeypathSettable.swift
// lottie-swift
//
// Created by Brandon Withrow on 2/4/19.
//
import Foundation
import QuartzCore
/// Protocol that provides keypath search functionality. Returns all node properties associated with a keypath.
protocol KeypathSearchable {
/// The name of the Keypath
var keypathName: String { get }
/// A list of properties belonging to the keypath.
var keypathProperties: [String: AnyNodeProperty] { get }
/// Children Keypaths
var childKeypaths: [KeypathSearchable] { get }
var keypathLayer: CALayer? { get }
}
@@ -0,0 +1,5 @@
#include "NodePropertyMap.hpp"
namespace lottie {
}
@@ -0,0 +1,41 @@
#ifndef NodePropertyMap_hpp
#define NodePropertyMap_hpp
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/Protocols/AnyNodeProperty.hpp"
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/Protocols/KeypathSearchable.hpp"
#include "Lottie/Public/Primitives/CALayer.hpp"
#include <vector>
namespace lottie {
class NodePropertyMap: virtual public HasChildKeypaths {
public:
virtual std::vector<std::shared_ptr<AnyNodeProperty>> &properties() = 0;
bool needsLocalUpdate(double frame) {
for (auto &property : properties()) {
if (property->needsUpdate(frame)) {
return true;
}
}
return false;
}
void updateNodeProperties(double frame) {
for (auto &property : properties()) {
property->update(frame);
}
}
};
class KeypathSearchableNodePropertyMap: virtual public NodePropertyMap, virtual public KeypathSearchable {
public:
virtual std::shared_ptr<CALayer> keypathLayer() {
return nullptr;
}
};
}
#endif /* NodePropertyMap_hpp */
@@ -0,0 +1,44 @@
//
// NodePropertyMap.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/21/19.
//
import Foundation
import QuartzCore
// MARK: - NodePropertyMap
protocol NodePropertyMap {
var properties: [AnyNodeProperty] { get }
}
extension NodePropertyMap {
var childKeypaths: [KeypathSearchable] {
[]
}
var keypathLayer: CALayer? {
nil
}
/// Checks if the node's local contents need to be rebuilt.
func needsLocalUpdate(frame: CGFloat) -> Bool {
for property in properties {
if property.needsUpdate(frame: frame) {
return true
}
}
return false
}
/// Rebuilds only the local nodes that have an update for the frame
func updateNodeProperties(frame: CGFloat) {
properties.forEach { property in
property.update(frame: frame)
}
}
}
@@ -0,0 +1,5 @@
#include "ValueContainer.hpp"
namespace lottie {
}
@@ -0,0 +1,58 @@
#ifndef ValueContainer_hpp
#define ValueContainer_hpp
#include "Lottie/Public/Primitives/AnyValue.hpp"
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/Protocols/AnyValueContainer.hpp"
namespace lottie {
/// A container for a node value that is Typed to T.
template<typename T>
class ValueContainer: public AnyValueContainer {
public:
ValueContainer(T value) :
_outputValue(value) {
}
public:
double _lastUpdateFrame = std::numeric_limits<double>::infinity();
bool _needsUpdate = true;
virtual AnyValue value() const override {
return AnyValue(_outputValue);
}
virtual bool needsUpdate() const override {
return _needsUpdate;
}
virtual double lastUpdateFrame() const override {
return _lastUpdateFrame;
}
T _outputValue;
T outputValue() {
return _outputValue;
}
void setOutputValue(T value) {
_outputValue = value;
_needsUpdate = false;
}
void setValue(AnyValue value, double forFrame) {
if (value.type() == AnyValueType<T>::type()) {
_needsUpdate = false;
_lastUpdateFrame = forFrame;
_outputValue = value.get<T>();
}
}
virtual void setNeedsUpdate() override {
_needsUpdate = true;
}
};
}
#endif /* ValueContainer_hpp */
@@ -0,0 +1,47 @@
//
// ValueContainer.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/30/19.
//
import CoreGraphics
import Foundation
/// A container for a node value that is Typed to T.
class ValueContainer<T>: AnyValueContainer {
// MARK: Lifecycle
init(_ value: T) {
outputValue = value
}
// MARK: Internal
private(set) var lastUpdateFrame = CGFloat.infinity
fileprivate(set) var needsUpdate = true
var value: Any {
outputValue as Any
}
var outputValue: T {
didSet {
needsUpdate = false
}
}
func setValue(_ value: Any, forFrame: CGFloat) {
if let typedValue = value as? T {
needsUpdate = false
lastUpdateFrame = forFrame
outputValue = typedValue
}
}
func setNeedsUpdate() {
needsUpdate = true
}
}
@@ -0,0 +1,5 @@
#include "DashPatternInterpolator.hpp"
namespace lottie {
}
@@ -0,0 +1,46 @@
#ifndef DashPatternInterpolator_hpp
#define DashPatternInterpolator_hpp
#include "Lottie/Private/MainThread/NodeRenderSystem/NodeProperties/ValueProviders/KeyframeInterpolator.hpp"
#include "Lottie/Public/Primitives/DashPattern.hpp"
namespace lottie {
/// A value provider that produces an array of values from an array of Keyframe Interpolators
class DashPatternInterpolator: public ValueProvider<DashPattern>, public std::enable_shared_from_this<DashPatternInterpolator> {
public:
/// Initialize with an array of array of keyframes.
DashPatternInterpolator(std::vector<std::vector<Keyframe<Vector1D>>> const &keyframeGroups) {
for (const auto &keyframeGroup : keyframeGroups) {
_keyframeInterpolators.push_back(std::make_shared<KeyframeInterpolator<Vector1D>>(keyframeGroup));
}
}
virtual AnyValue::Type valueType() const override {
return AnyValueType<DashPattern>::type();
}
virtual DashPattern value(AnimationFrameTime frame) override {
std::vector<double> values;
for (const auto &interpolator : _keyframeInterpolators) {
values.push_back(interpolator->value(frame).value);
}
return DashPattern(std::move(values));
}
virtual bool hasUpdate(double frame) const override {
for (const auto &interpolator : _keyframeInterpolators) {
if (interpolator->hasUpdate(frame)) {
return true;
}
}
return false;
}
private:
std::vector<std::shared_ptr<KeyframeInterpolator<Vector1D>>> _keyframeInterpolators;
};
}
#endif /* DashPatternInterpolator_hpp */
@@ -0,0 +1,39 @@
//
// KeyframeGroupInterpolator.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/22/19.
//
import CoreGraphics
import Foundation
/// A value provider that produces an array of values from an array of Keyframe Interpolators
final class GroupInterpolator<ValueType>: ValueProvider where ValueType: Interpolatable {
// MARK: Lifecycle
/// Initialize with an array of array of keyframes.
init(keyframeGroups: ContiguousArray<ContiguousArray<Keyframe<ValueType>>>) {
keyframeInterpolators = ContiguousArray(keyframeGroups.map({ KeyframeInterpolator(keyframes: $0) }))
}
// MARK: Internal
let keyframeInterpolators: ContiguousArray<KeyframeInterpolator<ValueType>>
var valueType: Any.Type {
[ValueType].self
}
var storage: ValueProviderStorage<[ValueType]> {
.closure { frame in
self.keyframeInterpolators.map({ $0.value(frame: frame) as! ValueType })
}
}
func hasUpdate(frame: CGFloat) -> Bool {
let updated = keyframeInterpolators.first(where: { $0.hasUpdate(frame: frame) })
return updated != nil
}
}
@@ -0,0 +1,5 @@
#include "KeyframeInterpolator.hpp"
namespace lottie {
}
@@ -0,0 +1,449 @@
#ifndef KeyframeInterpolator_hpp
#define KeyframeInterpolator_hpp
#include "Lottie/Public/DynamicProperties/AnyValueProvider.hpp"
namespace lottie {
/// A value provider that produces a value at Time from a group of keyframes
template<typename T>
class KeyframeInterpolator: public ValueProvider<T>, public std::enable_shared_from_this<KeyframeInterpolator<T>> {
public:
KeyframeInterpolator(std::vector<Keyframe<T>> const &keyframes_) :
keyframes(keyframes_) {
assert(!keyframes.empty());
}
public:
std::vector<Keyframe<T>> keyframes;
virtual AnyValue::Type valueType() const override {
return AnyValueType<T>::type();
}
virtual T value(AnimationFrameTime frame) override {
// First set the keyframe span for the frame.
updateSpanIndices(frame);
lastUpdatedFrame = frame;
// If only one keyframe return its value
if (leadingKeyframe.has_value() &&
trailingKeyframe.has_value())
{
/// We have leading and trailing keyframe.
auto progress = leadingKeyframe->interpolatedProgress(trailingKeyframe.value(), frame);
return leadingKeyframe->interpolate(trailingKeyframe.value(), progress);
} else if (leadingKeyframe.has_value()) {
return leadingKeyframe->value;
} else if (trailingKeyframe.has_value()) {
return trailingKeyframe->value;
} else {
/// Satisfy the compiler.
return keyframes[0].value;
}
}
/// Returns true to trigger a frame update for this interpolator.
///
/// An interpolator will be asked if it needs to update every frame.
/// If the interpolator needs updating it will be asked to compute its value for
/// the given frame.
///
/// Cases a keyframe should not be updated:
/// - If time is in span and leading keyframe is hold
/// - If time is after the last keyframe.
/// - If time is before the first keyframe
///
/// Cases for updating a keyframe:
/// - If time is in the span, and is not a hold
/// - If time is outside of the span, and there are more keyframes
/// - If a value delegate is set
/// - If leading and trailing are both nil.
virtual bool hasUpdate(double frame) const override {
if (!lastUpdatedFrame.has_value()) {
return true;
}
if (leadingKeyframe.has_value() &&
!trailingKeyframe.has_value() &&
leadingKeyframe->time < frame)
{
/// Frame is after bounds of keyframes
return false;
}
if (trailingKeyframe.has_value() &&
!leadingKeyframe.has_value() &&
frame < trailingKeyframe->time)
{
/// Frame is before bounds of keyframes
return false;
}
if (leadingKeyframe.has_value() &&
trailingKeyframe.has_value() &&
leadingKeyframe->isHold &&
leadingKeyframe->time < frame &&
frame < trailingKeyframe->time)
{
return false;
}
return true;
}
// MARK: Fileprivate
std::optional<double> lastUpdatedFrame;
std::optional<int> leadingIndex;
std::optional<int> trailingIndex;
std::optional<Keyframe<T>> leadingKeyframe;
std::optional<Keyframe<T>> trailingKeyframe;
/// Finds the appropriate Leading and Trailing keyframe index for the given time.
void updateSpanIndices(double frame) {
if (keyframes.empty()) {
leadingIndex = std::nullopt;
trailingIndex = std::nullopt;
leadingKeyframe = std::nullopt;
trailingKeyframe = std::nullopt;
return;
}
// This function searches through the array to find the span of two keyframes
// that contain the current time.
//
// We could use Array.first(where:) but that would search through the entire array
// each frame.
// Instead we track the last used index and search either forwards or
// backwards from there. This reduces the iterations and complexity from
//
// O(n), where n is the length of the sequence to
// O(n), where n is the number of items after or before the last used index.
//
if (keyframes.size() == 1) {
/// Only one keyframe. Set it as first and move on.
leadingIndex = 0;
trailingIndex = std::nullopt;
leadingKeyframe = keyframes[0];
trailingKeyframe = std::nullopt;
return;
}
/// Sets the initial keyframes. This is often only needed for the first check.
if
(!leadingIndex.has_value() &&
!trailingIndex.has_value())
{
if (frame < keyframes[0].time) {
/// Time is before the first keyframe. Set it as the trailing.
trailingIndex = 0;
} else {
/// Time is after the first keyframe. Set the keyframe and the trailing.
leadingIndex = 0;
trailingIndex = 1;
}
}
if
(trailingIndex.has_value() &&
keyframes[trailingIndex.value()].time <= frame)
{
/// Time is after the current span. Iterate forward.
auto newLeading = trailingIndex.value();
bool keyframeFound = false;
while (!keyframeFound) {
leadingIndex = newLeading;
if (newLeading + 1 >= 0 && newLeading + 1 < keyframes.size()) {
trailingIndex = newLeading + 1;
} else {
trailingIndex = std::nullopt;
}
if (!trailingIndex.has_value()) {
/// We have reached the end of our keyframes. Time is after the last keyframe.
keyframeFound = true;
continue;
}
if (frame < keyframes[trailingIndex.value()].time) {
/// Keyframe in current span.
keyframeFound = true;
continue;
}
/// Advance the array.
newLeading = trailingIndex.value();
}
} else if
(leadingIndex.has_value() &&
frame < keyframes[leadingIndex.value()].time)
{
/// Time is before the current span. Iterate backwards
auto newTrailing = leadingIndex.value();
bool keyframeFound = false;
while (!keyframeFound) {
if (newTrailing - 1 >= 0 && newTrailing - 1 < keyframes.size()) {
leadingIndex = newTrailing - 1;
} else {
leadingIndex = std::nullopt;
}
trailingIndex = newTrailing;
if (!leadingIndex.has_value()) {
/// We have reached the end of our keyframes. Time is after the last keyframe.
keyframeFound = true;
continue;
}
if (keyframes[leadingIndex.value()].time <= frame) {
/// Keyframe in current span.
keyframeFound = true;
continue;
}
/// Step back
newTrailing = leadingIndex.value();
}
}
if (const auto keyFrame = leadingIndex) {
leadingKeyframe = keyframes[keyFrame.value()];
} else {
leadingKeyframe = std::nullopt;
}
if (const auto keyFrame = trailingIndex) {
trailingKeyframe = keyframes[keyFrame.value()];
} else {
trailingKeyframe = std::nullopt;
}
}
};
class BezierPathKeyframeInterpolator {
public:
BezierPathKeyframeInterpolator(std::vector<Keyframe<BezierPath>> const &keyframes_) :
keyframes(keyframes_) {
assert(!keyframes.empty());
}
public:
std::vector<Keyframe<BezierPath>> keyframes;
void update(AnimationFrameTime frame, BezierPath &outPath) {
// First set the keyframe span for the frame.
updateSpanIndices(frame);
lastUpdatedFrame = frame;
// If only one keyframe return its value
if (leadingKeyframe.has_value() &&
trailingKeyframe.has_value())
{
/// We have leading and trailing keyframe.
auto progress = leadingKeyframe->interpolatedProgress(trailingKeyframe.value(), frame);
interpolateInplace(leadingKeyframe.value(), trailingKeyframe.value(), progress, outPath);
} else if (leadingKeyframe.has_value()) {
setInplace(leadingKeyframe.value(), outPath);
} else if (trailingKeyframe.has_value()) {
setInplace(trailingKeyframe.value(), outPath);
} else {
/// Satisfy the compiler.
setInplace(keyframes[0], outPath);
}
}
/// Returns true to trigger a frame update for this interpolator.
///
/// An interpolator will be asked if it needs to update every frame.
/// If the interpolator needs updating it will be asked to compute its value for
/// the given frame.
///
/// Cases a keyframe should not be updated:
/// - If time is in span and leading keyframe is hold
/// - If time is after the last keyframe.
/// - If time is before the first keyframe
///
/// Cases for updating a keyframe:
/// - If time is in the span, and is not a hold
/// - If time is outside of the span, and there are more keyframes
/// - If a value delegate is set
/// - If leading and trailing are both nil.
bool hasUpdate(double frame) const {
if (!lastUpdatedFrame.has_value()) {
return true;
}
if (leadingKeyframe.has_value() &&
!trailingKeyframe.has_value() &&
leadingKeyframe->time < frame)
{
/// Frame is after bounds of keyframes
return false;
}
if (trailingKeyframe.has_value() &&
!leadingKeyframe.has_value() &&
frame < trailingKeyframe->time)
{
/// Frame is before bounds of keyframes
return false;
}
if (leadingKeyframe.has_value() &&
trailingKeyframe.has_value() &&
leadingKeyframe->isHold &&
leadingKeyframe->time < frame &&
frame < trailingKeyframe->time)
{
return false;
}
return true;
}
// MARK: Fileprivate
std::optional<double> lastUpdatedFrame;
std::optional<int> leadingIndex;
std::optional<int> trailingIndex;
std::optional<Keyframe<BezierPath>> leadingKeyframe;
std::optional<Keyframe<BezierPath>> trailingKeyframe;
/// Finds the appropriate Leading and Trailing keyframe index for the given time.
void updateSpanIndices(double frame) {
if (keyframes.empty()) {
leadingIndex = std::nullopt;
trailingIndex = std::nullopt;
leadingKeyframe = std::nullopt;
trailingKeyframe = std::nullopt;
return;
}
// This function searches through the array to find the span of two keyframes
// that contain the current time.
//
// We could use Array.first(where:) but that would search through the entire array
// each frame.
// Instead we track the last used index and search either forwards or
// backwards from there. This reduces the iterations and complexity from
//
// O(n), where n is the length of the sequence to
// O(n), where n is the number of items after or before the last used index.
//
if (keyframes.size() == 1) {
/// Only one keyframe. Set it as first and move on.
leadingIndex = 0;
trailingIndex = std::nullopt;
leadingKeyframe = keyframes[0];
trailingKeyframe = std::nullopt;
return;
}
/// Sets the initial keyframes. This is often only needed for the first check.
if
(!leadingIndex.has_value() &&
!trailingIndex.has_value())
{
if (frame < keyframes[0].time) {
/// Time is before the first keyframe. Set it as the trailing.
trailingIndex = 0;
} else {
/// Time is after the first keyframe. Set the keyframe and the trailing.
leadingIndex = 0;
trailingIndex = 1;
}
}
if
(trailingIndex.has_value() &&
keyframes[trailingIndex.value()].time <= frame)
{
/// Time is after the current span. Iterate forward.
auto newLeading = trailingIndex.value();
bool keyframeFound = false;
while (!keyframeFound) {
leadingIndex = newLeading;
if (newLeading + 1 >= 0 && newLeading + 1 < keyframes.size()) {
trailingIndex = newLeading + 1;
} else {
trailingIndex = std::nullopt;
}
if (!trailingIndex.has_value()) {
/// We have reached the end of our keyframes. Time is after the last keyframe.
keyframeFound = true;
continue;
}
if (frame < keyframes[trailingIndex.value()].time) {
/// Keyframe in current span.
keyframeFound = true;
continue;
}
/// Advance the array.
newLeading = trailingIndex.value();
}
} else if
(leadingIndex.has_value() &&
frame < keyframes[leadingIndex.value()].time)
{
/// Time is before the current span. Iterate backwards
auto newTrailing = leadingIndex.value();
bool keyframeFound = false;
while (!keyframeFound) {
if (newTrailing - 1 >= 0 && newTrailing - 1 < keyframes.size()) {
leadingIndex = newTrailing - 1;
} else {
leadingIndex = std::nullopt;
}
trailingIndex = newTrailing;
if (!leadingIndex.has_value()) {
/// We have reached the end of our keyframes. Time is after the last keyframe.
keyframeFound = true;
continue;
}
if (keyframes[leadingIndex.value()].time <= frame) {
/// Keyframe in current span.
keyframeFound = true;
continue;
}
/// Step back
newTrailing = leadingIndex.value();
}
}
if (const auto keyFrame = leadingIndex) {
leadingKeyframe = keyframes[keyFrame.value()];
} else {
leadingKeyframe = std::nullopt;
}
if (const auto keyFrame = trailingIndex) {
trailingKeyframe = keyframes[keyFrame.value()];
} else {
trailingKeyframe = std::nullopt;
}
}
private:
void setInplace(Keyframe<BezierPath> const &from, BezierPath &outPath) {
ValueInterpolator<BezierPath>::setInplace(from.value, outPath);
}
void interpolateInplace(Keyframe<BezierPath> const &from, Keyframe<BezierPath> const &to, double progress, BezierPath &outPath) {
std::optional<Vector2D> spatialOutTangent2d;
if (from.spatialOutTangent) {
spatialOutTangent2d = Vector2D(from.spatialOutTangent->x, from.spatialOutTangent->y);
}
std::optional<Vector2D> spatialInTangent2d;
if (to.spatialInTangent) {
spatialInTangent2d = Vector2D(to.spatialInTangent->x, to.spatialInTangent->y);
}
ValueInterpolator<BezierPath>::interpolateInplace(from.value, to.value, progress, spatialOutTangent2d, spatialInTangent2d, outPath);
}
};
}
#endif /* KeyframeInterpolator_hpp */
@@ -0,0 +1,253 @@
//
// KeyframeInterpolator.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/15/19.
//
import CoreGraphics
import Foundation
// MARK: - KeyframeInterpolator
/// A value provider that produces a value at Time from a group of keyframes
final class KeyframeInterpolator<ValueType>: ValueProvider where ValueType: AnyInterpolatable {
// MARK: Lifecycle
init(keyframes: ContiguousArray<Keyframe<ValueType>>) {
self.keyframes = keyframes
}
// MARK: Internal
let keyframes: ContiguousArray<Keyframe<ValueType>>
var valueType: Any.Type {
ValueType.self
}
var storage: ValueProviderStorage<ValueType> {
.closure { [self] frame in
// First set the keyframe span for the frame.
updateSpanIndices(frame: frame)
lastUpdatedFrame = frame
// If only one keyframe return its value
let progress: CGFloat
let value: ValueType
if
let leading = leadingKeyframe,
let trailing = trailingKeyframe
{
/// We have leading and trailing keyframe.
progress = leading.interpolatedProgress(trailing, keyTime: frame)
value = leading.interpolate(to: trailing, progress: progress)
} else if let leading = leadingKeyframe {
progress = 0
value = leading.value
} else if let trailing = trailingKeyframe {
progress = 1
value = trailing.value
} else {
/// Satisfy the compiler.
progress = 0
value = keyframes[0].value
}
return value
}
}
/// Returns true to trigger a frame update for this interpolator.
///
/// An interpolator will be asked if it needs to update every frame.
/// If the interpolator needs updating it will be asked to compute its value for
/// the given frame.
///
/// Cases a keyframe should not be updated:
/// - If time is in span and leading keyframe is hold
/// - If time is after the last keyframe.
/// - If time is before the first keyframe
///
/// Cases for updating a keyframe:
/// - If time is in the span, and is not a hold
/// - If time is outside of the span, and there are more keyframes
/// - If a value delegate is set
/// - If leading and trailing are both nil.
func hasUpdate(frame: CGFloat) -> Bool {
if lastUpdatedFrame == nil {
return true
}
if
let leading = leadingKeyframe,
trailingKeyframe == nil,
leading.time < frame
{
/// Frame is after bounds of keyframes
return false
}
if
let trailing = trailingKeyframe,
leadingKeyframe == nil,
frame < trailing.time
{
/// Frame is before bounds of keyframes
return false
}
if
let leading = leadingKeyframe,
let trailing = trailingKeyframe,
leading.isHold,
leading.time < frame,
frame < trailing.time
{
return false
}
return true
}
// MARK: Fileprivate
fileprivate var lastUpdatedFrame: CGFloat?
fileprivate var leadingIndex: Int? = nil
fileprivate var trailingIndex: Int? = nil
fileprivate var leadingKeyframe: Keyframe<ValueType>? = nil
fileprivate var trailingKeyframe: Keyframe<ValueType>? = nil
/// Finds the appropriate Leading and Trailing keyframe index for the given time.
fileprivate func updateSpanIndices(frame: CGFloat) {
guard keyframes.count > 0 else {
leadingIndex = nil
trailingIndex = nil
leadingKeyframe = nil
trailingKeyframe = nil
return
}
// This function searches through the array to find the span of two keyframes
// that contain the current time.
//
// We could use Array.first(where:) but that would search through the entire array
// each frame.
// Instead we track the last used index and search either forwards or
// backwards from there. This reduces the iterations and complexity from
//
// O(n), where n is the length of the sequence to
// O(n), where n is the number of items after or before the last used index.
//
if keyframes.count == 1 {
/// Only one keyframe. Set it as first and move on.
leadingIndex = 0
trailingIndex = nil
leadingKeyframe = keyframes[0]
trailingKeyframe = nil
return
}
/// Sets the initial keyframes. This is often only needed for the first check.
if
leadingIndex == nil &&
trailingIndex == nil
{
if frame < keyframes[0].time {
/// Time is before the first keyframe. Set it as the trailing.
trailingIndex = 0
} else {
/// Time is after the first keyframe. Set the keyframe and the trailing.
leadingIndex = 0
trailingIndex = 1
}
}
if
let currentTrailing = trailingIndex,
keyframes[currentTrailing].time <= frame
{
/// Time is after the current span. Iterate forward.
var newLeading = currentTrailing
var keyframeFound = false
while !keyframeFound {
leadingIndex = newLeading
trailingIndex = keyframes.validIndex(newLeading + 1)
guard let trailing = trailingIndex else {
/// We have reached the end of our keyframes. Time is after the last keyframe.
keyframeFound = true
continue
}
if frame < keyframes[trailing].time {
/// Keyframe in current span.
keyframeFound = true
continue
}
/// Advance the array.
newLeading = trailing
}
} else if
let currentLeading = leadingIndex,
frame < keyframes[currentLeading].time
{
/// Time is before the current span. Iterate backwards
var newTrailing = currentLeading
var keyframeFound = false
while !keyframeFound {
leadingIndex = keyframes.validIndex(newTrailing - 1)
trailingIndex = newTrailing
guard let leading = leadingIndex else {
/// We have reached the end of our keyframes. Time is after the last keyframe.
keyframeFound = true
continue
}
if keyframes[leading].time <= frame {
/// Keyframe in current span.
keyframeFound = true
continue
}
/// Step back
newTrailing = leading
}
}
if let keyFrame = leadingIndex {
leadingKeyframe = keyframes[keyFrame]
} else {
leadingKeyframe = nil
}
if let keyFrame = trailingIndex {
trailingKeyframe = keyframes[keyFrame]
} else {
trailingKeyframe = nil
}
}
}
extension Array {
fileprivate func validIndex(_ index: Int) -> Int? {
if 0 <= index, index < endIndex {
return index
}
return nil
}
}
extension ContiguousArray {
fileprivate func validIndex(_ index: Int) -> Int? {
if 0 <= index, index < endIndex {
return index
}
return nil
}
}
@@ -0,0 +1,5 @@
#include "SingleValueProvider.hpp"
namespace lottie {
}
@@ -0,0 +1,40 @@
#ifndef SingleValueProvider_hpp
#define SingleValueProvider_hpp
#include "Lottie/Public/DynamicProperties/AnyValueProvider.hpp"
namespace lottie {
/// Returns a value for every frame.
template<typename T>
class SingleValueProvider: public ValueProvider<T> {
public:
SingleValueProvider(T const &value) :
_value(value) {
}
void setValue(T const &value) {
_value = value;
_hasUpdate = true;
}
virtual T value(AnimationFrameTime frame) override {
return _value;
}
virtual AnyValue::Type valueType() const override {
return AnyValueType<T>::type();
}
virtual bool hasUpdate(double frame) const override {
return _hasUpdate;
}
private:
T _value;
bool _hasUpdate = true;
};
}
#endif /* SingleValueProvider_hpp */
@@ -0,0 +1,43 @@
//
// SingleValueProvider.swift
// lottie-swift
//
// Created by Brandon Withrow on 1/30/19.
//
import Foundation
import QuartzCore
/// Returns a value for every frame.
final class SingleValueProvider<ValueType: AnyInterpolatable>: ValueProvider {
// MARK: Lifecycle
init(_ value: ValueType) {
self.value = value
}
// MARK: Internal
var value: ValueType {
didSet {
hasUpdate = true
}
}
var storage: ValueProviderStorage<ValueType> {
.singleValue(value)
}
var valueType: Any.Type {
ValueType.self
}
func hasUpdate(frame _: CGFloat) -> Bool {
hasUpdate
}
// MARK: Private
private var hasUpdate = true
}