Skip to content

Commit

Permalink
Add support for specifying text alignment to our wrapped text renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Oct 12, 2024
1 parent 69de805 commit ecb7368
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

public class GuiConfirmationDialog extends GuiWindow {

private static final int PADDING = 5;

private final WrappedTextRenderer wrappedTextRenderer;

private GuiConfirmationDialog(IGuiWrapper gui, int x, int y, int width, int height, ReplaceableWrappedTextRenderer renderer, Runnable onConfirm, DialogType type) {
Expand All @@ -28,14 +30,14 @@ private GuiConfirmationDialog(IGuiWrapper gui, int x, int y, int width, int heig
public static void show(IGuiWrapper gui, Component title, Runnable onConfirm, DialogType type) {
int width = 140;
ReplaceableWrappedTextRenderer renderer = new ReplaceableWrappedTextRenderer(gui, width, title);
int height = 33 + renderer.getRequiredHeight(width - 10);
int height = 33 + renderer.getRequiredHeight(width - 2 * PADDING);
gui.addWindow(new GuiConfirmationDialog(gui, (gui.getXSize() - width) / 2, (gui.getYSize() - height) / 2, width, height, renderer, onConfirm, type));
}

@Override
public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
super.renderForeground(guiGraphics, mouseX, mouseY);
wrappedTextRenderer.renderCentered(guiGraphics, relativeX, relativeY + 6, titleTextColor(), width - 10);
wrappedTextRenderer.render(guiGraphics, relativeX + PADDING, relativeY + 6, TextAlignment.CENTER, titleTextColor(), width - 2 * PADDING);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
upgradeTypeData.put(selectedType, textRenderer);
}
int screenWidth = rightScreen.getWidth() - 2;
int lines = textRenderer.renderWithScale(guiGraphics, rightScreen.getRelativeX() + 2, rightScreen.getRelativeY() + 2, screenTextColor(),
int lines = textRenderer.renderWithScale(guiGraphics, rightScreen.getRelativeX() + 2, rightScreen.getRelativeY() + 2, TextAlignment.LEFT, screenTextColor(),
screenWidth - 2, 0.6F);
int textY = 4 + 6 * lines;
rightScreen.drawScaledScrollingString(guiGraphics, MekanismLang.UPGRADE_COUNT.translate(amount, selectedType.getMax()), 0, textY, TextAlignment.LEFT,
Expand All @@ -99,7 +99,7 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
rightScreen.drawScaledScrollingString(guiGraphics, component, 0, textY, TextAlignment.LEFT, screenTextColor(), screenWidth, 2, false, 0.6F);
}
} else {
noSelection.renderWithScale(guiGraphics, rightScreen.getRelativeX() + 2, rightScreen.getRelativeY() + 2, screenTextColor(), 56, 0.8F);
noSelection.renderWithScale(guiGraphics, rightScreen.getRelativeX() + 2, rightScreen.getRelativeY() + 2, TextAlignment.LEFT, screenTextColor(), 56, 0.8F);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void drawForegroundText(@NotNull GuiGraphics guiGraphics, int mouseX,
renderTitleText(guiGraphics);
renderInventoryText(guiGraphics);
screen.drawScrollingString(guiGraphics, MekanismLang.PROCESS_RATE.translate(TextUtils.getPercent(tile.getProcessRate())), 0,
screen.getHeight() - getLineHeight() - 2, TextAlignment.LEFT, screenTextColor(), 2, false);
screen.getHeight() - getLineHeight() - 2, TextAlignment.CENTER, screenTextColor(), 2, false);
super.drawForegroundText(guiGraphics, mouseX, mouseY);
PoseStack pose = guiGraphics.pose();
pose.pushPose();
Expand Down
36 changes: 16 additions & 20 deletions src/main/java/mekanism/client/render/IFancyFontRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ default void drawScrollingString(GuiGraphics guiGraphics, Component text, int mi
if (isScrolling) {
targetX = prepScrollingString(guiGraphics, textWidth, areaWidth, minX, minY, maxX, maxY);
} else {
targetX = alignment.getTarget(minX, maxX, areaWidth, textWidth);
targetX = alignment.getTarget(font(), minX, maxX, textWidth);
}
guiGraphics.drawString(font(), text.getVisualOrderText(), targetX, targetY, color, shadow);
if (isScrolling) {
Expand Down Expand Up @@ -130,7 +130,7 @@ default void drawScaledScrollingString(GuiGraphics guiGraphics, Component text,
if (isScrolling) {
targetX = prepScrollingString(guiGraphics, textWidth, areaWidth, minX, minY, maxX, maxY);
} else {
targetX = alignment.getTarget(minX, maxX, areaWidth, textWidth);
targetX = alignment.getTarget(font(), minX, maxX, textWidth);
}
PoseStack pose = prepTextScale(guiGraphics, targetX, targetY, scale);
guiGraphics.drawString(font(), text, 0, 0, color, shadow);
Expand Down Expand Up @@ -182,16 +182,18 @@ private PoseStack prepTextScale(GuiGraphics guiGraphics, float x, float y, float
enum TextAlignment {
LEFT,
CENTER,
RIGHT;
RIGHT,
/**
* Represents that for left to right languages this will be left aligned, and for right to left it will be right aligned.
*/
RELATIVE;//TODO: Make use of this in various spots that make sense

public float getTarget(int minX, int maxX, float areaWidth, float textWidth) {
//TODO: Do we want to swap left and right when Font#isBidirectional is true?
// I believe in right to left languages text is meant to be aligned to the right
// but it likely would look odd in various GUIs
public float getTarget(Font font, int minX, int maxX, float textWidth) {
return switch (this) {
case LEFT -> minX;
case CENTER -> minX + (areaWidth - textWidth) / 2F;
case CENTER -> minX + ((maxX - minX) - textWidth) / 2F;
case RIGHT -> maxX - textWidth;
case RELATIVE -> font.isBidirectional() ? maxX - textWidth : minX;
};
}
}
Expand All @@ -211,30 +213,24 @@ public WrappedTextRenderer(IFancyFontRenderer fontRenderer, Component text) {
this.text = text;
}

public void renderCentered(GuiGraphics guiGraphics, int x, int y, int color, int maxLength) {
public void render(GuiGraphics guiGraphics, int x, int y, TextAlignment alignment, int color, int maxLength) {
calculateLines(maxLength);
drawLines(guiGraphics, x, y, color, true);
drawLines(guiGraphics, x, y, maxLength, alignment, color, 1);
}

public int renderWithScale(GuiGraphics guiGraphics, int x, int y, int color, int maxLength, float scale) {
public int renderWithScale(GuiGraphics guiGraphics, int x, int y, TextAlignment alignment, int color, int maxLength, float scale) {
//Divide by scale for calculating actual max length so that when the text is scaled it has the proper total space available
calculateLines(Mth.floor(maxLength / scale));
PoseStack pose = fontRenderer.prepTextScale(guiGraphics, x, y, scale);
drawLines(guiGraphics, 0, 0, color, false);
drawLines(guiGraphics, 0, 0, maxLength, alignment, color, scale);
pose.popPose();
return linesToDraw.size();
}

private void drawLines(GuiGraphics guiGraphics, int x, int startY, int color, boolean center) {
private void drawLines(GuiGraphics guiGraphics, int x, int startY, int maxLength, TextAlignment alignment, int color, float scale) {
Font font = fontRenderer.font();
for (FormattedCharSequence line : linesToDraw) {
float targetX;
if (center) {
targetX = x + (fontRenderer.getXSize() - font.width(line)) / 2F;
} else {
targetX = x;
}
guiGraphics.drawString(font, line, targetX, startY, color, false);
guiGraphics.drawString(font, line, alignment.getTarget(font, x, x + maxLength, font.width(line) * scale), startY, color, false);
startY += font.lineHeight;
}
}
Expand Down

0 comments on commit ecb7368

Please sign in to comment.