| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.function.Supplier;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.input.InputWithModifiers;
|
| 10 | import net.minecraft.client.input.KeyEvent;
|
| 11 | import net.minecraft.client.input.MouseButtonEvent;
|
| 12 | import net.minecraft.client.renderer.RenderPipelines;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.util.ARGB;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public abstract class AbstractButton extends AbstractWidget.WithInactiveMessage {
|
| 20 | protected static final int TEXT_MARGIN = 2;
|
| 21 | private static final WidgetSprites SPRITES = new WidgetSprites(
|
| 22 | Identifier.withDefaultNamespace("widget/button"),
|
| 23 | Identifier.withDefaultNamespace("widget/button_disabled"),
|
| 24 | Identifier.withDefaultNamespace("widget/button_highlighted")
|
| 25 | );
|
| 26 | private @Nullable Supplier<Boolean> overrideRenderHighlightedSprite;
|
| 27 |
|
| 28 | public AbstractButton(final int x, final int y, final int width, final int height, final Component message) {
|
| 29 | super(x, y, width, height, message);
|
| 30 | }
|
| 31 |
|
| 32 | public abstract void onPress(InputWithModifiers input);
|
| 33 |
|
| 34 | @Override
|
| 35 | protected final void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 36 | this.extractContents(graphics, mouseX, mouseY, a);
|
| 37 | this.handleCursor(graphics);
|
| 38 | }
|
| 39 |
|
| 40 | protected abstract void extractContents(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a);
|
| 41 |
|
| 42 | protected void extractDefaultLabel(final ActiveTextCollector output) {
|
| 43 | this.extractScrollingStringOverContents(output, this.getMessage(), 2);
|
| 44 | }
|
| 45 |
|
| 46 | protected final void extractDefaultSprite(final GuiGraphicsExtractor graphics) {
|
| 47 | graphics.blitSprite(
|
| 48 | RenderPipelines.GUI_TEXTURED,
|
| 49 | SPRITES.get(this.active, this.overrideRenderHighlightedSprite != null ? this.overrideRenderHighlightedSprite.get() : this.isHoveredOrFocused()),
|
| 50 | this.getX(),
|
| 51 | this.getY(),
|
| 52 | this.getWidth(),
|
| 53 | this.getHeight(),
|
| 54 | ARGB.white(this.alpha)
|
| 55 | );
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void onClick(final MouseButtonEvent event, final boolean doubleClick) {
|
| 60 | this.onPress(event);
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public boolean keyPressed(final KeyEvent event) {
|
| 65 | if (!this.isActive()) {
|
| 66 | return false;
|
| 67 | } else if (event.isSelection()) {
|
| 68 | this.playDownSound(Minecraft.getInstance().getSoundManager());
|
| 69 | this.onPress(event);
|
| 70 | return true;
|
| 71 | } else {
|
| 72 | return false;
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | public void setOverrideRenderHighlightedSprite(final Supplier<Boolean> overrideRenderHighlightedSprite) {
|
| 77 | this.overrideRenderHighlightedSprite = overrideRenderHighlightedSprite;
|
| 78 | }
|
| 79 | }
|
| 80 |
|