Class Cycle<T>

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

public final class Cycle<T> extends Entry
A button that steps through a fixed list of values, shown as "label: value" like vanilla's option buttons (e.g. "Graphics: Fancy"). Each click selects the next value, wrapping around after the last. Good for a handful of values, typically an enum; for long lists use a Dropdown, which has the same builder.
Cycle.<Mode>builder("mode")
        .label(Text.translatable("mymod.mode", "Mode"))
        .values(Arrays.asList(Mode.values()))
        .value(() -> config.mode)
        .valueText(m -> Text.translatable("mymod.mode." + m.name().toLowerCase(Locale.ROOT), m.name()))
        .onChange(m -> config.mode = m)
        .build();

Required: label, values (not empty), value, valueText, onChange. The value lives in the mod. Values are compared with Object.equals(Object); a current value that is not in the list shows as the first one.

Backends work with indexes (index(), select(int), next()) so they never need the value type.

  • Method Details

    • builder

      public static <T> Cycle.Builder<T> builder(String id)
      Starts building a cycle. Java usually can't infer T from a chained call, so give it explicitly: Cycle.<Mode>builder("mode").
      Type Parameters:
      T - the value type
      Parameters:
      id - the cycle'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 text shown before the value
    • values

      public List<T> values()
      The values, in cycling 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, from the mod's valueText function.
      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
    • next

      public void next()
      Selects the value after the current one, wrapping to the first after the last, as a click does.
    • 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