| 1 | package net.minecraft.client.gui.layouts;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Consumer;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.Util;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class FrameLayout extends AbstractLayout {
|
| 14 | private final List<FrameLayout.ChildContainer> children = new ArrayList<>();
|
| 15 | private int minWidth;
|
| 16 | private int minHeight;
|
| 17 | private final LayoutSettings defaultChildLayoutSettings = LayoutSettings.defaults().align(0.5F, 0.5F);
|
| 18 |
|
| 19 | public FrameLayout() {
|
| 20 | this(0, 0, 0, 0);
|
| 21 | }
|
| 22 |
|
| 23 | public FrameLayout(final int minWidth, final int minHeight) {
|
| 24 | this(0, 0, minWidth, minHeight);
|
| 25 | }
|
| 26 |
|
| 27 | public FrameLayout(final int x, final int y, final int minWidth, final int minHeight) {
|
| 28 | super(x, y, minWidth, minHeight);
|
| 29 | this.setMinDimensions(minWidth, minHeight);
|
| 30 | }
|
| 31 |
|
| 32 | public FrameLayout setMinDimensions(final int minWidth, final int minHeight) {
|
| 33 | return this.setMinWidth(minWidth).setMinHeight(minHeight);
|
| 34 | }
|
| 35 |
|
| 36 | public FrameLayout setMinHeight(final int minHeight) {
|
| 37 | this.minHeight = minHeight;
|
| 38 | return this;
|
| 39 | }
|
| 40 |
|
| 41 | public FrameLayout setMinWidth(final int minWidth) {
|
| 42 | this.minWidth = minWidth;
|
| 43 | return this;
|
| 44 | }
|
| 45 |
|
| 46 | public LayoutSettings newChildLayoutSettings() {
|
| 47 | return this.defaultChildLayoutSettings.copy();
|
| 48 | }
|
| 49 |
|
| 50 | public LayoutSettings defaultChildLayoutSetting() {
|
| 51 | return this.defaultChildLayoutSettings;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public void arrangeElements() {
|
| 56 | super.arrangeElements();
|
| 57 | int resultWidth = this.minWidth;
|
| 58 | int resultHeight = this.minHeight;
|
| 59 |
|
| 60 | for (FrameLayout.ChildContainer child : this.children) {
|
| 61 | resultWidth = Math.max(resultWidth, child.getWidth());
|
| 62 | resultHeight = Math.max(resultHeight, child.getHeight());
|
| 63 | }
|
| 64 |
|
| 65 | for (FrameLayout.ChildContainer child : this.children) {
|
| 66 | child.setX(this.getX(), resultWidth);
|
| 67 | child.setY(this.getY(), resultHeight);
|
| 68 | }
|
| 69 |
|
| 70 | this.width = resultWidth;
|
| 71 | this.height = resultHeight;
|
| 72 | }
|
| 73 |
|
| 74 | public <T extends LayoutElement> T addChild(final T child) {
|
| 75 | return this.addChild(child, this.newChildLayoutSettings());
|
| 76 | }
|
| 77 |
|
| 78 | public <T extends LayoutElement> T addChild(final T child, final LayoutSettings childLayoutSettings) {
|
| 79 | this.children.add(new FrameLayout.ChildContainer(child, childLayoutSettings));
|
| 80 | return child;
|
| 81 | }
|
| 82 |
|
| 83 | public <T extends LayoutElement> T addChild(final T child, final Consumer<LayoutSettings> layoutSettingsAdjustments) {
|
| 84 | return this.addChild(child, Util.make(this.newChildLayoutSettings(), layoutSettingsAdjustments));
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void visitChildren(final Consumer<LayoutElement> layoutElementVisitor) {
|
| 89 | this.children.forEach(wrapper -> layoutElementVisitor.accept(wrapper.child));
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void removeChildren() {
|
| 94 | this.children.clear();
|
| 95 | }
|
| 96 |
|
| 97 | public static void centerInRectangle(final LayoutElement widget, final int x, final int y, final int width, final int height) {
|
| 98 | alignInRectangle(widget, x, y, width, height, 0.5F, 0.5F);
|
| 99 | }
|
| 100 |
|
| 101 | public static void centerInRectangle(final LayoutElement widget, final ScreenRectangle rectangle) {
|
| 102 | centerInRectangle(widget, rectangle.position().x(), rectangle.position().y(), rectangle.width(), rectangle.height());
|
| 103 | }
|
| 104 |
|
| 105 | public static void alignInRectangle(final LayoutElement widget, final ScreenRectangle rectangle, final float alignX, final float alignY) {
|
| 106 | alignInRectangle(widget, rectangle.left(), rectangle.top(), rectangle.width(), rectangle.height(), alignX, alignY);
|
| 107 | }
|
| 108 |
|
| 109 | public static void alignInRectangle(
|
| 110 | final LayoutElement widget, final int x, final int y, final int width, final int height, final float alignX, final float alignY
|
| 111 | ) {
|
| 112 | alignInDimension(x, width, widget.getWidth(), widget::setX, alignX);
|
| 113 | alignInDimension(y, height, widget.getHeight(), widget::setY, alignY);
|
| 114 | }
|
| 115 |
|
| 116 | public static void alignInDimension(final int pos, final int length, final int widgetLength, final Consumer<Integer> setWidgetPos, final float align) {
|
| 117 | int offset = (int)Mth.lerp(align, 0.0F, length - widgetLength);
|
| 118 | setWidgetPos.accept(pos + offset);
|
| 119 | }
|
| 120 |
|
| 121 | @Environment(EnvType.CLIENT)
|
| 122 | private static class ChildContainer extends AbstractLayout.AbstractChildWrapper {
|
| 123 | protected ChildContainer(final LayoutElement child, final LayoutSettings layoutSettings) {
|
| 124 | super(child, layoutSettings);
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|