.setPosition does not accept float?
-
It seems that .setPosition is rounding the numbers that go into it, here producing uneven placement of keys on the x axis:
The x is being calculated by dividing the total keyboard area like so:
//Loops for positioning the keys //White keys for (i = 0; i < Math.ceil(keyboard0.length / 12 * 7); i++) { //Get keyboard panel area var a = [KeyboardPanel0.get("x"), KeyboardPanel0.get("y"), KeyboardPanel0.get("width"), KeyboardPanel0.get("height")]; keyboard0[allWhiteKeys[i]].setPosition (a[2] / 128 * (12 / 7) * i + gap, a[1] - offset, a[2] / 128 * (12 / 7) - gap, a[3] + offset * 0.333); }
Every time the x input is multiplied by "i" it gives a float that is getting rounded to the nearest integer, producing values that sometimes round down, and sometimes up, meaning the x interval keeps changing over the course of the loop.
If I use ceiling or floor on the x input, it evens out the spaces but does not align properly with the total keyboard width , as the little increments add up and top of the keyboard gets wonky.Any reason why the inputs are rounding to integers?
-
@VirtualVirgin Do they show as rounded in the interface designer property editor?
-
@d-healey said in .setPosition does not accept float?:
@VirtualVirgin Do they show as rounded in the interface designer property editor?
Yes, those show as rounded.
When I checked the API it says .setPosition only takes integers so-I tried this instead:
keyboard0[allWhiteKeys[i]].set("x", a[2] / 128 * (12 / 7) * i + gap / 2); keyboard0[allWhiteKeys[i]].set("y", a[1] - offset); keyboard0[allWhiteKeys[i]].set("width", a[2] / 128 * (12 / 7) - gap); keyboard0[allWhiteKeys[i]].set("height", (a[3] + offset * 0.333));
It sets the property by string, and shows rounding to 2 decimal points in the property editor for each key button:
But even with 100x the precision of the input, the appearance is exactly the same, with the jagged gap sizes:
Trying to set the gap size lower has very low resolution for change and this is the next lowest step it will accept:
So I can't seem to fix or fine tune the spacing.
-
@VirtualVirgin yes component positions are integers so if you need subpixel stuff you need to render it within a single component.
-
@VirtualVirgin Time to try again with a panel
-
@Christoph-Hart said in .setPosition does not accept float?:
@VirtualVirgin yes component positions are integers so if you need subpixel stuff you need to render it within a single component.
Can I ask why?
Maybe I'm wrong, but aren't there many use cases for spacing components the way I am (scaling/transforming multiple adjacent components from a panel area )? It seems odd to me that this then renders the use of any component for this purpose obsolete, meaning any components one has already made cannot be used (say using custom LAF sliders for a slider pack, or buttons for an input/output matrix).
I would be nice to have a bunch of components on a panel that can just be resized, then scaled as needed on compile without the irregular spacing.
-
@VirtualVirgin no there are exactly no use cases. Think about your screen it has fixed number of pixels, and can only turn them on or off, so all elements on your screen are at an integer position
-
Can I ask why?
JUCE says so (and perhaps some things in the underlying OS graphics API too but I haven't looked that far).
A good example how how to solve that issue is the slider pack though: if the width of the slider pack cannot be divided by
numSliders
to a integer, then it will calculate the "real" value and then round the position for both width and start position to the next integer:float x = 0.0f; for (int i = 0; i < sliders.size(); i++) { int thisXPos = std::floor(x); auto subMod = std::fmod(x, 1.0f); int thisWidth = std::floor(widthPerSlider + subMod) - 1; sliders[i]->setBounds(thisXPos, 0, thisWidth, getHeight()); x += widthPerSlider; }
This makes the sliders not 100% equal in width (some are 1 px bigger than the others), but it keeps the distance and gap between all sliders the same which is more important.
You should be able to apply this trick to your keyboard component as well.
-
@Christoph-Hart said in .setPosition does not accept float?:
You should be able to apply this trick to your keyboard component as well.
Ok, this is very cool!
I deciphered what you have here in the post and applied to my keyboard and it works!
-
@VirtualVirgin Looking good!