Class Slider
java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.Slider
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 (likeToggle'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 -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(EntryVisitor<R> visitor) Calls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result.static Slider.BuilderStarts building a slider.label()The text before ": value", from the mod's supplier.doublemax()The largest value.doublemin()The smallest value.voidnudge(int direction) One arrow key press: moves the value by one step, or by 1% of the range on a slider without a step.doubleposition()The knob's position for the current value: 0 atmin, 1 atmax.voidset(double newValue) Sets the value, snapped and clamped;onChangeruns only if that changes it.voidsetPosition(double position) Moves the knob toposition, as dragging or clicking the slider does: sets the value at that point of the range, snapped.doublestep()The step between values: they aremin + k * step; 0 allows any value in the range.doublevalue()The mod's current value, snapped to the step and clamped to the range (minif it is NaN).The current value as the slider shows it after "label: ", from the builder'svalueText.
-
Method Details
-
builder
Starts building a slider.- Parameters:
id- the slider's id, unique within its screen- Returns:
- a new builder
- Throws:
IllegalArgumentException- ifidis invalid
-
label
-
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 aremin + 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 (minif it is NaN).- Returns:
- the current value, snapped to the step and clamped to the range;
minif the mod's value is NaN
-
valueText
The current value as the slider shows it after "label: ", from the builder'svalueText.- Returns:
- the value as shown after the label
-
position
public double position()The knob's position for the current value: 0 atmin, 1 atmax.- Returns:
- the knob's position, from 0 at
minto 1 atmax
-
setPosition
public void setPosition(double position) Moves the knob toposition, as dragging or clicking the slider does: sets the value at that point of the range, snapped. NaN is ignored.- Parameters:
position- 0 forminto 1 formax; clamped
-
set
public void set(double newValue) Sets the value, snapped and clamped;onChangeruns 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
Description copied from class:EntryCalls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result. ASectionvisits as its header, aNumberFieldas itsTextInput.
-