From e3b24ac9800436893df09a008972ffaa62470415 Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 26 Feb 2024 10:29:14 -0500 Subject: [PATCH] WIP logic display transform matrix support --- core/src/mindustry/logic/LExecutor.java | 7 +- core/src/mindustry/logic/LStatements.java | 7 ++ .../world/blocks/logic/LogicDisplay.java | 68 +++++++++++++++++-- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index d991a727eb..8d02218748 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -1025,14 +1025,17 @@ public class LExecutor{ exec.textBuffer.setLength(0); } }else{ - int num1 = exec.numi(p1); + int num1 = exec.numi(p1), xval = packSign(exec.numi(x)), yval = packSign(exec.numi(y)); if(type == LogicDisplay.commandImage){ num1 = exec.obj(p1) instanceof UnlockableContent u ? u.iconId : 0; + }else if(type == LogicDisplay.commandScale){ + xval = packSign((int)(exec.numf(x) / LogicDisplay.scaleStep)); + yval = packSign((int)(exec.numf(y) / LogicDisplay.scaleStep)); } //add graphics calls, cap graphics buffer size - exec.graphicsBuffer.add(DisplayCmd.get(type, packSign(exec.numi(x)), packSign(exec.numi(y)), packSign(num1), packSign(exec.numi(p2)), packSign(exec.numi(p3)), packSign(exec.numi(p4)))); + exec.graphicsBuffer.add(DisplayCmd.get(type, xval, yval, packSign(num1), packSign(exec.numi(p2)), packSign(exec.numi(p3)), packSign(exec.numi(p4)))); } } diff --git a/core/src/mindustry/logic/LStatements.java b/core/src/mindustry/logic/LStatements.java index 32b812ee2b..ceae4740c6 100644 --- a/core/src/mindustry/logic/LStatements.java +++ b/core/src/mindustry/logic/LStatements.java @@ -260,6 +260,13 @@ public class LStatements{ }, 2, cell -> cell.size(165, 50))); }, Styles.logict, () -> {}).size(165, 40).color(s.color).left().padLeft(2); } + case translate, scale -> { + fields(s, "x", x, v -> x = v); + fields(s, "y", y, v -> y = v); + } + case rotate -> { + fields(s, "degrees", p1, v -> p1 = v); + } } }).expand().left(); } diff --git a/core/src/mindustry/world/blocks/logic/LogicDisplay.java b/core/src/mindustry/world/blocks/logic/LogicDisplay.java index 2be75a1e05..8de36fbf91 100644 --- a/core/src/mindustry/world/blocks/logic/LogicDisplay.java +++ b/core/src/mindustry/world/blocks/logic/LogicDisplay.java @@ -4,8 +4,10 @@ import arc.*; import arc.graphics.*; import arc.graphics.g2d.*; import arc.graphics.gl.*; +import arc.math.*; import arc.struct.*; import arc.util.*; +import arc.util.io.*; import mindustry.*; import mindustry.annotations.Annotations.*; import mindustry.gen.*; @@ -29,7 +31,15 @@ public class LogicDisplay extends Block{ commandTriangle = 9, commandImage = 10, //note that this command actually only draws 1 character, unpacked in instruction - commandPrint = 11; + commandPrint = 11, + + commandTranslate = 12, + commandScale = 13, + commandRotate = 14, + commandResetTransform = 15 + ; + + public static final float scaleStep = 0.05f; public int maxSides = 25; @@ -58,6 +68,7 @@ public class LogicDisplay extends Block{ public float color = Color.whiteFloatBits; public float stroke = 1f; public LongQueue commands = new LongQueue(256); + public @Nullable Mat transform; @Override public void draw(){ @@ -79,14 +90,18 @@ public class LogicDisplay extends Block{ if(!commands.isEmpty()){ Draw.draw(Draw.z(), () -> { Tmp.m1.set(Draw.proj()); + Tmp.m2.set(Draw.trans()); Draw.proj(0, 0, displaySize, displaySize); + if(transform != null){ + Draw.trans(transform); + } buffer.begin(); Draw.color(color); Lines.stroke(stroke); while(!commands.isEmpty()){ long c = commands.removeFirst(); - byte type = DisplayCmd.type(c); + int type = DisplayCmd.type(c); int x = unpackSign(DisplayCmd.x(c)), y = unpackSign(DisplayCmd.y(c)), p1 = unpackSign(DisplayCmd.p1(c)), p2 = unpackSign(DisplayCmd.p2(c)), p3 = unpackSign(DisplayCmd.p3(c)), p4 = unpackSign(DisplayCmd.p4(c)); @@ -117,11 +132,16 @@ public class LogicDisplay extends Block{ Draw.rect(Tmp.tr1, x + Tmp.tr1.width/2f + glyph.xoffset, y + Tmp.tr1.height/2f + glyph.yoffset + Fonts.logic.getData().capHeight + Fonts.logic.getData().ascent, Tmp.tr1.width, Tmp.tr1.height); } } + case commandTranslate -> Draw.trans((transform == null ? (transform = new Mat()) : transform).translate(x, y)); + case commandScale -> Draw.trans((transform == null ? (transform = new Mat()) : transform).scale(x * scaleStep, y * scaleStep)); + case commandRotate-> Draw.trans((transform == null ? (transform = new Mat()) : transform).rotate(p1)); + case commandResetTransform -> Draw.trans((transform == null ? (transform = new Mat()) : transform).idt()); } } buffer.end(); Draw.proj(Tmp.m1); + Draw.trans(Tmp.m2); Draw.reset(); }); } @@ -135,6 +155,41 @@ public class LogicDisplay extends Block{ Draw.blend(); } + @Override + public byte version(){ + return 1; + } + + @Override + public void write(Writes write){ + super.write(write); + + if(transform != null){ + write.bool(true); + for(int i = 0; i < transform.val.length; i++){ + write.f(transform.val[i]); + } + }else{ + write.bool(false); + } + } + + @Override + public void read(Reads read, byte revision){ + super.read(read, revision); + + if(revision >= 1){ + boolean hasTransform = read.bool(); + if(hasTransform){ + transform = new Mat(); + + for(int i = 0; i < transform.val.length; i++){ + transform.val[i] = read.f(); + } + } + } + } + @Override public void remove(){ super.remove(); @@ -163,7 +218,12 @@ public class LogicDisplay extends Block{ triangle, image, //note that this command actually only draws 1 character, unpacked in instruction - print; + print, + translate, + scale, + rotate, + reset + ; public static final GraphicsType[] all = values(); } @@ -171,7 +231,7 @@ public class LogicDisplay extends Block{ @Struct static class DisplayCmdStruct{ @StructField(4) - public byte type; + public int type; //at least 9 bits are required for full 360 degrees @StructField(10)