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:
+1
-1
@@ -6,7 +6,7 @@ minecraft_version=1.21.9
|
|||||||
yarn_mappings=1.21.9+build.1
|
yarn_mappings=1.21.9+build.1
|
||||||
loader_version=0.17.2
|
loader_version=0.17.2
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version=2.1.0
|
mod_version=2.1.1
|
||||||
maven_group=de.shiewk
|
maven_group=de.shiewk
|
||||||
archives_base_name=Widgets
|
archives_base_name=Widgets
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import net.minecraft.client.font.TextRenderer;
|
|||||||
import net.minecraft.client.gui.DrawContext;
|
import net.minecraft.client.gui.DrawContext;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
import org.joml.Matrix3x2fStack;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -60,8 +61,8 @@ public abstract class BasicTextWidget extends ResizableWidget {
|
|||||||
|
|
||||||
protected Text renderText = empty();
|
protected Text renderText = empty();
|
||||||
protected boolean shouldRender = true;
|
protected boolean shouldRender = true;
|
||||||
private int textX = 0;
|
private float textX = 0;
|
||||||
private int textY = 0;
|
private float textY = 0;
|
||||||
private int padding = 0;
|
private int padding = 0;
|
||||||
private TextRenderer renderer = null;
|
private TextRenderer renderer = null;
|
||||||
private boolean textShadow = true;
|
private boolean textShadow = true;
|
||||||
@@ -114,7 +115,11 @@ public abstract class BasicTextWidget extends ResizableWidget {
|
|||||||
if (!shouldRender) return;
|
if (!shouldRender) return;
|
||||||
renderer = textRenderer;
|
renderer = textRenderer;
|
||||||
context.fill(posX, posY, posX + width(), posY + height(), this.backgroundColor);
|
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) {
|
public static int rainbowColor(long n, float speed) {
|
||||||
@@ -128,11 +133,18 @@ public abstract class BasicTextWidget extends ResizableWidget {
|
|||||||
int textWidth = renderer.getWidth(renderText);
|
int textWidth = renderer.getWidth(renderText);
|
||||||
switch (textAlignment){
|
switch (textAlignment){
|
||||||
case LEFT -> textX = padding;
|
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;
|
case RIGHT -> textX = width() - padding - textWidth;
|
||||||
}
|
}
|
||||||
|
float textHeight = textShadow ? 8 : 7;
|
||||||
|
textY = (height() - textHeight) / 2f;
|
||||||
}
|
}
|
||||||
textY = (height-9) / 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void formatAndSetRenderText(Text renderText) {
|
protected void formatAndSetRenderText(Text renderText) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public abstract class ResizableWidget extends ModWidget {
|
|||||||
return settings;
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private float size = 1f;
|
protected float size = 1f;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void render(DrawContext context, long measuringTimeNano, TextRenderer textRenderer, int posX, int posY) {
|
public final void render(DrawContext context, long measuringTimeNano, TextRenderer textRenderer, int posX, int posY) {
|
||||||
|
|||||||
Reference in New Issue
Block a user