From 888e32f7ecc0a96d1c05c375c39bd7a6346932bc Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Thu, 13 Jul 2017 23:33:19 -0400 Subject: [PATCH 1/8] Add a simplistic prototype of wiring mechanism. --- .../minicards/CanvasView.java | 50 ++++++++--- .../minicards/MainActivity.java | 6 +- .../minicards/parts/buttons/RoundButton.java | 88 ++++++++++++++++++- .../parts/{ => samples}/Placeholder.java | 36 +++++++- .../minicards/types/Drawable.java | 7 ++ .../minicards/types/InputConnector.java | 5 ++ .../minicards/types/Moveable.java | 7 ++ .../minicards/types/OutputConnector.java | 9 ++ .../minicards/{parts => types}/Part.java | 13 +-- .../minicards/{parts => }/types/Position.java | 2 +- .../minicards/types/RoundOutputConnector.java | 79 +++++++++++++++++ .../minicards/types/Selectable.java | 6 ++ .../minicards/types/Wire.java | 55 ++++++++++++ 13 files changed, 339 insertions(+), 24 deletions(-) rename app/src/main/java/com/codigoparallevar/minicards/parts/{ => samples}/Placeholder.java (73%) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/Drawable.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java rename app/src/main/java/com/codigoparallevar/minicards/{parts => types}/Part.java (52%) rename app/src/main/java/com/codigoparallevar/minicards/{parts => }/types/Position.java (82%) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/Wire.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index d9a6a1b..8555d6d 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -1,6 +1,5 @@ package com.codigoparallevar.minicards; -import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; @@ -13,11 +12,13 @@ import android.view.MotionEvent; import android.view.View; import com.codigoparallevar.minicards.motion.MotionMode; -import com.codigoparallevar.minicards.parts.Part; -import com.codigoparallevar.minicards.parts.Placeholder; import com.codigoparallevar.minicards.parts.buttons.RoundButton; -import com.codigoparallevar.minicards.parts.types.Position; -import com.getbase.floatingactionbutton.FloatingActionsMenu; +import com.codigoparallevar.minicards.parts.samples.Placeholder; +import com.codigoparallevar.minicards.types.InputConnector; +import com.codigoparallevar.minicards.types.OutputConnector; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.Position; +import com.codigoparallevar.minicards.types.Selectable; import org.json.JSONArray; import org.json.JSONException; @@ -35,7 +36,7 @@ class CanvasView extends View { ArrayList parts = new ArrayList<>(); @Nullable - Part selectedPart; + Selectable selectedPart; @Nullable final Position lastTouchedPosition = new Position(); @@ -180,7 +181,9 @@ class CanvasView extends View { } if (motionMode != MotionMode.Type.LongTouch) { if (selectedPart != null){ - selectedPart.touched(); + if (selectedPart instanceof Part){ + ((Part) selectedPart).touched(); + } } } else if (motionMode == MotionMode.Type.LongTouch) { @@ -189,6 +192,9 @@ class CanvasView extends View { Log.d("Canvas", "Deleting element" + selectedPart); parts.remove(selectedPart); } + else { + selectedPart.getMoveable().drop(x, y); + } } } @@ -212,14 +218,15 @@ class CanvasView extends View { Log.i("Canvas", "X: " + x + " Y: " + y + " in drop zone " + _dropZone + " : " + inDropZone(x, y)); if (motionMode == null){ - final Part nowSelectedPart = getPartOn(x, y); + final Selectable nowSelectedPart = getPartOn(x, y); final boolean canWait = selectedPart == nowSelectedPart; motionMode = getMotionMode(canWait); } if (motionMode == MotionMode.Type.LongTouch){ if (selectedPart != null){ _isDragging = true; - selectedPart.move(x, y); + selectedPart.getMoveable().move(x, y); + invalidate(); } } } @@ -288,15 +295,32 @@ class CanvasView extends View { } @Nullable - private Part getPartOn(int x, int y) { - // Look in the list, in reverse so top-most elements are checked before + private Selectable getPartOn(int x, int y) { + // Look in the list of parts, in reverse so top-most elements are checked before for (int i = parts.size() - 1; i >= 0; i--){ final Part part = parts.get(i); - if ((x >= part.getLeft()) && (part.getRight() >= x) - && (y >= part.getTop()) && (part.getBottom() >= y)){ + if (part.containsPoint(x, y)){ return part; } } + + // 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); + // First try with output connectors + for (OutputConnector outputConnector : part.getOutputConnectors()){ + if (outputConnector.containsPoint(x, y)){ + return outputConnector; + } + } + // Then with input ones + for (InputConnector inputConnector : part.getInputConnectors()){ + if (inputConnector.containsPoint(x, y)){ + return inputConnector; + } + } + } + return null; } diff --git a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java index eed0677..d0bfd46 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java @@ -10,6 +10,7 @@ import android.view.MotionEvent; import android.view.View; import com.codigoparallevar.minicards.parts.buttons.RoundButton; +import com.codigoparallevar.minicards.types.Part; public class MainActivity extends AppCompatActivity { @@ -58,7 +59,10 @@ public class MainActivity extends AppCompatActivity { @Override public void onClick(View v) { if (canvasView != null) { - canvasView.addPart(new RoundButton(500, 500, 50, 100)); + Part part = new RoundButton( + 500, 1200, + 80, 100); + canvasView.addPart(part); } } }); 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 6c110f2..4742ffc 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 @@ -3,27 +3,48 @@ package com.codigoparallevar.minicards.parts.buttons; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.Path; import android.util.Log; -import com.codigoparallevar.minicards.parts.Part; +import com.codigoparallevar.minicards.types.InputConnector; +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.OutputConnector; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.RoundOutputConnector; import org.json.JSONException; import org.json.JSONObject; -public class RoundButton implements Part { +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +public class RoundButton implements Part { private int _xCenter; private int _yCenter; private final int _innerRadius; private final int _outerRadius; private final int _outerRadiusThickness = 10; + private final int _pathRunWay = 200; + private List _outputConnectors; + private final RoundOutputConnector pressedOuputConnector; public RoundButton(int xCenter, int yCenter, int innerRadius, int outerRadius) { _xCenter = xCenter; _yCenter = yCenter; _innerRadius = innerRadius; _outerRadius = outerRadius; + + // Create connectors + pressedOuputConnector = new RoundOutputConnector( + this, + getOutputConnectorCenterX(), getOutputConnectorCenterY(), + getOutputConnectRadius()); + + _outputConnectors = new LinkedList<>(); + _outputConnectors.add(pressedOuputConnector); + } @Override @@ -54,15 +75,57 @@ public class RoundButton implements Part { Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(Color.BLACK); + if (devMode){ + drawConnector(canvas); + drawWires(canvas, devMode); + } + canvas.drawCircle(_xCenter, _yCenter, _outerRadius, foregroundPaint); canvas.drawCircle(_xCenter, _yCenter, _outerRadius - _outerRadiusThickness, backgroundPaint); canvas.drawCircle(_xCenter, _yCenter, _innerRadius, foregroundPaint); } + private void drawConnector(Canvas canvas) { + Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + connectorPaint.setColor(Color.RED); + + canvas.drawCircle(getOutputConnectorCenterX(), getOutputConnectorCenterY(), + getOutputConnectRadius(), + connectorPaint); + } + + private int getOutputConnectorCenterX() { + return _xCenter + _outerRadius; + } + + private int getOutputConnectorCenterY() { + return _yCenter; + } + + private int getOutputConnectRadius() { + return _innerRadius / 2; + } + + private void drawWires(Canvas canvas, boolean devMode) { + for (OutputConnector outputConnector : _outputConnectors){ + outputConnector.drawWires(canvas, devMode); + } + } + @Override public void move(int x, int y) { _xCenter = x; _yCenter = y; + + // Move connectors too + pressedOuputConnector.updatePosition( + getOutputConnectorCenterX(), + getOutputConnectorCenterY()); + } + + @Override + public void drop(int x, int y) { + move(x, y); } @Override @@ -70,6 +133,16 @@ public class RoundButton implements Part { Log.d("RoundButton", "Round button touched"); } + @Override + public List getInputConnectors() { + return Collections.emptyList(); + } + + @Override + public List getOutputConnectors() { + return _outputConnectors; + } + @Override public JSONObject serialize() throws JSONException { JSONObject serialized = new JSONObject(); @@ -92,4 +165,15 @@ public class RoundButton implements Part { return new RoundButton(xCenter, yCenter, innerRadius, outerRadius); } + + @Override + public boolean containsPoint(int x, int y) { + return ((Math.abs(x - _xCenter) <= _outerRadius) + && (Math.abs(y - _yCenter) <= _outerRadius)); + } + + @Override + public Moveable getMoveable() { + return this; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java similarity index 73% rename from app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java rename to app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java index faa4134..eaf09e7 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java @@ -1,13 +1,21 @@ -package com.codigoparallevar.minicards.parts; +package com.codigoparallevar.minicards.parts.samples; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; +import com.codigoparallevar.minicards.types.InputConnector; +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.OutputConnector; +import com.codigoparallevar.minicards.types.Part; + import org.json.JSONException; import org.json.JSONObject; +import java.util.Collections; +import java.util.List; + public class Placeholder implements Part { private int _left; private int _top; @@ -72,11 +80,26 @@ public class Placeholder implements Part { _bottom = _top + height; } + @Override + public void drop(int x, int y) { + move(x, y); + } + @Override public void touched() { Log.d("Placeholder", "Placeholder touched"); } + @Override + public List getInputConnectors() { + return Collections.emptyList(); + } + + @Override + public List getOutputConnectors() { + return Collections.emptyList(); + } + @Override public JSONObject serialize() throws JSONException { JSONObject serialized = new JSONObject(); @@ -97,4 +120,15 @@ public class Placeholder implements Part { return new Placeholder(left, top, right, bottom); } + + @Override + public boolean containsPoint(int x, int y) { + return (x >= getLeft()) && (x <= getRight()) + && (y >= getTop()) && (y <= getBottom()); + } + + @Override + public Moveable getMoveable() { + return this; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Drawable.java b/app/src/main/java/com/codigoparallevar/minicards/types/Drawable.java new file mode 100644 index 0000000..2557c85 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Drawable.java @@ -0,0 +1,7 @@ +package com.codigoparallevar.minicards.types; + +import android.graphics.Canvas; + +public interface Drawable { + void draw(Canvas canvas, boolean devMode); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java new file mode 100644 index 0000000..d10e2d7 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java @@ -0,0 +1,5 @@ +package com.codigoparallevar.minicards.types; + +public interface InputConnector extends Selectable { + void updatePosition(int x, int y); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java b/app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java new file mode 100644 index 0000000..68df24e --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java @@ -0,0 +1,7 @@ +package com.codigoparallevar.minicards.types; + +public interface Moveable { + void move(int x, int y); + + void drop(int x, int y); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java new file mode 100644 index 0000000..bd3e1cf --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java @@ -0,0 +1,9 @@ +package com.codigoparallevar.minicards.types; + +import android.graphics.Canvas; + +public interface OutputConnector extends Selectable { + void drop(Wire wire); + void drawWires(Canvas canvas, boolean devMode); + void updatePosition(int x, int y); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java similarity index 52% rename from app/src/main/java/com/codigoparallevar/minicards/parts/Part.java rename to app/src/main/java/com/codigoparallevar/minicards/types/Part.java index cd3d79f..0c0fd46 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java @@ -1,21 +1,22 @@ -package com.codigoparallevar.minicards.parts; +package com.codigoparallevar.minicards.types; import android.graphics.Canvas; import org.json.JSONException; import org.json.JSONObject; -public interface Part { +import java.util.List; + +public interface Part extends Selectable, Moveable, Drawable { int getLeft(); int getRight(); int getTop(); int getBottom(); - void draw(Canvas canvas, boolean devMode); - - void move(int x, int y); - void touched(); + List getInputConnectors(); + List getOutputConnectors(); + JSONObject serialize() throws JSONException; } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/types/Position.java b/app/src/main/java/com/codigoparallevar/minicards/types/Position.java similarity index 82% rename from app/src/main/java/com/codigoparallevar/minicards/parts/types/Position.java rename to app/src/main/java/com/codigoparallevar/minicards/types/Position.java index 95bfee2..be985e1 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/types/Position.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Position.java @@ -1,4 +1,4 @@ -package com.codigoparallevar.minicards.parts.types; +package com.codigoparallevar.minicards.types; public class Position { private int _x; diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java new file mode 100644 index 0000000..4c66ba6 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java @@ -0,0 +1,79 @@ +package com.codigoparallevar.minicards.types; + +import android.graphics.Canvas; +import android.util.Log; + +import java.util.LinkedList; +import java.util.List; + +public class RoundOutputConnector implements OutputConnector, Drawable { + private int _centerX; + private int _centerY; + private final int _radius; + private final Part _part; + private Wire _currentWire = null; + private final List _wires; + + public RoundOutputConnector(Part part, int centerX, int centerY, int radius) { + _part = part; + _centerX = centerX; + _centerY = centerY; + _radius = radius; + _wires = new LinkedList<>(); + } + + @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; + } + + private void startWire() { + _currentWire = new Wire(this, _centerX, _centerY); + } + + @Override + public void drop(Wire wire) { + if (wire == _currentWire){ + _currentWire = null; + _wires.add(wire); + } + else { + Log.w("RoundOutputConnector", + "Asked to drop non matching wire " + + "(expected " + _currentWire + ", got " + wire + ")"); + } + } + + @Override + public void drawWires(Canvas canvas, boolean devMode) { + for (Wire 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; + } + + @Override + public void draw(Canvas canvas, boolean devMode) { + + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java b/app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java new file mode 100644 index 0000000..4d79ddd --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java @@ -0,0 +1,6 @@ +package com.codigoparallevar.minicards.types; + +public interface Selectable { + boolean containsPoint(int x, int y); + Moveable getMoveable(); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java new file mode 100644 index 0000000..c69692b --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java @@ -0,0 +1,55 @@ +package com.codigoparallevar.minicards.types; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; + +public class Wire implements Moveable, Drawable { + private final OutputConnector _dropper; + private final int _xinit; + private final int _yinit; + private int _xend; + private int _yend; + private final static int _pathRunWay = 100; + + public Wire(OutputConnector dropper, int xInit, int yInit) { + super(); + _dropper = dropper; + _xinit = xInit; + _yinit = yInit; + + _xend = xInit; + _yend = yInit; + } + + @Override + public void move(int x, int y) { + _xend = x; + _yend = y; + } + + @Override + public void drop(int x, int y) { + _dropper.drop(this); + } + + @Override + public void draw(Canvas canvas, boolean devMode) { + final Path samplePath = new Path(); + + samplePath.moveTo(_xinit, _yinit); + samplePath.cubicTo( + _xinit + _pathRunWay, _yinit, + _xend - _pathRunWay, _yend, + _xend, _yend); + + Paint pathPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + pathPaint.setColor(Color.GREEN); + pathPaint.setStrokeWidth(5.0f); + pathPaint.setStrokeCap(Paint.Cap.ROUND); + pathPaint.setStyle(Paint.Style.STROKE); + + canvas.drawPath(samplePath, pathPaint); + } +} From dcbc0e5cad209f8eb9db491e174490b50959d8f1 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Sat, 22 Jul 2017 16:31:21 -0400 Subject: [PATCH 2/8] Add signal passing prototype. --- .../minicards/CanvasView.java | 48 +++-- .../minicards/MainActivity.java | 12 +- .../minicards/parts/buttons/RoundButton.java | 33 ++-- .../minicards/parts/samples/ColorBox.java | 186 ++++++++++++++++++ .../minicards/parts/samples/Placeholder.java | 19 +- .../minicards/types/Dropper.java | 5 + .../minicards/types/InputConnector.java | 9 +- .../minicards/types/Moveable.java | 2 +- .../minicards/types/OutputConnector.java | 3 +- .../minicards/types/Part.java | 2 + .../minicards/types/PartGrid.java | 7 + .../minicards/types/RoundInputConnector.java | 69 +++++++ .../minicards/types/RoundOutputConnector.java | 24 ++- .../minicards/types/Wire.java | 37 +++- 14 files changed, 415 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 8555d6d..c6f36f9 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -13,10 +13,12 @@ import android.view.View; import com.codigoparallevar.minicards.motion.MotionMode; import com.codigoparallevar.minicards.parts.buttons.RoundButton; +import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.Placeholder; import com.codigoparallevar.minicards.types.InputConnector; import com.codigoparallevar.minicards.types.OutputConnector; import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.Position; import com.codigoparallevar.minicards.types.Selectable; @@ -30,7 +32,7 @@ import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; -class CanvasView extends View { +class CanvasView extends View implements PartGrid { @NonNull ArrayList parts = new ArrayList<>(); @@ -52,7 +54,7 @@ class CanvasView extends View { private final static float touchTimeForLongTouchInMillis = 500; private boolean _isDragging = false; private MainActivity parentActivity = null; - private Tuple _dropZone = new Tuple<>(0, 0, 0, 0); + private Tuple _dropToRemoveZone = new Tuple<>(0, 0, 0, 0); private boolean _devMode = false; public CanvasView(Context context) { @@ -69,8 +71,9 @@ class CanvasView extends View { this.setBackgroundColor(Color.rgb(4, 69, 99)); if (!loadState()){ - parts.add(new Placeholder(50, 50, 750, 500)); - parts.add(new RoundButton(500, 1200, 80, 100)); + parts.add(new Placeholder(this, 50, 50, 750, 500)); + parts.add(new ColorBox(this, 250, 250, 100, 100)); + parts.add(new RoundButton(this, 500, 1200, 80, 100)); } } @@ -108,10 +111,13 @@ class CanvasView extends View { private void deserializeObject(JSONObject jsonObject) throws JSONException { String type = jsonObject.getString("_type"); if(type.equals(RoundButton.class.getName())) { - parts.add(RoundButton.deserialize(jsonObject.getJSONObject("_data"))); + parts.add(RoundButton.deserialize(this, jsonObject.getJSONObject("_data"))); } else if (type.equals(Placeholder.class.getName())) { - parts.add(Placeholder.deserialize(jsonObject.getJSONObject("_data"))); + parts.add(Placeholder.deserialize(this, jsonObject.getJSONObject("_data"))); + } + else if (type.equals(ColorBox.class.getName())){ + parts.add(ColorBox.deserialize(this, jsonObject.getJSONObject("_data"))); } else { throw new JSONException("Expected known class, found " + type); @@ -216,7 +222,7 @@ class CanvasView extends View { } Log.i("Canvas", "X: " + x + " Y: " + y - + " in drop zone " + _dropZone + " : " + inDropZone(x, y)); + + " in drop zone " + _dropToRemoveZone + " : " + inDropZone(x, y)); if (motionMode == null){ final Selectable nowSelectedPart = getPartOn(x, y); final boolean canWait = selectedPart == nowSelectedPart; @@ -225,7 +231,7 @@ class CanvasView extends View { if (motionMode == MotionMode.Type.LongTouch){ if (selectedPart != null){ _isDragging = true; - selectedPart.getMoveable().move(x, y); + selectedPart.getMoveable().moveEnd(x, y); invalidate(); } } @@ -246,8 +252,8 @@ class CanvasView extends View { } private boolean inDropZone(int x, int y) { - return (x >= _dropZone._x1) && (x <= _dropZone._x2) - && (y >= _dropZone._y1) && (y <= _dropZone._y2); + return (x >= _dropToRemoveZone._x1) && (x <= _dropToRemoveZone._x2) + && (y >= _dropToRemoveZone._y1) && (y <= _dropToRemoveZone._y2); } private void saveState() throws IOException { @@ -295,7 +301,7 @@ class CanvasView extends View { } @Nullable - private Selectable getPartOn(int x, int y) { + public Selectable getPartOn(int x, int y) { // Look in the list of parts, in reverse so top-most elements are checked before for (int i = parts.size() - 1; i >= 0; i--){ final Part part = parts.get(i); @@ -324,6 +330,24 @@ class CanvasView extends View { return null; } + @Override + @Nullable + public InputConnector getInputConnectorOn(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.containsPoint(x, y)){ + return inputConnector; + } + } + } + + return null; + } + public void addPart(Part part) { parts.add(part); invalidate(); @@ -338,7 +362,7 @@ class CanvasView extends View { } public void setDropZone(float x1, float x2, float y1, float y2) { - _dropZone = new Tuple<>((int) x1, (int) x2, (int) y1, (int) y2); + _dropToRemoveZone = new Tuple<>((int) x1, (int) x2, (int) y1, (int) y2); } public void setDevMode(boolean devMode) { diff --git a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java index d0bfd46..682ad13 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java @@ -1,7 +1,6 @@ package com.codigoparallevar.minicards; import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; @@ -10,6 +9,7 @@ import android.view.MotionEvent; import android.view.View; import com.codigoparallevar.minicards.parts.buttons.RoundButton; +import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.types.Part; public class MainActivity extends AppCompatActivity { @@ -59,10 +59,16 @@ public class MainActivity extends AppCompatActivity { @Override public void onClick(View v) { if (canvasView != null) { - Part part = new RoundButton( + Part buttonPart = new RoundButton( + canvasView, 500, 1200, 80, 100); - canvasView.addPart(part); + canvasView.addPart(buttonPart); + + Part boxPart = new ColorBox(canvasView, + 400, 1100, + 500, 1200); + canvasView.addPart(boxPart); } } }); 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 4742ffc..d5f3807 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 @@ -3,13 +3,14 @@ package com.codigoparallevar.minicards.parts.buttons; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; -import android.graphics.Path; import android.util.Log; import com.codigoparallevar.minicards.types.InputConnector; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.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 org.json.JSONException; @@ -21,6 +22,7 @@ import java.util.List; public class RoundButton implements Part { + private final PartGrid _partGrid; private int _xCenter; private int _yCenter; private final int _innerRadius; @@ -28,22 +30,24 @@ public class RoundButton implements Part { private final int _outerRadiusThickness = 10; private final int _pathRunWay = 200; private List _outputConnectors; - private final RoundOutputConnector pressedOuputConnector; + private final RoundOutputConnector _pressedOuputConnector; - public RoundButton(int xCenter, int yCenter, int innerRadius, int outerRadius) { + public RoundButton(PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) { + _partGrid = partGrid; _xCenter = xCenter; _yCenter = yCenter; _innerRadius = innerRadius; _outerRadius = outerRadius; // Create connectors - pressedOuputConnector = new RoundOutputConnector( + _pressedOuputConnector = new RoundOutputConnector( this, + _partGrid, getOutputConnectorCenterX(), getOutputConnectorCenterY(), getOutputConnectRadius()); _outputConnectors = new LinkedList<>(); - _outputConnectors.add(pressedOuputConnector); + _outputConnectors.add(_pressedOuputConnector); } @@ -103,7 +107,7 @@ public class RoundButton implements Part { } private int getOutputConnectRadius() { - return _innerRadius / 2; + return _innerRadius; } private void drawWires(Canvas canvas, boolean devMode) { @@ -113,24 +117,26 @@ public class RoundButton implements Part { } @Override - public void move(int x, int y) { + public void moveEnd(int x, int y) { _xCenter = x; _yCenter = y; // Move connectors too - pressedOuputConnector.updatePosition( + _pressedOuputConnector.updatePosition( getOutputConnectorCenterX(), getOutputConnectorCenterY()); } @Override public void drop(int x, int y) { - move(x, y); + moveEnd(x, y); } @Override public void touched() { Log.d("RoundButton", "Round button touched"); + + _pressedOuputConnector.sendSignal(); } @Override @@ -156,14 +162,19 @@ public class RoundButton implements Part { return serialized; } - public static Part deserialize(JSONObject data) throws JSONException { + @Override + public void sendSignal(RoundInputConnector roundInputConnector) { + // @TODO: REMOVE THE NEED FOR THIS + } + + public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { int xCenter = data.getInt("x_center"); int yCenter = data.getInt("y_center"); int innerRadius = data.getInt("inner_radius"); int outerRadius = data.getInt("outer_radius"); - return new RoundButton(xCenter, yCenter, innerRadius, outerRadius); + return new RoundButton(partGrid, xCenter, yCenter, innerRadius, outerRadius); } @Override 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 new file mode 100644 index 0000000..bb28e57 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java @@ -0,0 +1,186 @@ +package com.codigoparallevar.minicards.parts.samples; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; +import android.util.Log; + +import com.codigoparallevar.minicards.types.InputConnector; +import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.OutputConnector; +import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.PartGrid; +import com.codigoparallevar.minicards.types.RoundInputConnector; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class ColorBox implements Part { + private final PartGrid _partGrid; + private boolean _activated; + private int _left; + private int _top; + private int _right; + private int _bottom; + private List inputConnectors; + private InputConnector _toggleInputConnector; + + public ColorBox(PartGrid partGrid, int left, int top, int right, int bottom) { + _partGrid = partGrid; + _left = left; + _top = top; + _right = right; + _bottom = bottom; + _activated = false; + + _toggleInputConnector = new RoundInputConnector( + this, + getInputConnectorCenterX(), + getInputConnectorCenterY(), + getInputConnectRadius()); + inputConnectors = new LinkedList<>(); + inputConnectors.add(_toggleInputConnector); + } + + @Override + public int getLeft() { + return _left; + } + + @Override + public int getRight() { + return _right; + } + + @Override + public int getTop() { + return _top; + } + + @Override + public int getBottom() { + return _bottom; + } + + @Override + public void draw(Canvas canvas, boolean devMode) { + Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); + if (_activated){ + paint.setColor(Color.GREEN); + } + else { + paint.setColor(Color.RED); + } + + if (devMode){ + drawConnector(canvas); + // drawWires(canvas, devMode); + } + + + canvas.drawRect( + new Rect(_left, _top, + _right, _bottom), + paint); + } + + private void drawConnector(Canvas canvas) { + Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + connectorPaint.setColor(Color.YELLOW); + + canvas.drawCircle( + getInputConnectorCenterX(), getInputConnectorCenterY(), + getInputConnectRadius(), + connectorPaint); + } + + @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()); + } + + @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 Collections.emptyList(); + } + + @Override + public JSONObject serialize() throws JSONException { + JSONObject serialized = new JSONObject(); + + serialized.put("left", _left); + serialized.put("top", _top); + serialized.put("right", _right); + serialized.put("bottom", _bottom); + + return serialized; + } + + @Override + public void sendSignal(RoundInputConnector roundInputConnector) { + _activated = !_activated; + } + + public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { + int left = data.getInt("left"); + int top = data.getInt("top"); + int right = data.getInt("right"); + int bottom = data.getInt("bottom"); + + return new ColorBox(partGrid, left, top, right, bottom); + } + + @Override + public boolean containsPoint(int x, int y) { + return (x >= getLeft()) && (x <= getRight()) + && (y >= getTop()) && (y <= getBottom()); + } + + @Override + public Moveable getMoveable() { + return this; + } + + public int getInputConnectorCenterX() { + return getLeft(); + } + + private int getInputConnectRadius() { + return (getRight() - getLeft()) / 2; + } + + public int getInputConnectorCenterY() { + return (getTop() + getBottom()) / 2; + } +} 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 eaf09e7..2631ffd 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 @@ -9,6 +9,8 @@ import com.codigoparallevar.minicards.types.InputConnector; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.OutputConnector; import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.types.PartGrid; +import com.codigoparallevar.minicards.types.RoundInputConnector; import org.json.JSONException; import org.json.JSONObject; @@ -17,12 +19,14 @@ import java.util.Collections; import java.util.List; public class Placeholder implements Part { + private final PartGrid _partGrid; private int _left; private int _top; private int _right; private int _bottom; - public Placeholder(int left, int top, int right, int bottom) { + public Placeholder(PartGrid partGrid, int left, int top, int right, int bottom) { + _partGrid = partGrid; _left = left; _top = top; _right = right; @@ -69,7 +73,7 @@ public class Placeholder implements Part { } @Override - public void move(int x, int y) { + public void moveEnd(int x, int y) { final int width = _right - _left; final int height = _bottom - _top; @@ -82,7 +86,7 @@ public class Placeholder implements Part { @Override public void drop(int x, int y) { - move(x, y); + moveEnd(x, y); } @Override @@ -112,13 +116,18 @@ public class Placeholder implements Part { return serialized; } - public static Part deserialize(JSONObject data) throws JSONException { + @Override + public void sendSignal(RoundInputConnector roundInputConnector) { + // @TODO: REMOVE THE NEED FOR THIS + } + + public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { int left = data.getInt("left"); int top = data.getInt("top"); int right = data.getInt("right"); int bottom = data.getInt("bottom"); - return new Placeholder(left, top, right, bottom); + return new Placeholder(partGrid, left, top, right, bottom); } @Override diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java b/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java new file mode 100644 index 0000000..fedea4f --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java @@ -0,0 +1,5 @@ +package com.codigoparallevar.minicards.types; + +interface Dropper { + void drop(Wire wire); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java index d10e2d7..1d480ea 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java @@ -1,5 +1,12 @@ package com.codigoparallevar.minicards.types; -public interface InputConnector extends Selectable { +public interface InputConnector extends Selectable, Dropper { void updatePosition(int x, int y); + + int getX(); + int getY(); + + void sendSignal(); + + void getAttachment(Wire wire); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java b/app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java index 68df24e..6f08290 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Moveable.java @@ -1,7 +1,7 @@ package com.codigoparallevar.minicards.types; public interface Moveable { - void move(int x, int y); + void moveEnd(int x, int y); void drop(int x, int y); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java index bd3e1cf..ddf321b 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java @@ -2,8 +2,7 @@ package com.codigoparallevar.minicards.types; import android.graphics.Canvas; -public interface OutputConnector extends Selectable { - void drop(Wire wire); +public interface OutputConnector extends Selectable, Dropper { void drawWires(Canvas canvas, boolean devMode); void updatePosition(int x, int y); } 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 0c0fd46..fd6c542 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java @@ -19,4 +19,6 @@ public interface Part extends Selectable, Moveable, Drawable { List getOutputConnectors(); JSONObject serialize() throws JSONException; + + void sendSignal(RoundInputConnector roundInputConnector); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java new file mode 100644 index 0000000..a63f19d --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java @@ -0,0 +1,7 @@ +package com.codigoparallevar.minicards.types; + +public interface PartGrid { + Selectable getPartOn(int x, int y); + + InputConnector getInputConnectorOn(int x, int y); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java new file mode 100644 index 0000000..aa8ce90 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java @@ -0,0 +1,69 @@ +package com.codigoparallevar.minicards.types; + +import android.util.Log; + +import java.util.LinkedList; +import java.util.List; + +public class RoundInputConnector implements InputConnector { + private final Part _part; + private int _xposition; + private int _yposition; + private final int _radius; + private final List _attachments = new LinkedList<>(); + + public RoundInputConnector(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 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 sendSignal() { + _part.sendSignal(this); + } + + @Override + public void getAttachment(Wire wire) { + _attachments.add(wire); + } + + @Override + public void drop(Wire wire) { + Log.d("InputConnector", "Dropped wire " + wire); + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java index 4c66ba6..b2b3251 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java @@ -7,6 +7,7 @@ import java.util.LinkedList; import java.util.List; public class RoundOutputConnector implements OutputConnector, Drawable { + private PartGrid _partGrid; private int _centerX; private int _centerY; private final int _radius; @@ -14,8 +15,9 @@ public class RoundOutputConnector implements OutputConnector, Drawable { private Wire _currentWire = null; private final List _wires; - public RoundOutputConnector(Part part, int centerX, int centerY, int radius) { + public RoundOutputConnector(Part part, PartGrid partGrid, int centerX, int centerY, int radius) { _part = part; + _partGrid = partGrid; _centerX = centerX; _centerY = centerY; _radius = radius; @@ -46,6 +48,16 @@ public class RoundOutputConnector implements OutputConnector, Drawable { public void drop(Wire wire) { if (wire == _currentWire){ _currentWire = null; + + InputConnector resultPoint = _partGrid.getInputConnectorOn(wire.getXEnd(), wire.getYEnd()); + + Log.d("RoundOutputConnector", "Dropped wire on " + resultPoint); + + if (resultPoint == null){ + return; + } + + wire.attachTo(resultPoint); _wires.add(wire); } else { @@ -70,10 +82,20 @@ public class RoundOutputConnector implements OutputConnector, Drawable { public void updatePosition(int x, int y) { _centerX = x; _centerY = y; + + for (Wire wire : _wires){ + wire.moveStart(x, y); + } } @Override public void draw(Canvas canvas, boolean devMode) { } + + public void sendSignal() { + for (Wire wire : _wires){ + wire.sendSignal(); + } + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java index c69692b..8b3cbd5 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java @@ -6,14 +6,15 @@ import android.graphics.Paint; import android.graphics.Path; public class Wire implements Moveable, Drawable { - private final OutputConnector _dropper; - private final int _xinit; - private final int _yinit; + private final Dropper _dropper; + private int _xinit; + private int _yinit; private int _xend; private int _yend; private final static int _pathRunWay = 100; + private InputConnector _attachedTo = null; - public Wire(OutputConnector dropper, int xInit, int yInit) { + public Wire(Dropper dropper, int xInit, int yInit) { super(); _dropper = dropper; _xinit = xInit; @@ -23,8 +24,13 @@ public class Wire implements Moveable, Drawable { _yend = yInit; } + public void moveStart(int x, int y){ + _xinit = x; + _yinit = y; + } + @Override - public void move(int x, int y) { + public void moveEnd(int x, int y) { _xend = x; _yend = y; } @@ -52,4 +58,25 @@ public class Wire implements Moveable, Drawable { canvas.drawPath(samplePath, pathPaint); } + + public int getXEnd() { + return _xend; + } + + public int getYEnd() { + return _yend; + } + + public void attachTo(InputConnector resultPoint) { + _attachedTo = resultPoint; + _xend = resultPoint.getX(); + _yend = resultPoint.getY(); + resultPoint.getAttachment(this); + } + + public void sendSignal() { + if (_attachedTo != null) { + _attachedTo.sendSignal(); + } + } } From b37e5e1ae12a3f931f01028e699ce68cc207ecf1 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Sat, 22 Jul 2017 17:46:24 -0400 Subject: [PATCH 3/8] Move Tuple4 out of CanvasView and create Tuple2. --- .../minicards/CanvasView.java | 25 +++---------------- .../minicards/types/PartConnection.java | 14 +++++++++++ .../minicards/types/Tuple2.java | 11 ++++++++ .../minicards/types/Tuple4.java | 21 ++++++++++++++++ 4 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/Tuple4.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index c6f36f9..ab0e011 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -21,6 +21,7 @@ import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.Position; import com.codigoparallevar.minicards.types.Selectable; +import com.codigoparallevar.minicards.types.Tuple4; import org.json.JSONArray; import org.json.JSONException; @@ -54,7 +55,7 @@ class CanvasView extends View implements PartGrid { private final static float touchTimeForLongTouchInMillis = 500; private boolean _isDragging = false; private MainActivity parentActivity = null; - private Tuple _dropToRemoveZone = new Tuple<>(0, 0, 0, 0); + private Tuple4 _dropToRemoveZone = new Tuple4<>(0, 0, 0, 0); private boolean _devMode = false; public CanvasView(Context context) { @@ -362,31 +363,11 @@ class CanvasView extends View implements PartGrid { } public void setDropZone(float x1, float x2, float y1, float y2) { - _dropToRemoveZone = new Tuple<>((int) x1, (int) x2, (int) y1, (int) y2); + _dropToRemoveZone = new Tuple4<>((int) x1, (int) x2, (int) y1, (int) y2); } public void setDevMode(boolean devMode) { _devMode = devMode; this.invalidate(); } - - private class Tuple { - - final T _x1; - final T1 _x2; - final T2 _y1; - final T3 _y2; - - public Tuple(T x1, T1 x2, T2 y1, T3 y2) { - _x1 = x1; - _x2 = x2; - _y1 = y1; - _y2 = y2; - } - - @Override - public String toString(){ - return "(" + _x1 + " - " + _x2 + ", " + _y1 + " - " + _y2 + ")"; - } - } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java new file mode 100644 index 0000000..a7af930 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java @@ -0,0 +1,14 @@ +package com.codigoparallevar.minicards.parts; + +import com.codigoparallevar.minicards.types.Part; + +public class PartConnection { + public final Part outputPart; + public final String inputPartId; + + + public PartConnection(Part outputPart, String inputPartId) { + this.outputPart = outputPart; + this.inputPartId = inputPartId; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java b/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java new file mode 100644 index 0000000..baa4dad --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java @@ -0,0 +1,11 @@ +package com.codigoparallevar.minicards.parts; + +public class Tuple { + public final T1 item1; + public final T2 item2; + + public Tuple(T1 item1, T2 item2) { + this.item1 = item1; + this.item2 = item2; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple4.java b/app/src/main/java/com/codigoparallevar/minicards/types/Tuple4.java new file mode 100644 index 0000000..3f10b97 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Tuple4.java @@ -0,0 +1,21 @@ +package com.codigoparallevar.minicards.types; + +public class Tuple4 { + + public final T _x1; + public final T1 _x2; + public final T2 _y1; + public final T3 _y2; + + public Tuple4(T x1, T1 x2, T2 y1, T3 y2) { + _x1 = x1; + _x2 = x2; + _y1 = y1; + _y2 = y2; + } + + @Override + public String toString(){ + return "(" + _x1 + " - " + _x2 + ", " + _y1 + " - " + _y2 + ")"; + } +} From 9157d2b7ad6539b69e66ba42770af945d7fdddaf Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Sat, 22 Jul 2017 18:35:31 -0400 Subject: [PATCH 4/8] Serialize and save connections. --- .../minicards/CanvasView.java | 63 +++++++++++++++++-- .../minicards/parts/buttons/RoundButton.java | 60 ++++++++++++++++-- .../minicards/parts/samples/ColorBox.java | 40 +++++++++++- .../minicards/parts/samples/Placeholder.java | 28 ++++++++- .../minicards/types/InputConnector.java | 4 ++ .../minicards/types/OutputConnector.java | 6 ++ .../minicards/types/Part.java | 6 ++ .../minicards/types/PartConnection.java | 37 +++++++++-- .../minicards/types/RoundInputConnector.java | 10 +++ .../minicards/types/RoundOutputConnector.java | 22 ++++++- .../minicards/types/Tuple2.java | 6 +- .../minicards/types/Wire.java | 4 ++ .../minicards/utils/Serializations.java | 19 ++++++ 13 files changed, 283 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/utils/Serializations.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index ab0e011..487c343 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -12,6 +12,7 @@ import android.view.MotionEvent; import android.view.View; import com.codigoparallevar.minicards.motion.MotionMode; +import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.parts.buttons.RoundButton; import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.Placeholder; @@ -21,6 +22,7 @@ import com.codigoparallevar.minicards.types.Part; 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 org.json.JSONArray; @@ -32,6 +34,11 @@ import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; class CanvasView extends View implements PartGrid { @@ -82,49 +89,95 @@ class CanvasView extends View implements PartGrid { File filesDir = getContext().getFilesDir(); File file = new File(filesDir + "/" + name); FileReader fileIn = null; + List connections = new LinkedList<>(); + try { fileIn = new FileReader(file); char[] data = new char[(int) file.length()]; fileIn.read(data); JSONArray jsonParts = new JSONArray(new String(data )); for (int i = 0; i < jsonParts.length(); i++){ - deserializeObject(jsonParts.getJSONObject(i)); + connections.addAll(deserializeObject(jsonParts.getJSONObject(i))); } } catch (IOException e) { parts.clear(); - Log.w("PartCanvasView", e.getMessage()); + Log.w("CanvasView", e.getMessage(), e); return false; } catch (JSONException e) { parts.clear(); - Log.w("PartCanvasView", e.getMessage()); + Log.w("CanvasView", e.getMessage(), e); return false; } + resolveConnections(connections); + try { fileIn.close(); } catch (IOException e) { Log.w("PartCanvasView", e.getMessage()); return false; } + return true; } - private void deserializeObject(JSONObject jsonObject) throws JSONException { + private List deserializeObject(JSONObject jsonObject) throws JSONException { String type = jsonObject.getString("_type"); + if(type.equals(RoundButton.class.getName())) { - parts.add(RoundButton.deserialize(this, jsonObject.getJSONObject("_data"))); + Tuple2> buttonInfo = RoundButton.deserialize( + this, + jsonObject.getJSONObject("_data")); + + parts.add(buttonInfo.item1); + return buttonInfo.item2; } else if (type.equals(Placeholder.class.getName())) { parts.add(Placeholder.deserialize(this, jsonObject.getJSONObject("_data"))); + return Collections.emptyList(); } else if (type.equals(ColorBox.class.getName())){ parts.add(ColorBox.deserialize(this, jsonObject.getJSONObject("_data"))); + return Collections.emptyList(); } else { throw new JSONException("Expected known class, found " + type); } } + private void resolveConnections(List connections) { + Map partsById = buildPartsById(); + for (PartConnection connection : connections){ + if (!partsById.containsKey(connection.inputPartId)){ + Log.e("Canvas view", "Key '" + connection.inputPartId + + "' not found on deserialization"); + continue; + } + + Part inputPart = partsById.get(connection.inputPartId); + InputConnector inputConnector = inputPart.getConnectorWithId(connection.inputConnectorId); + + if (inputConnector == null){ + Log.e("Canvas view", "Connector ID '" + connection.inputConnectorId + + "' not found on deserialization"); + continue; + } + + OutputConnector outputConnector = connection.outputConnector; + outputConnector.connectTo(inputConnector); + } + } + + private Map buildPartsById() { + Map partsById = new HashMap<>(parts.size()); + for (Part part : parts) { + partsById.put(part.getId(), part); + Log.w("CanvasView", "Added part ID: " + part.getId() + " - " + part); + } + + return partsById; + } + @Override public void onDraw(Canvas canvas){ final long renderStartTime = System.currentTimeMillis(); 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 d5f3807..0c8e957 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 @@ -5,6 +5,8 @@ import android.graphics.Color; import android.graphics.Paint; import android.util.Log; +import com.codigoparallevar.minicards.types.PartConnection; +import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.InputConnector; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.OutputConnector; @@ -12,16 +14,20 @@ 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.utils.Serializations; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.UUID; public class RoundButton implements Part { - + private final String _id; private final PartGrid _partGrid; private int _xCenter; private int _yCenter; @@ -32,7 +38,10 @@ public class RoundButton implements Part { private List _outputConnectors; private final RoundOutputConnector _pressedOuputConnector; - public RoundButton(PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) { + + private RoundButton(String id, PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) { + _id = id; + _partGrid = partGrid; _xCenter = xCenter; _yCenter = yCenter; @@ -48,7 +57,10 @@ public class RoundButton implements Part { _outputConnectors = new LinkedList<>(); _outputConnectors.add(_pressedOuputConnector); + } + public RoundButton(PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) { + this(UUID.randomUUID().toString(), partGrid, xCenter, yCenter, innerRadius, outerRadius); } @Override @@ -153,28 +165,68 @@ public class RoundButton implements Part { public JSONObject serialize() throws JSONException { JSONObject serialized = new JSONObject(); + serialized.put("id", _id); serialized.put("x_center", _xCenter); serialized.put("y_center", _yCenter); serialized.put("inner_radius", _innerRadius); serialized.put("outer_radius", _outerRadius); serialized.put("outer_radius_thickness", _outerRadiusThickness); + serialized.put("on_pressed_output_connector", + Serializations.serialize(serializeConnectionEndpoints())); return serialized; } + private List> serializeConnectionEndpoints() { + List> serializedData = new LinkedList<>(); + + for (Tuple2 endpoint : _pressedOuputConnector.getConnectionEndpoints()){ + serializedData.add(PartConnection.serialize(endpoint.item1, endpoint.item2)); + } + + return serializedData; + } + @Override public void sendSignal(RoundInputConnector roundInputConnector) { // @TODO: REMOVE THE NEED FOR THIS } - public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { + @Override + public String getId() { + return _id; + } + + @Override + public InputConnector getConnectorWithId(String inputConnectorId) { + return null; + } + + @Override + public String getConnectorId(InputConnector inputConnector) { + return null; + } + + public static Tuple2> deserialize(PartGrid partGrid, JSONObject data) throws JSONException { + String id = data.getString("id"); int xCenter = data.getInt("x_center"); int yCenter = data.getInt("y_center"); int innerRadius = data.getInt("inner_radius"); int outerRadius = data.getInt("outer_radius"); - return new RoundButton(partGrid, xCenter, yCenter, innerRadius, outerRadius); + RoundButton button = new RoundButton(id, partGrid, xCenter, yCenter, innerRadius, outerRadius); + + List connections = new LinkedList<>(); + + JSONArray connectorOuts = data.getJSONArray("on_pressed_output_connector"); + for (int i = 0; i < connectorOuts.length(); i++){ + connections.add(PartConnection.deserialize( + button._pressedOuputConnector, + connectorOuts.getJSONObject(i))); + } + + return new Tuple2>(button, connections); } @Override 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 bb28e57..b294ca5 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 @@ -19,8 +19,11 @@ import org.json.JSONObject; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.UUID; public class ColorBox implements Part { + private static final String TOGGLE_INPUT_CONNECTOR_ID = "toggle_input_connector"; + private final String _id; private final PartGrid _partGrid; private boolean _activated; private int _left; @@ -30,7 +33,8 @@ public class ColorBox implements Part { private List inputConnectors; private InputConnector _toggleInputConnector; - public ColorBox(PartGrid partGrid, int left, int top, int right, int bottom) { + private ColorBox(String id, PartGrid partGrid, int left, int top, int right, int bottom) { + _id = id; _partGrid = partGrid; _left = left; _top = top; @@ -45,6 +49,11 @@ public class ColorBox implements Part { getInputConnectRadius()); inputConnectors = new LinkedList<>(); inputConnectors.add(_toggleInputConnector); + + } + + public ColorBox(PartGrid partGrid, int left, int top, int right, int bottom) { + this(UUID.randomUUID().toString(), partGrid, left, top, right, bottom); } @Override @@ -139,6 +148,7 @@ public class ColorBox implements Part { 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); @@ -152,13 +162,39 @@ public class ColorBox implements Part { _activated = !_activated; } + @Override + public String getId() { + return _id; + } + + @Override + public InputConnector getConnectorWithId(String inputConnectorId) { + switch (inputConnectorId){ + case ColorBox.TOGGLE_INPUT_CONNECTOR_ID: + return _toggleInputConnector; + + default: + return null; + } + } + + @Override + public String getConnectorId(InputConnector inputConnector) { + if (inputConnector == _toggleInputConnector){ + return ColorBox.TOGGLE_INPUT_CONNECTOR_ID; + } + + return null; + } + public static Part 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"); - return new ColorBox(partGrid, left, top, right, bottom); + return new ColorBox(id, partGrid, left, top, right, bottom); } @Override 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 2631ffd..af6ebe1 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 @@ -17,15 +17,18 @@ import org.json.JSONObject; import java.util.Collections; import java.util.List; +import java.util.UUID; public class Placeholder implements Part { + private final String _id; private final PartGrid _partGrid; private int _left; private int _top; private int _right; private int _bottom; - public Placeholder(PartGrid partGrid, int left, int top, int right, int bottom) { + public Placeholder(String id, PartGrid partGrid, int left, int top, int right, int bottom) { + _id = id; _partGrid = partGrid; _left = left; _top = top; @@ -33,6 +36,10 @@ public class Placeholder implements Part { _bottom = bottom; } + public Placeholder(PartGrid partGrid, int left, int top, int right, int bottom) { + this(UUID.randomUUID().toString(), partGrid, left, top, right, bottom); + } + @Override public int getLeft() { return _left; @@ -108,6 +115,7 @@ public class Placeholder implements Part { 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); @@ -121,13 +129,29 @@ public class Placeholder implements Part { // @TODO: REMOVE THE NEED FOR THIS } + @Override + public String getId() { + return _id; + } + + @Override + public InputConnector getConnectorWithId(String inputConnectorId) { + return null; + } + + @Override + public String getConnectorId(InputConnector inputConnector) { + return null; + } + public static Part 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"); - return new Placeholder(partGrid, left, top, right, bottom); + return new Placeholder(id, partGrid, left, top, right, bottom); } @Override diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java index 1d480ea..a8f84e3 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java @@ -9,4 +9,8 @@ public interface InputConnector extends Selectable, Dropper { void sendSignal(); void getAttachment(Wire wire); + + Part getPart(); + + String getId(); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java index ddf321b..814bff5 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java @@ -2,7 +2,13 @@ package com.codigoparallevar.minicards.types; import android.graphics.Canvas; +import java.util.List; + public interface OutputConnector extends Selectable, Dropper { void drawWires(Canvas canvas, boolean devMode); void updatePosition(int x, int y); + + List> getConnectionEndpoints(); + + void connectTo(InputConnector inputConnector); } 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 fd6c542..05326e7 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java @@ -21,4 +21,10 @@ public interface Part extends Selectable, Moveable, Drawable { JSONObject serialize() throws JSONException; void sendSignal(RoundInputConnector roundInputConnector); + + String getId(); + + InputConnector getConnectorWithId(String inputConnectorId); + + String getConnectorId(InputConnector inputConnector); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java index a7af930..176662c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java @@ -1,14 +1,41 @@ -package com.codigoparallevar.minicards.parts; +package com.codigoparallevar.minicards.types; -import com.codigoparallevar.minicards.types.Part; +import com.codigoparallevar.minicards.parts.buttons.RoundButton; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.LinkedHashMap; +import java.util.Map; public class PartConnection { - public final Part outputPart; + private static final String INPUT_CONNECTOR_ID_KEY = "input_connector_id"; + private static final String INPUT_PART_ID_KEY = "input_part_id"; + public final OutputConnector outputConnector; public final String inputPartId; + public final String inputConnectorId; - public PartConnection(Part outputPart, String inputPartId) { - this.outputPart = outputPart; + public PartConnection(OutputConnector outputConnector, String inputPartId, + String inputConnectorId) { + this.outputConnector = outputConnector; this.inputPartId = inputPartId; + this.inputConnectorId = inputConnectorId; + } + + public static PartConnection deserialize(OutputConnector outputConnector, JSONObject metadata) throws JSONException { + String inputConnectorId = metadata.getString(PartConnection.INPUT_CONNECTOR_ID_KEY); + String inputPartId = metadata.getString(PartConnection.INPUT_PART_ID_KEY); + + return new PartConnection(outputConnector, inputPartId, inputConnectorId); + } + + public static Map serialize(final String inputConnectorId, final String inputPartId) { + + Map serialized = new LinkedHashMap<>(2); + serialized.put(PartConnection.INPUT_CONNECTOR_ID_KEY, inputConnectorId); + serialized.put(PartConnection.INPUT_PART_ID_KEY, inputPartId); + + return serialized; } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java index aa8ce90..e34bc5b 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java @@ -62,6 +62,16 @@ public class RoundInputConnector implements InputConnector { _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); diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java index b2b3251..a5e7b66 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java @@ -89,8 +89,28 @@ public class RoundOutputConnector implements OutputConnector, Drawable { } @Override - public void draw(Canvas canvas, boolean devMode) { + public List> getConnectionEndpoints() { + List> endpointIds = new LinkedList<>(); + for (Wire wire : _wires) { + InputConnector inputConnector = wire.getAttachedTo(); + Part endPart = inputConnector.getPart(); + endpointIds.add(new Tuple2<>(inputConnector.getId(), endPart.getId())); + } + + return endpointIds; + } + + @Override + public void connectTo(InputConnector inputConnector) { + Wire wire = new Wire(this, _centerX, _centerY); + wire.attachTo(inputConnector); + _wires.add(wire); + } + + @Override + public void draw(Canvas canvas, boolean devMode) { + // TODO: Complete this part } public void sendSignal() { diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java b/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java index baa4dad..081350a 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Tuple2.java @@ -1,10 +1,10 @@ -package com.codigoparallevar.minicards.parts; +package com.codigoparallevar.minicards.types; -public class Tuple { +public class Tuple2 { public final T1 item1; public final T2 item2; - public Tuple(T1 item1, T2 item2) { + public Tuple2(T1 item1, T2 item2) { this.item1 = item1; this.item2 = item2; } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java index 8b3cbd5..552d9c8 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java @@ -79,4 +79,8 @@ public class Wire implements Moveable, Drawable { _attachedTo.sendSignal(); } } + + public InputConnector getAttachedTo() { + return _attachedTo; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/utils/Serializations.java b/app/src/main/java/com/codigoparallevar/minicards/utils/Serializations.java new file mode 100644 index 0000000..1119a08 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/utils/Serializations.java @@ -0,0 +1,19 @@ +package com.codigoparallevar.minicards.utils; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.List; +import java.util.Map; + +public class Serializations { + + public static JSONArray serialize(List> data) { + JSONArray array = new JSONArray(); + for (Map dict : data) { + array.put(new JSONObject(dict)); + } + + return array; + } +} From 5839e9a693f150c15965b482db780174a6897209 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Sat, 22 Jul 2017 18:42:55 -0400 Subject: [PATCH 5/8] Fix colorbox spawned at the start. --- .../main/java/com/codigoparallevar/minicards/CanvasView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 487c343..413f1e9 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -80,8 +80,8 @@ class CanvasView extends View implements PartGrid { if (!loadState()){ parts.add(new Placeholder(this, 50, 50, 750, 500)); - parts.add(new ColorBox(this, 250, 250, 100, 100)); - parts.add(new RoundButton(this, 500, 1200, 80, 100)); + parts.add(new ColorBox(this, 600, 1000, 700, 1100)); + parts.add(new RoundButton(this, 300, 1200, 80, 100)); } } From 8773f46b609cc76cb2bd7512d725e421484b8f56 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Sun, 23 Jul 2017 09:29:30 -0400 Subject: [PATCH 6/8] Test the possibilities of Type-safe wiring. --- .../minicards/CanvasView.java | 11 ++++--- .../minicards/parts/buttons/RoundButton.java | 7 ++-- .../minicards/parts/samples/ColorBox.java | 11 ++++--- .../minicards/parts/samples/Placeholder.java | 7 ++-- .../minicards/types/Dropper.java | 4 +-- .../minicards/types/InputConnector.java | 16 ---------- .../minicards/types/OutputConnector.java | 14 -------- .../minicards/types/Part.java | 7 ++-- .../minicards/types/PartConnection.java | 2 +- .../minicards/types/PartGrid.java | 5 ++- .../minicards/types/RoundInputConnector.java | 8 +++-- .../minicards/types/RoundOutputConnector.java | 32 ++++++++++++------- .../minicards/types/SignalWire.java | 16 ++++++++++ .../minicards/types/Wire.java | 18 +++++------ .../connectors/input/InputConnector.java | 22 +++++++++++++ .../input/SignalInputConnector.java | 7 ++++ .../connectors/output/OutputConnector.java | 25 +++++++++++++++ .../output/SignalOutputConnector.java | 9 ++++++ .../minicards/types/wireData/Signal.java | 4 +++ .../types/wireData/WireDataType.java | 4 +++ 20 files changed, 153 insertions(+), 76 deletions(-) delete mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java delete mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/SignalWire.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/InputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/SignalInputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/SignalOutputConnector.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/wireData/Signal.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/types/wireData/WireDataType.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 413f1e9..b50193c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -16,8 +16,9 @@ import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.parts.buttons.RoundButton; import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.Placeholder; -import com.codigoparallevar.minicards.types.InputConnector; -import com.codigoparallevar.minicards.types.OutputConnector; +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.PartGrid; import com.codigoparallevar.minicards.types.Position; @@ -374,7 +375,7 @@ class CanvasView extends View implements PartGrid { } } // Then with input ones - for (InputConnector inputConnector : part.getInputConnectors()){ + for (InputConnector inputConnector : part.getSignalInputConnectors()){ if (inputConnector.containsPoint(x, y)){ return inputConnector; } @@ -386,13 +387,13 @@ class CanvasView extends View implements PartGrid { @Override @Nullable - public InputConnector getInputConnectorOn(int x, int y) { + public SignalInputConnector getSignalInputConnectorOn(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()){ + for (SignalInputConnector inputConnector : part.getSignalInputConnectors()){ if (inputConnector.containsPoint(x, y)){ return inputConnector; } 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 0c8e957..6f2aac1 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 @@ -7,9 +7,10 @@ import android.util.Log; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.Tuple2; -import com.codigoparallevar.minicards.types.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.Moveable; -import com.codigoparallevar.minicards.types.OutputConnector; +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; @@ -152,7 +153,7 @@ public class RoundButton implements Part { } @Override - public List getInputConnectors() { + public List getSignalInputConnectors() { return Collections.emptyList(); } 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 b294ca5..f86a775 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 @@ -6,9 +6,10 @@ import android.graphics.Paint; import android.graphics.Rect; import android.util.Log; -import com.codigoparallevar.minicards.types.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.Moveable; -import com.codigoparallevar.minicards.types.OutputConnector; +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; @@ -30,8 +31,8 @@ public class ColorBox implements Part { private int _top; private int _right; private int _bottom; - private List inputConnectors; - private InputConnector _toggleInputConnector; + private List inputConnectors; + private SignalInputConnector _toggleInputConnector; private ColorBox(String id, PartGrid partGrid, int left, int top, int right, int bottom) { _id = id; @@ -135,7 +136,7 @@ public class ColorBox implements Part { } @Override - public List getInputConnectors() { + public List getSignalInputConnectors() { return inputConnectors; } 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 af6ebe1..6271fc2 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,9 +5,10 @@ import android.graphics.Color; import android.graphics.Paint; import android.util.Log; -import com.codigoparallevar.minicards.types.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.Moveable; -import com.codigoparallevar.minicards.types.OutputConnector; +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; @@ -102,7 +103,7 @@ public class Placeholder implements Part { } @Override - public List getInputConnectors() { + public List getSignalInputConnectors() { return Collections.emptyList(); } 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 fedea4f..13b7139 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,5 @@ package com.codigoparallevar.minicards.types; -interface Dropper { - void drop(Wire wire); +public interface Dropper { + void drop(T wire); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java deleted file mode 100644 index a8f84e3..0000000 --- a/app/src/main/java/com/codigoparallevar/minicards/types/InputConnector.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.codigoparallevar.minicards.types; - -public interface InputConnector extends Selectable, Dropper { - void updatePosition(int x, int y); - - int getX(); - int getY(); - - void sendSignal(); - - void getAttachment(Wire wire); - - Part getPart(); - - String getId(); -} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java deleted file mode 100644 index 814bff5..0000000 --- a/app/src/main/java/com/codigoparallevar/minicards/types/OutputConnector.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.codigoparallevar.minicards.types; - -import android.graphics.Canvas; - -import java.util.List; - -public interface OutputConnector extends Selectable, Dropper { - void drawWires(Canvas canvas, boolean devMode); - void updatePosition(int x, int y); - - List> getConnectionEndpoints(); - - void connectTo(InputConnector inputConnector); -} 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 05326e7..3dc11ae 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java @@ -1,6 +1,8 @@ package com.codigoparallevar.minicards.types; -import android.graphics.Canvas; +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 org.json.JSONException; import org.json.JSONObject; @@ -15,7 +17,7 @@ public interface Part extends Selectable, Moveable, Drawable { void touched(); - List getInputConnectors(); + List getSignalInputConnectors(); List getOutputConnectors(); JSONObject serialize() throws JSONException; @@ -27,4 +29,5 @@ public interface Part extends Selectable, Moveable, Drawable { InputConnector getConnectorWithId(String inputConnectorId); String getConnectorId(InputConnector inputConnector); + } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java index 176662c..a5bdbd4 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartConnection.java @@ -1,6 +1,6 @@ package com.codigoparallevar.minicards.types; -import com.codigoparallevar.minicards.parts.buttons.RoundButton; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import org.json.JSONException; import org.json.JSONObject; 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 a63f19d..2502b0c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/PartGrid.java @@ -1,7 +1,10 @@ package com.codigoparallevar.minicards.types; +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; + public interface PartGrid { Selectable getPartOn(int x, int y); - InputConnector getInputConnectorOn(int x, int y); + SignalInputConnector getSignalInputConnectorOn(int x, int y); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java index e34bc5b..6e976c7 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java @@ -2,10 +2,13 @@ package com.codigoparallevar.minicards.types; import android.util.Log; +import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.wireData.Signal; + import java.util.LinkedList; import java.util.List; -public class RoundInputConnector implements InputConnector { +public class RoundInputConnector implements SignalInputConnector { private final Part _part; private int _xposition; private int _yposition; @@ -57,8 +60,9 @@ public class RoundInputConnector implements InputConnector { _part.sendSignal(this); } + @Override - public void getAttachment(Wire wire) { + public void getAttachment(Wire wire) { _attachments.add(wire); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java index a5e7b66..b139d47 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java @@ -3,17 +3,23 @@ package com.codigoparallevar.minicards.types; import android.graphics.Canvas; import android.util.Log; +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.connectors.output.SignalOutputConnector; +import com.codigoparallevar.minicards.types.wireData.Signal; + import java.util.LinkedList; import java.util.List; -public class RoundOutputConnector implements OutputConnector, Drawable { +public class RoundOutputConnector implements Drawable, SignalOutputConnector { private PartGrid _partGrid; private int _centerX; private int _centerY; private final int _radius; private final Part _part; - private Wire _currentWire = null; - private final List _wires; + private SignalWire _currentWire = null; + private final List _wires; public RoundOutputConnector(Part part, PartGrid partGrid, int centerX, int centerY, int radius) { _part = part; @@ -41,15 +47,17 @@ public class RoundOutputConnector implements OutputConnector, Drawable { } private void startWire() { - _currentWire = new Wire(this, _centerX, _centerY); + _currentWire = new SignalWire(this, _centerX, _centerY); } @Override - public void drop(Wire wire) { + public void drop(SignalWire wire) { + if (wire == _currentWire){ _currentWire = null; - InputConnector resultPoint = _partGrid.getInputConnectorOn(wire.getXEnd(), wire.getYEnd()); + SignalInputConnector resultPoint = _partGrid.getSignalInputConnectorOn( + wire.getXEnd(), wire.getYEnd()); Log.d("RoundOutputConnector", "Dropped wire on " + resultPoint); @@ -69,7 +77,7 @@ public class RoundOutputConnector implements OutputConnector, Drawable { @Override public void drawWires(Canvas canvas, boolean devMode) { - for (Wire wire : _wires) { + for (SignalWire wire : _wires) { wire.draw(canvas, devMode); } @@ -83,7 +91,7 @@ public class RoundOutputConnector implements OutputConnector, Drawable { _centerX = x; _centerY = y; - for (Wire wire : _wires){ + for (SignalWire wire : _wires){ wire.moveStart(x, y); } } @@ -92,7 +100,7 @@ public class RoundOutputConnector implements OutputConnector, Drawable { public List> getConnectionEndpoints() { List> endpointIds = new LinkedList<>(); - for (Wire wire : _wires) { + for (SignalWire wire : _wires) { InputConnector inputConnector = wire.getAttachedTo(); Part endPart = inputConnector.getPart(); endpointIds.add(new Tuple2<>(inputConnector.getId(), endPart.getId())); @@ -102,8 +110,8 @@ public class RoundOutputConnector implements OutputConnector, Drawable { } @Override - public void connectTo(InputConnector inputConnector) { - Wire wire = new Wire(this, _centerX, _centerY); + public void connectTo(SignalInputConnector inputConnector) { + SignalWire wire = new SignalWire(this, _centerX, _centerY); wire.attachTo(inputConnector); _wires.add(wire); } @@ -114,7 +122,7 @@ public class RoundOutputConnector implements OutputConnector, Drawable { } public void sendSignal() { - for (Wire wire : _wires){ + for (SignalWire wire : _wires){ wire.sendSignal(); } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/SignalWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/SignalWire.java new file mode 100644 index 0000000..17eb660 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/SignalWire.java @@ -0,0 +1,16 @@ +package com.codigoparallevar.minicards.types; + +import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.wireData.Signal; + +public class SignalWire extends Wire implements Moveable, Drawable { + public SignalWire(Dropper dropper, int xInit, int yInit) { + super(dropper, xInit, yInit); + } + + public void sendSignal() { + if (_attachedTo != null) { + _attachedTo.sendSignal(); + } + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java index 552d9c8..1531e47 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java @@ -5,17 +5,21 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; -public class Wire implements Moveable, Drawable { +import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.wireData.WireDataType; + +public class Wire> + implements Moveable, Drawable { + private final Dropper _dropper; private int _xinit; private int _yinit; private int _xend; private int _yend; private final static int _pathRunWay = 100; - private InputConnector _attachedTo = null; + protected InputConnectorType _attachedTo = null; public Wire(Dropper dropper, int xInit, int yInit) { - super(); _dropper = dropper; _xinit = xInit; _yinit = yInit; @@ -67,19 +71,13 @@ public class Wire implements Moveable, Drawable { return _yend; } - public void attachTo(InputConnector resultPoint) { + public void attachTo(InputConnectorType resultPoint) { _attachedTo = resultPoint; _xend = resultPoint.getX(); _yend = resultPoint.getY(); resultPoint.getAttachment(this); } - public void sendSignal() { - if (_attachedTo != null) { - _attachedTo.sendSignal(); - } - } - public InputConnector getAttachedTo() { return _attachedTo; } 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 new file mode 100644 index 0000000..7431fe0 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/InputConnector.java @@ -0,0 +1,22 @@ +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.wireData.WireDataType; + +public interface InputConnector> + extends Selectable, Dropper { + + void updatePosition(int x, int y); + + int getX(); + int getY(); + + void getAttachment(Wire wire); + + Part getPart(); + + String getId(); +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/SignalInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/SignalInputConnector.java new file mode 100644 index 0000000..9e32fa9 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/input/SignalInputConnector.java @@ -0,0 +1,7 @@ +package com.codigoparallevar.minicards.types.connectors.input; + +import com.codigoparallevar.minicards.types.wireData.Signal; + +public interface SignalInputConnector extends InputConnector { + void sendSignal(); +} 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 new file mode 100644 index 0000000..56840cb --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/OutputConnector.java @@ -0,0 +1,25 @@ +package com.codigoparallevar.minicards.types.connectors.output; + +import android.graphics.Canvas; + +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.input.InputConnector; +import com.codigoparallevar.minicards.types.wireData.WireDataType; + +import java.util.List; + +public interface OutputConnector, + T2 extends Wire> + extends Selectable, Dropper { + + void drawWires(Canvas canvas, boolean devMode); + void updatePosition(int x, int y); + + List> getConnectionEndpoints(); + + void connectTo(T1 inputConnector); +} 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 new file mode 100644 index 0000000..128a917 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/output/SignalOutputConnector.java @@ -0,0 +1,9 @@ +package com.codigoparallevar.minicards.types.connectors.output; + +import com.codigoparallevar.minicards.types.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/Signal.java b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/Signal.java new file mode 100644 index 0000000..0afe474 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/Signal.java @@ -0,0 +1,4 @@ +package com.codigoparallevar.minicards.types.wireData; + +public class Signal implements WireDataType { +} 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 new file mode 100644 index 0000000..6d073f8 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/types/wireData/WireDataType.java @@ -0,0 +1,4 @@ +package com.codigoparallevar.minicards.types.wireData; + +public interface WireDataType { +} From f63854c86380f49faad8e885304ab696ffdeb2f5 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Mon, 30 Oct 2017 22:34:58 +0100 Subject: [PATCH 7/8] Only allow one connection per in/out pair. --- .../minicards/types/RoundOutputConnector.java | 16 ++++++++++++++++ build.gradle | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java index b139d47..829356c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java @@ -9,6 +9,7 @@ import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector; import com.codigoparallevar.minicards.types.wireData.Signal; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -20,6 +21,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { 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; @@ -28,6 +30,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { _centerY = centerY; _radius = radius; _wires = new LinkedList<>(); + _connections = new HashSet<>(); } @Override @@ -61,10 +64,17 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { 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); } @@ -111,6 +121,12 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { @Override public void connectTo(SignalInputConnector inputConnector) { + if (_connections.contains(inputConnector)) { + return; + } + + _connections.add(inputConnector); + SignalWire wire = new SignalWire(this, _centerX, _centerY); wire.attachTo(inputConnector); _wires.add(wire); diff --git a/build.gradle b/build.gradle index a377c55..c960b37 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0-alpha5' + classpath 'com.android.tools.build:gradle:3.0.0-beta2' // NOTE: Do not place your application dependencies here; they belong From 82b6e7188cfd2a0716a8564bd10ac82e09f9dbe3 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Tue, 31 Oct 2017 00:06:46 +0100 Subject: [PATCH 8/8] Fix dangling wires when remove input-connected part. --- .../com/codigoparallevar/minicards/CanvasView.java | 7 ++++--- .../minicards/parts/buttons/RoundButton.java | 8 ++++++++ .../minicards/parts/samples/ColorBox.java | 8 ++++++++ .../minicards/parts/samples/Placeholder.java | 7 +++++++ .../codigoparallevar/minicards/types/Dropper.java | 1 + .../minicards/types/RoundInputConnector.java | 12 ++++++++++++ .../minicards/types/RoundOutputConnector.java | 8 ++++++++ .../codigoparallevar/minicards/types/Selectable.java | 1 + .../com/codigoparallevar/minicards/types/Wire.java | 5 +++++ 9 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index b50193c..19b32bc 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -249,13 +249,14 @@ class CanvasView extends View implements PartGrid { } else if (motionMode == MotionMode.Type.LongTouch) { if (selectedPart != null) { + selectedPart.getMoveable().drop(x, y); + if (inDropZone(x, y)) { Log.d("Canvas", "Deleting element" + selectedPart); parts.remove(selectedPart); + selectedPart.unlink(); } - else { - selectedPart.getMoveable().drop(x, y); - } + invalidate(); } } 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 6f2aac1..b0946b7 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 @@ -240,4 +240,12 @@ public class RoundButton implements Part { public Moveable getMoveable() { return this; } + + + @Override + public void unlink() { + for (InputConnector input : getSignalInputConnectors()) { + input.unlink(); + } + } } 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 f86a775..f19e872 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 @@ -6,6 +6,7 @@ import android.graphics.Paint; import android.graphics.Rect; import android.util.Log; +import com.codigoparallevar.minicards.types.Wire; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; @@ -209,6 +210,13 @@ public class ColorBox implements Part { return this; } + @Override + public void unlink() { + for (InputConnector input : getSignalInputConnectors()) { + input.unlink(); + } + } + public int getInputConnectorCenterX() { return getLeft(); } 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 6271fc2..35af650 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 @@ -165,4 +165,11 @@ public class Placeholder implements Part { public Moveable getMoveable() { return this; } + + @Override + public void unlink() { + for (InputConnector input : getSignalInputConnectors()) { + 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 13b7139..be9506d 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Dropper.java @@ -2,4 +2,5 @@ package com.codigoparallevar.minicards.types; public interface Dropper { void drop(T wire); + void unlinkWire(T wire); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java index 6e976c7..8c2f9ea 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundInputConnector.java @@ -35,6 +35,13 @@ public class RoundInputConnector implements SignalInputConnector { 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; @@ -80,4 +87,9 @@ public class RoundInputConnector implements SignalInputConnector { 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/types/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java index 829356c..4ffad30 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/RoundOutputConnector.java @@ -49,6 +49,9 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { return _currentWire; } + @Override + public void unlink() {} + private void startWire() { _currentWire = new SignalWire(this, _centerX, _centerY); } @@ -85,6 +88,11 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { } } + @Override + public void unlinkWire(SignalWire wire) { + _wires.remove(wire); + } + @Override public void drawWires(Canvas canvas, boolean devMode) { for (SignalWire wire : _wires) { diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java b/app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java index 4d79ddd..3877ede 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Selectable.java @@ -3,4 +3,5 @@ package com.codigoparallevar.minicards.types; public interface Selectable { boolean containsPoint(int x, int y); Moveable getMoveable(); + void unlink(); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java index 1531e47..ab95cfd 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Wire.java @@ -81,4 +81,9 @@ public class Wire