Function Problems
-
Hey!
I think I've been trying to fix this issue for hours and it just wont play nice! I have this:
namespace HoverAnimation { inline function applyHoverAnimation(component, baseColorNest, hoverColorNest, alphaSpeedNest, paintFnNest, clickFnNest) { reg nest = { baseColor: baseColorNest, hoverColor: hoverColorNest, alphaSpeed: alphaSpeedNest, paintFn: paintFnNest, clickFn: clickFnNest }; component.data.baseColor = baseColorNest; component.data.hoverColor = hoverColorNest; component.data.alphaSpeed = alphaSpeedNest; component.data.alpha = 0.0; component.data.hover = false; component.setPaintRoutine(function(g) { g.setColour(Colours.mix(this.data.baseColor, this.data.hoverColor, this.data.alpha)); nest.paintFn(g, this); }); component.setTimerCallback(function() { if (this.data.hover) { if (this.data.alpha < 1.0) this.data.alpha += this.data.alphaSpeed; } else { if (this.data.alpha > 0.0) this.data.alpha -= this.data.alphaSpeed; } this.data.alpha = Math.range(this.data.alpha, 0.0, 1.0); this.repaint(); if (this.data.alpha == 1.0 || this.data.alpha == 0.0) this.stopTimer(); }); component.setMouseCallback(function(event) { if (event.clicked) nest.clickFn(this); if (event.hover != this.data.hover) { this.data.hover = event.hover; this.startTimer(1000 / 60); } }); } }
I'm basically trying to make the hover animation stuff modular but as you can probably see, its a mess from some debugging. I saw this:
https://forum.hise.audio/topic/5370/possible-breaking-change-local-variables-references-inside-nested-function-definitionswhich made this somewhat harder and now I'm in a mess trying to solve this, I can't store functions in component.data and reg nest wont work as all the icons that use this fcn will be overridden by the latest call of the fcn. Local variables wont work due to scope issues, i'm completely lost and I believe there is an easier way to do all this and another pair of eyes would help!
Thanks!
-
@Casmat You shouldn't declare
reg
variables inside functions, uselocal
instead.I think the whole nest thing is unnecessary since we now have lambda variables.
It's hard to debug a block of code like this. Put it into a snippet that demonstrates the issue.
-
HiseSnippet 5025.3ocsZ1ujihjb.u6819BOs843tH7+61ye0i0bLBITizNdsWfBjPHjnz2nKt3BDfPfP.BPewcaD9QyOA9YwuA1YpVS2p6Yta2chvytwL+prxpprpJqrpRTFow1tYYwoWc8aF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qsf09ZK3CesEj+qsf0+oKH9JvD1lG+3dpfB5FxmhKd80xQVfG3Iu0q9+vNfQxI
Its kind of messy, but should show the usage of what I'm trying to do
Edit: updated snippet
-
Instead of passing individual parameters into your function that then set an object, you could just pass an object in.
There used to be a limit of 5 parameters for an inline function, I don't think that limit applies any more, but generally I find if you have more than 5 parameters your function is too complicated.
I also don't think it's good practice to pass functions as parameters - except for callbacks.
Will each component have a different paint routine and mouse callback?
-
@d-healey Ahh yup, will put it in objects!
And yeah each component would have different paint routines/callbacks, I was intending on using it more for a general animation use case and being able to apply it to any panel
-
@Casmat said in Function Problems:
each component would have different paint routines/callbacks,
Then these don't need to go in the factory function at all.
-
Your function will end up something like this
inline function applyHoverAnimation(component, properties) { for (propertyName in properties) component.data[propertyName] = properties[propertyName]; component.data.alpha = 0.0; component.data.hover = false; component.setTimerCallback(function() { if (this.data.hover && this.data.alpha < 1.0) this.data.alpha += this.data.alphaSpeed; if (!this.data.alpha && this.data.alpha > 0.0) this.data.alpha -= this.data.alphaSpeed; this.data.alpha = Math.range(this.data.alpha, 0.0, 1.0); this.repaint(); if (this.data.alpha >= 1.0 || this.data.alpha <= 0.0) this.stopTimer(); }); }
-
@d-healey I see! Thanks for your help!