From aafc14e3fc8423f45ea1d59e03bf2746c3bd338e Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 18 Mar 2023 14:14:27 +0100 Subject: [PATCH] [config-ui-tiny] show EditorScreen directly if config consists of single, unsupported entry --- .../config/api/v1/ui/tiny/ConfigScreen.java | 5 +- .../config/impl/ui/tiny/EditorScreen.java | 22 +++------ .../impl/ui/tiny/TinyConfigScreenFactory.java | 49 ++++++++++++++++++- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/api/v1/ui/tiny/ConfigScreen.java b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/api/v1/ui/tiny/ConfigScreen.java index f9fe507..9b60f21 100644 --- a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/api/v1/ui/tiny/ConfigScreen.java +++ b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/api/v1/ui/tiny/ConfigScreen.java @@ -1,12 +1,13 @@ package io.gitlab.jfronny.libjf.config.api.v1.ui.tiny; import io.gitlab.jfronny.libjf.config.api.v1.ConfigInstance; -import io.gitlab.jfronny.libjf.config.impl.ui.tiny.TinyConfigScreen; +import io.gitlab.jfronny.libjf.config.impl.ui.tiny.TinyConfigScreenFactory; import net.minecraft.client.gui.screen.Screen; @Deprecated public interface ConfigScreen { + TinyConfigScreenFactory FACTORY = new TinyConfigScreenFactory(); static Screen create(ConfigInstance config, Screen parent) { - return new TinyConfigScreen(config, parent); + return FACTORY.create(config, parent); } } diff --git a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/EditorScreen.java b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/EditorScreen.java index 4c9218b..c68ee2f 100644 --- a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/EditorScreen.java +++ b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/EditorScreen.java @@ -303,7 +303,7 @@ public class EditorScreen extends Screen { this.textRenderer.draw(matrices, line.text, line.x, line.y, 0xFFFFFFFF); } this.drawSelection(matrices, pageContent.selectionRectangles); - this.drawCursor(matrices, pageContent.position, pageContent.atEnd); + this.drawCursor(matrices, pageContent.position); disableScissor(); int i = this.getScrollbarPositionX(); @@ -322,14 +322,10 @@ public class EditorScreen extends Screen { super.render(matrices, mouseX, mouseY, delta); } - private void drawCursor(MatrixStack matrices, Position position, boolean atEnd) { + private void drawCursor(MatrixStack matrices, Position position) { if (this.tickCounter / 6 % 2 == 0) { position = this.absolutePositionToScreenPosition(position); - if (!atEnd) { - DrawableHelper.fill(matrices, position.x, position.y - 1, position.x + 1, position.y + this.textRenderer.fontHeight, 0xFFFFFFFF); - } else { - this.textRenderer.draw(matrices, "_", position.x, position.y, 0); - } + DrawableHelper.fill(matrices, position.x, position.y - 1, position.x + 1, position.y + this.textRenderer.fontHeight, 0xFFFFFFFF); } } @@ -451,10 +447,10 @@ public class EditorScreen extends Screen { list.add(new Line(style, string2, position.x, position.y)); }); int[] is = intList.toIntArray(); - boolean bl = i == content.length(); + boolean atEnd = i == content.length(); int l; Position position; - if (bl && mutableBoolean.isTrue()) { + if (atEnd && mutableBoolean.isTrue()) { position = new Position(0, list.size() * this.textRenderer.fontHeight); } else { int k = getLineFromOffset(is, i); @@ -483,7 +479,7 @@ public class EditorScreen extends Screen { list2.add(this.getLineSelectionRectangle(content, textHandler, is[o], m, o * this.textRenderer.fontHeight, is[o])); } } - return new PageContent(content, position, bl, is, list.toArray(new Line[0]), list2.toArray(new Rect2i[0])); + return new PageContent(content, position, is, list.toArray(new Line[0]), list2.toArray(new Rect2i[0])); } static int getLineFromOffset(int[] lineStarts, int position) { @@ -513,18 +509,16 @@ public class EditorScreen extends Screen { } static class PageContent { - static final PageContent EMPTY = new PageContent("", new Position(0, 0), true, new int[]{0}, new Line[]{new Line(Style.EMPTY, "", 0, 0)}, new Rect2i[0]); + static final PageContent EMPTY = new PageContent("", new Position(0, 0), new int[]{0}, new Line[]{new Line(Style.EMPTY, "", 0, 0)}, new Rect2i[0]); private final String pageContent; final Position position; - final boolean atEnd; private final int[] lineStarts; final Line[] lines; final Rect2i[] selectionRectangles; - public PageContent(String pageContent, Position position, boolean atEnd, int[] lineStarts, Line[] lines, Rect2i[] selectionRectangles) { + public PageContent(String pageContent, Position position, int[] lineStarts, Line[] lines, Rect2i[] selectionRectangles) { this.pageContent = pageContent; this.position = position; - this.atEnd = atEnd; this.lineStarts = lineStarts; this.lines = lines; this.selectionRectangles = selectionRectangles; diff --git a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/TinyConfigScreenFactory.java b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/TinyConfigScreenFactory.java index d0c458e..4cc7396 100644 --- a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/TinyConfigScreenFactory.java +++ b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/TinyConfigScreenFactory.java @@ -1,13 +1,58 @@ package io.gitlab.jfronny.libjf.config.impl.ui.tiny; +import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders; +import io.gitlab.jfronny.libjf.LibJf; import io.gitlab.jfronny.libjf.config.api.v1.ConfigInstance; +import io.gitlab.jfronny.libjf.config.api.v1.EntryInfo; +import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder; +import io.gitlab.jfronny.libjf.config.api.v1.type.Type; import io.gitlab.jfronny.libjf.config.api.v1.ui.ConfigScreenFactory; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.client.toast.SystemToast; +import net.minecraft.text.Text; // IDEA doesn't like this, but it does work in practice -public class TinyConfigScreenFactory implements ConfigScreenFactory { +public class TinyConfigScreenFactory implements ConfigScreenFactory { @Override - public TinyConfigScreen create(ConfigInstance config, Screen parent) { + public Screen create(ConfigInstance config, Screen parent) { + if (config.getEntries().size() == 1 + && config.getPresets().keySet().stream().allMatch(s -> s.equals(CategoryBuilder.CONFIG_PRESET_DEFAULT)) + && config.getReferencedConfigs().isEmpty() + && config.getCategories().isEmpty()) { + EntryInfo entry = config.getEntries().get(0); + Type type = entry.getValueType(); + if (!type.isInt() && !type.isLong() && !type.isFloat() && !type.isDouble() && !type.isString() && !type.isBool() && !type.isEnum()) { + final String jsonified; + try { + jsonified = GsonHolders.CONFIG.getGson().toJson(entry.getValue()); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + String key = config.getTranslationPrefix() + entry.getName(); + return new EditorScreen( + Text.translatable(key), + I18n.hasTranslation(key + ".tooltip") ? Text.translatable(key + ".tooltip") : null, + parent, + jsonified, + json -> { + try { + entry.setValue(GsonHolders.CONFIG.getGson().fromJson(json, type.asClass())); + config.write(); + } catch (Throwable e) { + LibJf.LOGGER.error("Could not write element", e); + SystemToast.add( + MinecraftClient.getInstance().getToastManager(), + SystemToast.Type.PACK_LOAD_FAILURE, + Text.translatable("libjf-config-ui-tiny-v1.entry.json.write.fail.title"), + Text.translatable("libjf-config-ui-tiny-v1.entry.json.write.fail.description") + ); + } + } + ); + } + } return new TinyConfigScreen(config, parent); }