[config] Improve reset button
This commit is contained in:
parent
1964cccf90
commit
76dd6e5df3
|
@ -22,6 +22,9 @@ public class EntryInfo {
|
|||
public Entry entry;
|
||||
|
||||
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) {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 == 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) {
|
||||
Function<Object, Text> func = value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
|
||||
toggle(info, button -> {
|
||||
info.value = !(Boolean) info.value;
|
||||
button.setMessage(func.apply(info.value));
|
||||
}, func);
|
||||
toggle(info,
|
||||
() -> info.value = !(Boolean) info.value,
|
||||
value -> new LiteralText((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED));
|
||||
} else if (type.isEnum()) {
|
||||
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, button -> {
|
||||
toggle(info, () -> {
|
||||
int index = values.indexOf(info.value) + 1;
|
||||
info.value = values.get(index >= values.size() ? 0 : index);
|
||||
button.setMessage(func.apply(info.value));
|
||||
}, func);
|
||||
}, value -> new TranslatableText(config.getModId() + ".jfconfig.enum." + type.getSimpleName() + "." + info.value.toString()));
|
||||
} else LibJf.LOGGER.error("Invalid entry type in " + info.field.getName() + ": " + type.getName());
|
||||
|
||||
try {
|
||||
|
@ -73,8 +69,14 @@ public class EntryInfoWidgetBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
private static void toggle(EntryInfo info, ButtonWidget.PressAction pressAction, Function<Object, Text> valueTextifier) {
|
||||
info.widget = (width, textRenderer, done) -> new ButtonWidget(width - 110, 0, info.width, 20, valueTextifier.apply(info.value), pressAction);
|
||||
private static void toggle(EntryInfo info, Runnable increment, Function<Object, Text> valueTextifier) {
|
||||
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);
|
||||
|
||||
return widget;
|
||||
return new EntryInfo.Widget(() -> widget.setText(info.value == null ? "" : info.value.toString()), widget);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,8 +32,8 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
|
|||
return this.width -7;
|
||||
}
|
||||
|
||||
public void addButton(ClickableWidget button, ClickableWidget resetButton, Text text) {
|
||||
this.addEntry(new ConfigScreenEntry(button, text, resetButton));
|
||||
public void addButton(ClickableWidget button, ClickableWidget resetButton, Supplier<Boolean> resetVisible, Text text) {
|
||||
this.addEntry(new ConfigScreenEntry(button, text, resetVisible, resetButton));
|
||||
}
|
||||
|
||||
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 {
|
||||
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
|
||||
public final ClickableWidget button;
|
||||
private final Supplier<Boolean> resetVisible;
|
||||
private final ClickableWidget resetButton;
|
||||
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.resetVisible = resetVisible;
|
||||
this.resetButton = resetButton;
|
||||
this.text = text;
|
||||
}
|
||||
|
@ -103,8 +105,10 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
|
|||
button.y = y;
|
||||
button.render(matrices, mouseX, mouseY, tickDelta);
|
||||
drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF);
|
||||
resetButton.y = y;
|
||||
resetButton.render(matrices, mouseX, mouseY, tickDelta);
|
||||
if (resetVisible.get()) {
|
||||
resetButton.y = y;
|
||||
resetButton.render(matrices, mouseX, mouseY, tickDelta);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -15,7 +15,6 @@ import net.minecraft.client.util.math.MatrixStack;
|
|||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
import net.minecraft.util.Formatting;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
@ -69,17 +68,19 @@ public class TinyConfigScreen extends Screen {
|
|||
for (EntryInfo info : config.getEntries()) {
|
||||
TranslatableText name = new TranslatableText(translationPrefix + info.field.getName());
|
||||
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.tempValue = info.value.toString();
|
||||
double scrollAmount = list.getScrollAmount();
|
||||
Objects.requireNonNull(client).setScreen(this);
|
||||
list.setScrollAmount(scrollAmount);
|
||||
control.update().run();
|
||||
}));
|
||||
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 {
|
||||
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()) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"libjf-config-v0.presets": "Presets",
|
||||
"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"
|
||||
}
|
Loading…
Reference in New Issue