diff --git a/.rive_head b/.rive_head index 75efa189..4a4ec308 100644 --- a/.rive_head +++ b/.rive_head @@ -1 +1 @@ -ad1ca22bd5911398018c9a6883e4cba7a1b07c53 +071b19ba62ea97db039f71dcab264d4934161db4 diff --git a/build/premake5.lua b/build/premake5.lua index 444763b7..62d4627e 100644 --- a/build/premake5.lua +++ b/build/premake5.lua @@ -50,7 +50,7 @@ do yoga, }) - defines({ 'YOGA_EXPORT=' }) + defines({ 'YOGA_EXPORT=', '_RIVE_INTERNAL_' }) files({ '../src/**.cpp' }) diff --git a/include/rive/internal/assert_internal_only.hpp b/include/rive/internal/assert_internal_only.hpp new file mode 100644 index 00000000..8183bb04 --- /dev/null +++ b/include/rive/internal/assert_internal_only.hpp @@ -0,0 +1,3 @@ +#ifndef _RIVE_INTERNAL_ +static_assert(false, "This file should never be included by public headers."); +#endif \ No newline at end of file diff --git a/include/rive/layout/layout_component_style.hpp b/include/rive/layout/layout_component_style.hpp index 919341c1..5902fa44 100644 --- a/include/rive/layout/layout_component_style.hpp +++ b/include/rive/layout/layout_component_style.hpp @@ -1,49 +1,14 @@ #ifndef _RIVE_LAYOUT_COMPONENT_STYLE_HPP_ #define _RIVE_LAYOUT_COMPONENT_STYLE_HPP_ #include "rive/generated/layout/layout_component_style_base.hpp" +#include "rive/layout/layout_enums.hpp" +#include "rive/internal/assert_internal_only.hpp" #ifdef WITH_RIVE_LAYOUT #include "yoga/Yoga.h" #endif #include namespace rive { -enum class LayoutAnimationStyle : uint8_t -{ - none, - inherit, - custom -}; - -enum class LayoutStyleInterpolation : uint8_t -{ - hold, - linear, - cubic, - elastic -}; - -enum class LayoutAlignmentType : uint8_t -{ - topLeft, - topCenter, - topRight, - centerLeft, - center, - centerRight, - bottomLeft, - bottomCenter, - bottomRight, - spaceBetweenStart, - spaceBetweenCenter, - spaceBetweenEnd -}; - -enum class LayoutScaleType : uint8_t -{ - fixed, - fill, - hug -}; class KeyFrameInterpolator; class LayoutComponentStyle : public LayoutComponentStyleBase diff --git a/include/rive/layout/layout_data.hpp b/include/rive/layout/layout_data.hpp new file mode 100644 index 00000000..92d0cb5e --- /dev/null +++ b/include/rive/layout/layout_data.hpp @@ -0,0 +1,21 @@ +#ifndef _RIVE_LAYOUT_DATA_HPP_ +#define _RIVE_LAYOUT_DATA_HPP_ + +#ifdef WITH_RIVE_LAYOUT +#include "yoga/YGNode.h" +#include "yoga/YGStyle.h" +#include "yoga/Yoga.h" +#endif + +namespace rive +{ +struct LayoutData +{ +#ifdef WITH_RIVE_LAYOUT + YGNode node; + YGStyle style; +#endif +}; + +} // namespace rive +#endif \ No newline at end of file diff --git a/include/rive/layout/layout_enums.hpp b/include/rive/layout/layout_enums.hpp new file mode 100644 index 00000000..55671dc5 --- /dev/null +++ b/include/rive/layout/layout_enums.hpp @@ -0,0 +1,46 @@ +#ifndef _RIVE_LAYOUT_ENUMS_HPP_ +#define _RIVE_LAYOUT_ENUMS_HPP_ + +#include + +namespace rive +{ +enum class LayoutAnimationStyle : uint8_t +{ + none, + inherit, + custom +}; + +enum class LayoutStyleInterpolation : uint8_t +{ + hold, + linear, + cubic, + elastic +}; + +enum class LayoutAlignmentType : uint8_t +{ + topLeft, + topCenter, + topRight, + centerLeft, + center, + centerRight, + bottomLeft, + bottomCenter, + bottomRight, + spaceBetweenStart, + spaceBetweenCenter, + spaceBetweenEnd +}; + +enum class LayoutScaleType : uint8_t +{ + fixed, + fill, + hug +}; +} // namespace rive +#endif \ No newline at end of file diff --git a/include/rive/layout_component.hpp b/include/rive/layout_component.hpp index f36de0a8..ef667fbd 100644 --- a/include/rive/layout_component.hpp +++ b/include/rive/layout_component.hpp @@ -4,32 +4,19 @@ #include "rive/animation/keyframe_interpolator.hpp" #include "rive/drawable.hpp" #include "rive/generated/layout_component_base.hpp" -#include "rive/layout/layout_component_style.hpp" #include "rive/layout/layout_measure_mode.hpp" #include "rive/math/raw_path.hpp" #include "rive/shapes/rectangle.hpp" #include "rive/shapes/shape_paint_container.hpp" #include "rive/advancing_component.hpp" -#ifdef WITH_RIVE_LAYOUT -#include "yoga/YGNode.h" -#include "yoga/YGStyle.h" -#include "yoga/Yoga.h" -#endif +#include "rive/layout/layout_enums.hpp" namespace rive { - class AABB; class KeyFrameInterpolator; - -struct LayoutData -{ -#ifdef WITH_RIVE_LAYOUT - YGNode node; - YGStyle style; -#endif -}; - +struct LayoutData; +class LayoutComponentStyle; class Layout { public: @@ -37,9 +24,6 @@ class Layout Layout(float left, float top, float width, float height) : m_left(left), m_top(top), m_width(width), m_height(height) {} -#ifdef WITH_RIVE_LAYOUT - Layout(const YGLayout& layout); -#endif bool operator==(const Layout& o) const { @@ -86,7 +70,7 @@ class LayoutComponent : public LayoutComponentBase, { protected: LayoutComponentStyle* m_style = nullptr; - std::unique_ptr m_layoutData; + LayoutData* m_layoutData; Layout m_layout; @@ -97,7 +81,7 @@ class LayoutComponent : public LayoutComponentBase, LayoutStyleInterpolation m_inheritedInterpolation = LayoutStyleInterpolation::hold; float m_inheritedInterpolationTime = 0; - Rectangle* m_backgroundRect = new Rectangle(); + Rectangle m_backgroundRect; rcp m_backgroundPath; rcp m_clipPath; DrawableProxy m_proxy; @@ -130,8 +114,6 @@ class LayoutComponent : public LayoutComponentBase, #ifdef WITH_RIVE_LAYOUT protected: - YGNode& layoutNode() { return m_layoutData->node; } - YGStyle& layoutStyle() { return m_layoutData->style; } void syncLayoutChildren(); void propagateSizeToChildren(ContainerComponent* component); bool applyInterpolation(float elapsedSeconds, bool animate = true); @@ -184,20 +166,16 @@ class LayoutComponent : public LayoutComponentBase, bool overridesKeyedInterpolation(int propertyKey) override; Drawable* hittableComponent() override { return nullptr; } bool hasShapePaints() const { return m_ShapePaints.size() > 0; } - Rectangle* backgroundRect() const { return m_backgroundRect; } + const Rectangle* backgroundRect() const { return &m_backgroundRect; } RenderPath* backgroundPath() const { return m_backgroundPath.get(); } bool advanceComponent(float elapsedSeconds, AdvanceFlags flags = AdvanceFlags::Animate | AdvanceFlags::NewFrame) override; + LayoutComponent(); + ~LayoutComponent(); #ifdef WITH_RIVE_LAYOUT - LayoutComponent() : - m_layoutData(std::unique_ptr(new LayoutData())), - m_proxy(this) - { - layoutNode().getConfig()->setPointScaleFactor(0); - } - ~LayoutComponent() { delete m_backgroundRect; } + void syncStyle(); virtual void propagateSize(); void updateLayoutBounds(bool animate = true); @@ -222,11 +200,6 @@ class LayoutComponent : public LayoutComponentBase, bool isLeaf(); void positionTypeChanged(); void scaleTypeChanged(); -#else - LayoutComponent() : - m_layoutData(std::unique_ptr(new LayoutData())), - m_proxy(this) - {} #endif void buildDependencies() override; diff --git a/premake5_v2.lua b/premake5_v2.lua index 8ad0d211..7eafdb17 100644 --- a/premake5_v2.lua +++ b/premake5_v2.lua @@ -53,7 +53,7 @@ do end filter({}) - defines({ 'YOGA_EXPORT=' }) + defines({ 'YOGA_EXPORT=', '_RIVE_INTERNAL_' }) files({ 'src/**.cpp' }) diff --git a/src/artboard.cpp b/src/artboard.cpp index 7c60667d..2f490f64 100644 --- a/src/artboard.cpp +++ b/src/artboard.cpp @@ -31,6 +31,7 @@ #include "rive/text/text_value_run.hpp" #include "rive/event.hpp" #include "rive/assets/audio_asset.hpp" +#include "rive/layout/layout_data.hpp" #include @@ -770,7 +771,7 @@ void* Artboard::takeLayoutNode() { #ifdef WITH_RIVE_LAYOUT m_updatesOwnLayout = false; - return static_cast(&layoutNode()); + return static_cast(&m_layoutData->node); #else return nullptr; #endif diff --git a/src/layout_component.cpp b/src/layout_component.cpp index 3211082b..27b89042 100644 --- a/src/layout_component.cpp +++ b/src/layout_component.cpp @@ -12,6 +12,8 @@ #include "rive/shapes/paint/stroke.hpp" #include "rive/shapes/rectangle.hpp" #include "rive/nested_artboard_layout.hpp" +#include "rive/layout/layout_data.hpp" +#include "rive/layout/layout_component_style.hpp" #ifdef WITH_RIVE_LAYOUT #include "rive/transform_component.hpp" #include "yoga/YGEnums.h" @@ -131,6 +133,13 @@ bool LayoutComponent::overridesKeyedInterpolation(int propertyKey) } #ifdef WITH_RIVE_LAYOUT + +LayoutComponent::LayoutComponent() : + m_layoutData(new LayoutData()), m_proxy(this) +{ + m_layoutData->node.getConfig()->setPointScaleFactor(0); +} + StatusCode LayoutComponent::onAddedDirty(CoreContext* context) { auto code = Super::onAddedDirty(context); @@ -161,8 +170,8 @@ StatusCode LayoutComponent::onAddedClean(CoreContext* context) markLayoutStyleDirty(); m_backgroundPath = artboard()->factory()->makeEmptyRenderPath(); m_clipPath = artboard()->factory()->makeEmptyRenderPath(); - m_backgroundRect->originX(0); - m_backgroundRect->originY(0); + m_backgroundRect.originX(0); + m_backgroundRect.originY(0); syncLayoutChildren(); return StatusCode::Ok; } @@ -186,13 +195,13 @@ void LayoutComponent::drawProxy(Renderer* renderer) { shapePaint->draw(renderer, m_backgroundPath.get(), - &m_backgroundRect->rawPath()); + &m_backgroundRect.rawPath()); } if (shapePaint->is()) { shapePaint->draw(renderer, m_backgroundPath.get(), - &m_backgroundRect->rawPath()); + &m_backgroundRect.rawPath()); } } renderer->restore(); @@ -209,23 +218,23 @@ void LayoutComponent::draw(Renderer* renderer) void LayoutComponent::updateRenderPath() { - m_backgroundRect->width(m_layout.width()); - m_backgroundRect->height(m_layout.height()); + m_backgroundRect.width(m_layout.width()); + m_backgroundRect.height(m_layout.height()); if (style() != nullptr) { - m_backgroundRect->linkCornerRadius(style()->linkCornerRadius()); - m_backgroundRect->cornerRadiusTL(style()->cornerRadiusTL()); - m_backgroundRect->cornerRadiusTR(style()->cornerRadiusTR()); - m_backgroundRect->cornerRadiusBL(style()->cornerRadiusBL()); - m_backgroundRect->cornerRadiusBR(style()->cornerRadiusBR()); + m_backgroundRect.linkCornerRadius(style()->linkCornerRadius()); + m_backgroundRect.cornerRadiusTL(style()->cornerRadiusTL()); + m_backgroundRect.cornerRadiusTR(style()->cornerRadiusTR()); + m_backgroundRect.cornerRadiusBL(style()->cornerRadiusBL()); + m_backgroundRect.cornerRadiusBR(style()->cornerRadiusBR()); } - m_backgroundRect->update(ComponentDirt::Path); + m_backgroundRect.update(ComponentDirt::Path); m_backgroundPath->rewind(); - m_backgroundRect->rawPath().addTo(m_backgroundPath.get()); + m_backgroundRect.rawPath().addTo(m_backgroundPath.get()); RawPath clipPath; - clipPath.addPath(m_backgroundRect->rawPath(), &m_WorldTransform); + clipPath.addPath(m_backgroundRect.rawPath(), &m_WorldTransform); m_clipPath = artboard()->factory()->makeRenderPath(clipPath, FillRule::nonZero); for (auto shapePaint : m_ShapePaints) @@ -320,8 +329,8 @@ void LayoutComponent::syncStyle() { return; } - YGNode& ygNode = layoutNode(); - YGStyle& ygStyle = layoutStyle(); + YGNode& ygNode = m_layoutData->node; + YGStyle& ygStyle = m_layoutData->style; if (m_style->intrinsicallySized() && isLeaf()) { ygNode.setContext(this); @@ -644,7 +653,7 @@ void LayoutComponent::syncStyle() void LayoutComponent::syncLayoutChildren() { - YGNode& ourNode = layoutNode(); + YGNode& ourNode = m_layoutData->node; YGNodeRemoveAllChildren(&ourNode); int index = 0; for (auto child : children()) @@ -653,7 +662,7 @@ void LayoutComponent::syncLayoutChildren() switch (child->coreType()) { case LayoutComponentBase::typeKey: - node = &child->as()->layoutNode(); + node = &child->as()->m_layoutData->node; break; case NestedArtboardLayoutBase::typeKey: node = static_cast( @@ -702,7 +711,7 @@ void LayoutComponent::propagateSizeToChildren(ContainerComponent* component) void LayoutComponent::calculateLayout() { - YGNodeCalculateLayout(&layoutNode(), + YGNodeCalculateLayout(&m_layoutData->node, width(), height(), YGDirection::YGDirectionInherit); @@ -719,16 +728,17 @@ void LayoutComponent::onDirty(ComponentDirt value) } } -Layout::Layout(const YGLayout& layout) : - m_left(layout.position[YGEdgeLeft]), - m_top(layout.position[YGEdgeTop]), - m_width(layout.dimensions[YGDimensionWidth]), - m_height(layout.dimensions[YGDimensionHeight]) -{} +static Layout layoutFromYoga(const YGLayout& layout) +{ + return Layout(layout.position[YGEdgeLeft], + layout.position[YGEdgeTop], + layout.dimensions[YGDimensionWidth], + layout.dimensions[YGDimensionHeight]); +} void LayoutComponent::updateLayoutBounds(bool animate) { - YGNode& node = layoutNode(); + YGNode& node = m_layoutData->node; bool updated = node.getHasNewLayout(); if (!updated) { @@ -748,7 +758,7 @@ void LayoutComponent::updateLayoutBounds(bool animate) } } - Layout newLayout(node.getLayout()); + Layout newLayout = layoutFromYoga(node.getLayout()); if (animate && animates()) { @@ -1036,7 +1046,7 @@ void LayoutComponent::interruptAnimation() void LayoutComponent::markLayoutNodeDirty() { - layoutNode().markDirtyAndPropagate(); + m_layoutData->node.markDirtyAndPropagate(); artboard()->markLayoutDirty(this); } @@ -1099,6 +1109,10 @@ void LayoutComponent::scaleTypeChanged() markLayoutNodeDirty(); } #else +LayoutComponent::LayoutComponent() : + m_layoutData(new LayoutData()), m_proxy(this) +{} + void LayoutComponent::drawProxy(Renderer* renderer) {} void LayoutComponent::draw(Renderer* renderer) {} @@ -1126,6 +1140,8 @@ bool LayoutComponent::mainAxisIsRow() { return true; } bool LayoutComponent::mainAxisIsColumn() { return false; } #endif +LayoutComponent::~LayoutComponent() { delete m_layoutData; } + void LayoutComponent::clipChanged() { markLayoutNodeDirty(); } void LayoutComponent::widthChanged() { markLayoutNodeDirty(); } void LayoutComponent::heightChanged() { markLayoutNodeDirty(); }