Skip to content

Commit

Permalink
Fix layout shape hug in CPP
Browse files Browse the repository at this point in the history
Diffs=
35a52873c Fix layout shape hug in CPP (#7770)

Co-authored-by: Philip Chung <[email protected]>
  • Loading branch information
philter and philter committed Aug 6, 2024
1 parent 33d096e commit 076f1a2
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
949c70600a6c30bfe3ef6e59685235407af1a323
35a52873cf0adabbda2670673762a36c91822e96
1 change: 1 addition & 0 deletions include/rive/shapes/parametric_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ParametricPath : public ParametricPathBase
float height,
LayoutMeasureMode heightMode) override;
void controlSize(Vec2D size) override;
void markPathDirty(bool sendToLayout = true) override;

protected:
void widthChanged() override;
Expand Down
2 changes: 1 addition & 1 deletion include/rive/shapes/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Path : public PathBase
bool canDeferPathUpdate();
void addVertex(PathVertex* vertex);

virtual void markPathDirty();
virtual void markPathDirty(bool sendToLayout = true);
virtual bool isPathClosed() const { return true; }
void onDirty(ComponentDirt dirt) override;
inline bool isHidden() const { return (pathFlags() & 0x1) == 0x1; }
Expand Down
2 changes: 1 addition & 1 deletion include/rive/shapes/points_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PointsPath : public PointsPathBase, public Skinnable
bool isPathClosed() const override { return isClosed(); }
void buildDependencies() override;
void update(ComponentDirt value) override;
void markPathDirty() override;
void markPathDirty(bool sendToLayout = true) override;
void markSkinDirty() override;
const Mat2D& pathTransform() const override;
};
Expand Down
4 changes: 4 additions & 0 deletions include/rive/shapes/shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class Shape : public ShapeBase, public ShapePaintContainer

AABB computeWorldBounds(const Mat2D* xform = nullptr) const;
AABB computeLocalBounds() const;
Vec2D measureLayout(float width,
LayoutMeasureMode widthMode,
float height,
LayoutMeasureMode heightMode) override;
};
} // namespace rive

Expand Down
65 changes: 39 additions & 26 deletions src/shapes/parametric_path.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "rive/layout_component.hpp"
#include "rive/math/aabb.hpp"
#include "rive/node.hpp"
#include "rive/shapes/parametric_path.hpp"
#include "rive/shapes/shape.hpp"

using namespace rive;

Expand All @@ -8,39 +11,49 @@ Vec2D ParametricPath::measureLayout(float width,
float height,
LayoutMeasureMode heightMode)
{
float measuredWidth, measuredHeight;
switch (widthMode)
{
case LayoutMeasureMode::atMost:
measuredWidth = std::max(ParametricPath::width(), width);
break;
case LayoutMeasureMode::exactly:
measuredWidth = width;
break;
case LayoutMeasureMode::undefined:
measuredWidth = ParametricPath::width();
break;
}
switch (heightMode)
{
case LayoutMeasureMode::atMost:
measuredHeight = std::max(ParametricPath::height(), height);
break;
case LayoutMeasureMode::exactly:
measuredHeight = height;
break;
case LayoutMeasureMode::undefined:
measuredHeight = ParametricPath::height();
break;
}
return Vec2D(measuredWidth, measuredHeight);
return Vec2D(
std::min(
(widthMode == LayoutMeasureMode::undefined ? std::numeric_limits<float>::max() : width),
ParametricPath::width()),
std::min((heightMode == LayoutMeasureMode::undefined ? std::numeric_limits<float>::max()
: height),
ParametricPath::height()));
}

void ParametricPath::controlSize(Vec2D size)
{
width(size.x);
height(size.y);
markWorldTransformDirty();
markPathDirty(false);
}

void ParametricPath::markPathDirty(bool sendToLayout)
{
Super::markPathDirty();
#ifdef WITH_RIVE_LAYOUT
if (sendToLayout)
{
for (ContainerComponent* p = parent(); p != nullptr; p = p->parent())
{
if (p->is<LayoutComponent>())
{
p->as<LayoutComponent>()->markLayoutNodeDirty();
break;
}
// If we're in a group we break out because objects in groups do
// not affect nor are affected by parent LayoutComponents
if (p->is<Node>())
{
if (p->is<Shape>() && p->as<Shape>() == shape())
{
continue;
}
break;
}
}
}
#endif
}

void ParametricPath::widthChanged() { markPathDirty(); }
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void Path::buildPath(RawPath& rawPath) const
}
}

void Path::markPathDirty()
void Path::markPathDirty(bool sendToLayout)
{
addDirt(ComponentDirt::Path);
if (m_Shape != nullptr)
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/points_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void PointsPath::update(ComponentDirt value)
Super::update(value);
}

void PointsPath::markPathDirty()
void PointsPath::markPathDirty(bool sendToLayout)
{
if (skin() != nullptr)
{
Expand Down
14 changes: 14 additions & 0 deletions src/shapes/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,18 @@ AABB Shape::computeLocalBounds() const
const Mat2D& world = worldTransform();
Mat2D inverseWorld = world.invertOrIdentity();
return computeWorldBounds(&inverseWorld);
}

Vec2D Shape::measureLayout(float width,
LayoutMeasureMode widthMode,
float height,
LayoutMeasureMode heightMode)
{
Vec2D size = Vec2D();
for (auto path : m_Paths)
{
Vec2D measured = path->measureLayout(width, widthMode, height, heightMode);
size = Vec2D(std::max(size.x, measured.x), std::max(size.y, measured.y));
}
return size;
}

0 comments on commit 076f1a2

Please sign in to comment.