2022-04-29 15:48:27 +02:00
|
|
|
package io.gitlab.jfronny.libjf.config.impl.client.gui;
|
2021-05-15 15:34:19 +02:00
|
|
|
|
2022-04-29 15:48:27 +02:00
|
|
|
import io.gitlab.jfronny.commons.throwable.Coerce;
|
|
|
|
import io.gitlab.jfronny.commons.throwable.Try;
|
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;
|
2022-04-29 15:48:27 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.api.EntryInfo;
|
|
|
|
import io.gitlab.jfronny.libjf.config.impl.EntryInfoImpl;
|
|
|
|
import io.gitlab.jfronny.libjf.config.impl.client.gui.presets.PresetsScreen;
|
|
|
|
import io.gitlab.jfronny.libjf.unsafe.SafeLog;
|
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-04-29 15:48:27 +02:00
|
|
|
this.widgets = EntryInfoWidgetBuilder.buildWidgets(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;
|
2022-04-29 15:48:27 +02:00
|
|
|
private final List<WidgetState<?>> widgets;
|
2021-05-15 15:34:19 +02:00
|
|
|
private MidnightConfigListWidget list;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void init() {
|
|
|
|
super.init();
|
|
|
|
|
2022-04-29 15:48:27 +02:00
|
|
|
config.verify();
|
2022-01-08 20:02:15 +01:00
|
|
|
|
|
|
|
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-04-29 15:48:27 +02:00
|
|
|
for (WidgetState<?> state : widgets) {
|
|
|
|
Try.orElse(state::writeToEntry, e -> SafeLog.error("Could not write config data to class", e));
|
|
|
|
}
|
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-04-29 15:48:27 +02:00
|
|
|
for (WidgetState<?> info : widgets) {
|
|
|
|
TranslatableText name = new TranslatableText(translationPrefix + info.entry.getName());
|
|
|
|
EntryInfoImpl.Widget control = info.entry.build(width, textRenderer, done);
|
|
|
|
ButtonWidget resetButton = new ButtonWidget(width - 155, 0, 40, 20, new TranslatableText("libjf-config-v0.reset"), (button -> {
|
|
|
|
info.reset();
|
|
|
|
control.update().run();
|
|
|
|
}));
|
|
|
|
this.list.addButton(control.widget(), resetButton, () -> {
|
|
|
|
boolean visible = !Objects.equals(info.entry.getDefault(), info.cachedValue);
|
|
|
|
resetButton.active = visible;
|
|
|
|
return visible;
|
|
|
|
}, 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-04-29 15:48:27 +02:00
|
|
|
for (WidgetState<?> info : widgets) {
|
2022-03-30 17:26:41 +02:00
|
|
|
Text text = hovered.get();
|
2022-04-29 15:48:27 +02:00
|
|
|
TranslatableText name = new TranslatableText(this.translationPrefix + info.entry.getName());
|
2021-12-30 15:11:20 +01:00
|
|
|
boolean showTooltip = text.equals(name);
|
2022-04-29 15:48:27 +02:00
|
|
|
String tooltipKey = translationPrefix + info.entry.getName() + ".tooltip";
|
2021-12-30 15:11:20 +01:00
|
|
|
if (showTooltip && info.error != null) {
|
|
|
|
showTooltip = false;
|
2022-04-29 15:48:27 +02:00
|
|
|
renderTooltip(matrices, info.error.right(), mouseX, mouseY);
|
2021-12-30 15:11:20 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|