Context to setPaintRoutine
-
I'm trying to pass some context to the
setPaintRoutine
function, but when I try this:for (i=0;i<items.length;i++) { Console.print('A - ' + i); instruments[i].setPaintRoutine(function (g) { Console.print('B - ' + i); }); Console.print('C - ' + i); };
It prints:
Interface: "C - 0" Interface: "A - 1" Interface: "C - 1" Interface: "A - 2" Interface: "C - 2" Interface: "A - 3" Interface: "C - 3" Interface: "A - 4" Interface: "C - 4" Interface: "B - 5" Interface: "B - 5" Interface: "B - 5" Interface: "B - 5" Interface: "B - 5"
And it always gets the context of the last item. Is there a way to pass the context (or the index) inside
setPaintRoutine
? -
I find the best way to access stuff in the paint routine is to set it as the panel's value, or text. That way it's all self contained.
If you want an index, you could just use
instruments.indexOf(this)
inside the paint routine I think. -
@d-healey good idea. I ended up using
this.getId()
, which gave me what I needed. Thanks! -
@daniloprates there‘s a data object for each panel that you can populate with anything and then access it in the callback with this.data.
-
This post is deleted! -
@Christoph-Hart I've added this to the panel's
setPropertiesFromJSON
, butthis.data
is returning an empty object:... data: { "test": "testing" }, ...
-
@daniloprates data is not a property that you can set through this call, but directly:
for(i = 0; i < 5; i++) { var p = Content.addPanel("P"+i, i * 100, 0); p.data.index = i; p.setPaintRoutine(function(g) { g.setColour(Colours.white); g.drawAlignedText(this.data.index, this.getLocalBounds(0), "centred"); }); };