| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 6 | import net.minecraft.client.input.KeyEvent;
|
| 7 | import net.minecraft.client.input.MouseButtonEvent;
|
| 8 | import net.minecraft.client.renderer.RenderPipelines;
|
| 9 | import net.minecraft.client.sounds.SoundManager;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public abstract class AbstractTextAreaWidget extends AbstractScrollArea {
|
| 15 | private static final WidgetSprites BACKGROUND_SPRITES = new WidgetSprites(
|
| 16 | Identifier.withDefaultNamespace("widget/text_field"), Identifier.withDefaultNamespace("widget/text_field_highlighted")
|
| 17 | );
|
| 18 | private static final int INNER_PADDING = 4;
|
| 19 | public static final int DEFAULT_TOTAL_PADDING = 8;
|
| 20 | private boolean showBackground = true;
|
| 21 | private boolean showDecorations = true;
|
| 22 |
|
| 23 | public AbstractTextAreaWidget(
|
| 24 | final int x, final int y, final int width, final int height, final Component narration, final AbstractScrollArea.ScrollbarSettings scrollbarSettings
|
| 25 | ) {
|
| 26 | super(x, y, width, height, narration, scrollbarSettings);
|
| 27 | }
|
| 28 |
|
| 29 | public AbstractTextAreaWidget(
|
| 30 | final int x,
|
| 31 | final int y,
|
| 32 | final int width,
|
| 33 | final int height,
|
| 34 | final Component narration,
|
| 35 | final AbstractScrollArea.ScrollbarSettings scrollbarSettings,
|
| 36 | final boolean showBackground,
|
| 37 | final boolean showDecorations
|
| 38 | ) {
|
| 39 | this(x, y, width, height, narration, scrollbarSettings);
|
| 40 | this.showBackground = showBackground;
|
| 41 | this.showDecorations = showDecorations;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 46 | boolean scrolling = this.updateScrolling(event);
|
| 47 | return super.mouseClicked(event, doubleClick) || scrolling;
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public boolean keyPressed(final KeyEvent event) {
|
| 52 | boolean isUp = event.isUp();
|
| 53 | boolean isDown = event.isDown();
|
| 54 | if (isUp || isDown) {
|
| 55 | double previousScrollAmount = this.scrollAmount();
|
| 56 | this.setScrollAmount(this.scrollAmount() + (isUp ? -1 : 1) * this.scrollRate());
|
| 57 | if (previousScrollAmount != this.scrollAmount()) {
|
| 58 | return true;
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | return super.keyPressed(event);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 67 | if (this.visible) {
|
| 68 | if (this.showBackground) {
|
| 69 | this.extractBackground(graphics);
|
| 70 | }
|
| 71 |
|
| 72 | graphics.enableScissor(this.getX() + 1, this.getY() + 1, this.getX() + this.width - 1, this.getY() + this.height - 1);
|
| 73 | graphics.pose().pushMatrix();
|
| 74 | graphics.pose().translate(0.0F, (float)(-this.scrollAmount()));
|
| 75 | this.extractContents(graphics, mouseX, mouseY, a);
|
| 76 | graphics.pose().popMatrix();
|
| 77 | graphics.disableScissor();
|
| 78 | this.extractScrollbar(graphics, mouseX, mouseY);
|
| 79 | if (this.showDecorations) {
|
| 80 | this.extractDecorations(graphics);
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | protected void extractDecorations(final GuiGraphicsExtractor graphics) {
|
| 86 | }
|
| 87 |
|
| 88 | protected int innerPadding() {
|
| 89 | return 4;
|
| 90 | }
|
| 91 |
|
| 92 | protected int totalInnerPadding() {
|
| 93 | return this.innerPadding() * 2;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public boolean isMouseOver(final double mouseX, final double mouseY) {
|
| 98 | return this.active
|
| 99 | && this.visible
|
| 100 | && mouseX >= this.getX()
|
| 101 | && mouseY >= this.getY()
|
| 102 | && mouseX < this.getRight() + this.scrollbarWidth()
|
| 103 | && mouseY < this.getBottom();
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | protected int scrollBarX() {
|
| 108 | return this.getRight();
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | protected int contentHeight() {
|
| 113 | return this.getInnerHeight() + this.totalInnerPadding();
|
| 114 | }
|
| 115 |
|
| 116 | protected void extractBackground(final GuiGraphicsExtractor graphics) {
|
| 117 | this.extractBorder(graphics, this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
| 118 | }
|
| 119 |
|
| 120 | protected void extractBorder(final GuiGraphicsExtractor graphics, final int x, final int y, final int width, final int height) {
|
| 121 | Identifier sprite = BACKGROUND_SPRITES.get(this.isActive(), this.isFocused());
|
| 122 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, x, y, width, height);
|
| 123 | }
|
| 124 |
|
| 125 | protected boolean withinContentAreaTopBottom(final int top, final int bottom) {
|
| 126 | return bottom - this.scrollAmount() >= this.getY() && top - this.scrollAmount() <= this.getY() + this.height;
|
| 127 | }
|
| 128 |
|
| 129 | protected abstract int getInnerHeight();
|
| 130 |
|
| 131 | protected abstract void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a);
|
| 132 |
|
| 133 | protected int getInnerLeft() {
|
| 134 | return this.getX() + this.innerPadding();
|
| 135 | }
|
| 136 |
|
| 137 | protected int getInnerTop() {
|
| 138 | return this.getY() + this.innerPadding();
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | public void playDownSound(final SoundManager soundManager) {
|
| 143 | }
|
| 144 | }
|
| 145 |
|