feat(FlowLayout): add option for vertical spacing
import org.terasology.math.geom.Vector2i;
import org.terasology.rendering.nui.Canvas;
import org.terasology.rendering.nui.CoreLayout;
+import org.terasology.rendering.nui.LayoutConfig;
import org.terasology.rendering.nui.LayoutHint;
import org.terasology.rendering.nui.UIWidget;
import java.util.List;
/**
+ *
*/
public class FlowLayout extends CoreLayout<LayoutHint> {
private List<UIWidget> contents = Lists.newArrayList();
+ /**
+ * The vertical spacing between adjacent widgets, in pixels
+ */
+ @LayoutConfig
+ private int verticalSpacing;
+
@Override
public void addWidget(UIWidget element, LayoutHint hint) {
contents.add(element);
@@ -55,7 +63,7 @@ public void onDraw(Canvas canvas) {
for (UIWidget widget : contents) {
Vector2i size = canvas.calculatePreferredSize(widget);
if (filledWidth != 0 && filledWidth + size.x > canvas.size().x) {
- heightOffset += filledHeight;
+ heightOffset += filledHeight + verticalSpacing;
filledWidth = 0;
filledHeight = 0;
}
@@ -74,7 +82,7 @@ public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
Vector2i size = canvas.calculatePreferredSize(widget);
if (filledWidth != 0 && filledWidth + size.x > sizeHint.x) {
result.x = Math.max(result.x, filledWidth);
- result.y += filledHeight;
+ result.y += filledHeight + verticalSpacing;
filledWidth = size.x;
filledHeight = size.y;
} else {
@@ -97,4 +105,24 @@ public Vector2i getMaxContentSize(Canvas canvas) {
public Iterator<UIWidget> iterator() {
return contents.iterator();
}
+
+ /**
+ * Retrieves the vertical spacing between adjacent widgets in this {@code FlowLayout}.
+ *
+ * @return The spacing, in pixels
+ */
+ public int getVerticalSpacing() {
+ return verticalSpacing;
+ }
+
+ /**
+ * Sets the vertical spacing between adjacent widgets in this {@code FlowLayout}.
+ *
+ * @param spacing The spacing, in pixels
+ * @return This {@code FlowLayout}
+ */
+ public FlowLayout setVerticalSpacing(int spacing) {
+ this.verticalSpacing = spacing;
+ return this;
+ }
}
