Skip to content

Commit

Permalink
Add NestedArtboardLayout hug
Browse files Browse the repository at this point in the history
Add hug overrides for nested artboards.

There is one caveat. Since the Artboard is a special type of layout where children can exist in regular transform space OR inside a layout, in order for nested artboard hug to work, the nested artboard must be in AutoLayout mode (ie has at least 1 relative layout child) and when you set hug on the nested artboard (inside the parent artboard) it will hug to whatever the size of the layout children are. It will not currently hug to components in the Artboard that exist in regular transform space.

https://github.com/user-attachments/assets/eee4568e-c5d3-4987-b852-a52cbd2af563

Diffs=
95cad5c60 Add NestedArtboardLayout hug (#8137)

Co-authored-by: Philip Chung <[email protected]>
  • Loading branch information
philter and philter committed Sep 14, 2024
1 parent ed33f09 commit 3c4e94c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
edc91a599a7f182007e488b232d64e0166f43139
95cad5c6055750ff6b1c033f903967e6b9c22bb6
4 changes: 4 additions & 0 deletions include/rive/layout_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class LayoutComponent : public LayoutComponentBase, public ProxyDrawing, public
float m_heightOverride = NAN;
int m_heightUnitValueOverride = -1;
bool m_parentIsRow = true;
bool m_widthIntrinsicallySizeOverride = false;
bool m_heightIntrinsicallySizeOverride = false;

#ifdef WITH_RIVE_LAYOUT
protected:
Expand Down Expand Up @@ -116,6 +118,8 @@ class LayoutComponent : public LayoutComponentBase, public ProxyDrawing, public
// width/height and unit values.
void widthOverride(float width, int unitValue = 1, bool isRow = true);
void heightOverride(float height, int unitValue = 1, bool isRow = true);
void widthIntrinsicallySizeOverride(bool intrinsic);
void heightIntrinsicallySizeOverride(bool intrinsic);
virtual bool canHaveOverrides() { return false; }
bool mainAxisIsRow();
bool mainAxisIsColumn();
Expand Down
36 changes: 34 additions & 2 deletions src/layout_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ void LayoutComponent::heightOverride(float height, int unitValue, bool isRow)
markLayoutNodeDirty();
}

void LayoutComponent::widthIntrinsicallySizeOverride(bool intrinsic)
{
m_widthIntrinsicallySizeOverride = intrinsic;
// If we have an intrinsically sized override, set units to auto
// otherwise set to points
m_widthUnitValueOverride = intrinsic ? 3 : 1;
markLayoutNodeDirty();
}

void LayoutComponent::heightIntrinsicallySizeOverride(bool intrinsic)
{
m_heightIntrinsicallySizeOverride = intrinsic;
// If we have an intrinsically sized override, set units to auto
// otherwise set to points
m_heightUnitValueOverride = intrinsic ? 3 : 1;
markLayoutNodeDirty();
}

#ifdef WITH_RIVE_LAYOUT
StatusCode LayoutComponent::onAddedDirty(CoreContext* context)
{
Expand Down Expand Up @@ -298,7 +316,14 @@ void LayoutComponent::syncStyle()
realWidthScaleType = LayoutScaleType::fixed;
break;
case YGUnitAuto:
realWidthScaleType = LayoutScaleType::fill;
if (m_widthIntrinsicallySizeOverride)
{
realWidthScaleType = LayoutScaleType::hug;
}
else
{
realWidthScaleType = LayoutScaleType::fill;
}
break;
default:
break;
Expand All @@ -314,7 +339,14 @@ void LayoutComponent::syncStyle()
realHeightScaleType = LayoutScaleType::fixed;
break;
case YGUnitAuto:
realHeightScaleType = LayoutScaleType::fill;
if (m_heightIntrinsicallySizeOverride)
{
realHeightScaleType = LayoutScaleType::hug;
}
else
{
realHeightScaleType = LayoutScaleType::fill;
}
break;
default:
break;
Expand Down
12 changes: 12 additions & 0 deletions src/nested_artboard_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,19 @@ void NestedArtboardLayout::updateWidthOverride()
if (instanceWidthScaleType() == 0) // LayoutScaleType::fixed
{
// If we're set to fixed, pass the unit value (points|percent)
artboardInstance()->widthIntrinsicallySizeOverride(false);
artboardInstance()->widthOverride(actualInstanceWidth(), instanceWidthUnitsValue(), isRow);
}
else if (instanceWidthScaleType() == 1) // LayoutScaleType::fill
{
// If we're set to fill, pass auto
artboardInstance()->widthIntrinsicallySizeOverride(false);
artboardInstance()->widthOverride(actualInstanceWidth(), 3, isRow);
}
else if (instanceWidthScaleType() == 2)
{
artboardInstance()->widthIntrinsicallySizeOverride(true);
}
}

void NestedArtboardLayout::updateHeightOverride()
Expand All @@ -114,15 +120,21 @@ void NestedArtboardLayout::updateHeightOverride()
if (instanceHeightScaleType() == 0) // LayoutScaleType::fixed
{
// If we're set to fixed, pass the unit value (points|percent)
artboardInstance()->heightIntrinsicallySizeOverride(false);
artboardInstance()->heightOverride(actualInstanceHeight(),
instanceHeightUnitsValue(),
isRow);
}
else if (instanceHeightScaleType() == 1) // LayoutScaleType::fill
{
// If we're set to fill, pass auto
artboardInstance()->heightIntrinsicallySizeOverride(false);
artboardInstance()->heightOverride(actualInstanceHeight(), 3, isRow);
}
else if (instanceWidthScaleType() == 2)
{
artboardInstance()->heightIntrinsicallySizeOverride(true);
}
}

void NestedArtboardLayout::instanceWidthChanged() { updateWidthOverride(); }
Expand Down

0 comments on commit 3c4e94c

Please sign in to comment.