Class Slider

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.Slider

public final class Slider extends Entry
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 on 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();

// Percent with a custom text
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();

Values

The value lives in the mod (like Toggle's): value reads it, onChange stores it. Values are doubles in [min, max]; with a step they are min + k * step. onChange gets every new value while the player drags, already snapped to the step and clamped to the range, and only when it differs from the current one. A value outside the range shows clamped.

Required: label, range, value, onChange. Can't be in a MenuScreen's footer.

Backends work with the knob's position from 0 to 1 (position(), setPosition(double)) and nudge(int) for arrow keys, so they never do the range or step math.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Builds a Slider; from builder(String).
  • Method Summary

    Modifier and Type
    Method
    Description
    <R> R
    accept(EntryVisitor<R> visitor)
    Calls the EntryVisitor method for this entry's type (visitButton for a Button, …) and returns its result.
    Starts building a slider.
    The text before ": value", from the mod's supplier.
    double
    max()
    The largest value.
    double
    min()
    The smallest value.
    void
    nudge(int direction)
    One arrow key press: moves the value by one step, or by 1% of the range on a slider without a step.
    double
    The knob's position for the current value: 0 at min, 1 at max.
    void
    set(double newValue)
    Sets the value, snapped and clamped; onChange runs only if that changes it.
    void
    setPosition(double position)
    Moves the knob to position, as dragging or clicking the slider does: sets the value at that point of the range, snapped.
    double
    The step between values: they are min + k * step; 0 allows any value in the range.
    double
    The mod's current value, snapped to the step and clamped to the range (min if it is NaN).
    The current value as the slider shows it after "label: ", from the builder's valueText.

    Methods inherited from class Entry

    enabled, id, tooltip, visible

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static Slider.Builder builder(String id)
      Starts building a slider.
      Parameters:
      id - the slider's id, unique within its screen
      Returns:
      a new builder
      Throws:
      IllegalArgumentException - if id is invalid
    • label

      public Text label()
      The text before ": value", from the mod's supplier.
      Returns:
      the label text
    • min

      public double min()
      The smallest value.
      Returns:
      the lower end of the range, inclusive
    • max

      public double max()
      The largest value.
      Returns:
      the upper end of the range, inclusive
    • step

      public double step()
      The step between values: they are min + k * step; 0 allows any value in the range.
      Returns:
      the step between values, or 0 if any value in the range is allowed
    • value

      public double value()
      The mod's current value, snapped to the step and clamped to the range (min if it is NaN).
      Returns:
      the current value, snapped to the step and clamped to the range; min if the mod's value is NaN
    • valueText

      public Text valueText()
      The current value as the slider shows it after "label: ", from the builder's valueText.
      Returns:
      the value as shown after the label
    • position

      public double position()
      The knob's position for the current value: 0 at min, 1 at max.
      Returns:
      the knob's position, from 0 at min to 1 at max
    • setPosition

      public void setPosition(double position)
      Moves the knob to position, as dragging or clicking the slider does: sets the value at that point of the range, snapped. NaN is ignored.
      Parameters:
      position - 0 for min to 1 for max; clamped
    • set

      public void set(double newValue)
      Sets the value, snapped and clamped; onChange runs only if that changes it. NaN is ignored.
      Parameters:
      newValue - the new value
    • nudge

      public void nudge(int direction)
      One arrow key press: moves the value by one step, or by 1% of the range on a slider without a step.
      Parameters:
      direction - negative (left/down) to decrease, positive (right/up) to increase
    • accept

      public <R> R accept(EntryVisitor<R> visitor)
      Description copied from class: Entry
      Calls the EntryVisitor method for this entry's type (visitButton for a Button, …) and returns its result. A Section visits as its header, a NumberField as its TextInput.
      Specified by:
      accept in class Entry
      Type Parameters:
      R - the visitor's result type
      Parameters:
      visitor - the visitor, usually a backend's widget factory
      Returns:
      what the visitor returned