Ver Fonte

Refactor list options to use AbstractTextFieldListListEntry

Mitchell Skaggs há 5 anos atrás
pai
commit
29ef7def4d

+ 16 - 56
src/main/java/me/shedaniel/clothconfig2/gui/entries/DoubleListListEntry.java

@@ -1,21 +1,17 @@
 package me.shedaniel.clothconfig2.gui.entries;
 
-import net.minecraft.client.MinecraftClient;
-import net.minecraft.client.gui.Element;
-import net.minecraft.client.gui.widget.TextFieldWidget;
 import net.minecraft.client.resource.language.I18n;
 import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
-import java.util.Collections;
 import java.util.List;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
-import java.util.stream.Collectors;
 
 @ApiStatus.Internal
-public class DoubleListListEntry extends AbstractListListEntry<Double, DoubleListListEntry.DoubleListCell, DoubleListListEntry> {
+public class DoubleListListEntry extends AbstractTextFieldListListEntry<Double, DoubleListListEntry.DoubleListCell, DoubleListListEntry> {
 
     private double minimum, maximum;
 
@@ -46,11 +42,6 @@ public class DoubleListListEntry extends AbstractListListEntry<Double, DoubleLis
         this.maximum = Double.POSITIVE_INFINITY;
     }
 
-    @Override
-    public List<Double> getValue() {
-        return cells.stream().map(DoubleListCell::getValue).collect(Collectors.toList());
-    }
-
     public DoubleListListEntry setMaximum(Double maximum) {
         this.maximum = maximum;
         return this;
@@ -66,34 +57,24 @@ public class DoubleListListEntry extends AbstractListListEntry<Double, DoubleLis
         return this;
     }
 
-    @Override
-    protected DoubleListCell getFromValue(Double value) {
-        return new DoubleListCell(value, this);
-    }
-
-    public static class DoubleListCell extends AbstractListListEntry.AbstractListCell<Double, DoubleListCell, DoubleListListEntry> {
-
-        private TextFieldWidget widget;
+    public static class DoubleListCell extends AbstractTextFieldListListEntry.AbstractTextFieldListCell<Double, DoubleListCell, DoubleListListEntry> {
 
         public DoubleListCell(Double value, final DoubleListListEntry listListEntry) {
             super(value, listListEntry);
+        }
 
+        @Nullable
+        @Override
+        protected Double substituteDefault(@Nullable Double value) {
             if (value == null)
-                value = 0d;
-            Double finalValue = value;
-
-            this.setErrorSupplier(() -> Optional.ofNullable(listListEntry.cellErrorSupplier).flatMap(fn -> fn.apply(this.getValue())));
-            widget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 100, 18, "");
-            widget.setTextPredicate(s -> s.chars().allMatch(c -> Character.isDigit(c) || c == '-' || c == '.'));
-            widget.setMaxLength(Integer.MAX_VALUE);
-            widget.setHasBorder(false);
-            widget.setText(value.toString());
-            widget.setChangedListener(s -> {
-                widget.setEditableColor(getPreferredTextColor());
-                if (!Objects.equals(s, finalValue.toString())) {
-                    this.listListEntry.getScreen().setEdited(true, this.listListEntry.isRequiresRestart());
-                }
-            });
+                return 0d;
+            else
+                return value;
+        }
+
+        @Override
+        protected boolean isValidText(@NotNull String text) {
+            return text.chars().allMatch(c -> Character.isDigit(c) || c == '-' || c == '.');
         }
 
         public Double getValue() {
@@ -118,27 +99,6 @@ public class DoubleListListEntry extends AbstractListListEntry<Double, DoubleLis
             return Optional.empty();
         }
 
-        @Override
-        public int getCellHeight() {
-            return 20;
-        }
-
-        @Override
-        public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
-            widget.setWidth(entryWidth - 12);
-            widget.x = x;
-            widget.y = y + 1;
-            widget.setEditable(listListEntry.isEditable());
-            widget.render(mouseX, mouseY, delta);
-            if (isSelected && listListEntry.isEditable())
-                fill(x, y + 12, x + entryWidth - 12, y + 13, getConfigError().isPresent() ? 0xffff5555 : 0xffe0e0e0);
-        }
-
-        @Override
-        public List<? extends Element> children() {
-            return Collections.singletonList(widget);
-        }
-
     }
 
 }

