Преглед на файлове

Created a somewhat solid ColorWidget

Update modmenu version


Created ColorEntry


Created ColorFieldBuilder and implemented it.


Added an example ColorEntry to the testing tab.
Bernardo Antunes преди 5 години
родител
ревизия
5c82f47d40

+ 1 - 1
gradle.properties

@@ -3,5 +3,5 @@ yarn_mappings=20w12a+build.3
 loader_version=0.7.8+build.186
 fabric_version=0.5.4+build.310-1.16
 mod_version=3.1.0-unstable
-modmenu_version=1.10.1+build.30
+modmenu_version=1.11.0+build.2
 nec_version=1.2.3+1.15.1

+ 1 - 0
src/main/java/me/shedaniel/clothconfig2/ClothConfigInitializer.java

@@ -302,6 +302,7 @@ public class ClothConfigInitializer implements ClientModInitializer {
         testing.addEntry(entryBuilder.startDoubleList("A list of Doubles", Arrays.asList(1d, 2d, 3d)).setDefaultValue(Arrays.asList(1d, 2d, 3d)).build());
         testing.addEntry(entryBuilder.startLongList("A list of Longs", Arrays.asList(1L, 2L, 3L)).setDefaultValue(Arrays.asList(1L, 2L, 3L)).build());
         testing.addEntry(entryBuilder.startStrList("A list of Strings", Arrays.asList("abc", "xyz")).setDefaultValue(Arrays.asList("abc", "xyz")).build());
+        testing.addEntry(entryBuilder.startColorField("A color field", 0x00ffff).setDefaultValue(0xff0000).build());
         return builder;
     }
     

+ 3 - 1
src/main/java/me/shedaniel/clothconfig2/api/ConfigEntryBuilder.java

