Skip to content

Commit

Permalink
Layout joysticks runtime
Browse files Browse the repository at this point in the history
Add support for joysticks in layouts in the runtimes.

Diffs=
a134ab656 Layout joysticks runtime (#7923)

Co-authored-by: Philip Chung <[email protected]>
  • Loading branch information
philter and philter committed Aug 26, 2024
1 parent 4babbdb commit 14d13c3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
728ac6286912a1bf9987a91488c698668bd67354
a134ab6564748353fa4278483f91fe784b0b9c0a
26 changes: 26 additions & 0 deletions include/rive/intrinsically_sizeable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _RIVE_INTRINSICALLY_SIZEABLE_HPP_
#define _RIVE_INTRINSICALLY_SIZEABLE_HPP_

#include <stdint.h>
#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
9 changes: 8 additions & 1 deletion include/rive/joystick.hpp
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
Expand All @@ -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;
Expand All @@ -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;

Expand Down
13 changes: 2 additions & 11 deletions include/rive/transform_component.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/intrinsically_sizeable.cpp
Original file line number Diff line number Diff line change
@@ -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<TransformComponent>();
case Joystick::typeKey:
return component->as<Joystick>();
}
return nullptr;
}
20 changes: 20 additions & 0 deletions src/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>::max() : width),
Joystick::width()),
std::min((heightMode == LayoutMeasureMode::undefined ? std::numeric_limits<float>::max()
: height),
Joystick::height()));
}

void Joystick::controlSize(Vec2D size)
{
width(size.x);
height(size.y);
}
15 changes: 7 additions & 8 deletions src/layout_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -211,12 +212,10 @@ Vec2D LayoutComponent::measureLayout(float width,
{
continue;
}
// && child->is<TransformComponent>()->canMeasure() for nested artboard layout
if (child->is<TransformComponent>())
auto sizeableChild = IntrinsicallySizeable::from(child);
if (sizeableChild != nullptr)
{
auto transformComponent = child->as<TransformComponent>();
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));
}
}
Expand Down Expand Up @@ -548,10 +547,10 @@ void LayoutComponent::propagateSizeToChildren(ContainerComponent* component)
{
continue;
}
if (child->is<TransformComponent>())
auto sizeableChild = IntrinsicallySizeable::from(child);
if (sizeableChild != nullptr)
{
auto sizableChild = child->as<TransformComponent>();
sizableChild->controlSize(Vec2D(m_layoutSizeWidth, m_layoutSizeHeight));
sizeableChild->controlSize(Vec2D(m_layoutSizeWidth, m_layoutSizeHeight));
}
if (child->is<ContainerComponent>())
{
Expand Down

0 comments on commit 14d13c3

Please sign in to comment.