+ 16 - 58
src/main/java/me/shedaniel/clothconfig2/gui/entries/FloatListListEntry.java

@@ -1,21 +1,17 @@
 package me.shedaniel.clothconfig2.gui.entries;
 
-import net.minecraft.client.MinecraftClient;
-import net.minecraft.client.gui.Element;
-import net.minecraft.client.gui.widget.TextFieldWidget;
 import net.minecraft.client.resource.language.I18n;
 import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
-import java.util.Collections;
 import java.util.List;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
-import java.util.stream.Collectors;
 
 @ApiStatus.Internal
-public class FloatListListEntry extends AbstractListListEntry<Float, FloatListListEntry.FloatListCell, FloatListListEntry> {
+public class FloatListListEntry extends AbstractTextFieldListListEntry<Float, FloatListListEntry.FloatListCell, FloatListListEntry> {
 
     private float minimum, maximum;
 
@@ -46,11 +42,6 @@ public class FloatListListEntry extends AbstractListListEntry<Float, FloatListLi
         this.maximum = Float.POSITIVE_INFINITY;
     }
 
-    @Override
-    public List<Float> getValue() {
-        return cells.stream().map(FloatListCell::getValue).collect(Collectors.toList());
-    }
-
     public FloatListListEntry setMaximum(float maximum) {
         this.maximum = maximum;
         return this;
@@ -66,34 +57,24 @@ public class FloatListListEntry extends AbstractListListEntry<Float, FloatListLi
         return this;
     }
 
-    @Override
-    protected FloatListCell getFromValue(Float value) {
-        return new FloatListCell(value, this);
-    }
-
-    public static class FloatListCell extends AbstractListListEntry.AbstractListCell<Float, FloatListCell, FloatListListEntry> {
-
-        private TextFieldWidget widget;
+    public static class FloatListCell extends AbstractTextFieldListListEntry.AbstractTextFieldListCell<Float, FloatListCell, FloatListListEntry> {
 
         public FloatListCell(Float value, FloatListListEntry listListEntry) {
             super(value, listListEntry);
+        }
 
+        @Nullable
+        @Override
+        protected Float substituteDefault(@Nullable Float value) {
             if (value == null)
-                value = 0f;
-            Float finalValue = value;
-
-            this.setErrorSupplier(() -> Optional.ofNullable(listListEntry.cellErrorSupplier).flatMap(fn -> fn.apply(this.getValue())));
-            widget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 100, 18, "");
-            widget.setTextPredicate(s -> s.chars().allMatch(c -> Character.isDigit(c) || c == '-' || c == '.'));
-            widget.setMaxLength(Integer.MAX_VALUE);
-            widget.setHasBorder(false);
-            widget.setText(value.toString());
-            widget.setChangedListener(s -> {
-                widget.setEditableColor(getPreferredTextColor());
-                if (!Objects.equals(s, finalValue.toString())) {
-                    this.listListEntry.getScreen().setEdited(true, this.listListEntry.isRequiresRestart());
-                }
-            });
+                return 0f;
+            else
+                return value;
+        }
+
+        @Override
+        protected boolean isValidText(@NotNull String text) {
+            return text.chars().allMatch(c -> Character.isDigit(c) || c == '-' || c == '.');
         }
 
         public Float getValue() {
@@ -117,28 +98,5 @@ public class FloatListListEntry extends AbstractListListEntry<Float, FloatListLi
             }
             return Optional.empty();
         }
-
-        @Override
-        public int getCellHeight() {
-            return 20;
-        }
-
-        @Override
-        public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
-            widget.setWidth(entryWidth - 12);
-            widget.x = x;
-            widget.y = y + 1;
-            widget.setEditable(listListEntry.isEditable());
-            widget.render(mouseX, mouseY, delta);
-            if (isSelected && listListEntry.isEditable())
-                fill(x, y + 12, x + entryWidth - 12, y + 13, getConfigError().isPresent() ? 0xffff5555 : 0xffe0e0e0);
-        }
-
-        @Override
-        public List<? extends Element> children() {
-            return Collections.singletonList(widget);
-        }
-
     }
