1
mirror of https://github.com/Shiewk/Widgets.git synced 2026-04-28 11:34:17 +02:00

(2.1.1) Fix the text offset on BasicTextWidget (#5)

This commit is contained in:
Shy
2025-12-16 17:13:01 +01:00
parent b924fe6f89
commit a55c09b103
3 changed files with 19 additions and 7 deletions
@@ -11,6 +11,7 @@ import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.joml.Matrix3x2fStack;
import java.awt.*;
import java.util.List;
@@ -60,8 +61,8 @@ public abstract class BasicTextWidget extends ResizableWidget {
protected Text renderText = empty();
protected boolean shouldRender = true;
private int textX = 0;
private int textY = 0;
private float textX = 0;
private float textY = 0;
private int padding = 0;
private TextRenderer renderer = null;
private boolean textShadow = true;
@@ -114,7 +115,11 @@ public abstract class BasicTextWidget extends ResizableWidget {
if (!shouldRender) return;
renderer = textRenderer;
context.fill(posX, posY, posX + width(), posY + height(), this.backgroundColor);
context.drawText(textRenderer, renderText, posX + textX, posY + (textShadow ? textY : textY + 1), rainbow ? rainbowColor(n, rainbowSpeed) : this.textColor, textShadow);
Matrix3x2fStack matrices = context.getMatrices()
.pushMatrix();
matrices.translate(posX + textX, posY + textY, matrices);
context.drawText(textRenderer, renderText, 0, 0, rainbow ? rainbowColor(n, rainbowSpeed) : this.textColor, textShadow);
matrices.popMatrix();
}
public static int rainbowColor(long n, float speed) {
@@ -128,11 +133,18 @@ public abstract class BasicTextWidget extends ResizableWidget {
int textWidth = renderer.getWidth(renderText);
switch (textAlignment){
case LEFT -> textX = padding;
case CENTER -> textX = width() / 2 - textWidth / 2;
case CENTER -> {
if (textShadow){
textX = (width() - textWidth) / 2f;
} else {
textX = (width() - textWidth + 1) / 2f;
}
}
case RIGHT -> textX = width() - padding - textWidth;
}
float textHeight = textShadow ? 8 : 7;
textY = (height() - textHeight) / 2f;
}
textY = (height-9) / 2;
}
protected void formatAndSetRenderText(Text renderText) {
@@ -25,7 +25,7 @@ public abstract class ResizableWidget extends ModWidget {
return settings;
}
private float size = 1f;
protected float size = 1f;
@Override
public final void render(DrawContext context, long measuringTimeNano, TextRenderer textRenderer, int posX, int posY) {