[config-ui-tiny] show EditorScreen directly if config consists of single, unsupported entry

This commit is contained in:
Johannes Frohnmeyer 2023-03-18 14:14:27 +01:00
parent 0d800e2a9d
commit aafc14e3fc
Signed by: Johannes
GPG Key ID: E76429612C2929F4
3 changed files with 58 additions and 18 deletions

View File

@ -1,12 +1,13 @@
package io.gitlab.jfronny.libjf.config.api.v1.ui.tiny; 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.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; import net.minecraft.client.gui.screen.Screen;
@Deprecated @Deprecated
public interface ConfigScreen { public interface ConfigScreen {
TinyConfigScreenFactory FACTORY = new TinyConfigScreenFactory();
static Screen create(ConfigInstance config, Screen parent) { static Screen create(ConfigInstance config, Screen parent) {
return new TinyConfigScreen(config, parent); return FACTORY.create(config, parent);
} }
} }

View File

@ -303,7 +303,7 @@ public class EditorScreen extends Screen {
this.textRenderer.draw(matrices, line.text, line.x, line.y, 0xFFFFFFFF); this.textRenderer.draw(matrices, line.text, line.x, line.y, 0xFFFFFFFF);
} }
this.drawSelection(matrices, pageContent.selectionRectangles); this.drawSelection(matrices, pageContent.selectionRectangles);
this.drawCursor(matrices, pageContent.position, pageContent.atEnd); this.drawCursor(matrices, pageContent.position);
disableScissor(); disableScissor();
int i = this.getScrollbarPositionX(); int i = this.getScrollbarPositionX();
@ -322,14 +322,10 @@ public class EditorScreen extends Screen {
super.render(matrices, mouseX, mouseY, delta); 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) { if (this.tickCounter / 6 % 2 == 0) {
position = this.absolutePositionToScreenPosition(position); position = this.absolutePositionToScreenPosition(position);
if (!atEnd) { DrawableHelper.fill(matrices, position.x, position.y - 1, position.x + 1, position.y + this.textRenderer.fontHeight, 0xFFFFFFFF);
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);
}
} }
} }
@ -451,10 +447,10 @@ public class EditorScreen extends Screen {
list.add(new Line(style, string2, position.x, position.y)); list.add(new Line(style, string2, position.x, position.y));
}); });
int[] is = intList.toIntArray(); int[] is = intList.toIntArray();
boolean bl = i == content.length(); boolean atEnd = i == content.length();
int l; int l;
Position position; Position position;
if (bl && mutableBoolean.isTrue()) { if (atEnd && mutableBoolean.isTrue()) {
position = new Position(0, list.size() * this.textRenderer.fontHeight); position = new Position(0, list.size() * this.textRenderer.fontHeight);
} else { } else {
int k = getLineFromOffset(is, i); 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])); 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) { static int getLineFromOffset(int[] lineStarts, int position) {
@ -513,18 +509,16 @@ public class EditorScreen extends Screen {
} }
static class PageContent { 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; private final String pageContent;
final Position position; final Position position;
final boolean atEnd;
private final int[] lineStarts; private final int[] lineStarts;
final Line[] lines; final Line[] lines;
final Rect2i[] selectionRectangles; 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.pageContent = pageContent;
this.position = position; this.position = position;
this.atEnd = atEnd;
this.lineStarts = lineStarts; this.lineStarts = lineStarts;
this.lines = lines; this.lines = lines;
this.selectionRectangles = selectionRectangles; this.selectionRectangles = selectionRectangles;

View File

@ -1,13 +1,58 @@
package io.gitlab.jfronny.libjf.config.impl.ui.tiny; 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.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 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.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 // IDEA doesn't like this, but it does work in practice
public class TinyConfigScreenFactory implements ConfigScreenFactory<TinyConfigScreen> { public class TinyConfigScreenFactory implements ConfigScreenFactory<Screen> {
@Override @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); return new TinyConfigScreen(config, parent);
} }