From 67348d9e03b39ba9f967cf58bb3af3a91333241a Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Fri, 26 Jan 2018 02:10:55 +0100 Subject: [PATCH] Add Any-Inputs, string wires (and *puts) and ConvertToString part. --- .../minicards/CanvasView.java | 71 +++- .../codigoparallevar/minicards/CardFile.java | 18 +- .../minicards/PartsHolder.java | 2 + .../minicards/StubPartGrid.java | 6 + .../connectors/AnyRoundInputConnector.java | 98 +++++ .../connectors/StringRoundInputConnector.java | 98 +++++ .../StringRoundOutputConnector.java | 158 ++++++++ .../parts/strings/ConvertToString.java | 351 ++++++++++++++++++ .../minicards/types/Part.java | 2 +- .../minicards/types/PartGrid.java | 3 + .../types/connectors/Wiring/StringWire.java | 19 + .../connectors/input/AnyInputConnector.java | 131 +++++++ .../input/StringInputConnector.java | 6 + .../output/StringOutputConnector.java | 11 + .../types/wireData/BooleanSignal.java | 7 +- .../minicards/types/wireData/Signal.java | 6 +- .../types/wireData/StringSignal.java | 14 + .../types/wireData/WireDataType.java | 3 +- 18 files changed, 988 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundInputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/AnyInputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/StringInputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/StringOutputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/wireData/StringSignal.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 6a7cc70..0a395d3 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -19,9 +19,11 @@ import com.codigoparallevar.minicards.types.Position; import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.Tuple4; +import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import java.io.IOException; @@ -101,7 +103,21 @@ class CanvasView extends View implements PartGrid { } OutputConnector outputConnector = connection.outputConnector; - outputConnector.connectTo(inputConnector); + if (inputConnector == null){ + Log.e("Canvas view", "Connector not found on connection"); + continue; + } + + if (inputConnector instanceof AnyInputConnector) { + inputConnector = ((AnyInputConnector) inputConnector).matchType(outputConnector); + } + + try { + outputConnector.connectTo(inputConnector); + } + catch (ClassCastException e) { + Log.e("Minicards - Canvas view", "Malformed connection", e); + } } } @@ -332,11 +348,17 @@ class CanvasView extends View implements PartGrid { // Then with input ones for (InputConnector inputConnector : part.getInputConnectors()){ - if (!(inputConnector instanceof SignalInputConnector)){ + SignalInputConnector signalInputConnector; + if (inputConnector instanceof SignalInputConnector){ + signalInputConnector = (SignalInputConnector) inputConnector; + } + else if (inputConnector instanceof AnyInputConnector) { + signalInputConnector = ((AnyInputConnector) inputConnector).ToSignalInputConnector(); + } + else { continue; } - SignalInputConnector signalInputConnector = (SignalInputConnector) inputConnector; if (inputConnector.containsPoint(x, y)){ return signalInputConnector; } @@ -355,13 +377,18 @@ class CanvasView extends View implements PartGrid { // Then with input ones for (InputConnector inputConnector : part.getInputConnectors()){ - if (!(inputConnector instanceof BooleanInputConnector)){ + BooleanInputConnector booleanInputConnector; + if (inputConnector instanceof BooleanInputConnector){ + booleanInputConnector = (BooleanInputConnector) inputConnector; + } + else if (inputConnector instanceof AnyInputConnector) { + booleanInputConnector = ((AnyInputConnector) inputConnector).ToBooleanInputConnector(); + } + else { continue; } - BooleanInputConnector booleanInputConnector = (BooleanInputConnector) inputConnector; - - if (booleanInputConnector.containsPoint(x, y)){ + if (inputConnector.containsPoint(x, y)){ return booleanInputConnector; } } @@ -370,6 +397,36 @@ class CanvasView extends View implements PartGrid { return null; } + @Override + @Nullable + public StringInputConnector getStringInputConnectorOn(int x, int y) { + // If no part was found, do the same for connectors + for (int i = parts.size() - 1; i >= 0; i--){ + final Part part = parts.get(i); + + // Then with input ones + for (InputConnector inputConnector : part.getInputConnectors()){ + StringInputConnector stringInputConnector; + if (inputConnector instanceof StringInputConnector){ + stringInputConnector = (StringInputConnector) inputConnector; + } + else if (inputConnector instanceof AnyInputConnector) { + stringInputConnector = ((AnyInputConnector) inputConnector).ToStringInputConnector(); + } + else { + continue; + } + + if (inputConnector.containsPoint(x, y)){ + return stringInputConnector; + } + } + } + + return null; + } + + public void addPart(Part part) { parts.add(part); invalidate(); diff --git a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java index 2ff3fee..ee796a7 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java @@ -12,6 +12,7 @@ import com.codigoparallevar.minicards.parts.logic.Ticker; import com.codigoparallevar.minicards.parts.logic.Toggle; import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.Placeholder; +import com.codigoparallevar.minicards.parts.strings.ConvertToString; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; @@ -263,18 +264,25 @@ public class CardFile { Collections.emptyList()); } else if (type.equals(Ticker.class.getName())){ - Tuple2> buttonInfo = Ticker.deserialize( + Tuple2> tickerInfo = Ticker.deserialize( grid, jsonObject.getJSONObject("_data")); - return buttonInfo; + return tickerInfo; } else if (type.equals(Toggle.class.getName())){ - Tuple2> buttonInfo = Toggle.deserialize( + Tuple2> toggleInfo = Toggle.deserialize( grid, jsonObject.getJSONObject("_data")); - return buttonInfo; + return toggleInfo; + } + else if (type.equals(ConvertToString.class.getName())){ + Tuple2> convertToStringInfo = ConvertToString.deserialize( + grid, + jsonObject.getJSONObject("_data")); + + return convertToStringInfo; } else { throw new JSONException("Expected known class, found " + type); @@ -293,7 +301,7 @@ public class CardFile { new File(oldPath).delete(); } catch (IOException e) { - Log.e("Minicards - cardfile rename", "Cannot create new file for rename", e); + Log.e("Minicards - rename", "Cannot create new file for rename", e); } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java index 66217ce..4ccf298 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java @@ -10,6 +10,7 @@ import com.codigoparallevar.minicards.parts.buttons.RoundButton; import com.codigoparallevar.minicards.parts.logic.Ticker; import com.codigoparallevar.minicards.parts.logic.Toggle; import com.codigoparallevar.minicards.parts.samples.ColorBox; +import com.codigoparallevar.minicards.parts.strings.ConvertToString; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Tuple2; @@ -25,6 +26,7 @@ class PartsHolder { add(new Tuple2<>("Ticker", Ticker.getInstantiator())); add(new Tuple2<>("Red/Green box", ColorBox.getInstantiator())); add(new Tuple2<>("Toggle", Toggle.getInstantiator())); + add(new Tuple2<>("ToString", ConvertToString.getInstantiator())); }}; public PartsHolder(Context context) { diff --git a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java index f82b95b..fab7739 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java @@ -5,6 +5,7 @@ import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; class StubPartGrid implements PartGrid { @Override @@ -30,4 +31,9 @@ class StubPartGrid implements PartGrid { @Override public void update() { } + + @Override + public StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd) { + return null; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java new file mode 100644 index 0000000..fbf5888 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java @@ -0,0 +1,98 @@ +package com.codigoparallevar.minicards.parts.connectors; + +import android.util.Log; + +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; +import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; +import com.codigoparallevar.minicards.types.wireData.WireDataType; + +import java.util.LinkedList; +import java.util.List; + +public class AnyRoundInputConnector extends AnyInputConnector { + private final Part _part; + private int _xposition; + private int _yposition; + private final int _radius; + private final List _attachments = new LinkedList<>(); + + public AnyRoundInputConnector(Part part, + int inputConnectorCenterX, int inputConnectorCenterY, + int inputConnectorRadius) { + _part = part; + _xposition = inputConnectorCenterX; + _yposition = inputConnectorCenterY; + _radius = inputConnectorRadius; + } + + @Override + public boolean containsPoint(int x, int y) { + return ((Math.abs(x - _xposition) <= _radius) + && (Math.abs(y - _yposition) <= _radius)); + } + + @Override + public Moveable getMoveable() { + return new Wire(this, _xposition, _yposition); + } + + @Override + public void unlink() { + for (Wire wire : _attachments) { + wire.unlink(); + } + } + + @Override + public void updatePosition(int x, int y) { + _xposition = x; + _yposition = y; + + for (Wire wire : _attachments){ + wire.moveEnd(x, y); + } + } + + @Override + public int getX() { + return _xposition; + } + + @Override + public int getY() { + return _yposition; + } + + @Override + public void send(WireDataType signal) { + _part.send(this, signal); + } + + + @Override + public void getAttachment(Wire wire) { + _attachments.add(wire); + } + + @Override + public Part getPart() { + return _part; + } + + @Override + public String getId() { + return _part.getConnectorId(this); + } + + @Override + public void drop(Wire wire) { + Log.d("InputConnector", "Dropped wire " + wire); + } + + @Override + public void unlinkWire(Wire wire) { + _attachments.remove(wire); + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundInputConnector.java new file mode 100644 index 0000000..b22426f --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundInputConnector.java @@ -0,0 +1,98 @@ +package com.codigoparallevar.minicards.parts.connectors; + +import android.util.Log; + +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; +import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; +import com.codigoparallevar.minicards.types.wireData.StringSignal; + +import java.util.LinkedList; +import java.util.List; + +public class StringRoundInputConnector implements StringInputConnector { + private final Part _part; + private int _xposition; + private int _yposition; + private final int _radius; + private final List _attachments = new LinkedList<>(); + + public StringRoundInputConnector(Part part, + int inputConnectorCenterX, int inputConnectorCenterY, + int inputConnectorRadius) { + _part = part; + _xposition = inputConnectorCenterX; + _yposition = inputConnectorCenterY; + _radius = inputConnectorRadius; + } + + @Override + public boolean containsPoint(int x, int y) { + return ((Math.abs(x - _xposition) <= _radius) + && (Math.abs(y - _yposition) <= _radius)); + } + + @Override + public Moveable getMoveable() { + return new Wire(this, _xposition, _yposition); + } + + @Override + public void unlink() { + for (Wire wire : _attachments) { + wire.unlink(); + } + } + + @Override + public void updatePosition(int x, int y) { + _xposition = x; + _yposition = y; + + for (Wire wire : _attachments){ + wire.moveEnd(x, y); + } + } + + @Override + public int getX() { + return _xposition; + } + + @Override + public int getY() { + return _yposition; + } + + @Override + public void send(StringSignal signal) { + _part.send(this, signal); + } + + + @Override + public void getAttachment(Wire wire) { + _attachments.add(wire); + } + + @Override + public Part getPart() { + return _part; + } + + @Override + public String getId() { + return _part.getConnectorId(this); + } + + @Override + public void drop(Wire wire) { + Log.d("InputConnector", "Dropped wire " + wire); + } + + @Override + public void unlinkWire(Wire wire) { + _attachments.remove(wire); + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java new file mode 100644 index 0000000..8dfb040 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java @@ -0,0 +1,158 @@ +package com.codigoparallevar.minicards.parts.connectors; + +import android.util.Log; + +import com.codigoparallevar.minicards.ScrolledCanvas; +import com.codigoparallevar.minicards.types.Drawable; +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.PartGrid; +import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.connectors.Wiring.StringWire; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.StringOutputConnector; +import com.codigoparallevar.minicards.types.wireData.StringSignal; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +public class StringRoundOutputConnector implements Drawable, StringOutputConnector { + private PartGrid _partGrid; + private int _centerX; + private int _centerY; + private final int _radius; + private final Part _part; + private StringWire _currentWire = null; + private final List _wires; + private final HashSet _connections; + + public StringRoundOutputConnector(Part part, PartGrid partGrid, int centerX, int centerY, int radius) { + _part = part; + _partGrid = partGrid; + _centerX = centerX; + _centerY = centerY; + _radius = radius; + _wires = new LinkedList<>(); + _connections = new HashSet<>(); + } + + @Override + public boolean containsPoint(int x, int y) { + return ((Math.abs(x - _centerX) <= _radius) + && (Math.abs(y - _centerY) <= _radius)); + } + + + @Override + public Moveable getMoveable() { + if (_currentWire == null) { + startWire(); + } + + return _currentWire; + } + + @Override + public void unlink() {} + + private void startWire() { + _currentWire = new StringWire(this, _centerX, _centerY); + } + + @Override + public void drop(StringWire wire) { + + if (wire == _currentWire){ + _currentWire = null; + + StringInputConnector resultPoint = _partGrid.getStringInputConnectorOn( + wire.getXEnd(), wire.getYEnd()); + + Log.d("RoundOutputConnector", "Dropped wire on " + resultPoint); + + // Not connected + if (resultPoint == null){ + return; + } + + // Already connected + if (_connections.contains(resultPoint)) { + return; + } + _connections.add(resultPoint); + + wire.attachTo(resultPoint); + _wires.add(wire); + } + else { + Log.w("RoundOutputConnector", + "Asked to drop non matching wire " + + "(expected " + _currentWire + ", got " + wire + ")"); + } + } + + @Override + public void unlinkWire(StringWire wire) { + _wires.remove(wire); + } + + @Override + public void drawWires(ScrolledCanvas canvas, boolean devMode) { + for (StringWire wire : _wires) { + wire.draw(canvas, devMode); + } + + if (_currentWire != null) { + _currentWire.draw(canvas, devMode); + } + } + + @Override + public void updatePosition(int x, int y) { + _centerX = x; + _centerY = y; + + for (StringWire wire : _wires){ + wire.moveStart(x, y); + } + } + + @Override + public List> getConnectionEndpoints() { + List> endpointIds = new LinkedList<>(); + + for (StringWire wire : _wires) { + InputConnector inputConnector = wire.getAttachedTo(); + Part endPart = inputConnector.getPart(); + endpointIds.add(new Tuple2<>(inputConnector.getId(), endPart.get_id())); + } + + return endpointIds; + } + + @Override + public void connectTo(StringInputConnector inputConnector) { + if (_connections.contains(inputConnector)) { + return; + } + + _connections.add(inputConnector); + + StringWire wire = new StringWire(this, _centerX, _centerY); + wire.attachTo(inputConnector); + _wires.add(wire); + } + + @Override + public void draw(ScrolledCanvas canvas, boolean devMode) { + // TODO: Complete this part + } + + public void send(StringSignal signal) { + for (StringWire wire : _wires){ + wire.send(signal); + } + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java b/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java new file mode 100644 index 0000000..8113fe5 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java @@ -0,0 +1,351 @@ +package com.codigoparallevar.minicards.parts.strings; + +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; +import android.support.annotation.NonNull; +import android.util.Log; + +import com.codigoparallevar.minicards.PartInstantiator; +import com.codigoparallevar.minicards.ScrolledCanvas; +import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector; +import com.codigoparallevar.minicards.parts.connectors.StringRoundOutputConnector; +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.PartConnection; +import com.codigoparallevar.minicards.types.PartGrid; +import com.codigoparallevar.minicards.types.Tuple2; +import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.StringSignal; +import com.codigoparallevar.minicards.types.wireData.WireDataType; +import com.codigoparallevar.minicards.utils.Serializations; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class ConvertToString implements Part { + private static final String TOGGLE_INPUT_CONNECTOR_ID = "any_input_connector"; + private static final int DEFAULT_SIDE_SIZE = 100; + private final String _id; + private final PartGrid _partGrid; + private final StringRoundOutputConnector _stringOutputConnector; + private final LinkedList _outputConnectors; + private int _left; + private int _top; + private int _right; + private int _bottom; + private List inputConnectors; + private AnyInputConnector _toggleInputConnector; + + @NonNull + private String _lastValue; + + private ConvertToString(String id, PartGrid partGrid, int left, int top, int right, int bottom) { + _id = id; + _partGrid = partGrid; + _left = left; + _top = top; + _right = right; + _bottom = bottom; + _lastValue = "-"; + + // Input connector + _toggleInputConnector = new AnyRoundInputConnector( + this, + getInputConnectorCenterX(), + getInputConnectorCenterY(), + getInputConnectRadius()); + inputConnectors = new LinkedList<>(); + inputConnectors.add(_toggleInputConnector); + + // Output connector + // Create connectors + _stringOutputConnector = new StringRoundOutputConnector( + this, + _partGrid, + getOutputConnectorCenterX(), getOutputConnectorCenterY(), + getOutputConnectRadius()); + + _outputConnectors = new LinkedList<>(); + _outputConnectors.add(_stringOutputConnector); + } + + public ConvertToString(PartGrid partGrid, int left, int top, int right, int bottom) { + this(UUID.randomUUID().toString(), partGrid, left, top, right, bottom); + } + + @Override + public int get_left() { + return _left; + } + + @Override + public int get_right() { + return _right; + } + + @Override + public int get_top() { + return _top; + } + + @Override + public int get_bottom() { + return _bottom; + } + + @Override + public void draw(ScrolledCanvas canvas, boolean devMode) { + + if (devMode) { + drawConnector(canvas); + drawWires(canvas, devMode); + + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); + paint.setColor(Color.WHITE); + canvas.drawRect( + new Rect(_left, _top, + _right, _bottom), + paint); + + Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + textPaint.setColor(Color.GREEN); + textPaint.setTextSize(100); + canvas.drawText(_lastValue, + _left, + _top - 5, + textPaint); + } + } + + private void drawConnector(ScrolledCanvas canvas) { + Paint inputConnectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + inputConnectorPaint.setColor(Color.YELLOW); + + canvas.drawCircle( + getInputConnectorCenterX(), getInputConnectorCenterY(), + getInputConnectRadius(), + inputConnectorPaint); + + Paint outputConnectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + outputConnectorPaint.setColor(Color.RED); + + canvas.drawCircle(_right, getOutputConnectorCenterY(), + getOutputConnectRadius(), + outputConnectorPaint); + } + + private void drawWires(ScrolledCanvas canvas, boolean devMode) { + for (OutputConnector outputConnector : _outputConnectors){ + outputConnector.drawWires(canvas, devMode); + } + } + + @Override + public void moveEnd(int x, int y) { + final int width = _right - _left; + final int height = _bottom - _top; + + _left = x - width / 2; + _right = _left + width; + + _top = y - height / 2; + _bottom = _top + height; + + _toggleInputConnector.updatePosition( + getInputConnectorCenterX(), + getInputConnectorCenterY()); + + _stringOutputConnector.updatePosition( + getOutputConnectorCenterX(), + getOutputConnectorCenterY()); + } + + @Override + public void drop(int x, int y) { + moveEnd(x, y); + } + + @Override + public void touched() { + Log.d("Placeholder", "Placeholder touched"); + } + + @Override + public List getInputConnectors() { + return inputConnectors; + } + + @Override + public List getOutputConnectors() { + return _outputConnectors; + } + + public AnyInputConnector getToggleInputConnector() { + return _toggleInputConnector; + } + + public OutputConnector getToggleOutputConnector() { + return _stringOutputConnector; + } + + + @Override + public JSONObject serialize() throws JSONException { + JSONObject serialized = new JSONObject(); + + serialized.put("id", _id); + serialized.put("left", _left); + serialized.put("top", _top); + serialized.put("right", _right); + serialized.put("bottom", _bottom); + + serialized.put("on_string_output_connector", + Serializations.serialize(serializeConnectionEndpoints())); + + return serialized; + } + + private List> serializeConnectionEndpoints() { + List> serializedData = new LinkedList<>(); + + for (Tuple2 endpoint : _stringOutputConnector.getConnectionEndpoints()){ + serializedData.add(PartConnection.serialize(endpoint.item1, endpoint.item2)); + } + + return serializedData; + } + + @Override + public void send(InputConnector roundInputConnector, WireDataType signal) { + String encoded = "null"; + Object value = signal.get(); + + if (value != null){ + encoded = value.toString(); + } + + _lastValue = encoded; + + _stringOutputConnector.send(new StringSignal(encoded)); + } + + @Override + public String get_id() { + return _id; + } + + @Override + public InputConnector getConnectorWithId(String inputConnectorId) { + switch (inputConnectorId){ + case ConvertToString.TOGGLE_INPUT_CONNECTOR_ID: + return _toggleInputConnector; + + default: + return null; + } + } + + @Override + public String getConnectorId(InputConnector inputConnector) { + if (inputConnector == _toggleInputConnector){ + return ConvertToString.TOGGLE_INPUT_CONNECTOR_ID; + } + + return null; + } + + @Override + public void resume() { + + } + + @Override + public void pause() { + + } + + public static Tuple2> deserialize(PartGrid partGrid, JSONObject data) throws JSONException { + String id = data.getString("id"); + int left = data.getInt("left"); + int top = data.getInt("top"); + int right = data.getInt("right"); + int bottom = data.getInt("bottom"); + + ConvertToString toggle = new ConvertToString(id, partGrid, left, top, right, bottom); + + 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))); + } + + return new Tuple2>(toggle, connections); + } + + @Override + public boolean containsPoint(int x, int y) { + return (x >= get_left()) && (x <= get_right()) + && (y >= get_top()) && (y <= get_bottom()); + } + + @Override + public Moveable getMoveable() { + return this; + } + + @Override + public void unlink() { + for (InputConnector input : getInputConnectors()) { + input.unlink(); + } + } + + public int getInputConnectorCenterX() { + return get_left(); + } + + private int getInputConnectRadius() { + return (get_right() - get_left()) / 2; + } + + public int getInputConnectorCenterY() { + return (get_top() + get_bottom()) / 2; + } + + private int getOutputConnectorCenterX() { + return _right; + } + + private int getOutputConnectorCenterY() { + return (_top + _bottom) / 2; + } + + private int getOutputConnectRadius() { + return (_right - _left) / 2; + } + + + public static PartInstantiator getInstantiator() { + final int halfSideSize = DEFAULT_SIDE_SIZE / 2; + return new PartInstantiator() { + @Override + protected Part instantiate(PartGrid grid, Tuple2 center) { + return new ConvertToString(grid, + center.item1 - halfSideSize, center.item2 - halfSideSize, + center.item1 + halfSideSize, center.item2 + halfSideSize); + } + }; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java index 640acab..dde9f01 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java @@ -22,7 +22,7 @@ public interface Part extends Selectable, Moveable, Drawable { JSONObject serialize() throws JSONException; - void send(InputConnector roundInputConnector, WireDataType signal); + void send(InputConnector inputConnector, WireDataType signal); String get_id(); diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java index 71d93ca..b0a0161 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java @@ -2,6 +2,7 @@ package com.codigoparallevar.minicards.types; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; public interface PartGrid { Selectable getPartOn(int x, int y); @@ -12,4 +13,6 @@ public interface PartGrid { Tuple2 getCenteredOn(); void update(); + + StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java new file mode 100644 index 0000000..9ccd9d5 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java @@ -0,0 +1,19 @@ +package com.codigoparallevar.minicards.types.connectors.Wiring; + +import com.codigoparallevar.minicards.types.Drawable; +import com.codigoparallevar.minicards.types.Dropper; +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; +import com.codigoparallevar.minicards.types.wireData.StringSignal; + +public class StringWire extends Wire implements Moveable, Drawable { + public StringWire(Dropper dropper, int xInit, int yInit) { + super(dropper, xInit, yInit); + } + + public void send(StringSignal signal) { + if (_attachedTo != null) { + _attachedTo.send(signal); + } + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/AnyInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/AnyInputConnector.java new file mode 100644 index 0000000..a92dd15 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/AnyInputConnector.java @@ -0,0 +1,131 @@ +package com.codigoparallevar.minicards.types.connectors.input; + +import android.support.annotation.NonNull; + +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; +import com.codigoparallevar.minicards.types.connectors.output.BooleanOutputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector; +import com.codigoparallevar.minicards.types.connectors.output.StringOutputConnector; +import com.codigoparallevar.minicards.types.wireData.BooleanSignal; +import com.codigoparallevar.minicards.types.wireData.Signal; +import com.codigoparallevar.minicards.types.wireData.StringSignal; +import com.codigoparallevar.minicards.types.wireData.WireDataType; + + +public abstract class AnyInputConnector implements InputConnector { + + public SignalInputConnector ToSignalInputConnector() { + return new WrapAsSignal(this); + } + + public BooleanInputConnector ToBooleanInputConnector(){ + return new WrapAsBoolean(this); + } + + public StringInputConnector ToStringInputConnector(){ + return new WrapAsString(this); + } + + @NonNull + public InputConnector matchType(@NonNull OutputConnector outputConnector) { + if (outputConnector instanceof SignalOutputConnector) { + return this.ToSignalInputConnector(); + } + else if (outputConnector instanceof BooleanOutputConnector){ + return this.ToBooleanInputConnector(); + } + else if (outputConnector instanceof StringOutputConnector){ + return this.ToStringInputConnector(); + } + + return this; + } + + private class WrapAsSignal extends WrapAs implements SignalInputConnector { + WrapAsSignal(AnyInputConnector baseInputConnector) { + super(baseInputConnector); + } + + @Override + public void send(Signal data) { + baseInputConnector.send(data); + } + } + + private class WrapAsBoolean extends WrapAs implements BooleanInputConnector { + WrapAsBoolean(AnyInputConnector baseInputConnector) { + super(baseInputConnector); + } + + @Override + public void send(BooleanSignal data) { + baseInputConnector.send(data); + } + } + + private class WrapAsString extends WrapAs implements StringInputConnector { + WrapAsString(AnyInputConnector baseInputConnector) { + super(baseInputConnector); + } + + @Override + public void send(StringSignal data) { + baseInputConnector.send(data); + } + } + + private abstract class WrapAs { + final AnyInputConnector baseInputConnector; + + WrapAs(AnyInputConnector baseInputConnector) { + this.baseInputConnector = baseInputConnector; + } + + public boolean containsPoint(int x, int y) { + return baseInputConnector.containsPoint(x, y); + } + + public Moveable getMoveable() { + return baseInputConnector.getMoveable(); + } + + public void unlink() { + baseInputConnector.unlink(); + } + + public void drop(Wire wire) { + baseInputConnector.drop(wire); + } + + public void unlinkWire(Wire wire) { + baseInputConnector.unlinkWire(wire); + } + + public void updatePosition(int x, int y) { + baseInputConnector.updatePosition(x, y); + } + + public int getX() { + return baseInputConnector.getX(); + } + + public int getY() { + return baseInputConnector.getY(); + } + + public Part getPart() { + return baseInputConnector.getPart(); + } + + public String getId() { + return baseInputConnector.getId(); + } + + public void getAttachment(Wire wire) { + baseInputConnector.getAttachment(wire); + } + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/StringInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/StringInputConnector.java new file mode 100644 index 0000000..56b4d77 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/StringInputConnector.java @@ -0,0 +1,6 @@ +package com.codigoparallevar.minicards.types.connectors.input; + +import com.codigoparallevar.minicards.types.wireData.StringSignal; + +public interface StringInputConnector extends InputConnector { +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/StringOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/StringOutputConnector.java new file mode 100644 index 0000000..769cff7 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/StringOutputConnector.java @@ -0,0 +1,11 @@ +package com.codigoparallevar.minicards.types.connectors.output; + +import com.codigoparallevar.minicards.types.connectors.Wiring.StringWire; +import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; +import com.codigoparallevar.minicards.types.wireData.StringSignal; + +public interface StringOutputConnector extends OutputConnector< + StringSignal, + StringInputConnector, + StringWire> { +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/BooleanSignal.java b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/BooleanSignal.java index 7a530e4..866617c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/BooleanSignal.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/BooleanSignal.java @@ -1,9 +1,14 @@ package com.codigoparallevar.minicards.types.wireData; -public class BooleanSignal implements WireDataType { +public class BooleanSignal implements WireDataType { public final boolean value; public BooleanSignal(boolean value) { this.value = value; } + + @Override + public Boolean get() { + return value; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/Signal.java b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/Signal.java index 0afe474..36f4703 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/Signal.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/Signal.java @@ -1,4 +1,8 @@ package com.codigoparallevar.minicards.types.wireData; -public class Signal implements WireDataType { +public class Signal implements WireDataType { + @Override + public Object get() { + return null; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/StringSignal.java b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/StringSignal.java new file mode 100644 index 0000000..ab604bd --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/StringSignal.java @@ -0,0 +1,14 @@ +package com.codigoparallevar.minicards.types.wireData; + +public class StringSignal implements WireDataType { + public final String value; + + public StringSignal(String value) { + this.value = value; + } + + @Override + public String get() { + return value; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/WireDataType.java b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/WireDataType.java index 6d073f8..c8cb8a9 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/WireDataType.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/WireDataType.java @@ -1,4 +1,5 @@ package com.codigoparallevar.minicards.types.wireData; -public interface WireDataType { +public interface WireDataType { + T get(); }