Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FlowLayout): add option for vertical alignment #53

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}