How to draw a Drawable


#1

I first did things like

paint (Graphics& g)
{

Drawable *semibrave;

semibrave->drawAt (g, x, y, 1.0f);
}

But then in drawable.h I read

Note that the preferred way to render a drawable in future is by using it as a component and adding it to a parent, so you might want to consider that before using this method.

So I guess my question is, have the future arrived yet, and if so, what does it mean to use it as a component and what’s the (future) benefit of that? Any coding examples?


#2

Just do addAndMakeVisible (semibrave);


#3

Hmm… that looks fine for more… stationary drawables. But if I want to draw a live score of say chords being played, it then would look something like this if done your way

OwnedArray<Component>scoreComponents;
Array<int> notes;
static Drawable *halfNote = Drawable::createFromImageFile(File(“WhiteNotehead.svg”));

void Score::paint(Graphics& g)
{
//clear previous chord/score
scoreComponents.clearQuick(true);

for each(int staffYPos in notes)
{
Component *c = new Component();
c->addAndMakeVisible(halfNote);
addAndMakeVisible©;
scoreComponents.add©;

  c->setBounds(x, staffYPos, noteWidth, linePitch);	

}
}

instead of

void Score::paint(Graphics& g)
{
Rectangle<float>rc;

for each(int staffYPos in notes)
{
rc.setBounds(x, staffYPos, noteWidth, linePitch);
halfNote->drawWithin(g, rc, RectanglePlacement::flags::yBottom, 1.0f);
}
}

Looks a bit more complicated to me. Or am I missing something?


#4

Might be better to keep a stash of svg drawables around and rather hide/un-hide the drawables and move them around.


#5

Yeah, but what’s the drawback with just using drawWithin()?

Using a stash of drawables as you suggest raises a bunch of deep philosophical questions like how many notes can there be in a chord, should putting your forearm on the piano keyboard be considered as legitimate music etc etc. And then there’s the filled notes, and quavers… and you end up with creating n*128 drawables just for some stupid end cases that with 99.999% certainty never gonna happen but where draw the line, does 99% suffice?

Anyway, I suggest to change the comment from “the preferred way in future…” to something more like “For a stationary drawable you might want to consider using it as a component and adding it to a parent”