Add Any-Inputs, string wires (and *puts) and ConvertToString part.

This commit is contained in:
kenkeiras 2018-01-26 02:10:55 +01:00
parent 2ea0515bba
commit 67348d9e03
18 changed files with 988 additions and 16 deletions

View File

@ -19,9 +19,11 @@ import com.codigoparallevar.minicards.types.Position;
import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.Selectable;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.Tuple4; import com.codigoparallevar.minicards.types.Tuple4;
import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import java.io.IOException; import java.io.IOException;
@ -101,7 +103,21 @@ class CanvasView extends View implements PartGrid {
} }
OutputConnector outputConnector = connection.outputConnector; OutputConnector outputConnector = connection.outputConnector;
outputConnector.connectTo(inputConnector); if (inputConnector == null){
Log.e("Canvas view", "Connector not found on connection");
continue;
}
if (inputConnector instanceof AnyInputConnector) {
inputConnector = ((AnyInputConnector) inputConnector).matchType(outputConnector);
}
try {
outputConnector.connectTo(inputConnector);
}
catch (ClassCastException e) {
Log.e("Minicards - Canvas view", "Malformed connection", e);
}
} }
} }
@ -332,11 +348,17 @@ class CanvasView extends View implements PartGrid {
// Then with input ones // Then with input ones
for (InputConnector inputConnector : part.getInputConnectors()){ for (InputConnector inputConnector : part.getInputConnectors()){
if (!(inputConnector instanceof SignalInputConnector)){ SignalInputConnector signalInputConnector;
if (inputConnector instanceof SignalInputConnector){
signalInputConnector = (SignalInputConnector) inputConnector;
}
else if (inputConnector instanceof AnyInputConnector) {
signalInputConnector = ((AnyInputConnector) inputConnector).ToSignalInputConnector();
}
else {
continue; continue;
} }
SignalInputConnector signalInputConnector = (SignalInputConnector) inputConnector;
if (inputConnector.containsPoint(x, y)){ if (inputConnector.containsPoint(x, y)){
return signalInputConnector; return signalInputConnector;
} }
@ -355,13 +377,18 @@ class CanvasView extends View implements PartGrid {
// Then with input ones // Then with input ones
for (InputConnector inputConnector : part.getInputConnectors()){ for (InputConnector inputConnector : part.getInputConnectors()){
if (!(inputConnector instanceof BooleanInputConnector)){ BooleanInputConnector booleanInputConnector;
if (inputConnector instanceof BooleanInputConnector){
booleanInputConnector = (BooleanInputConnector) inputConnector;
}
else if (inputConnector instanceof AnyInputConnector) {
booleanInputConnector = ((AnyInputConnector) inputConnector).ToBooleanInputConnector();
}
else {
continue; continue;
} }
BooleanInputConnector booleanInputConnector = (BooleanInputConnector) inputConnector; if (inputConnector.containsPoint(x, y)){
if (booleanInputConnector.containsPoint(x, y)){
return booleanInputConnector; return booleanInputConnector;
} }
} }
@ -370,6 +397,36 @@ class CanvasView extends View implements PartGrid {
return null; return null;
} }
@Override
@Nullable
public StringInputConnector getStringInputConnectorOn(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()){
StringInputConnector stringInputConnector;
if (inputConnector instanceof StringInputConnector){
stringInputConnector = (StringInputConnector) inputConnector;
}
else if (inputConnector instanceof AnyInputConnector) {
stringInputConnector = ((AnyInputConnector) inputConnector).ToStringInputConnector();
}
else {
continue;
}
if (inputConnector.containsPoint(x, y)){
return stringInputConnector;
}
}
}
return null;
}
public void addPart(Part part) { public void addPart(Part part) {
parts.add(part); parts.add(part);
invalidate(); invalidate();

View File

@ -12,6 +12,7 @@ import com.codigoparallevar.minicards.parts.logic.Ticker;
import com.codigoparallevar.minicards.parts.logic.Toggle; import com.codigoparallevar.minicards.parts.logic.Toggle;
import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.parts.samples.Placeholder; import com.codigoparallevar.minicards.parts.samples.Placeholder;
import com.codigoparallevar.minicards.parts.strings.ConvertToString;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid; import com.codigoparallevar.minicards.types.PartGrid;
@ -263,18 +264,25 @@ public class CardFile {
Collections.<PartConnection>emptyList()); Collections.<PartConnection>emptyList());
} }
else if (type.equals(Ticker.class.getName())){ else if (type.equals(Ticker.class.getName())){
Tuple2<Part, List<PartConnection>> buttonInfo = Ticker.deserialize( Tuple2<Part, List<PartConnection>> tickerInfo = Ticker.deserialize(
grid, grid,
jsonObject.getJSONObject("_data")); jsonObject.getJSONObject("_data"));
return buttonInfo; return tickerInfo;
} }
else if (type.equals(Toggle.class.getName())){ else if (type.equals(Toggle.class.getName())){
Tuple2<Part, List<PartConnection>> buttonInfo = Toggle.deserialize( Tuple2<Part, List<PartConnection>> toggleInfo = Toggle.deserialize(
grid, grid,
jsonObject.getJSONObject("_data")); jsonObject.getJSONObject("_data"));
return buttonInfo; return toggleInfo;
}
else if (type.equals(ConvertToString.class.getName())){
Tuple2<Part, List<PartConnection>> convertToStringInfo = ConvertToString.deserialize(
grid,
jsonObject.getJSONObject("_data"));
return convertToStringInfo;
} }
else { else {
throw new JSONException("Expected known class, found " + type); throw new JSONException("Expected known class, found " + type);
@ -293,7 +301,7 @@ public class CardFile {
new File(oldPath).delete(); new File(oldPath).delete();
} catch (IOException e) { } catch (IOException e) {
Log.e("Minicards - cardfile rename", "Cannot create new file for rename", e); Log.e("Minicards - rename", "Cannot create new file for rename", e);
} }
} }

