Browse Source

Add option for tooltips to category buttons.

Haven King 4 years ago
parent
commit
7fe32d6f41

+ 8 - 0
src/main/java/me/shedaniel/clothconfig2/api/ConfigCategory.java

@@ -4,8 +4,11 @@ import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.text.Text;
 import net.minecraft.util.Identifier;
+import org.jetbrains.annotations.Nullable;
 
 import java.util.List;
+import java.util.Optional;
+import java.util.function.Supplier;
 
 @Environment(EnvType.CLIENT)
 public interface ConfigCategory {
@@ -18,6 +21,11 @@ public interface ConfigCategory {
     ConfigCategory addEntry(AbstractConfigListEntry entry);
     
     ConfigCategory setCategoryBackground(Identifier identifier);
+
+    @Nullable
+    Supplier<Optional<Text[]>> getTooltipSupplier();
+
+    ConfigCategory setTooltipSupplier(@Nullable Supplier<Optional<Text[]>> tooltipSupplier);
     
     void removeCategory();
     

+ 5 - 2
src/main/java/me/shedaniel/clothconfig2/gui/ClothConfigScreen.java

@@ -60,9 +60,10 @@ public class ClothConfigScreen extends AbstractTabbedConfigScreen {
     private Rectangle tabsBounds, tabsLeftBounds, tabsRightBounds;
     private double tabsMaximumScrolled = -1d;
     private final List<ClothConfigTabButton> tabButtons = Lists.newArrayList();
+    private final Map<Text, ConfigCategory> categoryMap;
     
     @ApiStatus.Internal
-    public ClothConfigScreen(Screen parent, Text title, Map<Text, List<Object>> entriesMap, Identifier backgroundLocation) {
+    public ClothConfigScreen(Screen parent, Text title, Map<Text, List<Object>> entriesMap, Map<Text, ConfigCategory> categoryMap, Identifier backgroundLocation) {
         super(parent, title, backgroundLocation);
         entriesMap.forEach((categoryName, list) -> {
             List<AbstractConfigEntry<?>> entries = Lists.newArrayList();
@@ -78,7 +79,9 @@ public class ClothConfigScreen extends AbstractTabbedConfigScreen {
             }
             categorizedEntries.put(categoryName, entries);
         });
+
         this.tabs = categorizedEntries.keySet().stream().map(s -> new Pair<>(s, MinecraftClient.getInstance().textRenderer.getWidth(s) + 8)).collect(Collectors.toList());
+        this.categoryMap = categoryMap;
     }
     
     @Override
@@ -166,7 +169,7 @@ public class ClothConfigScreen extends AbstractTabbedConfigScreen {
             });
             int j = 0;
             for (Pair<Text, Integer> tab : tabs) {
-                tabButtons.add(new ClothConfigTabButton(this, j, -100, 43, tab.getRight(), 20, tab.getLeft()));
+                tabButtons.add(new ClothConfigTabButton(this, j, -100, 43, tab.getRight(), 20, tab.getLeft(), this.categoryMap.get(tab.getLeft()).getTooltipSupplier()));
                 j++;
             }
             children.addAll(tabButtons);

+ 31 - 1
src/main/java/me/shedaniel/clothconfig2/gui/ClothConfigTabButton.java

@@ -1,22 +1,34 @@
 package me.shedaniel.clothconfig2.gui;
 
+import me.shedaniel.clothconfig2.api.Tooltip;
+import me.shedaniel.math.Point;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.client.MinecraftClient;
 import net.minecraft.client.gui.widget.AbstractPressableButtonWidget;
 import net.minecraft.client.util.math.MatrixStack;
 import net.minecraft.text.Text;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Optional;
+import java.util.function.Supplier;
 
 @Environment(EnvType.CLIENT)
 public class ClothConfigTabButton extends AbstractPressableButtonWidget {
     
     private final int index;
     private final ClothConfigScreen screen;
+    @Nullable private final Supplier<Optional<Text[]>> tooltipSupplier;
     
-    public ClothConfigTabButton(ClothConfigScreen screen, int index, int int_1, int int_2, int int_3, int int_4, Text string_1) {
+    public ClothConfigTabButton(ClothConfigScreen screen, int index, int int_1, int int_2, int int_3, int int_4, Text string_1, Supplier<Optional<Text[]>> tooltipSupplier) {
         super(int_1, int_2, int_3, int_4, string_1);
         this.index = index;
         this.screen = screen;
+        this.tooltipSupplier = tooltipSupplier;
+    }
+
+    public ClothConfigTabButton(ClothConfigScreen screen, int index, int int_1, int int_2, int int_3, int int_4, Text string_1) {
+        this(screen, index, int_1, int_2, int_3, int_4, string_1, null);
     }
     
     @Override
@@ -30,6 +42,12 @@ public class ClothConfigTabButton extends AbstractPressableButtonWidget {
     public void render(MatrixStack matrices, int int_1, int int_2, float float_1) {
         active = index != screen.selectedCategoryIndex;
         super.render(matrices, int_1, int_2, float_1);
+
+        if (isMouseOver(int_1, int_2)) {
+            Optional<Text[]> tooltip = getTooltip();
+            if (tooltip.isPresent() && tooltip.get().length > 0)
+                screen.addTooltip(Tooltip.of(new Point(int_1, int_2), tooltip.get()));
+        }
     }
     
     @Override
@@ -41,4 +59,16 @@ public class ClothConfigTabButton extends AbstractPressableButtonWidget {
     public boolean isMouseOver(double double_1, double double_2) {
         return this.active && this.visible && double_1 >= this.x && double_2 >= this.y && double_1 < this.x + this.width && double_2 < this.y + this.height && double_1 >= 20 && double_1 < screen.width - 20;
     }
+
+    public Optional<Text[]> getTooltip() {
+        if (tooltipSupplier != null)
+            return tooltipSupplier.get();
+        return Optional.empty();
+    }
+
+    @Nullable
+    public Supplier<Optional<Text[]>> getTooltipSupplier() {
+        return tooltipSupplier;
+    }
+
 }

+ 6 - 9
src/main/java/me/shedaniel/clothconfig2/impl/ConfigBuilderImpl.java

@@ -40,6 +40,7 @@ public class ConfigBuilderImpl implements ConfigBuilder {
     private Consumer<Screen> afterInitConsumer = screen -> {};
     private final Map<Text, Identifier> categoryBackground = Maps.newHashMap();
     private final Map<Text, List<Object>> dataMap = Maps.newLinkedHashMap();
+    private final Map<Text, ConfigCategory> categoryMap = Maps.newHashMap();
     private Text fallbackCategory = null;
     private boolean alwaysShowTabs = false;
     
@@ -122,20 +123,16 @@ public class ConfigBuilderImpl implements ConfigBuilder {
     
     @Override
     public ConfigCategory getOrCreateCategory(Text categoryKey) {
-        if (dataMap.containsKey(categoryKey))
-            return new ConfigCategoryImpl(categoryKey, identifier -> {
-                if (transparentBackground)
-                    throw new IllegalStateException("Cannot set category background if screen is using transparent background.");
-                categoryBackground.put(categoryKey, identifier);
-            }, () -> dataMap.get(categoryKey), () -> removeCategory(categoryKey));
+        if (categoryMap.containsKey(categoryKey))
+            return categoryMap.get(categoryKey);
         dataMap.put(categoryKey, Lists.newArrayList());
         if (fallbackCategory == null)
             fallbackCategory = categoryKey;
-        return new ConfigCategoryImpl(categoryKey, identifier -> {
+        return categoryMap.computeIfAbsent(categoryKey, key -> new ConfigCategoryImpl(categoryKey, identifier -> {
             if (transparentBackground)
                 throw new IllegalStateException("Cannot set category background if screen is using transparent background.");
             categoryBackground.put(categoryKey, identifier);
-        }, () -> dataMap.get(categoryKey), () -> removeCategory(categoryKey));
+        }, () -> dataMap.get(categoryKey), () -> removeCategory(categoryKey)));
     }
     
     @Override
@@ -224,7 +221,7 @@ public class ConfigBuilderImpl implements ConfigBuilder {
         if (globalized) {
             screen = new GlobalizedClothConfigScreen(parent, title, dataMap, defaultBackground);
         } else {
-            screen = new ClothConfigScreen(parent, title, dataMap, defaultBackground);
+            screen = new ClothConfigScreen(parent, title, dataMap, categoryMap, defaultBackground);
         }
         screen.setSavingRunnable(savingRunnable);
         screen.setEditable(editable);

+ 15 - 2
src/main/java/me/shedaniel/clothconfig2/impl/ConfigCategoryImpl.java

@@ -6,8 +6,10 @@ import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.minecraft.text.Text;
 import net.minecraft.util.Identifier;
+import org.jetbrains.annotations.Nullable;
 
 import java.util.List;
+import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 
@@ -18,7 +20,8 @@ public class ConfigCategoryImpl implements ConfigCategory {
     private final Consumer<Identifier> backgroundConsumer;
     private final Runnable destroyCategory;
     private final Text categoryKey;
-    
+    @Nullable private Supplier<Optional<Text[]>> tooltipSupplier;
+
     ConfigCategoryImpl(Text categoryKey, Consumer<Identifier> backgroundConsumer, Supplier<List<Object>> listSupplier, Runnable destroyCategory) {
         this.listSupplier = listSupplier;
         this.backgroundConsumer = backgroundConsumer;
@@ -52,5 +55,15 @@ public class ConfigCategoryImpl implements ConfigCategory {
     public void removeCategory() {
         destroyCategory.run();
     }
-    
+
+    @Nullable
+    public Supplier<Optional<Text[]>> getTooltipSupplier() {
+        return tooltipSupplier;
+    }
+
+    public ConfigCategory setTooltipSupplier(@Nullable Supplier<Optional<Text[]>> tooltipSupplier) {
+        this.tooltipSupplier = tooltipSupplier;
+        return this;
+    }
+
 }