| 1 | package net.minecraft.client.gui.layouts;
|
| 2 |
|
| 3 | import com.mojang.math.Divisor;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.Iterator;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.util.Util;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class EqualSpacingLayout extends AbstractLayout {
|
| 14 | private final EqualSpacingLayout.Orientation orientation;
|
| 15 | private final List<EqualSpacingLayout.ChildContainer> children = new ArrayList<>();
|
| 16 | private final LayoutSettings defaultChildLayoutSettings = LayoutSettings.defaults();
|
| 17 |
|
| 18 | public EqualSpacingLayout(final int width, final int height, final EqualSpacingLayout.Orientation orientation) {
|
| 19 | this(0, 0, width, height, orientation);
|
| 20 | }
|
| 21 |
|
| 22 | public EqualSpacingLayout(final int x, final int y, final int width, final int height, final EqualSpacingLayout.Orientation orientation) {
|
| 23 | super(x, y, width, height);
|
| 24 | this.orientation = orientation;
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public void arrangeElements() {
|
| 29 | super.arrangeElements();
|
| 30 | if (!this.children.isEmpty()) {
|
| 31 | int totalChildPrimaryLength = 0;
|
| 32 | int maxChildSecondaryLength = this.orientation.getSecondaryLength(this);
|
| 33 |
|
| 34 | for (EqualSpacingLayout.ChildContainer child : this.children) {
|
| 35 | totalChildPrimaryLength += this.orientation.getPrimaryLength(child);
|
| 36 | maxChildSecondaryLength = Math.max(maxChildSecondaryLength, this.orientation.getSecondaryLength(child));
|
| 37 | }
|
| 38 |
|
| 39 | int remainingSpace = this.orientation.getPrimaryLength(this) - totalChildPrimaryLength;
|
| 40 | int position = this.orientation.getPrimaryPosition(this);
|
| 41 | Iterator<EqualSpacingLayout.ChildContainer> childIterator = this.children.iterator();
|
| 42 | EqualSpacingLayout.ChildContainer firstChild = childIterator.next();
|
| 43 | this.orientation.setPrimaryPosition(firstChild, position);
|
| 44 | position += this.orientation.getPrimaryLength(firstChild);
|
| 45 | if (this.children.size() >= 2) {
|
| 46 | Divisor divisor = new Divisor(remainingSpace, this.children.size() - 1);
|
| 47 |
|
| 48 | while (divisor.hasNext()) {
|
| 49 | position += divisor.nextInt();
|
| 50 | EqualSpacingLayout.ChildContainer child = childIterator.next();
|
| 51 | this.orientation.setPrimaryPosition(child, position);
|
| 52 | position += this.orientation.getPrimaryLength(child);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | int thisSecondaryPosition = this.orientation.getSecondaryPosition(this);
|
| 57 |
|
| 58 | for (EqualSpacingLayout.ChildContainer child : this.children) {
|
| 59 | this.orientation.setSecondaryPosition(child, thisSecondaryPosition, maxChildSecondaryLength);
|
| 60 | }
|
| 61 |
|
| 62 | switch (this.orientation) {
|
| 63 | case HORIZONTAL:
|
| 64 | this.height = maxChildSecondaryLength;
|
| 65 | break;
|
| 66 | case VERTICAL:
|
| 67 | this.width = maxChildSecondaryLength;
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void visitChildren(final Consumer<LayoutElement> layoutElementVisitor) {
|
| 74 | this.children.forEach(wrapper -> layoutElementVisitor.accept(wrapper.child));
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void removeChildren() {
|
| 79 | this.children.clear();
|
| 80 | }
|
| 81 |
|
| 82 | public LayoutSettings newChildLayoutSettings() {
|
| 83 | return this.defaultChildLayoutSettings.copy();
|
| 84 | }
|
| 85 |
|
| 86 | public LayoutSettings defaultChildLayoutSetting() {
|
| 87 | return this.defaultChildLayoutSettings;
|
| 88 | }
|
| 89 |
|
| 90 | public <T extends LayoutElement> T addChild(final T child) {
|
| 91 | return this.addChild(child, this.newChildLayoutSettings());
|
| 92 | }
|
| 93 |
|
| 94 | public <T extends LayoutElement> T addChild(final T child, final LayoutSettings layoutSettings) {
|
| 95 | this.children.add(new EqualSpacingLayout.ChildContainer(child, layoutSettings));
|
| 96 | return child;
|
| 97 | }
|
| 98 |
|
| 99 | public <T extends LayoutElement> T addChild(final T child, final Consumer<LayoutSettings> layoutSettingsAdjustments) {
|
| 100 | return this.addChild(child, Util.make(this.newChildLayoutSettings(), layoutSettingsAdjustments));
|
| 101 | }
|
| 102 |
|
| 103 | @Environment(EnvType.CLIENT)
|
| 104 | private static class ChildContainer extends AbstractLayout.AbstractChildWrapper {
|
| 105 | protected ChildContainer(final LayoutElement child, final LayoutSettings layoutSettings) {
|
| 106 | super(child, layoutSettings);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | @Environment(EnvType.CLIENT)
|
| 111 | public enum Orientation {
|
| 112 | HORIZONTAL,
|
| 113 | VERTICAL;
|
| 114 |
|
| 115 | private int getPrimaryLength(final LayoutElement widget) {
|
| 116 | return switch (this) {
|
| 117 | case HORIZONTAL -> widget.getWidth();
|
| 118 | case VERTICAL -> widget.getHeight();
|
| 119 | };
|
| 120 | }
|
| 121 |
|
| 122 | private int getPrimaryLength(final EqualSpacingLayout.ChildContainer childContainer) {
|
| 123 | return switch (this) {
|
| 124 | case HORIZONTAL -> childContainer.getWidth();
|
| 125 | case VERTICAL -> childContainer.getHeight();
|
| 126 | };
|
| 127 | }
|
| 128 |
|
| 129 | private int getSecondaryLength(final LayoutElement widget) {
|
| 130 | return switch (this) {
|
| 131 | case HORIZONTAL -> widget.getHeight();
|
| 132 | case VERTICAL -> widget.getWidth();
|
| 133 | };
|
| 134 | }
|
| 135 |
|
| 136 | private int getSecondaryLength(final EqualSpacingLayout.ChildContainer childContainer) {
|
| 137 | return switch (this) {
|
| 138 | case HORIZONTAL -> childContainer.getHeight();
|
| 139 | case VERTICAL -> childContainer.getWidth();
|
| 140 | };
|
| 141 | }
|
| 142 |
|
| 143 | private void setPrimaryPosition(final EqualSpacingLayout.ChildContainer childContainer, final int position) {
|
| 144 | switch (this) {
|
| 145 | case HORIZONTAL:
|
| 146 | childContainer.setX(position, childContainer.getWidth());
|
| 147 | break;
|
| 148 | case VERTICAL:
|
| 149 | childContainer.setY(position, childContainer.getHeight());
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private void setSecondaryPosition(final EqualSpacingLayout.ChildContainer childContainer, final int position, final int availableSpace) {
|
| 154 | switch (this) {
|
| 155 | case HORIZONTAL:
|
| 156 | childContainer.setY(position, availableSpace);
|
| 157 | break;
|
| 158 | case VERTICAL:
|
| 159 | childContainer.setX(position, availableSpace);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | private int getPrimaryPosition(final LayoutElement widget) {
|
| 164 | return switch (this) {
|
| 165 | case HORIZONTAL -> widget.getX();
|
| 166 | case VERTICAL -> widget.getY();
|
| 167 | };
|
| 168 | }
|
| 169 |
|
| 170 | private int getSecondaryPosition(final LayoutElement widget) {
|
| 171 | return switch (this) {
|
| 172 | case HORIZONTAL -> widget.getY();
|
| 173 | case VERTICAL -> widget.getX();
|
| 174 | };
|
| 175 | }
|
| 176 | }
|
| 177 | }
|
| 178 |
|