| 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.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 8 | import net.minecraft.client.input.InputWithModifiers;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.network.chat.MutableComponent;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public abstract class Button extends AbstractButton {
|
| 15 | public static final int SMALL_WIDTH = 120;
|
| 16 | public static final int DEFAULT_WIDTH = 150;
|
| 17 | public static final int BIG_WIDTH = 200;
|
| 18 | public static final int DEFAULT_HEIGHT = 20;
|
| 19 | public static final int DEFAULT_SPACING = 8;
|
| 20 | protected static final Button.CreateNarration DEFAULT_NARRATION = defaultNarrationSupplier -> defaultNarrationSupplier.get();
|
| 21 | protected final Button.OnPress onPress;
|
| 22 | protected final Button.CreateNarration createNarration;
|
| 23 |
|
| 24 | public static Button.Builder builder(final Component message, final Button.OnPress onPress) {
|
| 25 | return new Button.Builder(message, onPress);
|
| 26 | }
|
| 27 |
|
| 28 | protected Button(
|
| 29 | final int x,
|
| 30 | final int y,
|
| 31 | final int width,
|
| 32 | final int height,
|
| 33 | final Component message,
|
| 34 | final Button.OnPress onPress,
|
| 35 | final Button.CreateNarration createNarration
|
| 36 | ) {
|
| 37 | super(x, y, width, height, message);
|
| 38 | this.onPress = onPress;
|
| 39 | this.createNarration = createNarration;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public void onPress(final InputWithModifiers input) {
|
| 44 | this.onPress.onPress(this);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected MutableComponent createNarrationMessage() {
|
| 49 | return this.createNarration.createNarrationMessage(() -> super.createNarrationMessage());
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 54 | this.defaultButtonNarrationText(output);
|
| 55 | }
|
| 56 |
|
| 57 | @Environment(EnvType.CLIENT)
|
| 58 | public static class Builder {
|
| 59 | private final Component message;
|
| 60 | private final Button.OnPress onPress;
|
| 61 | private @Nullable Tooltip tooltip;
|
| 62 | private int x;
|
| 63 | private int y;
|
| 64 | private int width = 150;
|
| 65 | private int height = 20;
|
| 66 | private Button.CreateNarration createNarration = Button.DEFAULT_NARRATION;
|
| 67 |
|
| 68 | public Builder(final Component message, final Button.OnPress onPress) {
|
| 69 | this.message = message;
|
| 70 | this.onPress = onPress;
|
| 71 | }
|
| 72 |
|
| 73 | public Button.Builder pos(final int x, final int y) {
|
| 74 | this.x = x;
|
| 75 | this.y = y;
|
| 76 | return this;
|
| 77 | }
|
| 78 |
|
| 79 | public Button.Builder width(final int width) {
|
| 80 | this.width = width;
|
| 81 | return this;
|
| 82 | }
|
| 83 |
|
| 84 | public Button.Builder size(final int width, final int height) {
|
| 85 | this.width = width;
|
| 86 | this.height = height;
|
| 87 | return this;
|
| 88 | }
|
| 89 |
|
| 90 | public Button.Builder bounds(final int x, final int y, final int width, final int height) {
|
| 91 | return this.pos(x, y).size(width, height);
|
| 92 | }
|
| 93 |
|
| 94 | public Button.Builder tooltip(final @Nullable Tooltip tooltip) {
|
| 95 | this.tooltip = tooltip;
|
| 96 | return this;
|
| 97 | }
|
| 98 |
|
| 99 | public Button.Builder createNarration(final Button.CreateNarration createNarration) {
|
| 100 | this.createNarration = createNarration;
|
| 101 | return this;
|
| 102 | }
|
| 103 |
|
| 104 | public Button build() {
|
| 105 | Button button = new Button.Plain(this.x, this.y, this.width, this.height, this.message, this.onPress, this.createNarration);
|
| 106 | button.setTooltip(this.tooltip);
|
| 107 | return button;
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Environment(EnvType.CLIENT)
|
| 112 | public interface CreateNarration {
|
| 113 | MutableComponent createNarrationMessage(Supplier<MutableComponent> defaultNarrationSupplier);
|
| 114 | }
|
| 115 |
|
| 116 | @Environment(EnvType.CLIENT)
|
| 117 | public interface OnPress {
|
| 118 | void onPress(final Button button);
|
| 119 | }
|
| 120 |
|
| 121 | @Environment(EnvType.CLIENT)
|
| 122 | public static class Plain extends Button {
|
| 123 | protected Plain(
|
| 124 | final int x,
|
| 125 | final int y,
|
| 126 | final int width,
|
| 127 | final int height,
|
| 128 | final Component message,
|
| 129 | final Button.OnPress onPress,
|
| 130 | final Button.CreateNarration createNarration
|
| 131 | ) {
|
| 132 | super(x, y, width, height, message, onPress, createNarration);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 137 | this.extractDefaultSprite(graphics);
|
| 138 | this.extractDefaultLabel(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE));
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|