gfx/drawable_component.h
DrawableComponent and OwningDrawableComponent: a component that IS a picture, scaled into its bounds.
#include <sonore/gfx/drawable_component.h>- 2types
- Guides: The native editor
On this page
Design notes
Why a component, when Drawable::draw already takes a rectangle
Drawing an SVG from a parent's paint() is two lines, and for artwork that is part of a background it is still the right answer. A component earns its place when the picture has to behave like a thing in the tree: be laid out by a FlexBox or Grid with everything else, be shown and hidden, be the child a ConcertinaPanel or a TabbedComponent holds, sit in front of some controls and behind others. ImageComponent already does that for a Bitmap; this is the same for a Drawable, with the same placement rules (RectanglePlacement in effects2d.h) so the two cannot disagree about what "contain" means.
Referencing and owning
DrawableComponent REFERENCES its Drawable, like every container in this SDK references what it shows: an icon set parsed once is drawn by twenty components, and copying it into each would be twenty copies of the same shapes. OwningDrawableComponent holds its own, for the one-off picture parsed from a string where nobody else needs to keep it alive.
Placement
By default the picture is fitted the way RectanglePlacement says -- Contain and centred unless told otherwise -- against the Drawable's viewBox. The SVG's own preserveAspectRatio can be used instead (setUsesDrawablePlacement), which is what a designer who set it in the file expects. Stretch is honoured for real: the picture is drawn through a non-uniform scale rather than handed to Drawable::draw, which would keep the aspect ratio regardless.
Types 2
class DrawableComponent : public Widget
A component that shows a Drawable, fitted into its bounds. See the file comment for when this beats drawing the picture from a parent's paint(), and for the placement rules.
The Drawable is REFERENCED: it must outlive this component, or be replaced first with setDrawable. OwningDrawableComponent is the one that keeps its own. Clicks pass through to whatever is behind it. Message thread only.
class OwningDrawableComponent : public DrawableComponent
The same component, holding its own Drawable.