| 1 | package net.minecraft.client.gui.layouts;
|
| 2 |
|
| 3 | import java.util.function.Consumer;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.Font;
|
| 7 | import net.minecraft.client.gui.components.StringWidget;
|
| 8 | import net.minecraft.client.gui.screens.Screen;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class HeaderAndFooterLayout implements Layout {
|
| 13 | public static final int MAGIC_PADDING = 13;
|
| 14 | public static final int DEFAULT_HEADER_AND_FOOTER_HEIGHT = 33;
|
| 15 | private static final int CONTENT_MARGIN_TOP = 30;
|
| 16 | private final FrameLayout headerFrame = new FrameLayout();
|
| 17 | private final FrameLayout footerFrame = new FrameLayout();
|
| 18 | private final FrameLayout contentsFrame = new FrameLayout();
|
| 19 | private final Screen screen;
|
| 20 | private int headerHeight;
|
| 21 | private int footerHeight;
|
| 22 |
|
| 23 | public HeaderAndFooterLayout(final Screen screen) {
|
| 24 | this(screen, 33);
|
| 25 | }
|
| 26 |
|
| 27 | public HeaderAndFooterLayout(final Screen screen, final int headerAndFooterHeight) {
|
| 28 | this(screen, headerAndFooterHeight, headerAndFooterHeight);
|
| 29 | }
|
| 30 |
|
| 31 | public HeaderAndFooterLayout(final Screen screen, final int headerHeight, final int footerHeight) {
|
| 32 | this.screen = screen;
|
| 33 | this.headerHeight = headerHeight;
|
| 34 | this.footerHeight = footerHeight;
|
| 35 | this.headerFrame.defaultChildLayoutSetting().align(0.5F, 0.5F);
|
| 36 | this.footerFrame.defaultChildLayoutSetting().align(0.5F, 0.5F);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void setX(final int x) {
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void setY(final int y) {
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public int getX() {
|
| 49 | return 0;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public int getY() {
|
| 54 | return 0;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public int getWidth() {
|
| 59 | return this.screen.width;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public int getHeight() {
|
| 64 | return this.screen.height;
|
| 65 | }
|
| 66 |
|
| 67 | public int getFooterHeight() {
|
| 68 | return this.footerHeight;
|
| 69 | }
|
| 70 |
|
| 71 | public void setFooterHeight(final int footerHeight) {
|
| 72 | this.footerHeight = footerHeight;
|
| 73 | }
|
| 74 |
|
| 75 | public void setHeaderHeight(final int headerHeight) {
|
| 76 | this.headerHeight = headerHeight;
|
| 77 | }
|
| 78 |
|
| 79 | public int getHeaderHeight() {
|
| 80 | return this.headerHeight;
|
| 81 | }
|
| 82 |
|
| 83 | public int getContentHeight() {
|
| 84 | return this.screen.height - this.getHeaderHeight() - this.getFooterHeight();
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void visitChildren(final Consumer<LayoutElement> layoutElementVisitor) {
|
| 89 | this.headerFrame.visitChildren(layoutElementVisitor);
|
| 90 | this.contentsFrame.visitChildren(layoutElementVisitor);
|
| 91 | this.footerFrame.visitChildren(layoutElementVisitor);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void removeChildren() {
|
| 96 | this.headerFrame.removeChildren();
|
| 97 | this.contentsFrame.removeChildren();
|
| 98 | this.footerFrame.removeChildren();
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public void arrangeElements() {
|
| 103 | int headerHeight = this.getHeaderHeight();
|
| 104 | int footerHeight = this.getFooterHeight();
|
| 105 | this.headerFrame.setMinWidth(this.screen.width);
|
| 106 | this.headerFrame.setMinHeight(headerHeight);
|
| 107 | this.headerFrame.setPosition(0, 0);
|
| 108 | this.headerFrame.arrangeElements();
|
| 109 | this.footerFrame.setMinWidth(this.screen.width);
|
| 110 | this.footerFrame.setMinHeight(footerHeight);
|
| 111 | this.footerFrame.arrangeElements();
|
| 112 | this.footerFrame.setY(this.screen.height - footerHeight);
|
| 113 | this.contentsFrame.setMinWidth(this.screen.width);
|
| 114 | this.contentsFrame.arrangeElements();
|
| 115 | int preferredContentY = headerHeight + 30;
|
| 116 | int maxContentY = this.screen.height - footerHeight - this.contentsFrame.getHeight();
|
| 117 | this.contentsFrame.setPosition(0, Math.min(preferredContentY, maxContentY));
|
| 118 | }
|
| 119 |
|
| 120 | public <T extends LayoutElement> T addToHeader(final T child) {
|
| 121 | return this.headerFrame.addChild(child);
|
| 122 | }
|
| 123 |
|
| 124 | public <T extends LayoutElement> T addToHeader(final T child, final Consumer<LayoutSettings> layoutSettingsAdjustments) {
|
| 125 | return this.headerFrame.addChild(child, layoutSettingsAdjustments);
|
| 126 | }
|
| 127 |
|
| 128 | public void addTitleHeader(final Component component, final Font font) {
|
| 129 | this.headerFrame.addChild(new StringWidget(component, font));
|
| 130 | }
|
| 131 |
|
| 132 | public <T extends LayoutElement> T addToFooter(final T child) {
|
| 133 | return this.footerFrame.addChild(child);
|
| 134 | }
|
| 135 |
|
| 136 | public <T extends LayoutElement> T addToFooter(final T child, final Consumer<LayoutSettings> layoutSettingsAdjustments) {
|
| 137 | return this.footerFrame.addChild(child, layoutSettingsAdjustments);
|
| 138 | }
|
| 139 |
|
| 140 | public <T extends LayoutElement> T addToContents(final T child) {
|
| 141 | return this.contentsFrame.addChild(child);
|
| 142 | }
|
| 143 |
|
| 144 | public <T extends LayoutElement> T addToContents(final T child, final Consumer<LayoutSettings> layoutSettingsAdjustments) {
|
| 145 | return this.contentsFrame.addChild(child, layoutSettingsAdjustments);
|
| 146 | }
|
| 147 | }
|
| 148 |
|