Class NumberField<N extends Number>
java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.NumberField<N>
- Type Parameters:
N-IntegerforofInt(String),DoubleforofDouble(String)
A
TextInput for a number, optionally between a minimum and a maximum. Use it where the player types an exact
number (a port, a seed, a radius); for a value the player drags, use a Slider.
NumberField.ofInt("radius")
.label(Text.translatable("mymod.radius", "Radius (blocks)"))
.initial(config.radius)
.range(1, 256)
.onChange(r -> config.radius = r)
.build();
NumberField.ofDouble("scale")
.label(Text.translatable("mymod.scale", "Scale"))
.initial(config.scale)
.range(0.1, null) // no upper bound
.onChange(s -> config.scale = s)
.build();
Validation
The text is parsed withBigDecimal after trimming ('.' is the decimal separator; 1e3 is accepted).
Text that is not a number (UiTexts.NOT_A_NUMBER), not a whole number for ofInt(String)
(UiTexts.NOT_A_WHOLE_NUMBER), or out of range (UiTexts.AT_LEAST, UiTexts.AT_MOST) shows an
error below the field; the mod's own validator runs last, only on in-range numbers.
onChange gets only valid numbers. As with a text input, the text lives in the entry and survives refreshes.
Required: label, initial, onChange.
Backends draw it as its text input (its EntryVisitor.visitTextInput(TextInput) gets input(), with this
entry's id), so they need nothing for it.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(EntryVisitor<R> visitor) input()The text input backends draw; it has this entry's id, label, enabled state, tooltip and visibility.max()The maximum (inclusive), ornullfor none.min()The minimum (inclusive), ornullfor none.number()The number in the field, for an action that reads it directly.static NumberField.Builder<Double> Starts building a field for decimal numbers (double); '.' is the decimal separator in every language.static NumberField.Builder<Integer> Starts building a field for whole numbers (int, the fullintrange unless limited withNumberField.Builder.range(N, N)).
-
Method Details
-
ofInt
Starts building a field for whole numbers (int, the fullintrange unless limited withNumberField.Builder.range(N, N)). Decimal text such as2.5is rejected;2.0is accepted as 2.- Parameters:
id- the field's id, unique within its screen- Returns:
- a new builder
- Throws:
IllegalArgumentException- ifidis invalid
-
ofDouble
Starts building a field for decimal numbers (double); '.' is the decimal separator in every language.- Parameters:
id- the field's id, unique within its screen- Returns:
- a new builder
- Throws:
IllegalArgumentException- ifidis invalid
-
input
The text input backends draw; it has this entry's id, label, enabled state, tooltip and visibility. ItsTextInput.value()is the field's text.- Returns:
- the text input backing the field
-
min
-
max
-
number
The number in the field, for an action that reads it directly.- Returns:
- the parsed number, or
nullwhile the text is invalid
-
accept
-