Skip to content

Commit

Permalink
Fix some bugs found on forgecraft (#733)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Minecraftschurli authored Mar 11, 2024
1 parent bf39ad1 commit c9053e6
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,42 +18,49 @@

import java.util.List;

public class BookTextRenderer {
public class BookTextRenderer implements Renderable {
private final Book book;

private final List<Word> words;
private final float scale;
private final BookTextParser parser;
private final TextLayouter layouter;
private List<Word> 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);
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c9053e6

Please sign in to comment.