| 1 | package net.minecraft.client.gui.layouts;
|
| 2 |
|
| 3 | import com.mojang.math.Divisor;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.Util;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class GridLayout extends AbstractLayout {
|
| 14 | private final List<GridLayout.ChildContainer> children = new ArrayList<>();
|
| 15 | private final LayoutSettings defaultCellSettings = LayoutSettings.defaults();
|
| 16 | private int rowSpacing = 0;
|
| 17 | private int columnSpacing = 0;
|
| 18 |
|
| 19 | public GridLayout() {
|
| 20 | this(0, 0);
|
| 21 | }
|
| 22 |
|
| 23 | public GridLayout(final int x, final int y) {
|
| 24 | super(x, y, 0, 0);
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public void arrangeElements() {
|
| 29 | super.arrangeElements();
|
| 30 | int maxRow = 0;
|
| 31 | int maxColumn = 0;
|
| 32 |
|
| 33 | for (GridLayout.ChildContainer child : this.children) {
|
| 34 | maxRow = Math.max(child.getLastOccupiedRow(), maxRow);
|
| 35 | maxColumn = Math.max(child.getLastOccupiedColumn(), maxColumn);
|
| 36 | }
|
| 37 |
|
| 38 | int[] maxColumnWidths = new int[maxColumn + 1];
|
| 39 | int[] maxRowHeights = new int[maxRow + 1];
|
| 40 |
|
| 41 | for (GridLayout.ChildContainer child : this.children) {
|
| 42 | int childHeight = child.getHeight() - (child.occupiedRows - 1) * this.rowSpacing;
|
| 43 | Divisor heightDivisor = new Divisor(childHeight, child.occupiedRows);
|
| 44 |
|
| 45 | for (int row = child.row; row <= child.getLastOccupiedRow(); row++) {
|
| 46 | maxRowHeights[row] = Math.max(maxRowHeights[row], heightDivisor.nextInt());
|
| 47 | }
|
| 48 |
|
| 49 | int childWidth = child.getWidth() - (child.occupiedColumns - 1) * this.columnSpacing;
|
| 50 | Divisor widthDivisor = new Divisor(childWidth, child.occupiedColumns);
|
| 51 |
|
| 52 | for (int column = child.column; column <= child.getLastOccupiedColumn(); column++) {
|
| 53 | maxColumnWidths[column] = Math.max(maxColumnWidths[column], widthDivisor.nextInt());
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | int[] columnXOffsets = new int[maxColumn + 1];
|
| 58 | int[] rowYOffsets = new int[maxRow + 1];
|
| 59 | columnXOffsets[0] = 0;
|
| 60 |
|
| 61 | for (int column = 1; column <= maxColumn; column++) {
|
| 62 | columnXOffsets[column] = columnXOffsets[column - 1] + maxColumnWidths[column - 1] + this.columnSpacing;
|
| 63 | }
|
| 64 |
|
| 65 | rowYOffsets[0] = 0;
|
| 66 |
|
| 67 | for (int row = 1; row <= maxRow; row++) {
|
| 68 | rowYOffsets[row] = rowYOffsets[row - 1] + maxRowHeights[row - 1] + this.rowSpacing;
|
| 69 | }
|
| 70 |
|
| 71 | for (GridLayout.ChildContainer child : this.children) {
|
| 72 | int availableWidth = 0;
|
| 73 |
|
| 74 | for (int column = child.column; column <= child.getLastOccupiedColumn(); column++) {
|
| 75 | availableWidth += maxColumnWidths[column];
|
| 76 | }
|
| 77 |
|
| 78 | availableWidth += this.columnSpacing * (child.occupiedColumns - 1);
|
| 79 | child.setX(this.getX() + columnXOffsets[child.column], availableWidth);
|
| 80 | int availableHeight = 0;
|
| 81 |
|
| 82 | for (int row = child.row; row <= child.getLastOccupiedRow(); row++) {
|
| 83 | availableHeight += maxRowHeights[row];
|
| 84 | }
|
| 85 |
|
| 86 | availableHeight += this.rowSpacing * (child.occupiedRows - 1);
|
| 87 | child.setY(this.getY() + rowYOffsets[child.row], availableHeight);
|
| 88 | }
|
| 89 |
|
| 90 | this.width = columnXOffsets[maxColumn] + maxColumnWidths[maxColumn];
|
| 91 | this.height = rowYOffsets[maxRow] + maxRowHeights[maxRow];
|
| 92 | }
|
| 93 |
|
| 94 | public <T extends LayoutElement> T addChild(final T child, final int row, final int column) {
|
| 95 | return this.addChild(child, row, column, this.newCellSettings());
|
| 96 | }
|
| 97 |
|
| 98 | public <T extends LayoutElement> T addChild(final T child, final int row, final int column, final LayoutSettings cellSettings) {
|
| 99 | return this.addChild(child, row, column, 1, 1, cellSettings);
|
| 100 | }
|
| 101 |
|
| 102 | public <T extends LayoutElement> T addChild(final T child, final int row, final int column, final Consumer<LayoutSettings> layoutSettingsAdjustments) {
|
| 103 | return this.addChild(child, row, column, 1, 1, Util.make(this.newCellSettings(), layoutSettingsAdjustments));
|
| 104 | }
|
| 105 |
|
| 106 | public <T extends LayoutElement> T addChild(final T child, final int row, final int column, final int rows, final int columns) {
|
| 107 | return this.addChild(child, row, column, rows, columns, this.newCellSettings());
|
| 108 | }
|
| 109 |
|
| 110 | public <T extends LayoutElement> T addChild(
|
| 111 | final T child, final int row, final int column, final int rows, final int columns, final LayoutSettings cellSettings
|
| 112 | ) {
|
| 113 | if (rows < 1) {
|
| 114 | throw new IllegalArgumentException("Occupied rows must be at least 1");
|
| 115 | }
|
| 116 |
|
| 117 | if (columns < 1) {
|
| 118 | throw new IllegalArgumentException("Occupied columns must be at least 1");
|
| 119 | }
|
| 120 |
|
| 121 | this.children.add(new GridLayout.ChildContainer(child, row, column, rows, columns, cellSettings));
|
| 122 | return child;
|
| 123 | }
|
| 124 |
|
| 125 | public <T extends LayoutElement> T addChild(
|
| 126 | final T child, final int row, final int column, final int rows, final int columns, final Consumer<LayoutSettings> layoutSettingsAdjustments
|
| 127 | ) {
|
| 128 | return this.addChild(child, row, column, rows, columns, Util.make(this.newCellSettings(), layoutSettingsAdjustments));
|
| 129 | }
|
| 130 |
|
| 131 | public GridLayout columnSpacing(final int columnSpacing) {
|
| 132 | this.columnSpacing = columnSpacing;
|
| 133 | return this;
|
| 134 | }
|
| 135 |
|
| 136 | public GridLayout rowSpacing(final int rowSpacing) {
|
| 137 | this.rowSpacing = rowSpacing;
|
| 138 | return this;
|
| 139 | }
|
| 140 |
|
| 141 | public GridLayout spacing(final int spacing) {
|
| 142 | return this.columnSpacing(spacing).rowSpacing(spacing);
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public void visitChildren(final Consumer<LayoutElement> layoutElementVisitor) {
|
| 147 | this.children.forEach(child -> layoutElementVisitor.accept(child.child));
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public void removeChildren() {
|
| 152 | this.children.clear();
|
| 153 | }
|
| 154 |
|
| 155 | public LayoutSettings newCellSettings() {
|
| 156 | return this.defaultCellSettings.copy();
|
| 157 | }
|
| 158 |
|
| 159 | public LayoutSettings defaultCellSetting() {
|
| 160 | return this.defaultCellSettings;
|
| 161 | }
|
| 162 |
|
| 163 | public GridLayout.RowHelper createRowHelper(final int columns) {
|
| 164 | return new GridLayout.RowHelper(columns);
|
| 165 | }
|
| 166 |
|
| 167 | @Environment(EnvType.CLIENT)
|
| 168 | private static class ChildContainer extends AbstractLayout.AbstractChildWrapper {
|
| 169 | private final int row;
|
| 170 | private final int column;
|
| 171 | private final int occupiedRows;
|
| 172 | private final int occupiedColumns;
|
| 173 |
|
| 174 | private ChildContainer(
|
| 175 | final LayoutElement widget, final int row, final int column, final int occupiedRows, final int occupiedColumns, final LayoutSettings cellSettings
|
| 176 | ) {
|
| 177 | super(widget, cellSettings.getExposed());
|
| 178 | this.row = row;
|
| 179 | this.column = column;
|
| 180 | this.occupiedRows = occupiedRows;
|
| 181 | this.occupiedColumns = occupiedColumns;
|
| 182 | }
|
| 183 |
|
| 184 | public int getLastOccupiedRow() {
|
| 185 | return this.row + this.occupiedRows - 1;
|
| 186 | }
|
| 187 |
|
| 188 | public int getLastOccupiedColumn() {
|
| 189 | return this.column + this.occupiedColumns - 1;
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | @Environment(EnvType.CLIENT)
|
| 194 | public final class RowHelper {
|
| 195 | private final int columns;
|
| 196 | private int index;
|
| 197 |
|
| 198 | private RowHelper(final int columns) {
|
| 199 | this.columns = columns;
|
| 200 | }
|
| 201 |
|
| 202 | public <T extends LayoutElement> T addChild(final T widget) {
|
| 203 | return this.addChild(widget, 1);
|
| 204 | }
|
| 205 |
|
| 206 | public <T extends LayoutElement> T addChild(final T widget, final int columnWidth) {
|
| 207 | return this.addChild(widget, columnWidth, this.defaultCellSetting());
|
| 208 | }
|
| 209 |
|
| 210 | public <T extends LayoutElement> T addChild(final T widget, final LayoutSettings layoutSettings) {
|
| 211 | return this.addChild(widget, 1, layoutSettings);
|
| 212 | }
|
| 213 |
|
| 214 | public <T extends LayoutElement> T addChild(final T widget, final int columnWidth, final LayoutSettings layoutSettings) {
|
| 215 | int row = this.index / this.columns;
|
| 216 | int columnBegin = this.index % this.columns;
|
| 217 | if (columnBegin + columnWidth > this.columns) {
|
| 218 | row++;
|
| 219 | columnBegin = 0;
|
| 220 | this.index = Mth.roundToward(this.index, this.columns);
|
| 221 | }
|
| 222 |
|
| 223 | this.index += columnWidth;
|
| 224 | return GridLayout.this.addChild(widget, row, columnBegin, 1, columnWidth, layoutSettings);
|
| 225 | }
|
| 226 |
|
| 227 | public GridLayout getGrid() {
|
| 228 | return GridLayout.this;
|
| 229 | }
|
| 230 |
|
| 231 | public LayoutSettings newCellSettings() {
|
| 232 | return GridLayout.this.newCellSettings();
|
| 233 | }
|
| 234 |
|
| 235 | public LayoutSettings defaultCellSetting() {
|
| 236 | return GridLayout.this.defaultCellSetting();
|
| 237 | }
|
| 238 | }
|
| 239 | }
|
| 240 |
|