Skip to content

Commit 3e1e9cc

Browse files
authored
Allow specifying alignment for Grid (#74)
1 parent 5656199 commit 3e1e9cc

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

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

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.cleanroommc.modularui.api.layout.ILayoutWidget;
44
import com.cleanroommc.modularui.api.widget.IWidget;
5+
import com.cleanroommc.modularui.utils.Alignment;
56
import com.cleanroommc.modularui.widget.ScrollWidget;
67
import com.cleanroommc.modularui.widget.scroll.HorizontalScrollData;
78
import com.cleanroommc.modularui.widget.scroll.ScrollData;
@@ -25,6 +26,7 @@ public class Grid extends ScrollWidget<Grid> implements ILayoutWidget {
2526
private final List<List<IWidget>> matrix = new ArrayList<>();
2627
private final Box minElementMargin = new Box();
2728
private int minRowHeight = 5, minColWidth = 5;
29+
private Alignment alignment = Alignment.Center;
2830
private boolean dirty = false;
2931

3032
public Grid() {
@@ -83,8 +85,8 @@ public void layoutWidgets() {
8385
int width = colSizes.get(c);
8486
IWidget child = this.matrix.get(r).get(c);
8587
if (child != null) {
86-
child.getArea().rx = (int) (x + width * 0.5 - child.getArea().width * 0.5);
87-
child.getArea().ry = (int) (y + height * 0.5 - child.getArea().height * 0.5);
88+
child.getArea().rx = (int) (x + (width - child.getArea().width) * alignment.x);
89+
child.getArea().ry = (int) (y + (height - child.getArea().height) * alignment.y);
8890
child.resizer().setPosResized(true, true);
8991
}
9092
x += width;
@@ -215,6 +217,11 @@ public Grid minRowHeight(int minRowHeight) {
215217
return this;
216218
}
217219

220+
public Grid alignment(Alignment alignment) {
221+
this.alignment = alignment;
222+
return this;
223+
}
224+
218225
public Grid scrollable() {
219226
return scrollable(new VerticalScrollData(), new HorizontalScrollData());
220227
}

0 commit comments

Comments
 (0)