Many things
This commit is contained in:
@@ -10,11 +10,13 @@ import arc.scene.*;
|
||||
import arc.scene.event.*;
|
||||
import arc.scene.ui.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.struct.*;
|
||||
import arc.util.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.graphics.*;
|
||||
import mindustry.io.*;
|
||||
import mindustry.logic.LogicNode.*;
|
||||
import mindustry.logic.LogicNodes.*;
|
||||
import mindustry.logic.SavedLogic.*;
|
||||
import mindustry.ui.*;
|
||||
|
||||
public class LogicCanvas extends WidgetGroup{
|
||||
@@ -22,9 +24,38 @@ public class LogicCanvas extends WidgetGroup{
|
||||
|
||||
private Element selected;
|
||||
private Element entered;
|
||||
private Seq<LogicNode> nodes = new Seq<>();
|
||||
private Vec2 offset = new Vec2();
|
||||
|
||||
{
|
||||
addListener(new InputListener(){
|
||||
float lastX, lastY;
|
||||
|
||||
@Override
|
||||
public void touchDragged(InputEvent event, float mx, float my, int pointer){
|
||||
if(Core.app.isMobile() && pointer != 0) return;
|
||||
|
||||
float dx = mx - lastX, dy = my - lastY;
|
||||
offset.add(dx, dy);
|
||||
|
||||
for(Element e : getChildren()){
|
||||
e.moveBy(dx, dy);
|
||||
}
|
||||
|
||||
lastX = mx;
|
||||
lastY = my;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode button){
|
||||
if((Core.app.isMobile() && pointer != 0) || (Core.app.isDesktop() && button != KeyCode.mouseMiddle)) return false;
|
||||
|
||||
lastX = x;
|
||||
lastY = y;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
//TODO debug stuff
|
||||
add(new BinaryOpNode());
|
||||
add(new BinaryOpNode());
|
||||
add(new BinaryOpNode());
|
||||
@@ -34,15 +65,73 @@ public class LogicCanvas extends WidgetGroup{
|
||||
add(new ConditionNode());
|
||||
add(new ConditionNode());
|
||||
add(new SignalNode());
|
||||
add(new SequenceNode());
|
||||
|
||||
Log.info(JsonIO.print(JsonIO.write(save())));
|
||||
}
|
||||
|
||||
private void add(LogicNode node){
|
||||
LogicElement e = new LogicElement(node);
|
||||
NodeElement e = new NodeElement(node);
|
||||
e.setPosition(Core.graphics.getWidth()/2f, Core.graphics.getHeight()/2f);
|
||||
addChild(e);
|
||||
e.pack();
|
||||
}
|
||||
|
||||
public SavedLogic save(){
|
||||
|
||||
//convert elements to saved nodes for writing to JSON
|
||||
return new SavedLogic(getChildren().<NodeElement>as().map(e -> {
|
||||
SavedNode node = new SavedNode();
|
||||
node.state = e.node;
|
||||
node.x = e.x;
|
||||
node.y = e.y;
|
||||
node.connections = new SavedConnection[e.slots.length];
|
||||
|
||||
for(int i = 0; i < e.slots.length; i++){
|
||||
SavedConnection con = (node.connections[i] = new SavedConnection());
|
||||
SlotElement slot = e.slots[i];
|
||||
|
||||
if(slot.connection != null){
|
||||
SlotElement to = slot.connection;
|
||||
|
||||
con.node = getChildren().indexOf(to.node);
|
||||
con.slot = Structs.indexOf(to.node.slots, to);
|
||||
}else{
|
||||
con.node = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}).toArray(SavedNode.class));
|
||||
}
|
||||
|
||||
public void load(SavedLogic state){
|
||||
clear();
|
||||
|
||||
//add nodes as children first to make sure they can be accessed.
|
||||
for(SavedNode node : state.nodes){
|
||||
NodeElement elem = new NodeElement(node.state);
|
||||
elem.setPosition(node.x, node.y);
|
||||
addChild(elem);
|
||||
}
|
||||
|
||||
//assign connection data
|
||||
for(int i = 0; i < state.nodes.length; i++){
|
||||
SavedNode node = state.nodes[i];
|
||||
NodeElement elem = (NodeElement)getChildren().get(i);
|
||||
|
||||
for(int j = 0; j < node.connections.length; j++){
|
||||
SavedConnection con = node.connections[j];
|
||||
if(con.node >= 0 && con.node < state.nodes.length){
|
||||
SlotElement slot = elem.slots[j];
|
||||
slot.connection = ((NodeElement)getChildren().get(con.node)).slots[con.slot];
|
||||
}
|
||||
}
|
||||
|
||||
elem.pack();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(){
|
||||
Draw.color(backgroundCol);
|
||||
@@ -52,17 +141,18 @@ public class LogicCanvas extends WidgetGroup{
|
||||
Draw.color(gridCol);
|
||||
|
||||
float spacing = Scl.scl(50f);
|
||||
int xbars = (int)(width / spacing) + 1, ybars = (int)(width / spacing) + 1;
|
||||
int xbars = (int)(width / spacing) + 2, ybars = (int)(width / spacing) + 2;
|
||||
float ox = offset.x % spacing, oy = offset.y % spacing;
|
||||
|
||||
Lines.stroke(Scl.scl(3f));
|
||||
|
||||
for(int i = 0; i < xbars; i++){
|
||||
float cx = x + width/2f + (i - xbars/2) * spacing;
|
||||
float cx = x + width/2f + (i - xbars/2) * spacing + ox;
|
||||
Lines.line(cx, y, cx, y + height);
|
||||
}
|
||||
|
||||
for(int i = 0; i < ybars; i++){
|
||||
float cy = y + height/2f + (i - ybars/2) * spacing;
|
||||
float cy = y + height/2f + (i - ybars/2) * spacing + oy;
|
||||
Lines.line(0, cy, x + width, cy);
|
||||
}
|
||||
|
||||
@@ -71,17 +161,17 @@ public class LogicCanvas extends WidgetGroup{
|
||||
super.draw();
|
||||
|
||||
for(Element e : getChildren()){
|
||||
if(e instanceof LogicElement){
|
||||
LogicElement l = (LogicElement)e;
|
||||
if(e instanceof NodeElement){
|
||||
NodeElement l = (NodeElement)e;
|
||||
|
||||
for(SlotTable field : l.slots){
|
||||
for(SlotElement field : l.slots){
|
||||
field.drawConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(selected != null){
|
||||
SlotTable field = (SlotTable)selected.userObject;
|
||||
SlotElement field = (SlotElement)selected.userObject;
|
||||
Vec2 dest = selected.localToStageCoordinates(Tmp.v1.set(selected.getWidth()/2f, selected.getHeight()/2f));
|
||||
Vec2 mouse = Core.input.mouse();
|
||||
drawCurve(dest.x, dest.y, mouse.x, mouse.y, field.color);
|
||||
@@ -104,20 +194,13 @@ public class LogicCanvas extends WidgetGroup{
|
||||
Draw.reset();
|
||||
}
|
||||
|
||||
class LogicElement extends Table{
|
||||
class NodeElement extends Table{
|
||||
final LogicNode node;
|
||||
final SlotTable[] slots;
|
||||
SlotElement[] slots;
|
||||
Table slotTable;
|
||||
|
||||
LogicElement(LogicNode node){
|
||||
NodeElement(LogicNode node){
|
||||
this.node = node;
|
||||
nodes.add(node);
|
||||
|
||||
NodeSlot[] nslots = node.slots();
|
||||
|
||||
this.slots = new SlotTable[nslots.length];
|
||||
for(int i = 0; i < nslots.length; i++){
|
||||
this.slots[i] = new SlotTable(nslots[i]);
|
||||
}
|
||||
|
||||
background(Tex.whitePane);
|
||||
setColor(node.category().color);
|
||||
@@ -136,7 +219,6 @@ public class LogicCanvas extends WidgetGroup{
|
||||
t.button(Icon.cancel, Styles.onlyi, () -> {
|
||||
//TODO disconnect things
|
||||
remove();
|
||||
nodes.remove(node);
|
||||
});
|
||||
t.addListener(new InputListener(){
|
||||
float lastx, lasty;
|
||||
@@ -167,16 +249,32 @@ public class LogicCanvas extends WidgetGroup{
|
||||
|
||||
row();
|
||||
|
||||
defaults().height(30);
|
||||
table(t -> slotTable = t).fill();
|
||||
|
||||
for(SlotTable field : slots){
|
||||
add(field).align(field.slot.input ? Align.left : Align.right);
|
||||
row();
|
||||
}
|
||||
rebuildSlots();
|
||||
|
||||
marginBottom(7);
|
||||
}
|
||||
|
||||
void rebuildSlots(){
|
||||
slotTable.clear();
|
||||
slotTable.defaults().height(30);
|
||||
|
||||
NodeSlot[] nslots = node.slots();
|
||||
|
||||
this.slots = new SlotElement[nslots.length];
|
||||
for(int i = 0; i < nslots.length; i++){
|
||||
this.slots[i] = new SlotElement(this, nslots[i]);
|
||||
}
|
||||
|
||||
for(SlotElement field : slots){
|
||||
slotTable.add(field).growX().align(field.slot.input ? Align.left : Align.right);
|
||||
slotTable.row();
|
||||
}
|
||||
|
||||
pack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(){
|
||||
float pad = 10f;
|
||||
@@ -190,14 +288,16 @@ public class LogicCanvas extends WidgetGroup{
|
||||
}
|
||||
}
|
||||
|
||||
class SlotTable extends Table{
|
||||
class SlotElement extends Table{
|
||||
final NodeSlot slot;
|
||||
final NodeElement node;
|
||||
|
||||
ImageButton button;
|
||||
Element connection;
|
||||
SlotElement connection;
|
||||
|
||||
SlotTable(NodeSlot slot){
|
||||
SlotElement(NodeElement node, NodeSlot slot){
|
||||
this.slot = slot;
|
||||
this.node = node;
|
||||
|
||||
setColor(slot.type.color);
|
||||
|
||||
@@ -221,8 +321,9 @@ public class LogicCanvas extends WidgetGroup{
|
||||
|
||||
void drawConnection(){
|
||||
if(connection != null){
|
||||
Vec2 from = localToStageCoordinates(Tmp.v2.set(button.getX() + button.getWidth()/2f, button.getY() + button.getHeight()/2f));
|
||||
Vec2 to = connection.localToStageCoordinates(Tmp.v1.set(connection.getWidth()/2f, connection.getHeight()/2f));
|
||||
ImageButton cb = connection.button;
|
||||
Vec2 from = localToStageCoordinates(Tmp.v2.set(button.x + button.getWidth()/2f, button.y + button.getHeight()/2f));
|
||||
Vec2 to = cb.localToStageCoordinates(Tmp.v1.set(cb.getWidth()/2f, cb.getHeight()/2f));
|
||||
|
||||
drawCurve(from.x, from.y, to.x, to.y, color);
|
||||
}
|
||||
@@ -249,10 +350,13 @@ public class LogicCanvas extends WidgetGroup{
|
||||
button.addListener(new InputListener(){
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode code){
|
||||
if(selected == null){
|
||||
|
||||
if(selected == null && !slot.input){
|
||||
selected = button;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -265,11 +369,11 @@ public class LogicCanvas extends WidgetGroup{
|
||||
localToStageCoordinates(Tmp.v1.set(x, y));
|
||||
Element element = entered;
|
||||
|
||||
if(element != null && element.userObject instanceof SlotTable){
|
||||
SlotTable field = (SlotTable)element.userObject;
|
||||
if(element != null && element.userObject instanceof SlotElement){
|
||||
SlotElement field = (SlotElement)element.userObject;
|
||||
//make sure inputs are matched to outputs, and that slot types match
|
||||
if(field != SlotTable.this && field.slot.input != slot.input && field.slot.type == slot.type){
|
||||
connection = element;
|
||||
if(field != SlotElement.this && field.slot.input != slot.input && field.slot.type == slot.type){
|
||||
connection = field;
|
||||
//field.connection = button;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.scene.ui.layout.*;
|
||||
import mindustry.ui.dialogs.*;
|
||||
|
||||
public class LogicDialog extends BaseDialog{
|
||||
@@ -9,8 +10,12 @@ public class LogicDialog extends BaseDialog{
|
||||
super("logic");
|
||||
|
||||
canvas = new LogicCanvas();
|
||||
addCloseButton();
|
||||
|
||||
clear();
|
||||
add(canvas).grow();
|
||||
stack(canvas, new Table(t -> {
|
||||
t.bottom();
|
||||
t.add(buttons);
|
||||
})).grow();
|
||||
}
|
||||
}
|
||||
|
||||
45
core/src/mindustry/logic/LogicExecutor.java
Normal file
45
core/src/mindustry/logic/LogicExecutor.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import mindustry.logic.LogicNode.*;
|
||||
import mindustry.logic.SavedLogic.*;
|
||||
|
||||
public class LogicExecutor{
|
||||
/** all logical operations to be executed */
|
||||
private LogicOp[] ops = {};
|
||||
/** current operation index */
|
||||
private int index;
|
||||
|
||||
/** Set up executor state based on saved data. */
|
||||
public void setup(SavedLogic save){
|
||||
LogicNode[] out = new LogicNode[save.nodes.length];
|
||||
|
||||
//copy over the state so connections can be assigned
|
||||
for(int i = 0; i < out.length; i++){
|
||||
out[i] = save.nodes[i].state;
|
||||
}
|
||||
|
||||
for(int i = 0; i < out.length; i++){
|
||||
LogicNode node = out[i];
|
||||
NodeSlot[] slots = node.slots();
|
||||
|
||||
//connect node's slots to other nodes' slots
|
||||
for(int j = 0; j < save.nodes[i].connections.length; j++){
|
||||
SavedConnection con = save.nodes[i].connections[j];
|
||||
NodeSlot slot = slots[j];
|
||||
|
||||
if(con.node != -1){
|
||||
LogicNode other = out[con.node];
|
||||
NodeSlot[] otherSlots = other.slots();
|
||||
NodeSlot otherSlot = otherSlots[con.slot];
|
||||
|
||||
//slot.objOutput = (aslot, aobj) -> ((ObjOutput)otherSlot.objOutput).set(other, aobj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A single logical statement. */
|
||||
static class LogicOp{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,9 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.graphics.*;
|
||||
import arc.scene.ui.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.util.*;
|
||||
import mindustry.annotations.Annotations.*;
|
||||
import mindustry.gen.*;
|
||||
import mindustry.graphics.*;
|
||||
import mindustry.ui.*;
|
||||
|
||||
/** Base class for a type of logic node. */
|
||||
public abstract class LogicNode{
|
||||
@@ -27,96 +23,6 @@ public abstract class LogicNode{
|
||||
return getClass().getSimpleName().replace("Node", "");
|
||||
}
|
||||
|
||||
public static class BinaryOpNode extends LogicNode{
|
||||
public BinaryOp op = BinaryOp.add;
|
||||
@NodeSlotDef
|
||||
public double a, b;
|
||||
@NodeSlotDef
|
||||
public SetNum result = val -> {};
|
||||
|
||||
@Override
|
||||
public void build(Table table){
|
||||
//TODO replace with dropdown menu
|
||||
TextButton[] button = {null};
|
||||
button[0] = table.button(op.symbol, Styles.cleart, () -> {
|
||||
op = BinaryOp.all[(op.ordinal() + 1) % BinaryOp.all.length];
|
||||
button[0].setText(op.symbol);
|
||||
}).size(100f, 40f).pad(2f).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
result.set(op.function.get(a, b));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.operations;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConditionNode extends LogicNode{
|
||||
@NodeSlotDef(input = true)
|
||||
public Runnable input = this::run;
|
||||
@NodeSlotDef
|
||||
public double condition;
|
||||
@NodeSlotDef
|
||||
public Runnable yes, no;
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
if(condition > 0){
|
||||
yes.run();
|
||||
}else{
|
||||
no.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.controlFlow;
|
||||
}
|
||||
}
|
||||
|
||||
public static class NumberNode extends LogicNode{
|
||||
@NodeSlotDef
|
||||
public SetNum value;
|
||||
public double var;
|
||||
|
||||
@Override
|
||||
public void build(Table table){
|
||||
table.field(var + "", Styles.nodeField, str -> var = Strings.parseDouble(str, var))
|
||||
.valid(Strings::canParsePositiveFloat)
|
||||
.size(100f, 40f).pad(2f).color(table.color)
|
||||
.update(f -> f.setColor(f.isValid() ? table.color : Color.white));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
value.set(var);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.controlFlow;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SignalNode extends LogicNode{
|
||||
@NodeSlotDef
|
||||
public Runnable run;
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
run.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.controlFlow;
|
||||
}
|
||||
}
|
||||
|
||||
/** A field for a node, either an input or output. */
|
||||
public static class NodeSlot{
|
||||
/** The slot's display name. */
|
||||
@@ -138,10 +44,6 @@ public abstract class LogicNode{
|
||||
}
|
||||
}
|
||||
|
||||
static{
|
||||
new NodeSlot("a", true, DataType.number, (BinaryOpNode node, double val) -> node.a = val, null);
|
||||
}
|
||||
|
||||
public interface NumOutput<N>{
|
||||
void set(N node, double val);
|
||||
}
|
||||
|
||||
123
core/src/mindustry/logic/LogicNodes.java
Normal file
123
core/src/mindustry/logic/LogicNodes.java
Normal file
@@ -0,0 +1,123 @@
|
||||
package mindustry.logic;
|
||||
|
||||
import arc.graphics.*;
|
||||
import arc.scene.ui.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.util.*;
|
||||
import mindustry.annotations.Annotations.*;
|
||||
import mindustry.ui.*;
|
||||
|
||||
public class LogicNodes{
|
||||
public static class BinaryOpNode extends LogicNode{
|
||||
public BinaryOp op = BinaryOp.add;
|
||||
@Slot
|
||||
public double a, b;
|
||||
@Slot
|
||||
public transient SetNum result = val -> {};
|
||||
|
||||
@Override
|
||||
public void build(Table table){
|
||||
//TODO replace with dropdown menu
|
||||
TextButton[] button = {null};
|
||||
button[0] = table.button(op.symbol, Styles.cleart, () -> {
|
||||
op = BinaryOp.all[(op.ordinal() + 1) % BinaryOp.all.length];
|
||||
button[0].setText(op.symbol);
|
||||
}).size(100f, 40f).pad(2f).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
result.set(op.function.get(a, b));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.operations;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConditionNode extends LogicNode{
|
||||
@Slot(input = true)
|
||||
public transient Runnable input = this::run;
|
||||
@Slot
|
||||
public double condition;
|
||||
@Slot
|
||||
public transient Runnable yes, no;
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
if(condition > 0){
|
||||
yes.run();
|
||||
}else{
|
||||
no.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.controlFlow;
|
||||
}
|
||||
}
|
||||
|
||||
public static class NumberNode extends LogicNode{
|
||||
@Slot
|
||||
public transient SetNum value;
|
||||
public double var;
|
||||
|
||||
@Override
|
||||
public void build(Table table){
|
||||
table.field(var + "", Styles.nodeField, str -> var = parseDouble(str))
|
||||
.size(100f, 40f).pad(2f).color(table.color)
|
||||
.update(f -> f.setColor(f.isValid() ? table.color : Color.white));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
value.set(var);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.controlFlow;
|
||||
}
|
||||
|
||||
static double parseDouble(String s){
|
||||
return s.equals("yes") || s.equals("true") ? 1 :
|
||||
s.equals("no") || s.equals("false") ? 0 :
|
||||
Strings.parseDouble(s, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public static class SequenceNode extends LogicNode{
|
||||
@Slot(input = true)
|
||||
public transient Runnable input = this::run;
|
||||
@Slot
|
||||
public transient Runnable first, second;
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
first.run();
|
||||
second.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.controlFlow;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SignalNode extends LogicNode{
|
||||
@Slot
|
||||
public transient Runnable run;
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
run.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NodeCategory category(){
|
||||
return NodeCategory.controlFlow;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
core/src/mindustry/logic/SavedLogic.java
Normal file
26
core/src/mindustry/logic/SavedLogic.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package mindustry.logic;
|
||||
|
||||
/** The saved state of a logic board. */
|
||||
public class SavedLogic{
|
||||
public SavedNode[] nodes;
|
||||
|
||||
public SavedLogic(SavedNode[] nodes){
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
public static class SavedNode{
|
||||
/** Uninitialized state containing only relevant configuration. */
|
||||
public LogicNode state;
|
||||
/** Connections of this node. */
|
||||
public SavedConnection[] connections;
|
||||
/** x/y positions of the bottom left corner of the node */
|
||||
public float x, y;
|
||||
}
|
||||
|
||||
public static class SavedConnection{
|
||||
/** Node ID (in the array) that is being connected to. -1 means no connection */
|
||||
public int node;
|
||||
/** Slot number in the node */
|
||||
public int slot;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user