184 lines
5.8 KiB
Java
184 lines
5.8 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* org.jspecify.annotations.Nullable
|
|
*/
|
|
package net.minecraft.client.gui.components;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.function.Consumer;
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.client.gui.GuiGraphics;
|
|
import net.minecraft.client.gui.components.AbstractContainerWidget;
|
|
import net.minecraft.client.gui.components.AbstractWidget;
|
|
import net.minecraft.client.gui.components.events.GuiEventListener;
|
|
import net.minecraft.client.gui.layouts.Layout;
|
|
import net.minecraft.client.gui.layouts.LayoutElement;
|
|
import net.minecraft.client.gui.narration.NarratableEntry;
|
|
import net.minecraft.client.gui.narration.NarrationElementOutput;
|
|
import net.minecraft.client.gui.navigation.ScreenDirection;
|
|
import net.minecraft.client.gui.navigation.ScreenRectangle;
|
|
import net.minecraft.network.chat.CommonComponents;
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public class ScrollableLayout
|
|
implements Layout {
|
|
private static final int SCROLLBAR_SPACING = 4;
|
|
private static final int SCROLLBAR_RESERVE = 10;
|
|
private final Layout content;
|
|
private final Container container;
|
|
private int minWidth;
|
|
private int maxHeight;
|
|
|
|
public ScrollableLayout(Minecraft minecraft, Layout content, int maxHeight) {
|
|
this.content = content;
|
|
this.container = new Container(minecraft, 0, maxHeight);
|
|
}
|
|
|
|
public void setMinWidth(int minWidth) {
|
|
this.minWidth = minWidth;
|
|
this.container.setWidth(Math.max(this.content.getWidth(), minWidth));
|
|
}
|
|
|
|
public void setMaxHeight(int maxHeight) {
|
|
this.maxHeight = maxHeight;
|
|
this.container.setHeight(Math.min(this.content.getHeight(), maxHeight));
|
|
this.container.refreshScrollAmount();
|
|
}
|
|
|
|
@Override
|
|
public void arrangeElements() {
|
|
this.content.arrangeElements();
|
|
int contentWidth = this.content.getWidth();
|
|
this.container.setWidth(Math.max(contentWidth + 20, this.minWidth));
|
|
this.container.setHeight(Math.min(this.content.getHeight(), this.maxHeight));
|
|
this.container.refreshScrollAmount();
|
|
}
|
|
|
|
@Override
|
|
public void visitChildren(Consumer<LayoutElement> layoutElementVisitor) {
|
|
layoutElementVisitor.accept(this.container);
|
|
}
|
|
|
|
@Override
|
|
public void setX(int x) {
|
|
this.container.setX(x);
|
|
}
|
|
|
|
@Override
|
|
public void setY(int y) {
|
|
this.container.setY(y);
|
|
}
|
|
|
|
@Override
|
|
public int getX() {
|
|
return this.container.getX();
|
|
}
|
|
|
|
@Override
|
|
public int getY() {
|
|
return this.container.getY();
|
|
}
|
|
|
|
@Override
|
|
public int getWidth() {
|
|
return this.container.getWidth();
|
|
}
|
|
|
|
@Override
|
|
public int getHeight() {
|
|
return this.container.getHeight();
|
|
}
|
|
|
|
private class Container
|
|
extends AbstractContainerWidget {
|
|
private final Minecraft minecraft;
|
|
private final List<AbstractWidget> children;
|
|
|
|
public Container(Minecraft minecraft, int width, int height) {
|
|
super(0, 0, width, height, CommonComponents.EMPTY);
|
|
this.children = new ArrayList<AbstractWidget>();
|
|
this.minecraft = minecraft;
|
|
ScrollableLayout.this.content.visitWidgets(this.children::add);
|
|
}
|
|
|
|
@Override
|
|
protected int contentHeight() {
|
|
return ScrollableLayout.this.content.getHeight();
|
|
}
|
|
|
|
@Override
|
|
protected double scrollRate() {
|
|
return 10.0;
|
|
}
|
|
|
|
@Override
|
|
protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float a) {
|
|
graphics.enableScissor(this.getX(), this.getY(), this.getX() + this.width, this.getY() + this.height);
|
|
for (AbstractWidget child : this.children) {
|
|
child.render(graphics, mouseX, mouseY, a);
|
|
}
|
|
graphics.disableScissor();
|
|
this.renderScrollbar(graphics, mouseX, mouseY);
|
|
}
|
|
|
|
@Override
|
|
protected void updateWidgetNarration(NarrationElementOutput output) {
|
|
}
|
|
|
|
@Override
|
|
public ScreenRectangle getBorderForArrowNavigation(ScreenDirection opposite) {
|
|
return new ScreenRectangle(this.getX(), this.getY(), this.width, this.contentHeight());
|
|
}
|
|
|
|
@Override
|
|
public void setFocused(@Nullable GuiEventListener focused) {
|
|
super.setFocused(focused);
|
|
if (focused == null || !this.minecraft.getLastInputType().isKeyboard()) {
|
|
return;
|
|
}
|
|
ScreenRectangle area = this.getRectangle();
|
|
ScreenRectangle focusedRect = focused.getRectangle();
|
|
int topDelta = focusedRect.top() - area.top();
|
|
int bottomDelta = focusedRect.bottom() - area.bottom();
|
|
if (topDelta < 0) {
|
|
this.setScrollAmount(this.scrollAmount() + (double)topDelta - 14.0);
|
|
} else if (bottomDelta > 0) {
|
|
this.setScrollAmount(this.scrollAmount() + (double)bottomDelta + 14.0);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setX(int x) {
|
|
super.setX(x);
|
|
ScrollableLayout.this.content.setX(x + 10);
|
|
}
|
|
|
|
@Override
|
|
public void setY(int y) {
|
|
super.setY(y);
|
|
ScrollableLayout.this.content.setY(y - (int)this.scrollAmount());
|
|
}
|
|
|
|
@Override
|
|
public void setScrollAmount(double scrollAmount) {
|
|
super.setScrollAmount(scrollAmount);
|
|
ScrollableLayout.this.content.setY(this.getRectangle().top() - (int)this.scrollAmount());
|
|
}
|
|
|
|
@Override
|
|
public List<? extends GuiEventListener> children() {
|
|
return this.children;
|
|
}
|
|
|
|
@Override
|
|
public Collection<? extends NarratableEntry> getNarratables() {
|
|
return this.children;
|
|
}
|
|
}
|
|
}
|
|
|