-
 }

+ 16 - 57
src/main/java/me/shedaniel/clothconfig2/gui/entries/IntegerListListEntry.java

@@ -1,21 +1,17 @@
 package me.shedaniel.clothconfig2.gui.entries;
 
-import net.minecraft.client.MinecraftClient;
-import net.minecraft.client.gui.Element;
-import net.minecraft.client.gui.widget.TextFieldWidget;
 import net.minecraft.client.resource.language.I18n;
 import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
-import java.util.Collections;
 import java.util.List;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
-import java.util.stream.Collectors;
 
 @ApiStatus.Internal
-public class IntegerListListEntry extends AbstractListListEntry<Integer, IntegerListListEntry.IntegerListCell, IntegerListListEntry> {
+public class IntegerListListEntry extends AbstractTextFieldListListEntry<Integer, IntegerListListEntry.IntegerListCell, IntegerListListEntry> {
 
     private int minimum, maximum;
 
@@ -46,11 +42,6 @@ public class IntegerListListEntry extends AbstractListListEntry<Integer, Integer
         this.maximum = Integer.MAX_VALUE;
     }
 
-    @Override
-    public List<Integer> getValue() {
-        return cells.stream().map(IntegerListCell::getValue).collect(Collectors.toList());
-    }
-
     public IntegerListListEntry setMaximum(int maximum) {
         this.maximum = maximum;
         return this;
@@ -66,34 +57,24 @@ public class IntegerListListEntry extends AbstractListListEntry<Integer, Integer
         return this;
     }
 
-    @Override
-    protected IntegerListCell getFromValue(Integer value) {
-        return new IntegerListCell(value, this);
-    }
-
-    public static class IntegerListCell extends AbstractListListEntry.AbstractListCell<Integer, IntegerListCell, IntegerListListEntry> {
-
-        private TextFieldWidget widget;
+    public static class IntegerListCell extends AbstractTextFieldListListEntry.AbstractTextFieldListCell<Integer, IntegerListCell, IntegerListListEntry> {
 
         public IntegerListCell(Integer value, IntegerListListEntry listListEntry) {
             super(value, listListEntry);
+        }
 
+        @Nullable
+        @Override
+        protected Integer substituteDefault(@Nullable Integer value) {
             if (value == null)
-                value = 0;
-            Integer finalValue = value;
-
-            this.setErrorSupplier(() -> Optional.ofNullable(listListEntry.cellErrorSupplier).flatMap(fn -> fn.apply(this.getValue())));
-            widget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 100, 18, "");
-            widget.setTextPredicate(s -> s.chars().allMatch(c -> Character.isDigit(c) || c == '-'));
-            widget.setMaxLength(Integer.MAX_VALUE);
-            widget.setHasBorder(false);
-            widget.setText(value.toString());
-            widget.setChangedListener(s -> {
-                widget.setEditableColor(getPreferredTextColor());
-                if (!Objects.equals(s, finalValue.toString())) {
-                    this.listListEntry.getScreen().setEdited(true, this.listListEntry.isRequiresRestart());
-                }
-            });
+                return 0;
+            else
+                return value;
+        }
+
+        @Override
+        protected boolean isValidText(@NotNull String text) {
+            return text.chars().allMatch(c -> Character.isDigit(c) || c == '-');
         }
 
         public Integer getValue() {
@@ -117,28 +98,6 @@ public class IntegerListListEntry extends AbstractListListEntry<Integer, Integer
             }
             return Optional.empty();
         }
-
-        @Override
-        public int getCellHeight() {
-            return 20;
-        }
-
-        @Override
-        public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
-            widget.setWidth(entryWidth - 12);
-            widget.x = x;
-            widget.y = y + 1;
-            widget.setEditable(listListEntry.isEditable());
-            widget.render(mouseX, mouseY, delta);
-            if (isSelected && listListEntry.isEditable())
-                fill(x, y + 12, x + entryWidth - 12, y + 13, getConfigError().isPresent() ? 0xffff5555 : 0xffe0e0e0);
-        }
-
-        @Override
-        public List<? extends Element> children() {
-            return Collections.singletonList(widget);
-        }
-
     }
 
 }

