Skip to content
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
34 changes: 34 additions & 0 deletions src/main/java/com/cleanroommc/modularui/widgets/layout/Grid.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cleanroommc.modularui.widgets.layout;

import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.GuiAxis;
import com.cleanroommc.modularui.api.layout.ILayoutWidget;
import com.cleanroommc.modularui.api.widget.IParentWidget;
Expand Down Expand Up @@ -33,6 +34,8 @@ public class Grid extends AbstractScrollWidget<IWidget, Grid> implements ILayout
private final Box minElementMargin = new Box();
private int minRowHeight = 5, minColWidth = 5;
private Alignment alignment = Alignment.Center;
private Alignment[] rowAlignments, colAlignments;
private boolean rowAlignmentOverride = false, colAlignmentOverride = false;
private boolean collapseDisabledChild = false;
private boolean dirty = false, unsanitized = false;

Expand Down Expand Up @@ -85,6 +88,8 @@ private int border(int index, int size) {
@Override
public boolean layoutWidgets() {
sanitizeMatrix();
checkAlignmentOverride();

IntList rowSizes = new IntArrayList();
IntList colSizes = new IntArrayList();

Expand Down Expand Up @@ -123,6 +128,10 @@ public boolean layoutWidgets() {
int xe = getMarginEnd(area, GuiAxis.X, xBorder);
int ys = getMarginStart(area, GuiAxis.Y, yBorder);
int ye = getMarginEnd(area, GuiAxis.Y, yBorder);
Alignment alignment = this.alignment;
if (rowAlignmentOverride) alignment = rowAlignments[r];
if (colAlignmentOverride) alignment = colAlignments[c];

child.getArea().rx = (int) (x + xs + (width - xs - xe - area.width) * alignment.x);
child.getArea().ry = (int) (y + ys + (height - ys - ye - area.height) * alignment.y);
child.resizer().setPosResized(true, true);
Expand Down Expand Up @@ -404,6 +413,20 @@ public Grid alignment(Alignment alignment) {
return this;
}

public Grid rowAlignments(Alignment[] rowAlignments) {
this.rowAlignments = rowAlignments;
this.rowAlignmentOverride = true;
this.colAlignmentOverride = false;
return this;
}

public Grid columnAlignments(Alignment[] colAlignments) {
this.colAlignments = colAlignments;
this.colAlignmentOverride = true;
this.rowAlignmentOverride = false;
return this;
}

public Grid scrollable() {
return scrollable(new VerticalScrollData(), new HorizontalScrollData());
}
Expand Down Expand Up @@ -576,6 +599,17 @@ private void sanitizeMatrix() {
this.unsanitized = false;
}

private void checkAlignmentOverride() {
if (rowAlignmentOverride && rowAlignments.length != matrix.size()) {
ModularUI.LOGGER.warn("Grid row alignment override cannot be applied, # of rows does not match overriding array size!");
rowAlignmentOverride = false;
}
if (colAlignmentOverride && colAlignments.length != matrix.get(0).size()) {
ModularUI.LOGGER.warn("Grid column alignment override cannot be applied, # of column does not match overriding array size!");
colAlignmentOverride = false;
}
}

private static void foreachRow(Iterable<IWidget> row, Consumer<IWidget> consumer) {
row.forEach(consumer);
}
Expand Down