CycleButtonWidget.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package chylex.bettercontrols.gui.elements;
  2. import net.minecraft.client.gui.components.Button;
  3. import java.util.List;
  4. import java.util.function.Consumer;
  5. public class CycleButtonWidget<T> extends Button {
  6. private final List<Option<T>> options;
  7. private final Consumer<T> onChanged;
  8. private T selectedValue;
  9. public CycleButtonWidget(final int x, final int y, final int width, final int height, final List<Option<T>> options, final T selectedValue, final Consumer<T> onChanged) {
  10. super(x, y, width, height, Option.find(options, selectedValue).getText(), btn -> {});
  11. this.options = options;
  12. this.selectedValue = selectedValue;
  13. this.onChanged = onChanged;
  14. }
  15. public CycleButtonWidget(final int x, final int y, final int width, final List<Option<T>> options, final T selectedValue, final Consumer<T> onChanged) {
  16. this(x, y, width, 20, options, selectedValue, onChanged);
  17. }
  18. @Override
  19. public void onPress() {
  20. int nextIndex = options.indexOf(Option.find(options, selectedValue)) + 1;
  21. if (nextIndex >= options.size()) {
  22. nextIndex = 0;
  23. }
  24. final Option<T> newSelectedOption = options.get(nextIndex);
  25. selectedValue = newSelectedOption.getValue();
  26. onChanged.accept(selectedValue);
  27. setMessage(newSelectedOption.getText());
  28. }
  29. }