Many things

This commit is contained in:
Anuken
2020-08-06 20:00:02 -04:00
parent ac3106728f
commit 176e4a3bb9
20 changed files with 370 additions and 167 deletions

View File

@@ -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;
}
}