From a9b5c8f02ba954266b23ddf4fa7e376b617cf398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Tue, 26 May 2020 11:56:49 +0200 Subject: [PATCH] Fix custom block wiring. --- .../minicards/CanvasView.java | 2 +- .../parts/ProgramakerCustomBlockPart.java | 99 ++++++++++++++----- .../minicards/types/PartConnection.java | 13 +++ .../connectors/output/OutputConnector.java | 2 +- 4 files changed, 91 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 8130d90..b94e38b 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -250,7 +250,7 @@ public class CanvasView extends View implements PartGrid { _mouseDownPoint = new Tuple2(xInScreen, yInScreen); } else { - Log.i("Canvas", "X: " + xInScreen + " Y: " + yInScreen + Log.d("Canvas", "X: " + xInScreen + " Y: " + yInScreen + " in drop zone " + _dropToRemoveZone + " : " + inDropZone(xInScreen, yInScreen)); diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java b/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java index 5810e00..0ffff73 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java @@ -17,6 +17,7 @@ import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; 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.ui_helpers.DoAsync; 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.ProgramakerFunctionCallResult; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -54,6 +56,8 @@ public class ProgramakerCustomBlockPart implements Part { private String token = null; private Object[] lastValues; private boolean active = true; + private Tuple2 saveToOutput; + private RoundOutputConnector pulseOutput; public ProgramakerCustomBlockPart(String id, PartGrid grid, Tuple2 center, ProgramakerCustomBlock block) { @@ -127,13 +131,15 @@ public class ProgramakerCustomBlockPart implements Part { } private void updatePorts() { - String type = this._block.getBlock_type() == null ? "" : this._block.getBlock_type(); - boolean has_pulse_input = type.equals("operation"); - boolean has_pulse_output = has_pulse_input || type.equals("trigger"); - boolean hasImplicitOutput = type.equals("getter"); + final String type = this._block.getBlock_type() == null ? "" : this._block.getBlock_type(); + final boolean has_pulse_input = type.equals("operation"); + final boolean has_pulse_output = has_pulse_input || type.equals("trigger"); + final boolean hasImplicitOutput = type.equals("getter"); - List> inputs = new LinkedList<>(); - List> outputs = new LinkedList<>(); + final List> inputs = new LinkedList<>(); + final List> outputs = new LinkedList<>(); + + RoundOutputConnector pulseOutput = null; // Add pulses if (has_pulse_input) { @@ -143,9 +149,10 @@ public class ProgramakerCustomBlockPart implements Part { ); } if (has_pulse_output) { + pulseOutput = new RoundOutputConnector(this, this._partGrid, 0, 0, + IO_RADIUS); outputs.add(new Tuple2<>(new ConnectorTypeInfo(ConnectorTypeInfo.Type.PULSE), - new RoundOutputConnector(this, this._partGrid, 0, 0, - IO_RADIUS)) + pulseOutput) ); } @@ -179,9 +186,12 @@ public class ProgramakerCustomBlockPart implements Part { } } + Tuple2 saveToOutput = null; + if (savedTo != null) { - outputs.add(new Tuple2<>(ConnectorTypeInfo.FromTypeName(savedTo.getType()), - new RoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS))); + saveToOutput = new Tuple2<>(ConnectorTypeInfo.FromTypeName(savedTo.getType()), + new RoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS)); + outputs.add(saveToOutput); } if (hasImplicitOutput) { @@ -192,6 +202,8 @@ public class ProgramakerCustomBlockPart implements Part { // Tie everything inputConnectors = inputs; outputConnectors = outputs; + this.saveToOutput = saveToOutput; + this.pulseOutput = pulseOutput; this.updatePortPositions(); } @@ -261,12 +273,26 @@ public class ProgramakerCustomBlockPart implements Part { JSONObject blockData = _block.serialize(); serialized.put("block", blockData); -// serialized.put("on_string_output_connector", -// Serializations.serialize(serializeConnectionEndpoints())); + serialized.put("on_string_output_connector", serializeConnectionEndpoints()); return serialized; } + private JSONArray serializeConnectionEndpoints() { + JSONArray serializedData = new JSONArray(); + + for (OutputConnector output : getOutputConnectors()) { + JSONArray elements = new JSONArray(); + + for (Tuple2 endpoint : (List>) output.getConnectionEndpoints()) { + elements.put(PartConnection.serializeToJson(endpoint.item1, endpoint.item2)); + } + + serializedData.put(elements); + } + return serializedData; + } + public static Tuple2> deserialize(PartGrid grid, JSONObject data) throws JSONException { String id = data.getString("id"); int left = data.getInt("left"); @@ -278,12 +304,19 @@ public class ProgramakerCustomBlockPart implements Part { List connections = new LinkedList<>(); -// JSONArray connectorOuts = data.getJSONArray("on_string_output_connector"); -// for (int i = 0; i < connectorOuts.length(); i++) { -// connections.add(PartConnection.deserialize( -// toggle._stringOutputConnector, -// connectorOuts.getJSONObject(i))); -// } + JSONArray allConnectorOuts = data.optJSONArray("on_string_output_connector"); + if (allConnectorOuts == null) { + allConnectorOuts = new JSONArray(); + } + + for (int i = 0; i < allConnectorOuts.length(); i++) { + JSONArray connectorOuts = allConnectorOuts.getJSONArray(i); + Tuple2 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); } @@ -291,7 +324,7 @@ public class ProgramakerCustomBlockPart implements Part { @Override 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); if (info == null) { Log.e(LogTag, "Unknown connector" + inputConnector); @@ -311,7 +344,7 @@ public class ProgramakerCustomBlockPart implements Part { } 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(); if (token != null) { @@ -357,7 +390,22 @@ public class ProgramakerCustomBlockPart implements Part { } 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 savedTo = this.saveToOutput; + if (savedTo != null) { + // TODO: Fix output typing + // savedTo.item2.send((WireDataType) () -> result.getResult()); + } + + RoundOutputConnector pulseOutput = this.pulseOutput; + if (pulseOutput != null) { + pulseOutput.send(new Signal()); + } } private void freeBlock(String token) { @@ -459,7 +507,7 @@ public class ProgramakerCustomBlockPart implements Part { } drawConnectors(canvas); -// drawWires(canvas, devMode); // TODO: Implement + drawWires(canvas, devMode); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 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 public void moveEnd(int x, int y) { _left = x - width / 2; @@ -536,7 +590,6 @@ public class ProgramakerCustomBlockPart implements Part { @Override 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()) && (y >= this.get_top()) && (y <= this.get_bottom())); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java index a5bdbd4..8c2dd16 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java @@ -38,4 +38,17 @@ public class PartConnection { 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; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java index ea6773c..05228de 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java @@ -3,9 +3,9 @@ package com.codigoparallevar.minicards.types.connectors.output; import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.types.Dropper; 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.input.InputConnector; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.wireData.WireDataType; import java.util.List;