[config] Improve reset button

This commit is contained in:
Johannes Frohnmeyer 2022-03-31 21:25:14 +02:00
parent 1964cccf90
commit 76dd6e5df3
Signed by: Johannes
GPG Key ID: E76429612C2929F4
5 changed files with 38 additions and 27 deletions

View File

@ -22,6 +22,9 @@ public class EntryInfo {
public Entry entry; public Entry entry;
public interface WidgetFactory { public interface WidgetFactory {
ClickableWidget build(int width, TextRenderer textRenderer, ButtonWidget done); Widget build(int width, TextRenderer textRenderer, ButtonWidget done);
}
public record Widget(Runnable update, ClickableWidget widget) {
} }
} }

View File

@ -51,19 +51,15 @@ public class EntryInfoWidgetBuilder {
else if (type == double.class || type == Double.class) textField(config, info, DECIMAL_ONLY, Double::parseDouble, false, info.entry.min(), info.entry.max()); else if (type == double.class || type == Double.class) textField(config, info, DECIMAL_ONLY, Double::parseDouble, false, info.entry.min(), info.entry.max());
else if (type == String.class) textField(config, info, null, String::length, true, Math.min(info.entry.min(),0), Math.max(info.entry.max(),1)); else if (type == String.class) textField(config, info, null, String::length, true, Math.min(info.entry.min(),0), Math.max(info.entry.max(),1));
else if (type == boolean.class || type == Boolean.class) { else if (type == boolean.class || type == Boolean.class) {
Function<Object, Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED); toggle(info,
toggle(info, button -> { () -> info.value = !(Boolean) info.value,
info.value = !(Boolean) info.value; value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED));
button.setMessage(func.apply(info.value));
}, func);
} else if (type.isEnum()) { } else if (type.isEnum()) {
List<?> values = Arrays.asList(info.field.getType().getEnumConstants()); List<?> values = Arrays.asList(info.field.getType().getEnumConstants());
Function<Object,Text> func = value -> new TranslatableText(config.getModId() + ".jfconfig.enum." + type.getSimpleName() + "." + info.value.toString()); toggle(info, () -> {
toggle(info, button -> {
int index = values.indexOf(info.value) + 1; int index = values.indexOf(info.value) + 1;
info.value = values.get(index >= values.size() ? 0 : index); info.value = values.get(index >= values.size() ? 0 : index);
button.setMessage(func.apply(info.value)); }, value -> new TranslatableText(config.getModId() + ".jfconfig.enum." + type.getSimpleName() + "." + info.value.toString()));
}, func);
} else LibJf.LOGGER.error("Invalid entry type in " + info.field.getName() + ": " + type.getName()); } else LibJf.LOGGER.error("Invalid entry type in " + info.field.getName() + ": " + type.getName());
try { try {
@ -73,8 +69,14 @@ public class EntryInfoWidgetBuilder {
} }
} }
private static void toggle(EntryInfo info, ButtonWidget.PressAction pressAction, Function<Object, Text> valueTextifier) { private static void toggle(EntryInfo info, Runnable increment, Function<Object, Text> valueTextifier) {
info.widget = (width, textRenderer, done) -> new ButtonWidget(width - 110, 0, info.width, 20, valueTextifier.apply(info.value), pressAction); info.widget = (width, textRenderer, done) -> {
ButtonWidget button = new ButtonWidget(width - 110, 0, info.width, 20, valueTextifier.apply(info.value), btn -> {
increment.run();
btn.setMessage(valueTextifier.apply(info.value));
});
return new EntryInfo.Widget(() -> button.setMessage(valueTextifier.apply(info.value)), button);
};
} }
/** /**
@ -119,7 +121,7 @@ public class EntryInfoWidgetBuilder {
}; };
widget.setTextPredicate(processor); widget.setTextPredicate(processor);
return widget; return new EntryInfo.Widget(() -> widget.setText(info.value == null ? "" : info.value.toString()), widget);
}; };
} }
} }

View File

@ -32,8 +32,8 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
return this.width -7; return this.width -7;
} }
public void addButton(ClickableWidget button, ClickableWidget resetButton, Text text) { public void addButton(ClickableWidget button, ClickableWidget resetButton, Supplier<Boolean> resetVisible, Text text) {
this.addEntry(new ConfigScreenEntry(button, text, resetButton)); this.addEntry(new ConfigScreenEntry(button, text, resetVisible, resetButton));
} }
public void addReference(int centerX, Text text, Supplier<Screen> targetScreen) { public void addReference(int centerX, Text text, Supplier<Screen> targetScreen) {
@ -89,11 +89,13 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
public static class ConfigScreenEntry extends ConfigListEntryAbstract { public static class ConfigScreenEntry extends ConfigListEntryAbstract {
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
public final ClickableWidget button; public final ClickableWidget button;
private final Supplier<Boolean> resetVisible;
private final ClickableWidget resetButton; private final ClickableWidget resetButton;
private final Text text; private final Text text;
public ConfigScreenEntry(ClickableWidget button, Text text, ClickableWidget resetButton) { public ConfigScreenEntry(ClickableWidget button, Text text, Supplier<Boolean> resetVisible, ClickableWidget resetButton) {
this.button = button; this.button = button;
this.resetVisible = resetVisible;
this.resetButton = resetButton; this.resetButton = resetButton;
this.text = text; this.text = text;
} }
@ -103,9 +105,11 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
button.y = y; button.y = y;
button.render(matrices, mouseX, mouseY, tickDelta); button.render(matrices, mouseX, mouseY, tickDelta);
drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF); drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF);
if (resetVisible.get()) {
resetButton.y = y; resetButton.y = y;
resetButton.render(matrices, mouseX, mouseY, tickDelta); resetButton.render(matrices, mouseX, mouseY, tickDelta);
} }
}
@Override @Override
public List<? extends Element> children() { public List<? extends Element> children() {

View File

@ -15,7 +15,6 @@ import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import java.util.*; import java.util.*;
@ -69,17 +68,19 @@ public class TinyConfigScreen extends Screen {
for (EntryInfo info : config.getEntries()) { for (EntryInfo info : config.getEntries()) {
TranslatableText name = new TranslatableText(translationPrefix + info.field.getName()); TranslatableText name = new TranslatableText(translationPrefix + info.field.getName());
if (info.widget != null) { if (info.widget != null) {
ButtonWidget resetButton = new ButtonWidget(width - 155, 0, 40, 20, new LiteralText("Reset").formatted(Formatting.RED), (button -> { EntryInfo.Widget control = info.widget.build(width, textRenderer, done);
ButtonWidget resetButton = new ButtonWidget(width - 155, 0, 40, 20, new TranslatableText("libjf-config-v0.reset"), (button -> {
info.value = info.defaultValue; info.value = info.defaultValue;
info.tempValue = info.value.toString(); control.update().run();
double scrollAmount = list.getScrollAmount();
Objects.requireNonNull(client).setScreen(this);
list.setScrollAmount(scrollAmount);
})); }));
this.list.addButton(info.widget.build(width, textRenderer, done), resetButton, name); this.list.addButton(control.widget(), resetButton, () -> {
boolean visible = !Objects.equals(info.defaultValue, info.value);
resetButton.active = visible;
return visible;
}, name);
} else { } else {
ButtonWidget dummy = new ButtonWidget(-10, 0, 0, 0, Text.of(""), null); ButtonWidget dummy = new ButtonWidget(-10, 0, 0, 0, Text.of(""), null);
this.list.addButton(dummy,dummy,name); this.list.addButton(dummy,dummy, () -> false,name);
} }
} }
for (String referencedConfig : config.getReferencedConfigs()) { for (String referencedConfig : config.getReferencedConfigs()) {

View File

@ -1,5 +1,6 @@
{ {
"libjf-config-v0.presets": "Presets", "libjf-config-v0.presets": "Presets",
"libjf-config-v0.default": "Default", "libjf-config-v0.default": "Default",
"libjf-config-v0.see-also": "See also: %s" "libjf-config-v0.see-also": "See also: %s",
"libjf-config-v0.reset": "Reset"
} }