Class Toggle

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.Toggle

public final class Toggle extends Entry
An on/off setting, drawn as a vanilla button reading "label: ON" or "label: OFF" (UiTexts.ON, UiTexts.OFF), like the toggles on vanilla's options screens. A click flips it.
Toggle.builder("particles")
        .label(Text.translatable("mymod.particles", "Particles"))
        .value(() -> config.particles)
        .onChange(on -> config.particles = on)
        .build();

The value lives in the mod: the button shows what value returns, and a click calls onChange with the opposite. Required: label, value, onChange. Checkbox is the same setting drawn as a check box.

  • Method Details

    • builder

      public static Toggle.Builder builder(String id)
      Starts building a toggle.
      Parameters:
      id - the toggle's id, unique within its screen
      Returns:
      a new builder
      Throws:
      IllegalArgumentException - if id is invalid
    • label

      public Text label()
      The text before ": ON/OFF", from the mod's supplier.
      Returns:
      the label
    • value

      public boolean value()
      The mod's current value, from its value supplier.
      Returns:
      true if the toggle shows ON
    • set

      public void set(boolean newValue)
      Passes newValue to onChange, even if it equals the current value.
      Parameters:
      newValue - the new value
    • toggle

      public void toggle()
      Passes the opposite of value() to onChange, 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