add support for joystick time based dependents # Description fix for joystick controlled timelines not updating the time # Details If a state machine has a timeline controlling a nested remapped animation through keyframes, and that timeline is itself controlled by a joystick our update cycle would not apply the time updated by the joystick because the advance cycle has passed. This PR looks for joystick controlled animations, and stores which objects are nested remapped animations to advance them while they are being applied. Diffs= 038bd34aee add support for joystick time based dependents (#9272) Co-authored-by: hernan <hernan@rive.app>
diff --git a/.rive_head b/.rive_head index ae2b08b..84c64ea 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -a70f0f84dda65ddd956b1229b9530aed37f44b18 +038bd34aee08546d1395c93c9696d273488ff586
diff --git a/include/rive/joystick.hpp b/include/rive/joystick.hpp index ebe6381..fb317e0 100644 --- a/include/rive/joystick.hpp +++ b/include/rive/joystick.hpp
@@ -3,6 +3,7 @@ #include "rive/generated/joystick_base.hpp" #include "rive/intrinsically_sizeable.hpp" #include "rive/joystick_flags.hpp" +#include "rive/animation/nested_remap_animation.hpp" #include "rive/math/mat2d.hpp" #include <stdio.h> @@ -32,6 +33,8 @@ bool canApplyBeforeUpdate() const { return m_handleSource == nullptr; } + void addDependents(Artboard* artboard); + Vec2D measureLayout(float width, LayoutMeasureMode widthMode, float height, @@ -51,6 +54,9 @@ LinearAnimation* m_xAnimation = nullptr; LinearAnimation* m_yAnimation = nullptr; TransformComponent* m_handleSource = nullptr; + std::vector<NestedRemapAnimation*> m_dependents; + + void addAnimationDependents(Artboard* artboard, LinearAnimation* animation); }; } // namespace rive
diff --git a/src/artboard.cpp b/src/artboard.cpp index f81356c..1c734b6 100644 --- a/src/artboard.cpp +++ b/src/artboard.cpp
@@ -252,6 +252,7 @@ { m_JoysticksApplyBeforeUpdate = false; } + joystick->addDependents(this); m_Joysticks.push_back(joystick); break; }
diff --git a/src/joystick.cpp b/src/joystick.cpp index b84abbe..ea9d6e1 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp
@@ -1,6 +1,7 @@ #include "rive/joystick.hpp" #include "rive/artboard.hpp" #include "rive/transform_component.hpp" +#include "rive/animation/keyed_object.hpp" using namespace rive; @@ -95,6 +96,10 @@ ((isJoystickFlagged(JoystickFlags::invertY) ? -y() : y()) + 1.0f) / 2.0f * m_yAnimation->durationSeconds()); } + for (const auto& nestedRemapAnimation : m_dependents) + { + nestedRemapAnimation->advance(0, false); + } } Vec2D Joystick::measureLayout(float width, @@ -134,4 +139,32 @@ void Joystick::heightChanged() { artboard()->addDirt(ComponentDirt::Components); +} + +void Joystick::addAnimationDependents(Artboard* artboard, + LinearAnimation* animation) +{ + + auto totalObjects = animation->numKeyedObjects(); + for (int i = 0; i < totalObjects; i++) + { + auto object = animation->getObject(i); + auto coreObject = artboard->resolve(object->objectId()); + if (coreObject != nullptr && coreObject->is<NestedRemapAnimation>()) + { + m_dependents.push_back(coreObject->as<NestedRemapAnimation>()); + } + } +} + +void Joystick::addDependents(Artboard* artboard) +{ + if (m_yAnimation != nullptr) + { + addAnimationDependents(artboard, m_yAnimation); + } + if (m_xAnimation != nullptr) + { + addAnimationDependents(artboard, m_xAnimation); + } } \ No newline at end of file
diff --git a/tests/unit_tests/assets/joystick_nested_remap.riv b/tests/unit_tests/assets/joystick_nested_remap.riv new file mode 100644 index 0000000..33b0cd0 --- /dev/null +++ b/tests/unit_tests/assets/joystick_nested_remap.riv Binary files differ
diff --git a/tests/unit_tests/runtime/nested_artboard_test.cpp b/tests/unit_tests/runtime/nested_artboard_test.cpp index 34c6db4..92d6021 100644 --- a/tests/unit_tests/runtime/nested_artboard_test.cpp +++ b/tests/unit_tests/runtime/nested_artboard_test.cpp
@@ -68,4 +68,25 @@ REQUIRE(stateMachine->advanceAndApply(0.1f) == true); // animation has ended and no events to report REQUIRE(stateMachine->advanceAndApply(0.1f) == false); +} + +TEST_CASE( + "nested artboard remapped timelines controlled by a timeline controlled " + "itself by a joystick correctly apply the time after the joystick ran", + "[nested]") +{ + auto file = ReadRiveFile("assets/joystick_nested_remap.riv"); + auto artboard = file->artboard("parent")->instance(); + artboard->advance(0.0f); + auto stateMachine = artboard->stateMachineAt(0); + + REQUIRE(artboard->find<rive::NestedArtboard>("child") != nullptr); + + auto child = artboard->find<rive::NestedArtboard>("child"); + + auto nestedArtboardArtboard = child->artboardInstance(); + REQUIRE(nestedArtboardArtboard != nullptr); + auto rect = nestedArtboardArtboard->find<rive::Shape>("rect"); + REQUIRE(rect != nullptr); + REQUIRE(rect->x() == 250.0f); } \ No newline at end of file