Class Dropdown<T>

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.Dropdown<T>
Type Parameters:
T - the value type

public final class Dropdown<T> extends Entry
Picks one of a list of values, for lists too long to step through with a Cycle: languages, biomes, profiles. It has the same builder and the same value handling as a cycle; only pressing it differs.
Dropdown.<String>builder("profile")
        .label(Text.translatable("mymod.profile", "Profile"))
        .values(profileNames)
        .value(() -> config.profile)
        .valueText(Text::literal)
        .onChange(name -> config.profile = name)
        .build();

How it looks and behaves

Backends draw it like a cycle: a button "label: value". Pressing it calls open(UiHost), which opens a picker screen titled with the label: the values as a SelectList (<id>.list, row ids are the indexes, the current value selected) and a Cancel button (<id>.cancel) in the footer. Picking a row closes the picker, as UiHost.back(), and then passes the value to onChange. No backend draws a popup list (TODO.md Phase 11). The picker's ids are derived from the dropdown's, so keep <id>.picker, <id>.list and <id>.cancel free.

Required: label, values (not empty), value, valueText, onChange. Allowed in a MenuScreen's footer.

  • Method Details

    • builder

      public static <T> Dropdown.Builder<T> builder(String id)
      Starts building a dropdown. Give T explicitly: Dropdown.<String>builder("profile").
      Type Parameters:
      T - the value type
      Parameters:
      id - the dropdown'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; also the picker screen's title.
      Returns:
      the text shown before the value
    • values

      public List<T> values()
      The values, in list order: a copy of the builder's list taken at build time.
      Returns:
      an unmodifiable list, never empty
    • valueTexts

      public List<Text> valueTexts()
      Every value's text, in values() order: the picker's rows.
      Returns:
      a new list
    • index

      public int index()
      Index of the mod's current value in values(); 0 if the current value is not in the list.
      Returns:
      the index of the current value, or 0 if it is not in the list
    • valueText

      public Text valueText()
      The current value's text, shown after "label: ".
      Returns:
      the text of the current value
    • select

      public void select(int index)
      Passes the value at index to onChange, even if it is the current one.
      Parameters:
      index - an index into values()
      Throws:
      IndexOutOfBoundsException - if index is out of range
    • open

      public void open(UiHost host)
      Opens the picker screen on host, the current value selected. Backends call it when the button is pressed, with their own host; the picker belongs to the same mod as this entry, for Failsafe. Picking a row goes back and then selects it; Cancel just goes back.
      Parameters:
      host - the host showing this dropdown's screen
    • 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