Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with LinearAnimationInstance with no backing Animation. #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/rive/animation/linear_animation_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ namespace rive {
// between 0 and 1) is the strength at which the animation is mixed with
// other animations applied to the artboard.
void apply(float mix = 1.0f) const {
if (m_Animation == nullptr) {
return;
}
m_Animation->apply(m_ArtboardInstance, m_Time, mix);
}

Expand All @@ -69,7 +72,7 @@ namespace rive {
float speed() const;
float startSeconds() const;
std::string name() const;

// Returns either the animation's default or overridden loop values
Loop loop() { return (Loop)loopValue(); }
int loopValue();
Expand Down
41 changes: 31 additions & 10 deletions src/animation/linear_animation_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ LinearAnimationInstance::LinearAnimationInstance(const LinearAnimation* animatio
Artboard* instance) :
m_Animation(animation),
m_ArtboardInstance(instance),
m_Time(animation->enableWorkArea() ? (float)animation->workStart() / animation->fps() : 0),
m_Time(animation == nullptr
? 0.0f
: (animation->enableWorkArea() ? (float)animation->workStart() / animation->fps()
: 0.0f)),
m_TotalTime(0.0f),
m_LastTotalTime(0.0f),
m_SpilledTime(0.0f),
m_Direction(1) {}

bool LinearAnimationInstance::advance(float elapsedSeconds) {
if (m_Animation == nullptr) {
/// No backing animation, we can early out and tell the advancer that we
/// no longer need to advance.
return false;
}
const LinearAnimation& animation = *m_Animation;
m_Time += elapsedSeconds * animation.speed() * m_Direction;
m_LastTotalTime = m_TotalTime;
Expand Down Expand Up @@ -105,7 +113,10 @@ void LinearAnimationInstance::time(float value) {
// can track change even when setting time.
auto diff = m_TotalTime - m_LastTotalTime;

int start = (m_Animation->enableWorkArea() ? m_Animation->workStart() : 0) * m_Animation->fps();
int start =
(m_Animation == nullptr
? 0.0f
: (m_Animation->enableWorkArea() ? m_Animation->workStart() : 0) * m_Animation->fps());
m_TotalTime = value - start;
m_LastTotalTime = m_TotalTime - diff;

Expand All @@ -114,35 +125,45 @@ void LinearAnimationInstance::time(float value) {
m_Direction = 1;
}

uint32_t LinearAnimationInstance::fps() const { return m_Animation->fps(); }
uint32_t LinearAnimationInstance::fps() const {
return m_Animation == nullptr ? 60 : m_Animation->fps();
}

uint32_t LinearAnimationInstance::duration() const { return m_Animation->duration(); }
uint32_t LinearAnimationInstance::duration() const {
return m_Animation == nullptr ? 0 : m_Animation->duration();
}

float LinearAnimationInstance::speed() const { return m_Animation->speed(); }
float LinearAnimationInstance::speed() const {
return m_Animation == nullptr ? 1.0f : m_Animation->speed();
}

float LinearAnimationInstance::startSeconds() const { return m_Animation->startSeconds(); }
float LinearAnimationInstance::startSeconds() const {
return m_Animation == nullptr ? 0.0f : m_Animation->startSeconds();
}

std::string LinearAnimationInstance::name() const {
return m_Animation->name();
return m_Animation == nullptr ? "???" : m_Animation->name();
}

// Returns either the animation's default or overridden loop values
int LinearAnimationInstance::loopValue() {
if (m_LoopValue != -1) {
return m_LoopValue;
}
return m_Animation->loopValue();
return m_Animation == nullptr ? 0 : m_Animation->loopValue();
}

// Override the animation's loop value
void LinearAnimationInstance::loopValue(int value) {
if (m_LoopValue == value) {
return;
}
if (m_LoopValue == -1 && m_Animation->loopValue() == value) {
if (m_LoopValue == -1 && m_Animation != nullptr && m_Animation->loopValue() == value) {
return;
}
m_LoopValue = value;
}

float LinearAnimationInstance::durationSeconds() const { return m_Animation->durationSeconds(); }
float LinearAnimationInstance::durationSeconds() const {
return m_Animation == nullptr ? 0.0f : m_Animation->durationSeconds();
}