Text align (#10804)

* Text/ShapeText marker text alignment

* Allow `draw print` to use a dynamic align

* Highlight currently selected value when possible

* Fix alignment with newlines

* Add `lineAlign`

---------

Co-authored-by: Anuken <arnukren@gmail.com>
This commit is contained in:
Redstonneur1256
2025-07-02 20:04:43 +02:00
committed by GitHub
parent b3050c299d
commit 835c8b4a6c
8 changed files with 130 additions and 31 deletions

View File

@@ -31,10 +31,10 @@ public abstract class WorldLabelComp implements Posc, Drawc, Syncc{
@Override
public void draw(){
drawAt(text, x, y, z, flags, fontSize);
drawAt(text, x, y, z, flags, fontSize, Align.center, Align.center);
}
public static void drawAt(String text, float x, float y, float layer, int flags, float fontSize){
public static void drawAt(String text, float x, float y, float layer, int flags, float fontSize, int align, int lineAlign){
Draw.z(layer);
float z = Drawf.text();
@@ -46,14 +46,32 @@ public abstract class WorldLabelComp implements Posc, Drawc, Syncc{
font.getData().setScale(0.25f / Scl.scl(1f) * fontSize);
layout.setText(font, text);
int border = (flags & flagBackground) != 0 ? 1 : 0;
if(Align.isBottom(align)){
y += layout.height + border * 1.5f;
}else if(Align.isTop(align)){
y -= border * 1.5f;
}else{
y += layout.height / 2;
}
if(Align.isLeft(align)){
x += layout.width / 2 + border;
}else if(Align.isRight(align)){
x -= layout.width / 2 + border;
}
if((flags & flagBackground) != 0){
Draw.color(0f, 0f, 0f, 0.3f);
Fill.rect(x, y - layout.height / 2, layout.width + 2, layout.height + 3);
Draw.color();
}
float tx = Align.isLeft(lineAlign) ? -layout.width * 0.5f : Align.isRight(lineAlign) ? layout.width * 0.5f : 0;
font.setColor(Color.white);
font.draw(text, x, y, 0, Align.center, false);
font.draw(text, x + tx, y, 0, lineAlign, false);
Draw.reset();
Pools.free(layout);