Skip to content

Commit

Permalink
Add AdvancingComponent & animate flag to advance()
Browse files Browse the repository at this point in the history
@luigi-rosso I pulled the AdvancingComponent stuff from the scroll branch into this, so I could build on top of it.

This PR addresses a couple of things we discussed around Layout animation:
- Added an animate param to advance which tells the advancing component whether to animate or not. This allowed the addition of a toggle for layout animation because it can be annoying to have the animation running while editing.
- Also prevent animation at runtime startup by interrupting it if ComponentDirt is filthy. This also prevents startup animation whenever a nested artboard is regenerated in editor.

Diffs=
0c8fa5c313 Add AdvancingComponent & animate flag to advance() (#8362)

Co-authored-by: Philip Chung <philterdesign@gmail.com>
  • Loading branch information
philter and philter committed Oct 21, 2024
1 parent 3a8939c commit fc62506
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d1372c444fa3aaada618ce16032327e4117057b9
0c8fa5c31311059d7136d040e4f470eee3edc106
7 changes: 5 additions & 2 deletions include/rive/artboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,13 @@ class Artboard : public ArtboardBase, public CoreContext
bool syncStyleChanges();
bool canHaveOverrides() override { return true; }

bool advance(double elapsedSeconds, bool nested = true);
bool advance(double elapsedSeconds,
bool nested = true,
bool animate = true);
bool advanceInternal(double elapsedSeconds,
bool isRoot,
bool nested = true);
bool nested = true,
bool animate = true);
bool hasChangedDrawOrderInLastUpdate()
{
return m_HasChangedDrawOrderInLastUpdate;
Expand Down
8 changes: 8 additions & 0 deletions include/rive/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class Component : public ComponentBase
return (m_Dirt & ComponentDirt::Collapsed) == ComponentDirt::Collapsed;
}
};

class AdvancingComponent
{
public:
virtual bool advanceComponent(double elapsedSeconds,
bool animate = true) = 0;
static AdvancingComponent* from(Component* component);
};
} // namespace rive

#endif
8 changes: 5 additions & 3 deletions include/rive/layout_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct LayoutAnimationData
class LayoutComponent : public LayoutComponentBase,
public ProxyDrawing,
public ShapePaintContainer,
public AdvancingComponent,
public InterpolatorHost
{
protected:
Expand Down Expand Up @@ -90,7 +91,7 @@ class LayoutComponent : public LayoutComponentBase,
YGStyle& layoutStyle() { return m_layoutData->style; }
void syncLayoutChildren();
void propagateSizeToChildren(ContainerComponent* component);
bool applyInterpolation(double elapsedSeconds);
bool applyInterpolation(double elapsedSeconds, bool animate = true);

protected:
void calculateLayout();
Expand Down Expand Up @@ -141,6 +142,7 @@ class LayoutComponent : public LayoutComponentBase,
bool mainAxisIsRow();
bool mainAxisIsColumn();
bool overridesKeyedInterpolation(int propertyKey) override;
bool advanceComponent(double elapsedSeconds, bool animate = true) override;

#ifdef WITH_RIVE_LAYOUT
LayoutComponent() :
Expand All @@ -152,11 +154,10 @@ class LayoutComponent : public LayoutComponentBase,
~LayoutComponent() { delete m_backgroundRect; }
void syncStyle();
virtual void propagateSize();
void updateLayoutBounds();
void updateLayoutBounds(bool animate = true);
StatusCode onAddedDirty(CoreContext* context) override;
StatusCode onAddedClean(CoreContext* context) override;

bool advance(double elapsedSeconds);
bool animates();
LayoutAnimationStyle animationStyle();
KeyFrameInterpolator* interpolator();
Expand All @@ -171,6 +172,7 @@ class LayoutComponent : public LayoutComponentBase,
KeyFrameInterpolator* inheritedInterpolator,
float inheritedInterpolationTime);
void clearInheritedInterpolation();
void interruptAnimation();
bool isLeaf();
void positionTypeChanged();
void scaleTypeChanged();
Expand Down
20 changes: 9 additions & 11 deletions src/artboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,10 @@ bool Artboard::syncStyleChanges()
return updated;
}

bool Artboard::advanceInternal(double elapsedSeconds, bool isRoot, bool nested)
bool Artboard::advanceInternal(double elapsedSeconds,
bool isRoot,
bool nested,
bool animate)
{
bool didUpdate = false;
m_HasChangedDrawOrderInLastUpdate = false;
Expand All @@ -774,15 +777,10 @@ bool Artboard::advanceInternal(double elapsedSeconds, bool isRoot, bool nested)

for (auto dep : m_DependencyOrder)
{
if (dep->is<LayoutComponent>())
auto adv = AdvancingComponent::from(dep);
if (adv != nullptr && adv->advanceComponent(elapsedSeconds, animate))
{
auto layout = dep->as<LayoutComponent>();
layout->updateLayoutBounds();
if ((dep == this && Super::advance(elapsedSeconds)) ||
(dep != this && layout->advance(elapsedSeconds)))
{
didUpdate = true;
}
didUpdate = true;
}
}

Expand Down Expand Up @@ -841,9 +839,9 @@ bool Artboard::advanceInternal(double elapsedSeconds, bool isRoot, bool nested)
return didUpdate;
}

bool Artboard::advance(double elapsedSeconds, bool nested)
bool Artboard::advance(double elapsedSeconds, bool nested, bool animate)
{
return advanceInternal(elapsedSeconds, true, nested);
return advanceInternal(elapsedSeconds, true, nested, animate);
}