+ 16 - 57
src/main/java/me/shedaniel/clothconfig2/gui/entries/LongListListEntry.java

@@ -1,21 +1,17 @@
 package me.shedaniel.clothconfig2.gui.entries;
 
-import net.minecraft.client.MinecraftClient;
-import net.minecraft.client.gui.Element;
-import net.minecraft.client.gui.widget.TextFieldWidget;
 import net.minecraft.client.resource.language.I18n;
 import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
-import java.util.Collections;
 import java.util.List;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
-import java.util.stream.Collectors;
 
 @ApiStatus.Internal
-public class LongListListEntry extends AbstractListListEntry<Long, LongListListEntry.LongListCell, LongListListEntry> {
+public class LongListListEntry extends AbstractTextFieldListListEntry<Long, LongListListEntry.LongListCell, LongListListEntry> {
 
     private long minimum, maximum;
 
@@ -35,11 +31,6 @@ public class LongListListEntry extends AbstractListListEntry<Long, LongListListE
         this.maximum = Long.MAX_VALUE;
     }
 
-    @Override
-    public List<Long> getValue() {
-        return cells.stream().map(LongListCell::getValue).collect(Collectors.toList());
-    }
-
     public LongListListEntry setMaximum(long maximum) {
         this.maximum = maximum;
         return this;
@@ -55,34 +46,24 @@ public class LongListListEntry extends AbstractListListEntry<Long, LongListListE
         return this;
     }
 
-    @Override
-    protected LongListCell getFromValue(Long value) {
-        return new LongListCell(value, this);
-    }
-
-    public static class LongListCell extends AbstractListListEntry.AbstractListCell<Long, LongListCell, LongListListEntry> {
-
-        private TextFieldWidget widget;
+    public static class LongListCell extends AbstractTextFieldListListEntry.AbstractTextFieldListCell<Long, LongListCell, LongListListEntry> {
 
         public LongListCell(Long value, LongListListEntry listListEntry) {
             super(value, listListEntry);
+        }
 
+        @Nullable
+        @Override
+        protected Long substituteDefault(@Nullable Long value) {
             if (value == null)
-                value = 0L;
-            Long finalValue = value;
-
-            this.setErrorSupplier(() -> Optional.ofNullable(listListEntry.cellErrorSupplier).flatMap(fn -> fn.apply(this.getValue())));
-            widget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 100, 18, "");
-            widget.setTextPredicate(s -> s.chars().allMatch(c -> Character.isDigit(c) || c == '-'));
-            widget.setMaxLength(Integer.MAX_VALUE);
-            widget.setHasBorder(false);
-            widget.setText(value.toString());
-            widget.setChangedListener(s -> {
-                widget.setEditableColor(getPreferredTextColor());
-                if (!Objects.equals(s, finalValue.toString())) {
-                    this.listListEntry.getScreen().setEdited(true, this.listListEntry.isRequiresRestart());
-                }
-            });
+                return 0L;
+            else
+                return value;
+        }
+
+        @Override
+        protected boolean isValidText(@NotNull String text) {
+            return text.chars().allMatch(c -> Character.isDigit(c) || c == '-');
         }
 
         public Long getValue() {
@@ -106,28 +87,6 @@ public class LongListListEntry extends AbstractListListEntry<Long, LongListListE
             }
             return Optional.empty();
         }
-
-        @Override
-        public int getCellHeight() {
-            return 20;
-        }
-
-        @Override
-        public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
-            widget.setWidth(entryWidth - 12);
-            widget.x = x;
-            widget.y = y + 1;
-            widget.setEditable(listListEntry.isEditable());
-            widget.render(mouseX, mouseY, delta);
-            if (isSelected && listListEntry.isEditable())
-                fill(x, y + 12, x + entryWidth - 12, y + 13, getConfigError().isPresent() ? 0xffff5555 : 0xffe0e0e0);
-        }
-
-        @Override
-        public List<? extends Element> children() {
-            return Collections.singletonList(widget);
-        }
-
     }
 
 }

