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

@@ -147,6 +147,8 @@ public class GlobalVars{
put("@" + sensor.name(), sensor);
}
LStatement.nameToAlign.each((name, align) -> put("@" + name, align));
logicIdToContent = new UnlockableContent[ContentType.all.length][];
contentIdToLogicId = new int[ContentType.all.length][];

View File

@@ -897,7 +897,7 @@ public class LExecutor{
int advance = (int)data.spaceXadvance, lineHeight = (int)data.lineHeight;
int xOffset, yOffset;
int align = p1.id; //p1 is not a variable, it's a raw align value. what a massive hack
int align = p1.numi();
int maxWidth = 0, lines = 1, lineWidth = 0;
for(int i = 0; i < str.length(); i++){

View File

@@ -18,6 +18,8 @@ public enum LMarkerControl{
flushText("fetch"),
fontSize("size"),
textHeight("height"),
textAlign("align"),
lineAlign("align"),
labelFlags("background", "outline"),
texture("printFlush", "name"),
textureSize("width", "height"),

View File

@@ -23,6 +23,25 @@ import static mindustry.logic.LCanvas.*;
* A statement is an intermediate representation of an instruction, to be used mostly in UI.
* Contains all relevant variable information. */
public abstract class LStatement{
private static final String[] aligns = {"topLeft", "top", "topRight", "left", "center", "right", "bottomLeft", "bottom", "bottomRight"};
public static final ObjectMap<String, Integer> nameToAlign = ObjectMap.of(
"center", Align.center,
"top", Align.top,
"bottom", Align.bottom,
"left", Align.left,
"right", Align.right,
"topLeft", Align.topLeft,
"topRight", Align.topRight,
"bottomLeft", Align.bottomLeft,
"bottomRight", Align.bottomRight
);
public static final IntMap<String> alignToName = new IntMap<>();
static {
nameToAlign.each((k, v) -> alignToName.put(v, k));
}
public transient @Nullable StatementElem elem;
public abstract void build(Table table);
@@ -175,7 +194,36 @@ public abstract class LStatement{
showSelect(b, values, current, getter, 4, c -> {});
}
protected void showSelectTable(Button b, Cons2<Table, Runnable> hideCons){
protected void fieldAlignSelect(Table t, Prov<String> get, Cons<String> set, boolean hor, boolean ver) {
t.button(b -> {
b.image(Icon.pencilSmall);
b.clicked(() -> {
var current = get.get();
showAlignSelect(b, current.startsWith("@") ? nameToAlign.get(current.substring(1), -1) : -1, align -> set.get("@" + alignToName.get(align)), hor, ver);
});
}, Styles.logict, () -> {}).size(40f).color(t.color).left().padLeft(-10);
}
public static void showAlignSelect(Button b, int current, Intc setter, boolean hor, boolean ver) {
showSelectTable(b, (t, hide) -> {
t.defaults().size(150f, 40f);
int i = 0;
for(String align : aligns){
int val = nameToAlign.get(align);
if(!hor && !Align.isCenterHorizontal(val)) continue;
if(!ver && !Align.isCenterVertical(val)) continue;
t.button(align, Styles.logicTogglet, () -> {
setter.get(val);
hide.run();
}).checked(current == nameToAlign.get(align)).grow();
if (++i % 3 == 0) t.row();
}
});
}
protected static void showSelectTable(Button b, Cons2<Table, Runnable> hideCons){
Table t = new Table(Tex.paneSolid){
@Override
public float getPrefHeight(){

View File

@@ -6,7 +6,6 @@ import arc.graphics.*;
import arc.scene.style.*;
import arc.scene.ui.*;
import arc.scene.ui.layout.*;
import arc.struct.*;
import arc.util.*;
import mindustry.*;
import mindustry.annotations.Annotations.*;
@@ -123,19 +122,6 @@ public class LStatements{
@RegisterStatement("draw")
public static class DrawStatement extends LStatement{
static final String[] aligns = {"center", "top", "bottom", "left", "right", "topLeft", "topRight", "bottomLeft", "bottomRight"};
//yes, boxing Integer is gross but this is easier to construct and Integers <128 don't allocate anyway
static final ObjectMap<String, Integer> nameToAlign = ObjectMap.of(
"center", Align.center,
"top", Align.top,
"bottom", Align.bottom,
"left", Align.left,
"right", Align.right,
"topLeft", Align.topLeft,
"topRight", Align.topRight,
"bottomLeft", Align.bottomLeft,
"bottomRight", Align.bottomRight
);
public GraphicsType type = GraphicsType.clear;
public String x = "0", y = "0", p1 = "0", p2 = "0", p3 = "0", p4 = "0";
@@ -165,7 +151,7 @@ public class LStatements{
}
if(type == GraphicsType.print){
p1 = "bottomLeft";
p1 = "@bottomLeft";
}
rebuild(table);
@@ -253,13 +239,11 @@ public class LStatements{
row(s);
s.add("align ");
s.button(b -> {
b.label(() -> nameToAlign.containsKey(p1) ? p1 : "bottomLeft");
b.clicked(() -> showSelect(b, aligns, p1, t -> {
p1 = t;
}, 2, cell -> cell.size(165, 50)));
}, Styles.logict, () -> {}).size(165, 40).color(s.color).left().padLeft(2);
fields(s, "align", p1, v -> p1 = v);
fieldAlignSelect(s, () -> p1, v -> {
p1 = v;
rebuild(table);
}, true, true);
}
case translate, scale -> {
fields(s, "x", x, v -> x = v);
@@ -278,12 +262,15 @@ public class LStatements{
if(type == GraphicsType.color && p2.equals("0")){
p2 = "255";
}
if(type == GraphicsType.print && nameToAlign.get(p1) != null){
p1 = "@" + p1;
}
}
@Override
public LInstruction build(LAssembler builder){
return new DrawI((byte)type.ordinal(), builder.var(x), builder.var(y),
type == GraphicsType.print ? new LVar(p1, nameToAlign.get(p1, Align.bottomLeft), true) : builder.var(p1), builder.var(p2), builder.var(p3), builder.var(p4));
return new DrawI((byte)type.ordinal(), builder.var(x), builder.var(y), builder.var(p1), builder.var(p2), builder.var(p3), builder.var(p4));
}
@Override
@@ -2399,6 +2386,11 @@ public class LStatements{
}).width(240f).left();
}));
}, Styles.logict, () -> {}).size(40f).padLeft(-11).color(table.color);
}else if(type == LMarkerControl.textAlign || type == LMarkerControl.lineAlign){
fieldAlignSelect(t, () -> p1, v -> {
p1 = v;
rebuild(table);
}, true, type != LMarkerControl.lineAlign);
}
});