2022-02-17 18:35:07 +01:00
|
|
|
package io.gitlab.jfronny.libjf.config.impl.client.screen;
|
2021-05-15 15:34:19 +02:00
|
|
|
|
2022-03-30 17:26:41 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.api.ConfigHolder;
|
2022-02-12 10:13:19 +01:00
|
|
|
import io.gitlab.jfronny.libjf.config.api.ConfigInstance;
|
2021-10-06 19:01:48 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.impl.EntryInfo;
|
2022-02-17 18:35:07 +01:00
|
|
|
import io.gitlab.jfronny.libjf.config.impl.client.screen.presets.PresetsScreen;
|
2021-05-15 15:34:19 +02:00
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
2022-01-08 20:02:15 +01:00
|
|
|
import net.minecraft.client.MinecraftClient;
|
2021-05-15 15:34:19 +02:00
|
|
|
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.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;
|
|
|
|
|
2021-12-30 15:11:20 +01:00
|
|
|
import java.util.*;
|
2021-05-15 15:34:19 +02:00
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
|
|
public class TinyConfigScreen extends Screen {
|
2022-03-25 18:34:43 +01:00
|
|
|
public TinyConfigScreen(ConfigInstance config, Screen parent) {
|
2022-03-30 23:08:51 +02:00
|
|
|
super(new TranslatableText(config.getModId() + ".jfconfig." + config.getCategoryPath() + "title"));
|
2021-05-15 15:34:19 +02:00
|
|
|
this.parent = parent;
|
|
|
|
this.config = config;
|
2022-03-30 23:08:51 +02:00
|
|
|
this.translationPrefix = config.getModId() + ".jfconfig." + config.getCategoryPath();
|
2021-05-15 15:34:19 +02:00
|
|
|
}
|
|
|
|
private final String translationPrefix;
|
|
|
|
private final Screen parent;
|
2022-02-12 10:13:19 +01:00
|
|
|
private final ConfigInstance config;
|
2021-05-15 15:34:19 +02:00
|
|
|
private MidnightConfigListWidget list;
|
|
|
|
|
|
|
|
// Real Time config update //
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
2022-01-08 20:02:15 +01:00
|
|
|
config.syncToClass();
|
2021-05-15 15:34:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void init() {
|
|
|
|
super.init();
|
|
|
|
|
2022-01-08 20:02:15 +01:00
|
|
|
config.syncFromClass();
|
|
|
|
|
|
|
|
this.addDrawableChild(new ButtonWidget(4, 6, 80, 20, new TranslatableText("libjf-config-v0.presets"), button -> {
|
|
|
|
MinecraftClient.getInstance().setScreen(new PresetsScreen(this, config));
|
|
|
|
}));
|
|
|
|
|
2021-05-29 10:37:13 +02:00
|
|
|
this.addDrawableChild(new ButtonWidget(this.width / 2 - 154, this.height - 28, 150, 20, ScreenTexts.CANCEL, button -> {
|
2022-02-12 10:13:19 +01:00
|
|
|
config.load();
|
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) -> {
|
2022-01-08 20:02:15 +01:00
|
|
|
config.syncToClass();
|
2021-05-15 15:34:19 +02:00
|
|
|
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);
|
2022-03-30 23:08:51 +02:00
|
|
|
for (Map.Entry<String, ConfigInstance> entry : config.getCategories().entrySet()) {
|
|
|
|
this.list.addReference(width / 2,
|
|
|
|
new TranslatableText(entry.getValue().getModId() + ".jfconfig." + entry.getValue().getCategoryPath() + "title"),
|
|
|
|
() -> new TinyConfigScreen(entry.getValue(), this));
|
|
|
|
}
|
2022-02-12 10:13:19 +01:00
|
|
|
for (EntryInfo info : config.getEntries()) {
|
2021-05-15 15:34:19 +02:00
|
|
|
TranslatableText name = new TranslatableText(translationPrefix + info.field.getName());
|
2022-03-31 20:45:10 +02:00
|
|
|
if (info.widget != null) {
|
2022-03-31 21:25:14 +02:00
|
|
|
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 -> {
|
2022-03-31 20:45:10 +02:00
|
|
|
info.value = info.defaultValue;
|
2022-03-31 21:25:14 +02:00
|
|
|
control.update().run();
|
2022-03-31 20:45:10 +02:00
|
|
|
}));
|
2022-03-31 21:25:14 +02:00
|
|
|
this.list.addButton(control.widget(), resetButton, () -> {
|
|
|
|
boolean visible = !Objects.equals(info.defaultValue, info.value);
|
|
|
|
resetButton.active = visible;
|
|
|
|
return visible;
|
|
|
|
}, name);
|
2021-05-15 15:34:19 +02:00
|
|
|
} else {
|
|
|
|
ButtonWidget dummy = new ButtonWidget(-10, 0, 0, 0, Text.of(""), null);
|
2022-03-31 21:25:14 +02:00
|
|
|
this.list.addButton(dummy,dummy, () -> false,name);
|
2021-05-15 15:34:19 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-30 17:26:41 +02:00
|
|
|
for (String referencedConfig : config.getReferencedConfigs()) {
|
|
|
|
ConfigInstance ci = ConfigHolder.getInstance().get(referencedConfig);
|
|
|
|
if (ci != null) {
|
|
|
|
this.list.addReference(width / 2,
|
2022-03-30 23:08:51 +02:00
|
|
|
new TranslatableText("libjf-config-v0.see-also", new TranslatableText(ci.getModId() + ".jfconfig." + ci.getCategoryPath() + "title")),
|
2022-03-30 17:26:41 +02:00
|
|
|
() -> new TinyConfigScreen(ci, this));
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 15:34:19 +02:00
|
|
|
}
|
2022-01-08 20:02:15 +01:00
|
|
|
|
2021-05-15 15:34:19 +02:00
|
|
|
@Override
|
|
|
|
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
|
|
|
|
this.renderBackground(matrices);
|
|
|
|
this.list.render(matrices, mouseX, mouseY, delta);
|
|
|
|
|
|
|
|
drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF);
|
|
|
|
|
2022-03-30 17:26:41 +02:00
|
|
|
Optional<Text> hovered = list.getHoveredEntryTitle(mouseY);
|
|
|
|
if (hovered.isPresent()) {
|
2022-02-12 10:13:19 +01:00
|
|
|
for (EntryInfo info : config.getEntries()) {
|
2022-03-30 17:26:41 +02:00
|
|
|
Text text = hovered.get();
|
2021-05-15 15:34:19 +02:00
|
|
|
TranslatableText name = new TranslatableText(this.translationPrefix + info.field.getName());
|
2021-12-30 15:11:20 +01:00
|
|
|
boolean showTooltip = text.equals(name);
|
|
|
|
String tooltipKey = translationPrefix + info.field.getName() + ".tooltip";
|
|
|
|
if (showTooltip && info.error != null) {
|
|
|
|
showTooltip = false;
|
|
|
|
renderTooltip(matrices, info.error.getValue(), mouseX, mouseY);
|
|
|
|
}
|
|
|
|
if (showTooltip && I18n.hasTranslation(tooltipKey)) {
|
|
|
|
showTooltip = false;
|
2021-05-15 15:34:19 +02:00
|
|
|
List<Text> list = new ArrayList<>();
|
2021-12-30 15:11:20 +01:00
|
|
|
for (String str : I18n.translate(tooltipKey).split("\n"))
|
2021-05-15 15:34:19 +02:00
|
|
|
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
|
|
|
}
|
2022-01-08 20:02:15 +01:00
|
|
|
|
|
|
|
@Override
|
2022-02-12 09:52:43 +01:00
|
|
|
public void close() {
|
2022-01-08 20:02:15 +01:00
|
|
|
MinecraftClient.getInstance().setScreen(parent);
|
|
|
|
}
|
2021-05-15 15:34:19 +02:00
|
|
|
}
|