Laf function triggers twice for each setLocalLookAndFeel ?
-
I'm trying to draw 2 triangles for "previous" and "next" buttons.
In my Laf I have a condition to check the object.text name to either rotate it to the left or the right.
But it's not working and print returns this when I run itInterface: btnPrevious Interface: btnPrevious Interface: false Interface: btnNext Interface: btnNext Interface: false
Here's my code for it
// Select Buttons const var btnPrevious = Content.addButton("btnPrevious", a[0] + 15, a[1] + 5); const var btnNext = Content.addButton("btnNext", a[0] + 45, a[1] + 5); const LafButton = Content.createLocalLookAndFeel(); LafButton.registerFunction("drawToggleButton", function(g, obj){ //Console.print(trace(obj)); // trace converts contents of object into a string var area = obj.area; // get "area" property from obj and save into a var for quick access var border = 3; g.setColour(Colours.withAlpha(obj.itemColour1, alpha)); Console.print(obj.text); if (Console.print(obj.text) == "btnPrevious"){ g.fillTriangle([0, 0, 20, 20], Math.toRadians(270)); Console.print("true"); } else{ g.fillTriangle([0, 0, 20, 20], Math.toRadians(90)); Console.print("false"); } }); btnPrevious.setLocalLookAndFeel(LafButton); btnNext.setLocalLookAndFeel(LafButton);
-
@observantsound lookandfeel functions are triggered whenever the OS decides to repaint an area or the entire UI element so it‘s absolutely fine for it to be executed multiple times and any logic that depends on it being called only when you assume it‘s necessary should be considered as flawed.
-
@Christoph-Hart I just caught my own mistake.
The wrong line was...
if (Console.print(obj.text) == "btnPrevious"){
When it should have been...
if (obj.text == "btnPrevious"){
Is there a way to make this more fool proof though?
To not make it dependent on exact button names? -
@observantsound Use a separate laf function for each button
-
@d-healey What if I know that I'll definitely re-use this for other buttons?
One pair might be called "btnPreviousSample" or "btnPreviousFX" etc.
-
@observantsound The part that will be reused can be placed in an inline function. The stuff that is different goes in the individual laf functions, from which you can call the inline function.
I thought I'd made a video about this but I can't find it now, maybe I imagined it...Ah I think this is the video I was thinking of. It's for paint routines but the same principle applies to laf.
-
Another option is to use the text property as a value. So you could set the text of one button to 270 and another to 90 and you use that value in your
.toRadians
call. -
@d-healey Using the txt property seems like a nice idea.
The video was also helpful. Thx!