133 lines
5.5 KiB
Java
133 lines
5.5 KiB
Java
package io.gitlab.jfronny.libjf.config;
|
|
|
|
import io.gitlab.jfronny.libjf.Libjf;
|
|
import net.minecraft.client.gui.screen.Screen;
|
|
import net.minecraft.client.gui.screen.ScreenTexts;
|
|
import net.minecraft.client.gui.widget.ButtonWidget;
|
|
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;
|
|
|
|
public class ConfigScreen extends Screen {
|
|
public ConfigScreen(Screen parent, Config config) {
|
|
super(new TranslatableText(config.translationPrefix + "title"));
|
|
this.parent = parent;
|
|
this.config = config;
|
|
}
|
|
private final Screen parent;
|
|
private final Config config;
|
|
|
|
// Real Time config update //
|
|
@Override
|
|
public void tick() {
|
|
for (EntryInfo info : config.entries)
|
|
try { info.field.set(null, info.value); }
|
|
catch (IllegalAccessException ignore) {}
|
|
}
|
|
|
|
@Override
|
|
protected void init() {
|
|
super.init();
|
|
this.addButton(new ButtonWidget(this.width / 2 - 154, this.height - 28, 150, 20, ScreenTexts.CANCEL, button -> {
|
|
try { Libjf.GSON.fromJson(Files.newBufferedReader(config.path), config.configClass); }
|
|
catch (Exception e) { config.write(); }
|
|
|
|
for (EntryInfo info : config.entries) {
|
|
try {
|
|
info.value = info.field.get(null);
|
|
info.tempValue = info.value.toString();
|
|
}
|
|
catch (IllegalAccessException ignored) {}
|
|
}
|
|
Objects.requireNonNull(client).openScreen(parent);
|
|
}));
|
|
|
|
ButtonWidget done = this.addButton(new ButtonWidget(this.width / 2 + 4, this.height - 28, 150, 20, ScreenTexts.DONE, (button) -> {
|
|
for (EntryInfo info : config.entries)
|
|
try { info.field.set(null, info.value); }
|
|
catch (IllegalAccessException ignore) {}
|
|
config.write();
|
|
Objects.requireNonNull(client).openScreen(parent);
|
|
}));
|
|
|
|
int y = 45;
|
|
for (EntryInfo info : config.entries) {
|
|
addButton(new ButtonWidget(width - 155, y, 40,20, new LiteralText("Reset").formatted(Formatting.RED), (button -> {
|
|
info.value = info.defaultValue;
|
|
info.tempValue = info.value.toString();
|
|
Objects.requireNonNull(client).openScreen(this);
|
|
})));
|
|
|
|
if (info.widget instanceof Map.Entry) {
|
|
Map.Entry<ButtonWidget.PressAction, Function<Object,Text>> widget = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.widget;
|
|
addButton(new ButtonWidget(width-110,y,info.width,20, widget.getValue().apply(info.value), widget.getKey()));
|
|
}
|
|
else {
|
|
TextFieldWidget widget = addButton(new TextFieldWidget(textRenderer, width-110, y, info.width, 20, null));
|
|
widget.setText(info.tempValue);
|
|
|
|
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.widget).apply(widget,done);
|
|
widget.setTextPredicate(processor);
|
|
|
|
children.add(widget);
|
|
}
|
|
y += 25;
|
|
}
|
|
|
|
}
|
|
int aniX = this.width / 2;
|
|
@Override
|
|
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
|
|
this.renderBackground(matrices);
|
|
|
|
if (aniX < this.width / 2) {
|
|
aniX = aniX +40;
|
|
}
|
|
|
|
int stringWidth = (int) (title.getString().length() * 2.75f);
|
|
this.fillGradient(matrices, this.width / 2 - stringWidth, 10, this.width /2 + stringWidth, 29, -1072689136, -804253680);
|
|
this.fillGradient(matrices, this.width / 2 - aniX, 35, width/2 + aniX, this.height - 40, -1072689136, -804253680);
|
|
|
|
super.render(matrices, mouseX, mouseY, delta);
|
|
drawCenteredText(matrices, textRenderer, title, width/2, 15, 0xFFFFFF);
|
|
|
|
int y = 40;
|
|
for (EntryInfo info : config.entries) {
|
|
drawTextWithShadow(matrices, textRenderer, new TranslatableText(config.translationPrefix + info.field.getName()), 12, y + 10, 0xFFFFFF);
|
|
|
|
if (info.error != null && info.error.getKey().isMouseOver(mouseX,mouseY))
|
|
renderTooltip(matrices, info.error.getValue(), mouseX, mouseY);
|
|
else if (mouseY >= y && mouseY < (y + 25)) {
|
|
if (info.dynamicTooltip != null) {
|
|
try {
|
|
renderTooltip(matrices, (List<Text>) info.dynamicTooltip.invoke(null, config.entries), mouseX, mouseY);
|
|
y += 25;
|
|
continue;
|
|
} catch (Exception e) { e.printStackTrace(); }
|
|
}
|
|
String key = config.translationPrefix + info.field.getName() + ".tooltip";
|
|
if (I18n.hasTranslation(key)) {
|
|
List<Text> list = new ArrayList<>();
|
|
for (String str : I18n.translate(key).split("\n"))
|
|
list.add(new LiteralText(str));
|
|
renderTooltip(matrices, list, mouseX, mouseY);
|
|
}
|
|
}
|
|
y += 25;
|
|
}
|
|
}
|
|
}
|