From cdda57015784cbbfaa137dfd3528f5a4c04d79f9 Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Mon, 22 Jan 2024 19:21:38 +0100 Subject: [PATCH] Add textfield appendix type --- .../cyclopscore/infobook/InfoBookParser.java | 9 ++ .../pageelement/TextFieldAppendix.java | 101 ++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/main/java/org/cyclops/cyclopscore/infobook/pageelement/TextFieldAppendix.java diff --git a/src/main/java/org/cyclops/cyclopscore/infobook/InfoBookParser.java b/src/main/java/org/cyclops/cyclopscore/infobook/InfoBookParser.java index 0f41eeb190..d3d8190597 100644 --- a/src/main/java/org/cyclops/cyclopscore/infobook/InfoBookParser.java +++ b/src/main/java/org/cyclops/cyclopscore/infobook/InfoBookParser.java @@ -83,6 +83,15 @@ public SectionAppendix create(IInfoBook infoBook, Element node) throws InvalidAp Integer.parseInt(node.getAttribute("width")), Integer.parseInt(node.getAttribute("height"))); } + }); + registerAppendixFactory("textfield", new IAppendixFactory() { + + @Override + public SectionAppendix create(IInfoBook infoBook, Element node) throws InvalidAppendixException { + return new TextFieldAppendix(infoBook, node.getTextContent(), + Double.parseDouble(node.getAttribute("scale"))); + } + }); InfoBookParser.registerAppendixRecipeFactories(RecipeType.CRAFTING, CraftingRecipeAppendix::new); InfoBookParser.registerAppendixRecipeFactories(RecipeType.SMELTING, FurnaceRecipeAppendix::new); diff --git a/src/main/java/org/cyclops/cyclopscore/infobook/pageelement/TextFieldAppendix.java b/src/main/java/org/cyclops/cyclopscore/infobook/pageelement/TextFieldAppendix.java new file mode 100644 index 0000000000..1fbeecb680 --- /dev/null +++ b/src/main/java/org/cyclops/cyclopscore/infobook/pageelement/TextFieldAppendix.java @@ -0,0 +1,101 @@ +package org.cyclops.cyclopscore.infobook.pageelement; + +import com.google.common.collect.Lists; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.StringSplitter; +import net.minecraft.network.chat.Style; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import org.apache.commons.lang3.StringUtils; +import org.cyclops.cyclopscore.helper.Helpers; +import org.cyclops.cyclopscore.helper.RenderHelpers; +import org.cyclops.cyclopscore.infobook.IInfoBook; +import org.cyclops.cyclopscore.infobook.InfoSection; +import org.cyclops.cyclopscore.infobook.ScreenInfoBook; + +import java.util.List; + +/** + * Text fields that can be added to sections. + * @author rubensworks + */ +public class TextFieldAppendix extends SectionAppendix { + + private static final int OFFSET_Y = 0; + + private final String text; + private final double scale; + private int height; + private int maxWidth; + private List lines = null; + + public TextFieldAppendix(IInfoBook infoBook, String text, double scale) { + super(infoBook); + this.text = text; + this.scale = scale; + this.height = this.text.split("\n").length * 9; // Set temp value + } + + @Override + protected int getOffsetY() { + return OFFSET_Y; + } + + @Override + protected int getWidth() { + return this.maxWidth == 0 ? 120 : this.maxWidth; + } + + @Override + protected int getHeight() { + return height; + } + + @Override + @OnlyIn(Dist.CLIENT) + protected void drawElement(ScreenInfoBook gui, PoseStack matrixStack, int x, int y, int width, int height, int page, int mx, int my) { + if (this.lines == null) { + StringSplitter stringSplitter = gui.getFont().getSplitter(); + this.lines = Lists.newArrayList(); + stringSplitter.splitLines(this.text, this.getWidth(), Style.EMPTY, true, (style, startPos, endPos) -> { + String stringPart = this.text.substring(startPos, endPos); + String line = StringUtils.stripEnd(stringPart, " \n"); + lines.add(line); + this.maxWidth = (int) Math.max(this.maxWidth, gui.getFont().width(line) * this.scale); + }); + this.height = (int) (this.scale * lines.size() * gui.getFont().lineHeight); + } + + int lineId = 0; + for (String line : lines) { + RenderHelpers.drawScaledString( + matrixStack, + gui.getFont(), + line, + x, + (int) (y + (((float) lineId) * gui.getFont().lineHeight * this.scale)), + (float) this.scale, + Helpers.RGBToInt(10, 10, 10) + ); + lineId++; + } + + gui.drawOuterBorder(matrixStack, x - 1, y - 1, getWidth() + 2, getHeight() + 2, 0.5F, 0.5F, 0.5F, 0.4f); + } + + @Override + @OnlyIn(Dist.CLIENT) + protected void postDrawElement(ScreenInfoBook gui, PoseStack matrixStack, int x, int y, int width, int height, int page, int mx, int my) { + + } + + @Override + public void preBakeElement(InfoSection infoSection) { + + } + + @Override + public void bakeElement(InfoSection infoSection) { + + } +}