Core* Artboard::hitTest(HitInfo* hinfo, const Mat2D& xform)
Expand Down
13 changes: 13 additions & 0 deletions src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "rive/core_context.hpp"
#include "rive/importers/artboard_importer.hpp"
#include "rive/importers/import_stack.hpp"
#include "rive/layout_component.hpp"
#include <algorithm>

using namespace rive;
Expand Down Expand Up @@ -93,4 +94,16 @@ bool Component::collapse(bool value)
onDirty(m_Dirt);
m_DependencyHelper.onComponentDirty(this);
return true;
}

AdvancingComponent* AdvancingComponent::from(Component* component)
{
switch (component->coreType())
{
case LayoutComponent::typeKey:
return component->as<LayoutComponent>();
case Artboard::typeKey:
return component->as<Artboard>();
}
return nullptr;
}
39 changes: 33 additions & 6 deletions src/layout_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ void LayoutComponent::updateRenderPath()
void LayoutComponent::update(ComponentDirt value)
{
Super::update(value);
#ifdef WITH_RIVE_LAYOUT
if (value == ComponentDirt::Filthy)
{
// Use this to prevent layout animation on startup
interruptAnimation();
}
#endif
if (hasDirt(value, ComponentDirt::RenderOpacity))
{
propagateOpacity(childOpacity());
Expand Down Expand Up @@ -688,7 +695,7 @@ void LayoutComponent::onDirty(ComponentDirt value)
}
}

void LayoutComponent::updateLayoutBounds()
void LayoutComponent::updateLayoutBounds(bool animate)
{
auto node = &layoutNode();
auto left = YGNodeLayoutGetLeft(node);
Expand All @@ -711,7 +718,7 @@ void LayoutComponent::updateLayoutBounds()
return;
}
#endif
if (animates())
if (animate && animates())
{
auto toBounds = m_animationData.toBounds;
if (left != toBounds.left() || top != toBounds.top() ||
Expand Down Expand Up @@ -746,15 +753,17 @@ void LayoutComponent::updateLayoutBounds()
m_layoutLocationY = top;
m_layoutSizeWidth = width;
m_layoutSizeHeight = height;
m_animationData.toBounds = AABB(left, top, left + width, top + height);

propagateSize();
markWorldTransformDirty();
}
}

bool LayoutComponent::advance(double elapsedSeconds)
bool LayoutComponent::advanceComponent(double elapsedSeconds, bool animate)
{
return applyInterpolation(elapsedSeconds);
updateLayoutBounds(animate);
return applyInterpolation(elapsedSeconds, animate);
}

bool LayoutComponent::animates()
Expand Down Expand Up @@ -876,9 +885,9 @@ void LayoutComponent::clearInheritedInterpolation()
m_inheritedInterpolationTime = 0;
}

bool LayoutComponent::applyInterpolation(double elapsedSeconds)
bool LayoutComponent::applyInterpolation(double elapsedSeconds, bool animate)
{
if (!animates() || m_style == nullptr ||
if (!animate || !animates() || m_style == nullptr ||
m_animationData.toBounds == layoutBounds())
{
return false;
Expand Down Expand Up @@ -976,6 +985,19 @@ bool LayoutComponent::applyInterpolation(double elapsedSeconds)
return needsAdvance;
}

void LayoutComponent::interruptAnimation()
{
if (animates())
{
auto toBounds = m_animationData.toBounds;
m_layoutLocationX = toBounds.left();
m_layoutLocationY = toBounds.top();
m_layoutSizeWidth = toBounds.width();
m_layoutSizeHeight = toBounds.height();
propagateSize();
}
}

void LayoutComponent::markLayoutNodeDirty()
{
layoutNode().markDirtyAndPropagate();
Expand Down Expand Up @@ -1049,6 +1071,11 @@ Vec2D LayoutComponent::measureLayout(float width,
return Vec2D();
}

bool LayoutComponent::advanceComponent(double elapsedSeconds, bool animate)
{
return false;
}

void LayoutComponent::markLayoutNodeDirty() {}
void LayoutComponent::markLayoutStyleDirty() {}
void LayoutComponent::onDirty(ComponentDirt value) {}
Expand Down
6 changes: 3 additions & 3 deletions viewer/src/viewer_content/scene_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SceneContent : public ViewerContent
m_File->createViewModelInstance(m_ArtboardInstance.get());
m_ArtboardInstance->setDataContextFromInstance(m_ViewModelInstance);

m_ArtboardInstance->advance(0.0f);
m_ArtboardInstance->advance(0.0f, true, false);
loadNames(m_ArtboardInstance.get());

initStateMachine(REQUEST_DEFAULT_SCENE);
Expand All @@ -103,7 +103,7 @@ class SceneContent : public ViewerContent
m_AnimationIndex = -1;
m_CurrentScene = nullptr;

m_ArtboardInstance->advance(0.0f);
m_ArtboardInstance->advance(0.0f, true, false);

if (index < 0)
{
Expand Down Expand Up @@ -138,7 +138,7 @@ class SceneContent : public ViewerContent
m_AnimationIndex = -1;
m_CurrentScene = nullptr;

m_ArtboardInstance->advance(0.0f);
m_ArtboardInstance->advance(0.0f, true, false);

if (index >= 0 && index < m_ArtboardInstance->animationCount())
{
Expand Down

0 comments on commit fc62506

Please sign in to comment.