Jelajahi Sumber

disable suggestion mode, fix #44, close #43

Signed-off-by: shedaniel <daniel@shedaniel.me>
shedaniel 5 tahun lalu
induk
melakukan
f298d2c3e9

+ 1 - 1
gradle.properties

@@ -3,6 +3,6 @@ minecraft_version=20w18a
 yarn_mappings=20w18a+build.1
 loader_version=0.8.2+build.194
 fabric_version=0.7.1+build.331-1.16
-mod_version=4.0.9-unstable
+mod_version=4.1.0-unstable
 modmenu_version=1.11.2+build.6
 nec_version=1.2.3+1.15.1

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

@@ -58,7 +58,7 @@ public class ColorEntry extends TextFieldListEntry<Integer> {
     
     @Override
     protected void textFieldPreRender(TextFieldWidget widget) {
-        if (!getError().isPresent()) {
+        if (!getConfigError().isPresent()) {
             widget.setEditableColor(14737632);
         } else {
             widget.setEditableColor(16733525);

+ 46 - 15
src/main/java/me/shedaniel/clothconfig2/gui/entries/DropdownBoxEntry.java

@@ -46,6 +46,7 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
     protected SelectionElement<T> selectionElement;
     @NotNull private Supplier<T> defaultValue;
     @Nullable private Consumer<T> saveConsumer;
+    private boolean suggestionMode = true;
     
     @ApiStatus.Internal
     @Deprecated
@@ -82,6 +83,14 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
         selectionElement.render(matrices, mouseX, mouseY, delta);
     }
     
+    public boolean isSuggestionMode() {
+        return suggestionMode;
+    }
+    
+    public void setSuggestionMode(boolean suggestionMode) {
+        this.suggestionMode = suggestionMode;
+    }
+    
     @Override
     public void updateSelected(boolean isSelected) {
         selectionElement.topRenderer.isSelected = isSelected;
@@ -241,6 +250,10 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
             return isSelected && this.getEntry().getFocused() == this.getEntry().selectionElement;
         }
         
+        public final boolean isSuggestionMode() {
+            return entry.isSuggestionMode();
+        }
+        
         @Override
         public abstract List<SelectionCellElement<R>> children();
     }
@@ -288,18 +301,23 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
         }
         
         public void search() {
-            currentElements.clear();
-            String keyword = this.lastSearchKeyword.getString().toLowerCase();
-            for (SelectionCellElement<R> cell : cells) {
-                Text key = cell.getSearchKey();
-                if (key == null || key.getString().toLowerCase().contains(keyword))
-                    currentElements.add(cell);
-            }
-            if (!keyword.isEmpty()) {
-                Comparator<SelectionCellElement<?>> c = Comparator.comparingDouble(i -> i.getSearchKey() == null ? Double.MAX_VALUE : similarity(i.getSearchKey().getString(), keyword));
-                currentElements.sort(c.reversed());
+            if (isSuggestionMode()) {
+                currentElements.clear();
+                String keyword = this.lastSearchKeyword.getString().toLowerCase();
+                for (SelectionCellElement<R> cell : cells) {
+                    Text key = cell.getSearchKey();
+                    if (key == null || key.getString().toLowerCase().contains(keyword))
+                        currentElements.add(cell);
+                }
+                if (!keyword.isEmpty()) {
+                    Comparator<SelectionCellElement<?>> c = Comparator.comparingDouble(i -> i.getSearchKey() == null ? Double.MAX_VALUE : similarity(i.getSearchKey().getString(), keyword));
+                    currentElements.sort(c.reversed());
+                }
+                scrollTo(0, false);
+            } else {
+                currentElements.clear();
+                currentElements.addAll(cells);
             }
-            scrollTo(0, false);
         }
         
         protected int editDistance(String s1, String s2) {
@@ -538,7 +556,7 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
     }
     
     public static abstract class SelectionCellElement<R> extends AbstractParentElement {
-        @Deprecated @NotNull private DropdownBoxEntry<R> entry;
+        @SuppressWarnings("NotNullFieldNotInitialized") @Deprecated @NotNull private DropdownBoxEntry<R> entry;
         
         @NotNull
         public final DropdownBoxEntry<R> getEntry() {
@@ -641,11 +659,19 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
         public final boolean hasConfigError() {
             return getConfigError().isPresent();
         }
+    
+        public final boolean hasError() {
+            return getError().isPresent();
+        }
         
         public final int getPreferredTextColor() {
             return getConfigError().isPresent() ? 16733525 : 16777215;
         }
         
+        public final boolean isSuggestionMode() {
+            return getParent().isSuggestionMode();
+        }
+        
         public void selectFirstRecommendation() {
             List<SelectionCellElement<R>> children = getParent().selectionElement.menu.children();
             for (SelectionCellElement<R> child : children) {
@@ -673,7 +699,7 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
             textFieldWidget = new TextFieldWidget(MinecraftClient.getInstance().textRenderer, 0, 0, 148, 18, NarratorManager.EMPTY) {
                 @Override
                 public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
-                    setFocused(isSelected && DefaultSelectionTopCellElement.this.getParent().getFocused() == DefaultSelectionTopCellElement.this.getParent().selectionElement && DefaultSelectionTopCellElement.this.getParent().selectionElement.getFocused() == DefaultSelectionTopCellElement.this && DefaultSelectionTopCellElement.this.getFocused() == this);
+                    setFocused(isSuggestionMode() && isSelected && DefaultSelectionTopCellElement.this.getParent().getFocused() == DefaultSelectionTopCellElement.this.getParent().selectionElement && DefaultSelectionTopCellElement.this.getParent().selectionElement.getFocused() == DefaultSelectionTopCellElement.this && DefaultSelectionTopCellElement.this.getFocused() == this);
                     super.render(matrices, mouseX, mouseY, delta);
                 }
                 
@@ -683,7 +709,12 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
                         DefaultSelectionTopCellElement.this.selectFirstRecommendation();
                         return true;
                     }
-                    return super.keyPressed(int_1, int_2, int_3);
+                    return isSuggestionMode() && super.keyPressed(int_1, int_2, int_3);
+                }
+                
+                @Override
+                public boolean charTyped(char chr, int keyCode) {
+                    return isSuggestionMode() && super.charTyped(chr, keyCode);
                 }
             };
             textFieldWidget.setHasBorder(false);
@@ -707,7 +738,7 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
         
         @Override
         public R getValue() {
-            if (hasConfigError())
+            if (hasError())
                 return value;
             return toObjectFunction.apply(textFieldWidget.getText());
         }

+ 5 - 3
src/main/java/me/shedaniel/clothconfig2/gui/entries/SubCategoryListEntry.java

@@ -159,12 +159,14 @@ public class SubCategoryListEntry extends TooltipListEntry<List<AbstractConfigLi
     @Override
     public Optional<Text> getError() {
         Text error = null;
-        for (AbstractConfigListEntry<?> entry : entries)
-            if (entry.getError().isPresent()) {
+        for (AbstractConfigListEntry<?> entry : entries) {
+            Optional<Text> configError = entry.getConfigError();
+            if (configError.isPresent()) {
                 if (error != null)
                     return Optional.ofNullable(new TranslatableText("text.cloth-config.multi_error"));
-                return entry.getError();
+                return configError;
             }
+        }
         return Optional.ofNullable(error);
     }
     

+ 11 - 0
src/main/java/me/shedaniel/clothconfig2/impl/builders/DropdownMenuBuilder.java

@@ -34,6 +34,7 @@ public class DropdownMenuBuilder<T> extends FieldBuilder<T, DropdownBoxEntry<T>>
     protected Function<T, Optional<Text[]>> tooltipSupplier = str -> Optional.empty();
     protected Consumer<T> saveConsumer = null;
     protected Iterable<T> selections = Collections.emptyList();
+    protected boolean suggestionMode = true;
     
     public DropdownMenuBuilder(Text resetButtonKey, Text fieldNameKey, SelectionTopCellElement<T> topCellElement, SelectionCellCreator<T> cellCreator) {
         super(resetButtonKey, fieldNameKey);
@@ -91,6 +92,15 @@ public class DropdownMenuBuilder<T> extends FieldBuilder<T, DropdownBoxEntry<T>>
         return this;
     }
     
+    public DropdownMenuBuilder<T> setSuggestionMode(boolean suggestionMode) {
+        this.suggestionMode = suggestionMode;
+        return this;
+    }
+    
+    public boolean isSuggestionMode() {
+        return suggestionMode;
+    }
+    
     @NotNull
     @Override
     public DropdownBoxEntry<T> build() {
@@ -98,6 +108,7 @@ public class DropdownMenuBuilder<T> extends FieldBuilder<T, DropdownBoxEntry<T>>
         entry.setTooltipSupplier(() -> tooltipSupplier.apply(entry.getValue()));
         if (errorSupplier != null)
             entry.setErrorSupplier(() -> errorSupplier.apply(entry.getValue()));
+        entry.setSuggestionMode(suggestionMode);
         return entry;
     }