2023-03-11 21:21:53 +01:00
|
|
|
package io.gitlab.jfronny.libjf.config.impl.ui.tiny;
|
|
|
|
|
|
|
|
import io.gitlab.jfronny.libjf.config.api.v1.ConfigCategory;
|
|
|
|
import io.gitlab.jfronny.libjf.config.api.v1.ConfigInstance;
|
|
|
|
import io.gitlab.jfronny.libjf.config.api.v1.ui.tiny.WidgetFactory;
|
2023-08-13 19:48:21 +02:00
|
|
|
import io.gitlab.jfronny.libjf.config.impl.entrypoint.JfConfigSafe;
|
2023-03-18 13:57:01 +01:00
|
|
|
import io.gitlab.jfronny.libjf.config.impl.ui.tiny.entry.*;
|
2023-03-11 21:21:53 +01:00
|
|
|
import io.gitlab.jfronny.libjf.config.impl.ui.tiny.presets.PresetsScreen;
|
|
|
|
import net.minecraft.client.font.TextRenderer;
|
|
|
|
import net.minecraft.client.gui.ScreenRect;
|
|
|
|
import net.minecraft.client.gui.tab.Tab;
|
|
|
|
import net.minecraft.client.gui.widget.ButtonWidget;
|
|
|
|
import net.minecraft.client.gui.widget.ClickableWidget;
|
|
|
|
import net.minecraft.text.MutableText;
|
|
|
|
import net.minecraft.text.Text;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.function.BooleanSupplier;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
public class TinyConfigTab implements Tab {
|
|
|
|
private final ConfigCategory config;
|
|
|
|
private final EntryListWidget list;
|
|
|
|
|
|
|
|
public TinyConfigTab(TinyConfigScreen screen, ConfigCategory config, TextRenderer textRenderer, boolean isRoot) {
|
|
|
|
this.config = config;
|
|
|
|
List<WidgetState<?>> widgets = EntryInfoWidgetBuilder.buildWidgets(config, screen.widgets);
|
|
|
|
|
|
|
|
config.fix();
|
|
|
|
for (WidgetState<?> widget : widgets) {
|
|
|
|
widget.updateCache();
|
|
|
|
}
|
|
|
|
|
2023-03-18 14:58:44 +01:00
|
|
|
// Sizing is also done in TinyConfigScreen. Keep these in sync!
|
2023-08-13 19:48:21 +02:00
|
|
|
this.list = new EntryListWidget(screen.getClient(), textRenderer, screen.width, screen.height - 68, 32, screen.height - 36);
|
|
|
|
|
|
|
|
String tooltipPath = config.getTranslationPrefix() + "tooltip";
|
|
|
|
if (JfConfigSafe.TRANSLATION_SUPPLIER.apply(tooltipPath) != null) {
|
|
|
|
this.list.addText(Text.translatable(tooltipPath));
|
|
|
|
}
|
2023-03-11 21:21:53 +01:00
|
|
|
|
|
|
|
if (isRoot) {
|
|
|
|
for (Map.Entry<String, ConfigCategory> entry : config.getCategories().entrySet()) {
|
2023-03-18 14:58:44 +01:00
|
|
|
this.list.addReference(TinyConfigScreen.getTitle(entry.getValue().getTranslationPrefix()),
|
2023-03-11 21:21:53 +01:00
|
|
|
() -> new TinyConfigScreen(entry.getValue(), screen));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!config.getPresets().isEmpty()) {
|
2023-03-18 14:58:44 +01:00
|
|
|
this.list.addReference(Text.translatable("libjf-config-v1.presets"),
|
2023-03-22 15:01:55 +01:00
|
|
|
() -> new PresetsScreen(screen, config, screen::afterSelectPreset));
|
2023-03-11 21:21:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (WidgetState<?> info : widgets) {
|
|
|
|
MutableText name = Text.translatable(config.getTranslationPrefix() + info.entry.getName());
|
|
|
|
WidgetFactory.Widget control = info.factory == null ? null : info.factory.build(screen, textRenderer);
|
|
|
|
ButtonWidget resetButton = ButtonWidget.builder(Text.translatable("libjf-config-v1.reset"), (button -> {
|
|
|
|
info.reset();
|
|
|
|
if (control != null) control.updateControls().run();
|
|
|
|
}))
|
2023-03-18 14:58:44 +01:00
|
|
|
.dimensions(screen.width - 155, 0, 40, 20)
|
2023-03-11 21:21:53 +01:00
|
|
|
.build();
|
|
|
|
BooleanSupplier resetVisible = () -> {
|
|
|
|
boolean visible = !Objects.equals(info.entry.getDefault(), info.cachedValue);
|
|
|
|
resetButton.active = visible;
|
|
|
|
return visible;
|
|
|
|
};
|
2023-03-18 14:58:44 +01:00
|
|
|
Reflowable reflow = (width, height) -> {
|
|
|
|
resetButton.setX(width - 155);
|
|
|
|
if (control != null) control.reflow(width, height);
|
|
|
|
};
|
|
|
|
if (control == null) this.list.addUnknown(resetButton, resetVisible, name, reflow);
|
|
|
|
else this.list.addButton(control.control(), resetButton, resetVisible, name, reflow);
|
2023-03-11 21:21:53 +01:00
|
|
|
}
|
|
|
|
for (ConfigInstance ci : config.getReferencedConfigs()) {
|
|
|
|
if (ci != null) {
|
2023-03-18 14:58:44 +01:00
|
|
|
this.list.addReference(Text.translatable("libjf-config-v1.see-also", TinyConfigScreen.getTitle(ci.getTranslationPrefix())),
|
2023-03-11 21:21:53 +01:00
|
|
|
() -> new TinyConfigScreen(ci, screen));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Text getTitle() {
|
|
|
|
return TinyConfigScreen.getTitle(config.getTranslationPrefix());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void forEachChild(Consumer<ClickableWidget> consumer) {
|
|
|
|
consumer.accept(new TinyConfigTabWrapper(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void refreshGrid(ScreenRect tabArea) {
|
2023-03-18 14:58:44 +01:00
|
|
|
list.refreshGrid(tabArea);
|
2023-03-11 21:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
|
|
|
Tab.super.tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
public EntryListWidget getList() {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|