From 14d13c3ded7b141d5130c0246901008dc070c9fe Mon Sep 17 00:00:00 2001 From: philter Date: Mon, 26 Aug 2024 19:57:58 +0000 Subject: [PATCH] Layout joysticks runtime Add support for joysticks in layouts in the runtimes. Diffs= a134ab656 Layout joysticks runtime (#7923) Co-authored-by: Philip Chung --- .rive_head | 2 +- include/rive/intrinsically_sizeable.hpp | 26 +++++++++++++++++++++++++ include/rive/joystick.hpp | 9 ++++++++- include/rive/transform_component.hpp | 13 ++----------- src/intrinsically_sizeable.cpp | 17 ++++++++++++++++ src/joystick.cpp | 20 +++++++++++++++++++ src/layout_component.cpp | 15 +++++++------- 7 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 include/rive/intrinsically_sizeable.hpp create mode 100644 src/intrinsically_sizeable.cpp diff --git a/.rive_head b/.rive_head index 39051bfa..95346123 100644 --- a/.rive_head +++ b/.rive_head @@ -1 +1 @@ -728ac6286912a1bf9987a91488c698668bd67354 +a134ab6564748353fa4278483f91fe784b0b9c0a diff --git a/include/rive/intrinsically_sizeable.hpp b/include/rive/intrinsically_sizeable.hpp new file mode 100644 index 00000000..cc3db340 --- /dev/null +++ b/include/rive/intrinsically_sizeable.hpp @@ -0,0 +1,26 @@ +#ifndef _RIVE_INTRINSICALLY_SIZEABLE_HPP_ +#define _RIVE_INTRINSICALLY_SIZEABLE_HPP_ + +#include +#include "rive/component.hpp" +#include "rive/layout/layout_measure_mode.hpp" +#include "rive/math/vec2d.hpp" + +namespace rive +{ +class IntrinsicallySizeable +{ +public: + virtual Vec2D measureLayout(float width, + LayoutMeasureMode widthMode, + float height, + LayoutMeasureMode heightMode) + { + return Vec2D(); + } + + virtual void controlSize(Vec2D size) {} + static IntrinsicallySizeable* from(Component* component); +}; +} // namespace rive +#endif \ No newline at end of file diff --git a/include/rive/joystick.hpp b/include/rive/joystick.hpp index b7818900..cf477a4c 100644 --- a/include/rive/joystick.hpp +++ b/include/rive/joystick.hpp @@ -1,6 +1,7 @@ #ifndef _RIVE_JOYSTICK_HPP_ #define _RIVE_JOYSTICK_HPP_ #include "rive/generated/joystick_base.hpp" +#include "rive/intrinsically_sizeable.hpp" #include "rive/joystick_flags.hpp" #include "rive/math/mat2d.hpp" #include @@ -10,7 +11,7 @@ namespace rive class Artboard; class LinearAnimation; class TransformComponent; -class Joystick : public JoystickBase +class Joystick : public JoystickBase, public IntrinsicallySizeable { public: void update(ComponentDirt value) override; @@ -25,6 +26,12 @@ class Joystick : public JoystickBase bool canApplyBeforeUpdate() const { return m_handleSource == nullptr; } + Vec2D measureLayout(float width, + LayoutMeasureMode widthMode, + float height, + LayoutMeasureMode heightMode) override; + void controlSize(Vec2D size) override; + protected: void buildDependencies() override; diff --git a/include/rive/transform_component.hpp b/include/rive/transform_component.hpp index 36580a66..bb2ea7d2 100644 --- a/include/rive/transform_component.hpp +++ b/include/rive/transform_component.hpp @@ -1,6 +1,7 @@ #ifndef _RIVE_TRANSFORM_COMPONENT_HPP_ #define _RIVE_TRANSFORM_COMPONENT_HPP_ #include "rive/generated/transform_component_base.hpp" +#include "rive/intrinsically_sizeable.hpp" #include "rive/math/aabb.hpp" #include "rive/math/mat2d.hpp" #include "rive/layout/layout_measure_mode.hpp" @@ -10,7 +11,7 @@ namespace rive class Constraint; class WorldTransformComponent; class AABB; -class TransformComponent : public TransformComponentBase +class TransformComponent : public TransformComponentBase, public IntrinsicallySizeable { protected: Mat2D m_Transform; @@ -52,16 +53,6 @@ class TransformComponent : public TransformComponentBase void addConstraint(Constraint* constraint); virtual AABB localBounds() const; void markDirtyIfConstrained(); - - virtual Vec2D measureLayout(float width, - LayoutMeasureMode widthMode, - float height, - LayoutMeasureMode heightMode) - { - return Vec2D(); - } - - virtual void controlSize(Vec2D size) {} }; } // namespace rive diff --git a/src/intrinsically_sizeable.cpp b/src/intrinsically_sizeable.cpp new file mode 100644 index 00000000..3e45f7db --- /dev/null +++ b/src/intrinsically_sizeable.cpp @@ -0,0 +1,17 @@ +#include "rive/intrinsically_sizeable.hpp" +#include "rive/joystick.hpp" +#include "rive/transform_component.hpp" + +using namespace rive; + +IntrinsicallySizeable* IntrinsicallySizeable::from(Component* component) +{ + switch (component->coreType()) + { + case TransformComponent::typeKey: + return component->as(); + case Joystick::typeKey: + return component->as(); + } + return nullptr; +} \ No newline at end of file diff --git a/src/joystick.cpp b/src/joystick.cpp index 334faced..40b4d4c0 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -92,3 +92,23 @@ void Joystick::apply(Artboard* artboard) const 2.0f * m_yAnimation->durationSeconds()); } } + +Vec2D Joystick::measureLayout(float width, + LayoutMeasureMode widthMode, + float height, + LayoutMeasureMode heightMode) +{ + return Vec2D( + std::min( + (widthMode == LayoutMeasureMode::undefined ? std::numeric_limits::max() : width), + Joystick::width()), + std::min((heightMode == LayoutMeasureMode::undefined ? std::numeric_limits::max() + : height), + Joystick::height())); +} + +void Joystick::controlSize(Vec2D size) +{ + width(size.x); + height(size.y); +} \ No newline at end of file diff --git a/src/layout_component.cpp b/src/layout_component.cpp index c72a6140..0f960c49 100644 --- a/src/layout_component.cpp +++ b/src/layout_component.cpp @@ -2,6 +2,7 @@ #include "rive/artboard.hpp" #include "rive/drawable.hpp" #include "rive/factory.hpp" +#include "rive/intrinsically_sizeable.hpp" #include "rive/layout_component.hpp" #include "rive/node.hpp" #include "rive/math/aabb.hpp" @@ -211,12 +212,10 @@ Vec2D LayoutComponent::measureLayout(float width, { continue; } - // && child->is()->canMeasure() for nested artboard layout - if (child->is()) + auto sizeableChild = IntrinsicallySizeable::from(child); + if (sizeableChild != nullptr) { - auto transformComponent = child->as(); - Vec2D measured = - transformComponent->measureLayout(width, widthMode, height, heightMode); + Vec2D measured = sizeableChild->measureLayout(width, widthMode, height, heightMode); size = Vec2D(std::max(size.x, measured.x), std::max(size.y, measured.y)); } } @@ -548,10 +547,10 @@ void LayoutComponent::propagateSizeToChildren(ContainerComponent* component) { continue; } - if (child->is()) + auto sizeableChild = IntrinsicallySizeable::from(child); + if (sizeableChild != nullptr) { - auto sizableChild = child->as(); - sizableChild->controlSize(Vec2D(m_layoutSizeWidth, m_layoutSizeHeight)); + sizeableChild->controlSize(Vec2D(m_layoutSizeWidth, m_layoutSizeHeight)); } if (child->is()) {