Clipboard export / Conditional jumps / Saving of state
This commit is contained in:
@@ -7,6 +7,7 @@ public enum BinaryOp{
|
||||
div("/", (a, b) -> a / b),
|
||||
mod("%", (a, b) -> a % b),
|
||||
equal("==", (a, b) -> Math.abs(a - b) < 0.000001 ? 1 : 0),
|
||||
notEqual("not", (a, b) -> Math.abs(a - b) < 0.000001 ? 0 : 1),
|
||||
lessThan("<", (a, b) -> a < b ? 1 : 0),
|
||||
lessThanEq("<=", (a, b) -> a <= b ? 1 : 0),
|
||||
greaterThan(">", (a, b) -> a > b ? 1 : 0),
|
||||
|
||||
29
core/src/mindustry/logic/ConditionOp.java
Normal file
29
core/src/mindustry/logic/ConditionOp.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package mindustry.logic;
|
||||
|
||||
public enum ConditionOp{
|
||||
equal("==", (a, b) -> Math.abs(a - b) < 0.000001),
|
||||
notEqual("not", (a, b) -> Math.abs(a - b) >= 0.000001),
|
||||
lessThan("<", (a, b) -> a < b),
|
||||
lessThanEq("<=", (a, b) -> a <= b),
|
||||
greaterThan(">", (a, b) -> a > b),
|
||||
greaterThanEq(">=", (a, b) -> a >= b);
|
||||
|
||||
public static final ConditionOp[] all = values();
|
||||
|
||||
public final CondOpLambda function;
|
||||
public final String symbol;
|
||||
|
||||
ConditionOp(String symbol, CondOpLambda function){
|
||||
this.symbol = symbol;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return symbol;
|
||||
}
|
||||
|
||||
interface CondOpLambda{
|
||||
boolean get(double a, double b);
|
||||
}
|
||||
}
|
||||
@@ -102,19 +102,14 @@ public class LCanvas extends Table{
|
||||
}
|
||||
|
||||
void load(String asm){
|
||||
statements.clearChildren();
|
||||
try{
|
||||
Seq<LStatement> statements = LAssembler.read(asm);
|
||||
for(LStatement st : statements){
|
||||
add(st);
|
||||
}
|
||||
Seq<LStatement> statements = LAssembler.read(asm);
|
||||
this.statements.clearChildren();
|
||||
for(LStatement st : statements){
|
||||
add(st);
|
||||
}
|
||||
|
||||
for(LStatement st : statements){
|
||||
st.setupUI();
|
||||
}
|
||||
}catch(Exception e){
|
||||
//ignore errors reading asm
|
||||
e.printStackTrace();
|
||||
for(LStatement st : statements){
|
||||
st.setupUI();
|
||||
}
|
||||
|
||||
this.statements.layout();
|
||||
@@ -272,6 +267,20 @@ public class LCanvas extends Table{
|
||||
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode button){
|
||||
|
||||
if(button == KeyCode.mouseMiddle){
|
||||
LStatement copy = st.copy();
|
||||
if(copy != null){
|
||||
StatementElem s = new StatementElem(copy);
|
||||
|
||||
statements.addChildAfter(StatementElem.this,s);
|
||||
statements.layout();
|
||||
copy.elem = s;
|
||||
copy.setupUI();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Vec2 v = localToStageCoordinates(Tmp.v1.set(x, y));
|
||||
lastx = v.x;
|
||||
lasty = v.y;
|
||||
|
||||
@@ -373,19 +373,23 @@ public class LExecutor{
|
||||
}
|
||||
|
||||
public static class JumpI implements LInstruction{
|
||||
public int cond, to;
|
||||
public ConditionOp op = ConditionOp.notEqual;
|
||||
public int value, compare, address;
|
||||
|
||||
public JumpI(int cond, int to){
|
||||
this.cond = cond;
|
||||
this.to = to;
|
||||
public JumpI(ConditionOp op, int value, int compare, int address){
|
||||
this.op = op;
|
||||
this.value = value;
|
||||
this.compare = compare;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
JumpI(){}
|
||||
public JumpI(){
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(LExecutor exec){
|
||||
if(to != -1 && exec.bool(cond)){
|
||||
exec.vars[varCounter].numval = to;
|
||||
if(address != -1 && op.function.get(exec.num(value), exec.num(compare))){
|
||||
exec.vars[varCounter].numval = address;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,13 @@ public abstract class LStatement{
|
||||
public abstract LCategory category();
|
||||
public abstract LInstruction build(LAssembler builder);
|
||||
|
||||
//TODO doesn't work with modded stuff
|
||||
public LStatement copy(){
|
||||
StringBuilder build = new StringBuilder();
|
||||
write(build);
|
||||
return LogicIO.read(build.toString().split(" "));
|
||||
}
|
||||
|
||||
//protected methods are only for internal UI layout utilities
|
||||
|
||||
protected Cell<TextField> field(Table table, String value, Cons<String> setter){
|
||||
|
||||
@@ -458,21 +458,31 @@ public class LStatements{
|
||||
public transient StatementElem dest;
|
||||
|
||||
public int destIndex;
|
||||
public String condition = "true";
|
||||
|
||||
public ConditionOp op = ConditionOp.notEqual;
|
||||
public String value = "x", compare = "false";
|
||||
|
||||
@Override
|
||||
public void build(Table table){
|
||||
table.add("if ").padLeft(6);
|
||||
field(table, condition, str -> condition = str);
|
||||
table.add("if ").padLeft(4);
|
||||
|
||||
field(table, value, str -> value = str);
|
||||
|
||||
table.button(b -> {
|
||||
b.label(() -> op.symbol);
|
||||
b.clicked(() -> showSelect(b, ConditionOp.all, op, o -> op = o));
|
||||
}, Styles.logict, () -> {}).size(48f, 40f).pad(4f).color(table.color);
|
||||
|
||||
field(table, compare, str -> compare = str);
|
||||
|
||||
table.add().growX();
|
||||
table.add(new JumpButton(Color.white, () -> dest, s -> dest = s)).size(30).right().padRight(-17);
|
||||
table.add(new JumpButton(Color.white, () -> dest, s -> dest = s)).size(30).right().padLeft(-8);
|
||||
}
|
||||
|
||||
//elements need separate conversion logic
|
||||
@Override
|
||||
public void setupUI(){
|
||||
if(elem != null){
|
||||
if(elem != null && destIndex > 0 && destIndex < elem.parent.getChildren().size){
|
||||
dest = (StatementElem)elem.parent.getChildren().get(destIndex);
|
||||
}
|
||||
}
|
||||
@@ -486,7 +496,7 @@ public class LStatements{
|
||||
|
||||
@Override
|
||||
public LInstruction build(LAssembler builder){
|
||||
return new JumpI(builder.var(condition),destIndex);
|
||||
return new JumpI(op, builder.var(value), builder.var(compare), destIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.*;
|
||||
import arc.func.*;
|
||||
import arc.scene.ui.TextButton.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.util.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.ui.*;
|
||||
import mindustry.ui.dialogs.*;
|
||||
|
||||
import static mindustry.Vars.*;
|
||||
|
||||
public class LogicDialog extends BaseDialog{
|
||||
LCanvas canvas;
|
||||
Cons<String> consumer = s -> Log.info(s);
|
||||
@@ -17,6 +23,34 @@ public class LogicDialog extends BaseDialog{
|
||||
canvas = new LCanvas();
|
||||
addCloseButton();
|
||||
|
||||
buttons.button("@edit", Icon.edit, () -> {
|
||||
BaseDialog dialog = new BaseDialog("@editor.export");
|
||||
dialog.cont.pane(p -> {
|
||||
p.margin(10f);
|
||||
p.table(Tex.button, t -> {
|
||||
TextButtonStyle style = Styles.cleart;
|
||||
t.defaults().size(280f, 60f).left();
|
||||
t.row();
|
||||
t.button("@schematic.copy.import", Icon.download, style, () -> {
|
||||
dialog.hide();
|
||||
try{
|
||||
canvas.load(Core.app.getClipboardText());
|
||||
}catch(Throwable e){
|
||||
ui.showException(e);
|
||||
}
|
||||
}).marginLeft(12f).disabled(b -> Core.app.getClipboardText() == null);
|
||||
t.row();
|
||||
t.button("@schematic.copy", Icon.copy, style, () -> {
|
||||
dialog.hide();
|
||||
Core.app.setClipboardText(canvas.save());
|
||||
}).marginLeft(12f);
|
||||
});
|
||||
});
|
||||
|
||||
dialog.addCloseButton();
|
||||
dialog.show();
|
||||
});
|
||||
|
||||
stack(canvas, new Table(t -> {
|
||||
t.bottom();
|
||||
t.add(buttons);
|
||||
@@ -28,7 +62,12 @@ public class LogicDialog extends BaseDialog{
|
||||
}
|
||||
|
||||
public void show(String code, Cons<String> consumer){
|
||||
canvas.load(code);
|
||||
try{
|
||||
canvas.load(code);
|
||||
}catch(Throwable t){
|
||||
t.printStackTrace();
|
||||
canvas.load("");
|
||||
}
|
||||
this.consumer = consumer;
|
||||
|
||||
show();
|
||||
|
||||
Reference in New Issue
Block a user