mirror of
https://github.com/rive-app/rive-cpp.git
synced 2026-01-18 21:21:17 +01:00
Add AdvanceFlags
Initially this PR was to fix a bug preventing LayoutComponents from animating when the state machine isn't running (and also in design mode), unlike NestedArtboards. In the process, we decided to convert advance params to flags to allow less effort if its necessary to add more params in the future. Diffs= 17cbda4b69 Add AdvanceFlags (#8492) Co-authored-by: Philip Chung <philterdesign@gmail.com>
This commit is contained in:
@@ -1 +1 @@
|
||||
6cb69b688b5d92fc24fbb27d5c9ba674215a8496
|
||||
17cbda4b699154e815065da48cdb04ecf4f62d6f
|
||||
|
||||
26
include/rive/advance_flags.hpp
Normal file
26
include/rive/advance_flags.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef _RIVE_ADVANCE_FLAGS_HPP_
|
||||
#define _RIVE_ADVANCE_FLAGS_HPP_
|
||||
|
||||
#include "rive/enum_bitset.hpp"
|
||||
|
||||
namespace rive
|
||||
{
|
||||
enum class AdvanceFlags : unsigned short
|
||||
{
|
||||
None = 0,
|
||||
|
||||
/// Whether NestedArtboards should advance
|
||||
AdvanceNested = 1 << 0,
|
||||
|
||||
/// Whether the Component should animate when advancing
|
||||
Animate = 1 << 1,
|
||||
|
||||
/// Whether this Component is on the root artboard
|
||||
IsRoot = 1 << 2,
|
||||
|
||||
/// Whether we are advancing to a new frame
|
||||
NewFrame = 1 << 3,
|
||||
};
|
||||
RIVE_MAKE_ENUM_BITSET(AdvanceFlags)
|
||||
} // namespace rive
|
||||
#endif
|
||||
@@ -1,15 +1,18 @@
|
||||
#ifndef _RIVE_ADVANCING_COMPONENT_HPP_
|
||||
#define _RIVE_ADVANCING_COMPONENT_HPP_
|
||||
|
||||
#include "rive/advance_flags.hpp"
|
||||
|
||||
namespace rive
|
||||
{
|
||||
class Component;
|
||||
class AdvancingComponent
|
||||
{
|
||||
public:
|
||||
virtual bool advanceComponent(float elapsedSeconds,
|
||||
bool animate = true,
|
||||
bool newFrame = true) = 0;
|
||||
virtual bool advanceComponent(
|
||||
float elapsedSeconds,
|
||||
AdvanceFlags flags = AdvanceFlags::Animate |
|
||||
AdvanceFlags::NewFrame) = 0;
|
||||
static AdvancingComponent* from(Component* component);
|
||||
};
|
||||
} // namespace rive
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef _RIVE_ARTBOARD_HPP_
|
||||
#define _RIVE_ARTBOARD_HPP_
|
||||
|
||||
#include "rive/advance_flags.hpp"
|
||||
#include "rive/animation/linear_animation.hpp"
|
||||
#include "rive/animation/state_machine.hpp"
|
||||
#include "rive/core_context.hpp"
|
||||
@@ -146,14 +147,13 @@ public:
|
||||
bool canHaveOverrides() override { return true; }
|
||||
|
||||
bool advance(float elapsedSeconds,
|
||||
bool nested = true,
|
||||
bool animate = true,
|
||||
bool newFrame = true);
|
||||
AdvanceFlags flags = AdvanceFlags::AdvanceNested |
|
||||
AdvanceFlags::Animate |
|
||||
AdvanceFlags::NewFrame);
|
||||
bool advanceInternal(float elapsedSeconds,
|
||||
bool isRoot,
|
||||
bool nested = true,
|
||||
bool animate = true,
|
||||
bool newFrame = true);
|
||||
AdvanceFlags flags = AdvanceFlags::AdvanceNested |
|
||||
AdvanceFlags::Animate |
|
||||
AdvanceFlags::NewFrame);
|
||||
bool hasChangedDrawOrderInLastUpdate()
|
||||
{
|
||||
return m_HasChangedDrawOrderInLastUpdate;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#ifndef _RIVE_LAYOUT_COMPONENT_HPP_
|
||||
#define _RIVE_LAYOUT_COMPONENT_HPP_
|
||||
#include "rive/advance_flags.hpp"
|
||||
#include "rive/animation/keyframe_interpolator.hpp"
|
||||
#include "rive/drawable.hpp"
|
||||
#include "rive/generated/layout_component_base.hpp"
|
||||
@@ -186,8 +187,8 @@ public:
|
||||
Rectangle* backgroundRect() const { return m_backgroundRect; }
|
||||
RenderPath* backgroundPath() const { return m_backgroundPath.get(); }
|
||||
bool advanceComponent(float elapsedSeconds,
|
||||
bool animate = true,
|
||||
bool newFrame = true) override;
|
||||
AdvanceFlags flags = AdvanceFlags::Animate |
|
||||
AdvanceFlags::NewFrame) override;
|
||||
|
||||
#ifdef WITH_RIVE_LAYOUT
|
||||
LayoutComponent() :
|
||||
|
||||
@@ -70,8 +70,8 @@ public:
|
||||
void clearDataContext();
|
||||
|
||||
bool advanceComponent(float elapsedSeconds,
|
||||
bool animate = true,
|
||||
bool newFrame = true) override;
|
||||
AdvanceFlags flags = AdvanceFlags::Animate |
|
||||
AdvanceFlags::NewFrame) override;
|
||||
};
|
||||
} // namespace rive
|
||||
|
||||
|
||||
@@ -1273,7 +1273,10 @@ bool StateMachineInstance::advance(float seconds, bool newFrame)
|
||||
|
||||
bool StateMachineInstance::advanceAndApply(float seconds)
|
||||
{
|
||||
bool keepGoing = m_artboardInstance->advanceInternal(seconds, true);
|
||||
bool keepGoing = m_artboardInstance->advanceInternal(
|
||||
seconds,
|
||||
AdvanceFlags::IsRoot | AdvanceFlags::Animate |
|
||||
AdvanceFlags::AdvanceNested | AdvanceFlags::NewFrame);
|
||||
if (this->advance(seconds, true))
|
||||
{
|
||||
keepGoing = true;
|
||||
@@ -1286,7 +1289,10 @@ bool StateMachineInstance::advanceAndApply(float seconds)
|
||||
keepGoing = true;
|
||||
}
|
||||
|
||||
if (m_artboardInstance->advanceInternal(0.0f, true))
|
||||
if (m_artboardInstance->advanceInternal(
|
||||
0.0f,
|
||||
AdvanceFlags::IsRoot | AdvanceFlags::Animate |
|
||||
AdvanceFlags::AdvanceNested | AdvanceFlags::NewFrame))
|
||||
{
|
||||
keepGoing = true;
|
||||
}
|
||||
|
||||
@@ -884,19 +884,14 @@ bool Artboard::updatePass(bool isRoot)
|
||||
return didUpdate;
|
||||
}
|
||||
|
||||
bool Artboard::advanceInternal(float elapsedSeconds,
|
||||
bool isRoot,
|
||||
bool nested,
|
||||
bool animate,
|
||||
bool newFrame)
|
||||
bool Artboard::advanceInternal(float elapsedSeconds, AdvanceFlags flags)
|
||||
{
|
||||
bool didUpdate = false;
|
||||
|
||||
for (auto dep : m_DependencyOrder)
|
||||
{
|
||||
auto adv = AdvancingComponent::from(dep);
|
||||
if (adv != nullptr &&
|
||||
adv->advanceComponent(elapsedSeconds, animate && nested))
|
||||
if (adv != nullptr && adv->advanceComponent(elapsedSeconds, flags))
|
||||
{
|
||||
didUpdate = true;
|
||||
}
|
||||
@@ -905,13 +900,11 @@ bool Artboard::advanceInternal(float elapsedSeconds,
|
||||
return didUpdate;
|
||||
}
|
||||
|
||||
bool Artboard::advance(float elapsedSeconds,
|
||||
bool nested,
|
||||
bool animate,
|
||||
bool newFrame)
|
||||
bool Artboard::advance(float elapsedSeconds, AdvanceFlags flags)
|
||||
{
|
||||
bool didUpdate =
|
||||
advanceInternal(elapsedSeconds, true, nested, animate, newFrame);
|
||||
AdvanceFlags advancingFlags = flags;
|
||||
advancingFlags |= AdvanceFlags::IsRoot;
|
||||
bool didUpdate = advanceInternal(elapsedSeconds, advancingFlags);
|
||||
if (updatePass(true))
|
||||
{
|
||||
didUpdate = true;
|
||||
|
||||
@@ -22,7 +22,7 @@ std::unique_ptr<ArtboardInstance> DataBindContextValueList::createArtboard(
|
||||
auto mainArtboard = target->artboard();
|
||||
auto dataContext = mainArtboard->dataContext();
|
||||
auto artboardCopy = artboard->instance();
|
||||
artboardCopy->advanceInternal(0.0f, false);
|
||||
artboardCopy->advanceInternal(0.0f);
|
||||
artboardCopy->setDataContextFromInstance(listItem->viewModelInstance(),
|
||||
dataContext,
|
||||
false);
|
||||
|
||||
@@ -789,11 +789,10 @@ void LayoutComponent::updateLayoutBounds(bool animate)
|
||||
}
|
||||
}
|
||||
|
||||
bool LayoutComponent::advanceComponent(float elapsedSeconds,
|
||||
bool animate,
|
||||
bool newFrame)
|
||||
bool LayoutComponent::advanceComponent(float elapsedSeconds, AdvanceFlags flags)
|
||||
{
|
||||
return applyInterpolation(elapsedSeconds, animate);
|
||||
return applyInterpolation(elapsedSeconds,
|
||||
(flags & AdvanceFlags::Animate) != 0);
|
||||
}
|
||||
|
||||
bool LayoutComponent::animates()
|
||||
@@ -1109,9 +1108,7 @@ Vec2D LayoutComponent::measureLayout(float width,
|
||||
return Vec2D();
|
||||
}
|
||||
|
||||
bool LayoutComponent::advanceComponent(float elapsedSeconds,
|
||||
bool animate,
|
||||
bool newFrame)
|
||||
bool LayoutComponent::advanceComponent(float elapsedSeconds, AdvanceFlags flags)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -319,17 +319,20 @@ void NestedArtboard::setDataContextFromInstance(
|
||||
}
|
||||
}
|
||||
|
||||
bool NestedArtboard::advanceComponent(float elapsedSeconds,
|
||||
bool animate,
|
||||
bool newFrame)
|
||||
bool NestedArtboard::advanceComponent(float elapsedSeconds, AdvanceFlags flags)
|
||||
{
|
||||
if (m_Artboard == nullptr || isCollapsed())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool keepGoing = false;
|
||||
if (animate)
|
||||
bool animate = (flags & AdvanceFlags::Animate) == AdvanceFlags::Animate;
|
||||
bool advanceNested =
|
||||
(flags & AdvanceFlags::AdvanceNested) == AdvanceFlags::AdvanceNested;
|
||||
if (animate && advanceNested)
|
||||
{
|
||||
bool newFrame =
|
||||
(flags & AdvanceFlags::NewFrame) == AdvanceFlags::NewFrame;
|
||||
for (auto animation : m_NestedAnimations)
|
||||
{
|
||||
if (animation->advance(elapsedSeconds, newFrame))
|
||||
@@ -339,8 +342,8 @@ bool NestedArtboard::advanceComponent(float elapsedSeconds,
|
||||
}
|
||||
}
|
||||
|
||||
if (m_Artboard
|
||||
->advanceInternal(elapsedSeconds, false, true, animate, newFrame) ||
|
||||
auto advancingFlags = flags | AdvanceFlags::AdvanceNested;
|
||||
if (m_Artboard->advanceInternal(elapsedSeconds, advancingFlags) ||
|
||||
m_Artboard->hasDirt(ComponentDirt::Components))
|
||||
{
|
||||
// The animation(s) caused the artboard to need an update.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* Copyright 2022 Rive
|
||||
*/
|
||||
|
||||
#include "rive/advance_flags.hpp"
|
||||
#include "rive/animation/linear_animation_instance.hpp"
|
||||
#include "rive/animation/state_machine_instance.hpp"
|
||||
#include "rive/animation/state_machine_input_instance.hpp"
|
||||
@@ -91,7 +92,9 @@ class SceneContent : public ViewerContent
|
||||
m_File->createViewModelInstance(m_ArtboardInstance.get());
|
||||
m_ArtboardInstance->setDataContextFromInstance(m_ViewModelInstance);
|
||||
|
||||
m_ArtboardInstance->advance(0.0f, true, false);
|
||||
m_ArtboardInstance->advance(0.0f,
|
||||
rive::AdvanceFlags::AdvanceNested |
|
||||
rive::AdvanceFlags::NewFrame);
|
||||
loadNames(m_ArtboardInstance.get());
|
||||
|
||||
initStateMachine(REQUEST_DEFAULT_SCENE);
|
||||
@@ -103,7 +106,9 @@ class SceneContent : public ViewerContent
|
||||
m_AnimationIndex = -1;
|
||||
m_CurrentScene = nullptr;
|
||||
|
||||
m_ArtboardInstance->advance(0.0f, true, false);
|
||||
m_ArtboardInstance->advance(0.0f,
|
||||
rive::AdvanceFlags::AdvanceNested |
|
||||
rive::AdvanceFlags::NewFrame);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
@@ -138,7 +143,9 @@ class SceneContent : public ViewerContent
|
||||
m_AnimationIndex = -1;
|
||||
m_CurrentScene = nullptr;
|
||||
|
||||
m_ArtboardInstance->advance(0.0f, true, false);
|
||||
m_ArtboardInstance->advance(0.0f,
|
||||
rive::AdvanceFlags::AdvanceNested |
|
||||
rive::AdvanceFlags::NewFrame);
|
||||
|
||||
if (index >= 0 && index < m_ArtboardInstance->animationCount())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user