| 1 | package net.minecraft.client.gui.screens.dialog.input;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.function.Supplier;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.gui.Font;
|
| 11 | import net.minecraft.client.gui.components.Checkbox;
|
| 12 | import net.minecraft.client.gui.components.CycleButton;
|
| 13 | import net.minecraft.client.gui.components.EditBox;
|
| 14 | import net.minecraft.client.gui.components.MultiLineEditBox;
|
| 15 | import net.minecraft.client.gui.layouts.CommonLayouts;
|
| 16 | import net.minecraft.client.gui.layouts.LayoutElement;
|
| 17 | import net.minecraft.client.gui.screens.Screen;
|
| 18 | import net.minecraft.client.gui.screens.dialog.input.InputControlHandlers.BooleanHandler.1;
|
| 19 | import net.minecraft.client.gui.screens.dialog.input.InputControlHandlers.NumberRangeHandler.SliderImpl;
|
| 20 | import net.minecraft.network.chat.CommonComponents;
|
| 21 | import net.minecraft.server.dialog.action.Action.ValueGetter;
|
| 22 | import net.minecraft.server.dialog.input.BooleanInput;
|
| 23 | import net.minecraft.server.dialog.input.InputControl;
|
| 24 | import net.minecraft.server.dialog.input.NumberRangeInput;
|
| 25 | import net.minecraft.server.dialog.input.SingleOptionInput;
|
| 26 | import net.minecraft.server.dialog.input.TextInput;
|
| 27 | import net.minecraft.server.dialog.input.SingleOptionInput.Entry;
|
| 28 | import net.minecraft.server.dialog.input.TextInput.MultilineOptions;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 | import org.slf4j.Logger;
|
| 31 |
|
| 32 | @Environment(EnvType.CLIENT)
|
| 33 | public class InputControlHandlers {
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | private static final Map<MapCodec<? extends InputControl>, InputControlHandler<?>> HANDLERS = new HashMap<>();
|
| 36 |
|
| 37 | private static <T extends InputControl> void register(final MapCodec<T> type, final InputControlHandler<? super T> handler) {
|
| 38 | HANDLERS.put(type, handler);
|
| 39 | }
|
| 40 |
|
| 41 | private static <T extends InputControl> @Nullable InputControlHandler<T> get(final T inputControl) {
|
| 42 | return (InputControlHandler<T>)HANDLERS.get(inputControl.mapCodec());
|
| 43 | }
|
| 44 |
|
| 45 | public static <T extends InputControl> void createHandler(final T inputControl, final Screen screen, final InputControlHandler.Output outputConsumer) {
|
| 46 | InputControlHandler<T> handler = get(inputControl);
|
| 47 | if (handler == null) {
|
| 48 | LOGGER.warn("Unrecognized input control {}", inputControl);
|
| 49 | } else {
|
| 50 | handler.addControl(inputControl, screen, outputConsumer);
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | public static void bootstrap() {
|
| 55 | register(TextInput.MAP_CODEC, new InputControlHandlers.TextInputHandler());
|
| 56 | register(SingleOptionInput.MAP_CODEC, new InputControlHandlers.SingleOptionHandler());
|
| 57 | register(BooleanInput.MAP_CODEC, new InputControlHandlers.BooleanHandler());
|
| 58 | register(NumberRangeInput.MAP_CODEC, new InputControlHandlers.NumberRangeHandler());
|
| 59 | }
|
| 60 |
|
| 61 | @Environment(EnvType.CLIENT)
|
| 62 | private static class BooleanHandler implements InputControlHandler<BooleanInput> {
|
| 63 | public void addControl(final BooleanInput input, final Screen screen, final InputControlHandler.Output output) {
|
| 64 | Font font = screen.getFont();
|
| 65 | Checkbox control = Checkbox.builder(input.label(), font).selected(input.initial()).build();
|
| 66 | output.accept(control, new 1(this, control, input));
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | @Environment(EnvType.CLIENT)
|
| 71 | private static class NumberRangeHandler implements InputControlHandler<NumberRangeInput> {
|
| 72 | public void addControl(final NumberRangeInput input, final Screen screen, final InputControlHandler.Output output) {
|
| 73 | float initialValue = input.rangeInfo().initialSliderValue();
|
| 74 | SliderImpl control = new SliderImpl(input, initialValue);
|
| 75 | output.accept(control, new net.minecraft.client.gui.screens.dialog.input.InputControlHandlers.NumberRangeHandler.1(this, control));
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | @Environment(EnvType.CLIENT)
|
| 80 | private static class SingleOptionHandler implements InputControlHandler<SingleOptionInput> {
|
| 81 | public void addControl(final SingleOptionInput input, final Screen screen, final InputControlHandler.Output output) {
|
| 82 | Entry initial = input.initial().orElse(input.entries().getFirst());
|
| 83 | CycleButton.Builder<Entry> controlBuilder = CycleButton.builder(Entry::displayOrDefault, initial)
|
| 84 | .withValues(input.entries())
|
| 85 | .displayState(!input.labelVisible() ? CycleButton.DisplayState.VALUE : CycleButton.DisplayState.NAME_AND_VALUE);
|
| 86 | CycleButton<Entry> control = controlBuilder.create(0, 0, input.width(), 20, input.label());
|
| 87 | output.accept(control, ValueGetter.of(() -> control.getValue().id()));
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Environment(EnvType.CLIENT)
|
| 92 | private static class TextInputHandler implements InputControlHandler<TextInput> {
|
| 93 | public void addControl(final TextInput input, final Screen screen, final InputControlHandler.Output output) {
|
| 94 | Font font = screen.getFont();
|
| 95 | LayoutElement control;
|
| 96 | Supplier<String> getter;
|
| 97 | if (input.multiline().isPresent()) {
|
| 98 | MultilineOptions multiline = input.multiline().get();
|
| 99 | int computedHeight = multiline.height().orElseGet(() -> {
|
| 100 | int lineCountToFit = multiline.maxLines().orElse(4);
|
| 101 | return Math.min(9 * lineCountToFit + 8, 512);
|
| 102 | });
|
| 103 | MultiLineEditBox editBox = MultiLineEditBox.builder().build(font, input.width(), computedHeight, CommonComponents.EMPTY);
|
| 104 | editBox.setCharacterLimit(input.maxLength());
|
| 105 | multiline.maxLines().ifPresent(editBox::setLineLimit);
|
| 106 | editBox.setValue(input.initial());
|
| 107 | control = editBox;
|
| 108 | getter = editBox::getValue;
|
| 109 | } else {
|
| 110 | EditBox editBox = new EditBox(font, input.width(), 20, input.label());
|
| 111 | editBox.setMaxLength(input.maxLength());
|
| 112 | editBox.setValue(input.initial());
|
| 113 | control = editBox;
|
| 114 | getter = editBox::getValue;
|
| 115 | }
|
| 116 |
|
| 117 | LayoutElement wrappedControl = input.labelVisible() ? CommonLayouts.labeledElement(font, control, input.label()) : control;
|
| 118 | output.accept(wrappedControl, new net.minecraft.client.gui.screens.dialog.input.InputControlHandlers.TextInputHandler.1(this, getter));
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|