| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.Collection;
|
| 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.client.Minecraft;
|
| 10 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 11 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 12 | import net.minecraft.client.gui.layouts.Layout;
|
| 13 | import net.minecraft.client.gui.layouts.LayoutElement;
|
| 14 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 15 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 16 | import net.minecraft.client.gui.navigation.ScreenDirection;
|
| 17 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 18 | import net.minecraft.network.chat.CommonComponents;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class ScrollableLayout implements Layout {
|
| 23 | private static final int DEFAULT_SCROLLBAR_SPACING = 4;
|
| 24 | private final Layout content;
|
| 25 | private final ScrollableLayout.Container container;
|
| 26 | private final ScrollableLayout.ReserveStrategy reserveStrategy;
|
| 27 | private int scrollbarSpacing = 4;
|
| 28 | private int minWidth;
|
| 29 | private int minHeight;
|
| 30 | private int maxHeight;
|
| 31 |
|
| 32 | public ScrollableLayout(final Minecraft minecraft, final Layout content, final int maxHeight) {
|
| 33 | this(minecraft, content, maxHeight, ScrollableLayout.ReserveStrategy.BOTH);
|
| 34 | }
|
| 35 |
|
| 36 | public ScrollableLayout(final Minecraft minecraft, final Layout content, final int maxHeight, final ScrollableLayout.ReserveStrategy reserveStrategy) {
|
| 37 | this.content = content;
|
| 38 | this.maxHeight = maxHeight;
|
| 39 | this.reserveStrategy = reserveStrategy;
|
| 40 | this.container = new ScrollableLayout.Container(minecraft, 0, maxHeight, AbstractScrollArea.defaultSettings(10));
|
| 41 | }
|
| 42 |
|
| 43 | public void setScrollbarSpacing(final int scrollbarSpacing) {
|
| 44 | this.scrollbarSpacing = scrollbarSpacing;
|
| 45 | }
|
| 46 |
|
| 47 | public void setMinWidth(final int minWidth) {
|
| 48 | this.minWidth = minWidth;
|
| 49 | this.container.setWidth(Math.max(this.content.getWidth(), minWidth));
|
| 50 | }
|
| 51 |
|
| 52 | public void setMinHeight(final int minHeight) {
|
| 53 | this.minHeight = minHeight;
|
| 54 | this.container.setHeight(Math.max(this.content.getHeight(), minHeight));
|
| 55 | }
|
| 56 |
|
| 57 | public void setMaxHeight(final int maxHeight) {
|
| 58 | this.maxHeight = maxHeight;
|
| 59 | this.container.setHeight(Math.min(this.content.getHeight(), maxHeight));
|
| 60 | this.container.refreshScrollAmount();
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public void arrangeElements() {
|
| 65 | this.content.arrangeElements();
|
| 66 | int contentWidth = this.content.getWidth();
|
| 67 |
|
| 68 | int scrollbarReserve = switch (this.reserveStrategy) {
|
| 69 | case RIGHT -> this.container.scrollbarReserve();
|
| 70 | case BOTH -> 2 * this.container.scrollbarReserve();
|
| 71 | };
|
| 72 | this.container.setWidth(Math.max(contentWidth, this.minWidth) + scrollbarReserve);
|
| 73 | this.container.setHeight(Math.clamp(this.container.getHeight(), this.minHeight, this.maxHeight));
|
| 74 | this.container.refreshChildren();
|
| 75 | this.container.refreshScrollAmount();
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void visitChildren(final Consumer<LayoutElement> layoutElementVisitor) {
|
| 80 | layoutElementVisitor.accept(this.container);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void removeChildren() {
|
| 85 | this.container.children().clear();
|
| 86 | this.content.removeChildren();
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void setX(final int x) {
|
| 91 | this.container.setX(x);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void setY(final int y) {
|
| 96 | this.container.setY(y);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public int getX() {
|
| 101 | return this.container.getX();
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public int getY() {
|
| 106 | return this.container.getY();
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public int getWidth() {
|
| 111 | return this.container.getWidth();
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public int getHeight() {
|
| 116 | return this.container.getHeight();
|
| 117 | }
|
| 118 |
|
| 119 | public double getScrollAmount() {
|
| 120 | return this.container.scrollAmount();
|
| 121 | }
|
| 122 |
|
| 123 | @Environment(EnvType.CLIENT)
|
| 124 | private class Container extends AbstractContainerWidget {
|
| 125 | private final Minecraft minecraft;
|
| 126 | private final List<AbstractWidget> children = new ArrayList<>();
|
| 127 |
|
| 128 | public Container(final Minecraft minecraft, final int width, final int height, final AbstractScrollArea.ScrollbarSettings scrollbarSettings) {
|
| 129 | super(0, 0, width, height, CommonComponents.EMPTY, scrollbarSettings);
|
| 130 | this.minecraft = minecraft;
|
| 131 | this.refreshChildren();
|
| 132 | }
|
| 133 |
|
| 134 | public void refreshChildren() {
|
| 135 | this.children.clear();
|
| 136 | ScrollableLayout.this.content.visitWidgets(this.children::add);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected int contentHeight() {
|
| 141 | return ScrollableLayout.this.content.getHeight();
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 146 | graphics.enableScissor(this.getX(), this.getY(), this.getX() + this.width, this.getY() + this.height);
|
| 147 |
|
| 148 | for (AbstractWidget child : this.children) {
|
| 149 | child.extractRenderState(graphics, mouseX, mouseY, a);
|
| 150 | }
|
| 151 |
|
| 152 | graphics.disableScissor();
|
| 153 | this.extractScrollbar(graphics, mouseX, mouseY);
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | protected void updateWidgetNarration(final NarrationElementOutput output) {
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public ScreenRectangle getBorderForArrowNavigation(final ScreenDirection opposite) {
|
| 162 | GuiEventListener focused = this.getFocused();
|
| 163 | return focused != null
|
| 164 | ? focused.getBorderForArrowNavigation(opposite)
|
| 165 | : new ScreenRectangle(this.getX(), this.getY(), this.width, this.contentHeight()).getBorder(opposite);
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | public void setFocused(final @Nullable GuiEventListener focused) {
|
| 170 | super.setFocused(focused);
|
| 171 | if (focused != null && this.minecraft.getLastInputType().isKeyboard()) {
|
| 172 | ScreenRectangle area = this.getRectangle();
|
| 173 | ScreenRectangle focusedRect = focused.getRectangle();
|
| 174 | int topDelta = focusedRect.top() - area.top();
|
| 175 | int bottomDelta = focusedRect.bottom() - area.bottom();
|
| 176 | double scrollRate = this.scrollRate();
|
| 177 | if (topDelta < 0) {
|
| 178 | this.setScrollAmount(this.scrollAmount() + topDelta - scrollRate);
|
| 179 | } else if (bottomDelta > 0) {
|
| 180 | this.setScrollAmount(this.scrollAmount() + bottomDelta + scrollRate);
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public void setX(final int x) {
|
| 187 | super.setX(x);
|
| 188 | ScrollableLayout.this.content.setX(x + (ScrollableLayout.this.reserveStrategy == ScrollableLayout.ReserveStrategy.BOTH ? this.scrollbarReserve() : 0));
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public void setY(final int y) {
|
| 193 | super.setY(y);
|
| 194 | ScrollableLayout.this.content.setY(y - (int)this.scrollAmount());
|
| 195 | }
|
| 196 |
|
| 197 | private int scrollbarReserve() {
|
| 198 | return ScrollableLayout.this.scrollbarSpacing + this.scrollbarWidth();
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public void setScrollAmount(final double scrollAmount) {
|
| 203 | super.setScrollAmount(scrollAmount);
|
| 204 | ScrollableLayout.this.content.setY(this.getRectangle().top() - (int)this.scrollAmount());
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public List<? extends GuiEventListener> children() {
|
| 209 | return this.children;
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public Collection<? extends NarratableEntry> getNarratables() {
|
| 214 | return this.children;
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | @Environment(EnvType.CLIENT)
|
| 219 | public enum ReserveStrategy {
|
| 220 | RIGHT,
|
| 221 | BOTH;
|
| 222 | }
|
| 223 | }
|
| 224 |
|