Skip to content

Commit 14d13c3

Browse files
philterphilter
andcommitted
Layout joysticks runtime
Add support for joysticks in layouts in the runtimes. Diffs= a134ab656 Layout joysticks runtime (#7923) Co-authored-by: Philip Chung <[email protected]>
1 parent 4babbdb commit 14d13c3

File tree

7 files changed

+81
-21
lines changed

7 files changed

+81
-21
lines changed

.rive_head

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
728ac6286912a1bf9987a91488c698668bd67354
1+
a134ab6564748353fa4278483f91fe784b0b9c0a
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef _RIVE_INTRINSICALLY_SIZEABLE_HPP_
2+
#define _RIVE_INTRINSICALLY_SIZEABLE_HPP_
3+
4+
#include <stdint.h>
5+
#include "rive/component.hpp"
6+
#include "rive/layout/layout_measure_mode.hpp"
7+
#include "rive/math/vec2d.hpp"
8+
9+
namespace rive
10+
{
11+
class IntrinsicallySizeable
12+
{
13+
public:
14+
virtual Vec2D measureLayout(float width,
15+
LayoutMeasureMode widthMode,
16+
float height,
17+
LayoutMeasureMode heightMode)
18+
{
19+
return Vec2D();
20+
}
21+
22+
virtual void controlSize(Vec2D size) {}
23+
static IntrinsicallySizeable* from(Component* component);
24+
};
25+
} // namespace rive
26+
#endif

include/rive/joystick.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _RIVE_JOYSTICK_HPP_
22
#define _RIVE_JOYSTICK_HPP_
33
#include "rive/generated/joystick_base.hpp"
4+
#include "rive/intrinsically_sizeable.hpp"
45
#include "rive/joystick_flags.hpp"
56
#include "rive/math/mat2d.hpp"
67
#include <stdio.h>
@@ -10,7 +11,7 @@ namespace rive
1011
class Artboard;
1112
class LinearAnimation;
1213
class TransformComponent;
13-
class Joystick : public JoystickBase
14+
class Joystick : public JoystickBase, public IntrinsicallySizeable
1415
{
1516
public:
1617
void update(ComponentDirt value) override;
@@ -25,6 +26,12 @@ class Joystick : public JoystickBase
2526

2627
bool canApplyBeforeUpdate() const { return m_handleSource == nullptr; }
2728

29+
Vec2D measureLayout(float width,
30+
LayoutMeasureMode widthMode,
31+
float height,
32+
LayoutMeasureMode heightMode) override;
33+
void controlSize(Vec2D size) override;
34+
2835
protected:
2936
void buildDependencies() override;
3037

include/rive/transform_component.hpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _RIVE_TRANSFORM_COMPONENT_HPP_
22
#define _RIVE_TRANSFORM_COMPONENT_HPP_
33
#include "rive/generated/transform_component_base.hpp"
4+
#include "rive/intrinsically_sizeable.hpp"
45
#include "rive/math/aabb.hpp"
56
#include "rive/math/mat2d.hpp"
67
#include "rive/layout/layout_measure_mode.hpp"
@@ -10,7 +11,7 @@ namespace rive
1011
class Constraint;
1112
class WorldTransformComponent;
1213
class AABB;
13-
class TransformComponent : public TransformComponentBase
14+
class TransformComponent : public TransformComponentBase, public IntrinsicallySizeable
1415
{
1516
protected:
1617
Mat2D m_Transform;
@@ -52,16 +53,6 @@ class TransformComponent : public TransformComponentBase
5253
void addConstraint(Constraint* constraint);
5354
virtual AABB localBounds() const;
5455
void markDirtyIfConstrained();
55-
56-
virtual Vec2D measureLayout(float width,
57-
LayoutMeasureMode widthMode,
58-
float height,
59-
LayoutMeasureMode heightMode)
60-
{
61-
return Vec2D();
62-
}
63-
64-
virtual void controlSize(Vec2D size) {}
6556
};
6657
} // namespace rive
6758

src/intrinsically_sizeable.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "rive/intrinsically_sizeable.hpp"
2+
#include "rive/joystick.hpp"
3+
#include "rive/transform_component.hpp"
4+
5+
using namespace rive;
6+
7+
IntrinsicallySizeable* IntrinsicallySizeable::from(Component* component)
8+
{
9+
switch (component->coreType())
10+
{
11+
case TransformComponent::typeKey:
12+
return component->as<TransformComponent>();
13+
case Joystick::typeKey:
14+
return component->as<Joystick>();
15+
}
16+
return nullptr;
17+
}

src/joystick.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,23 @@ void Joystick::apply(Artboard* artboard) const
9292
2.0f * m_yAnimation->durationSeconds());
9393
}
9494
}
95+
96+
Vec2D Joystick::measureLayout(float width,
97+
LayoutMeasureMode widthMode,
98+
float height,
99+
LayoutMeasureMode heightMode)
100+
{
101+
return Vec2D(
102+
std::min(
103+
(widthMode == LayoutMeasureMode::undefined ? std::numeric_limits<float>::max() : width),
104+
Joystick::width()),
105+
std::min((heightMode == LayoutMeasureMode::undefined ? std::numeric_limits<float>::max()
106+
: height),
107+
Joystick::height()));
108+
}
109+
110+
void Joystick::controlSize(Vec2D size)
111+
{
112+
width(size.x);
113+
height(size.y);
114+
}

src/layout_component.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "rive/artboard.hpp"
33
#include "rive/drawable.hpp"
44
#include "rive/factory.hpp"
5+
#include "rive/intrinsically_sizeable.hpp"
56
#include "rive/layout_component.hpp"
67
#include "rive/node.hpp"
78
#include "rive/math/aabb.hpp"
@@ -211,12 +212,10 @@ Vec2D LayoutComponent::measureLayout(float width,
211212
{
212213
continue;
213214
}
214-
// && child->is<TransformComponent>()->canMeasure() for nested artboard layout
215-
if (child->is<TransformComponent>())
215+
auto sizeableChild = IntrinsicallySizeable::from(child);
216+
if (sizeableChild != nullptr)
216217
{
217-
auto transformComponent = child->as<TransformComponent>();
218-
Vec2D measured =
219-
transformComponent->measureLayout(width, widthMode, height, heightMode);
218+
Vec2D measured = sizeableChild->measureLayout(width, widthMode, height, heightMode);
220219
size = Vec2D(std::max(size.x, measured.x), std::max(size.y, measured.y));
221220
}
222221
}
@@ -548,10 +547,10 @@ void LayoutComponent::propagateSizeToChildren(ContainerComponent* component)
548547
{
549548
continue;
550549
}
551-
if (child->is<TransformComponent>())
550+
auto sizeableChild = IntrinsicallySizeable::from(child);
551+
if (sizeableChild != nullptr)
552552
{
553-
auto sizableChild = child->as<TransformComponent>();
554-
sizableChild->controlSize(Vec2D(m_layoutSizeWidth, m_layoutSizeHeight));
553+
sizeableChild->controlSize(Vec2D(m_layoutSizeWidth, m_layoutSizeHeight));
555554
}
556555
if (child->is<ContainerComponent>())
557556
{

0 commit comments

Comments
 (0)