| 1 | package net.minecraft.server.dialog.input;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.network.chat.ComponentSerialization;
|
| 10 | import net.minecraft.server.dialog.Dialog;
|
| 11 | import net.minecraft.util.ExtraCodecs;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 |
|
| 14 | public record NumberRangeInput(int width, Component label, String labelFormat, NumberRangeInput.RangeInfo rangeInfo) implements InputControl {
|
| 15 | public static final MapCodec<NumberRangeInput> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 16 | i -> i.group(
|
| 17 | Dialog.WIDTH_CODEC.optionalFieldOf("width", 200).forGetter(NumberRangeInput::width),
|
| 18 | ComponentSerialization.CODEC.fieldOf("label").forGetter(NumberRangeInput::label),
|
| 19 | Codec.STRING.optionalFieldOf("label_format", "options.generic_value").forGetter(NumberRangeInput::labelFormat),
|
| 20 | NumberRangeInput.RangeInfo.MAP_CODEC.forGetter(NumberRangeInput::rangeInfo)
|
| 21 | )
|
| 22 | .apply(i, NumberRangeInput::new)
|
| 23 | );
|
| 24 |
|
| 25 | @Override
|
| 26 | public MapCodec<NumberRangeInput> mapCodec() {
|
| 27 | return MAP_CODEC;
|
| 28 | }
|
| 29 |
|
| 30 | public Component computeLabel(final String value) {
|
| 31 | return Component.translatable(this.labelFormat, this.label, value);
|
| 32 | }
|
| 33 |
|
| 34 | public record RangeInfo(float start, float end, Optional<Float> initial, Optional<Float> step) {
|
| 35 | public static final MapCodec<NumberRangeInput.RangeInfo> MAP_CODEC = RecordCodecBuilder.<NumberRangeInput.RangeInfo>mapCodec(
|
| 36 | i -> i.group(
|
| 37 | Codec.FLOAT.fieldOf("start").forGetter(NumberRangeInput.RangeInfo::start),
|
| 38 | Codec.FLOAT.fieldOf("end").forGetter(NumberRangeInput.RangeInfo::end),
|
| 39 | Codec.FLOAT.optionalFieldOf("initial").forGetter(NumberRangeInput.RangeInfo::initial),
|
| 40 | ExtraCodecs.POSITIVE_FLOAT.optionalFieldOf("step").forGetter(NumberRangeInput.RangeInfo::step)
|
| 41 | )
|
| 42 | .apply(i, NumberRangeInput.RangeInfo::new)
|
| 43 | )
|
| 44 | .validate(range -> {
|
| 45 | if (range.initial.isPresent()) {
|
| 46 | double initial = range.initial.get().floatValue();
|
| 47 | double min = Math.min(range.start, range.end);
|
| 48 | double max = Math.max(range.start, range.end);
|
| 49 | if (initial < min || initial > max) {
|
| 50 | return DataResult.error(() -> "Initial value " + initial + " is outside of range [" + min + ", " + max + "]");
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | return DataResult.success(range);
|
| 55 | });
|
| 56 |
|
| 57 | public float computeScaledValue(final float sliderValue) {
|
| 58 | float valueInRange = Mth.lerp(sliderValue, this.start, this.end);
|
| 59 | if (this.step.isEmpty()) {
|
| 60 | return valueInRange;
|
| 61 | }
|
| 62 |
|
| 63 | float step = this.step.get();
|
| 64 | float initialValue = this.initialScaledValue();
|
| 65 | float deltaToInitial = valueInRange - initialValue;
|
| 66 | int stepsOutsideInitial = Math.round(deltaToInitial / step);
|
| 67 | float result = initialValue + stepsOutsideInitial * step;
|
| 68 | if (!this.isOutOfRange(result)) {
|
| 69 | return result;
|
| 70 | }
|
| 71 |
|
| 72 | int oneStepLess = stepsOutsideInitial - Mth.sign(stepsOutsideInitial);
|
| 73 | return initialValue + oneStepLess * step;
|
| 74 | }
|
| 75 |
|
| 76 | private boolean isOutOfRange(final float scaledValue) {
|
| 77 | float sliderPos = this.scaledValueToSlider(scaledValue);
|
| 78 | return sliderPos < 0.0 || sliderPos > 1.0;
|
| 79 | }
|
| 80 |
|
| 81 | private float initialScaledValue() {
|
| 82 | return this.initial.isPresent() ? this.initial.get() : (this.start + this.end) / 2.0F;
|
| 83 | }
|
| 84 |
|
| 85 | public float initialSliderValue() {
|
| 86 | float value = this.initialScaledValue();
|
| 87 | return this.scaledValueToSlider(value);
|
| 88 | }
|
| 89 |
|
| 90 | private float scaledValueToSlider(final float value) {
|
| 91 | return this.start == this.end ? 0.5F : Mth.inverseLerp(value, this.start, this.end);
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|