2025-11-24 22:52:51 +03:00

118 lines
4.3 KiB
Java

/*
* Decompiled with CFR 0.152.
*/
package net.minecraft.client.gui.components;
import com.mojang.blaze3d.platform.cursor.CursorTypes;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.util.Mth;
public abstract class AbstractScrollArea
extends AbstractWidget {
public static final int SCROLLBAR_WIDTH = 6;
private double scrollAmount;
private static final Identifier SCROLLER_SPRITE = Identifier.withDefaultNamespace("widget/scroller");
private static final Identifier SCROLLER_BACKGROUND_SPRITE = Identifier.withDefaultNamespace("widget/scroller_background");
private boolean scrolling;
public AbstractScrollArea(int x, int y, int width, int height, Component message) {
super(x, y, width, height, message);
}
@Override
public boolean mouseScrolled(double mx, double my, double scrollX, double scrollY) {
if (!this.visible) {
return false;
}
this.setScrollAmount(this.scrollAmount() - scrollY * this.scrollRate());
return true;
}
@Override
public boolean mouseDragged(MouseButtonEvent event, double dx, double dy) {
if (this.scrolling) {
if (event.y() < (double)this.getY()) {
this.setScrollAmount(0.0);
} else if (event.y() > (double)this.getBottom()) {
this.setScrollAmount(this.maxScrollAmount());
} else {
double max = Math.max(1, this.maxScrollAmount());
int barHeight = this.scrollerHeight();
double yDragScale = Math.max(1.0, max / (double)(this.height - barHeight));
this.setScrollAmount(this.scrollAmount() + dy * yDragScale);
}
return true;
}
return super.mouseDragged(event, dx, dy);
}
@Override
public void onRelease(MouseButtonEvent event) {
this.scrolling = false;
}
public double scrollAmount() {
return this.scrollAmount;
}
public void setScrollAmount(double scrollAmount) {
this.scrollAmount = Mth.clamp(scrollAmount, 0.0, (double)this.maxScrollAmount());
}
public boolean updateScrolling(MouseButtonEvent event) {
this.scrolling = this.scrollbarVisible() && this.isValidClickButton(event.buttonInfo()) && this.isOverScrollbar(event.x(), event.y());
return this.scrolling;
}
protected boolean isOverScrollbar(double x, double y) {
return x >= (double)this.scrollBarX() && x <= (double)(this.scrollBarX() + 6) && y >= (double)this.getY() && y < (double)this.getBottom();
}
public void refreshScrollAmount() {
this.setScrollAmount(this.scrollAmount);
}
public int maxScrollAmount() {
return Math.max(0, this.contentHeight() - this.height);
}
protected boolean scrollbarVisible() {
return this.maxScrollAmount() > 0;
}
protected int scrollerHeight() {
return Mth.clamp((int)((float)(this.height * this.height) / (float)this.contentHeight()), 32, this.height - 8);
}
protected int scrollBarX() {
return this.getRight() - 6;
}
protected int scrollBarY() {
return Math.max(this.getY(), (int)this.scrollAmount * (this.height - this.scrollerHeight()) / this.maxScrollAmount() + this.getY());
}
protected void renderScrollbar(GuiGraphics graphics, int mouseX, int mouseY) {
if (this.scrollbarVisible()) {
int scrollbarX = this.scrollBarX();
int scrollerHeight = this.scrollerHeight();
int scrollerY = this.scrollBarY();
graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SCROLLER_BACKGROUND_SPRITE, scrollbarX, this.getY(), 6, this.getHeight());
graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SCROLLER_SPRITE, scrollbarX, scrollerY, 6, scrollerHeight);
if (this.isOverScrollbar(mouseX, mouseY)) {
graphics.requestCursor(this.scrolling ? CursorTypes.RESIZE_NS : CursorTypes.POINTING_HAND);
}
}
}
protected abstract int contentHeight();
protected abstract double scrollRate();
}