2021-05-15 15:34:19 +02:00
|
|
|
package io.gitlab.jfronny.libjf.config.gui;
|
|
|
|
|
2021-09-27 20:55:48 +02:00
|
|
|
import io.gitlab.jfronny.libjf.LibJf;
|
2021-05-15 15:34:19 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.Config;
|
2021-09-27 20:55:48 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.entry.Entry;
|
2021-05-15 15:34:19 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.EntryInfo;
|
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
|
|
|
import net.minecraft.client.gui.screen.Screen;
|
|
|
|
import net.minecraft.client.gui.screen.ScreenTexts;
|
|
|
|
import net.minecraft.client.gui.widget.ButtonWidget;
|
2021-05-29 10:37:13 +02:00
|
|
|
import net.minecraft.client.gui.widget.ClickableWidget;
|
2021-05-15 15:34:19 +02:00
|
|
|
import net.minecraft.client.gui.widget.TextFieldWidget;
|
|
|
|
import net.minecraft.client.resource.language.I18n;
|
|
|
|
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.nio.file.Files;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.function.BiFunction;
|
|
|
|
import java.util.function.Function;
|
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
|
|
public class TinyConfigScreen extends Screen {
|
|
|
|
public TinyConfigScreen(Screen parent, Config config) {
|
|
|
|
super(new TranslatableText(config.modid + ".jfconfig." + "title"));
|
|
|
|
this.parent = parent;
|
|
|
|
this.config = config;
|
|
|
|
this.translationPrefix = config.modid + ".jfconfig.";
|
|
|
|
}
|
|
|
|
private final String translationPrefix;
|
|
|
|
private final Screen parent;
|
|
|
|
private final Config config;
|
|
|
|
private MidnightConfigListWidget list;
|
|
|
|
|
|
|
|
// Real Time config update //
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
for (EntryInfo info : config.entries)
|
|
|
|
try { info.field.set(null, info.value); }
|
|
|
|
catch (IllegalAccessException ignored) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void init() {
|
|
|
|
super.init();
|
|
|
|
|
2021-05-29 10:37:13 +02:00
|
|
|
this.addDrawableChild(new ButtonWidget(this.width / 2 - 154, this.height - 28, 150, 20, ScreenTexts.CANCEL, button -> {
|
2021-05-15 15:34:19 +02:00
|
|
|
try {
|
2021-09-27 20:55:48 +02:00
|
|
|
LibJf.GSON.fromJson(Files.newBufferedReader(config.path), config.configClass); }
|
2021-05-15 15:34:19 +02:00
|
|
|
catch (Exception e) { config.write(); }
|
|
|
|
|
|
|
|
for (EntryInfo info : config.entries) {
|
|
|
|
if (info.field.isAnnotationPresent(Entry.class)) {
|
|
|
|
try {
|
|
|
|
info.value = info.field.get(null);
|
|
|
|
info.tempValue = info.value.toString();
|
|
|
|
} catch (IllegalAccessException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 19:35:20 +02:00
|
|
|
Objects.requireNonNull(client).setScreen(parent);
|
2021-05-15 15:34:19 +02:00
|
|
|
}));
|
|
|
|
|
2021-05-29 10:37:13 +02:00
|
|
|
ButtonWidget done = this.addDrawableChild(new ButtonWidget(this.width / 2 + 4, this.height - 28, 150, 20, ScreenTexts.DONE, (button) -> {
|
2021-05-15 15:34:19 +02:00
|
|
|
for (EntryInfo info : config.entries)
|
|
|
|
try {
|
|
|
|
info.field.set(null, info.value);
|
|
|
|
} catch (IllegalAccessException ignored) {}
|
|
|
|
config.write();
|
2021-08-21 19:35:20 +02:00
|
|
|
Objects.requireNonNull(client).setScreen(parent);
|
2021-05-15 15:34:19 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
this.list = new MidnightConfigListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
|
2021-05-29 10:37:13 +02:00
|
|
|
this.addSelectableChild(this.list);
|
2021-05-15 15:34:19 +02:00
|
|
|
for (EntryInfo info : config.entries) {
|
|
|
|
TranslatableText name = new TranslatableText(translationPrefix + info.field.getName());
|
|
|
|
ButtonWidget resetButton = new ButtonWidget(width - 155, 0, 40, 20, new LiteralText("Reset").formatted(Formatting.RED), (button -> {
|
|
|
|
info.value = info.defaultValue;
|
|
|
|
info.tempValue = info.value.toString();
|
|
|
|
double scrollAmount = list.getScrollAmount();
|
2021-08-21 19:35:20 +02:00
|
|
|
Objects.requireNonNull(client).setScreen(this);
|
2021-05-15 15:34:19 +02:00
|
|
|
list.setScrollAmount(scrollAmount);
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (info.widget instanceof Map.Entry) {
|
|
|
|
Map.Entry<ButtonWidget.PressAction, Function<Object, Text>> widget = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.widget;
|
|
|
|
if (info.field.getType().isEnum()) widget.setValue(value -> new TranslatableText(translationPrefix + "enum." + info.field.getType().getSimpleName() + "." + info.value.toString()));
|
|
|
|
this.list.addButton(new ButtonWidget(width - 110, 0, info.width, 20, widget.getValue().apply(info.value), widget.getKey()),resetButton,name);
|
|
|
|
} else if (info.widget != null) {
|
|
|
|
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 110, 0, info.width, 20, null);
|
|
|
|
|
|
|
|
widget.setText(info.tempValue);
|
|
|
|
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.widget).apply(widget, done);
|
|
|
|
widget.setTextPredicate(processor);
|
|
|
|
this.list.addButton(widget, resetButton, name);
|
|
|
|
} else {
|
|
|
|
ButtonWidget dummy = new ButtonWidget(-10, 0, 0, 0, Text.of(""), null);
|
|
|
|
this.list.addButton(dummy,dummy,name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
|
|
|
|
this.renderBackground(matrices);
|
|
|
|
this.list.render(matrices, mouseX, mouseY, delta);
|
|
|
|
|
2021-05-29 10:37:13 +02:00
|
|
|
//super.render(matrices, mouseX, mouseY, delta);
|
2021-05-15 15:34:19 +02:00
|
|
|
drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF);
|
|
|
|
|
|
|
|
for (EntryInfo info : config.entries) {
|
|
|
|
if (list.getHoveredButton(mouseX,mouseY).isPresent()) {
|
2021-05-29 10:37:13 +02:00
|
|
|
ClickableWidget buttonWidget = list.getHoveredButton(mouseX,mouseY).get();
|
2021-05-15 15:34:19 +02:00
|
|
|
Text text = ButtonEntry.buttonsWithText.get(buttonWidget);
|
|
|
|
TranslatableText name = new TranslatableText(this.translationPrefix + info.field.getName());
|
|
|
|
String key = translationPrefix + info.field.getName() + ".tooltip";
|
|
|
|
|
|
|
|
if (info.error != null && text.equals(name)) renderTooltip(matrices, info.error.getValue(), mouseX, mouseY);
|
|
|
|
else if (I18n.hasTranslation(key) && text.equals(name)) {
|
|
|
|
List<Text> list = new ArrayList<>();
|
|
|
|
for (String str : I18n.translate(key).split("\n"))
|
|
|
|
list.add(new LiteralText(str));
|
|
|
|
renderTooltip(matrices, list, mouseX, mouseY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 10:37:13 +02:00
|
|
|
super.render(matrices,mouseX,mouseY,delta);
|
2021-05-15 15:34:19 +02:00
|
|
|
}
|
|
|
|
}
|