feat(FlowLayout): Add option for horizontal spacing
@@ -45,6 +45,12 @@ public void addWidget(UIWidget element, LayoutHint hint) {
contents.add(element);
}
+ /**
+ * The horizontal spacing between adjacent widgets, in pixels
+ */
+ @LayoutConfig
+ private int horizontalSpacing;
+
@Override
public void removeWidget(UIWidget element) {
contents.remove(element);
@@ -68,7 +74,7 @@ public void onDraw(Canvas canvas) {
filledHeight = 0;
}
canvas.drawWidget(widget, Rect2i.createFromMinAndSize(filledWidth, heightOffset, size.x, size.y));
- filledWidth += size.x;
+ filledWidth += size.x + horizontalSpacing;
filledHeight = Math.max(filledHeight, size.y);
}
}
@@ -83,10 +89,10 @@ public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
if (filledWidth != 0 && filledWidth + size.x > sizeHint.x) {
result.x = Math.max(result.x, filledWidth);
result.y += filledHeight + verticalSpacing;
- filledWidth = size.x;
+ filledWidth = size.x + horizontalSpacing;
filledHeight = size.y;
} else {
- filledWidth += size.x;
+ filledWidth += size.x + horizontalSpacing;
filledHeight = Math.max(filledHeight, size.y);
}
}
@@ -106,6 +112,15 @@ public Vector2i getMaxContentSize(Canvas canvas) {
return contents.iterator();
}
+ /**
+ * Retrieves the horizontal spacing between adjacent widgets in this {@code FlowLayout}.
+ *
+ * @return The spacing, in pixels
+ */
+ public int getHorizontalSpacing() {
+ return horizontalSpacing;
+ }
+
/**
* Retrieves the vertical spacing between adjacent widgets in this {@code FlowLayout}.
*
@@ -115,6 +130,17 @@ public int getVerticalSpacing() {
return verticalSpacing;
}
+ /**
+ * Sets the horizontal spacing between adjacent widgets in this {@code FlowLayout}.
+ *
+ * @param spacing The spacing, in pixels
+ * @return This {@code FlowLayout}
+ */
+ public FlowLayout setHorizontalSpacing(int spacing) {
+ this.horizontalSpacing = spacing;
+ return this;
+ }
+
/**
* Sets the vertical spacing between adjacent widgets in this {@code FlowLayout}.
*
