KeyCodeEntry.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package me.shedaniel.clothconfig2.gui.entries;
  2. import com.google.common.collect.Lists;
  3. import me.shedaniel.clothconfig2.api.ModifierKeyCode;
  4. import net.fabricmc.api.EnvType;
  5. import net.fabricmc.api.Environment;
  6. import net.minecraft.client.MinecraftClient;
  7. import net.minecraft.client.gui.Element;
  8. import net.minecraft.client.gui.widget.ButtonWidget;
  9. import net.minecraft.client.util.NarratorManager;
  10. import net.minecraft.client.util.Window;
  11. import net.minecraft.client.util.math.MatrixStack;
  12. import net.minecraft.text.LiteralText;
  13. import net.minecraft.text.Text;
  14. import net.minecraft.util.Formatting;
  15. import org.jetbrains.annotations.ApiStatus;
  16. import java.util.List;
  17. import java.util.Optional;
  18. import java.util.function.Consumer;
  19. import java.util.function.Supplier;
  20. @SuppressWarnings("DuplicatedCode")
  21. @Environment(EnvType.CLIENT)
  22. public class KeyCodeEntry extends TooltipListEntry<ModifierKeyCode> {
  23. private ModifierKeyCode value;
  24. private ModifierKeyCode original;
  25. private ButtonWidget buttonWidget, resetButton;
  26. private Consumer<ModifierKeyCode> saveConsumer;
  27. private Supplier<ModifierKeyCode> defaultValue;
  28. private List<Element> widgets;
  29. private boolean allowMouse = true, allowKey = true, allowModifiers = true;
  30. @ApiStatus.Internal
  31. @Deprecated
  32. public KeyCodeEntry(Text fieldName, ModifierKeyCode value, Text resetButtonKey, Supplier<ModifierKeyCode> defaultValue, Consumer<ModifierKeyCode> saveConsumer, Supplier<Optional<Text[]>> tooltipSupplier, boolean requiresRestart) {
  33. super(fieldName, tooltipSupplier, requiresRestart);
  34. this.defaultValue = defaultValue;
  35. this.value = value.copy();
  36. this.original = value.copy();
  37. this.buttonWidget = new ButtonWidget(0, 0, 150, 20, NarratorManager.EMPTY, widget -> {
  38. getConfigScreen().setFocusedBinding(this);
  39. });
  40. this.resetButton = new ButtonWidget(0, 0, MinecraftClient.getInstance().textRenderer.getWidth(resetButtonKey) + 6, 20, resetButtonKey, widget -> {
  41. KeyCodeEntry.this.value = getDefaultValue().orElse(null).copy();
  42. getConfigScreen().setFocusedBinding(null);
  43. });
  44. this.saveConsumer = saveConsumer;
  45. this.widgets = Lists.newArrayList(buttonWidget, resetButton);
  46. }
  47. @Override
  48. public boolean isEdited() {
  49. return super.isEdited() || !this.original.equals(getValue());
  50. }
  51. public boolean isAllowModifiers() {
  52. return allowModifiers;
  53. }
  54. public void setAllowModifiers(boolean allowModifiers) {
  55. this.allowModifiers = allowModifiers;
  56. }
  57. public boolean isAllowKey() {
  58. return allowKey;
  59. }
  60. public void setAllowKey(boolean allowKey) {
  61. this.allowKey = allowKey;
  62. }
  63. public boolean isAllowMouse() {
  64. return allowMouse;
  65. }
  66. public void setAllowMouse(boolean allowMouse) {
  67. this.allowMouse = allowMouse;
  68. }
  69. @Override
  70. public void save() {
  71. if (saveConsumer != null)
  72. saveConsumer.accept(getValue());
  73. }
  74. @Override
  75. public ModifierKeyCode getValue() {
  76. return value;
  77. }
  78. public void setValue(ModifierKeyCode value) {
  79. this.value = value;
  80. }
  81. @Override
  82. public Optional<ModifierKeyCode> getDefaultValue() {
  83. return Optional.ofNullable(defaultValue).map(Supplier::get).map(ModifierKeyCode::copy);
  84. }
  85. private Text getLocalizedName() {
  86. return this.value.getLocalizedName();
  87. }
  88. @Override
  89. public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
  90. super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
  91. Window window = MinecraftClient.getInstance().getWindow();
  92. this.resetButton.active = isEditable() && getDefaultValue().isPresent() && !getDefaultValue().get().equals(getValue());
  93. this.resetButton.y = y;
  94. this.buttonWidget.active = isEditable();
  95. this.buttonWidget.y = y;
  96. this.buttonWidget.setMessage(getLocalizedName());
  97. if (getConfigScreen().getFocusedBinding() == this)
  98. this.buttonWidget.setMessage(new LiteralText("> ").formatted(Formatting.WHITE).append(this.buttonWidget.getMessage().copy().formatted(Formatting.YELLOW)).append(new LiteralText(" <").formatted(Formatting.WHITE)));
  99. Text displayedFieldName = getDisplayedFieldName();
  100. if (MinecraftClient.getInstance().textRenderer.isRightToLeft()) {
  101. MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, displayedFieldName, window.getScaledWidth() - x - MinecraftClient.getInstance().textRenderer.getWidth(displayedFieldName), y + 5, 16777215);
  102. this.resetButton.x = x;
  103. this.buttonWidget.x = x + resetButton.getWidth() + 2;
  104. } else {
  105. MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, displayedFieldName, x, y + 5, getPreferredTextColor());
  106. this.resetButton.x = x + entryWidth - resetButton.getWidth();
  107. this.buttonWidget.x = x + entryWidth - 150;
  108. }
  109. this.buttonWidget.setWidth(150 - resetButton.getWidth() - 2);
  110. resetButton.render(matrices, mouseX, mouseY, delta);
  111. buttonWidget.render(matrices, mouseX, mouseY, delta);
  112. }
  113. @Override
  114. public List<? extends Element> children() {
  115. return widgets;
  116. }
  117. }