1 #include "shapes/clipping_shape.hpp"
2 #include "artboard.hpp"
3 #include "core_context.hpp"
5 #include "renderer.hpp"
6 #include "shapes/path_composer.hpp"
7 #include "shapes/shape.hpp"
11 StatusCode ClippingShape::onAddedClean(CoreContext* context)
13 auto clippingHolder = parent();
15 auto artboard = static_cast<Artboard*>(context);
16 for (auto core : artboard->objects())
18 // Iterate artboard to find drawables that are parented to this clipping
19 // shape, they need to know they'll be clipped by this shape.
20 if (core->is<Drawable>())
22 auto drawable = core->as<Drawable>();
23 for (ContainerComponent* component = drawable; component != nullptr;
24 component = component->parent())
26 if (component == clippingHolder)
28 drawable->addClippingShape(this);
34 // Iterate artboard to find shapes that are parented to the source,
35 // their paths will need to be RenderPaths in order to be used for
36 // clipping operations.
37 if (core->is<Shape>() && core != clippingHolder)
39 auto component = core->as<ContainerComponent>();
40 while (component != nullptr)
42 if (component == m_Source)
44 auto shape = core->as<Shape>();
45 shape->addDefaultPathSpace(PathSpace::World |
47 m_Shapes.push_back(shape);
50 component = component->parent();
55 m_RenderPath = rive::makeRenderPath();
57 return StatusCode::Ok;
60 StatusCode ClippingShape::onAddedDirty(CoreContext* context)
62 StatusCode code = Super::onAddedDirty(context);
63 if (code != StatusCode::Ok)
67 auto coreObject = context->resolve(sourceId());
68 if (coreObject == nullptr || !coreObject->is<Node>())
70 return StatusCode::MissingObject;
73 m_Source = reinterpret_cast<Node*>(coreObject);
75 return StatusCode::Ok;
78 void ClippingShape::buildDependencies()
80 for (auto shape : m_Shapes)
82 shape->pathComposer()->addDependent(this);
86 static Mat2D identity;
87 void ClippingShape::update(ComponentDirt value)
89 if (hasDirt(value, ComponentDirt::Path | ComponentDirt::WorldTransform))
91 m_RenderPath->reset();
93 m_RenderPath->fillRule((FillRule)fillRule());
94 for (auto shape : m_Shapes)
96 m_RenderPath->addPath(shape->pathComposer()->worldPath(), identity);