Skip to content

Commit

Permalink
fix state machine advanceAndApply
Browse files Browse the repository at this point in the history
this almost fixes the issue with ios. but somehow this ends up returning false sometimes still... it seem to stop in a similar position each time, a little nudge being enough to get it moving again for a longer while

also

```
- (bool)advanceBy:(double)elapsedSeconds
{
    return instance->advanceAndApply(elapsedSeconds);
}
```

note the intervals at which we call this function are somewhat laughable,

![CleanShot 2024-05-01 at 16 07 05](https://github.com/rive-app/rive/assets/1216025/694e61bf-8d0c-465e-9676-af221d6be174)

Diffs=
8db7cac50 fix state machine advanceAndApply (#7183)

Co-authored-by: Maxwell Talbot <talbot.maxwell@gmail.com>
  • Loading branch information
mjtalbot and mjtalbot committed May 2, 2024
1 parent 331c05a commit efe0382
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a55f1ffb6cabe9b918fa0b8fd1ad11b4d656ae1c
8db7cac50c53ab10838da0cd80c869917c51c72a
2 changes: 1 addition & 1 deletion include/rive/animation/nested_remap_animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class NestedRemapAnimation : public NestedRemapAnimationBase
{
public:
void timeChanged() override;
void advance(float elapsedSeconds) override;
bool advance(float elapsedSeconds) override;
void initializeAnimation(ArtboardInstance*) override;
};
} // namespace rive
Expand Down
2 changes: 1 addition & 1 deletion include/rive/animation/nested_simple_animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace rive
class NestedSimpleAnimation : public NestedSimpleAnimationBase
{
public:
void advance(float elapsedSeconds) override;
bool advance(float elapsedSeconds) override;
};
} // namespace rive

Expand Down
2 changes: 1 addition & 1 deletion include/rive/animation/nested_state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class NestedStateMachine : public NestedStateMachineBase
public:
NestedStateMachine();
~NestedStateMachine() override;
void advance(float elapsedSeconds) override;
bool advance(float elapsedSeconds) override;
void initializeAnimation(ArtboardInstance*) override;
StateMachineInstance* stateMachineInstance();

Expand Down
2 changes: 1 addition & 1 deletion include/rive/nested_animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NestedAnimation : public NestedAnimationBase
StatusCode onAddedDirty(CoreContext* context) override;

// Advance animations and apply them to the artboard.
virtual void advance(float elapsedSeconds) = 0;
virtual bool advance(float elapsedSeconds) = 0;

// Initialize the animation (make instances as necessary) from the
// source artboard.
Expand Down
5 changes: 4 additions & 1 deletion src/animation/nested_remap_animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ void NestedRemapAnimation::initializeAnimation(ArtboardInstance* artboard)
timeChanged();
}

void NestedRemapAnimation::advance(float elapsedSeconds)
bool NestedRemapAnimation::advance(float elapsedSeconds)
{
bool keepGoing = false;
if (m_AnimationInstance != nullptr && mix() != 0.0f)
{
m_AnimationInstance->apply(mix());
keepGoing = true;
}
return keepGoing;
}
6 changes: 4 additions & 2 deletions src/animation/nested_simple_animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

using namespace rive;

void NestedSimpleAnimation::advance(float elapsedSeconds)
bool NestedSimpleAnimation::advance(float elapsedSeconds)
{
bool keepGoing = false;
if (m_AnimationInstance != nullptr)
{
if (isPlaying())
{
m_AnimationInstance->advance(elapsedSeconds * speed());
keepGoing = m_AnimationInstance->advance(elapsedSeconds * speed());
}
if (mix() != 0.0f)
{
m_AnimationInstance->apply(mix());
}
}
return keepGoing;
}
6 changes: 4 additions & 2 deletions src/animation/nested_state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ using namespace rive;
NestedStateMachine::NestedStateMachine() {}
NestedStateMachine::~NestedStateMachine() {}

void NestedStateMachine::advance(float elapsedSeconds)
bool NestedStateMachine::advance(float elapsedSeconds)
{
bool keepGoing = false;
if (m_StateMachineInstance != nullptr)
{
m_StateMachineInstance->advance(elapsedSeconds);
keepGoing = m_StateMachineInstance->advance(elapsedSeconds);
}
return keepGoing;
}

void NestedStateMachine::initializeAnimation(ArtboardInstance* artboard)
Expand Down
6 changes: 3 additions & 3 deletions src/animation/state_machine_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,9 @@ bool StateMachineInstance::advance(float seconds)

bool StateMachineInstance::advanceAndApply(float seconds)
{
bool more = this->advance(seconds);
m_artboardInstance->advance(seconds);
return more;
bool keepGoing = this->advance(seconds);
keepGoing = m_artboardInstance->advance(seconds) || keepGoing;
return keepGoing;
}

void StateMachineInstance::markNeedsAdvance() { m_needsAdvance = true; }
Expand Down
7 changes: 4 additions & 3 deletions src/nested_artboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ StatusCode NestedArtboard::onAddedClean(CoreContext* context)

bool NestedArtboard::advance(float elapsedSeconds)
{
bool keepGoing = false;
if (m_Artboard == nullptr || isCollapsed())
{
return false;
return keepGoing;
}
for (auto animation : m_NestedAnimations)
{
animation->advance(elapsedSeconds);
keepGoing = keepGoing || animation->advance(elapsedSeconds);
}
return m_Artboard->advance(elapsedSeconds);
return m_Artboard->advance(elapsedSeconds) || keepGoing;
}

void NestedArtboard::update(ComponentDirt value)
Expand Down
Binary file added test/assets/ball_test.riv
Binary file not shown.
26 changes: 26 additions & 0 deletions test/nested_artboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,30 @@ TEST_CASE("collapsed nested artboards do not advance", "[solo]")
auto greenMovingShapes = greenNestedArtboardArtboard->find<rive::Shape>();
auto greenRect = greenMovingShapes.at(0);
REQUIRE(greenRect->x() == 50);
}

TEST_CASE("nested artboards with looping animations will keep main advanceAndApply advancing",
"[nested]")
{
auto file = ReadRiveFile("../../test/assets/ball_test.riv");
auto artboard = file->artboard("Artboard")->instance();
artboard->advance(0.0f);
auto stateMachine = artboard->stateMachineAt(0);
REQUIRE(stateMachine->advanceAndApply(0.0f) == true);
REQUIRE(stateMachine->advanceAndApply(1.0f) == true);
REQUIRE(stateMachine->advanceAndApply(1.0f) == true);
}
TEST_CASE("nested artboards with one shot animations will not main advanceAndApply advancing",
"[nested]")
{

auto file = ReadRiveFile("../../test/assets/ball_test.riv");
auto artboard = file->artboard("Artboard 2")->instance();
artboard->advance(0.0f);
auto stateMachine = artboard->stateMachineAt(0);
REQUIRE(stateMachine->advanceAndApply(0.0f) == true);
REQUIRE(stateMachine->advanceAndApply(0.9f) == true);
REQUIRE(stateMachine->advanceAndApply(0.1f) == true);
// nested artboards animation is 1s long
REQUIRE(stateMachine->advanceAndApply(0.1f) == false);
}

0 comments on commit efe0382

Please sign in to comment.