Skip to content

Commit

Permalink
feat(FlowLayout): add option for vertical alignment (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouRicky authored May 22, 2021
1 parent c40b01b commit 9001cad
Showing 1 changed file with 75 additions and 21 deletions.
96 changes: 75 additions & 21 deletions nui/src/main/java/org/terasology/nui/layouts/FlowLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public class FlowLayout extends CoreLayout<LayoutHint> {
@LayoutConfig
private Binding<Boolean> rightToLeftAlign = new DefaultBinding<>(false);

// vertical alignment code addition
@LayoutConfig
private Binding<Boolean> topToBottomAlign = new DefaultBinding<>(false);

@Override
public void addWidget(UIWidget element, LayoutHint hint) {
contents.add(element);
Expand Down Expand Up @@ -125,32 +129,63 @@ private Vector2i layout(Canvas canvas, Vector2i boundingSize, boolean draw) {
int heightOffset = 0;
// local maximum for row height
int rowHeight = 0;
// local maximum for row width
int rowWidth = 0;


if (isTopToBottomAlign()) {
for (UIWidget widget : contents) {
Vector2i size = canvas.calculatePreferredSize(widget);

if (heightOffset != 0 && heightOffset + verticalSpacing + size.y <= boundingSize.y) {
// place widget in current column
heightOffset += verticalSpacing;
} else if (heightOffset != 0) {
// wrap the column
result.x += rowWidth + horizontalSpacing;
result.y = Math.max(result.y, heightOffset);
widthOffset = result.x;
heightOffset = 0;
rowWidth = 0;
}

for (UIWidget widget : contents) {
Vector2i size = canvas.calculatePreferredSize(widget);

if (widthOffset != 0 && widthOffset + horizontalSpacing + size.x <= boundingSize.x) {
// place widget in the current row
widthOffset += horizontalSpacing;
} else if (widthOffset != 0) {
// wrap the row
result.x = Math.max(result.x, widthOffset);
result.y += rowHeight + verticalSpacing;
heightOffset = result.y;
widthOffset = 0;
rowHeight = 0;
if (draw) {
int xPosition = isRightToLeftAlign() ? canvas.size().x - widthOffset - size.x : widthOffset;
canvas.drawWidget(widget, RectUtility.createFromMinAndSize(xPosition, heightOffset, size.x, size.y));
}
heightOffset += size.y;
rowWidth = Math.max(rowWidth, size.x);
}

if (draw) {
int xPosition = isRightToLeftAlign() ? canvas.size().x - widthOffset - size.x : widthOffset;
canvas.drawWidget(widget, RectUtility.createFromMinAndSize(xPosition, heightOffset, size.x, size.y));
result.x += rowWidth;
result.y = Math.max(result.y, heightOffset);
} else {
for (UIWidget widget : contents) {
Vector2i size = canvas.calculatePreferredSize(widget);

if (widthOffset != 0 && widthOffset + horizontalSpacing + size.x <= boundingSize.x) {
// place widget in the current row
widthOffset += horizontalSpacing;
} else if (widthOffset != 0) {
// wrap the row
result.x = Math.max(result.x, widthOffset);
result.y += rowHeight + verticalSpacing;
heightOffset = result.y;
widthOffset = 0;
rowHeight = 0;
}

if (draw) {
int xPosition = isRightToLeftAlign() ? canvas.size().x - widthOffset - size.x : widthOffset;
canvas.drawWidget(widget, RectUtility.createFromMinAndSize(xPosition, heightOffset, size.x, size.y));
}
widthOffset += size.x;
rowHeight = Math.max(rowHeight, size.y);
}
widthOffset += size.x;
rowHeight = Math.max(rowHeight, size.y);
}

result.x = Math.max(result.x, widthOffset);
result.y += rowHeight;
result.x = Math.max(result.x, widthOffset);
result.y += rowHeight;
}

return result;
}
Expand Down Expand Up @@ -252,4 +287,23 @@ public void bindRightToLeftAlign(Binding<Boolean> binding) {
public void clearRightToLeftAlignBinding() {
this.rightToLeftAlign = new DefaultBinding<>(false);
}



// vertical alignment code addition
public boolean isTopToBottomAlign() {
return topToBottomAlign.get();
}

public void setTopToBottomAlign(boolean topToBottomAlign) {
this.topToBottomAlign.set(topToBottomAlign);
}

public void bindTopToBottomAlign(Binding<Boolean> binding) {
this.topToBottomAlign = binding;
}

public void clearTopToBottomAlignBinding() {
this.topToBottomAlign = new DefaultBinding<>(false);
}
}

0 comments on commit 9001cad

Please sign in to comment.