Class NumberField<N extends Number>

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.NumberField<N>
Type Parameters:
N - Integer for ofInt(String), Double for ofDouble(String)

public final class NumberField<N extends Number> extends Entry
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 with BigDecimal 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.

  • Method Details

    • ofInt

      public static NumberField.Builder<Integer> ofInt(String id)
      Starts building a field for whole numbers (int, the full int range unless limited with NumberField.Builder.range(N, N)). Decimal text such as 2.5 is rejected; 2.0 is accepted as 2.
      Parameters:
      id - the field's id, unique within its screen
      Returns:
      a new builder
      Throws:
      IllegalArgumentException - if id is invalid
    • ofDouble

      public static NumberField.Builder<Double> ofDouble(String id)
      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 - if id is invalid
    • input

      public TextInput input()
      The text input backends draw; it has this entry's id, label, enabled state, tooltip and visibility. Its TextInput.value() is the field's text.
      Returns:
      the text input backing the field
    • min

      public N min()
      The minimum (inclusive), or null for none.
      Returns:
      the minimum, or null for none
    • max

      public N max()
      The maximum (inclusive), or null for none.
      Returns:
      the maximum, or null for none
    • number

      public N number()
      The number in the field, for an action that reads it directly.
      Returns:
      the parsed number, or null while the text is invalid
    • accept

      public <R> R accept(EntryVisitor<R> visitor)
      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