+ 15 - 54
src/main/java/me/shedaniel/clothconfig2/gui/entries/StringListListEntry.java

@@ -1,20 +1,16 @@
 package me.shedaniel.clothconfig2.gui.entries;
 
-import net.minecraft.client.MinecraftClient;
-import net.minecraft.client.gui.Element;
-import net.minecraft.client.gui.widget.TextFieldWidget;
 import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
-import java.util.Collections;
 import java.util.List;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
-import java.util.stream.Collectors;
 
 @ApiStatus.Internal
-public class StringListListEntry extends AbstractListListEntry<String, StringListListEntry.StringListCell, StringListListEntry> {
+public class StringListListEntry extends AbstractTextFieldListListEntry<String, StringListListEntry.StringListCell, StringListListEntry> {
 
     @Deprecated
     public StringListListEntry(String fieldName, List<String> value, boolean defaultExpanded, Supplier<Optional<String[]>> tooltipSupplier, Consumer<List<String>> saveConsumer, Supplier<List<String>> defaultValue, String resetButtonKey) {
@@ -30,43 +26,29 @@ public class StringListListEntry extends AbstractListListEntry<String, StringLis
         super(fieldName, value, defaultExpanded, tooltipSupplier, saveConsumer, defaultValue, resetButtonKey, requiresRestart, deleteButtonEnabled, insertInFront, StringListCell::new);
     }
 
-    @Override
-    public List<String> getValue() {
-        return cells.stream().map(cell -> cell.widget.getText()).collect(Collectors.toList());
-    }
-
     @Override
     public StringListListEntry self() {
         return this;
     }
 
-    @Override
-    protected StringListCell getFromValue(String value) {
-        return new StringListCell(value, this);
-    }
-
-    public static class StringListCell extends AbstractListListEntry.AbstractListCell<String, StringListCell, StringListListEntry> {
-
-        private TextFieldWidget widget;
+    public static class StringListCell extends AbstractTextFieldListListEntry.AbstractTextFieldListCell<String, StringListCell, StringListListEntry> {
 
         public StringListCell(String value, StringListListEntry listListEntry) {
             super(value, listListEntry);
+        }
 
+        @Nullable
+        @Override
+        protected String substituteDefault(@Nullable String value) {
             if (value == null)
-                value = "";
-            String finalValue = value;
+                return "";
+            else
+                return value;
+        }
 
-            this.setErrorSupplier(() -> Optional.ofNullable(listListEntry.cellErrorSupplier).flatMap(fn -> fn.apply(this.getValue())));
-            widget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 100, 18, "");
-            widget.setMaxLength(Integer.MAX_VALUE);
-            widget.setHasBorder(false);
-            widget.setText(value);
-            widget.setChangedListener(s -> {
-                widget.setEditableColor(getPreferredTextColor());
-                if (!Objects.equals(s, finalValue)) {
-                    this.listListEntry.getScreen().setEdited(true, this.listListEntry.isRequiresRestart());
-                }
-            });
+        @Override
+        protected boolean isValidText(@NotNull String text) {
+            return true;
         }
 
         @Override
@@ -79,27 +61,6 @@ public class StringListListEntry extends AbstractListListEntry<String, StringLis
             return Optional.empty();
         }
 
-        @Override
-        public int getCellHeight() {
-            return 20;
-        }
-
-        @Override
-        public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
-            widget.setWidth(entryWidth - 12);
-            widget.x = x;
-            widget.y = y + 1;
-            widget.setEditable(listListEntry.isEditable());
-            widget.render(mouseX, mouseY, delta);
-            if (isSelected && listListEntry.isEditable())
-                fill(x, y + 12, x + entryWidth - 12, y + 13, getConfigError().isPresent() ? 0xffff5555 : 0xffe0e0e0);
-        }
-
-        @Override
-        public List<? extends Element> children() {
-            return Collections.singletonList(widget);
-        }
-
     }
 
 }