diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 3f45a70..6a7cc70 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -12,16 +12,17 @@ import android.view.MotionEvent; import android.view.View; import com.codigoparallevar.minicards.motion.MotionMode; -import com.codigoparallevar.minicards.types.PartConnection; -import com.codigoparallevar.minicards.types.connectors.input.InputConnector; -import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; -import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; 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.BooleanInputConnector; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import java.io.IOException; import java.util.ArrayList; @@ -312,7 +313,7 @@ class CanvasView extends View implements PartGrid { } } // Then with input ones - for (InputConnector inputConnector : part.getSignalInputConnectors()){ + for (InputConnector inputConnector : part.getInputConnectors()){ if (inputConnector.containsPoint(x, y)){ return inputConnector; } @@ -330,9 +331,38 @@ class CanvasView extends View implements PartGrid { final Part part = parts.get(i); // Then with input ones - for (SignalInputConnector inputConnector : part.getSignalInputConnectors()){ + for (InputConnector inputConnector : part.getInputConnectors()){ + if (!(inputConnector instanceof SignalInputConnector)){ + continue; + } + + SignalInputConnector signalInputConnector = (SignalInputConnector) inputConnector; if (inputConnector.containsPoint(x, y)){ - return inputConnector; + return signalInputConnector; + } + } + } + + return null; + } + + @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()){ + if (!(inputConnector instanceof BooleanInputConnector)){ + continue; + } + + BooleanInputConnector booleanInputConnector = (BooleanInputConnector) inputConnector; + + if (booleanInputConnector.containsPoint(x, y)){ + return booleanInputConnector; } } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java index b06fe43..2ff3fee 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CardFile.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CardFile.java @@ -9,6 +9,7 @@ import android.util.Log; 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.samples.Placeholder; import com.codigoparallevar.minicards.types.Part; @@ -137,8 +138,10 @@ public class CardFile { public static void createDefaultCards(File cardsDir) throws IOException { // Create a button and a color box, and connect them RoundButton button = new RoundButton(new StubPartGrid(), 200, 400, 80, 100); + Toggle toggle = new Toggle(new StubPartGrid(), 350, 450, 450, 550); ColorBox box = new ColorBox(new StubPartGrid(), 500, 350, 600, 450); - button.getPressedOutputConnector().connectTo(box.getToggleInputConnector()); + button.getPressedOutputConnector().connectTo(toggle.getToggleInputConnector()); + toggle.getToggleOutputConnector().connectTo(box.getStateInputConnector()); CardFile buttonAndColorBoxCard = (new CardFile(cardsDir.getAbsolutePath()) .addPart(button) @@ -266,6 +269,13 @@ public class CardFile { return buttonInfo; } + else if (type.equals(Toggle.class.getName())){ + Tuple2> buttonInfo = Toggle.deserialize( + grid, + jsonObject.getJSONObject("_data")); + + return buttonInfo; + } else { throw new JSONException("Expected known class, found " + type); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java index c9266f7..66217ce 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java @@ -8,6 +8,7 @@ import android.util.Log; 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.types.Part; import com.codigoparallevar.minicards.types.Tuple2; @@ -20,9 +21,10 @@ class PartsHolder { private final static List> BuiltInParts = new Vector>(){{ - add(new Tuple2("Round button", RoundButton.getInstantiator())); - add(new Tuple2("Ticker", Ticker.getInstantiator())); - add(new Tuple2("Red/Green box", ColorBox.getInstantiator())); + add(new Tuple2<>("Round button", RoundButton.getInstantiator())); + add(new Tuple2<>("Ticker", Ticker.getInstantiator())); + add(new Tuple2<>("Red/Green box", ColorBox.getInstantiator())); + add(new Tuple2<>("Toggle", Toggle.getInstantiator())); }}; public PartsHolder(Context context) { diff --git a/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java b/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java index c03622a..46e650e 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java +++ b/app/src/main/java/com/codigoparallevar/minicards/ScrolledCanvas.java @@ -41,4 +41,19 @@ public class ScrolledCanvas { offsetPath.offset(-xOrig, -yOrig); canvas.drawPath(offsetPath, paint); } + + + public void drawCenteredText(String text, int x, int y, Paint paint) { + paint.setTextAlign(Paint.Align.LEFT); + Rect r = new Rect(); + + paint.getTextBounds(text, 0, text.length(), r); + x = x + (int) (r.width() / 2f - r.left); + y = y + (int) (r.height() / 2f - r.bottom); + drawText(text, x, y, paint); + } + + public void drawText(String text, int x, int y, Paint paint) { + canvas.drawText(text, x - xOrig, y - yOrig, paint); + } } \ No newline at end of file diff --git a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java index 9bbed58..f82b95b 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/StubPartGrid.java @@ -3,6 +3,7 @@ package com.codigoparallevar.minicards; import com.codigoparallevar.minicards.types.PartGrid; 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; class StubPartGrid implements PartGrid { @@ -16,6 +17,11 @@ class StubPartGrid implements PartGrid { return null; } + @Override + public BooleanInputConnector getBooleanInputConnectorOn(int x, int y) { + return null; + } + @Override public Tuple2 getCenteredOn() { return 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 e0eef50..f545071 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,16 +6,16 @@ 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.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.InputConnector; -import com.codigoparallevar.minicards.types.Moveable; -import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; -import com.codigoparallevar.minicards.types.Part; -import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.RoundInputConnector; -import com.codigoparallevar.minicards.types.RoundOutputConnector; +import com.codigoparallevar.minicards.types.wireData.Signal; +import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.utils.Serializations; import org.json.JSONArray; @@ -152,11 +152,11 @@ public class RoundButton implements Part { public void touched() { Log.d("RoundButton", "Round button touched"); - _pressedOutputConnector.sendSignal(); + _pressedOutputConnector.send(new Signal()); } @Override - public List getSignalInputConnectors() { + public List getInputConnectors() { return Collections.emptyList(); } @@ -192,7 +192,7 @@ public class RoundButton implements Part { } @Override - public void sendSignal(RoundInputConnector roundInputConnector) { + public void send(InputConnector roundInputConnector, WireDataType signal) { // @TODO: REMOVE THE NEED FOR THIS } @@ -261,7 +261,7 @@ public class RoundButton implements Part { @Override public void unlink() { - for (InputConnector input : getSignalInputConnectors()) { + for (InputConnector input : getInputConnectors()) { input.unlink(); } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundInputConnector.java new file mode 100644 index 0000000..b1d2056 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundInputConnector.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.BooleanInputConnector; +import com.codigoparallevar.minicards.types.wireData.BooleanSignal; + +import java.util.LinkedList; +import java.util.List; + +public class BooleanRoundInputConnector implements BooleanInputConnector { + private final Part _part; + private int _xposition; + private int _yposition; + private final int _radius; + private final List _attachments = new LinkedList<>(); + + public BooleanRoundInputConnector(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(BooleanSignal 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/BooleanRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java new file mode 100644 index 0000000..b1b3969 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.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.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.wireData.BooleanSignal; + +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConnector { + private PartGrid _partGrid; + private int _centerX; + private int _centerY; + private final int _radius; + private final Part _part; + private BooleanWire _currentWire = null; + private final List _wires; + private final HashSet _connections; + + public BooleanRoundOutputConnector(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 BooleanWire(this, _centerX, _centerY); + } + + @Override + public void drop(BooleanWire wire) { + + if (wire == _currentWire){ + _currentWire = null; + + BooleanInputConnector resultPoint = _partGrid.getBooleanInputConnectorOn( + 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(BooleanWire wire) { + _wires.remove(wire); + } + + @Override + public void drawWires(ScrolledCanvas canvas, boolean devMode) { + for (BooleanWire 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 (BooleanWire wire : _wires){ + wire.moveStart(x, y); + } + } + + @Override + public List> getConnectionEndpoints() { + List> endpointIds = new LinkedList<>(); + + for (BooleanWire 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(BooleanInputConnector inputConnector) { + if (_connections.contains(inputConnector)) { + return; + } + + _connections.add(inputConnector); + + BooleanWire wire = new BooleanWire(this, _centerX, _centerY); + wire.attachTo(inputConnector); + _wires.add(wire); + } + + @Override + public void draw(ScrolledCanvas canvas, boolean devMode) { + // TODO: Complete this part + } + + public void send(BooleanSignal signal) { + for (BooleanWire wire : _wires){ + wire.send(signal); + } + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundInputConnector.java similarity index 87% rename from app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java rename to app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundInputConnector.java index 8c2f9ea..14a0de3 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundInputConnector.java @@ -1,7 +1,10 @@ -package com.codigoparallevar.minicards.types; +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.SignalInputConnector; import com.codigoparallevar.minicards.types.wireData.Signal; @@ -63,8 +66,8 @@ public class RoundInputConnector implements SignalInputConnector { } @Override - public void sendSignal() { - _part.sendSignal(this); + public void send(Signal signal) { + _part.send(this, signal); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java similarity index 88% rename from app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java rename to app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java index a8c32ff..d6c19a1 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java @@ -1,11 +1,18 @@ -package com.codigoparallevar.minicards.types; +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.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.wireData.Signal; import java.util.HashSet; import java.util.LinkedList; @@ -143,9 +150,9 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { // TODO: Complete this part } - public void sendSignal() { + public void send(Signal signal) { for (SignalWire wire : _wires){ - wire.sendSignal(); + wire.send(signal); } } } 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 e620cff..8b34e01 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,16 +7,16 @@ 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.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.RoundInputConnector; -import com.codigoparallevar.minicards.types.RoundOutputConnector; import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; -import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.Signal; +import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.utils.Serializations; import org.json.JSONArray; @@ -104,7 +104,7 @@ public class Ticker implements Part { public void unlink() { pause(); - for (InputConnector input : getSignalInputConnectors()) { + for (InputConnector input : getInputConnectors()) { input.unlink(); } } @@ -184,7 +184,7 @@ public class Ticker implements Part { } @Override - public List getSignalInputConnectors() { + public List getInputConnectors() { return Collections.emptyList(); } @@ -240,7 +240,7 @@ public class Ticker implements Part { } @Override - public void sendSignal(RoundInputConnector roundInputConnector) { + public void send(InputConnector roundInputConnector, WireDataType signal) { // @TODO: REMOVE THE NEED FOR THIS } @@ -265,7 +265,7 @@ public class Ticker implements Part { @Override public void run() { while (Ticker.this._thread == Thread.currentThread()) { - Ticker.this._signalOutputConnector.sendSignal(); + Ticker.this._signalOutputConnector.send(new Signal()); _partGrid.update(); try { diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java new file mode 100644 index 0000000..a8e9a57 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java @@ -0,0 +1,344 @@ +package com.codigoparallevar.minicards.parts.logic; + +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; +import android.util.Log; + +import com.codigoparallevar.minicards.PartInstantiator; +import com.codigoparallevar.minicards.ScrolledCanvas; +import com.codigoparallevar.minicards.parts.connectors.BooleanRoundOutputConnector; +import com.codigoparallevar.minicards.parts.connectors.RoundInputConnector; +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.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.BooleanSignal; +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 Toggle implements Part { + private static final String TOGGLE_INPUT_CONNECTOR_ID = "toggle_input_connector"; + private static final int DEFAULT_SIDE_SIZE = 100; + private final String _id; + private final PartGrid _partGrid; + private final BooleanRoundOutputConnector _stateOutputConnector; + private final LinkedList _outputConnectors; + private boolean _activated; + private int _left; + private int _top; + private int _right; + private int _bottom; + private List inputConnectors; + private SignalInputConnector _toggleInputConnector; + + private Toggle(String id, PartGrid partGrid, int left, int top, int right, int bottom) { + _id = id; + _partGrid = partGrid; + _left = left; + _top = top; + _right = right; + _bottom = bottom; + _activated = false; + + // Input connector + _toggleInputConnector = new RoundInputConnector( + this, + getInputConnectorCenterX(), + getInputConnectorCenterY(), + getInputConnectRadius()); + inputConnectors = new LinkedList<>(); + inputConnectors.add(_toggleInputConnector); + + // Output connector + // Create connectors + _stateOutputConnector = new BooleanRoundOutputConnector( + this, + _partGrid, + getOutputConnectorCenterX(), getOutputConnectorCenterY(), + getOutputConnectRadius()); + + _outputConnectors = new LinkedList<>(); + _outputConnectors.add(_stateOutputConnector); + } + + public Toggle(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.GRAY); + canvas.drawRect( + new Rect(_left, _top, + _right, _bottom), + paint); + + Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + textPaint.setColor(Color.GREEN); + textPaint.setTextSize(100); + String text = "0"; + if (_activated) { + text = "1"; + } + canvas.drawCenteredText(text, + (int) ((_left + _right) / 2f - DEFAULT_SIDE_SIZE * 0.4f), + (_top + _bottom) / 2, + 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()); + + _stateOutputConnector.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 SignalInputConnector getToggleInputConnector() { + return _toggleInputConnector; + } + + public OutputConnector getToggleOutputConnector() { + return _stateOutputConnector; + } + + + @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_state_output_connector", + Serializations.serialize(serializeConnectionEndpoints())); + + return serialized; + } + + private List> serializeConnectionEndpoints() { + List> serializedData = new LinkedList<>(); + + for (Tuple2 endpoint : _stateOutputConnector.getConnectionEndpoints()){ + serializedData.add(PartConnection.serialize(endpoint.item1, endpoint.item2)); + } + + return serializedData; + } + + @Override + public void send(InputConnector roundInputConnector, WireDataType signal) { + _activated = !_activated; + _stateOutputConnector.send(new BooleanSignal(_activated)); + } + + @Override + public String get_id() { + return _id; + } + + @Override + public InputConnector getConnectorWithId(String inputConnectorId) { + switch (inputConnectorId){ + case Toggle.TOGGLE_INPUT_CONNECTOR_ID: + return _toggleInputConnector; + + default: + return null; + } + } + + @Override + public String getConnectorId(InputConnector inputConnector) { + if (inputConnector == _toggleInputConnector){ + return Toggle.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"); + + Toggle toggle = new Toggle(id, partGrid, left, top, right, bottom); + + List connections = new LinkedList<>(); + + JSONArray connectorOuts = data.getJSONArray("on_state_output_connector"); + for (int i = 0; i < connectorOuts.length(); i++) { + connections.add(PartConnection.deserialize( + toggle._stateOutputConnector, + 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 Toggle(grid, + center.item1 - halfSideSize, center.item2 - halfSideSize, + center.item1 + halfSideSize, center.item2 + halfSideSize); + } + }; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java index dec65d3..6c52a5c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java @@ -7,14 +7,16 @@ import android.util.Log; import com.codigoparallevar.minicards.PartInstantiator; import com.codigoparallevar.minicards.ScrolledCanvas; +import com.codigoparallevar.minicards.parts.connectors.BooleanRoundInputConnector; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.RoundInputConnector; import com.codigoparallevar.minicards.types.Tuple2; +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.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.BooleanSignal; +import com.codigoparallevar.minicards.types.wireData.WireDataType; import org.json.JSONException; import org.json.JSONObject; @@ -34,8 +36,8 @@ public class ColorBox implements Part { private int _top; private int _right; private int _bottom; - private List inputConnectors; - private SignalInputConnector _toggleInputConnector; + private List inputConnectors; + private BooleanInputConnector _toggleInputConnector; private ColorBox(String id, PartGrid partGrid, int left, int top, int right, int bottom) { _id = id; @@ -46,7 +48,7 @@ public class ColorBox implements Part { _bottom = bottom; _activated = false; - _toggleInputConnector = new RoundInputConnector( + _toggleInputConnector = new BooleanRoundInputConnector( this, getInputConnectorCenterX(), getInputConnectorCenterY(), @@ -139,7 +141,7 @@ public class ColorBox implements Part { } @Override - public List getSignalInputConnectors() { + public List getInputConnectors() { return inputConnectors; } @@ -148,7 +150,7 @@ public class ColorBox implements Part { return Collections.emptyList(); } - public SignalInputConnector getToggleInputConnector() { + public BooleanInputConnector getStateInputConnector() { return _toggleInputConnector; } @@ -166,8 +168,10 @@ public class ColorBox implements Part { } @Override - public void sendSignal(RoundInputConnector roundInputConnector) { - _activated = !_activated; + public void send(InputConnector roundInputConnector, WireDataType signal) { + if (signal instanceof BooleanSignal) { + _activated = ((BooleanSignal) signal).value; + } } @Override @@ -228,7 +232,7 @@ public class ColorBox implements Part { @Override public void unlink() { - for (InputConnector input : getSignalInputConnectors()) { + for (InputConnector input : getInputConnectors()) { input.unlink(); } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java index 72ae38d..d496e96 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java @@ -5,13 +5,12 @@ import android.graphics.Paint; import android.util.Log; import com.codigoparallevar.minicards.ScrolledCanvas; -import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.Moveable; -import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; -import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; -import com.codigoparallevar.minicards.types.RoundInputConnector; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.WireDataType; import org.json.JSONException; import org.json.JSONObject; @@ -103,7 +102,7 @@ public class Placeholder implements Part { } @Override - public List getSignalInputConnectors() { + public List getInputConnectors() { return Collections.emptyList(); } @@ -126,7 +125,7 @@ public class Placeholder implements Part { } @Override - public void sendSignal(RoundInputConnector roundInputConnector) { + public void send(InputConnector roundInputConnector, WireDataType signal) { // @TODO: REMOVE THE NEED FOR THIS } @@ -178,7 +177,7 @@ public class Placeholder implements Part { @Override public void unlink() { - for (InputConnector input : getSignalInputConnectors()) { + for (InputConnector input : getInputConnectors()) { input.unlink(); } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java b/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java index be9506d..7aea9ee 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java @@ -1,5 +1,7 @@ package com.codigoparallevar.minicards.types; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; + public interface Dropper { void drop(T wire); void unlinkWire(T wire); 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 d16d459..640acab 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java @@ -1,8 +1,8 @@ package com.codigoparallevar.minicards.types; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; -import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; +import com.codigoparallevar.minicards.types.wireData.WireDataType; import org.json.JSONException; import org.json.JSONObject; @@ -17,12 +17,12 @@ public interface Part extends Selectable, Moveable, Drawable { void touched(); - List getSignalInputConnectors(); + List getInputConnectors(); List getOutputConnectors(); JSONObject serialize() throws JSONException; - void sendSignal(RoundInputConnector roundInputConnector); + void send(InputConnector roundInputConnector, 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 92517f5..71d93ca 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java @@ -1,11 +1,13 @@ package com.codigoparallevar.minicards.types; +import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; public interface PartGrid { Selectable getPartOn(int x, int y); SignalInputConnector getSignalInputConnectorOn(int x, int y); + BooleanInputConnector getBooleanInputConnectorOn(int x, int y); Tuple2 getCenteredOn(); diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/BooleanWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/BooleanWire.java new file mode 100644 index 0000000..9275c73 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/BooleanWire.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.BooleanInputConnector; +import com.codigoparallevar.minicards.types.wireData.BooleanSignal; + +public class BooleanWire extends Wire implements Moveable, Drawable { + public BooleanWire(Dropper dropper, int xInit, int yInit) { + super(dropper, xInit, yInit); + } + + public void send(BooleanSignal signal) { + if (_attachedTo != null) { + _attachedTo.send(signal); + } + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/SignalWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/SignalWire.java similarity index 57% rename from app/src/main/java/com/codigoparallevar/minicards/types/SignalWire.java rename to app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/SignalWire.java index 17eb660..61e767c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/SignalWire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/SignalWire.java @@ -1,5 +1,8 @@ -package com.codigoparallevar.minicards.types; +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.SignalInputConnector; import com.codigoparallevar.minicards.types.wireData.Signal; @@ -8,9 +11,9 @@ public class SignalWire extends Wire implements Mo super(dropper, xInit, yInit); } - public void sendSignal() { + public void send(Signal signal) { if (_attachedTo != null) { - _attachedTo.sendSignal(); + _attachedTo.send(signal); } } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java similarity index 90% rename from app/src/main/java/com/codigoparallevar/minicards/types/Wire.java rename to app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java index dfb8dd8..10cf453 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java @@ -1,10 +1,13 @@ -package com.codigoparallevar.minicards.types; +package com.codigoparallevar.minicards.types.connectors.Wiring; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import com.codigoparallevar.minicards.ScrolledCanvas; +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.InputConnector; import com.codigoparallevar.minicards.types.wireData.WireDataType; diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/BooleanInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/BooleanInputConnector.java new file mode 100644 index 0000000..a5d8375 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/BooleanInputConnector.java @@ -0,0 +1,6 @@ +package com.codigoparallevar.minicards.types.connectors.input; + +import com.codigoparallevar.minicards.types.wireData.BooleanSignal; + +public interface BooleanInputConnector extends InputConnector { +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/InputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/InputConnector.java index 7431fe0..125a994 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/InputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/InputConnector.java @@ -3,7 +3,7 @@ package com.codigoparallevar.minicards.types.connectors.input; import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Selectable; -import com.codigoparallevar.minicards.types.Wire; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; import com.codigoparallevar.minicards.types.wireData.WireDataType; public interface InputConnector> @@ -19,4 +19,6 @@ public interface InputConnector { - void sendSignal(); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/BooleanOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/BooleanOutputConnector.java new file mode 100644 index 0000000..6fd8f86 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/BooleanOutputConnector.java @@ -0,0 +1,11 @@ +package com.codigoparallevar.minicards.types.connectors.output; + +import com.codigoparallevar.minicards.types.connectors.Wiring.BooleanWire; +import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; +import com.codigoparallevar.minicards.types.wireData.BooleanSignal; + +public interface BooleanOutputConnector extends OutputConnector< + BooleanSignal, + BooleanInputConnector, + BooleanWire> { +} 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 b74b425..4970793 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 @@ -4,7 +4,7 @@ import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.Tuple2; -import com.codigoparallevar.minicards.types.Wire; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.wireData.WireDataType; @@ -21,4 +21,6 @@ public interface OutputConnector> getConnectionEndpoints(); void connectTo(T1 inputConnector); + + void send(T data); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/SignalOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/SignalOutputConnector.java index 128a917..24fa323 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/SignalOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/SignalOutputConnector.java @@ -1,9 +1,8 @@ package com.codigoparallevar.minicards.types.connectors.output; -import com.codigoparallevar.minicards.types.SignalWire; +import com.codigoparallevar.minicards.types.connectors.Wiring.SignalWire; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.wireData.Signal; public interface SignalOutputConnector extends OutputConnector { - void sendSignal(); } 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 new file mode 100644 index 0000000..7a530e4 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/BooleanSignal.java @@ -0,0 +1,9 @@ +package com.codigoparallevar.minicards.types.wireData; + +public class BooleanSignal implements WireDataType { + public final boolean value; + + public BooleanSignal(boolean value) { + this.value = value; + } +}