I’m playing with layouts and it is very common to have the exception:
java.lang.IllegalArgumentException: Size cannot be negative:(9.0, -0.5, 0.0)
at com.simsilica.lemur.core.GuiControl.setSize(GuiControl.java:299)
at com.simsilica.lemur.component.SpringGridLayout$Entry.setSize(SpringGridLayout.java:537)
After some tries I could verify that when a SpringGridLayout
(with defaults configs) has some childrens there is a size where the owner container can’t resize smaller without throwing the exception. The x-axis seems to be behaving well but the minimum y axis size is always (when having 2 children) the difference between both children + 2 (what seems to be some size set by the present components, eg: backgrounds adds +0.5).
Is this normal?, if so, why?
Edit: If I remember well, the layout always uses the index to get the axis values based on the major/minor axis so I suppose that it will give the same issues if they are switched (with x instead of y).
Some test code:
public class TestLemurLayouts extends SimpleApplication {
public static void main(String[] args) {
new TestLemurLayouts().start();
}
Container container;
public void simpleInitApp() {
GuiGlobals.initialize(stateManager.getApplication());
BaseStyles.loadGlassStyle();
GuiGlobals.getInstance().getStyles().setDefaultStyle("glass");
container = new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.Even, FillMode.Even));
container.setPreferredSize(new Vector3f(100, 100, 1));
test1();
}
private void test1() {
Panel a = container.addChild(new Panel("glass"));
Panel b = container.addChild(new Panel("glass"));
a.setPreferredSize(new Vector3f(10, 10, 1));
b.setPreferredSize(new Vector3f(20, 20, 1));
// If set, crashes starts at 9 instead of 11.
// container.setBackground(null);
// Doesn't crash
// container.setPreferredSize(new Vector3f(12, 12, 1));
// Crashes: java.lang.IllegalArgumentException: Size cannot be negative:(9.0, -0.5, 0.0)
container.setPreferredSize(new Vector3f(11, 11, 1));
setCommons(container);
}
private void test2() {
container.addChild(new Panel()).setPreferredSize(new Vector3f(20, 20, 1));
container.addChild(new Panel()).setPreferredSize(new Vector3f(20, 20, 1));
container.addChild(new Panel()).setPreferredSize(new Vector3f(20, 20, 1));
setCommons(container);
}
private void test3() {
container.addChild(new Panel()).setPreferredSize(new Vector3f(80, 80, 1));
container.addChild(new Panel()).setPreferredSize(new Vector3f(100, 100, 1));
// Crashes: java.lang.IllegalArgumentException: Size cannot be negative:(19.0, -0.5, 0.0)
container.setPreferredSize(new Vector3f(21, 21, 1));
setCommons(container);
}
private void setCommons(Panel container) {
container.setLocalTranslation(new Vector3f(500, 500, 0));
guiNode.attachChild(container);
}
}