Browse Source

Merge branch 'pulls/62' into v4

shedaniel 4 years ago
parent
commit
7e6ba54a7d

+ 1 - 1
gradle.properties

@@ -5,5 +5,5 @@ minecraft_version=20w30a
 yarn_mappings=20w30a+build.3+legacy.20w09a+build.8
 loader_version=0.9.0+build.204
 fabric_version=0.15.2+build.382-1.16
-mod_version=4.8.1
+mod_version=4.8.2
 modmenu_version=1.14.6+build.31

+ 16 - 0
src/main/java/me/shedaniel/clothconfig2/gui/entries/AbstractListListEntry.java

@@ -6,7 +6,9 @@ import net.minecraft.text.Text;
 import org.jetbrains.annotations.ApiStatus;
 import org.jetbrains.annotations.Nullable;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.function.BiFunction;
 import java.util.function.Consumer;
@@ -25,11 +27,13 @@ public abstract class AbstractListListEntry<T, C extends AbstractListListEntry.A
     
     protected final BiFunction<T, SELF, C> createNewCell;
     protected Function<T, Optional<Text>> cellErrorSupplier;
+    protected List<T> original;
     
     @ApiStatus.Internal
     public AbstractListListEntry(Text fieldName, List<T> value, boolean defaultExpanded, Supplier<Optional<Text[]>> tooltipSupplier, Consumer<List<T>> saveConsumer, Supplier<List<T>> defaultValue, Text resetButtonKey, boolean requiresRestart, boolean deleteButtonEnabled, boolean insertInFront, BiFunction<T, SELF, C> createNewCell) {
         super(fieldName, tooltipSupplier, defaultValue, abstractListListEntry -> createNewCell.apply(null, abstractListListEntry), saveConsumer, resetButtonKey, requiresRestart, deleteButtonEnabled, insertInFront);
         this.createNewCell = createNewCell;
+        this.original = new ArrayList<T>(value);
         for (T f : value)
             cells.add(createNewCell.apply(f, this.self()));
         this.widgets.addAll(cells);
@@ -54,6 +58,18 @@ public abstract class AbstractListListEntry<T, C extends AbstractListListEntry.A
         return createNewCell.apply(value, this.self());
     }
     