@@ -44,7 +44,9 @@ public interface ConfigEntryBuilder {
     BooleanToggleBuilder startBooleanToggle(String fieldNameKey, boolean value);
     
     StringFieldBuilder startStrField(String fieldNameKey, String value);
-    
+
+    ColorFieldBuilder startColorField(String fieldNameKey, int value);
+
     TextFieldBuilder startTextField(String fieldNameKey, String value);
     
     TextDescriptionBuilder startTextDescription(String value);

+ 107 - 0
src/main/java/me/shedaniel/clothconfig2/gui/entries/ColorEntry.java

@@ -0,0 +1,107 @@
+package me.shedaniel.clothconfig2.gui.entries;
+
+import me.shedaniel.clothconfig2.gui.widget.ColorDisplayWidget;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.gui.widget.TextFieldWidget;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.Optional;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
+public class ColorEntry extends TextFieldListEntry<Integer> {
+
+    private ColorDisplayWidget colorDisplayWidget;
+    private Consumer<Integer> saveConsumer;
+    private static boolean alpha = false;
+
+    @Deprecated
+    public ColorEntry(String fieldName, int value, String resetButtonKey, Supplier<Integer> defaultValue, Consumer<Integer> saveConsumer, Supplier<Optional<String[]>> tooltipSupplier, boolean requiresRestart) {
+        super(fieldName, value, resetButtonKey, defaultValue, tooltipSupplier, requiresRestart);
+        this.saveConsumer = saveConsumer;
+        this.colorDisplayWidget = new ColorDisplayWidget(0, 0, 18, getValidIntColor(textFieldWidget.getText()));
+    }
+
+    @Override
+    public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
+        super.render(index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
+        this.colorDisplayWidget.y = y + 1;
+        if (isValidHexColorString(textFieldWidget.getText()))
+            colorDisplayWidget.setColor(getValidIntColor(textFieldWidget.getText()));
+        if (MinecraftClient.getInstance().textRenderer.isRightToLeft()) {
+            this.colorDisplayWidget.x = x + resetButton.getWidth() + textFieldWidget.getWidth();
+        } else {
+            this.colorDisplayWidget.x = textFieldWidget.x - 21;
+        }
+        colorDisplayWidget.render(mouseX, mouseY, delta);
+    }
+
+    @Override
+    protected void textFieldPreRender(TextFieldWidget widget) {
+        if (isValidHexColorString(textFieldWidget.getText())) {
+            widget.setEditableColor(14737632);
+        } else {
+            widget.setEditableColor(16733525);
+        }
+    }
+
+    @Override
+    public void save() {
+        if (saveConsumer != null)
+            saveConsumer.accept(getValue());
+    }
+
+    @Override
+    protected boolean isMatchDefault(String text) {
+        return getDefaultValue().isPresent() && (getValidIntColor(text) == getDefaultValue().get());
+    }
+
+    @Override
+    public Integer getValue() {
+        return getValidIntColor(textFieldWidget.getText());
+    }
+
+    public void withAlpha() {
+        if (!alpha) {
+            this.alpha = true;
+        }
+        //textFieldWidget.setText(getHexColorString(original));
+    }
+
+    public void withoutAlpha() {
+        if (alpha) {
+            alpha = false;
+        }
+        //textFieldWidget.setText(getHexColorString(original));
+    }
+
+    protected String stripHexStarter(String hex) {
+        if (hex.startsWith("#")) {
+            return hex.substring(1);
+        } else return hex;
+    }
+
+    protected boolean isValidHexColorString(String hex) {
+        try {
+            String stripped = stripHexStarter(hex);
+            Long.parseLong(stripped, 16);
+            return alpha ? stripped.length() == 8 : stripped.length() == 6;
+        } catch (NumberFormatException ex) {
+            return false;
+        }
+    }
+
+    protected int getValidIntColor(String hex) {
+        if (isValidHexColorString(hex)) {
+            try {
+                return (int) (Long.parseLong(stripHexStarter(hex), 16));
+            } catch (NumberFormatException ex) {
+                return -1;
+            }
+        } else return -1;
+    }
+
+    protected static String getHexColorString(int color) {
+        return "#" + StringUtils.leftPad(Integer.toHexString(color), alpha ? 8 : 6, '0');
+    }
+}

+ 34 - 0
src/main/java/me/shedaniel/clothconfig2/gui/widget/ColorDisplayWidget.java

@@ -0,0 +1,34 @@
+package me.shedaniel.clothconfig2.gui.widget;
+
+import net.minecraft.client.gui.widget.AbstractButtonWidget;
+
+public class ColorDisplayWidget extends AbstractButtonWidget {
+
+    protected int color;
+    protected int size;
+
+    public ColorDisplayWidget(int x, int y, int size, int color) {
+        super(x, y, size, size, "");
+        this.color = 0xff000000 | color;
+        this.size = size;
+    }
+
+    @Override
+    public void renderButton(int mouseX, int mouseY, float delta) {
+        fillGradient(this.x, this.y, this.x + size, this.y + size, -0x5F5F60, -0x5F5F60);
+        fillGradient(this.x + 1, this.y + 1, this.x + size - 1, this.y + size - 1, 0xff000000, 0xff000000);
+        fillGradient(this.x + 1, this.y + 1, this.x + size - 1, this.y + size - 1, color, color);
+    }
+
+    @Override
+    public void onClick(double mouseX, double mouseY) {
+    }
+
+    @Override
+    public void onRelease(double mouseX, double mouseY) {
+    }
+
+    public void setColor(int color) {
+        this.color = 0xff000000 | color;
+    }
+}

+ 5 - 0
src/main/java/me/shedaniel/clothconfig2/impl/ConfigEntryBuilderImpl.java

@@ -90,6 +90,11 @@ public class ConfigEntryBuilderImpl implements ConfigEntryBuilder {
     public StringFieldBuilder startStrField(String fieldNameKey, String value) {
         return new StringFieldBuilder(resetButtonKey, fieldNameKey, value);
     }
+
+    @Override
+    public ColorFieldBuilder startColorField(String fieldNameKey, int value) {
+        return new ColorFieldBuilder(resetButtonKey, fieldNameKey, value);
+    }
     
     @Override
     public TextFieldBuilder startTextField(String fieldNameKey, String value) {

+ 94 - 0
src/main/java/me/shedaniel/clothconfig2/impl/builders/ColorFieldBuilder.java

@@ -0,0 +1,94 @@
+package me.shedaniel.clothconfig2.impl.builders;
+
+import me.shedaniel.clothconfig2.gui.entries.ColorEntry;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+@Environment(EnvType.CLIENT)
+public class ColorFieldBuilder extends FieldBuilder<String, ColorEntry> {
+
+    private Consumer<Integer> saveConsumer = null;
+    private Function<Integer, Optional<String>> errorSupplier;
+    private Function<Integer, Optional<String[]>> tooltipSupplier = str -> Optional.empty();
+    private final int value;
+    private Supplier<Integer> defaultValue;
+    private boolean alpha;
+
+    public ColorFieldBuilder(String resetButtonKey, String fieldNameKey, int value) {
+        super(resetButtonKey, fieldNameKey);
+        this.value = value;
+    }
+
+    public ColorFieldBuilder setErrorSupplier(Function<Integer, Optional<String>> errorSupplier) {
+        this.errorSupplier = errorSupplier;
+        return this;
+    }
+
+    public ColorFieldBuilder requireRestart() {
+        requireRestart(true);
+        return this;
+    }
+
+    public ColorFieldBuilder setSaveConsumer(Consumer<Integer> saveConsumer) {
+        this.saveConsumer = saveConsumer;
+        return this;
+    }
+
+    public ColorFieldBuilder setDefaultValue(Supplier<Integer> defaultValue) {
+        this.defaultValue = defaultValue;
+        return this;
+    }
+
+    public ColorFieldBuilder setAlphaMode(boolean withAlpha) {
+        this.alpha = withAlpha;
+        return this;
+    }
+
+    public ColorFieldBuilder setDefaultValue(int defaultValue) {
+        this.defaultValue = () -> Objects.requireNonNull(defaultValue);
+        return this;
+    }
+
+    public ColorFieldBuilder setTooltipSupplier(Supplier<Optional<String[]>> tooltipSupplier) {
+        this.tooltipSupplier = str -> tooltipSupplier.get();
+        return this;
+    }
+
+    public ColorFieldBuilder setTooltipSupplier(Function<Integer, Optional<String[]>> tooltipSupplier) {
+        this.tooltipSupplier = tooltipSupplier;
+        return this;
+    }
+
+    public ColorFieldBuilder setTooltip(Optional<String[]> tooltip) {
+        this.tooltipSupplier = str -> tooltip;
+        return this;
+    }
+
+    public ColorFieldBuilder setTooltip(String... tooltip) {
+        this.tooltipSupplier = str -> Optional.ofNullable(tooltip);
+        return this;
+    }
+
+    @NotNull
+    @Override
+    public ColorEntry build() {
+        ColorEntry entry = new ColorEntry(getFieldNameKey(), value, getResetButtonKey(), defaultValue, saveConsumer, null, isRequireRestart());
+        if (this.alpha) {
+            entry.withAlpha();
+        } else {
+            entry.withoutAlpha();
+        }
+        entry.setTooltipSupplier(() -> tooltipSupplier.apply(entry.getValue()));
+        if (errorSupplier != null)
+            entry.setErrorSupplier(() -> errorSupplier.apply(entry.getValue()));
+        return entry;
+    }
+
+}