diff --git a/common/src/main/java/dev/ftb/mods/ftblibrary/snbt/SNBT.java b/common/src/main/java/dev/ftb/mods/ftblibrary/snbt/SNBT.java index 640715fd..7cfac2cf 100644 --- a/common/src/main/java/dev/ftb/mods/ftblibrary/snbt/SNBT.java +++ b/common/src/main/java/dev/ftb/mods/ftblibrary/snbt/SNBT.java @@ -4,6 +4,7 @@ import net.minecraft.nbt.*; import org.jetbrains.annotations.Nullable; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -20,6 +21,18 @@ public static SNBTCompoundTag readLines(List lines) { return SNBTParser.read(lines); } + public static SNBTCompoundTag tryRead(Path path) throws IOException { + return readLines(Files.readAllLines(path, StandardCharsets.UTF_8)); + } + + public static void tryWrite(Path path, CompoundTag tag) throws IOException { + if (Files.notExists(path.getParent())) { + Files.createDirectories(path.getParent()); + } + + Files.write(path, writeLines(tag)); + } + @Nullable public static SNBTCompoundTag read(Path path) { if (Files.notExists(path) || Files.isDirectory(path) || !Files.isReadable(path)) { diff --git a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/MultilineTextBox.java b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/MultilineTextBox.java index 26f7e751..447f824a 100644 --- a/common/src/main/java/dev/ftb/mods/ftblibrary/ui/MultilineTextBox.java +++ b/common/src/main/java/dev/ftb/mods/ftblibrary/ui/MultilineTextBox.java @@ -103,6 +103,26 @@ public int cursorPos() { return textField.cursor(); } + public void selectCurrentLine() { + MultilineTextField.StringView view = textField.getLineView(textField.getLineAtCursor()); + textField.setSelecting(false); + textField.seekCursor(Whence.ABSOLUTE, view.beginIndex()); + textField.setSelecting(true); + textField.seekCursor(Whence.ABSOLUTE, view.endIndex()); + } + + public StringExtents getLineView() { + return StringExtents.of(textField.getLineView(textField.getLineAtCursor())); + } + + public StringExtents getLineView(int line) { + return StringExtents.of(textField.getLineView(line)); + } + + public StringExtents getSelected() { + return StringExtents.of(textField.getSelected()); + } + @Override public void tick() { ++frame; @@ -301,4 +321,10 @@ private void scrollToCursor() { private int innerPadding() { return 4; } + + public record StringExtents(int start, int end) { + public static StringExtents of (MultilineTextField.StringView view) { + return new StringExtents(view.beginIndex(), view.endIndex()); + } + } } diff --git a/common/src/main/java/dev/ftb/mods/ftblibrary/util/PanelPositioning.java b/common/src/main/java/dev/ftb/mods/ftblibrary/util/PanelPositioning.java new file mode 100644 index 00000000..485799c7 --- /dev/null +++ b/common/src/main/java/dev/ftb/mods/ftblibrary/util/PanelPositioning.java @@ -0,0 +1,45 @@ +package dev.ftb.mods.ftblibrary.util; + +import dev.ftb.mods.ftblibrary.config.NameMap; + +public enum PanelPositioning { + BOTTOM_LEFT(0, 2), + LEFT(0, 1), + TOP_LEFT(0, 0), + TOP_CENTER(1, 0), + TOP_RIGHT(1, 0), + RIGHT(1, 1), + BOTTOM_RIGHT(1, 2), + BOTTOM_CENTER(1, 2); + + public static final NameMap NAME_MAP = NameMap.of(TOP_RIGHT, values()).baseNameKey("ftbquests.panel.position").create(); + + private final int posX; + private final int posY; + + PanelPositioning(int x, int y) { + posX = x; + posY = y; + } + + public PanelPos getPanelPos(int screenW, int screenH, int panelW, int panelH, int insetX, int insetY) { + int px = switch (posX) { + case 0 -> insetX; + case 1 -> (screenW - panelW) / 2; + default -> screenW - panelW - insetX; + }; + int py = switch (posY) { + case 0 -> insetY; + case 1 -> (screenH - panelH) / 2; + default -> screenH - panelH - insetY; + }; + return new PanelPos(px, py); + } + + public PanelPos getPanelPos(int screenW, int screenH, int panelW, int panelH, float insetX, float insetY) { + return getPanelPos(screenW, screenH, panelW, panelH, (int)(screenW * insetX / 2), (int)(screenH * insetY / 2)); + } + + public record PanelPos(int x, int y) { + } +} diff --git a/gradle.properties b/gradle.properties index f6b28826..42a2f8c5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.daemon=false mod_id=ftblibrary archives_base_name=ftb-library maven_group=dev.ftb.mods -mod_version=2001.1.3 +mod_version=2001.1.4 mod_author=FTB Team minecraft_version=1.20.1 architectury_version=9.0.8