| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.input.MouseButtonEvent;
|
| 8 | import net.minecraft.client.renderer.RenderPipelines;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public abstract class AbstractScrollArea extends AbstractWidget {
|
| 16 | public static final int SCROLLBAR_WIDTH = 6;
|
| 17 | private static final int SCROLLBAR_MIN_HEIGHT = 32;
|
| 18 | private static final Identifier SCROLLER_SPRITE = Identifier.withDefaultNamespace("widget/scroller");
|
| 19 | private static final Identifier SCROLLER_BACKGROUND_SPRITE = Identifier.withDefaultNamespace("widget/scroller_background");
|
| 20 | private final AbstractScrollArea.ScrollbarSettings scrollbarSettings;
|
| 21 | private double scrollAmount;
|
| 22 | private boolean scrolling;
|
| 23 |
|
| 24 | public AbstractScrollArea(
|
| 25 | final int x, final int y, final int width, final int height, final Component message, final AbstractScrollArea.ScrollbarSettings scrollbarSettings
|
| 26 | ) {
|
| 27 | super(x, y, width, height, message);
|
| 28 | this.scrollbarSettings = scrollbarSettings;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public boolean mouseScrolled(final double mx, final double my, final double scrollX, final double scrollY) {
|
| 33 | if (!this.visible) {
|
| 34 | return false;
|
| 35 | }
|
| 36 |
|
| 37 | this.setScrollAmount(this.scrollAmount() - scrollY * this.scrollRate());
|
| 38 | return true;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
| 43 | if (this.scrolling) {
|
| 44 | if (event.y() < this.getY()) {
|
| 45 | this.setScrollAmount(0.0);
|
| 46 | } else if (event.y() > this.getBottom()) {
|
| 47 | this.setScrollAmount(this.maxScrollAmount());
|
| 48 | } else {
|
| 49 | double max = Math.max(1, this.maxScrollAmount());
|
| 50 | int barHeight = this.scrollerHeight();
|
| 51 | double yDragScale = Math.max(1.0, max / (this.height - barHeight));
|
| 52 | this.setScrollAmount(this.scrollAmount() + dy * yDragScale);
|
| 53 | }
|
| 54 |
|
| 55 | return true;
|
| 56 | } else {
|
| 57 | return super.mouseDragged(event, dx, dy);
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void onRelease(final MouseButtonEvent event) {
|
| 63 | this.scrolling = false;
|
| 64 | }
|
| 65 |
|
| 66 | public double scrollAmount() {
|
| 67 | return this.scrollAmount;
|
| 68 | }
|
| 69 |
|
| 70 | public void setScrollAmount(final double scrollAmount) {
|
| 71 | this.scrollAmount = Mth.clamp(scrollAmount, 0.0, this.maxScrollAmount());
|
| 72 | }
|
| 73 |
|
| 74 | public boolean updateScrolling(final MouseButtonEvent event) {
|
| 75 | this.scrolling = this.scrollable() && this.isValidClickButton(event.buttonInfo()) && this.isOverScrollbar(event.x(), event.y());
|
| 76 | return this.scrolling;
|
| 77 | }
|
| 78 |
|
| 79 | protected boolean isOverScrollbar(final double x, final double y) {
|
| 80 | return x >= this.scrollBarX() && x <= this.scrollBarX() + this.scrollbarWidth() && y >= this.getY() && y < this.getBottom();
|
| 81 | }
|
| 82 |
|
| 83 | public void refreshScrollAmount() {
|
| 84 | this.setScrollAmount(this.scrollAmount);
|
| 85 | }
|
| 86 |
|
| 87 | public int maxScrollAmount() {
|
| 88 | return Math.max(0, this.contentHeight() - this.height);
|
| 89 | }
|
| 90 |
|
| 91 | protected boolean scrollable() {
|
| 92 | return this.maxScrollAmount() > 0;
|
| 93 | }
|
| 94 |
|
| 95 | public int scrollbarWidth() {
|
| 96 | return this.scrollbarSettings.scrollbarWidth();
|
| 97 | }
|
| 98 |
|
| 99 | protected int scrollerHeight() {
|
| 100 | return Mth.clamp((int)((float)(this.height * this.height) / this.contentHeight()), 32, this.height - 8);
|
| 101 | }
|
| 102 |
|
| 103 | protected int scrollBarX() {
|
| 104 | return this.getRight() - this.scrollbarWidth();
|
| 105 | }
|
| 106 |
|
| 107 | public int scrollBarY() {
|
| 108 | return this.maxScrollAmount() == 0
|
| 109 | ? this.getY()
|
| 110 | : Math.max(this.getY(), (int)this.scrollAmount * (this.height - this.scrollerHeight()) / this.maxScrollAmount() + this.getY());
|
| 111 | }
|
| 112 |
|
| 113 | protected void extractScrollbar(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY) {
|
| 114 | int scrollbarX = this.scrollBarX();
|
| 115 | int scrollerHeight = this.scrollerHeight();
|
| 116 | int scrollerY = this.scrollBarY();
|
| 117 | if (!this.scrollable() && this.scrollbarSettings.disabledScrollerSprite() != null) {
|
| 118 | graphics.blitSprite(
|
| 119 | RenderPipelines.GUI_TEXTURED, this.scrollbarSettings.backgroundSprite(), scrollbarX, this.getY(), this.scrollbarWidth(), this.getHeight()
|
| 120 | );
|
| 121 | graphics.blitSprite(
|
| 122 | RenderPipelines.GUI_TEXTURED, this.scrollbarSettings.disabledScrollerSprite(), scrollbarX, this.getY(), this.scrollbarWidth(), scrollerHeight
|
| 123 | );
|
| 124 | if (this.isOverScrollbar(mouseX, mouseY)) {
|
| 125 | graphics.requestCursor(CursorTypes.NOT_ALLOWED);
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | if (this.scrollable()) {
|
| 130 | graphics.blitSprite(
|
| 131 | RenderPipelines.GUI_TEXTURED, this.scrollbarSettings.backgroundSprite(), scrollbarX, this.getY(), this.scrollbarWidth(), this.getHeight()
|
| 132 | );
|
| 133 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.scrollbarSettings.scrollerSprite(), scrollbarX, scrollerY, this.scrollbarWidth(), scrollerHeight);
|
| 134 | if (this.isOverScrollbar(mouseX, mouseY)) {
|
| 135 | graphics.requestCursor(this.scrolling ? CursorTypes.RESIZE_NS : CursorTypes.POINTING_HAND);
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | protected abstract int contentHeight();
|
| 141 |
|
| 142 | protected double scrollRate() {
|
| 143 | return this.scrollbarSettings.scrollRate();
|
| 144 | }
|
| 145 |
|
| 146 | public static AbstractScrollArea.ScrollbarSettings defaultSettings(final int scrollRate) {
|
| 147 | return new AbstractScrollArea.ScrollbarSettings(SCROLLER_SPRITE, null, SCROLLER_BACKGROUND_SPRITE, 6, 32, scrollRate, true);
|
| 148 | }
|
| 149 |
|
| 150 | @Environment(EnvType.CLIENT)
|
| 151 | public record ScrollbarSettings(
|
| 152 | Identifier scrollerSprite,
|
| 153 | @Nullable Identifier disabledScrollerSprite,
|
| 154 | Identifier backgroundSprite,
|
| 155 | int scrollbarWidth,
|
| 156 | int scrollbarMinHeight,
|
| 157 | int scrollRate,
|
| 158 | boolean resizingScrollbar
|
| 159 | ) {
|
| 160 | public static final AbstractScrollArea.ScrollbarSettings NO_SCROLL = AbstractScrollArea.defaultSettings(0);
|
| 161 | }
|
| 162 | }
|
| 163 |
|