Skip to content

Commit ea44b09

Browse files
authored
Added per-row and per-column Grid alignment (#127)
1 parent 0e8cf99 commit ea44b09

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

  • src/main/java/com/cleanroommc/modularui/widgets/layout

src/main/java/com/cleanroommc/modularui/widgets/layout/Grid.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.widgets.layout;
22

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

@@ -85,6 +88,8 @@ private int border(int index, int size) {
8588
@Override
8689
public boolean layoutWidgets() {
8790
sanitizeMatrix();
91+
checkAlignmentOverride();
92+
8893
IntList rowSizes = new IntArrayList();
8994
IntList colSizes = new IntArrayList();
9095

@@ -123,6 +128,10 @@ public boolean layoutWidgets() {
123128
int xe = getMarginEnd(area, GuiAxis.X, xBorder);
124129
int ys = getMarginStart(area, GuiAxis.Y, yBorder);
125130
int ye = getMarginEnd(area, GuiAxis.Y, yBorder);
131+
Alignment alignment = this.alignment;
132+
if (rowAlignmentOverride) alignment = rowAlignments[r];
133+
if (colAlignmentOverride) alignment = colAlignments[c];
134+
126135
child.getArea().rx = (int) (x + xs + (width - xs - xe - area.width) * alignment.x);
127136
child.getArea().ry = (int) (y + ys + (height - ys - ye - area.height) * alignment.y);
128137
child.resizer().setPosResized(true, true);
@@ -404,6 +413,20 @@ public Grid alignment(Alignment alignment) {
404413
return this;
405414
}
406415

416+
public Grid rowAlignments(Alignment[] rowAlignments) {
417+
this.rowAlignments = rowAlignments;
418+
this.rowAlignmentOverride = true;
419+
this.colAlignmentOverride = false;
420+
return this;
421+
}
422+
423+
public Grid columnAlignments(Alignment[] colAlignments) {
424+
this.colAlignments = colAlignments;
425+
this.colAlignmentOverride = true;
426+
this.rowAlignmentOverride = false;
427+
return this;
428+
}
429+
407430
public Grid scrollable() {
408431
return scrollable(new VerticalScrollData(), new HorizontalScrollData());
409432
}
@@ -576,6 +599,17 @@ private void sanitizeMatrix() {
576599
this.unsanitized = false;
577600
}
578601

602+
private void checkAlignmentOverride() {
603+
if (rowAlignmentOverride && rowAlignments.length != matrix.size()) {
604+
ModularUI.LOGGER.warn("Grid row alignment override cannot be applied, # of rows does not match overriding array size!");
605+
rowAlignmentOverride = false;
606+
}
607+
if (colAlignmentOverride && colAlignments.length != matrix.get(0).size()) {
608+
ModularUI.LOGGER.warn("Grid column alignment override cannot be applied, # of column does not match overriding array size!");
609+
colAlignmentOverride = false;
610+
}
611+
}
612+
579613
private static void foreachRow(Iterable<IWidget> row, Consumer<IWidget> consumer) {
580614
row.forEach(consumer);
581615
}

0 commit comments

Comments
 (0)