View File

@ -10,6 +10,7 @@ import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.logic.Ticker; import com.codigoparallevar.minicards.parts.logic.Ticker;
import com.codigoparallevar.minicards.parts.logic.Toggle; import com.codigoparallevar.minicards.parts.logic.Toggle;
import com.codigoparallevar.minicards.parts.samples.ColorBox; import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.parts.strings.ConvertToString;
import com.codigoparallevar.minicards.types.Part; import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.Tuple2;
@ -25,6 +26,7 @@ class PartsHolder {
add(new Tuple2<>("Ticker", Ticker.getInstantiator())); add(new Tuple2<>("Ticker", Ticker.getInstantiator()));
add(new Tuple2<>("Red/Green box", ColorBox.getInstantiator())); add(new Tuple2<>("Red/Green box", ColorBox.getInstantiator()));
add(new Tuple2<>("Toggle", Toggle.getInstantiator())); add(new Tuple2<>("Toggle", Toggle.getInstantiator()));
add(new Tuple2<>("ToString", ConvertToString.getInstantiator()));
}}; }};
public PartsHolder(Context context) { public PartsHolder(Context context) {

View File

@ -5,6 +5,7 @@ import com.codigoparallevar.minicards.types.Selectable;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
class StubPartGrid implements PartGrid { class StubPartGrid implements PartGrid {
@Override @Override
@ -30,4 +31,9 @@ class StubPartGrid implements PartGrid {
@Override @Override
public void update() { public void update() {
} }
@Override
public StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd) {
return null;
}
} }

View File

@ -0,0 +1,98 @@
package com.codigoparallevar.minicards.parts.connectors;
import android.util.Log;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.connectors.Wiring.Wire;
import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
import java.util.LinkedList;
import java.util.List;
public class AnyRoundInputConnector extends AnyInputConnector {
private final Part _part;
private int _xposition;
private int _yposition;
private final int _radius;
private final List<Wire> _attachments = new LinkedList<>();
public AnyRoundInputConnector(Part part,
int inputConnectorCenterX, int inputConnectorCenterY,
int inputConnectorRadius) {
_part = part;
_xposition = inputConnectorCenterX;
_yposition = inputConnectorCenterY;
_radius = inputConnectorRadius;
}
@Override
public boolean containsPoint(int x, int y) {
return ((Math.abs(x - _xposition) <= _radius)
&& (Math.abs(y - _yposition) <= _radius));
}
@Override
public Moveable getMoveable() {
return new Wire(this, _xposition, _yposition);
}
@Override
public void unlink() {
for (Wire wire : _attachments) {
wire.unlink();
}
}
@Override
public void updatePosition(int x, int y) {
_xposition = x;
_yposition = y;
for (Wire wire : _attachments){
wire.moveEnd(x, y);
}
}
@Override
public int getX() {
return _xposition;
}
@Override
public int getY() {
return _yposition;
}
@Override
public void send(WireDataType signal) {
_part.send(this, signal);
}
@Override
public void getAttachment(Wire wire) {
_attachments.add(wire);
}
@Override
public Part getPart() {
return _part;
}
@Override
public String getId() {
return _part.getConnectorId(this);
}
@Override
public void drop(Wire wire) {
Log.d("InputConnector", "Dropped wire " + wire);
}
@Override
public void unlinkWire(Wire wire) {
_attachments.remove(wire);
}
}

View File

@ -0,0 +1,98 @@
package com.codigoparallevar.minicards.parts.connectors;
import android.util.Log;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.connectors.Wiring.Wire;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
import java.util.LinkedList;
import java.util.List;
public class StringRoundInputConnector implements StringInputConnector {
private final Part _part;
private int _xposition;
private int _yposition;
private final int _radius;
private final List<Wire> _attachments = new LinkedList<>();
public StringRoundInputConnector(Part part,
int inputConnectorCenterX, int inputConnectorCenterY,
int inputConnectorRadius) {
_part = part;
_xposition = inputConnectorCenterX;
_yposition = inputConnectorCenterY;
_radius = inputConnectorRadius;
}
@Override
public boolean containsPoint(int x, int y) {
return ((Math.abs(x - _xposition) <= _radius)
&& (Math.abs(y - _yposition) <= _radius));
}
@Override
public Moveable getMoveable() {
return new Wire(this, _xposition, _yposition);
}
@Override
public void unlink() {
for (Wire wire : _attachments) {
wire.unlink();
}
}
@Override
public void updatePosition(int x, int y) {
_xposition = x;
_yposition = y;
for (Wire wire : _attachments){
wire.moveEnd(x, y);
}
}
@Override
public int getX() {
return _xposition;
}
@Override
public int getY() {
return _yposition;
}
@Override
public void send(StringSignal signal) {
_part.send(this, signal);
}
@Override
public void getAttachment(Wire<StringSignal, StringInputConnector> wire) {
_attachments.add(wire);
}
@Override
public Part getPart() {
return _part;
}
@Override
public String getId() {
return _part.getConnectorId(this);
}
@Override
public void drop(Wire wire) {
Log.d("InputConnector", "Dropped wire " + wire);
}
@Override
public void unlinkWire(Wire wire) {
_attachments.remove(wire);
}
}

View File

@ -0,0 +1,158 @@
package com.codigoparallevar.minicards.parts.connectors;
import android.util.Log;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.connectors.Wiring.StringWire;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.StringOutputConnector;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
public class StringRoundOutputConnector implements Drawable, StringOutputConnector {
private PartGrid _partGrid;
private int _centerX;
private int _centerY;
private final int _radius;
private final Part _part;
private StringWire _currentWire = null;
private final List<StringWire> _wires;
private final HashSet<InputConnector> _connections;
public StringRoundOutputConnector(Part part, PartGrid partGrid, int centerX, int centerY, int radius) {
_part = part;
_partGrid = partGrid;
_centerX = centerX;
_centerY = centerY;
_radius = radius;
_wires = new LinkedList<>();
_connections = new HashSet<>();
}
@Override
public boolean containsPoint(int x, int y) {
return ((Math.abs(x - _centerX) <= _radius)
&& (Math.abs(y - _centerY) <= _radius));
}
@Override
public Moveable getMoveable() {
if (_currentWire == null) {
startWire();
}
return _currentWire;
}
@Override
public void unlink() {}
private void startWire() {
_currentWire = new StringWire(this, _centerX, _centerY);
}
@Override
public void drop(StringWire wire) {
if (wire == _currentWire){
_currentWire = null;
StringInputConnector resultPoint = _partGrid.getStringInputConnectorOn(
wire.getXEnd(), wire.getYEnd());
Log.d("RoundOutputConnector", "Dropped wire on " + resultPoint);
// Not connected
if (resultPoint == null){
return;
}
// Already connected
if (_connections.contains(resultPoint)) {
return;
}
_connections.add(resultPoint);
wire.attachTo(resultPoint);
_wires.add(wire);
}
else {
Log.w("RoundOutputConnector",
"Asked to drop non matching wire "
+ "(expected " + _currentWire + ", got " + wire + ")");
}
}
@Override
public void unlinkWire(StringWire wire) {
_wires.remove(wire);
}
@Override
public void drawWires(ScrolledCanvas canvas, boolean devMode) {
for (StringWire wire : _wires) {
wire.draw(canvas, devMode);
}
if (_currentWire != null) {
_currentWire.draw(canvas, devMode);
}
}
@Override
public void updatePosition(int x, int y) {
_centerX = x;
_centerY = y;
for (StringWire wire : _wires){
wire.moveStart(x, y);
}
}
@Override
public List<Tuple2<String, String>> getConnectionEndpoints() {
List<Tuple2<String, String>> endpointIds = new LinkedList<>();
for (StringWire wire : _wires) {
InputConnector inputConnector = wire.getAttachedTo();
Part endPart = inputConnector.getPart();
endpointIds.add(new Tuple2<>(inputConnector.getId(), endPart.get_id()));
}
return endpointIds;
}
@Override
public void connectTo(StringInputConnector inputConnector) {
if (_connections.contains(inputConnector)) {
return;
}
_connections.add(inputConnector);
StringWire wire = new StringWire(this, _centerX, _centerY);
wire.attachTo(inputConnector);
_wires.add(wire);
}
@Override
public void draw(ScrolledCanvas canvas, boolean devMode) {
// TODO: Complete this part
}
public void send(StringSignal signal) {
for (StringWire wire : _wires){
wire.send(signal);
}
}
}

View File

@ -0,0 +1,351 @@
package com.codigoparallevar.minicards.parts.strings;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.util.Log;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector;
import com.codigoparallevar.minicards.parts.connectors.StringRoundOutputConnector;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.connectors.input.AnyInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
import com.codigoparallevar.minicards.utils.Serializations;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class ConvertToString implements Part {
private static final String TOGGLE_INPUT_CONNECTOR_ID = "any_input_connector";
private static final int DEFAULT_SIDE_SIZE = 100;
private final String _id;
private final PartGrid _partGrid;
private final StringRoundOutputConnector _stringOutputConnector;
private final LinkedList<OutputConnector> _outputConnectors;
private int _left;
private int _top;
private int _right;
private int _bottom;
private List<InputConnector> inputConnectors;
private AnyInputConnector _toggleInputConnector;
@NonNull
private String _lastValue;
private ConvertToString(String id, PartGrid partGrid, int left, int top, int right, int bottom) {
_id = id;
_partGrid = partGrid;
_left = left;
_top = top;
_right = right;
_bottom = bottom;
_lastValue = "-";
// Input connector
_toggleInputConnector = new AnyRoundInputConnector(
this,
getInputConnectorCenterX(),
getInputConnectorCenterY(),
getInputConnectRadius());
inputConnectors = new LinkedList<>();
inputConnectors.add(_toggleInputConnector);
// Output connector
// Create connectors
_stringOutputConnector = new StringRoundOutputConnector(
this,
_partGrid,
getOutputConnectorCenterX(), getOutputConnectorCenterY(),
getOutputConnectRadius());
_outputConnectors = new LinkedList<>();
_outputConnectors.add(_stringOutputConnector);
}
public ConvertToString(PartGrid partGrid, int left, int top, int right, int bottom) {
this(UUID.randomUUID().toString(), partGrid, left, top, right, bottom);
}
@Override
public int get_left() {
return _left;
}
@Override
public int get_right() {
return _right;
}
@Override
public int get_top() {
return _top;
}
@Override
public int get_bottom() {
return _bottom;
}
@Override
public void draw(ScrolledCanvas canvas, boolean devMode) {
if (devMode) {
drawConnector(canvas);
drawWires(canvas, devMode);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(
new Rect(_left, _top,
_right, _bottom),
paint);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.GREEN);
textPaint.setTextSize(100);
canvas.drawText(_lastValue,
_left,
_top - 5,
textPaint);
}
}
private void drawConnector(ScrolledCanvas canvas) {
Paint inputConnectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
inputConnectorPaint.setColor(Color.YELLOW);
canvas.drawCircle(
getInputConnectorCenterX(), getInputConnectorCenterY(),
getInputConnectRadius(),
inputConnectorPaint);
Paint outputConnectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
outputConnectorPaint.setColor(Color.RED);
canvas.drawCircle(_right, getOutputConnectorCenterY(),
getOutputConnectRadius(),
outputConnectorPaint);
}
private void drawWires(ScrolledCanvas canvas, boolean devMode) {
for (OutputConnector outputConnector : _outputConnectors){
outputConnector.drawWires(canvas, devMode);
}
}
@Override
public void moveEnd(int x, int y) {
final int width = _right - _left;
final int height = _bottom - _top;
_left = x - width / 2;
_right = _left + width;
_top = y - height / 2;
_bottom = _top + height;
_toggleInputConnector.updatePosition(
getInputConnectorCenterX(),
getInputConnectorCenterY());
_stringOutputConnector.updatePosition(
getOutputConnectorCenterX(),
getOutputConnectorCenterY());
}
@Override
public void drop(int x, int y) {
moveEnd(x, y);
}
@Override
public void touched() {
Log.d("Placeholder", "Placeholder touched");
}
@Override
public List<InputConnector> getInputConnectors() {
return inputConnectors;
}
@Override
public List<OutputConnector> getOutputConnectors() {
return _outputConnectors;
}
public AnyInputConnector getToggleInputConnector() {
return _toggleInputConnector;
}
public OutputConnector getToggleOutputConnector() {
return _stringOutputConnector;
}
@Override
public JSONObject serialize() throws JSONException {
JSONObject serialized = new JSONObject();
serialized.put("id", _id);
serialized.put("left", _left);
serialized.put("top", _top);
serialized.put("right", _right);
serialized.put("bottom", _bottom);
serialized.put("on_string_output_connector",
Serializations.serialize(serializeConnectionEndpoints()));
return serialized;
}
private List<Map<String, String>> serializeConnectionEndpoints() {
List<Map<String, String>> serializedData = new LinkedList<>();
for (Tuple2<String, String> endpoint : _stringOutputConnector.getConnectionEndpoints()){
serializedData.add(PartConnection.serialize(endpoint.item1, endpoint.item2));
}
return serializedData;
}
@Override
public void send(InputConnector roundInputConnector, WireDataType signal) {
String encoded = "null";
Object value = signal.get();
if (value != null){
encoded = value.toString();
}
_lastValue = encoded;
_stringOutputConnector.send(new StringSignal(encoded));
}
@Override
public String get_id() {
return _id;
}
@Override
public InputConnector getConnectorWithId(String inputConnectorId) {
switch (inputConnectorId){
case ConvertToString.TOGGLE_INPUT_CONNECTOR_ID:
return _toggleInputConnector;
default:
return null;
}
}
@Override
public String getConnectorId(InputConnector inputConnector) {
if (inputConnector == _toggleInputConnector){
return ConvertToString.TOGGLE_INPUT_CONNECTOR_ID;
}
return null;
}
@Override
public void resume() {
}
@Override
public void pause() {
}
public static Tuple2<Part, List<PartConnection>> 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");
ConvertToString toggle = new ConvertToString(id, partGrid, left, top, right, bottom);
List<PartConnection> connections = new LinkedList<>();
JSONArray connectorOuts = data.getJSONArray("on_string_output_connector");
for (int i = 0; i < connectorOuts.length(); i++) {
connections.add(PartConnection.deserialize(
toggle._stringOutputConnector,
connectorOuts.getJSONObject(i)));
}
return new Tuple2<Part, List<PartConnection>>(toggle, connections);
}
@Override
public boolean containsPoint(int x, int y) {
return (x >= get_left()) && (x <= get_right())
&& (y >= get_top()) && (y <= get_bottom());
}
@Override
public Moveable getMoveable() {
return this;
}
@Override
public void unlink() {
for (InputConnector input : getInputConnectors()) {
input.unlink();
}
}
public int getInputConnectorCenterX() {
return get_left();
}
private int getInputConnectRadius() {
return (get_right() - get_left()) / 2;
}
public int getInputConnectorCenterY() {
return (get_top() + get_bottom()) / 2;
}
private int getOutputConnectorCenterX() {
return _right;
}
private int getOutputConnectorCenterY() {
return (_top + _bottom) / 2;
}
private int getOutputConnectRadius() {
return (_right - _left) / 2;
}
public static PartInstantiator getInstantiator() {
final int halfSideSize = DEFAULT_SIDE_SIZE / 2;
return new PartInstantiator() {
@Override
protected Part instantiate(PartGrid grid, Tuple2<Integer, Integer> center) {
return new ConvertToString(grid,
center.item1 - halfSideSize, center.item2 - halfSideSize,
center.item1 + halfSideSize, center.item2 + halfSideSize);
}
};
}
}

View File

@ -22,7 +22,7 @@ public interface Part extends Selectable, Moveable, Drawable {
JSONObject serialize() throws JSONException; JSONObject serialize() throws JSONException;
void send(InputConnector roundInputConnector, WireDataType signal); void send(InputConnector inputConnector, WireDataType signal);
String get_id(); String get_id();

View File

@ -2,6 +2,7 @@ package com.codigoparallevar.minicards.types;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
public interface PartGrid { public interface PartGrid {
Selectable getPartOn(int x, int y); Selectable getPartOn(int x, int y);
@ -12,4 +13,6 @@ public interface PartGrid {
Tuple2<Integer,Integer> getCenteredOn(); Tuple2<Integer,Integer> getCenteredOn();
void update(); void update();
StringInputConnector getStringInputConnectorOn(int xEnd, int yEnd);
} }

View File

@ -0,0 +1,19 @@
package com.codigoparallevar.minicards.types.connectors.Wiring;
import com.codigoparallevar.minicards.types.Drawable;
import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
public class StringWire extends Wire<StringSignal, StringInputConnector> implements Moveable, Drawable {
public StringWire(Dropper dropper, int xInit, int yInit) {
super(dropper, xInit, yInit);
}
public void send(StringSignal signal) {
if (_attachedTo != null) {
_attachedTo.send(signal);
}
}
}

View File

@ -0,0 +1,131 @@
package com.codigoparallevar.minicards.types.connectors.input;
import android.support.annotation.NonNull;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.connectors.Wiring.Wire;
import com.codigoparallevar.minicards.types.connectors.output.BooleanOutputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector;
import com.codigoparallevar.minicards.types.connectors.output.StringOutputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
import com.codigoparallevar.minicards.types.wireData.Signal;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
public abstract class AnyInputConnector implements InputConnector<WireDataType, AnyInputConnector> {
public SignalInputConnector ToSignalInputConnector() {
return new WrapAsSignal(this);
}
public BooleanInputConnector ToBooleanInputConnector(){
return new WrapAsBoolean(this);
}
public StringInputConnector ToStringInputConnector(){
return new WrapAsString(this);
}
@NonNull
public InputConnector matchType(@NonNull OutputConnector outputConnector) {
if (outputConnector instanceof SignalOutputConnector) {
return this.ToSignalInputConnector();
}
else if (outputConnector instanceof BooleanOutputConnector){
return this.ToBooleanInputConnector();
}
else if (outputConnector instanceof StringOutputConnector){
return this.ToStringInputConnector();
}
return this;
}
private class WrapAsSignal extends WrapAs implements SignalInputConnector {
WrapAsSignal(AnyInputConnector baseInputConnector) {
super(baseInputConnector);
}
@Override
public void send(Signal data) {
baseInputConnector.send(data);
}
}
private class WrapAsBoolean extends WrapAs implements BooleanInputConnector {
WrapAsBoolean(AnyInputConnector baseInputConnector) {
super(baseInputConnector);
}
@Override
public void send(BooleanSignal data) {
baseInputConnector.send(data);
}
}
private class WrapAsString extends WrapAs implements StringInputConnector {
WrapAsString(AnyInputConnector baseInputConnector) {
super(baseInputConnector);
}
@Override
public void send(StringSignal data) {
baseInputConnector.send(data);
}
}
private abstract class WrapAs {
final AnyInputConnector baseInputConnector;
WrapAs(AnyInputConnector baseInputConnector) {
this.baseInputConnector = baseInputConnector;
}
public boolean containsPoint(int x, int y) {
return baseInputConnector.containsPoint(x, y);
}
public Moveable getMoveable() {
return baseInputConnector.getMoveable();
}
public void unlink() {
baseInputConnector.unlink();
}
public void drop(Wire wire) {
baseInputConnector.drop(wire);
}
public void unlinkWire(Wire wire) {
baseInputConnector.unlinkWire(wire);
}
public void updatePosition(int x, int y) {
baseInputConnector.updatePosition(x, y);
}
public int getX() {
return baseInputConnector.getX();
}
public int getY() {
return baseInputConnector.getY();
}
public Part getPart() {
return baseInputConnector.getPart();
}
public String getId() {
return baseInputConnector.getId();
}
public void getAttachment(Wire wire) {
baseInputConnector.getAttachment(wire);
}
}
}

View File

@ -0,0 +1,6 @@
package com.codigoparallevar.minicards.types.connectors.input;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
public interface StringInputConnector extends InputConnector<StringSignal, StringInputConnector> {
}

View File

@ -0,0 +1,11 @@
package com.codigoparallevar.minicards.types.connectors.output;
import com.codigoparallevar.minicards.types.connectors.Wiring.StringWire;
import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector;
import com.codigoparallevar.minicards.types.wireData.StringSignal;
public interface StringOutputConnector extends OutputConnector<
StringSignal,
StringInputConnector,
StringWire> {
}

View File

@ -1,9 +1,14 @@
package com.codigoparallevar.minicards.types.wireData; package com.codigoparallevar.minicards.types.wireData;
public class BooleanSignal implements WireDataType { public class BooleanSignal implements WireDataType<Boolean> {
public final boolean value; public final boolean value;
public BooleanSignal(boolean value) { public BooleanSignal(boolean value) {
this.value = value; this.value = value;
} }
@Override
public Boolean get() {
return value;
}
} }

View File

@ -1,4 +1,8 @@
package com.codigoparallevar.minicards.types.wireData; package com.codigoparallevar.minicards.types.wireData;
public class Signal implements WireDataType { public class Signal implements WireDataType<Object> {
@Override
public Object get() {
return null;
}
} }

View File

@ -0,0 +1,14 @@
package com.codigoparallevar.minicards.types.wireData;
public class StringSignal implements WireDataType<String> {
public final String value;
public StringSignal(String value) {
this.value = value;
}
@Override
public String get() {
return value;
}
}

View File

@ -1,4 +1,5 @@
package com.codigoparallevar.minicards.types.wireData; package com.codigoparallevar.minicards.types.wireData;
public interface WireDataType { public interface WireDataType<T> {
T get();
} }