diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 880eadc5c1..7272ca6a06 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -415,6 +415,7 @@ wavemode.health = health editor.default = [lightgray] details = Details... edit = Edit... +variables = Vars editor.name = Name: editor.spawn = Spawn Unit editor.removeunit = Remove Unit diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 01e6782497..88db7a2f7d 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -934,27 +934,31 @@ public class LExecutor{ //this should avoid any garbage allocation Var v = exec.var(value); if(v.isobj && value != 0){ - String strValue = - v.objval == null ? "null" : - v.objval instanceof String s ? s : - v.objval == Blocks.stoneWall ? "solid" : //special alias - v.objval instanceof MappableContent content ? content.name : - v.objval instanceof Content ? "[content]" : - v.objval instanceof Building build ? build.block.name : - v.objval instanceof Unit unit ? unit.type.name : - v.objval instanceof Enum e ? e.name() : - "[object]"; + String strValue = toString(v.objval); exec.textBuffer.append(strValue); }else{ //display integer version when possible - if(Math.abs(v.numval - (long)v.numval) < 0.000001){ + if(Math.abs(v.numval - (long)v.numval) < 0.00001){ exec.textBuffer.append((long)v.numval); }else{ exec.textBuffer.append(v.numval); } } } + + public static String toString(Object obj){ + return + obj == null ? "null" : + obj instanceof String s ? s : + obj == Blocks.stoneWall ? "solid" : //special alias + obj instanceof MappableContent content ? content.name : + obj instanceof Content ? "[content]" : + obj instanceof Building build ? build.block.name : + obj instanceof Unit unit ? unit.type.name : + obj instanceof Enum e ? e.name() : + "[object]"; + } } public static class PrintFlushI implements LInstruction{ diff --git a/core/src/mindustry/logic/LogicDialog.java b/core/src/mindustry/logic/LogicDialog.java index c34f5347ba..17a2ff880e 100644 --- a/core/src/mindustry/logic/LogicDialog.java +++ b/core/src/mindustry/logic/LogicDialog.java @@ -2,9 +2,15 @@ package mindustry.logic; import arc.*; import arc.func.*; +import arc.graphics.*; +import arc.scene.actions.*; +import arc.scene.ui.*; import arc.scene.ui.TextButton.*; import arc.util.*; +import mindustry.ctype.*; import mindustry.gen.*; +import mindustry.graphics.*; +import mindustry.logic.LExecutor.*; import mindustry.logic.LStatements.*; import mindustry.ui.*; import mindustry.ui.dialogs.*; @@ -15,6 +21,7 @@ import static mindustry.logic.LCanvas.*; public class LogicDialog extends BaseDialog{ public LCanvas canvas; Cons consumer = s -> {}; + @Nullable LExecutor executor; public LogicDialog(){ super("logic"); @@ -57,6 +64,80 @@ public class LogicDialog extends BaseDialog{ dialog.show(); }).name("edit"); + buttons.button("@variables", Icon.menu, () -> { + BaseDialog dialog = new BaseDialog("@variables"); + dialog.cont.pane(p -> { + p.margin(10f).marginRight(16f); + p.table(Tex.button, t -> { + t.defaults().fillX().height(45f); + int i = 0; + for(var s : executor.vars){ + if(s.constant) continue; + + Color varColor = Pal.gray; + float stub = 8f, mul = 0.5f, pad = 4; + + Color color = + !s.isobj ? Pal.place : + s.objval == null ? Color.darkGray : + s.objval instanceof String ? Pal.ammo : + s.objval instanceof Content ? Pal.logicOperations : + s.objval instanceof Building ? Pal.logicBlocks : + s.objval instanceof Unit ? Pal.logicUnits : + s.objval instanceof Enum ? Pal.logicIo : + Color.white; + + String typeName = + !s.isobj ? "number" : + s.objval == null ? "null" : + s.objval instanceof String ? "string" : + s.objval instanceof Content ? "content" : + s.objval instanceof Building ? "building" : + s.objval instanceof Unit ? "unit" : + s.objval instanceof Enum ? "enum" : + "unknown"; + + t.add(new Image(Tex.whiteui, varColor.cpy().mul(mul))).width(stub); + t.stack(new Image(Tex.whiteui, varColor), new Label(" " + s.name + " ", Styles.outlineLabel){{ + setColor(Pal.accent); + }}).padRight(pad); + + t.add(new Image(Tex.whiteui, Pal.gray.cpy().mul(mul))).width(stub); + t.table(Tex.pane, out -> { + float period = 15f; + float[] counter = {-1f}; + Label label = out.add("").style(Styles.outlineLabel).padLeft(4).padRight(4).width(140f).wrap().get(); + label.update(() -> { + if(counter[0] < 0 || (counter[0] += Time.delta) >= period){ + String text = s.isobj ? PrintI.toString(s.objval) : Math.abs(s.numval - (long)s.numval) < 0.00001 ? (long)s.numval + "" : s.numval + ""; + if(!label.textEquals(text)){ + label.setText(text); + if(counter[0] >= 0f){ + label.actions(Actions.color(Pal.accent), Actions.color(Color.white, 0.2f)); + } + } + counter[0] = 0f; + } + }); + label.act(1f); + }).padRight(pad); + + t.add(new Image(Tex.whiteui, color.cpy().mul(mul))).width(stub); + t.stack(new Image(Tex.whiteui, color), new Label(" " + typeName + " ", Styles.outlineLabel)); + + t.row(); + + t.add().growX().colspan(6).height(4); + + t.row(); + } + }); + }); + + dialog.addCloseButton(); + dialog.show(); + }).name("variables").disabled(b -> executor == null || executor.vars.length == 0); + buttons.button("@add", Icon.add, () -> { BaseDialog dialog = new BaseDialog("@add"); dialog.cont.pane(t -> { @@ -92,7 +173,8 @@ public class LogicDialog extends BaseDialog{ onResize(() -> canvas.rebuild()); } - public void show(String code, Cons modified){ + public void show(String code, LExecutor executor, Cons modified){ + this.executor = executor; canvas.statements.clearChildren(); canvas.rebuild(); try{ diff --git a/core/src/mindustry/world/blocks/logic/LogicBlock.java b/core/src/mindustry/world/blocks/logic/LogicBlock.java index e8cf14ef16..411e0067b5 100644 --- a/core/src/mindustry/world/blocks/logic/LogicBlock.java +++ b/core/src/mindustry/world/blocks/logic/LogicBlock.java @@ -7,7 +7,6 @@ import arc.struct.Bits; import arc.struct.*; import arc.util.*; import arc.util.io.*; -import mindustry.*; import mindustry.ai.types.*; import mindustry.core.*; import mindustry.gen.*; @@ -493,7 +492,7 @@ public class LogicBlock extends Block{ @Override public void buildConfiguration(Table table){ table.button(Icon.pencil, Styles.clearTransi, () -> { - Vars.ui.logic.show(code, code -> configure(compress(code, relativeConnections()))); + ui.logic.show(code, executor, code -> configure(compress(code, relativeConnections()))); }).size(40); }