Let me start by talking about my first run at implementing an integrated Layout solution.
- Layout is an interface, so if the DefaultLayout class doesn’t do what you like, you can always write your own.
- All Elements can contain their own layout
- All Elements also have an instance of LayoutHints for individually adjusting components if you aren’t satisfied with the Layout positioning.
- The default Layout has 3 modes to choose from aside from the GUI libraries default absolute positioning… these are:
— Vertical - positions elements on after another along the y axis
— Horizontal - positions elements on after another along the x axis until the next element would push past it’s parent bounds and the line wraps
— Flow - A combination of vertical and horizontal, using LayoutHints to allow you advanceY + set # of line feeds - Layouts have margins and padding settings.
- Element now has a centerToParentV and centerToParentH method
Here is a simple example of usage:
[java]
Screen screen = new Screen(this);
guiNode.addControl(screen);
DefaultLayout layout = new DefaultLayout(screen);
layout.setMode(Layout.LayoutMode.Vertical);
layout.setPadding(20);
layout.setMargins(30,30);
Panel p = new Panel(screen, Vector2f.ZERO, new Vector2f(200,500));
p.setLayout(layout);
for (int i = 0; i < 6; i++) {
ButtonAdapter b = new ButtonAdapter(screen, Vector2f.ZERO);
b.setText(String.valueOf(i));
p.addChild(b);
}
p.getLayout().layoutChildren();
p.sizeToContent();
screen.addElement§;
p.centerToParent();
[/java]
And here is the results:
Here is the same test with different margins and padding:
Here is a test of Horizontal mode:
Different margins and padding:
And most importantly, here is the Flow layout results with a complex set of components:
Now for the discussion part!
I have only implemented positioning, however I would like the Layouts to be able to set hints for how to size components as apposed to always using absolute dimensions. Can someone (or multiple someones) talk to me about approaches?