Skip to content

Commit

Permalink
Fixing tool drawing
Browse files Browse the repository at this point in the history
A lot of the tool draw methods aren't implemented with "render" for the Rive Renderer.

The Joystick, TextTool, and others weren't working. This PR cleans them up and removes a bunch of dependent methods which are no longer necessary. It also mostly gets the StateMachine stage to completely work with the Renderer abstraction (save for the icons which hack back to Canvas). When this part gets wrapped we can move to Rive Renderer for the state machine stage too.

~~This is in draft because I still have to get dashing working with RenderPath (which the state machine uses).~~

Diffs=
5c312867e Fixing tool drawing (#8028)

Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
  • Loading branch information
luigi-rosso and luigi-rosso committed Sep 2, 2024
1 parent 7af06f2 commit 33a51d0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2bd842b54b10e487731fcb51c728098d4956996a
5c312867e32d4a347e5483dca9798b1dd3dd30d1
39 changes: 39 additions & 0 deletions include/rive/shapes/paint/dash_path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef _RIVE_DASH_PATH_HPP_
#define _RIVE_DASH_PATH_HPP_

#include "rive/shapes/paint/stroke_effect.hpp"
#include "rive/renderer.hpp"
#include "rive/math/raw_path.hpp"
#include "rive/math/contour_measure.hpp"

namespace rive
{
class Dash
{
public:
Dash();
Dash(float value, bool percentage);

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

private:
float m_value;
bool m_percentage;
};

class PathDasher
{
protected:
void invalidateSourcePath();
void invalidateDash();
RenderPath* dash(const RawPath& source, Factory* factory, Dash offset, Span<Dash> dashes);

private:
RawPath m_rawPath;
RenderPath* m_renderPath = nullptr;
rcp<RenderPath> m_dashedPath;
std::vector<rcp<ContourMeasure>> m_contours;
};
} // namespace rive
#endif
70 changes: 70 additions & 0 deletions src/shapes/paint/dash_path.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "rive/shapes/paint/dash_path.hpp"
#include "rive/factory.hpp"

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; }
bool Dash::percentage() const { return m_percentage; }

void PathDasher::invalidateSourcePath()
{
m_contours.clear();
invalidateDash();
}
void PathDasher::invalidateDash() { m_renderPath = nullptr; }

RenderPath* PathDasher::dash(const RawPath& source,
Factory* factory,
Dash offset,
Span<Dash> dashes)
{
if (m_renderPath != nullptr)
{
// Previous result hasn't been invalidated, it's still good.
return m_renderPath;
}

m_rawPath.rewind();
if (m_contours.empty())
{
ContourMeasureIter iter(&source);
while (auto meas = iter.next())
{
m_contours.push_back(meas);
}
}

int dashIndex = 0;
for (const rcp<ContourMeasure>& contour : m_contours)
{
float distance = offset.percentage() ? offset.value() * contour->length() : offset.value();
bool draw = true;
while (distance < contour->length())
{
const Dash& dash = dashes[dashIndex];
float dashLength = dash.percentage() ? dash.value() * contour->length() : dash.value();
if (draw)
{
contour->getSegment(distance, distance + dashLength, &m_rawPath, true);
}
distance += dashLength;
draw = !draw;
}
}

if (!m_dashedPath)
{
m_dashedPath = factory->makeEmptyRenderPath();
}
else
{
m_dashedPath->rewind();
}

m_renderPath = m_dashedPath.get();
m_rawPath.addTo(m_renderPath);

return m_renderPath;
}

0 comments on commit 33a51d0

Please sign in to comment.