From c9053e60b4d517b0714456b7bc8501c86b795a7d Mon Sep 17 00:00:00 2001 From: Minecraftschurli Date: Mon, 11 Mar 2024 11:26:39 +0100 Subject: [PATCH] Fix some bugs found on forgecraft (#733) * Fixes previous page button missing * Fixes previous and next page buttons being rendered for 1 tick when navigating * Fixes entry unlock toast having the wrong colors and using the missing texture * Fixes text input not being selectable in book editor --- .../client/base/ClientAdvancements.java | 11 ++--- .../client/book/gui/BookTextRenderer.java | 38 +++++++++------- .../client/book/gui/GuiBookEntryList.java | 2 +- .../client/book/gui/GuiBookLanding.java | 2 +- .../client/book/gui/GuiBookWriter.java | 44 +++++++++++-------- .../client/book/gui/button/GuiButtonBook.java | 5 ++- .../client/book/page/abstr/PageWithText.java | 2 +- .../template/component/ComponentText.java | 2 +- 8 files changed, 59 insertions(+), 47 deletions(-) diff --git a/Xplat/src/main/java/vazkii/patchouli/client/base/ClientAdvancements.java b/Xplat/src/main/java/vazkii/patchouli/client/base/ClientAdvancements.java index 60ae3416..7a3f3c58 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/base/ClientAdvancements.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/base/ClientAdvancements.java @@ -1,7 +1,5 @@ package vazkii.patchouli.client.base; -import com.mojang.blaze3d.systems.RenderSystem; - import net.minecraft.advancements.AdvancementHolder; import net.minecraft.advancements.AdvancementProgress; import net.minecraft.client.Minecraft; @@ -83,14 +81,11 @@ public Book getToken() { @NotNull @Override public Visibility render(GuiGraphics graphics, ToastComponent toastGui, long delta) { - RenderSystem.setShaderTexture(0, BACKGROUND_SPRITE); - - RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); - graphics.blit(BACKGROUND_SPRITE, 0, 0, 0, 32, 160, 32); + graphics.blitSprite(BACKGROUND_SPRITE, 0, 0, width(), height()); Font font = toastGui.getMinecraft().font; - graphics.drawString(font, Component.translatable(book.name), 30, 7, -11534256, false); - graphics.drawString(font, Component.translatable("patchouli.gui.lexicon.toast.info"), 30, 17, -16777216, false); + graphics.drawString(font, Component.translatable(book.name), 30, 7, 0xfff000f0, false); + graphics.drawString(font, Component.translatable("patchouli.gui.lexicon.toast.info"), 30, 17, 0xffffffff, false); graphics.renderItem(book.getBookItem(), 8, 8); graphics.renderItemDecorations(font, book.getBookItem(), 8, 8); diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/BookTextRenderer.java b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/BookTextRenderer.java index 77e4097d..73ff0c74 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/BookTextRenderer.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/BookTextRenderer.java @@ -3,6 +3,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.Renderable; import net.minecraft.client.resources.language.I18n; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Style; @@ -17,11 +18,13 @@ import java.util.List; -public class BookTextRenderer { +public class BookTextRenderer implements Renderable { private final Book book; - private final List words; - private final float scale; + private final BookTextParser parser; + private final TextLayouter layouter; + private List words; + private float scale; public BookTextRenderer(GuiBook gui, Component text, int x, int y) { this(gui, text, x, y, GuiBook.PAGE_WIDTH, GuiBook.TEXT_LINE_HEIGHT, gui.book.textColor); @@ -29,30 +32,35 @@ public BookTextRenderer(GuiBook gui, Component text, int x, int y) { public BookTextRenderer(GuiBook gui, Component text, int x, int y, int width, int lineHeight, int baseColor) { this.book = gui.book; - Component text1; - if (book.i18n && text.getContents() instanceof PlainTextContents.LiteralContents lc) { - text1 = Component.literal(I18n.get(lc.text())); - } else { - text1 = text; - } Style baseStyle = book.getFontStyle().withColor(TextColor.fromRgb(baseColor)); - var parser = new BookTextParser(gui, this.book, x, y, width, lineHeight, baseStyle); + this.parser = new BookTextParser(gui, this.book, x, y, width, lineHeight, baseStyle); var overflowMode = this.book.overflowMode; if (overflowMode == null) { overflowMode = PatchouliConfig.get().overflowMode(); } - var layouter = new TextLayouter(gui, x, y, lineHeight, width, overflowMode); - layouter.layout(Minecraft.getInstance().font, parser.parse(text1)); - this.scale = layouter.getScale(); - this.words = layouter.getWords(); + this.layouter = new TextLayouter(gui, x, y, lineHeight, width, overflowMode); + setText(text); + } + + void setText(Component text) { + Component text1; + if (this.book.i18n && text.getContents() instanceof PlainTextContents.LiteralContents lc) { + text1 = Component.literal(I18n.get(lc.text())); + } else { + text1 = text; + } + this.layouter.layout(Minecraft.getInstance().font, this.parser.parse(text1)); + this.scale = this.layouter.getScale(); + this.words = this.layouter.getWords(); } private double rescale(double in, double origin) { return origin + (in - origin) / scale; } - public void render(GuiGraphics graphics, int mouseX, int mouseY) { + @Override + public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) { if (!words.isEmpty()) { Font font = Minecraft.getInstance().font; Style style = book.getFontStyle(); diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntryList.java b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntryList.java index 3f4155b6..078cddcf 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntryList.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntryList.java @@ -92,7 +92,7 @@ void drawForegroundElements(GuiGraphics graphics, int mouseX, int mouseY, float drawSeparator(graphics, book, LEFT_PAGE_X, TOP_PADDING + 12); drawSeparator(graphics, book, RIGHT_PAGE_X, TOP_PADDING + 12); - text.render(graphics, mouseX, mouseY); + text.render(graphics, mouseX, mouseY, partialTicks); if (shouldDrawProgressBar()) { drawProgressBar(graphics, book, mouseX, mouseY, this::doesEntryCountForProgress); } diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookLanding.java b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookLanding.java index 8bc73d46..449ef2c0 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookLanding.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookLanding.java @@ -115,7 +115,7 @@ private void addCategoryButton(int i, BookCategory category) { @Override void drawForegroundElements(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) { if (text != null) { - text.render(graphics, mouseX, mouseY); + text.render(graphics, mouseX, mouseY, partialTicks); } int topSeparator = TOP_PADDING + 12; diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookWriter.java b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookWriter.java index 86c2e688..9cf0b8f6 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookWriter.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/GuiBookWriter.java @@ -26,14 +26,15 @@ public GuiBookWriter(Book book) { public void init() { super.init(); - text = new BookTextRenderer(this, Component.translatable("patchouli.gui.lexicon.editor.info"), LEFT_PAGE_X, TOP_PADDING + 20); - textfield = new EditBox(font, 10, FULL_HEIGHT - 40, PAGE_WIDTH, 20, Component.empty()); - textfield.setMaxLength(Integer.MAX_VALUE); - textfield.setValue(savedText); - - var button = new GuiButtonBook(this, bookLeft + 115, bookTop + PAGE_HEIGHT - 36, 330, 9, 11, 11, this::handleToggleHeaderButton, - Component.translatable("patchouli.gui.lexicon.button.toggle_mock_header")); - addRenderableWidget(button); + this.text = new BookTextRenderer(this, Component.translatable("patchouli.gui.lexicon.editor.info"), LEFT_PAGE_X, TOP_PADDING + 20); + this.textfield = new EditBox(font, 15, FULL_HEIGHT - 40, PAGE_WIDTH, 20, textfield, Component.empty()); + this.textfield.setMaxLength(Integer.MAX_VALUE); + if (this.textfield.getValue().isEmpty()) { + this.textfield.setValue(savedText); + } + this.editableText = new BookTextRenderer(this, Component.literal(""), RIGHT_PAGE_X, TOP_PADDING + (drawHeader ? 22 : -4)); + + addRenderableWidget(new GuiButtonBook(this, bookLeft + 115, bookTop + PAGE_HEIGHT - 36, 330, 9, 11, 11, this::handleToggleHeaderButton, Component.translatable("patchouli.gui.lexicon.button.toggle_mock_header"))); refreshText(); } @@ -50,16 +51,23 @@ void drawForegroundElements(GuiGraphics graphics, int mouseX, int mouseY, float } textfield.render(graphics, mouseX, mouseY, partialTicks); - text.render(graphics, mouseX, mouseY); - editableText.render(graphics, mouseX, mouseY); + text.render(graphics, mouseX, mouseY, partialTicks); + editableText.render(graphics, mouseX, mouseY, partialTicks); } @Override public boolean mouseClickedScaled(double mouseX, double mouseY, int mouseButton) { - return textfield.mouseClicked(getRelativeX(mouseX), getRelativeY(mouseY), mouseButton) - || text.click(mouseX, mouseY, mouseButton) - || editableText.click(mouseX, mouseY, mouseButton) - || super.mouseClickedScaled(mouseX, mouseY, mouseButton); + if (textfield.mouseClicked(getRelativeX(mouseX), getRelativeY(mouseY), mouseButton)) { + textfield.setFocused(true); + return true; + } + if (text.click(mouseX, mouseY, mouseButton)) { + return true; + } + if (editableText.click(mouseX, mouseY, mouseButton)) { + return true; + } + return super.mouseClickedScaled(mouseX, mouseY, mouseButton); } @Override @@ -84,17 +92,15 @@ public boolean charTyped(char c, int i) { private void handleToggleHeaderButton(Button button) { drawHeader = !drawHeader; - refreshText(); + init(); } private void refreshText() { - int yPos = TOP_PADDING + (drawHeader ? 22 : -4); - savedText = textfield.getValue(); try { - editableText = new BookTextRenderer(this, Component.literal(savedText), RIGHT_PAGE_X, yPos); + editableText.setText(Component.literal(savedText)); } catch (Throwable e) { - editableText = new BookTextRenderer(this, Component.literal("[ERROR]"), RIGHT_PAGE_X, yPos); + editableText.setText(Component.literal("[ERROR]")); PatchouliAPI.LOGGER.catching(e); } } diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/button/GuiButtonBook.java b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/button/GuiButtonBook.java index 95eac0ab..75946b48 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/gui/button/GuiButtonBook.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/gui/button/GuiButtonBook.java @@ -35,7 +35,10 @@ public GuiButtonBook(GuiBook parent, int x, int y, int u, int v, int w, int h, S @Override public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) { - active = visible = displayCondition.get(); + active = displayCondition.get(); + if (!active) { + return; + } RenderSystem.setShaderColor(1F, 1F, 1F, 1F); GuiBook.drawFromTexture(graphics, parent.book, getX(), getY(), u + (isHoveredOrFocused() ? width : 0), v, width, height); if (isHoveredOrFocused()) { diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/page/abstr/PageWithText.java b/Xplat/src/main/java/vazkii/patchouli/client/book/page/abstr/PageWithText.java index 60b006cf..6b181d04 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/page/abstr/PageWithText.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/page/abstr/PageWithText.java @@ -30,7 +30,7 @@ public void onDisplayed(GuiBookEntry parent, int left, int top) { @Override public void render(GuiGraphics graphics, int mouseX, int mouseY, float pticks) { if (shouldRenderText()) { - textRender.render(graphics, mouseX, mouseY); + textRender.render(graphics, mouseX, mouseY, pticks); } } diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/template/component/ComponentText.java b/Xplat/src/main/java/vazkii/patchouli/client/book/template/component/ComponentText.java index d22e59d9..900553d3 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/template/component/ComponentText.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/template/component/ComponentText.java @@ -52,7 +52,7 @@ public void onDisplayed(BookPage page, GuiBookEntry parent, int left, int top) { @Override public void render(GuiGraphics graphics, BookPage page, int mouseX, int mouseY, float pticks) { - textRenderer.render(graphics, mouseX, mouseY); + textRenderer.render(graphics, mouseX, mouseY, pticks); } @Override