Nnnnn scripted interpolators (#12505) 44b83c5345 * feature: stateless scripted interpolator * feature: statefull scripted interpolator Co-authored-by: hernan <hernan@rive.app>
diff --git a/.rive_head b/.rive_head index 2387d84..d4bea82 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -9f0dc79e3f9018f28337c715d58232096ad0a317 +44b83c5345f5abbae7b5a2fc0d7c4733f7d6e240
diff --git a/dev/defs/scripted/scripted_interpolator.json b/dev/defs/scripted/scripted_interpolator.json new file mode 100644 index 0000000..ea3e178 --- /dev/null +++ b/dev/defs/scripted/scripted_interpolator.json
@@ -0,0 +1,20 @@ +{ + "name": "ScriptedInterpolator", + "key": { + "int": 972, + "string": "scriptedinterpolator" + }, + "extends": "animation/keyframe_interpolator.json", + "properties": { + "scriptAssetId": { + "type": "Id", + "typeRuntime": "uint", + "initialValue": "Core.missingId", + "initialValueRuntime": "-1", + "key": { + "int": 1015, + "string": "scriptassetid" + } + } + } +} \ No newline at end of file
diff --git a/include/rive/animation/interpolating_keyframe.hpp b/include/rive/animation/interpolating_keyframe.hpp index a2d31f6..7684604 100644 --- a/include/rive/animation/interpolating_keyframe.hpp +++ b/include/rive/animation/interpolating_keyframe.hpp
@@ -5,16 +5,28 @@ namespace rive { class KeyFrameInterpolator; +class LinearAnimationInstance; class InterpolatingKeyFrame : public InterpolatingKeyFrameBase { public: inline KeyFrameInterpolator* interpolator() const { return m_interpolator; } + + /// Returns the interpolator that should run for this keyframe given the + /// LAI context. For ScriptedInterpolators this resolves to the per-(LAI, + /// keyframe) stateful clone; for everything else (or when context is + /// null) it returns the shared `m_interpolator`. Defined out-of-line so + /// the header doesn't have to drag in linear_animation_instance.hpp. + KeyFrameInterpolator* effectiveInterpolator( + const LinearAnimationInstance* context) const; + virtual void apply(Core* object, int propertyKey, float mix) = 0; - virtual void applyInterpolation(Core* object, - int propertyKey, - float seconds, - const KeyFrame* nextFrame, - float mix) = 0; + virtual void applyInterpolation( + Core* object, + int propertyKey, + float seconds, + const KeyFrame* nextFrame, + float mix, + const LinearAnimationInstance* context = nullptr) = 0; StatusCode onAddedDirty(CoreContext* context) override; private:
diff --git a/include/rive/animation/keyed_object.hpp b/include/rive/animation/keyed_object.hpp index 3aba7b9..c849d25 100644 --- a/include/rive/animation/keyed_object.hpp +++ b/include/rive/animation/keyed_object.hpp
@@ -7,6 +7,7 @@ class Artboard; class KeyedProperty; class KeyedCallbackReporter; +class LinearAnimationInstance; class KeyedObject : public KeyedObjectBase { public: @@ -20,7 +21,10 @@ float secondsFrom, float secondsTo, bool isAtStartFrame) const; - void apply(Artboard* coreContext, float time, float mix); + void apply(Artboard* coreContext, + float time, + float mix, + const LinearAnimationInstance* context = nullptr); StatusCode import(ImportStack& importStack) override;
diff --git a/include/rive/animation/keyed_property.hpp b/include/rive/animation/keyed_property.hpp index d9d0425..35a9859 100644 --- a/include/rive/animation/keyed_property.hpp +++ b/include/rive/animation/keyed_property.hpp
@@ -6,6 +6,7 @@ { class KeyFrame; class KeyedCallbackReporter; +class LinearAnimationInstance; class KeyedProperty : public KeyedPropertyBase { public: @@ -22,8 +23,14 @@ float secondsTo, bool isAtStartFrame) const; - /// Apply interpolating key frames. - void apply(Core* object, float time, float mix); + /// Apply interpolating key frames. `context` is the running + /// LinearAnimationInstance, propagated so scripted interpolators can vend + /// per-(LAI, keyframe) stateful clones. Default-null keeps direct callers + /// (tests, hold animations) source-compatible. + void apply(Core* object, + float time, + float mix, + const LinearAnimationInstance* context = nullptr); StatusCode import(ImportStack& importStack) override; KeyFrame* first() const
diff --git a/include/rive/animation/keyframe_bool.hpp b/include/rive/animation/keyframe_bool.hpp index 226fd87..fa993d8 100644 --- a/include/rive/animation/keyframe_bool.hpp +++ b/include/rive/animation/keyframe_bool.hpp
@@ -8,11 +8,13 @@ { public: void apply(Core* object, int propertyKey, float mix) override; - void applyInterpolation(Core* object, - int propertyKey, - float seconds, - const KeyFrame* nextFrame, - float mix) override; + void applyInterpolation( + Core* object, + int propertyKey, + float seconds, + const KeyFrame* nextFrame, + float mix, + const LinearAnimationInstance* context = nullptr) override; }; } // namespace rive
diff --git a/include/rive/animation/keyframe_color.hpp b/include/rive/animation/keyframe_color.hpp index 82c8bd9..1b858d7 100644 --- a/include/rive/animation/keyframe_color.hpp +++ b/include/rive/animation/keyframe_color.hpp
@@ -7,11 +7,13 @@ { public: void apply(Core* object, int propertyKey, float mix) override; - void applyInterpolation(Core* object, - int propertyKey, - float seconds, - const KeyFrame* nextFrame, - float mix) override; + void applyInterpolation( + Core* object, + int propertyKey, + float seconds, + const KeyFrame* nextFrame, + float mix, + const LinearAnimationInstance* context = nullptr) override; }; } // namespace rive
diff --git a/include/rive/animation/keyframe_double.hpp b/include/rive/animation/keyframe_double.hpp index 2bdff78..ba27c12 100644 --- a/include/rive/animation/keyframe_double.hpp +++ b/include/rive/animation/keyframe_double.hpp
@@ -7,11 +7,13 @@ { public: void apply(Core* object, int propertyKey, float mix) override; - void applyInterpolation(Core* object, - int propertyKey, - float seconds, - const KeyFrame* nextFrame, - float mix) override; + void applyInterpolation( + Core* object, + int propertyKey, + float seconds, + const KeyFrame* nextFrame, + float mix, + const LinearAnimationInstance* context = nullptr) override; }; } // namespace rive
diff --git a/include/rive/animation/keyframe_id.hpp b/include/rive/animation/keyframe_id.hpp index 5182c6e..399f30c 100644 --- a/include/rive/animation/keyframe_id.hpp +++ b/include/rive/animation/keyframe_id.hpp
@@ -8,11 +8,13 @@ { public: void apply(Core* object, int propertyKey, float mix) override; - void applyInterpolation(Core* object, - int propertyKey, - float seconds, - const KeyFrame* nextFrame, - float mix) override; + void applyInterpolation( + Core* object, + int propertyKey, + float seconds, + const KeyFrame* nextFrame, + float mix, + const LinearAnimationInstance* context = nullptr) override; }; } // namespace rive
diff --git a/include/rive/animation/keyframe_string.hpp b/include/rive/animation/keyframe_string.hpp index 4749dc2..a62d452 100644 --- a/include/rive/animation/keyframe_string.hpp +++ b/include/rive/animation/keyframe_string.hpp
@@ -8,11 +8,13 @@ { public: void apply(Core* object, int propertyKey, float mix) override; - void applyInterpolation(Core* object, - int propertyKey, - float seconds, - const KeyFrame* nextFrame, - float mix) override; + void applyInterpolation( + Core* object, + int propertyKey, + float seconds, + const KeyFrame* nextFrame, + float mix, + const LinearAnimationInstance* context = nullptr) override; }; } // namespace rive
diff --git a/include/rive/animation/keyframe_uint.hpp b/include/rive/animation/keyframe_uint.hpp index 2b398de..62e0e26 100644 --- a/include/rive/animation/keyframe_uint.hpp +++ b/include/rive/animation/keyframe_uint.hpp
@@ -8,11 +8,13 @@ { public: void apply(Core* object, int propertyKey, float mix) override; - void applyInterpolation(Core* object, - int propertyKey, - float seconds, - const KeyFrame* nextFrame, - float mix) override; + void applyInterpolation( + Core* object, + int propertyKey, + float seconds, + const KeyFrame* nextFrame, + float mix, + const LinearAnimationInstance* context = nullptr) override; }; } // namespace rive
diff --git a/include/rive/animation/linear_animation.hpp b/include/rive/animation/linear_animation.hpp index 6d4d1dc..2e1783a 100644 --- a/include/rive/animation/linear_animation.hpp +++ b/include/rive/animation/linear_animation.hpp
@@ -8,6 +8,7 @@ class Artboard; class KeyedObject; class KeyedCallbackReporter; +class LinearAnimationInstance; class LinearAnimation : public LinearAnimationBase { @@ -22,7 +23,15 @@ StatusCode onAddedDirty(CoreContext* context) override; StatusCode onAddedClean(CoreContext* context) override; void addKeyedObject(std::unique_ptr<KeyedObject>); - void apply(Artboard* artboard, float time, float mix = 1.0f) const; + /// `context` is the running LinearAnimationInstance, threaded down so + /// scripted interpolators can vend per-(LAI, keyframe) stateful clones. + /// Default-null keeps direct callers (state-machine hold animations, + /// tests) source-compatible — they degrade to identity for any scripted + /// interpolators in the snapshot. + void apply(Artboard* artboard, + float time, + float mix = 1.0f, + const LinearAnimationInstance* context = nullptr) const; Loop loop() const { return (Loop)loopValue(); }
diff --git a/include/rive/animation/linear_animation_instance.hpp b/include/rive/animation/linear_animation_instance.hpp index 751ac3a..086a5aa 100644 --- a/include/rive/animation/linear_animation_instance.hpp +++ b/include/rive/animation/linear_animation_instance.hpp
@@ -6,10 +6,16 @@ #include "rive/nested_animation.hpp" #include "rive/scene.hpp" +#include <memory> +#include <unordered_map> + namespace rive { class LinearAnimation; class NestedEventNotifier; +class InterpolatingKeyFrame; +class ScriptedInterpolator; +class DataBind; class LinearAnimationInstance : public Scene, public NestedEventNotifier { @@ -62,9 +68,20 @@ // other animations applied to the artboard. void apply(float mix = 1.0f) const { - m_animation->apply(m_artboardInstance, m_time, mix); + m_animation->apply(m_artboardInstance, m_time, mix, this); } + // Returns a per-(this LAI, keyframe) stateful clone of the given + // ScriptedInterpolator, lazily creating it on first call. Returns nullptr + // when scripting is disabled or the clone cannot be vended; callers + // should fall back to the shared template (which already degrades to + // identity/linear when its Lua side isn't ready). Time-seeking via + // `time(float)` and `reset()` intentionally does NOT clear the cache — + // statefulness is the whole point. + ScriptedInterpolator* statefulInterpolator( + const InterpolatingKeyFrame* keyframe, + const ScriptedInterpolator* shared) const; + // Set when the animation is advanced, true if the animation has stopped // (oneShot), reached the end (loop), or changed direction (pingPong) bool didLoop() const { return m_didLoop; } @@ -120,6 +137,27 @@ float m_direction; bool m_didLoop; int m_loopValue = -1; + + // Lazy outer pointer => the common case (no scripted interpolators) pays + // one nullptr check on the apply hot path. Inner unique_ptr destroys the + // cloned ScriptedInterpolator (and its Lua ref) when this LAI is + // destroyed. `mutable` so the cache populates from the const apply(). + // Intentionally not copied by the copy ctor — a copied LAI starts with + // a fresh empty cache. + mutable std::unique_ptr< + std::unordered_map<const InterpolatingKeyFrame*, + std::unique_ptr<ScriptedInterpolator>>> + m_scriptedInterpolatorInstances; + + // Data binds that cloneProperties() appended to m_artboardInstance on + // our behalf for the cloned ScriptedInterpolators above. We must + // removeDataBind+delete each of these in ~LinearAnimationInstance BEFORE + // m_scriptedInterpolatorInstances tears down, because the bind targets + // point at CustomPropertys owned by the clones. Captured by snapshotting + // m_artboardInstance->dataBinds().size() before/after each cloneScripted- + // Object call (addDataBind only ever appends). `mutable` so it can + // populate from the const apply() path. Not copied by the copy ctor. + mutable std::vector<DataBind*> m_clonedArtboardDataBinds; }; } // namespace rive #endif
diff --git a/include/rive/assets/script_asset.hpp b/include/rive/assets/script_asset.hpp index 8b9d5d8..5339480 100644 --- a/include/rive/assets/script_asset.hpp +++ b/include/rive/assets/script_asset.hpp
@@ -28,7 +28,8 @@ converter, pathEffect, listenerAction, - transitionCondition + transitionCondition, + interpolator }; #ifdef WITH_RIVE_SCRIPTING
diff --git a/include/rive/custom_property_container.hpp b/include/rive/custom_property_container.hpp index 160a70a..ea84d66 100644 --- a/include/rive/custom_property_container.hpp +++ b/include/rive/custom_property_container.hpp
@@ -21,6 +21,10 @@ static const std::vector<Component*> emptyVec; return emptyVec; } + const std::vector<CustomProperty*>& customProperties() const + { + return m_customProperties; + } }; } // namespace rive
diff --git a/include/rive/data_bind/data_bind_container.hpp b/include/rive/data_bind/data_bind_container.hpp index cecdfe0..382f855 100644 --- a/include/rive/data_bind/data_bind_container.hpp +++ b/include/rive/data_bind/data_bind_container.hpp
@@ -31,6 +31,7 @@ std::vector<DataBind*> m_persistingDataBinds; std::vector<DataBind*> m_dirtyDataBinds; std::vector<DataBind*> m_pendingDirtyDataBinds; + DataContext* m_dataContext = nullptr; bool m_isProcessing = false; }; } // namespace rive
diff --git a/include/rive/file.hpp b/include/rive/file.hpp index 77149a7..6a495bc 100644 --- a/include/rive/file.hpp +++ b/include/rive/file.hpp
@@ -40,6 +40,7 @@ class ViewModelRuntime; class BindableArtboard; class ScriptingVM; +class ScriptedInterpolator; /// /// Tracks the success/failure result when importing a Rive file. @@ -267,6 +268,7 @@ std::vector<DataConverter*> m_DataConverters; std::vector<KeyFrameInterpolator*> m_keyframeInterpolators; + std::vector<ScriptedInterpolator*> m_scriptedInterpolators; std::vector<ScrollPhysics*> m_scrollPhysics; /// List of artboards in the file. Each artboard encapsulates a set of
diff --git a/include/rive/generated/animation/listener_types/listener_input_type_semantic_base.hpp b/include/rive/generated/animation/listener_types/listener_input_type_semantic_base.hpp index 6163b8f..8f8f2ab 100644 --- a/include/rive/generated/animation/listener_types/listener_input_type_semantic_base.hpp +++ b/include/rive/generated/animation/listener_types/listener_input_type_semantic_base.hpp
@@ -11,6 +11,8 @@ public: static const uint16_t typeKey = 669; + /// Helper to quickly determine if a core object extends another without + /// RTTI at runtime. bool isTypeOf(uint16_t typeKey) const override { switch (typeKey) @@ -31,4 +33,4 @@ }; } // namespace rive -#endif +#endif \ No newline at end of file
diff --git a/include/rive/generated/core_registry.hpp b/include/rive/generated/core_registry.hpp index c7bb3cb..6f56f05 100644 --- a/include/rive/generated/core_registry.hpp +++ b/include/rive/generated/core_registry.hpp
@@ -234,6 +234,7 @@ #include "rive/script_input_viewmodel_property.hpp" #include "rive/scripted/scripted_data_converter.hpp" #include "rive/scripted/scripted_drawable.hpp" +#include "rive/scripted/scripted_interpolator.hpp" #include "rive/scripted/scripted_layout.hpp" #include "rive/scripted/scripted_path_effect.hpp" #include "rive/semantic/semantic_data.hpp" @@ -464,6 +465,8 @@ return new ScriptedDrawable(); case ScriptedDataConverterBase::typeKey: return new ScriptedDataConverter(); + case ScriptedInterpolatorBase::typeKey: + return new ScriptedInterpolator(); case ScriptedLayoutBase::typeKey: return new ScriptedLayout(); case ScriptedPathEffectBase::typeKey: @@ -1025,6 +1028,9 @@ case ScriptedDataConverterBase::scriptAssetIdPropertyKey: object->as<ScriptedDataConverterBase>()->scriptAssetId(value); break; + case ScriptedInterpolatorBase::scriptAssetIdPropertyKey: + object->as<ScriptedInterpolatorBase>()->scriptAssetId(value); + break; case ScriptedPathEffectBase::scriptAssetIdPropertyKey: object->as<ScriptedPathEffectBase>()->scriptAssetId(value); break; @@ -3090,6 +3096,8 @@ return object->as<ScriptedDrawableBase>()->scriptAssetId(); case ScriptedDataConverterBase::scriptAssetIdPropertyKey: return object->as<ScriptedDataConverterBase>()->scriptAssetId(); + case ScriptedInterpolatorBase::scriptAssetIdPropertyKey: + return object->as<ScriptedInterpolatorBase>()->scriptAssetId(); case ScriptedPathEffectBase::scriptAssetIdPropertyKey: return object->as<ScriptedPathEffectBase>()->scriptAssetId(); case NestedArtboardLayoutBase::instanceWidthUnitsValuePropertyKey: @@ -4261,6 +4269,7 @@ case SoloBase::activeComponentIdPropertyKey: case ScriptedDrawableBase::scriptAssetIdPropertyKey: case ScriptedDataConverterBase::scriptAssetIdPropertyKey: + case ScriptedInterpolatorBase::scriptAssetIdPropertyKey: case ScriptedPathEffectBase::scriptAssetIdPropertyKey: case NestedArtboardLayoutBase::instanceWidthUnitsValuePropertyKey: case NestedArtboardLayoutBase::instanceHeightUnitsValuePropertyKey: @@ -4886,6 +4895,8 @@ return object->is<ScriptedDrawableBase>(); case ScriptedDataConverterBase::scriptAssetIdPropertyKey: return object->is<ScriptedDataConverterBase>(); + case ScriptedInterpolatorBase::scriptAssetIdPropertyKey: + return object->is<ScriptedInterpolatorBase>(); case ScriptedPathEffectBase::scriptAssetIdPropertyKey: return object->is<ScriptedPathEffectBase>(); case NestedArtboardLayoutBase::instanceWidthUnitsValuePropertyKey:
diff --git a/include/rive/generated/inputs/semantic_input_base.hpp b/include/rive/generated/inputs/semantic_input_base.hpp index 74fac38..bbdf85f 100644 --- a/include/rive/generated/inputs/semantic_input_base.hpp +++ b/include/rive/generated/inputs/semantic_input_base.hpp
@@ -2,8 +2,6 @@ #define _RIVE_SEMANTIC_INPUT_BASE_HPP_ #include "rive/core/field_types/core_uint_type.hpp" #include "rive/inputs/user_input.hpp" - -#include <cstdint> namespace rive { class SemanticInputBase : public UserInput @@ -14,6 +12,8 @@ public: static const uint16_t typeKey = 670; + /// Helper to quickly determine if a core object extends another without + /// RTTI at runtime. bool isTypeOf(uint16_t typeKey) const override { switch (typeKey) @@ -68,4 +68,4 @@ }; } // namespace rive -#endif +#endif \ No newline at end of file
diff --git a/include/rive/generated/scripted/scripted_interpolator_base.hpp b/include/rive/generated/scripted/scripted_interpolator_base.hpp new file mode 100644 index 0000000..660b575 --- /dev/null +++ b/include/rive/generated/scripted/scripted_interpolator_base.hpp
@@ -0,0 +1,71 @@ +#ifndef _RIVE_SCRIPTED_INTERPOLATOR_BASE_HPP_ +#define _RIVE_SCRIPTED_INTERPOLATOR_BASE_HPP_ +#include "rive/animation/keyframe_interpolator.hpp" +#include "rive/core/field_types/core_uint_type.hpp" +namespace rive +{ +class ScriptedInterpolatorBase : public KeyFrameInterpolator +{ +protected: + typedef KeyFrameInterpolator Super; + +public: + static const uint16_t typeKey = 972; + + /// Helper to quickly determine if a core object extends another without + /// RTTI at runtime. + bool isTypeOf(uint16_t typeKey) const override + { + switch (typeKey) + { + case ScriptedInterpolatorBase::typeKey: + case KeyFrameInterpolatorBase::typeKey: + return true; + default: + return false; + } + } + + uint16_t coreType() const override { return typeKey; } + + static const uint16_t scriptAssetIdPropertyKey = 1015; + +protected: + uint32_t m_ScriptAssetId = -1; + +public: + inline uint32_t scriptAssetId() const { return m_ScriptAssetId; } + void scriptAssetId(uint32_t value) + { + if (m_ScriptAssetId == value) + { + return; + } + m_ScriptAssetId = value; + scriptAssetIdChanged(); + } + + Core* clone() const override; + void copy(const ScriptedInterpolatorBase& object) + { + m_ScriptAssetId = object.m_ScriptAssetId; + KeyFrameInterpolator::copy(object); + } + + bool deserialize(uint16_t propertyKey, BinaryReader& reader) override + { + switch (propertyKey) + { + case scriptAssetIdPropertyKey: + m_ScriptAssetId = CoreUintType::deserialize(reader); + return true; + } + return KeyFrameInterpolator::deserialize(propertyKey, reader); + } + +protected: + virtual void scriptAssetIdChanged() {} +}; +} // namespace rive + +#endif \ No newline at end of file
diff --git a/include/rive/lua/lua_state.hpp b/include/rive/lua/lua_state.hpp index 985cc33..b585c81 100644 --- a/include/rive/lua/lua_state.hpp +++ b/include/rive/lua/lua_state.hpp
@@ -3,6 +3,7 @@ #ifdef WITH_RIVE_SCRIPTING #include "lua.h" +#include "rive/scripted/scripted_interpolator.hpp" #endif #include <vector>
diff --git a/include/rive/scripted/scripted_interpolator.hpp b/include/rive/scripted/scripted_interpolator.hpp new file mode 100644 index 0000000..b6f8a07 --- /dev/null +++ b/include/rive/scripted/scripted_interpolator.hpp
@@ -0,0 +1,38 @@ +#ifndef _RIVE_SCRIPTED_INTERPOLATOR_HPP_ +#define _RIVE_SCRIPTED_INTERPOLATOR_HPP_ +#include "rive/generated/scripted/scripted_interpolator_base.hpp" +#include "rive/scripted/scripted_object.hpp" +#include <stdio.h> +namespace rive +{ +class ScriptedInterpolator : public ScriptedInterpolatorBase, + public ScriptedObject +{ +public: + float transformValue(float valueFrom, float valueTo, float factor) override; + float transform(float factor) const override; + uint32_t assetId() override { return scriptAssetId(); } + bool addScriptedDirt(ComponentDirt value, bool recurse = false) override + { + return false; + } + ScriptProtocol scriptProtocol() override + { + return ScriptProtocol::interpolator; + } + Component* component() override { return nullptr; } + StatusCode import(ImportStack& importStack) override; + + // Cloning support so each (LinearAnimationInstance, InterpolatingKeyFrame) + // pair can vend its own stateful Lua instance. Mirrors + // ScriptedListenerAction / ScriptedTransitionCondition. Without these the + // base `ScriptedObject::cloneScriptedObject` returns nullptr and the + // per-instance path silently degrades to the shared template's + // identity/linear fallback. + Core* clone() const override; + ScriptedObject* cloneScriptedObject(DataBindContainer*) const override; + void addProperty(CustomProperty* prop) override; +}; +} // namespace rive + +#endif \ No newline at end of file
diff --git a/src/animation/interpolating_keyframe.cpp b/src/animation/interpolating_keyframe.cpp index 7f7a0da..bf3a6cb 100644 --- a/src/animation/interpolating_keyframe.cpp +++ b/src/animation/interpolating_keyframe.cpp
@@ -1,6 +1,8 @@ #include "rive/animation/interpolating_keyframe.hpp" #include "rive/animation/keyframe_interpolator.hpp" +#include "rive/animation/linear_animation_instance.hpp" #include "rive/core_context.hpp" +#include "rive/scripted/scripted_interpolator.hpp" using namespace rive; @@ -17,4 +19,21 @@ } return StatusCode::Ok; +} + +KeyFrameInterpolator* InterpolatingKeyFrame::effectiveInterpolator( + const LinearAnimationInstance* context) const +{ + KeyFrameInterpolator* shared = m_interpolator; + if (shared == nullptr || context == nullptr || + !shared->is<ScriptedInterpolator>()) + { + return shared; + } + auto* sharedSI = static_cast<const ScriptedInterpolator*>(shared); + if (auto* stateful = context->statefulInterpolator(this, sharedSI)) + { + return stateful; + } + return shared; } \ No newline at end of file
diff --git a/src/animation/keyed_object.cpp b/src/animation/keyed_object.cpp index 5db97ad..041c868 100644 --- a/src/animation/keyed_object.cpp +++ b/src/animation/keyed_object.cpp
@@ -73,7 +73,10 @@ } } -void KeyedObject::apply(Artboard* artboard, float time, float mix) +void KeyedObject::apply(Artboard* artboard, + float time, + float mix, + const LinearAnimationInstance* context) { Core* object = artboard->resolve(objectId()); if (object == nullptr) @@ -86,7 +89,7 @@ { continue; } - property->apply(object, time, mix); + property->apply(object, time, mix, context); } }
diff --git a/src/animation/keyed_property.cpp b/src/animation/keyed_property.cpp index 2f35ef5..1883f98 100644 --- a/src/animation/keyed_property.cpp +++ b/src/animation/keyed_property.cpp
@@ -97,7 +97,10 @@ } } -void KeyedProperty::apply(Core* object, float seconds, float mix) +void KeyedProperty::apply(Core* object, + float seconds, + float mix, + const LinearAnimationInstance* context) { assert(!m_keyFrames.empty()); @@ -141,7 +144,8 @@ pk, seconds, toFrame, - actualMix); + actualMix, + context); } } }
diff --git a/src/animation/keyframe_bool.cpp b/src/animation/keyframe_bool.cpp index 2d89d8d..2e69ec5 100644 --- a/src/animation/keyframe_bool.cpp +++ b/src/animation/keyframe_bool.cpp
@@ -12,7 +12,9 @@ int propertyKey, float currentTime, const KeyFrame* nextFrame, - float mix) + float mix, + const LinearAnimationInstance* context) { + (void)context; CoreRegistry::setBool(object, propertyKey, value()); } \ No newline at end of file
diff --git a/src/animation/keyframe_color.cpp b/src/animation/keyframe_color.cpp index 01ad723..91ec544 100644 --- a/src/animation/keyframe_color.cpp +++ b/src/animation/keyframe_color.cpp
@@ -27,13 +27,15 @@ int propertyKey, float currentTime, const KeyFrame* nextFrame, - float mix) + float mix, + const LinearAnimationInstance* context) { auto kfc = nextFrame->as<KeyFrameColor>(); const KeyFrameColor& nextColor = *kfc; float f = (currentTime - seconds()) / (nextColor.seconds() - seconds()); - if (KeyFrameInterpolator* keyframeInterpolator = interpolator()) + if (KeyFrameInterpolator* keyframeInterpolator = + effectiveInterpolator(context)) { f = keyframeInterpolator->transform(f); }
diff --git a/src/animation/keyframe_double.cpp b/src/animation/keyframe_double.cpp index d2feb76..1ee3749 100644 --- a/src/animation/keyframe_double.cpp +++ b/src/animation/keyframe_double.cpp
@@ -33,14 +33,16 @@ int propertyKey, float currentTime, const KeyFrame* nextFrame, - float mix) + float mix, + const LinearAnimationInstance* context) { auto kfd = nextFrame->as<KeyFrameDouble>(); const KeyFrameDouble& nextDouble = *kfd; float f = (currentTime - seconds()) / (nextDouble.seconds() - seconds()); float frameValue; - if (KeyFrameInterpolator* keyframeInterpolator = interpolator()) + if (KeyFrameInterpolator* keyframeInterpolator = + effectiveInterpolator(context)) { frameValue = keyframeInterpolator->transformValue(value(), nextDouble.value(),
diff --git a/src/animation/keyframe_id.cpp b/src/animation/keyframe_id.cpp index dfa8fcc..a1aabd7 100644 --- a/src/animation/keyframe_id.cpp +++ b/src/animation/keyframe_id.cpp
@@ -12,7 +12,9 @@ int propertyKey, float currentTime, const KeyFrame* nextFrame, - float mix) + float mix, + const LinearAnimationInstance* context) { + (void)context; CoreRegistry::setUint(object, propertyKey, value()); } \ No newline at end of file
diff --git a/src/animation/keyframe_string.cpp b/src/animation/keyframe_string.cpp index 7968dda..caa4e20 100644 --- a/src/animation/keyframe_string.cpp +++ b/src/animation/keyframe_string.cpp
@@ -12,7 +12,9 @@ int propertyKey, float currentTime, const KeyFrame* nextFrame, - float mix) + float mix, + const LinearAnimationInstance* context) { + (void)context; CoreRegistry::setString(object, propertyKey, value()); } \ No newline at end of file
diff --git a/src/animation/keyframe_uint.cpp b/src/animation/keyframe_uint.cpp index 398abe3..4268f9f 100644 --- a/src/animation/keyframe_uint.cpp +++ b/src/animation/keyframe_uint.cpp
@@ -12,7 +12,9 @@ int propertyKey, float currentTime, const KeyFrame* nextFrame, - float mix) + float mix, + const LinearAnimationInstance* context) { + (void)context; CoreRegistry::setUint(object, propertyKey, value()); } \ No newline at end of file
diff --git a/src/animation/linear_animation.cpp b/src/animation/linear_animation.cpp index 2af3576..55a8671 100644 --- a/src/animation/linear_animation.cpp +++ b/src/animation/linear_animation.cpp
@@ -68,7 +68,10 @@ m_KeyedObjects.push_back(std::move(object)); } -void LinearAnimation::apply(Artboard* artboard, float time, float mix) const +void LinearAnimation::apply(Artboard* artboard, + float time, + float mix, + const LinearAnimationInstance* context) const { if (quantize()) { @@ -77,7 +80,7 @@ } for (const auto& object : m_KeyedObjects) { - object->apply(artboard, time, mix); + object->apply(artboard, time, mix, context); } }
diff --git a/src/animation/linear_animation_instance.cpp b/src/animation/linear_animation_instance.cpp index 0bcb8f4..623c2ab 100644 --- a/src/animation/linear_animation_instance.cpp +++ b/src/animation/linear_animation_instance.cpp
@@ -1,8 +1,12 @@ #include "rive/animation/linear_animation_instance.hpp" +#include "rive/animation/interpolating_keyframe.hpp" #include "rive/animation/linear_animation.hpp" #include "rive/animation/loop.hpp" #include "rive/animation/keyed_callback_reporter.hpp" +#include "rive/assets/script_asset.hpp" +#include "rive/data_bind/data_bind.hpp" #include "rive/profiler/profiler_macros.h" +#include "rive/scripted/scripted_interpolator.hpp" #include <cmath> #include <cassert> @@ -38,7 +42,99 @@ m_loopValue(lhs.m_loopValue) {} -LinearAnimationInstance::~LinearAnimationInstance() {} +LinearAnimationInstance::~LinearAnimationInstance() +{ + // Critical teardown order, mirroring the SMI pattern at + // state_machine_instance.cpp:2011-2044: pull cloned data binds out of + // the artboard and delete them BEFORE m_scriptedInterpolatorInstances + // destroys the clones whose CustomPropertys are those binds' targets. + // Without this, the next Artboard::updateDataBinds() (which runs every + // frame from updatePass) reads through DataBind::target() into freed + // memory. + if (m_artboardInstance != nullptr) + { + for (auto* bind : m_clonedArtboardDataBinds) + { + m_artboardInstance->removeDataBind(bind); + delete bind; + } + } + m_clonedArtboardDataBinds.clear(); + // m_scriptedInterpolatorInstances destructs here via unique_ptr; safe now + // that no DataBind still points at the clones' CustomPropertys. +} + +// Returns a per-(this LAI, keyframe) stateful clone of the given shared +// ScriptedInterpolator. Mirrors StateMachineInstance::scriptedObjects but +// keyed by the keyframe pointer so distinct keyframes that share a script +// each get their own Lua `self` table. Lazily allocates the map and the +// clone on first hit; subsequent calls return the cached clone. Cleanup +// runs in ~LinearAnimationInstance via unique_ptr. +ScriptedInterpolator* LinearAnimationInstance::statefulInterpolator( + const InterpolatingKeyFrame* keyframe, + const ScriptedInterpolator* shared) const +{ + if (shared == nullptr || keyframe == nullptr) + { + return nullptr; + } + if (m_scriptedInterpolatorInstances == nullptr) + { + m_scriptedInterpolatorInstances = std::make_unique< + std::unordered_map<const InterpolatingKeyFrame*, + std::unique_ptr<ScriptedInterpolator>>>(); + } + auto& map = *m_scriptedInterpolatorInstances; + auto it = map.find(keyframe); + if (it != map.end()) + { + return it->second.get(); + } + + // cloneScriptedObject is non-const on the source; the shared template is + // logically read-only here so the const_cast is safe and matches the + // ScriptedListenerAction pattern. + auto* cloneAsObj = + const_cast<ScriptedInterpolator*>(shared)->cloneScriptedObject( + m_artboardInstance); + if (cloneAsObj == nullptr) + { + return nullptr; + } + auto* clone = static_cast<ScriptedInterpolator*>(cloneAsObj); + clone->dataContext(m_artboardInstance->dataContext()); + + // Walk the clone's ScriptInputs to discover the binds cloneProperties + // just parked on the artboard for this clone. The back-pointer is set + // inside cloneProperties (scripted_object.cpp) immediately after + // dataBindContainer->addDataBind(...). We track these so ~LAI can + // removeDataBind + delete each one BEFORE the clone — whose + // CustomProperty is the bind's target — is destroyed. + for (auto* prop : clone->customProperties()) + { + if (auto* input = ScriptInput::from(prop)) + { + if (auto* bind = input->dataBind()) + { + m_clonedArtboardDataBinds.push_back(bind); + } + } + } + + // Late-binding safety net: reinit() inside cloneScriptedObject is a no-op + // when scriptAsset() is null at clone time. If the asset became available + // later (editor live-edit), init now. ensureScriptInitialized + // short-circuits when already initialized so this stays cheap. + if (clone->scriptAsset() != nullptr && !clone->userLuaInitDone()) + { + clone->scriptAsset()->initScriptedObject(clone); + clone->hydrateScriptInputs(); + } + + auto* raw = clone; + map.emplace(keyframe, std::unique_ptr<ScriptedInterpolator>(clone)); + return raw; +} bool LinearAnimationInstance::advanceAndApply(float seconds) {
diff --git a/src/assets/script_asset.cpp b/src/assets/script_asset.cpp index 900085a..1fc0726 100644 --- a/src/assets/script_asset.cpp +++ b/src/assets/script_asset.cpp
@@ -108,7 +108,8 @@ scriptProtocol == ScriptProtocol::converter || scriptProtocol == ScriptProtocol::pathEffect || scriptProtocol == ScriptProtocol::listenerAction || - scriptProtocol == ScriptProtocol::transitionCondition) + scriptProtocol == ScriptProtocol::transitionCondition || + scriptProtocol == ScriptProtocol::interpolator) { if (static_cast<lua_Type>(lua_getfield(state, -1, "update")) == LUA_TFUNCTION)
diff --git a/src/data_bind/data_bind_container.cpp b/src/data_bind/data_bind_container.cpp index 221c252..e3b70db 100644 --- a/src/data_bind/data_bind_container.cpp +++ b/src/data_bind/data_bind_container.cpp
@@ -19,6 +19,7 @@ { dataBind->unbind(); } + m_dataContext = nullptr; } void DataBindContainer::bindDataBindsFromContext(DataContext* dataContext) @@ -31,6 +32,7 @@ dataBind->as<DataBindContext>()->bindFromContext(dataContext); } } + m_dataContext = dataContext; } bool DataBindContainer::advanceDataBinds(float elapsedSeconds) @@ -81,6 +83,11 @@ m_persistingDataBinds.push_back(dataBind); } dataBind->container(this); + if (m_dataContext && dataBind->is<DataBindContext>()) + { + dataBind->as<DataBindContext>()->bindFromContext(m_dataContext); + updateDataBind(dataBind, true); + } } void DataBindContainer::updateDataBind(DataBind* dataBind,
diff --git a/src/file.cpp b/src/file.cpp index b6e72f1..2e87d20 100644 --- a/src/file.cpp +++ b/src/file.cpp
@@ -76,6 +76,7 @@ #include "rive/scripted/scripted_layout.hpp" #include "rive/scripted/scripted_object.hpp" #include "rive/scripted/scripted_path_effect.hpp" +#include "rive/scripted/scripted_interpolator.hpp" #include "rive/viewmodel/viewmodel.hpp" #include "rive/viewmodel/data_enum.hpp" #include "rive/viewmodel/viewmodel_instance.hpp" @@ -585,6 +586,7 @@ case ScriptedPathEffect::typeKey: case ScriptedListenerAction::typeKey: case ScriptedTransitionCondition::typeKey: + case ScriptedInterpolator::typeKey: { auto scriptedObject = ScriptedObject::from(object); if (scriptedObject != nullptr) @@ -634,6 +636,11 @@ m_keyframeInterpolators.push_back( object->as<KeyFrameInterpolator>()); } + if (object->is<ScriptedInterpolator>()) + { + m_scriptedInterpolators.push_back( + object->as<ScriptedInterpolator>()); + } } else if (object->is<ScrollPhysics>()) { @@ -698,6 +705,15 @@ // and retries vm->performRegistration(); } + + for (auto& interpolator : m_scriptedInterpolators) + { + auto scriptAsset = interpolator->scriptAsset(); + if (scriptAsset != nullptr) + { + scriptAsset->initScriptedObject(interpolator); + } + } } }
diff --git a/src/generated/scripted/scripted_interpolator_base.cpp b/src/generated/scripted/scripted_interpolator_base.cpp new file mode 100644 index 0000000..778e3d9 --- /dev/null +++ b/src/generated/scripted/scripted_interpolator_base.cpp
@@ -0,0 +1,11 @@ +#include "rive/generated/scripted/scripted_interpolator_base.hpp" +#include "rive/scripted/scripted_interpolator.hpp" + +using namespace rive; + +Core* ScriptedInterpolatorBase::clone() const +{ + auto cloned = new ScriptedInterpolator(); + cloned->copy(*this); + return cloned; +}
diff --git a/src/lua/lua_state.cpp b/src/lua/lua_state.cpp index 6972551..53ac079 100644 --- a/src/lua/lua_state.cpp +++ b/src/lua/lua_state.cpp
@@ -4,6 +4,7 @@ #include "rive/lua/rive_lua_libs.hpp" #endif #include "rive/viewmodel/viewmodel.hpp" +#include "rive/scripted/scripted_interpolator.hpp" #include <stdio.h> using namespace rive;
diff --git a/src/scripted/scripted_interpolator.cpp b/src/scripted/scripted_interpolator.cpp new file mode 100644 index 0000000..535fab5 --- /dev/null +++ b/src/scripted/scripted_interpolator.cpp
@@ -0,0 +1,150 @@ +#ifdef WITH_RIVE_SCRIPTING +#include "rive/lua/rive_lua_libs.hpp" +#endif +#include "rive/component_dirt.hpp" +#include "rive/assets/script_asset.hpp" +#include "rive/scripted/scripted_interpolator.hpp" + +using namespace rive; + +#ifdef WITH_RIVE_SCRIPTING + +// Mirrors the Dart `_interpolatorBoilerplate` defaults: identity for +// `transform`, linear interpolation for `transformValue`. Used whenever the +// Lua side isn't ready (no VM, no `m_self`), the script doesn't define the +// method, or the pcall fails — so the runtime degrades to standard linear +// behavior instead of producing garbage. +static inline float defaultTransformValue(float from, float to, float factor) +{ + return from + (to - from) * factor; +} + +float ScriptedInterpolator::transform(float factor) const +{ + lua_State* L = state(); + if (L == nullptr || m_self == 0) + { + return factor; + } + // Stack: [] + rive_lua_pushRef(L, m_self); + // Stack: [self] + lua_getfield(L, -1, "transform"); + // Stack: [self, transform-or-nil] + if (static_cast<lua_Type>(lua_type(L, -1)) != LUA_TFUNCTION) + { + // Method not implemented in user script — fall back to identity. + rive_lua_pop(L, 2); + return factor; + } + lua_pushvalue(L, -2); + // Stack: [self, transform, self] + lua_pushnumber(L, factor); + // Stack: [self, transform, self, factor] + // pcall_with_context wants a non-const ScriptedObject*; transform() is + // const because it doesn't mutate the C++ object — only the Lua VM. + if (static_cast<lua_Status>( + rive_lua_pcall_with_context(L, + const_cast<ScriptedInterpolator*>(this), + 2, + 1)) != LUA_OK) + { + // Stack: [self, errorMessage] + rive_lua_pop(L, 2); + return factor; + } + // Stack: [self, result] + float result = static_cast<float>(lua_tonumber(L, -1)); + rive_lua_pop(L, 2); + return result; +} + +float ScriptedInterpolator::transformValue(float valueFrom, + float valueTo, + float factor) +{ + lua_State* L = state(); + if (L == nullptr || m_self == 0) + { + return defaultTransformValue(valueFrom, valueTo, factor); + } + // Stack: [] + rive_lua_pushRef(L, m_self); + // Stack: [self] + lua_getfield(L, -1, "transformValue"); + // Stack: [self, transformValue] + lua_pushvalue(L, -2); + // Stack: [self, transformValue, self] + lua_pushnumber(L, valueFrom); + lua_pushnumber(L, valueTo); + lua_pushnumber(L, factor); + // Stack: [self, transformValue, self, from, to, factor] + if (static_cast<lua_Status>(rive_lua_pcall_with_context(L, this, 4, 1)) != + LUA_OK) + { + // Stack: [self, errorMessage] + rive_lua_pop(L, 2); + return defaultTransformValue(valueFrom, valueTo, factor); + } + // Stack: [self, result] + float result = static_cast<float>(lua_tonumber(L, -1)); + rive_lua_pop(L, 2); + return result; +} + +#else + +float ScriptedInterpolator::transform(float factor) const { return factor; } + +float ScriptedInterpolator::transformValue(float valueFrom, + float valueTo, + float factor) +{ + return valueFrom + (valueTo - valueFrom) * factor; +} + +#endif + +void ScriptedInterpolator::addProperty(CustomProperty* prop) +{ + auto* scriptInput = ScriptInput::from(prop); + if (scriptInput != nullptr) + { + scriptInput->scriptedObject(this); + } + CustomPropertyContainer::addProperty(prop); +} + +StatusCode ScriptedInterpolator::import(ImportStack& importStack) +{ + auto result = registerReferencer(importStack); + if (result != StatusCode::Ok) + { + return result; + } + return Super::import(importStack); +} + +// Mirrors ScriptedListenerAction::clone — preserve the file asset reference +// across the clone so the new instance vends the same script bytecode. +Core* ScriptedInterpolator::clone() const +{ + auto* twin = ScriptedInterpolatorBase::clone()->as<ScriptedInterpolator>(); + if (m_fileAsset != nullptr) + { + twin->setAsset(m_fileAsset); + } + return twin; +} + +// Used by LinearAnimationInstance::statefulInterpolator() to vend a per-(LAI, +// keyframe) Lua instance lazily. `dataBindContainer` is the +// ArtboardInstance — it owns any cloned data binds. +ScriptedObject* ScriptedInterpolator::cloneScriptedObject( + DataBindContainer* dataBindContainer) const +{ + auto* cloned = clone()->as<ScriptedInterpolator>(); + cloneProperties(cloned, dataBindContainer); + cloned->reinit(); + return cloned; +}
diff --git a/src/scripted/scripted_object.cpp b/src/scripted/scripted_object.cpp index 5d56e49..b70cd87 100644 --- a/src/scripted/scripted_object.cpp +++ b/src/scripted/scripted_object.cpp
@@ -9,6 +9,7 @@ #include "rive/animation/scripted_transition_condition.hpp" #include "rive/scripted/scripted_data_converter.hpp" #include "rive/scripted/scripted_drawable.hpp" +#include "rive/scripted/scripted_interpolator.hpp" #include "rive/scripted/scripted_layout.hpp" #include "rive/scripted/scripted_path_effect.hpp" #include "rive/scripted/scripted_object.hpp" @@ -32,6 +33,8 @@ return object->as<ScriptedListenerAction>(); case ScriptedTransitionCondition::typeKey: return object->as<ScriptedTransitionCondition>(); + case ScriptedInterpolator::typeKey: + return object->as<ScriptedInterpolator>(); } return nullptr; } @@ -519,6 +522,11 @@ } dataBindClone->target(clonedValue); dataBindContainer->addDataBind(dataBindClone); + if (auto* clonedInput = ScriptInput::from(clonedValue)) + { + clonedInput->dataBind(dataBindClone, + /*ownsDataBind=*/false); + } } } }