Render sub-options more nicely

This commit is contained in:
embeddedt 2023-07-28 21:18:56 -04:00
parent f036350b0a
commit 5da78f7565
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 38 additions and 16 deletions

View File

@ -46,6 +46,13 @@ public class Option {
return this.parent; return this.parent;
} }
public int getDepth() {
if(this.parent == null)
return 0;
else
return this.parent.getDepth() + 1;
}
public boolean isEnabled() { public boolean isEnabled() {
return this.enabled; return this.enabled;
} }
@ -74,6 +81,13 @@ public class Option {
return this.name; return this.name;
} }
public String getSelfName() {
if(this.parent == null)
return this.name;
else
return this.name.substring(this.parent.getName().length() + 1);
}
public void clearModsDefiningValue() { public void clearModsDefiningValue() {
this.modDefined = null; this.modDefined = null;
} }

View File

@ -19,10 +19,11 @@ import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class OptionList extends ContainerObjectSelectionList<OptionList.Entry> { public class OptionList extends ContainerObjectSelectionList<OptionList.Entry> {
private final int maxNameWidth; private int maxNameWidth = 0;
private static final int DEPTH_OFFSET = 20;
private static final Component OPTION_ON = new TranslatableComponent("modernfix.option.on").withStyle(style -> style.withColor(ChatFormatting.GREEN)); private static final Component OPTION_ON = new TranslatableComponent("modernfix.option.on").withStyle(style -> style.withColor(ChatFormatting.GREEN));
private static final Component OPTION_OFF = new TranslatableComponent("modernfix.option.off").withStyle(style -> style.withColor(ChatFormatting.RED)); private static final Component OPTION_OFF = new TranslatableComponent("modernfix.option.off").withStyle(style -> style.withColor(ChatFormatting.RED));
@ -31,9 +32,9 @@ public class OptionList extends ContainerObjectSelectionList<OptionList.Entry> {
private ModernFixConfigScreen mainScreen; private ModernFixConfigScreen mainScreen;
private static MutableComponent getOptionComponent(String optionName) { private static MutableComponent getOptionComponent(Option option) {
String friendlyKey = "modernfix.option.name." + optionName; String friendlyKey = "modernfix.option.name." + option.getName();
TextComponent baseComponent = new TextComponent(optionName); TextComponent baseComponent = new TextComponent(option.getSelfName());
if(I18n.exists(friendlyKey)) if(I18n.exists(friendlyKey))
return new TranslatableComponent(friendlyKey).withStyle(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, baseComponent))); return new TranslatableComponent(friendlyKey).withStyle(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, baseComponent)));
else else
@ -48,12 +49,25 @@ public class OptionList extends ContainerObjectSelectionList<OptionList.Entry> {
} }
} }
private final Set<Option> addedOptions = new HashSet<>();
private void addOption(Option option) {
if(addedOptions.add(option)) {
int w = this.minecraft.font.width(getOptionComponent(option)) + DEPTH_OFFSET * option.getDepth();
this.maxNameWidth = Math.max(w, this.maxNameWidth);
this.addEntry(new OptionEntry(option.getName(), option));
ModernFixMixinPlugin.instance.config.getOptionMap().values().stream()
.filter(subOption -> subOption.getParent() == option)
.sorted(Comparator.comparing(Option::getName))
.forEach(this::addOption);
}
}
public OptionList(ModernFixConfigScreen arg, Minecraft arg2) { public OptionList(ModernFixConfigScreen arg, Minecraft arg2) {
super(arg2,arg.width + 45, arg.height, 43, arg.height - 32, 20); super(arg2,arg.width + 45, arg.height, 43, arg.height - 32, 20);
this.mainScreen = arg; this.mainScreen = arg;
int maxW = 0;
Multimap<String, Option> optionsByCategory = ModernFixMixinPlugin.instance.config.getOptionCategoryMap(); Multimap<String, Option> optionsByCategory = ModernFixMixinPlugin.instance.config.getOptionCategoryMap();
List<String> theCategories = OptionCategories.getCategoriesInOrder(); List<String> theCategories = OptionCategories.getCategoriesInOrder();
for(String category : theCategories) { for(String category : theCategories) {
@ -61,21 +75,15 @@ public class OptionList extends ContainerObjectSelectionList<OptionList.Entry> {
this.addEntry(new CategoryEntry(new TranslatableComponent(categoryTranslationKey) this.addEntry(new CategoryEntry(new TranslatableComponent(categoryTranslationKey)
.withStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TranslatableComponent(categoryTranslationKey + ".description")))) .withStyle(Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TranslatableComponent(categoryTranslationKey + ".description"))))
)); ));
List<Option> sortedKeys = optionsByCategory.get(category).stream().filter(key -> { optionsByCategory.get(category).stream().filter(key -> {
int dotCount = 0; int dotCount = 0;
for(char c : key.getName().toCharArray()) { for(char c : key.getName().toCharArray()) {
if(c == '.') if(c == '.')
dotCount++; dotCount++;
} }
return dotCount >= 2; return dotCount >= 2;
}).sorted(Comparator.comparing(Option::getName)).collect(Collectors.toList()); }).sorted(Comparator.comparing(Option::getName)).forEach(this::addOption);
for(Option option : sortedKeys) {
int w = this.minecraft.font.width(getOptionComponent(option.getName()));
maxW = Math.max(w, maxW);
this.addEntry(new OptionEntry(option.getName(), option));
}
} }
this.maxNameWidth = maxW;
} }
protected int getScrollbarPosition() { protected int getScrollbarPosition() {
@ -165,10 +173,10 @@ public class OptionList extends ContainerObjectSelectionList<OptionList.Entry> {
@Override @Override
public void render(PoseStack matrixStack, int index, int top, int left, int width, int height, int mouseX, int mouseY, boolean isMouseOver, float partialTicks) { public void render(PoseStack matrixStack, int index, int top, int left, int width, int height, int mouseX, int mouseY, boolean isMouseOver, float partialTicks) {
MutableComponent nameComponent = getOptionComponent(this.name); MutableComponent nameComponent = getOptionComponent(option);
if(this.option.isUserDefined()) if(this.option.isUserDefined())
nameComponent = nameComponent.withStyle(style -> style.withItalic(true)).append(new TranslatableComponent("modernfix.config.not_default")); nameComponent = nameComponent.withStyle(style -> style.withItalic(true)).append(new TranslatableComponent("modernfix.config.not_default"));
float textX = (float)(left + 160 - OptionList.this.maxNameWidth); float textX = (float)(left + DEPTH_OFFSET * option.getDepth() + 160 - OptionList.this.maxNameWidth);
float textY = (float)(top + height / 2 - 4); float textY = (float)(top + height / 2 - 4);
OptionList.this.minecraft.font.draw(matrixStack, nameComponent, textX, textY, 16777215); OptionList.this.minecraft.font.draw(matrixStack, nameComponent, textX, textY, 16777215);
this.toggleButton.x = left + 175; this.toggleButton.x = left + 175;