Versatile Java game engine with pluggable backends (this was used in Rogue, I think)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gamecore/src/mightypork/gamecore/gui/components/layout/FlowColumnLayout.java

87 lines
1.9 KiB

package mightypork.gamecore.gui.components.layout;
import mightypork.gamecore.gui.components.Component;
import mightypork.gamecore.gui.components.LayoutComponent;
import mightypork.utils.math.AlignX;
import mightypork.utils.math.constraints.num.Num;
import mightypork.utils.math.constraints.rect.Rect;
import mightypork.utils.math.constraints.rect.RectBound;
/**
* Holder with same-sized columns, aligned to left or right
*
* @author Ondřej Hruška (MightyPork)
*/
public class FlowColumnLayout extends LayoutComponent {
private int col = 0;
private Num elementWidth;
private final AlignX align;
/**
* @param context context
* @param elementWidth width of all elements
* @param align component align. Legal values are LEFT and RIGHT.
*/
public FlowColumnLayout(RectBound context, Num elementWidth, AlignX align)
{
super(context);
this.elementWidth = elementWidth;
this.align = align;
if (align != AlignX.LEFT && align != AlignX.RIGHT) {
throw new IllegalArgumentException("Can align only left or right.");
}
}
/**
* make a new holder.<br>
* Context must be assigned before rendering.
*
* @param elementWidth width of all elements
* @param align component align. Legal values are LEFT and RIGHT.
*/
public FlowColumnLayout(Num elementWidth, AlignX align)
{
this(null, elementWidth, align);
}
/**
* Add an item
*
* @param elem
*/
public void add(final Component elem)
{
if (elem == null) return;
final Rect r;
switch (align) {
case LEFT:
r = leftEdge().growRight(elementWidth).moveX(elementWidth.mul(col++));
break;
case RIGHT:
r = rightEdge().growLeft(elementWidth).moveX(elementWidth.mul(-(col++)));
break;
default:
throw new IllegalArgumentException("Bad align.");
}
elem.setRect(r);
attach(elem);
}
public void setElementWidth(Num elementWidth)
{
this.elementWidth = elementWidth;
}
}