Slider

Slider is a vanilla slider over a range of numbers with an optional step and custom value text, like the game's FOV and volume sliders.

Last updated

On this page

A vanilla slider over a range of numbers, showing "label: value" on the slider itself, like vanilla's FOV or volume sliders. The player drags the knob, clicks the track, or uses the arrow keys while it is focused.

// Whole numbers: step 1, cast in onChange
Slider.builder("renderDistance")
        .label(Text.translatable("mymod.distance", "Distance"))
        .range(2, 32).step(1)
        .value(() -> config.distance)
        .onChange(v -> config.distance = (int) v)
        .build();

// A fraction shown as a percentage
Slider.builder("opacity")
        .label(Text.translatable("mymod.opacity", "Opacity"))
        .range(0, 1).step(0.05)
        .value(() -> config.opacity)
        .valueText(v -> Text.literal(Math.round(v * 100) + "%"))
        .onChange(v -> config.opacity = v)
        .build();

Builder#

Method Required Default Notes
label(Supplier<Text>) / label(Text) yes the text before ": value"
range(double min, double max) yes both ends inclusive; min must be less than max, both finite
value(DoubleSupplier) yes your current value; re-read on every refresh and during drags
onChange(DoubleConsumer) yes stores a new value; main thread
step(double) no 0: any value the distance between values: 1 for whole numbers, 0.1 for one decimal. At most max - min.
valueText(DoubleFunction<Text>) no the number how the value is shown after "label: "
enabled, tooltip, visible no options every widget has

build() throws if a required field is missing, the range is empty or infinite, or the step is negative or larger than the range.

Behaviour#

Test it#

host.slide("renderDistance", 12);      // snapped and clamped as a drag would be

Rendered as [slider renderDistance] Distance: 12.

See also#