| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.function.BooleanSupplier;
|
| 11 | import java.util.function.DoubleFunction;
|
| 12 | import java.util.function.Function;
|
| 13 | import java.util.function.IntFunction;
|
| 14 | import java.util.function.IntSupplier;
|
| 15 | import java.util.function.Supplier;
|
| 16 | import java.util.function.ToDoubleFunction;
|
| 17 | import java.util.function.ToIntFunction;
|
| 18 | import java.util.stream.IntStream;
|
| 19 | import net.fabricmc.api.EnvType;
|
| 20 | import net.fabricmc.api.Environment;
|
| 21 | import net.minecraft.client.OptionInstance.CycleableValueSet.ValueSetter;
|
| 22 | import net.minecraft.client.OptionInstance.IntRangeBase.1;
|
| 23 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 24 | import net.minecraft.client.gui.components.AbstractOptionSliderButton;
|
| 25 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 26 | import net.minecraft.client.gui.components.CycleButton;
|
| 27 | import net.minecraft.client.gui.components.ResettableOptionWidget;
|
| 28 | import net.minecraft.client.gui.components.Tooltip;
|
| 29 | import net.minecraft.client.input.KeyEvent;
|
| 30 | import net.minecraft.client.input.MouseButtonEvent;
|
| 31 | import net.minecraft.network.chat.CommonComponents;
|
| 32 | import net.minecraft.network.chat.Component;
|
| 33 | import net.minecraft.util.Mth;
|
| 34 | import net.minecraft.util.Util;
|
| 35 | import org.jspecify.annotations.Nullable;
|
| 36 | import org.slf4j.Logger;
|
| 37 |
|
| 38 | @Environment(EnvType.CLIENT)
|
| 39 | public final class OptionInstance<T> {
|
| 40 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 41 | public static final OptionInstance.Enum<Boolean> BOOLEAN_VALUES = new OptionInstance.Enum<>(ImmutableList.of(Boolean.TRUE, Boolean.FALSE), Codec.BOOL);
|
| 42 | public static final OptionInstance.CaptionBasedToString<Boolean> BOOLEAN_TO_STRING = (var0, b) -> b ? CommonComponents.OPTION_ON : CommonComponents.OPTION_OFF;
|
| 43 | public static final OptionInstance.ValueUpdateListener<Object> NO_ACTION = var0 -> {};
|
| 44 | private final OptionInstance.TooltipSupplier<T> tooltip;
|
| 45 | private final Function<T, Component> toString;
|
| 46 | private final OptionInstance.ValueSet<T> values;
|
| 47 | private final Codec<T> codec;
|
| 48 | private final T initialValue;
|
| 49 | private final OptionInstance.ValueUpdateListener<? super T> onValueUpdate;
|
| 50 | private final Component caption;
|
| 51 | private T value;
|
| 52 |
|
| 53 | public static OptionInstance<Boolean> createBoolean(
|
| 54 | final String captionId, final boolean initialValue, final OptionInstance.ValueUpdateListener<? super Boolean> onValueUpdate
|
| 55 | ) {
|
| 56 | return createBoolean(captionId, noTooltip(), initialValue, onValueUpdate);
|
| 57 | }
|
| 58 |
|
| 59 | public static OptionInstance<Boolean> createBoolean(final String captionId, final boolean initialValue) {
|
| 60 | return createBoolean(captionId, noTooltip(), initialValue, NO_ACTION);
|
| 61 | }
|
| 62 |
|
| 63 | public static OptionInstance<Boolean> createBoolean(final String captionId, final OptionInstance.TooltipSupplier<Boolean> tooltip, final boolean initialValue) {
|
| 64 | return createBoolean(captionId, tooltip, initialValue, NO_ACTION);
|
| 65 | }
|
| 66 |
|
| 67 | public static OptionInstance<Boolean> createBoolean(
|
| 68 | final String captionId,
|
| 69 | final OptionInstance.TooltipSupplier<Boolean> tooltip,
|
| 70 | final boolean initialValue,
|
| 71 | final OptionInstance.ValueUpdateListener<? super Boolean> onValueUpdate
|
| 72 | ) {
|
| 73 | return createBoolean(captionId, tooltip, BOOLEAN_TO_STRING, initialValue, onValueUpdate);
|
| 74 | }
|
| 75 |
|
| 76 | public static OptionInstance<Boolean> createBoolean(
|
| 77 | final String captionId,
|
| 78 | final OptionInstance.TooltipSupplier<Boolean> tooltip,
|
| 79 | final OptionInstance.CaptionBasedToString<Boolean> toString,
|
| 80 | final boolean initialValue,
|
| 81 | final OptionInstance.ValueUpdateListener<? super Boolean> onValueUpdate
|
| 82 | ) {
|
| 83 | return new OptionInstance<>(captionId, tooltip, toString, BOOLEAN_VALUES, initialValue, onValueUpdate);
|
| 84 | }
|
| 85 |
|
| 86 | public OptionInstance(
|
| 87 | final String captionId,
|
| 88 | final OptionInstance.TooltipSupplier<T> tooltip,
|
| 89 | final OptionInstance.CaptionBasedToString<T> toString,
|
| 90 | final OptionInstance.ValueSet<T> values,
|
| 91 | final T initialValue,
|
| 92 | final OptionInstance.ValueUpdateListener<? super T> onValueUpdate
|
| 93 | ) {
|
| 94 | this(captionId, tooltip, toString, values, values.codec(), initialValue, onValueUpdate);
|
| 95 | }
|
| 96 |
|
| 97 | public OptionInstance(
|
| 98 | final String captionId,
|
| 99 | final OptionInstance.TooltipSupplier<T> tooltip,
|
| 100 | final OptionInstance.CaptionBasedToString<T> toString,
|
| 101 | final OptionInstance.ValueSet<T> values,
|
| 102 | final Codec<T> codec,
|
| 103 | final T initialValue,
|
| 104 | final OptionInstance.ValueUpdateListener<? super T> onValueUpdate
|
| 105 | ) {
|
| 106 | this.caption = Component.translatable(captionId);
|
| 107 | this.tooltip = tooltip;
|
| 108 | this.toString = value -> toString.toString(this.caption, value);
|
| 109 | this.values = values;
|
| 110 | this.codec = codec;
|
| 111 | this.initialValue = initialValue;
|
| 112 | this.onValueUpdate = onValueUpdate;
|
| 113 | this.value = this.initialValue;
|
| 114 | }
|
| 115 |
|
| 116 | public static <T> OptionInstance.TooltipSupplier<T> noTooltip() {
|
| 117 | return var0 -> null;
|
| 118 | }
|
| 119 |
|
| 120 | public static <T> OptionInstance.TooltipSupplier<T> cachedConstantTooltip(final Component tooltipComponent) {
|
| 121 | return var1 -> Tooltip.create(tooltipComponent);
|
| 122 | }
|
| 123 |
|
| 124 | public AbstractWidget createButton(final Options options) {
|
| 125 | return this.createButton(options, 0, 0, 150);
|
| 126 | }
|
| 127 |
|
| 128 | public AbstractWidget createButton(final Options options, final int x, final int y, final int width) {
|
| 129 | return this.createButton(options, x, y, width, NO_ACTION);
|
| 130 | }
|
| 131 |
|
| 132 | public AbstractWidget createButton(
|
| 133 | final Options options, final int x, final int y, final int width, final OptionInstance.ValueUpdateListener<? super T> onValueChanged
|
| 134 | ) {
|
| 135 | return this.values.createButton(this.tooltip, options, x, y, width, onValueChanged).apply(this);
|
| 136 | }
|
| 137 |
|
| 138 | public T get() {
|
| 139 | return this.value;
|
| 140 | }
|
| 141 |
|
| 142 | public Codec<T> codec() {
|
| 143 | return this.codec;
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public String toString() {
|
| 148 | return this.caption.getString();
|
| 149 | }
|
| 150 |
|
| 151 | public void set(final T value) {
|
| 152 | T newValue = this.values.validateValue(value).orElseGet(() -> {
|
| 153 | LOGGER.error("Illegal option value {} for {}", value, this.caption.getString());
|
| 154 | return this.initialValue;
|
| 155 | });
|
| 156 | if (!Minecraft.getInstance().isRunning()) {
|
| 157 | this.value = newValue;
|
| 158 | } else {
|
| 159 | if (!Objects.equals(this.value, newValue)) {
|
| 160 | this.value = newValue;
|
| 161 | this.onValueUpdate.valueChanged(newValue);
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | public OptionInstance.ValueSet<T> values() {
|
| 167 | return this.values;
|
| 168 | }
|
| 169 |
|
| 170 | @Environment(EnvType.CLIENT)
|
| 171 | public record AltEnum<T>(List<T> values, List<T> altValues, BooleanSupplier altCondition, ValueSetter<T> valueSetter, Codec<T> codec)
|
| 172 | implements OptionInstance.CycleableValueSet<T> {
|
| 173 | @Override
|
| 174 | public CycleButton.ValueListSupplier<T> valueListSupplier() {
|
| 175 | return CycleButton.ValueListSupplier.create(this.altCondition, this.values, this.altValues);
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public Optional<T> validateValue(final T value) {
|
| 180 | return (this.altCondition.getAsBoolean() ? this.altValues : this.values).contains(value) ? Optional.of(value) : Optional.empty();
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @FunctionalInterface
|
| 185 | @Environment(EnvType.CLIENT)
|
| 186 | public interface CaptionBasedToString<T> {
|
| 187 | Component toString(Component caption, T value);
|
| 188 | }
|
| 189 |
|
| 190 | @Environment(EnvType.CLIENT)
|
| 191 | public record ClampingLazyMaxIntRange(int minInclusive, IntSupplier maxSupplier, int encodableMaxInclusive)
|
| 192 | implements OptionInstance.IntRangeBase,
|
| 193 | OptionInstance.SliderableOrCyclableValueSet<Integer> {
|
| 194 | public Optional<Integer> validateValue(final Integer value) {
|
| 195 | return Optional.of(Mth.clamp(value, this.minInclusive(), this.maxInclusive()));
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public int maxInclusive() {
|
| 200 | return this.maxSupplier.getAsInt();
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public Codec<Integer> codec() {
|
| 205 | return Codec.INT
|
| 206 | .validate(
|
| 207 | value -> {
|
| 208 | int maxExclusive = this.encodableMaxInclusive + 1;
|
| 209 | return value.compareTo(this.minInclusive) >= 0 && value.compareTo(maxExclusive) <= 0
|
| 210 | ? DataResult.success(value)
|
| 211 | : DataResult.error(() -> "Value " + value + " outside of range [" + this.minInclusive + ":" + maxExclusive + "]", value);
|
| 212 | }
|
| 213 | );
|
| 214 | }
|
| 215 |
|
| 216 | @Override
|
| 217 | public boolean createCycleButton() {
|
| 218 | return true;
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | public CycleButton.ValueListSupplier<Integer> valueListSupplier() {
|
| 223 | return CycleButton.ValueListSupplier.create(IntStream.range(this.minInclusive, this.maxInclusive() + 1).boxed().toList());
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | @Environment(EnvType.CLIENT)
|
| 228 | public interface CycleableValueSet<T> extends OptionInstance.ValueSet<T> {
|
| 229 | CycleButton.ValueListSupplier<T> valueListSupplier();
|
| 230 |
|
| 231 | default ValueSetter<T> valueSetter() {
|
| 232 | return OptionInstance::set;
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | default Function<OptionInstance<T>, AbstractWidget> createButton(
|
| 237 | final OptionInstance.TooltipSupplier<T> tooltip,
|
| 238 | final Options options,
|
| 239 | final int x,
|
| 240 | final int y,
|
| 241 | final int width,
|
| 242 | final OptionInstance.ValueUpdateListener<? super T> onValueChanged
|
| 243 | ) {
|
| 244 | return instance -> CycleButton.builder(instance.toString, instance::get)
|
| 245 | .withValues(this.valueListSupplier())
|
| 246 | .withTooltip(tooltip)
|
| 247 | .create(x, y, width, 20, instance.caption, (var4x, value) -> {
|
| 248 | this.valueSetter().set(instance, value);
|
| 249 | options.save();
|
| 250 | onValueChanged.valueChanged(value);
|
| 251 | });
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | @Environment(EnvType.CLIENT)
|
| 256 | public record Enum<T>(List<T> values, Codec<T> codec) implements OptionInstance.CycleableValueSet<T> {
|
| 257 | @Override
|
| 258 | public Optional<T> validateValue(final T value) {
|
| 259 | return this.values.contains(value) ? Optional.of(value) : Optional.empty();
|
| 260 | }
|
| 261 |
|
| 262 | @Override
|
| 263 | public CycleButton.ValueListSupplier<T> valueListSupplier() {
|
| 264 | return CycleButton.ValueListSupplier.create(this.values);
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | @Environment(EnvType.CLIENT)
|
| 269 | public record IntRange(int minInclusive, int maxInclusive, boolean applyValueImmediately) implements OptionInstance.IntRangeBase {
|
| 270 | public IntRange(final int minInclusive, final int maxInclusive) {
|
| 271 | this(minInclusive, maxInclusive, true);
|
| 272 | }
|
| 273 |
|
| 274 | public Optional<Integer> validateValue(final Integer value) {
|
| 275 | return value.compareTo(this.minInclusive()) >= 0 && value.compareTo(this.maxInclusive()) <= 0 ? Optional.of(value) : Optional.empty();
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public Codec<Integer> codec() {
|
| 280 | return Codec.intRange(this.minInclusive, this.maxInclusive + 1);
|
| 281 | }
|
| 282 | }
|
| 283 |
|
| 284 | @Environment(EnvType.CLIENT)
|
| 285 | public interface IntRangeBase extends OptionInstance.SliderableValueSet<Integer> {
|
| 286 | int minInclusive();
|
| 287 |
|
| 288 | int maxInclusive();
|
| 289 |
|
| 290 | default Optional<Integer> next(final Integer current) {
|
| 291 | return Optional.of(current + 1);
|
| 292 | }
|
| 293 |
|
| 294 | default Optional<Integer> previous(final Integer current) {
|
| 295 | return Optional.of(current - 1);
|
| 296 | }
|
| 297 |
|
| 298 | default double toSliderValue(final Integer value) {
|
| 299 | if (value == this.minInclusive()) {
|
| 300 | return 0.0;
|
| 301 | } else {
|
| 302 | return value == this.maxInclusive() ? 1.0 : Mth.map(value.intValue() + 0.5, this.minInclusive(), this.maxInclusive() + 1.0, 0.0, 1.0);
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | default Integer fromSliderValue(double slider) {
|
| 307 | if (slider >= 1.0) {
|
| 308 | slider = 0.99999F;
|
| 309 | }
|
| 310 |
|
| 311 | return Mth.floor(Mth.map(slider, 0.0, 1.0, this.minInclusive(), this.maxInclusive() + 1.0));
|
| 312 | }
|
| 313 |
|
| 314 | default <R> OptionInstance.SliderableValueSet<R> xmap(final IntFunction<? extends R> to, final ToIntFunction<? super R> from, final boolean discrete) {
|
| 315 | return new 1(this, from, to, discrete);
|
| 316 | }
|
| 317 | }
|
| 318 |
|
| 319 | @Environment(EnvType.CLIENT)
|
| 320 | public record LazyEnum<T>(Supplier<List<T>> values, Function<T, Optional<T>> validateValue, Codec<T> codec) implements OptionInstance.CycleableValueSet<T> {
|
| 321 | @Override
|
| 322 | public Optional<T> validateValue(final T value) {
|
| 323 | return this.validateValue.apply(value);
|
| 324 | }
|
| 325 |
|
| 326 | @Override
|
| 327 | public CycleButton.ValueListSupplier<T> valueListSupplier() {
|
| 328 | return CycleButton.ValueListSupplier.create(this.values.get());
|
| 329 | }
|
| 330 | }
|
| 331 |
|
| 332 | @Environment(EnvType.CLIENT)
|
| 333 | public static final class OptionInstanceSliderButton<N> extends AbstractOptionSliderButton implements ResettableOptionWidget {
|
| 334 | private final OptionInstance<N> instance;
|
| 335 | private final OptionInstance.SliderableValueSet<N> values;
|
| 336 | private final OptionInstance.TooltipSupplier<N> tooltipSupplier;
|
| 337 | private final OptionInstance.ValueUpdateListener<? super N> onValueChanged;
|
| 338 | private @Nullable Long delayedApplyAt;
|
| 339 | private final boolean applyValueImmediately;
|
| 340 |
|
| 341 | private OptionInstanceSliderButton(
|
| 342 | final Options options,
|
| 343 | final int x,
|
| 344 | final int y,
|
| 345 | final int width,
|
| 346 | final int height,
|
| 347 | final OptionInstance<N> instance,
|
| 348 | final OptionInstance.SliderableValueSet<N> values,
|
| 349 | final OptionInstance.TooltipSupplier<N> tooltipSupplier,
|
| 350 | final OptionInstance.ValueUpdateListener<? super N> onValueChanged,
|
| 351 | final boolean applyValueImmediately
|
| 352 | ) {
|
| 353 | super(options, x, y, width, height, values.toSliderValue(instance.get()));
|
| 354 | this.instance = instance;
|
| 355 | this.values = values;
|
| 356 | this.tooltipSupplier = tooltipSupplier;
|
| 357 | this.onValueChanged = onValueChanged;
|
| 358 | this.applyValueImmediately = applyValueImmediately;
|
| 359 | this.updateMessage();
|
| 360 | }
|
| 361 |
|
| 362 | @Override
|
| 363 | protected void updateMessage() {
|
| 364 | this.setMessage(this.instance.toString.apply(this.values.fromSliderValue(this.value)));
|
| 365 | this.setTooltip(this.tooltipSupplier.apply(this.values.fromSliderValue(this.value)));
|
| 366 | }
|
| 367 |
|
| 368 | @Override
|
| 369 | protected void applyValue() {
|
| 370 | if (this.applyValueImmediately) {
|
| 371 | this.applyUnsavedValue();
|
| 372 | } else {
|
| 373 | this.delayedApplyAt = Util.getMillis() + 600L;
|
| 374 | }
|
| 375 | }
|
| 376 |
|
| 377 | public void applyUnsavedValue() {
|
| 378 | N sliderValue = this.values.fromSliderValue(this.value);
|
| 379 | if (!Objects.equals(sliderValue, this.instance.get())) {
|
| 380 | this.instance.set(sliderValue);
|
| 381 | this.onValueChanged.valueChanged(this.instance.get());
|
| 382 | }
|
| 383 | }
|
| 384 |
|
| 385 | @Override
|
| 386 | public void resetValue() {
|
| 387 | if (this.value != this.values.toSliderValue(this.instance.get())) {
|
| 388 | this.value = this.values.toSliderValue(this.instance.get());
|
| 389 | this.delayedApplyAt = null;
|
| 390 | this.updateMessage();
|
| 391 | }
|
| 392 | }
|
| 393 |
|
| 394 | @Override
|
| 395 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 396 | super.extractWidgetRenderState(graphics, mouseX, mouseY, a);
|
| 397 | if (this.delayedApplyAt != null && Util.getMillis() >= this.delayedApplyAt) {
|
| 398 | this.delayedApplyAt = null;
|
| 399 | this.applyUnsavedValue();
|
| 400 | this.resetValue();
|
| 401 | }
|
| 402 | }
|
| 403 |
|
| 404 | @Override
|
| 405 | public void onRelease(final MouseButtonEvent event) {
|
| 406 | super.onRelease(event);
|
| 407 | if (this.applyValueImmediately) {
|
| 408 | this.resetValue();
|
| 409 | }
|
| 410 | }
|
| 411 |
|
| 412 | @Override
|
| 413 | public boolean keyPressed(final KeyEvent event) {
|
| 414 | if (event.isSelection()) {
|
| 415 | this.canChangeValue = !this.canChangeValue;
|
| 416 | return true;
|
| 417 | }
|
| 418 |
|
| 419 | if (this.canChangeValue) {
|
| 420 | boolean left = event.isLeft();
|
| 421 | boolean right = event.isRight();
|
| 422 | if (left) {
|
| 423 | Optional<N> previous = this.values.previous(this.values.fromSliderValue(this.value));
|
| 424 | if (previous.isPresent()) {
|
| 425 | this.setValue(this.values.toSliderValue(previous.get()));
|
| 426 | return true;
|
| 427 | }
|
| 428 | }
|
| 429 |
|
| 430 | if (right) {
|
| 431 | Optional<N> next = this.values.next(this.values.fromSliderValue(this.value));
|
| 432 | if (next.isPresent()) {
|
| 433 | this.setValue(this.values.toSliderValue(next.get()));
|
| 434 | return true;
|
| 435 | }
|
| 436 | }
|
| 437 |
|
| 438 | if (left || right) {
|
| 439 | float direction = left ? -1.0F : 1.0F;
|
| 440 | this.setValue(this.value + direction / (this.width - 8));
|
| 441 | return true;
|
| 442 | }
|
| 443 | }
|
| 444 |
|
| 445 | return false;
|
| 446 | }
|
| 447 | }
|
| 448 |
|
| 449 | @Environment(EnvType.CLIENT)
|
| 450 | public record SliderableEnum<T>(List<T> values, Codec<T> codec) implements OptionInstance.SliderableValueSet<T> {
|
| 451 | @Override
|
| 452 | public double toSliderValue(final T value) {
|
| 453 | if (value == this.values.getFirst()) {
|
| 454 | return 0.0;
|
| 455 | } else {
|
| 456 | return value == this.values.getLast() ? 1.0 : Mth.map(this.values.indexOf(value), 0.0, this.values.size() - 1, 0.0, 1.0);
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | @Override
|
| 461 | public Optional<T> next(final T current) {
|
| 462 | int currentIntex = this.values.indexOf(current);
|
| 463 | int nextIndex = Mth.clamp(currentIntex + 1, 0, this.values.size() - 1);
|
| 464 | return Optional.of(this.values.get(nextIndex));
|
| 465 | }
|
| 466 |
|
| 467 | @Override
|
| 468 | public Optional<T> previous(final T current) {
|
| 469 | int currentIntex = this.values.indexOf(current);
|
| 470 | int previousIndex = Mth.clamp(currentIntex - 1, 0, this.values.size() - 1);
|
| 471 | return Optional.of(this.values.get(previousIndex));
|
| 472 | }
|
| 473 |
|
| 474 | @Override
|
| 475 | public T fromSliderValue(double slider) {
|
| 476 | if (slider >= 1.0) {
|
| 477 | slider = 0.99999F;
|
| 478 | }
|
| 479 |
|
| 480 | int index = Mth.floor(Mth.map(slider, 0.0, 1.0, 0.0, this.values.size()));
|
| 481 | return this.values.get(Mth.clamp(index, 0, this.values.size() - 1));
|
| 482 | }
|
| 483 |
|
| 484 | @Override
|
| 485 | public Optional<T> validateValue(final T value) {
|
| 486 | int index = this.values.indexOf(value);
|
| 487 | return index > -1 ? Optional.of(value) : Optional.empty();
|
| 488 | }
|
| 489 | }
|
| 490 |
|
| 491 | @Environment(EnvType.CLIENT)
|
| 492 | public interface SliderableOrCyclableValueSet<T> extends OptionInstance.SliderableValueSet<T>, OptionInstance.CycleableValueSet<T> {
|
| 493 | boolean createCycleButton();
|
| 494 |
|
| 495 | @Override
|
| 496 | default Function<OptionInstance<T>, AbstractWidget> createButton(
|
| 497 | final OptionInstance.TooltipSupplier<T> tooltip,
|
| 498 | final Options options,
|
| 499 | final int x,
|
| 500 | final int y,
|
| 501 | final int width,
|
| 502 | final OptionInstance.ValueUpdateListener<? super T> onValueChanged
|
| 503 | ) {
|
| 504 | return this.createCycleButton()
|
| 505 | ? OptionInstance.CycleableValueSet.super.createButton(tooltip, options, x, y, width, onValueChanged)
|
| 506 | : OptionInstance.SliderableValueSet.super.createButton(tooltip, options, x, y, width, onValueChanged);
|
| 507 | }
|
| 508 | }
|
| 509 |
|
| 510 | @Environment(EnvType.CLIENT)
|
| 511 | public interface SliderableValueSet<T> extends OptionInstance.ValueSet<T> {
|
| 512 | double toSliderValue(final T value);
|
| 513 |
|
| 514 | default Optional<T> next(final T current) {
|
| 515 | return Optional.empty();
|
| 516 | }
|
| 517 |
|
| 518 | default Optional<T> previous(final T current) {
|
| 519 | return Optional.empty();
|
| 520 | }
|
| 521 |
|
| 522 | T fromSliderValue(final double slider);
|
| 523 |
|
| 524 | default boolean applyValueImmediately() {
|
| 525 | return true;
|
| 526 | }
|
| 527 |
|
| 528 | @Override
|
| 529 | default Function<OptionInstance<T>, AbstractWidget> createButton(
|
| 530 | final OptionInstance.TooltipSupplier<T> tooltip,
|
| 531 | final Options options,
|
| 532 | final int x,
|
| 533 | final int y,
|
| 534 | final int width,
|
| 535 | final OptionInstance.ValueUpdateListener<? super T> onValueChanged
|
| 536 | ) {
|
| 537 | return instance -> new OptionInstance.OptionInstanceSliderButton<>(
|
| 538 | options, x, y, width, 20, instance, this, tooltip, onValueChanged, this.applyValueImmediately()
|
| 539 | );
|
| 540 | }
|
| 541 | }
|
| 542 |
|
| 543 | @FunctionalInterface
|
| 544 | @Environment(EnvType.CLIENT)
|
| 545 | public interface TooltipSupplier<T> {
|
| 546 | @Nullable Tooltip apply(T value);
|
| 547 | }
|
| 548 |
|
| 549 | @Environment(EnvType.CLIENT)
|
| 550 | public enum UnitDouble implements OptionInstance.SliderableValueSet<Double> {
|
| 551 | INSTANCE;
|
| 552 |
|
| 553 | public Optional<Double> validateValue(final Double value) {
|
| 554 | return value >= 0.0 && value <= 1.0 ? Optional.of(value) : Optional.empty();
|
| 555 | }
|
| 556 |
|
| 557 | public double toSliderValue(final Double value) {
|
| 558 | return value;
|
| 559 | }
|
| 560 |
|
| 561 | public Double fromSliderValue(final double slider) {
|
| 562 | return slider;
|
| 563 | }
|
| 564 |
|
| 565 | public <R> OptionInstance.SliderableValueSet<R> xmap(final DoubleFunction<? extends R> to, final ToDoubleFunction<? super R> from) {
|
| 566 | return new net.minecraft.client.OptionInstance.UnitDouble.1(this, from, to);
|
| 567 | }
|
| 568 |
|
| 569 | @Override
|
| 570 | public Codec<Double> codec() {
|
| 571 | return Codec.withAlternative(Codec.doubleRange(0.0, 1.0), Codec.BOOL, b -> b ? 1.0 : 0.0);
|
| 572 | }
|
| 573 | }
|
| 574 |
|
| 575 | @Environment(EnvType.CLIENT)
|
| 576 | public interface ValueSet<T> {
|
| 577 | Function<OptionInstance<T>, AbstractWidget> createButton(
|
| 578 | final OptionInstance.TooltipSupplier<T> tooltip,
|
| 579 | Options options,
|
| 580 | final int x,
|
| 581 | final int y,
|
| 582 | final int width,
|
| 583 | final OptionInstance.ValueUpdateListener<? super T> onValueChanged
|
| 584 | );
|
| 585 |
|
| 586 | Optional<T> validateValue(final T value);
|
| 587 |
|
| 588 | Codec<T> codec();
|
| 589 | }
|
| 590 |
|
| 591 | @FunctionalInterface
|
| 592 | @Environment(EnvType.CLIENT)
|
| 593 | public interface ValueUpdateListener<T> {
|
| 594 | void valueChanged(T newValue);
|
| 595 | }
|
| 596 | }
|
| 597 |
|