Fix custom block wiring.

This commit is contained in:
Sergio Martínez Portela 2020-05-26 11:56:49 +02:00
parent acb0dbec05
commit a9b5c8f02b
4 changed files with 91 additions and 25 deletions

View File

@ -250,7 +250,7 @@ public class CanvasView extends View implements PartGrid {
_mouseDownPoint = new Tuple2(xInScreen, yInScreen); _mouseDownPoint = new Tuple2(xInScreen, yInScreen);
} }
else { else {
Log.i("Canvas", "X: " + xInScreen + " Y: " + yInScreen Log.d("Canvas", "X: " + xInScreen + " Y: " + yInScreen
+ " in drop zone " + _dropToRemoveZone + " : " + " in drop zone " + _dropToRemoveZone + " : "
+ inDropZone(xInScreen, yInScreen)); + inDropZone(xInScreen, yInScreen));

View File

@ -17,6 +17,7 @@ import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.functional.Tuple2;
import com.codigoparallevar.minicards.types.wireData.Signal;
import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.types.wireData.WireDataType;
import com.codigoparallevar.minicards.ui_helpers.DoAsync; import com.codigoparallevar.minicards.ui_helpers.DoAsync;
import com.programaker.api.ProgramakerApi; import com.programaker.api.ProgramakerApi;
@ -25,6 +26,7 @@ import com.programaker.api.data.ProgramakerCustomBlockArgument;
import com.programaker.api.data.ProgramakerCustomBlockSaveTo; import com.programaker.api.data.ProgramakerCustomBlockSaveTo;
import com.programaker.api.data.ProgramakerFunctionCallResult; import com.programaker.api.data.ProgramakerFunctionCallResult;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -54,6 +56,8 @@ public class ProgramakerCustomBlockPart implements Part {
private String token = null; private String token = null;
private Object[] lastValues; private Object[] lastValues;
private boolean active = true; private boolean active = true;
private Tuple2<ConnectorTypeInfo, RoundOutputConnector> saveToOutput;
private RoundOutputConnector pulseOutput;
public ProgramakerCustomBlockPart(String id, PartGrid grid, Tuple2<Integer, Integer> center, ProgramakerCustomBlock block) { public ProgramakerCustomBlockPart(String id, PartGrid grid, Tuple2<Integer, Integer> center, ProgramakerCustomBlock block) {
@ -127,13 +131,15 @@ public class ProgramakerCustomBlockPart implements Part {
} }
private void updatePorts() { private void updatePorts() {
String type = this._block.getBlock_type() == null ? "" : this._block.getBlock_type(); final String type = this._block.getBlock_type() == null ? "" : this._block.getBlock_type();
boolean has_pulse_input = type.equals("operation"); final boolean has_pulse_input = type.equals("operation");
boolean has_pulse_output = has_pulse_input || type.equals("trigger"); final boolean has_pulse_output = has_pulse_input || type.equals("trigger");
boolean hasImplicitOutput = type.equals("getter"); final boolean hasImplicitOutput = type.equals("getter");
List<Tuple2<ConnectorTypeInfo, AnyRoundInputConnector>> inputs = new LinkedList<>(); final List<Tuple2<ConnectorTypeInfo, AnyRoundInputConnector>> inputs = new LinkedList<>();
List<Tuple2<ConnectorTypeInfo, RoundOutputConnector>> outputs = new LinkedList<>(); final List<Tuple2<ConnectorTypeInfo, RoundOutputConnector>> outputs = new LinkedList<>();
RoundOutputConnector pulseOutput = null;
// Add pulses // Add pulses
if (has_pulse_input) { if (has_pulse_input) {
@ -143,9 +149,10 @@ public class ProgramakerCustomBlockPart implements Part {
); );
} }
if (has_pulse_output) { if (has_pulse_output) {
pulseOutput = new RoundOutputConnector(this, this._partGrid, 0, 0,
IO_RADIUS);
outputs.add(new Tuple2<>(new ConnectorTypeInfo(ConnectorTypeInfo.Type.PULSE), outputs.add(new Tuple2<>(new ConnectorTypeInfo(ConnectorTypeInfo.Type.PULSE),
new RoundOutputConnector(this, this._partGrid, 0, 0, pulseOutput)
IO_RADIUS))
); );
} }
@ -179,9 +186,12 @@ public class ProgramakerCustomBlockPart implements Part {
} }
} }
Tuple2<ConnectorTypeInfo, RoundOutputConnector> saveToOutput = null;
if (savedTo != null) { if (savedTo != null) {
outputs.add(new Tuple2<>(ConnectorTypeInfo.FromTypeName(savedTo.getType()), saveToOutput = new Tuple2<>(ConnectorTypeInfo.FromTypeName(savedTo.getType()),
new RoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS))); new RoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS));
outputs.add(saveToOutput);
} }
if (hasImplicitOutput) { if (hasImplicitOutput) {
@ -192,6 +202,8 @@ public class ProgramakerCustomBlockPart implements Part {
// Tie everything // Tie everything
inputConnectors = inputs; inputConnectors = inputs;
outputConnectors = outputs; outputConnectors = outputs;
this.saveToOutput = saveToOutput;
this.pulseOutput = pulseOutput;
this.updatePortPositions(); this.updatePortPositions();
} }
@ -261,12 +273,26 @@ public class ProgramakerCustomBlockPart implements Part {
JSONObject blockData = _block.serialize(); JSONObject blockData = _block.serialize();
serialized.put("block", blockData); serialized.put("block", blockData);
// serialized.put("on_string_output_connector", serialized.put("on_string_output_connector", serializeConnectionEndpoints());
// Serializations.serialize(serializeConnectionEndpoints()));
return serialized; return serialized;
} }
private JSONArray serializeConnectionEndpoints() {
JSONArray serializedData = new JSONArray();
for (OutputConnector output : getOutputConnectors()) {
JSONArray elements = new JSONArray();
for (Tuple2<String, String> endpoint : (List<Tuple2<String, String>>) output.getConnectionEndpoints()) {
elements.put(PartConnection.serializeToJson(endpoint.item1, endpoint.item2));
}
serializedData.put(elements);
}
return serializedData;
}
public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid grid, JSONObject data) throws JSONException { public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid grid, JSONObject data) throws JSONException {
String id = data.getString("id"); String id = data.getString("id");
int left = data.getInt("left"); int left = data.getInt("left");
@ -278,12 +304,19 @@ public class ProgramakerCustomBlockPart implements Part {
List<PartConnection> connections = new LinkedList<>(); List<PartConnection> connections = new LinkedList<>();
// JSONArray connectorOuts = data.getJSONArray("on_string_output_connector"); JSONArray allConnectorOuts = data.optJSONArray("on_string_output_connector");
// for (int i = 0; i < connectorOuts.length(); i++) { if (allConnectorOuts == null) {
// connections.add(PartConnection.deserialize( allConnectorOuts = new JSONArray();
// toggle._stringOutputConnector, }
// connectorOuts.getJSONObject(i)));
// } for (int i = 0; i < allConnectorOuts.length(); i++) {
JSONArray connectorOuts = allConnectorOuts.getJSONArray(i);
Tuple2<ConnectorTypeInfo, RoundOutputConnector> connector = part.outputConnectors.get(i);
for (int j = 0; j < connectorOuts.length(); j++) {
connections.add(PartConnection.deserialize(
connector.item2, connectorOuts.getJSONObject(j)));
}
}
return new Tuple2<>(part, connections); return new Tuple2<>(part, connections);
} }
@ -291,7 +324,7 @@ public class ProgramakerCustomBlockPart implements Part {
@Override @Override
public void send(InputConnector inputConnector, WireDataType signal) { public void send(InputConnector inputConnector, WireDataType signal) {
Log.i(LogTag, "Received signal from Input="+inputConnector); // Log.i(LogTag, "Received signal from Input="+inputConnector);
ConnectorTypeInfo info = getConnectorInfo(inputConnector); ConnectorTypeInfo info = getConnectorInfo(inputConnector);
if (info == null) { if (info == null) {
Log.e(LogTag, "Unknown connector" + inputConnector); Log.e(LogTag, "Unknown connector" + inputConnector);
@ -311,7 +344,7 @@ public class ProgramakerCustomBlockPart implements Part {
} }
private void wrappedRunOperation() { private void wrappedRunOperation() {
Log.i(LogTag, "Running block function=" + this._block.getFunction_name()); Log.d(LogTag, "Running block function=" + this._block.getFunction_name());
String token = this.prepareStart(); String token = this.prepareStart();
if (token != null) { if (token != null) {
@ -357,7 +390,22 @@ public class ProgramakerCustomBlockPart implements Part {
} }
ProgramakerFunctionCallResult result = api.callBlock(this._block, arguments); ProgramakerFunctionCallResult result = api.callBlock(this._block, arguments);
Log.d(LogTag, "Execution result="+result); Log.d(LogTag, "Execution result="+result.getResult());
onExecutionCompleted(result);
}
private void onExecutionCompleted(ProgramakerFunctionCallResult result) {
Tuple2<ConnectorTypeInfo, RoundOutputConnector> savedTo = this.saveToOutput;
if (savedTo != null) {
// TODO: Fix output typing
// savedTo.item2.send((WireDataType<Object>) () -> result.getResult());
}
RoundOutputConnector pulseOutput = this.pulseOutput;
if (pulseOutput != null) {
pulseOutput.send(new Signal());
}
} }
private void freeBlock(String token) { private void freeBlock(String token) {
@ -459,7 +507,7 @@ public class ProgramakerCustomBlockPart implements Part {
} }
drawConnectors(canvas); drawConnectors(canvas);
// drawWires(canvas, devMode); // TODO: Implement drawWires(canvas, devMode);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE); paint.setColor(Color.WHITE);
@ -521,6 +569,12 @@ public class ProgramakerCustomBlockPart implements Part {
} }
} }
private void drawWires(ScrolledCanvas canvas, boolean devMode) {
for (OutputConnector outputConnector : getOutputConnectors()){
outputConnector.drawWires(canvas, devMode);
}
}
@Override @Override
public void moveEnd(int x, int y) { public void moveEnd(int x, int y) {
_left = x - width / 2; _left = x - width / 2;
@ -536,7 +590,6 @@ public class ProgramakerCustomBlockPart implements Part {
@Override @Override
public boolean containsPoint(int x, int y) { public boolean containsPoint(int x, int y) {
Log.d(LogTag, "Contains (" +x+","+y+") in {left=" +get_left()+",right="+get_right()+",top="+get_top()+",bottom=" + get_bottom() + "}");
return ((x >= this.get_left()) && (x <= this.get_right()) return ((x >= this.get_left()) && (x <= this.get_right())
&& (y >= this.get_top()) && (y <= this.get_bottom())); && (y >= this.get_top()) && (y <= this.get_bottom()));
} }

View File

@ -38,4 +38,17 @@ public class PartConnection {
return serialized; return serialized;
} }
public static JSONObject serializeToJson(final String inputConnectorId, final String inputPartId) {
JSONObject serialized = new JSONObject();
try {
serialized.put(PartConnection.INPUT_CONNECTOR_ID_KEY, inputConnectorId);
serialized.put(PartConnection.INPUT_PART_ID_KEY, inputPartId);
} catch (JSONException e) {
e.printStackTrace();
}
return serialized;
}
} }

View File

@ -3,9 +3,9 @@ package com.codigoparallevar.minicards.types.connectors.output;
import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.Selectable;
import com.codigoparallevar.minicards.types.functional.Tuple2;
import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; import com.codigoparallevar.minicards.types.connectors.Wiring.Wire;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.functional.Tuple2;
import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.types.wireData.WireDataType;
import java.util.List; import java.util.List;