+    @Override
+    public boolean isEdited() {
+        if (super.isEdited()) return true;
+        List<T> value = getValue();
+        if (value.size() != original.size()) return true;
+        for (int i = 0; i < value.size(); i++) {
+            if (!Objects.equals(value.get(i), original.get(i)))
+                return true;
+        }
+        return false;
+    }
+    
     /**
      * @param <T>           the configuration object type
      * @param <SELF>        the "curiously recurring template pattern" type parameter for this class

+ 1 - 0
src/main/java/me/shedaniel/clothconfig2/gui/entries/AbstractTextFieldListListEntry.java

@@ -64,6 +64,7 @@ public abstract class AbstractTextFieldListListEntry<T, C extends AbstractTextFi
             widget.setMaxLength(Integer.MAX_VALUE);
             widget.setHasBorder(false);
             widget.setText(Objects.toString(finalValue));
+            widget.setCursorToStart();
             widget.setChangedListener(s -> {
                 widget.setEditableColor(getPreferredTextColor());
             });

+ 24 - 13
src/main/java/me/shedaniel/clothconfig2/gui/entries/BaseListEntry.java

@@ -3,8 +3,6 @@ package me.shedaniel.clothconfig2.gui.entries;
 import com.google.common.collect.Lists;
 import com.mojang.blaze3d.systems.RenderSystem;
 import me.shedaniel.clothconfig2.api.Expandable;
-import me.shedaniel.clothconfig2.api.Tooltip;
-import me.shedaniel.math.Point;
 import me.shedaniel.math.Rectangle;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
@@ -56,17 +54,17 @@ public abstract class BaseListEntry<T, C extends BaseListCell, SELF extends Base
     protected Text addTooltip = new TranslatableText("text.cloth-config.list.add"), removeTooltip = new TranslatableText("text.cloth-config.list.remove");
     
     @ApiStatus.Internal
-    public BaseListEntry(@NotNull Text fieldName, @Nullable Supplier<Optional<Text[]>> tooltipSupplier, @NotNull Supplier<List<T>> defaultValue, @NotNull Function<SELF, C> createNewInstance, @Nullable Consumer<List<T>> saveConsumer, Text resetButtonKey) {
+    public BaseListEntry(@NotNull Text fieldName, @Nullable Supplier<Optional<Text[]>> tooltipSupplier, @Nullable Supplier<List<T>> defaultValue, @NotNull Function<SELF, C> createNewInstance, @Nullable Consumer<List<T>> saveConsumer, Text resetButtonKey) {
         this(fieldName, tooltipSupplier, defaultValue, createNewInstance, saveConsumer, resetButtonKey, false);
     }
     
     @ApiStatus.Internal
-    public BaseListEntry(@NotNull Text fieldName, @Nullable Supplier<Optional<Text[]>> tooltipSupplier, @NotNull Supplier<List<T>> defaultValue, @NotNull Function<SELF, C> createNewInstance, @Nullable Consumer<List<T>> saveConsumer, Text resetButtonKey, boolean requiresRestart) {
+    public BaseListEntry(@NotNull Text fieldName, @Nullable Supplier<Optional<Text[]>> tooltipSupplier, @Nullable Supplier<List<T>> defaultValue, @NotNull Function<SELF, C> createNewInstance, @Nullable Consumer<List<T>> saveConsumer, Text resetButtonKey, boolean requiresRestart) {
         this(fieldName, tooltipSupplier, defaultValue, createNewInstance, saveConsumer, resetButtonKey, requiresRestart, true, true);
     }
     
     @ApiStatus.Internal
-    public BaseListEntry(@NotNull Text fieldName, @Nullable Supplier<Optional<Text[]>> tooltipSupplier, @NotNull Supplier<List<T>> defaultValue, @NotNull Function<SELF, C> createNewInstance, @Nullable Consumer<List<T>> saveConsumer, Text resetButtonKey, boolean requiresRestart, boolean deleteButtonEnabled, boolean insertInFront) {
+    public BaseListEntry(@NotNull Text fieldName, @Nullable Supplier<Optional<Text[]>> tooltipSupplier, @Nullable Supplier<List<T>> defaultValue, @NotNull Function<SELF, C> createNewInstance, @Nullable Consumer<List<T>> saveConsumer, Text resetButtonKey, boolean requiresRestart, boolean deleteButtonEnabled, boolean insertInFront) {
         super(fieldName, tooltipSupplier, requiresRestart);
         this.deleteButtonEnabled = deleteButtonEnabled;
         this.insertInFront = insertInFront;
@@ -105,12 +103,20 @@ public abstract class BaseListEntry<T, C extends BaseListCell, SELF extends Base
     public boolean isEdited() {
         if (super.isEdited()) return true;
         if (cells.stream().anyMatch(BaseListCell::isEdited)) return true;
-        List<T> value = getValue();
-        List<T> defaultValue = this.defaultValue.get();
-        if (value.size() != defaultValue.size()) return true;
-        for (int i = 0; i < value.size(); i++) {
-            if (!Objects.equals(value.get(i), defaultValue.get(i)))
-                return true;
+        return false;
+    }
+    
+    public boolean isMatchDefault() {
+        Optional<List<T>> defaultValueOptional = getDefaultValue();
+        if (defaultValueOptional.isPresent()) {
+            List<T> value = getValue();
+            List<T> defaultValue = defaultValueOptional.get();
+            if (value.size() != defaultValue.size()) return false;
+            for (int i = 0; i < value.size(); i++) {
+                if (!Objects.equals(value.get(i), defaultValue.get(i)))
+                    return false;
+            }
+            return true;
         }
         return false;
     }
@@ -161,7 +167,11 @@ public abstract class BaseListEntry<T, C extends BaseListCell, SELF extends Base
     
     @Override
     public Optional<List<T>> getDefaultValue() {
-        return Optional.ofNullable(defaultValue.get());
+        if (defaultValue == null) {
+            return Optional.empty();
+        } else {
+            return Optional.ofNullable(defaultValue.get());
+        }
     }
     
     @Override
@@ -218,6 +228,7 @@ public abstract class BaseListEntry<T, C extends BaseListCell, SELF extends Base
         return isDeleteButtonEnabled() && mouseX >= labelWidget.rectangle.x + 25 && mouseY >= labelWidget.rectangle.y + 3 && mouseX <= labelWidget.rectangle.x + 25 + 11 && mouseY <= labelWidget.rectangle.y + 3 + 11;
     }
     
+    @Override
     public Optional<Text[]> getTooltip(int mouseX, int mouseY) {
         if (addTooltip != null && isInsideCreateNew(mouseX, mouseY))
             return Optional.of(new Text[]{addTooltip});
@@ -243,7 +254,7 @@ public abstract class BaseListEntry<T, C extends BaseListCell, SELF extends Base
             drawTexture(matrices, x - 15 + 26, y + 5, 24 + 27, focused == null ? 0 : insideDelete ? 18 : 9, 9, 9);
         resetWidget.x = x + entryWidth - resetWidget.getWidth();
         resetWidget.y = y;
-        resetWidget.active = isEdited();
+        resetWidget.active = isEditable() && getDefaultValue().isPresent() && !isMatchDefault();
         resetWidget.render(matrices, mouseX, mouseY, delta);
         MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, getDisplayedFieldName().method_30937(), isDeleteButtonEnabled() ? x + 24 : x + 24 - 9, y + 6, labelWidget.rectangle.contains(mouseX, mouseY) && !resetWidget.isMouseOver(mouseX, mouseY) && !insideDelete && !insideCreateNew ? 0xffe6fe16 : getPreferredTextColor());
         if (expanded) {

+ 5 - 1
src/main/java/me/shedaniel/clothconfig2/gui/entries/TooltipListEntry.java

@@ -35,7 +35,7 @@ public abstract class TooltipListEntry<T> extends AbstractConfigListEntry<T> {
     public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isHovered, float delta) {
         super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isHovered, delta);
         if (isMouseInside(mouseX, mouseY, x, y, entryWidth, entryHeight)) {
-            Optional<Text[]> tooltip = getTooltip();
+            Optional<Text[]> tooltip = getTooltip(mouseX, mouseY);
             if (tooltip.isPresent() && tooltip.get().length > 0)
                 addTooltip(Tooltip.of(new Point(mouseX, mouseY), tooltip.get()));
         }
@@ -47,6 +47,10 @@ public abstract class TooltipListEntry<T> extends AbstractConfigListEntry<T> {
         return Optional.empty();
     }
     
+    public Optional<Text[]> getTooltip(int mouseX, int mouseY) {
+        return getTooltip();
+    }
+    
     @Nullable
     public Supplier<Optional<Text[]>> getTooltipSupplier() {
         return tooltipSupplier;