Skip to content

Commit

Permalink
Dashing (through the snow)
Browse files Browse the repository at this point in the history
For @alxgibsn to make awesome.

Diffs=
8a974784d Dashing (through the snow) (#8093)

Co-authored-by: Alex Gibson <[email protected]>
Co-authored-by: Luigi Rosso <[email protected]>
  • Loading branch information
3 people committed Sep 20, 2024
1 parent b9a6958 commit 3d8e05f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9e4b817628ceccea8d383fd01df4f623ae3bea9e
8a974784d34fdd302cb0676d523d94cafe78f63e
1 change: 1 addition & 0 deletions include/rive/shapes/paint/dash_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Dash
Dash(float value, bool percentage);

float value() const;
float normalizedValue(float length) const;
bool percentage() const;

private:
Expand Down
58 changes: 47 additions & 11 deletions src/shapes/paint/dash_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ using namespace rive;

Dash::Dash() : m_value(0.0f), m_percentage(false) {}
Dash::Dash(float value, bool percentage) : m_value(value), m_percentage(percentage) {}
float Dash::value() const { return m_value < 0.0 ? 0.0f : m_value; }
float Dash::value() const { return m_value; }

float Dash::normalizedValue(float length) const
{
float right = m_percentage ? 1.0f : length;
float p = fmodf(m_value, right);
fprintf(stderr, "Normalized value: %f | %f %f\n", p, m_value, m_percentage ? 1.0f : length);
if (p < 0.0f)
{
p += right;
}
return m_percentage ? p * length : p;
}
bool Dash::percentage() const { return m_percentage; }

void PathDasher::invalidateSourcePath()
Expand Down Expand Up @@ -38,11 +50,18 @@ RenderPath* PathDasher::dash(const RawPath& source,

// Make sure dashes have some length.
bool hasValidDash = false;
for (auto dash : dashes)
for (const rcp<ContourMeasure>& contour : m_contours)
{
if (dash.value() > 0.0f)
for (auto dash : dashes)
{
if (dash.normalizedValue(contour->length()) > 0.0f)
{
hasValidDash = true;
break;
}
}
if (hasValidDash)
{
hasValidDash = true;
break;
}
}
Expand All @@ -51,19 +70,36 @@ RenderPath* PathDasher::dash(const RawPath& source,
int dashIndex = 0;
for (const rcp<ContourMeasure>& contour : m_contours)
{
float distance =
offset.percentage() ? offset.value() * contour->length() : offset.value();
float dashed = 0.0f;
float distance = offset.normalizedValue(contour->length());
bool draw = true;
while (distance < contour->length())
while (dashed < contour->length())
{
const Dash& dash = dashes[dashIndex++ % dashes.size()];
float dashLength =
dash.percentage() ? dash.value() * contour->length() : dash.value();
if (draw)
float dashLength = dash.normalizedValue(contour->length());
if (dashLength > contour->length())
{
dashLength = contour->length();
}
float endLength = distance + dashLength;
if (endLength > contour->length())
{
endLength -= contour->length();
if (draw)
{
contour->getSegment(distance, contour->length(), &m_rawPath, true);
contour->getSegment(0.0f, endLength, &m_rawPath, !contour->isClosed());
}

// Setup next step.
distance = endLength - dashLength;
}
else if (draw)
{
contour->getSegment(distance, distance + dashLength, &m_rawPath, true);
contour->getSegment(distance, endLength, &m_rawPath, true);
}
distance += dashLength;
dashed += dashLength;
draw = !draw;
}
}
Expand Down

0 comments on commit 3d8e05f

Please sign in to comment.