Let’s say a Drawable, d, is created from an SVG image, and is to be painted inside a parent component such that it is scaled to a specific area.
I can draw it like this without any problems:
//in paint(Graphics &g)
d->drawWithin(g, Rectangle<float>(x, y, w, h), RectanglePlacement::Flags::centred, 1.0f);
But I’d prefer to add the drawable as a child component instead. What would be the equivalent way of setting its bounds and transforms etc such that it produces similar results as the drawWithin() method?
I tried this, but it doesn’t scale:
//Constructor
addAndMakeVisible(d);
// resize()
d->setBoundsToFit(x, y, w, h, Justification::centred, false);
IIRC Drawables do not paint themselves, you have to draw' them. In which case you need some kind of wrapper component that callsdraw’ on the Drawable. You could make such a wrapper, then just use that as you would any other component.
I was under the impression that you can just as well use them as components directly, without using a wrapper. In fact, that seems to be the recommended way of using them (judging by the comments in the Drawable draw routines), and the actual drawing works fine already. It is only the scaling that I cannot get to work properly.
I think the comments mean that Drawables are components like everything else. But drawables dont paint themselves. I think this is because they dont know how you wish them to be fitted in their bounding box.
I’ve always had to build a wrapper around the topmost drawable so as to make it paint. This wrapper can then have flags to say how `draw’ should be called. Perhaps such a wrapper should be part of Juce.
Thanks. Some progress by passing the target rectangle to DrawableComposite::setBoundingBox(). It stretches to the specified boundig box now, but I still need to figure out how to preserve the original aspect ratio of the drawable.
At least one step closer to what Drawable::drawWithin() achieves =)
Did you find a satisfactory solution to this? drawWithin() works fine for me, and I can scale within paint() but adding as a Component I can’t get my SVG Drawable to scale at all. setBounds() etc has no effect…
Stumbled across this today too. Is there a reason why we can’t do setBounds on a Drawable and have it resize? Is there a clear example somewhere of creating a DrawableComposite from an SVG?
Replying to this with a usable answer in case anyone else lands here looking for a solution…
SVG Drawable could be thought of more like a Path inside a Component. To resize it, it isn’t sufficient to set the bounds, a transform needs to be set to scale and / or translate the actual SVG. So: