From 8b3bbfee782dc23906ca5d89d4a83161a6d13d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Mon, 1 Jun 2020 15:07:48 +0200 Subject: [PATCH] Add removal mechanism for signal wires. --- .../minicards/CanvasView.java | 75 +++++++++---- .../minicards/CardActivity.java | 2 - .../SignalRoundOutputConnector.java | 11 +- .../types/connectors/Wiring/Wire.java | 101 +++++++++++++++--- 4 files changed, 154 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 0dbd211..e09319c 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -13,11 +13,14 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.codigoparallevar.minicards.motion.MotionMode; +import com.codigoparallevar.minicards.parts.connectors.SignalRoundOutputConnector; import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.Position; import com.codigoparallevar.minicards.types.Selectable; +import com.codigoparallevar.minicards.types.connectors.Wiring.SignalWire; +import com.codigoparallevar.minicards.types.connectors.Wiring.Wire; import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; @@ -35,6 +38,7 @@ import java.util.List; import java.util.Map; public class CanvasView extends View implements PartGrid { + private static final String LogTag = "Canvas view"; @NonNull List parts = new ArrayList<>(); @@ -92,7 +96,7 @@ public class CanvasView extends View implements PartGrid { Map partsById = buildPartsById(); for (PartConnection connection : connections){ if (!partsById.containsKey(connection.inputPartId)){ - Log.e("Canvas view", "Key '" + connection.inputPartId + Log.e(LogTag, "Key '" + connection.inputPartId + "' not found on deserialization"); continue; } @@ -101,14 +105,14 @@ public class CanvasView extends View implements PartGrid { InputConnector inputConnector = inputPart.getConnectorWithId(connection.inputConnectorId); if (inputConnector == null){ - Log.e("Canvas view", "Connector ID '" + connection.inputConnectorId + Log.e(LogTag, "Connector ID '" + connection.inputConnectorId + "' not found on deserialization"); continue; } OutputConnector outputConnector = connection.outputConnector; if (inputConnector == null){ - Log.e("Canvas view", "Connector not found on connection"); + Log.e(LogTag, "Connector not found on connection"); continue; } @@ -120,7 +124,7 @@ public class CanvasView extends View implements PartGrid { outputConnector.connectTo(inputConnector); } catch (ClassCastException e) { - Log.e("Minicards - Canvas view", "Malformed connection", e); + Log.e(LogTag, "Malformed connection", e); } } } @@ -129,7 +133,7 @@ public class CanvasView extends View implements PartGrid { Map partsById = new HashMap<>(parts.size()); for (Part part : parts) { partsById.put(part.get_id(), part); - Log.w("CanvasView", "Added part ID: " + part.get_id() + " - " + part); + Log.w(LogTag, "Added part ID: " + part.get_id() + " - " + part); } return partsById; @@ -146,7 +150,7 @@ public class CanvasView extends View implements PartGrid { part.draw(scrolledCanvas, _devMode); } - // Log.d("Render time", System.currentTimeMillis() - renderStartTime + "ms"); + Log.v(LogTag, "Render time: " + (System.currentTimeMillis() - renderStartTime) + "ms"); } private void drawBackground(ScrolledCanvas canvas) { @@ -190,11 +194,21 @@ public class CanvasView extends View implements PartGrid { lastTouchedPosition.to(xInScreen, yInScreen); lastTouchedTime = System.currentTimeMillis(); if (selectedPart == null) { - Log.d("Touched part", "not found"); + Log.d(LogTag, "Touched part not found"); return false; } + if (selectedPart instanceof Wire) { + // No drag or long-touch, just show the "cut" option + // TODO: + Log.d(LogTag, "Touched Wire"); + Wire selectedWire = (Wire) selectedPart; + selectedPart = null; - Log.d("Touched part", "Part: " + selectedPart); + selectedWire.unlink(); + } + else { + Log.d(LogTag, "Touched part FOUND. Part: " + selectedPart); + } } break; @@ -215,7 +229,7 @@ public class CanvasView extends View implements PartGrid { selectedPart.getMoveable().drop(xInCanvas, yInCanvas); if (inDropZone(xInScreen, yInScreen)) { - Log.d("Canvas", "Deleting element" + selectedPart); + Log.d(LogTag, "Deleting element" + selectedPart); parts.remove(selectedPart); selectedPart.unlink(); } @@ -230,7 +244,7 @@ public class CanvasView extends View implements PartGrid { try { saveState(); } catch (IOException e) { - Log.w("PartCanvasView", e.getMessage()); + Log.w(LogTag, e.getMessage()); } break; @@ -240,7 +254,7 @@ public class CanvasView extends View implements PartGrid { break; } - Log.d("CanvasView", "Moving part="+selectedPart); + // Log.v(LogTag, "Moving part="+selectedPart); if (selectedPart == null){ int xMovement = _mouseDownPoint.item1 - xInScreen; int yMovement = _mouseDownPoint.item2 - yInScreen; @@ -251,7 +265,7 @@ public class CanvasView extends View implements PartGrid { _mouseDownPoint = new Tuple2(xInScreen, yInScreen); } else { - Log.d("Canvas", "X: " + xInScreen + " Y: " + yInScreen + Log.d(LogTag, "X: " + xInScreen + " Y: " + yInScreen + " in drop zone " + _dropToRemoveZone + " : " + inDropZone(xInScreen, yInScreen)); @@ -273,7 +287,7 @@ public class CanvasView extends View implements PartGrid { default: { - Log.d("PartCanvasView", "Unhandled action: " + event.getAction()); + Log.d(LogTag, "Unhandled action: " + event.getAction()); } } @@ -337,12 +351,35 @@ public class CanvasView extends View implements PartGrid { } } - // Then with input ones - List inputConnectors = part.getInputConnectors(); - if (inputConnectors != null) { - for (InputConnector inputConnector : inputConnectors) { - if (inputConnector.containsPoint(x, y)) { - return inputConnector; +// // Then with input ones +// List inputConnectors = part.getInputConnectors(); +// if (inputConnectors != null) { +// for (InputConnector inputConnector : inputConnectors) { +// if (inputConnector.containsPoint(x, y)) { +// return inputConnector; +// } +// } +// } + } + + // Finally, try with the wires + for (int i = parts.size() - 1; i >= 0; i--) { + final Part part = parts.get(i); + List outputConnectors = part.getOutputConnectors(); + if (outputConnectors != null) { + for (OutputConnector outputConnector : outputConnectors) { + if (outputConnector instanceof SignalRoundOutputConnector) { + SignalRoundOutputConnector conn = (SignalRoundOutputConnector) outputConnector; + List wires = conn.getWires(); + if (wires == null) { + continue; + } + for (Wire wire : wires) { + if (wire.containsPoint(x, y)) { + Log.d(LogTag, "Point in wire " + wire); + return wire; + } + } } } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java b/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java index 9a3dbfe..76632a4 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CardActivity.java @@ -230,12 +230,10 @@ public class CardActivity extends AppCompatActivity { devFabMenu.setVisibility(View.GONE); userFabMenu.setVisibility(View.GONE); ((View)removePartFab).setVisibility(View.VISIBLE); - Log.d("Main", "Changing visibility!"); } else { this.setDevMode(devMode); ((View)removePartFab).setVisibility(View.GONE); - Log.d("Main", "Now changing visibility!"); } canvasView.setDropZone( removePartFab.getX(), removePartFab.getX() + removePartFab.getWidth(), diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/SignalRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/SignalRoundOutputConnector.java index 3aa0dc6..1d3101f 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/SignalRoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/SignalRoundOutputConnector.java @@ -20,6 +20,8 @@ import java.util.List; public class SignalRoundOutputConnector implements Drawable, SignalOutputConnector, RoundOutputConnector { + private static final String LogTag = "RoundOutputConnector"; + private PartGrid _partGrid; private int _centerX; private int _centerY; @@ -71,7 +73,7 @@ public class SignalRoundOutputConnector implements Drawable, SignalOutputConnect SignalInputConnector resultPoint = _partGrid.getSignalInputConnectorOn( wire.getXEnd(), wire.getYEnd()); - Log.d("RoundOutputConnector", "Dropped wire on " + resultPoint); + Log.d(LogTag, "Dropped wire on " + resultPoint); // Not connected if (resultPoint == null){ @@ -88,7 +90,7 @@ public class SignalRoundOutputConnector implements Drawable, SignalOutputConnect _wires.add(wire); } else { - Log.w("RoundOutputConnector", + Log.w(LogTag, "Asked to drop non matching wire " + "(expected " + _currentWire + ", got " + wire + ")"); } @@ -97,6 +99,7 @@ public class SignalRoundOutputConnector implements Drawable, SignalOutputConnect @Override public void unlinkWire(SignalWire wire) { _wires.remove(wire); + _connections.remove(wire.getAttachedTo()); } @Override @@ -173,4 +176,8 @@ public class SignalRoundOutputConnector implements Drawable, SignalOutputConnect public float getRadius() { return _radius; } + + public List getWires() { + return this._wires; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java index dec86e3..cefae9f 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java @@ -3,23 +3,28 @@ package com.codigoparallevar.minicards.types.connectors.Wiring; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; +import android.graphics.RectF; import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Moveable; +import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.wireData.WireDataType; public class Wire> - implements Moveable, Drawable { + implements Moveable, Drawable, Selectable { + + private final double MAX_DISTANCE_TO_CONTAINS = 15; private final Dropper _dropper; private int _xinit; private int _yinit; private int _xend; private int _yend; + private Path path = null; private final static int _pathRunWay = 100; protected InputConnectorType _attachedTo = null; protected OutputConnector _attachedFrom = null; @@ -36,12 +41,14 @@ public class Wire