diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index d9154d9..0dbd211 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -400,11 +400,9 @@ public class CanvasView extends View implements PartGrid { @Override @Nullable public BooleanInputConnector getBooleanInputConnectorOn(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()){ BooleanInputConnector booleanInputConnector; if (inputConnector instanceof BooleanInputConnector){ @@ -426,6 +424,25 @@ public class CanvasView extends View implements PartGrid { return null; } + @Override + public AnyInputConnector getAnyInputConnectorOn(int x, int y) { + for (int i = parts.size() - 1; i >= 0; i--){ + final Part part = parts.get(i); + + for (InputConnector inputConnector : part.getInputConnectors()){ + if (!(inputConnector instanceof AnyInputConnector)) { + continue; + } + + if (inputConnector.containsPoint(x, y)){ + return (AnyInputConnector) inputConnector; + } + } + } + + return null; + } + @Override @Nullable public StringInputConnector getStringInputConnectorOn(int x, int y) { diff --git a/app/src/main/java/com/codigoparallevar/minicards/SignalListenerManager.java b/app/src/main/java/com/codigoparallevar/minicards/SignalListenerManager.java index badbcc8..b9b50eb 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/SignalListenerManager.java +++ b/app/src/main/java/com/codigoparallevar/minicards/SignalListenerManager.java @@ -58,9 +58,10 @@ public class SignalListenerManager implements ProgramakerSignalListener { remainingListeners.remove(listener); if (remainingListeners.size() == 0) { ProgramakerListeningChannel channel = idToChannel.get(id); - channel.stop(); idToChannel.remove(id); channelToListener.remove(id); + + channel.stop(); } } } @@ -69,6 +70,15 @@ public class SignalListenerManager implements ProgramakerSignalListener { Log.w(LogTag, "Connection lost to (bridge="+bridgeId+",key="+key + ")"); Tuple2 id = new Tuple2<>(bridgeId, key); + // Check that there is still someone listening + if (!this.channelToListener.containsKey(id)) { + return; + } + if (this.channelToListener.get(id).size() == 0) { + this.channelToListener.remove(id); + return; + } + // On disconnect disable the connection, wait 2 seconds and retry idToChannel.put(id, null); diff --git a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java index b1a49a7..4cff119 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java @@ -2,6 +2,7 @@ package com.codigoparallevar.minicards; import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.Selectable; +import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; @@ -34,6 +35,16 @@ class StubPartGrid implements PartGrid { return null; } + @Override + public AnyInputConnector getAnyInputConnectorOn(int x, int y) { + return null; + } + + @Override + public StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd) { + return null; + } + @Override public Tuple2 getCenteredOn() { return null; @@ -43,8 +54,4 @@ class StubPartGrid implements PartGrid { public void update() { } - @Override - public StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd) { - return null; - } } 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 8ceb2ba..3188268 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java @@ -8,8 +8,10 @@ import android.util.Log; import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector; +import com.codigoparallevar.minicards.parts.connectors.AnyRoundOutputConnector; import com.codigoparallevar.minicards.parts.connectors.ConnectorTypeInfo; import com.codigoparallevar.minicards.parts.connectors.RoundOutputConnector; +import com.codigoparallevar.minicards.parts.connectors.SignalRoundOutputConnector; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; @@ -17,6 +19,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.AnySignal; import com.codigoparallevar.minicards.types.wireData.Signal; import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.ui_helpers.DoAsync; @@ -59,8 +62,8 @@ public class ProgramakerCustomBlockPart implements Part, ProgramakerSignalListen private String token = null; private Object[] lastValues; private boolean active = true; - private Tuple2 saveToOutput; - private RoundOutputConnector pulseOutput; + private Tuple2 saveToOutput; + private SignalRoundOutputConnector pulseOutput; public ProgramakerCustomBlockPart(String id, PartGrid grid, Tuple2 center, ProgramakerCustomBlock block) { this._id = id; @@ -141,7 +144,7 @@ public class ProgramakerCustomBlockPart implements Part, ProgramakerSignalListen final List> inputs = new LinkedList<>(); final List> outputs = new LinkedList<>(); - RoundOutputConnector pulseOutput = null; + SignalRoundOutputConnector pulseOutput = null; // Add pulses if (has_pulse_input) { @@ -151,7 +154,7 @@ public class ProgramakerCustomBlockPart implements Part, ProgramakerSignalListen ); } if (has_pulse_output) { - pulseOutput = new RoundOutputConnector(this, this._partGrid, 0, 0, + pulseOutput = new SignalRoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS); outputs.add(new Tuple2<>(new ConnectorTypeInfo(ConnectorTypeInfo.Type.PULSE), pulseOutput) @@ -188,17 +191,17 @@ public class ProgramakerCustomBlockPart implements Part, ProgramakerSignalListen } } - Tuple2 saveToOutput = null; + Tuple2 saveToOutput = null; if (savedTo != null) { saveToOutput = new Tuple2<>(ConnectorTypeInfo.FromTypeName(savedTo.getType()), - new RoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS)); - outputs.add(saveToOutput); + new AnyRoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS)); + outputs.add(new Tuple2<>(saveToOutput.item1, saveToOutput.item2)); } if (hasImplicitOutput) { outputs.add(new Tuple2<>(ConnectorTypeInfo.FromTypeName(_block.getBlock_result_type()), - new RoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS))); + new AnyRoundOutputConnector(this, this._partGrid, 0, 0, IO_RADIUS))); } // Tie everything @@ -410,13 +413,16 @@ public class ProgramakerCustomBlockPart implements Part, ProgramakerSignalListen } private void onExecutionCompleted(ProgramakerFunctionCallResult result) { - Tuple2 savedTo = this.saveToOutput; + Tuple2 savedTo = this.saveToOutput; if (savedTo != null) { - // TODO: Fix output typing - // savedTo.item2.send((WireDataType) () -> result.getResult()); + Object value = null; + if (result != null) { + result.getResult(); + } + savedTo.item2.send(new AnySignal(value)); } - RoundOutputConnector pulseOutput = this.pulseOutput; + SignalRoundOutputConnector pulseOutput = this.pulseOutput; if (pulseOutput != null) { pulseOutput.send(new Signal()); } @@ -537,7 +543,7 @@ public class ProgramakerCustomBlockPart implements Part, ProgramakerSignalListen // Stream object on save_to, then trigger pulse Object content = signal.get("content"); if (this.saveToOutput != null) { - // this.saveToOutput.item2.send(content); // TODO: Have an output type that allows this + this.saveToOutput.item2.send(new AnySignal(content)); } if (this.pulseOutput != null) { diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java index 6b2c732..dd0e01a 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java @@ -6,7 +6,7 @@ import android.util.Log; import com.codigoparallevar.minicards.PartInstantiator; import com.codigoparallevar.minicards.ScrolledCanvas; -import com.codigoparallevar.minicards.parts.connectors.RoundOutputConnector; +import com.codigoparallevar.minicards.parts.connectors.SignalRoundOutputConnector; import com.codigoparallevar.minicards.parts.style.CardTheme; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; @@ -39,7 +39,7 @@ public class RoundButton implements Part { private final int _outerRadiusThickness = 10; private final int _pathRunWay = 200; private List _outputConnectors; - private final RoundOutputConnector _pressedOutputConnector; + private final SignalRoundOutputConnector _pressedOutputConnector; private final static int DEFAULT_INNER_RADIUS = 80; private final static int DEFAULT_OUTER_RADIUS = 100; @@ -54,7 +54,7 @@ public class RoundButton implements Part { _outerRadius = outerRadius; // Create connectors - _pressedOutputConnector = new RoundOutputConnector( + _pressedOutputConnector = new SignalRoundOutputConnector( this, _partGrid, getOutputConnectorCenterX(), getOutputConnectorCenterY(), @@ -234,7 +234,7 @@ public class RoundButton implements Part { return null; // Only pulse output, so no relevant data } - public RoundOutputConnector getPressedOutputConnector(){ + public SignalRoundOutputConnector getPressedOutputConnector(){ return _pressedOutputConnector; } 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 index 7c5db4b..17e5142 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java @@ -4,14 +4,15 @@ import android.util.Log; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.connectors.RoundConnector; import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; -import com.codigoparallevar.minicards.types.wireData.WireDataType; +import com.codigoparallevar.minicards.types.wireData.AnySignal; import java.util.LinkedList; import java.util.List; -public class AnyRoundInputConnector extends AnyInputConnector { +public class AnyRoundInputConnector extends AnyInputConnector implements RoundConnector { private final Part _part; private int _xposition; private int _yposition; @@ -81,7 +82,7 @@ public class AnyRoundInputConnector extends AnyInputConnector { } @Override - public void send(WireDataType signal) { + public void send(AnySignal signal) { _part.send(this, signal); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundOutputConnector.java new file mode 100644 index 0000000..bae7fdc --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundOutputConnector.java @@ -0,0 +1,176 @@ +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.connectors.Wiring.AnyWire; +import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.output.AnyOutputConnector; +import com.codigoparallevar.minicards.types.functional.Tuple2; +import com.codigoparallevar.minicards.types.wireData.AnySignal; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +public class AnyRoundOutputConnector implements Drawable, AnyOutputConnector, + RoundOutputConnector { + private PartGrid _partGrid; + private int _centerX; + private int _centerY; + private final int _radius; + private final Part _part; + private AnyWire _currentWire = null; + private final List _wires; + private final HashSet _connections; + + public AnyRoundOutputConnector(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 AnyWire(this, _centerX, _centerY); + } + + @Override + public void drop(AnyWire wire) { + + if (wire == _currentWire){ + _currentWire = null; + + AnyInputConnector resultPoint = _partGrid.getAnyInputConnectorOn( + 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, this); + _wires.add(wire); + } + else { + Log.w("RoundOutputConnector", + "Asked to drop non matching wire " + + "(expected " + _currentWire + ", got " + wire + ")"); + } + } + + @Override + public void unlinkWire(AnyWire wire) { + _wires.remove(wire); + } + + @Override + public void drawWires(ScrolledCanvas canvas, boolean devMode) { + for (AnyWire 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 (AnyWire wire : _wires){ + wire.moveStart(x, y); + } + } + + @Override + public List> getConnectionEndpoints() { + List> endpointIds = new LinkedList<>(); + + for (AnyWire 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(AnyInputConnector inputConnector) { + if (_connections.contains(inputConnector)) { + return; + } + + _connections.add(inputConnector); + + AnyWire wire = new AnyWire(this, _centerX, _centerY); + wire.attachTo(inputConnector, this); + _wires.add(wire); + } + + @Override + public void draw(ScrolledCanvas canvas, boolean devMode) { + // TODO: Complete this part + } + + public void send(AnySignal signal) { + for (AnyWire wire : _wires){ + wire.send(signal); + } + } + + @Override + public Object query(Object lastValue) { + return _part.query(lastValue); + } + + public int getX() { + return _centerX; + } + + public int getY() { + return _centerY; + } + + public float getRadius() { + return _radius; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java index e5846ca..f68b04f 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java @@ -7,11 +7,11 @@ 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.functional.Tuple2; import com.codigoparallevar.minicards.types.connectors.Wiring.BooleanWire; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.output.BooleanOutputConnector; +import com.codigoparallevar.minicards.types.functional.Tuple2; import com.codigoparallevar.minicards.types.wireData.BooleanSignal; import java.util.HashSet; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java index e19c790..53844b3 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java @@ -1,175 +1,14 @@ 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.functional.Tuple2; -import com.codigoparallevar.minicards.types.connectors.Wiring.SignalWire; +import com.codigoparallevar.minicards.types.connectors.RoundConnector; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; -import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; -import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector; -import com.codigoparallevar.minicards.types.wireData.Signal; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.WireDataType; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; - -public class RoundOutputConnector implements Drawable, SignalOutputConnector { - private PartGrid _partGrid; - private int _centerX; - private int _centerY; - private final int _radius; - private final Part _part; - private SignalWire _currentWire = null; - private final List _wires; - private final HashSet _connections; - - public RoundOutputConnector(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 SignalWire(this, _centerX, _centerY); - } - - @Override - public void drop(SignalWire wire) { - - if (wire == _currentWire){ - _currentWire = null; - - SignalInputConnector resultPoint = _partGrid.getSignalInputConnectorOn( - 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, this); - _wires.add(wire); - } - else { - Log.w("RoundOutputConnector", - "Asked to drop non matching wire " - + "(expected " + _currentWire + ", got " + wire + ")"); - } - } - - @Override - public void unlinkWire(SignalWire wire) { - _wires.remove(wire); - } - - @Override - public void drawWires(ScrolledCanvas canvas, boolean devMode) { - for (SignalWire 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 (SignalWire wire : _wires){ - wire.moveStart(x, y); - } - } - - @Override - public List> getConnectionEndpoints() { - List> endpointIds = new LinkedList<>(); - - for (SignalWire 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(SignalInputConnector inputConnector) { - if (_connections.contains(inputConnector)) { - return; - } - - _connections.add(inputConnector); - - SignalWire wire = new SignalWire(this, _centerX, _centerY); - wire.attachTo(inputConnector, this); - _wires.add(wire); - } - - @Override - public void draw(ScrolledCanvas canvas, boolean devMode) { - // TODO: Complete this part - } - - public void send(Signal signal) { - for (SignalWire wire : _wires){ - wire.send(signal); - } - } - - @Override - public Object query(Object lastValue) { - return _part.query(lastValue); - } - - public float getX() { - return _centerX; - } - - public float getY() { - return _centerY; - } - - public float getRadius() { - return _radius; - } +public interface RoundOutputConnector< + T1 extends WireDataType, + T2 extends InputConnector, + T3 extends Wire + > extends RoundConnector, OutputConnector { } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/SignalRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/SignalRoundOutputConnector.java new file mode 100644 index 0000000..3aa0dc6 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/SignalRoundOutputConnector.java @@ -0,0 +1,176 @@ +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.connectors.Wiring.SignalWire; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector; +import com.codigoparallevar.minicards.types.functional.Tuple2; +import com.codigoparallevar.minicards.types.wireData.Signal; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +public class SignalRoundOutputConnector implements Drawable, SignalOutputConnector, + RoundOutputConnector { + private PartGrid _partGrid; + private int _centerX; + private int _centerY; + private final int _radius; + private final Part _part; + private SignalWire _currentWire = null; + private final List _wires; + private final HashSet _connections; + + public SignalRoundOutputConnector(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 SignalWire(this, _centerX, _centerY); + } + + @Override + public void drop(SignalWire wire) { + + if (wire == _currentWire){ + _currentWire = null; + + SignalInputConnector resultPoint = _partGrid.getSignalInputConnectorOn( + 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, this); + _wires.add(wire); + } + else { + Log.w("RoundOutputConnector", + "Asked to drop non matching wire " + + "(expected " + _currentWire + ", got " + wire + ")"); + } + } + + @Override + public void unlinkWire(SignalWire wire) { + _wires.remove(wire); + } + + @Override + public void drawWires(ScrolledCanvas canvas, boolean devMode) { + for (SignalWire 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 (SignalWire wire : _wires){ + wire.moveStart(x, y); + } + } + + @Override + public List> getConnectionEndpoints() { + List> endpointIds = new LinkedList<>(); + + for (SignalWire 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(SignalInputConnector inputConnector) { + if (_connections.contains(inputConnector)) { + return; + } + + _connections.add(inputConnector); + + SignalWire wire = new SignalWire(this, _centerX, _centerY); + wire.attachTo(inputConnector, this); + _wires.add(wire); + } + + @Override + public void draw(ScrolledCanvas canvas, boolean devMode) { + // TODO: Complete this part + } + + public void send(Signal signal) { + for (SignalWire wire : _wires){ + wire.send(signal); + } + } + + @Override + public Object query(Object lastValue) { + return _part.query(lastValue); + } + + public int getX() { + return _centerX; + } + + public int getY() { + return _centerY; + } + + public float getRadius() { + return _radius; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java index d7f3759..f706311 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java @@ -7,7 +7,7 @@ import android.util.Log; import com.codigoparallevar.minicards.PartInstantiator; import com.codigoparallevar.minicards.ScrolledCanvas; -import com.codigoparallevar.minicards.parts.connectors.RoundOutputConnector; +import com.codigoparallevar.minicards.parts.connectors.SignalRoundOutputConnector; import com.codigoparallevar.minicards.parts.style.CardTheme; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; @@ -40,7 +40,7 @@ public class Ticker implements Part { private int _right; private int _bottom; private List _outputConnectors; - private final RoundOutputConnector _signalOutputConnector; + private final SignalRoundOutputConnector _signalOutputConnector; private final long SLEEP_TIME = 1000; private Thread _thread = null; @@ -53,7 +53,7 @@ public class Ticker implements Part { _bottom = bottom; // Create connectors - _signalOutputConnector = new RoundOutputConnector( + _signalOutputConnector = new SignalRoundOutputConnector( this, _partGrid, getOutputConnectorCenterX(), getOutputConnectorCenterY(), 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 index 75d5a26..7ac2087 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java @@ -257,7 +257,7 @@ public class ConvertToString implements Part { encoded = value.toString(); } - if ((_lastValue != null) && !_lastValue.equals(encoded)) { + if ((_lastValue == null) || !_lastValue.equals(encoded)) { _lastValue = encoded; _stringOutputConnector.send(new StringSignal(encoded)); 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 9cc90b6..2cffe7e 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java @@ -1,6 +1,7 @@ package com.codigoparallevar.minicards.types; import com.codigoparallevar.minicards.SignalListenerManager; +import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; @@ -14,10 +15,11 @@ public interface PartGrid { SignalInputConnector getSignalInputConnectorOn(int x, int y); BooleanInputConnector getBooleanInputConnectorOn(int x, int y); + AnyInputConnector getAnyInputConnectorOn(int x, int y); + StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd); Tuple2 getCenteredOn(); void update(); - StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/RoundConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/RoundConnector.java new file mode 100644 index 0000000..042bac9 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/RoundConnector.java @@ -0,0 +1,7 @@ +package com.codigoparallevar.minicards.types.connectors; + +public interface RoundConnector { + int getX(); + int getY(); + float getRadius(); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/AnyWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/AnyWire.java new file mode 100644 index 0000000..cc2e30a --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/AnyWire.java @@ -0,0 +1,19 @@ +package com.codigoparallevar.minicards.types.connectors.Wiring; + +import com.codigoparallevar.minicards.types.Drawable; +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.AnySignal; + +public class AnyWire extends Wire implements Moveable, Drawable { + public AnyWire(OutputConnector dropper, int xInit, int yInit) { + super(dropper, xInit, yInit); + } + + public void send(AnySignal 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 index fefc48e..ac64455 100644 --- 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 @@ -9,13 +9,13 @@ import com.codigoparallevar.minicards.types.connectors.output.BooleanOutputConne 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.AnySignal; 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 abstract class AnyInputConnector implements InputConnector { public SignalInputConnector ToSignalInputConnector() { return new WrapAsSignal(this); @@ -51,7 +51,7 @@ public abstract class AnyInputConnector implements InputConnector { +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/wireData/AnySignal.java b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/AnySignal.java new file mode 100644 index 0000000..67de038 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/AnySignal.java @@ -0,0 +1,14 @@ +package com.codigoparallevar.minicards.types.wireData; + +public class AnySignal implements WireDataType { + public final Object value; + + public AnySignal(Object value) { + this.value = value; + } + + @Override + public Object get() { + return value; + } +}