Files
Mindustry/core/src/mindustry/logic/LStatements.java

187 lines
5.4 KiB
Java
Raw Normal View History

2020-08-07 16:11:02 -04:00
package mindustry.logic;
import arc.graphics.*;
import arc.scene.ui.*;
import arc.scene.ui.layout.*;
import mindustry.logic.LExecutor.*;
import mindustry.logic.LCanvas.*;
import mindustry.ui.*;
public class LStatements{
public static class AssignStatement extends LStatement{
public String to = "result";
public String from = "0";
@Override
public void build(Table table){
table.field(to, Styles.nodeField, str -> to = str)
.size(100f, 40f).pad(2f).color(table.color);
table.add(" = ");
table.field(from, Styles.nodeField, str -> from = str)
.size(100f, 40f).pad(2f).color(table.color);
}
@Override
public LCategory category(){
return LCategory.operations;
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 16:11:02 -04:00
//TODO internal consts need to start with ___ and not be assignable to
return new LExecutor.AssignI(builder.var(from), builder.var(to));
}
}
public static class ToggleStatement extends LStatement{
public String target = "result";
public String value = "0";
@Override
public void build(Table table){
table.field(target, Styles.nodeField, str -> target = str)
.size(100f, 40f).pad(2f).color(table.color);
table.add(" -> ");
table.field(value, Styles.nodeField, str -> value = str)
.size(100f, 40f).pad(2f).color(table.color);
}
@Override
public LCategory category(){
return LCategory.operations;
}
@Override
2020-08-07 17:50:54 -04:00
public LExecutor.LInstruction build(LAssembler builder){
2020-08-07 16:11:02 -04:00
return new LExecutor.ToggleI(builder.var(target), builder.var(value));
}
}
public static class OpStatement extends LStatement{
2020-08-07 17:50:54 -04:00
public BinaryOp op = BinaryOp.add;
2020-08-07 16:11:02 -04:00
public String a = "a", b = "b", dest = "result";
@Override
public void build(Table table){
table.field(dest, Styles.nodeField, str -> dest = str)
.size(100f, 40f).pad(2f).color(table.color);
table.add(" = ");
table.field(a, Styles.nodeField, str -> a = str)
.size(90f, 40f).pad(2f).color(table.color);
TextButton[] button = {null};
button[0] = table.button(op.symbol, Styles.cleart, () -> {
2020-08-07 17:50:54 -04:00
op = BinaryOp.all[(op.ordinal() + 1) % BinaryOp.all.length];
2020-08-07 16:11:02 -04:00
button[0].setText(op.symbol);
}).size(50f, 30f).pad(4f).get();
table.field(b, Styles.nodeField, str -> b = str)
.size(90f, 40f).pad(2f).color(table.color);
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 16:11:02 -04:00
return new BinaryOpI(op,builder.var(a), builder.var(b), builder.var(dest));
}
@Override
public LCategory category(){
return LCategory.operations;
}
}
public static class EndStatement extends LStatement{
@Override
public void build(Table table){
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 16:11:02 -04:00
return new EndI();
}
@Override
public LCategory category(){
return LCategory.control;
}
}
public static class JumpStatement extends LStatement{
2020-08-07 17:50:54 -04:00
public transient StatementElem dest;
public int destIndex;
public String condition = "true";
2020-08-07 16:11:02 -04:00
@Override
public void build(Table table){
table.add("if ").padLeft(6);
table.field(condition, Styles.nodeField, str -> condition = str)
.size(100f, 40f).pad(2f).color(table.color);
table.add().growX();
2020-08-07 17:50:54 -04:00
table.add(new JumpButton(Color.white, () -> dest, s -> dest = s)).size(30).right().padRight(-17);
2020-08-07 16:11:02 -04:00
}
2020-08-07 17:50:54 -04:00
//elements need separate conversion logic
2020-08-07 16:11:02 -04:00
@Override
2020-08-07 17:50:54 -04:00
public void afterLoad(LAssembler assembler){
if(assembler.elements != null){
dest = assembler.elements.get(destIndex);
}
}
@Override
public void beforeSave(LAssembler assembler){
destIndex = dest == null ? -1 : dest.parent.getChildren().indexOf(dest);
}
@Override
public LInstruction build(LAssembler builder){
beforeSave(builder);
return new JumpI(builder.var(condition),destIndex);
2020-08-07 16:11:02 -04:00
}
@Override
public LCategory category(){
return LCategory.control;
}
}
public static class FetchBuildStatement extends LStatement{
public String x = "0", y = "0", dest = "result";
@Override
public void build(Table table){
table.field(dest, Styles.nodeField, str -> dest = str)
.size(100f, 40f).pad(2f).color(table.color);
table.add(" = ");
table.field(x, Styles.nodeField, str -> x = str)
.size(90f, 40f).pad(2f).color(table.color);
table.add(", ");
table.field(y, Styles.nodeField, str -> y = str)
.size(90f, 40f).pad(2f).color(table.color);
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 16:11:02 -04:00
return new FetchBuildI(builder.var(dest), builder.var(x), builder.var(y));
}
@Override
public LCategory category(){
return LCategory.blocks;
}
}
}