Send arguments to drawFunc?

I don’t believe it’s possible to send arguments to a UserView drawFunc - that would specify details of the drawing to be performed. Rather, this information needs to be held in global variables outside the drawFunc. Is this correct? It would seem to me a tidier way of doing things to be able to send args to drawFunc. Grateful for explanation and comments on this.

I don’t think you can send custom arguments to a drawFunc. What I do, is to enclose the drawFunc inside an other function, which I call instead, passing the arguments. This way, variables do not have to be stored as global, but the function has to :

(
var view = UserView();
var myColor = Color.red;

view.drawFunc = { |view|
	Pen.fillColor_(myColor);
	Pen.fillRect(
		Rect(0, 0, view.bounds.width, view.bounds.height)
	)
};

~customDrawFunc = { |myNewColor|
	myColor = myNewColor;
	view.refresh;
};

view.front;
)

~customDrawFunc.value(Color.green)

This way, you’re effectively reducing your global variable usage to one, or two if you also need to access the UserView itself globally. I guess that in this second case, you could “override” the drawFunc by using .addUniqueMethod to access the new refresh method while only referencing the UserView globally :

(
var myColor = Color.red;
var view = UserView();

view.drawFunc = { |view|
	Pen.fillColor_(myColor);
	Pen.fillRect(
		Rect(0, 0, view.bounds.width, view.bounds.height)
	)
};

view.addUniqueMethod(
	\customDrawFunc,
	{ |self, myNewColor|
		myColor = myNewColor;
		view.refresh;
	}
);

~myView = view;
view.front;
)

~myView.customDrawFunc(Color.green)

Thank you very much - this is very helpful.