|
1 | 1 | package com.cleanroommc.modularui.widgets.layout; |
2 | 2 |
|
| 3 | +import com.cleanroommc.modularui.ModularUI; |
3 | 4 | import com.cleanroommc.modularui.api.GuiAxis; |
4 | 5 | import com.cleanroommc.modularui.api.layout.ILayoutWidget; |
5 | 6 | import com.cleanroommc.modularui.api.widget.IParentWidget; |
@@ -33,6 +34,8 @@ public class Grid extends AbstractScrollWidget<IWidget, Grid> implements ILayout |
33 | 34 | private final Box minElementMargin = new Box(); |
34 | 35 | private int minRowHeight = 5, minColWidth = 5; |
35 | 36 | private Alignment alignment = Alignment.Center; |
| 37 | + private Alignment[] rowAlignments, colAlignments; |
| 38 | + private boolean rowAlignmentOverride = false, colAlignmentOverride = false; |
36 | 39 | private boolean collapseDisabledChild = false; |
37 | 40 | private boolean dirty = false, unsanitized = false; |
38 | 41 |
|
@@ -85,6 +88,8 @@ private int border(int index, int size) { |
85 | 88 | @Override |
86 | 89 | public boolean layoutWidgets() { |
87 | 90 | sanitizeMatrix(); |
| 91 | + checkAlignmentOverride(); |
| 92 | + |
88 | 93 | IntList rowSizes = new IntArrayList(); |
89 | 94 | IntList colSizes = new IntArrayList(); |
90 | 95 |
|
@@ -123,6 +128,10 @@ public boolean layoutWidgets() { |
123 | 128 | int xe = getMarginEnd(area, GuiAxis.X, xBorder); |
124 | 129 | int ys = getMarginStart(area, GuiAxis.Y, yBorder); |
125 | 130 | 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 | + |
126 | 135 | child.getArea().rx = (int) (x + xs + (width - xs - xe - area.width) * alignment.x); |
127 | 136 | child.getArea().ry = (int) (y + ys + (height - ys - ye - area.height) * alignment.y); |
128 | 137 | child.resizer().setPosResized(true, true); |
@@ -404,6 +413,20 @@ public Grid alignment(Alignment alignment) { |
404 | 413 | return this; |
405 | 414 | } |
406 | 415 |
|
| 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 | + |
407 | 430 | public Grid scrollable() { |
408 | 431 | return scrollable(new VerticalScrollData(), new HorizontalScrollData()); |
409 | 432 | } |
@@ -576,6 +599,17 @@ private void sanitizeMatrix() { |
576 | 599 | this.unsanitized = false; |
577 | 600 | } |
578 | 601 |
|
| 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 | + |
579 | 613 | private static void foreachRow(Iterable<IWidget> row, Consumer<IWidget> consumer) { |
580 | 614 | row.forEach(consumer); |
581 | 615 | } |
|
0 commit comments