Skip to content

Commit

Permalink
make config category collapsible (#281)
Browse files Browse the repository at this point in the history
* make config category collapsible

* remove commented codes

* save collapse state

(cherry picked from commit 6255ca1)
  • Loading branch information
deirn committed Jul 21, 2024
1 parent 069f363 commit ee97595
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 149 deletions.
17 changes: 10 additions & 7 deletions src/main/java/mcp/mobius/waila/gui/screen/ConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ public void init() {
options = getOptions();
}

var searchBox = options.getSearchBox();
if (searchBox.isActive()) addWidget(searchBox);
options.init();

if (options.enableSearchBox) {
var searchBox = options.getSearchBox();
addWidget(searchBox);
setInitialFocus(searchBox);
}

addWidget(options);
options.init();

if (saver != null && canceller != null) {
addRenderableWidget(createButton(width / 2 - 102, height - 25, 100, 20, CommonComponents.GUI_DONE, w -> {
Expand All @@ -80,8 +84,6 @@ public void init() {
}
}));
}

if (searchBox.isActive()) setInitialFocus(searchBox);
}

protected void renderForeground(GuiGraphics ctx, int rowLeft, int rowWidth, int mouseX, int mouseY, float partialTicks) {
Expand All @@ -99,8 +101,9 @@ public void render(@NotNull GuiGraphics ctx, int mouseX, int mouseY, float parti

options.render(ctx, mouseX, mouseY, partialTicks);

var searchBox = options.getSearchBox();
if (searchBox.isActive()) options.getSearchBox().render(ctx, mouseX, mouseY, partialTicks);
if (options.enableSearchBox) {
options.getSearchBox().render(ctx, mouseX, mouseY, partialTicks);
}

renderForeground(ctx, options.getRowLeft(), options.getRowWidth(), mouseX, mouseY, partialTicks);

Expand Down
89 changes: 38 additions & 51 deletions src/main/java/mcp/mobius/waila/gui/screen/PluginConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.util.Map;
import java.util.function.Consumer;

import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import lol.bai.badpackets.api.PacketSender;
import mcp.mobius.waila.buildconst.Tl;
import mcp.mobius.waila.config.ConfigEntry;
Expand All @@ -21,7 +19,6 @@
import mcp.mobius.waila.gui.widget.value.IntInputValue;
import mcp.mobius.waila.network.common.VersionPayload;
import mcp.mobius.waila.registry.Registrar;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n;
Expand Down Expand Up @@ -61,53 +58,47 @@ public ConfigListWidget getOptions() {
var keys = PluginConfig.getAllKeys(namespace);
if (keys.isEmpty()) continue;

options.with(new ButtonEntry(namespaceTlKey, 100, 20, w -> minecraft.setScreen(new ConfigScreen(PluginConfigScreen.this,
Component.translatable(Tl.Gui.Plugin.SETTINGS).append(" > ").withStyle(ChatFormatting.DARK_GRAY)
.append(Component.translatable(namespaceTlKey).withStyle(ChatFormatting.WHITE))) {
@Override
public ConfigListWidget getOptions() {
var options = new ConfigListWidget(this, minecraft, width, height, 32, height - 32, 26);
Object2IntMap<String> categories = new Object2IntLinkedOpenHashMap<>();
categories.put(NO_CATEGORY, 0);

for (var key : keys) {
var entry = PluginConfig.getEntry(key);
if (entry.isAlias()) continue;

var path = key.getPath();
var category = NO_CATEGORY;

if (path.contains(".")) {
var c = path.split("[.]", 2)[0];
var categoryTlKey = namespaceTlKey + "." + c;

if (I18n.exists(categoryTlKey)) {
category = c;

if (!categories.containsKey(category)) {
options.with(new CategoryEntry(categoryTlKey));
categories.put(category, options.children().size());
}
}
}
var namespaceCategory = new CategoryEntry(namespaceTlKey);
options.with(namespaceCategory);

var index = categories.getInt(category);
var entryTlKey = namespaceTlKey + "." + path;
var categories = new HashMap<String, CategoryEntry>();
categories.put(NO_CATEGORY, namespaceCategory);

for (var e : categories.object2IntEntrySet()) {
if (e.getIntValue() >= index) {
e.setValue(e.getIntValue() + 1);
}
}
for (var key : keys) {
var entry = PluginConfig.getEntry(key);
if (entry.isAlias()) continue;

if (entry.getType().equals(ConfigEntry.PATH)) {
options.with(index, new ButtonEntry(entryTlKey, Tl.Config.OPEN_FILE, 100, 20, b ->
Util.getPlatform().openFile(((Path) entry.getDefaultValue()).toFile())));
continue;
var path = key.getPath();
var categoryKey = NO_CATEGORY;

if (path.contains(".")) {
var c = path.split("[.]", 2)[0];
var categoryTlKey = namespaceTlKey + "." + c;

if (I18n.exists(categoryTlKey)) {
categoryKey = c;

if (!categories.containsKey(categoryKey)) {
var category = new CategoryEntry(categoryTlKey);
namespaceCategory.with(category);
categories.put(categoryKey, category);
}
}
}

var category = categories.get(categoryKey);
var entryTlKey = namespaceTlKey + "." + path;

if (entry.getType().equals(ConfigEntry.PATH)) {
var button = new ButtonEntry(entryTlKey, Tl.Config.OPEN_FILE, 100, 20, b ->
Util.getPlatform().openFile(((Path) entry.getDefaultValue()).toFile()));

category.with(button);
continue;
}

var value = ENTRY_TO_VALUE.get(entry.getType()).create(key, entryTlKey, entry.getLocalValue(), entry.getDefaultValue(), entry::setLocalValue);
value.setId(key.toString());
var value = ENTRY_TO_VALUE.get(entry.getType()).create(key, entryTlKey, entry.getLocalValue(), entry.getDefaultValue(), entry::setLocalValue);
value.setId(key.toString());

if (entry.blocksClientEdit() && minecraft.getCurrentServer() != null) {
if (entry.getServerValue() == null) {
Expand All @@ -123,12 +114,8 @@ public ConfigListWidget getOptions() {
}
}

options.with(index, value);
}

return options;
}
})));
category.with(value);
}
}
return options;
}
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/mcp/mobius/waila/gui/screen/ThemeEditorScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.toasts.SystemToast;
import net.minecraft.client.gui.screens.ConfirmScreen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -97,7 +96,7 @@ public void setValue(String value) {
if (options.save(true)) {
super.setValue(value);
type = Registrar.get().themeTypes.get(new ResourceLocation(value));
options.children().removeIf(it -> it.category.equals(I18n.get(Tl.Config.OverlayThemeEditor.ATTRIBUTES)));
options.children().removeIf(it -> it.category == typeVal.category);
addTypeProperties(options);
options.init();
options.setFocused(this);
Expand All @@ -123,21 +122,20 @@ public void setValue(String value) {

if (edit) {
options
.with(new CategoryEntry(Tl.Config.OverlayThemeEditor.DELETE))
.with(new ButtonEntry(Tl.Config.OverlayThemeEditor.DELETE, 100, 20, button -> minecraft.setScreen(new ConfirmScreen(delete -> {
if (delete) {
parent.removeTheme(template.id);
minecraft.setScreen(parent);
} else {
minecraft.setScreen(this);
}
}, Component.translatable(Tl.Config.OverlayThemeEditor.DELETE_PROMPT, template.id), CommonComponents.EMPTY))));
.with(new CategoryEntry(Tl.Config.OverlayThemeEditor.DELETE)
.with(new ButtonEntry(Tl.Config.OverlayThemeEditor.DELETE, 100, 20, button -> minecraft.setScreen(new ConfirmScreen(delete -> {
if (delete) {
parent.removeTheme(template.id);
minecraft.setScreen(parent);
} else {
minecraft.setScreen(this);
}
}, Component.translatable(Tl.Config.OverlayThemeEditor.DELETE_PROMPT, template.id), CommonComponents.EMPTY)))));
}

addTypeProperties(options);

options.getSearchBox().active = false;
options.getSearchBox().setEditable(false);
options.enableSearchBox = false;
return options;
}

Expand Down Expand Up @@ -169,10 +167,9 @@ private void addTypeProperties(ConfigListWidget options) {
}

value.setId(key);
value.category = category.category;
attrValues.put(key, TypeUtil.uncheckedCast(value));

options.add(options.children().size() - (edit ? 2 : 0), value);
category.with(value);
});
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/mcp/mobius/waila/gui/screen/WailaConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected void renderForeground(GuiGraphics ctx, int rowLeft, int rowWidth, int
@Override
public ConfigListWidget getOptions() {
var options = new ConfigListWidget(this, minecraft, width, height, 42, height - 32, 26, Waila.CONFIG::save);
options.with(new CategoryEntry(Tl.Config.GENERAL))
options.with(new CategoryEntry(Tl.Config.GENERAL)
.with(new BooleanValue(Tl.Config.DISPLAY_TOOLTIP,
get().getGeneral().isDisplayTooltip(),
defaultConfig.getGeneral().isDisplayTooltip(),
Expand Down Expand Up @@ -176,9 +176,9 @@ public ConfigListWidget getOptions() {
val -> get().getGeneral().setRateLimit(Math.max(val, 250)),
InputValue.POSITIVE_INTEGER))
.with(new ButtonEntry(Tl.Config.BLACKLIST, Tl.Config.BLACKLIST_OPEN, 100, 20, w ->
Util.getPlatform().openFile(Waila.BLACKLIST_CONFIG.getPath().toFile())));
Util.getPlatform().openFile(Waila.BLACKLIST_CONFIG.getPath().toFile()))));

options.with(new CategoryEntry(Tl.Config.OVERLAY))
options.with(new CategoryEntry(Tl.Config.OVERLAY)
.with(fpsVal = Util.make(new InputValue<>(Tl.Config.OVERLAY_FPS,
get().getOverlay().getFps(),
defaultConfig.getOverlay().getFps(),
Expand Down Expand Up @@ -229,9 +229,9 @@ public ConfigListWidget getOptions() {
defaultConfig.getOverlay().getColor().getBackgroundAlpha(),
val -> get().getOverlay().getColor().setBackgroundAlpha(Mth.clamp(val, 0x00, 0xFF)),
InputValue.POSITIVE_INTEGER))
.with(themeIdVal = new ThemeValue());
.with(themeIdVal = new ThemeValue()));

options.with(new CategoryEntry(Tl.Config.FORMATTING))
options.with(new CategoryEntry(Tl.Config.FORMATTING)
.with(modNameFormatVal = new InputValue<>(Tl.Config.FORMAT_MOD_NAME,
get().getFormatter().getModName(),
defaultConfig.getFormatter().getModName(),
Expand All @@ -256,14 +256,14 @@ public ConfigListWidget getOptions() {
get().getFormatter().getRegistryName(),
defaultConfig.getFormatter().getRegistryName(),
val -> get().getFormatter().setRegistryName(!val.contains("%s") ? get().getFormatter().getRegistryName() : val),
InputValue.ANY));
InputValue.ANY)));

options.with(new CategoryEntry(Tl.Config.KEYBINDS))
options.with(new CategoryEntry(Tl.Config.KEYBINDS)
.with(new KeyBindValue(WailaClient.keyOpenConfig))
.with(new KeyBindValue(WailaClient.keyShowOverlay))
.with(new KeyBindValue(WailaClient.keyToggleLiquid))
.with(new KeyBindValue(WailaClient.keyShowRecipeInput))
.with(new KeyBindValue(WailaClient.keyShowRecipeOutput));
.with(new KeyBindValue(WailaClient.keyShowRecipeOutput)));

return options;
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/mcp/mobius/waila/gui/widget/ButtonEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import static mcp.mobius.waila.util.DisplayUtil.createButton;

Expand Down Expand Up @@ -35,9 +33,7 @@ public ButtonEntry(String name, String button, int width, int height, Button.OnP
}

@Override
public void render(@NotNull GuiGraphics ctx, int index, int rowTop, int rowLeft, int width, int height, int mouseX, int mouseY, boolean hovered, float deltaTime) {
super.render(ctx, index, rowTop, rowLeft, width, height, mouseX, mouseY, hovered, deltaTime);

protected void drawEntry(GuiGraphics ctx, int index, int rowTop, int rowLeft, int width, int height, int mouseX, int mouseY, boolean hovered, float deltaTime) {
ctx.drawString(client.font, title, rowLeft, rowTop + (height - client.font.lineHeight) / 2, 0xFFFFFF);
this.button.setX(rowLeft + width - button.getWidth());
this.button.setY(rowTop + (height - button.getHeight()) / 2);
Expand All @@ -50,8 +46,8 @@ protected void gatherChildren(ImmutableList.Builder<GuiEventListener> children)
}

@Override
public boolean match(String filter) {
return super.match(filter) || StringUtils.containsIgnoreCase(title, filter);
protected void buildSearchKey(StringBuilder sb) {
sb.append(title);
}

}
Loading

0 comments on commit ee97595

Please sign in to comment.