Test the possibilities of Type-safe wiring.

This commit is contained in:
kenkeiras 2017-07-23 09:29:30 -04:00
parent 5839e9a693
commit 8773f46b60
20 changed files with 153 additions and 76 deletions

View File

@ -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;
}

View File

@ -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<InputConnector> getInputConnectors() {
public List<SignalInputConnector> getSignalInputConnectors() {
return Collections.emptyList();
}

View File

@ -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<InputConnector> inputConnectors;
private InputConnector _toggleInputConnector;
private List<SignalInputConnector> 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<InputConnector> getInputConnectors() {
public List<SignalInputConnector> getSignalInputConnectors() {
return inputConnectors;
}

View File

@ -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<InputConnector> getInputConnectors() {
public List<SignalInputConnector> getSignalInputConnectors() {
return Collections.emptyList();
}

View File

@ -1,5 +1,5 @@
package com.codigoparallevar.minicards.types;
interface Dropper {
void drop(Wire wire);
public interface Dropper<T extends Wire> {
void drop(T wire);
}

View File

@ -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();
}

View File

@ -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<Tuple2<String, String>> getConnectionEndpoints();
void connectTo(InputConnector inputConnector);
}

View File

@ -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<InputConnector> getInputConnectors();
List<SignalInputConnector> getSignalInputConnectors();
List<OutputConnector> getOutputConnectors();
JSONObject serialize() throws JSONException;
@ -27,4 +29,5 @@ public interface Part extends Selectable, Moveable, Drawable {
InputConnector getConnectorWithId(String inputConnectorId);
String getConnectorId(InputConnector inputConnector);
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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<Signal, SignalInputConnector> wire) {
_attachments.add(wire);
}

View File

@ -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<Wire> _wires;
private SignalWire _currentWire = null;
private final List<SignalWire> _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<Tuple2<String, String>> getConnectionEndpoints() {
List<Tuple2<String, String>> 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();
}
}

View File

@ -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<Signal, SignalInputConnector> implements Moveable, Drawable {
public SignalWire(Dropper dropper, int xInit, int yInit) {
super(dropper, xInit, yInit);
}
public void sendSignal() {
if (_attachedTo != null) {
_attachedTo.sendSignal();
}
}
}

View File

@ -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<T extends WireDataType, InputConnectorType extends InputConnector<T, InputConnectorType>>
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;
}

View File

@ -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<T extends WireDataType, T1 extends InputConnector<T, T1>>
extends Selectable, Dropper {
void updatePosition(int x, int y);
int getX();
int getY();
void getAttachment(Wire<T, T1> wire);
Part getPart();
String getId();
}

View File

@ -0,0 +1,7 @@
package com.codigoparallevar.minicards.types.connectors.input;
import com.codigoparallevar.minicards.types.wireData.Signal;
public interface SignalInputConnector extends InputConnector<Signal, SignalInputConnector> {
void sendSignal();
}

View File

@ -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<T extends WireDataType,
T1 extends InputConnector<T, T1>,
T2 extends Wire<T, T1>>
extends Selectable, Dropper<T2> {
void drawWires(Canvas canvas, boolean devMode);
void updatePosition(int x, int y);
List<Tuple2<String, String>> getConnectionEndpoints();
void connectTo(T1 inputConnector);
}

View File

@ -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<Signal, SignalInputConnector, SignalWire> {
void sendSignal();
}

View File

@ -0,0 +1,4 @@
package com.codigoparallevar.minicards.types.wireData;
public class Signal implements WireDataType {
}

View File

@ -0,0 +1,4 @@
package com.codigoparallevar.minicards.types.wireData;
public interface WireDataType {
}