From 82b6e7188cfd2a0716a8564bd10ac82e09f9dbe3 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Tue, 31 Oct 2017 00:06:46 +0100 Subject: [PATCH] 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