| 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.Minecraft;
|
| 6 | import net.minecraft.client.OptionInstance;
|
| 7 | import net.minecraft.client.gui.Font;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 10 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 11 | import net.minecraft.client.input.InputWithModifiers;
|
| 12 | import net.minecraft.client.renderer.RenderPipelines;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.Style;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.util.ARGB;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class Checkbox extends AbstractButton {
|
| 21 | private static final Identifier CHECKBOX_SELECTED_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/checkbox_selected_highlighted");
|
| 22 | private static final Identifier CHECKBOX_SELECTED_SPRITE = Identifier.withDefaultNamespace("widget/checkbox_selected");
|
| 23 | private static final Identifier CHECKBOX_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/checkbox_highlighted");
|
| 24 | private static final Identifier CHECKBOX_SPRITE = Identifier.withDefaultNamespace("widget/checkbox");
|
| 25 | private static final int SPACING = 4;
|
| 26 | private static final int ROWS = 2;
|
| 27 | private static final int BOX_PADDING = 8;
|
| 28 | private boolean selected;
|
| 29 | private final Checkbox.OnValueChange onValueChange;
|
| 30 | private final MultiLineTextWidget textWidget;
|
| 31 |
|
| 32 | private Checkbox(
|
| 33 | final int x, final int y, final int maxWidth, final Component message, final Font font, final boolean selected, final Checkbox.OnValueChange onValueChange
|
| 34 | ) {
|
| 35 | super(x, y, 0, 0, message);
|
| 36 | this.textWidget = new MultiLineTextWidget(message, font);
|
| 37 | this.textWidget.setMaxRows(2);
|
| 38 | this.width = this.adjustWidth(maxWidth, font);
|
| 39 | this.height = this.getAdjustedHeight(font);
|
| 40 | this.selected = selected;
|
| 41 | this.onValueChange = onValueChange;
|
| 42 | }
|
| 43 |
|
| 44 | public int adjustWidth(final int maxWidth, final Font font) {
|
| 45 | this.width = this.getAdjustedWidth(maxWidth, this.getMessage(), font);
|
| 46 | this.textWidget.setMaxWidth(this.width);
|
| 47 | return this.width;
|
| 48 | }
|
| 49 |
|
| 50 | private int getAdjustedWidth(final int maxWidth, final Component message, final Font font) {
|
| 51 | return Math.min(getDefaultWidth(message, font), maxWidth);
|
| 52 | }
|
| 53 |
|
| 54 | private int getAdjustedHeight(final Font font) {
|
| 55 | return Math.max(getBoxSize(font), this.textWidget.getHeight());
|
| 56 | }
|
| 57 |
|
| 58 | private static int getDefaultWidth(final Component message, final Font font) {
|
| 59 | return getBoxSize(font) + 4 + font.width(message);
|
| 60 | }
|
| 61 |
|
| 62 | private boolean overflowsRowLimit(final Font font) {
|
| 63 | return font.getSplitter().splitLines(this.textWidget.getMessage(), this.width, Style.EMPTY).size() > 2;
|
| 64 | }
|
| 65 |
|
| 66 | public static Checkbox.Builder builder(final Component message, final Font font) {
|
| 67 | return new Checkbox.Builder(message, font);
|
| 68 | }
|
| 69 |
|
| 70 | public static int getBoxSize(final Font font) {
|
| 71 | return 9 + 8;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void onPress(final InputWithModifiers input) {
|
| 76 | this.selected = !this.selected;
|
| 77 | this.onValueChange.onValueChange(this, this.selected);
|
| 78 | }
|
| 79 |
|
| 80 | public boolean selected() {
|
| 81 | return this.selected;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 86 | output.add(NarratedElementType.TITLE, this.createNarrationMessage());
|
| 87 | if (this.active) {
|
| 88 | if (this.isFocused()) {
|
| 89 | output.add(
|
| 90 | NarratedElementType.USAGE, Component.translatable(this.selected ? "narration.checkbox.usage.focused.uncheck" : "narration.checkbox.usage.focused.check")
|
| 91 | );
|
| 92 | } else {
|
| 93 | output.add(
|
| 94 | NarratedElementType.USAGE, Component.translatable(this.selected ? "narration.checkbox.usage.hovered.uncheck" : "narration.checkbox.usage.hovered.check")
|
| 95 | );
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 102 | Minecraft minecraft = Minecraft.getInstance();
|
| 103 | Font font = minecraft.font;
|
| 104 | Identifier sprite;
|
| 105 | if (this.selected) {
|
| 106 | sprite = this.isFocused() ? CHECKBOX_SELECTED_HIGHLIGHTED_SPRITE : CHECKBOX_SELECTED_SPRITE;
|
| 107 | } else {
|
| 108 | sprite = this.isFocused() ? CHECKBOX_HIGHLIGHTED_SPRITE : CHECKBOX_SPRITE;
|
| 109 | }
|
| 110 |
|
| 111 | int boxSize = getBoxSize(font);
|
| 112 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, this.getX(), this.getY(), boxSize, boxSize, ARGB.white(this.alpha));
|
| 113 | int textX = this.getX() + boxSize + 4;
|
| 114 | int textY = this.getY() + boxSize / 2 - this.textWidget.getHeight() / 2;
|
| 115 | this.textWidget.setPosition(textX, textY);
|
| 116 | this.textWidget.visitLines(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.notClickable(this.isHovered())));
|
| 117 | }
|
| 118 |
|
| 119 | @Environment(EnvType.CLIENT)
|
| 120 | public static class Builder {
|
| 121 | private final Component message;
|
| 122 | private final Font font;
|
| 123 | private int maxWidth;
|
| 124 | private int x = 0;
|
| 125 | private int y = 0;
|
| 126 | private Checkbox.OnValueChange onValueChange = Checkbox.OnValueChange.NOP;
|
| 127 | private boolean selected = false;
|
| 128 | private @Nullable OptionInstance<Boolean> option = null;
|
| 129 | private @Nullable Tooltip tooltip = null;
|
| 130 |
|
| 131 | private Builder(final Component message, final Font font) {
|
| 132 | this.message = message;
|
| 133 | this.font = font;
|
| 134 | this.maxWidth = Checkbox.getDefaultWidth(message, font);
|
| 135 | }
|
| 136 |
|
| 137 | public Checkbox.Builder pos(final int x, final int y) {
|
| 138 | this.x = x;
|
| 139 | this.y = y;
|
| 140 | return this;
|
| 141 | }
|
| 142 |
|
| 143 | public Checkbox.Builder onValueChange(final Checkbox.OnValueChange onValueChange) {
|
| 144 | this.onValueChange = onValueChange;
|
| 145 | return this;
|
| 146 | }
|
| 147 |
|
| 148 | public Checkbox.Builder selected(final boolean selected) {
|
| 149 | this.selected = selected;
|
| 150 | this.option = null;
|
| 151 | return this;
|
| 152 | }
|
| 153 |
|
| 154 | public Checkbox.Builder selected(final OptionInstance<Boolean> option) {
|
| 155 | this.option = option;
|
| 156 | this.selected = option.get();
|
| 157 | return this;
|
| 158 | }
|
| 159 |
|
| 160 | public Checkbox.Builder tooltip(final Tooltip tooltip) {
|
| 161 | this.tooltip = tooltip;
|
| 162 | return this;
|
| 163 | }
|
| 164 |
|
| 165 | public Checkbox.Builder maxWidth(final int maxWidth) {
|
| 166 | this.maxWidth = maxWidth;
|
| 167 | return this;
|
| 168 | }
|
| 169 |
|
| 170 | public Checkbox build() {
|
| 171 | Checkbox.OnValueChange onChange = this.option == null ? this.onValueChange : (checkbox, value) -> {
|
| 172 | this.option.set(value);
|
| 173 | this.onValueChange.onValueChange(checkbox, value);
|
| 174 | };
|
| 175 | Checkbox box = new Checkbox(this.x, this.y, this.maxWidth, this.message, this.font, this.selected, onChange);
|
| 176 | if (box.overflowsRowLimit(this.font)) {
|
| 177 | box.setTooltip(this.tooltip);
|
| 178 | }
|
| 179 |
|
| 180 | return box;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Environment(EnvType.CLIENT)
|
| 185 | public interface OnValueChange {
|
| 186 | Checkbox.OnValueChange NOP = (checkbox, value) -> {};
|
| 187 |
|
| 188 | void onValueChange(Checkbox checkbox, boolean value);
|
| 189 | }
|
| 190 | }
|
| 191 |
|