AddToggler, add boolean wires, make ColorBox accept boolean.

This commit is contained in:
kenkeiras 2018-01-26 00:46:28 +01:00
parent 7a8d92cb95
commit 2ea0515bba
27 changed files with 796 additions and 63 deletions

View File

@ -12,16 +12,17 @@ import android.view.MotionEvent;
import android.view.View;
import com.codigoparallevar.minicards.motion.MotionMode;
import com.codigoparallevar.minicards.types.PartConnection;
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.PartConnection;
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 com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
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 java.io.IOException;
import java.util.ArrayList;
@ -312,7 +313,7 @@ class CanvasView extends View implements PartGrid {
}
}
// Then with input ones
for (InputConnector inputConnector : part.getSignalInputConnectors()){
for (InputConnector inputConnector : part.getInputConnectors()){
if (inputConnector.containsPoint(x, y)){
return inputConnector;
}
@ -330,9 +331,38 @@ class CanvasView extends View implements PartGrid {
final Part part = parts.get(i);
// Then with input ones
for (SignalInputConnector inputConnector : part.getSignalInputConnectors()){
for (InputConnector inputConnector : part.getInputConnectors()){
if (!(inputConnector instanceof SignalInputConnector)){
continue;
}
SignalInputConnector signalInputConnector = (SignalInputConnector) inputConnector;
if (inputConnector.containsPoint(x, y)){
return inputConnector;
return signalInputConnector;
}
}
}
return null;
}
@Override
@Nullable
public BooleanInputConnector getBooleanInputConnectorOn(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 instanceof BooleanInputConnector)){
continue;
}
BooleanInputConnector booleanInputConnector = (BooleanInputConnector) inputConnector;
if (booleanInputConnector.containsPoint(x, y)){
return booleanInputConnector;
}
}
}

View File

@ -9,6 +9,7 @@ import android.util.Log;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.logic.Ticker;
import com.codigoparallevar.minicards.parts.logic.Toggle;
import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.parts.samples.Placeholder;
import com.codigoparallevar.minicards.types.Part;
@ -137,8 +138,10 @@ public class CardFile {
public static void createDefaultCards(File cardsDir) throws IOException {
// Create a button and a color box, and connect them
RoundButton button = new RoundButton(new StubPartGrid(), 200, 400, 80, 100);
Toggle toggle = new Toggle(new StubPartGrid(), 350, 450, 450, 550);
ColorBox box = new ColorBox(new StubPartGrid(), 500, 350, 600, 450);
button.getPressedOutputConnector().connectTo(box.getToggleInputConnector());
button.getPressedOutputConnector().connectTo(toggle.getToggleInputConnector());
toggle.getToggleOutputConnector().connectTo(box.getStateInputConnector());
CardFile buttonAndColorBoxCard = (new CardFile(cardsDir.getAbsolutePath())
.addPart(button)
@ -266,6 +269,13 @@ public class CardFile {
return buttonInfo;
}
else if (type.equals(Toggle.class.getName())){
Tuple2<Part, List<PartConnection>> buttonInfo = Toggle.deserialize(
grid,
jsonObject.getJSONObject("_data"));
return buttonInfo;
}
else {
throw new JSONException("Expected known class, found " + type);
}

View File

@ -8,6 +8,7 @@ import android.util.Log;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.logic.Ticker;
import com.codigoparallevar.minicards.parts.logic.Toggle;
import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.Tuple2;
@ -20,9 +21,10 @@ class PartsHolder {
private final static List<Tuple2<String, PartInstantiator>> BuiltInParts =
new Vector<Tuple2<String, PartInstantiator>>(){{
add(new Tuple2<String, PartInstantiator>("Round button", RoundButton.getInstantiator()));
add(new Tuple2<String, PartInstantiator>("Ticker", Ticker.getInstantiator()));
add(new Tuple2<String, PartInstantiator>("Red/Green box", ColorBox.getInstantiator()));
add(new Tuple2<>("Round button", RoundButton.getInstantiator()));
add(new Tuple2<>("Ticker", Ticker.getInstantiator()));
add(new Tuple2<>("Red/Green box", ColorBox.getInstantiator()));
add(new Tuple2<>("Toggle", Toggle.getInstantiator()));
}};
public PartsHolder(Context context) {

View File

@ -41,4 +41,19 @@ public class ScrolledCanvas {
offsetPath.offset(-xOrig, -yOrig);
canvas.drawPath(offsetPath, paint);
}
public void drawCenteredText(String text, int x, int y, Paint paint) {
paint.setTextAlign(Paint.Align.LEFT);
Rect r = new Rect();
paint.getTextBounds(text, 0, text.length(), r);
x = x + (int) (r.width() / 2f - r.left);
y = y + (int) (r.height() / 2f - r.bottom);
drawText(text, x, y, paint);
}
public void drawText(String text, int x, int y, Paint paint) {
canvas.drawText(text, x - xOrig, y - yOrig, paint);
}
}

View File

@ -3,6 +3,7 @@ package com.codigoparallevar.minicards;
import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Selectable;
import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
class StubPartGrid implements PartGrid {
@ -16,6 +17,11 @@ class StubPartGrid implements PartGrid {
return null;
}
@Override
public BooleanInputConnector getBooleanInputConnectorOn(int x, int y) {
return null;
}
@Override
public Tuple2<Integer, Integer> getCenteredOn() {
return null;

View File

@ -6,16 +6,16 @@ import android.util.Log;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.parts.connectors.RoundOutputConnector;
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.InputConnector;
import com.codigoparallevar.minicards.types.Moveable;
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;
import com.codigoparallevar.minicards.types.RoundOutputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
import com.codigoparallevar.minicards.utils.Serializations;
import org.json.JSONArray;
@ -152,11 +152,11 @@ public class RoundButton implements Part {
public void touched() {
Log.d("RoundButton", "Round button touched");
_pressedOutputConnector.sendSignal();
_pressedOutputConnector.send(new Signal());
}
@Override
public List<SignalInputConnector> getSignalInputConnectors() {
public List<InputConnector> getInputConnectors() {
return Collections.emptyList();
}
@ -192,7 +192,7 @@ public class RoundButton implements Part {
}
@Override
public void sendSignal(RoundInputConnector roundInputConnector) {
public void send(InputConnector roundInputConnector, WireDataType signal) {
// @TODO: REMOVE THE NEED FOR THIS
}
@ -261,7 +261,7 @@ public class RoundButton implements Part {
@Override
public void unlink() {
for (InputConnector input : getSignalInputConnectors()) {
for (InputConnector input : getInputConnectors()) {
input.unlink();
}
}

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.BooleanInputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
import java.util.LinkedList;
import java.util.List;
public class BooleanRoundInputConnector implements BooleanInputConnector {
private final Part _part;
private int _xposition;
private int _yposition;
private final int _radius;
private final List<Wire> _attachments = new LinkedList<>();
public BooleanRoundInputConnector(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(BooleanSignal signal) {
_part.send(this, signal);
}
@Override
public void getAttachment(Wire<BooleanSignal, BooleanInputConnector> 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.BooleanWire;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.BooleanOutputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConnector {
private PartGrid _partGrid;
private int _centerX;
private int _centerY;
private final int _radius;
private final Part _part;
private BooleanWire _currentWire = null;
private final List<BooleanWire> _wires;
private final HashSet<InputConnector> _connections;
public BooleanRoundOutputConnector(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 BooleanWire(this, _centerX, _centerY);
}
@Override
public void drop(BooleanWire wire) {
if (wire == _currentWire){
_currentWire = null;
BooleanInputConnector resultPoint = _partGrid.getBooleanInputConnectorOn(
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(BooleanWire wire) {
_wires.remove(wire);
}
@Override
public void drawWires(ScrolledCanvas canvas, boolean devMode) {
for (BooleanWire 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 (BooleanWire wire : _wires){
wire.moveStart(x, y);
}
}
@Override
public List<Tuple2<String, String>> getConnectionEndpoints() {
List<Tuple2<String, String>> endpointIds = new LinkedList<>();
for (BooleanWire 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(BooleanInputConnector inputConnector) {
if (_connections.contains(inputConnector)) {
return;
}
_connections.add(inputConnector);
BooleanWire wire = new BooleanWire(this, _centerX, _centerY);
wire.attachTo(inputConnector);
_wires.add(wire);
}
@Override
public void draw(ScrolledCanvas canvas, boolean devMode) {
// TODO: Complete this part
}
public void send(BooleanSignal signal) {
for (BooleanWire wire : _wires){
wire.send(signal);
}
}
}

View File

@ -1,7 +1,10 @@
package com.codigoparallevar.minicards.types;
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.SignalInputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal;
@ -63,8 +66,8 @@ public class RoundInputConnector implements SignalInputConnector {
}
@Override
public void sendSignal() {
_part.sendSignal(this);
public void send(Signal signal) {
_part.send(this, signal);
}

View File

@ -1,11 +1,18 @@
package com.codigoparallevar.minicards.types;
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.SignalWire;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal;
import java.util.HashSet;
import java.util.LinkedList;
@ -143,9 +150,9 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
// TODO: Complete this part
}
public void sendSignal() {
public void send(Signal signal) {
for (SignalWire wire : _wires){
wire.sendSignal();
wire.send(signal);
}
}
}

View File

@ -7,16 +7,16 @@ import android.util.Log;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.parts.connectors.RoundOutputConnector;
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.RoundInputConnector;
import com.codigoparallevar.minicards.types.RoundOutputConnector;
import com.codigoparallevar.minicards.types.Tuple2;
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.wireData.Signal;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
import com.codigoparallevar.minicards.utils.Serializations;
import org.json.JSONArray;
@ -104,7 +104,7 @@ public class Ticker implements Part {
public void unlink() {
pause();
for (InputConnector input : getSignalInputConnectors()) {
for (InputConnector input : getInputConnectors()) {
input.unlink();
}
}
@ -184,7 +184,7 @@ public class Ticker implements Part {
}
@Override
public List<SignalInputConnector> getSignalInputConnectors() {
public List<InputConnector> getInputConnectors() {
return Collections.emptyList();
}
@ -240,7 +240,7 @@ public class Ticker implements Part {
}
@Override
public void sendSignal(RoundInputConnector roundInputConnector) {
public void send(InputConnector roundInputConnector, WireDataType signal) {
// @TODO: REMOVE THE NEED FOR THIS
}
@ -265,7 +265,7 @@ public class Ticker implements Part {
@Override
public void run() {
while (Ticker.this._thread == Thread.currentThread()) {
Ticker.this._signalOutputConnector.sendSignal();
Ticker.this._signalOutputConnector.send(new Signal());
_partGrid.update();
try {

View File

@ -0,0 +1,344 @@
package com.codigoparallevar.minicards.parts.logic;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.parts.connectors.BooleanRoundOutputConnector;
import com.codigoparallevar.minicards.parts.connectors.RoundInputConnector;
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.InputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
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 Toggle implements Part {
private static final String TOGGLE_INPUT_CONNECTOR_ID = "toggle_input_connector";
private static final int DEFAULT_SIDE_SIZE = 100;
private final String _id;
private final PartGrid _partGrid;
private final BooleanRoundOutputConnector _stateOutputConnector;
private final LinkedList<OutputConnector> _outputConnectors;
private boolean _activated;
private int _left;
private int _top;
private int _right;
private int _bottom;
private List<InputConnector> inputConnectors;
private SignalInputConnector _toggleInputConnector;
private Toggle(String id, PartGrid partGrid, int left, int top, int right, int bottom) {
_id = id;
_partGrid = partGrid;
_left = left;
_top = top;
_right = right;
_bottom = bottom;
_activated = false;
// Input connector
_toggleInputConnector = new RoundInputConnector(
this,
getInputConnectorCenterX(),
getInputConnectorCenterY(),
getInputConnectRadius());
inputConnectors = new LinkedList<>();
inputConnectors.add(_toggleInputConnector);
// Output connector
// Create connectors
_stateOutputConnector = new BooleanRoundOutputConnector(
this,
_partGrid,
getOutputConnectorCenterX(), getOutputConnectorCenterY(),
getOutputConnectRadius());
_outputConnectors = new LinkedList<>();
_outputConnectors.add(_stateOutputConnector);
}
public Toggle(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.GRAY);
canvas.drawRect(
new Rect(_left, _top,
_right, _bottom),
paint);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.GREEN);
textPaint.setTextSize(100);
String text = "0";
if (_activated) {
text = "1";
}
canvas.drawCenteredText(text,
(int) ((_left + _right) / 2f - DEFAULT_SIDE_SIZE * 0.4f),
(_top + _bottom) / 2,
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());
_stateOutputConnector.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 SignalInputConnector getToggleInputConnector() {
return _toggleInputConnector;
}
public OutputConnector getToggleOutputConnector() {
return _stateOutputConnector;
}
@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_state_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 : _stateOutputConnector.getConnectionEndpoints()){
serializedData.add(PartConnection.serialize(endpoint.item1, endpoint.item2));
}
return serializedData;
}
@Override
public void send(InputConnector roundInputConnector, WireDataType signal) {
_activated = !_activated;
_stateOutputConnector.send(new BooleanSignal(_activated));
}
@Override
public String get_id() {
return _id;
}
@Override
public InputConnector getConnectorWithId(String inputConnectorId) {
switch (inputConnectorId){
case Toggle.TOGGLE_INPUT_CONNECTOR_ID:
return _toggleInputConnector;
default:
return null;
}
}
@Override
public String getConnectorId(InputConnector inputConnector) {
if (inputConnector == _toggleInputConnector){
return Toggle.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");
Toggle toggle = new Toggle(id, partGrid, left, top, right, bottom);
List<PartConnection> connections = new LinkedList<>();
JSONArray connectorOuts = data.getJSONArray("on_state_output_connector");
for (int i = 0; i < connectorOuts.length(); i++) {
connections.add(PartConnection.deserialize(
toggle._stateOutputConnector,
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 Toggle(grid,
center.item1 - halfSideSize, center.item2 - halfSideSize,
center.item1 + halfSideSize, center.item2 + halfSideSize);
}
};
}
}

View File

@ -7,14 +7,16 @@ import android.util.Log;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.parts.connectors.BooleanRoundInputConnector;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.RoundInputConnector;
import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
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.wireData.BooleanSignal;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
import org.json.JSONException;
import org.json.JSONObject;
@ -34,8 +36,8 @@ public class ColorBox implements Part {
private int _top;
private int _right;
private int _bottom;
private List<SignalInputConnector> inputConnectors;
private SignalInputConnector _toggleInputConnector;
private List<InputConnector> inputConnectors;
private BooleanInputConnector _toggleInputConnector;
private ColorBox(String id, PartGrid partGrid, int left, int top, int right, int bottom) {
_id = id;
@ -46,7 +48,7 @@ public class ColorBox implements Part {
_bottom = bottom;
_activated = false;
_toggleInputConnector = new RoundInputConnector(
_toggleInputConnector = new BooleanRoundInputConnector(
this,
getInputConnectorCenterX(),
getInputConnectorCenterY(),
@ -139,7 +141,7 @@ public class ColorBox implements Part {
}
@Override
public List<SignalInputConnector> getSignalInputConnectors() {
public List<InputConnector> getInputConnectors() {
return inputConnectors;
}
@ -148,7 +150,7 @@ public class ColorBox implements Part {
return Collections.emptyList();
}
public SignalInputConnector getToggleInputConnector() {
public BooleanInputConnector getStateInputConnector() {
return _toggleInputConnector;
}
@ -166,8 +168,10 @@ public class ColorBox implements Part {
}
@Override
public void sendSignal(RoundInputConnector roundInputConnector) {
_activated = !_activated;
public void send(InputConnector roundInputConnector, WireDataType signal) {
if (signal instanceof BooleanSignal) {
_activated = ((BooleanSignal) signal).value;
}
}
@Override
@ -228,7 +232,7 @@ public class ColorBox implements Part {
@Override
public void unlink() {
for (InputConnector input : getSignalInputConnectors()) {
for (InputConnector input : getInputConnectors()) {
input.unlink();
}
}

View File

@ -5,13 +5,12 @@ import android.graphics.Paint;
import android.util.Log;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.Moveable;
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;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
import org.json.JSONException;
import org.json.JSONObject;
@ -103,7 +102,7 @@ public class Placeholder implements Part {
}
@Override
public List<SignalInputConnector> getSignalInputConnectors() {
public List<InputConnector> getInputConnectors() {
return Collections.emptyList();
}
@ -126,7 +125,7 @@ public class Placeholder implements Part {
}
@Override
public void sendSignal(RoundInputConnector roundInputConnector) {
public void send(InputConnector roundInputConnector, WireDataType signal) {
// @TODO: REMOVE THE NEED FOR THIS
}
@ -178,7 +177,7 @@ public class Placeholder implements Part {
@Override
public void unlink() {
for (InputConnector input : getSignalInputConnectors()) {
for (InputConnector input : getInputConnectors()) {
input.unlink();
}
}

View File

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

View File

@ -1,8 +1,8 @@
package com.codigoparallevar.minicards.types;
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.wireData.WireDataType;
import org.json.JSONException;
import org.json.JSONObject;
@ -17,12 +17,12 @@ public interface Part extends Selectable, Moveable, Drawable {
void touched();
List<SignalInputConnector> getSignalInputConnectors();
List<InputConnector> getInputConnectors();
List<OutputConnector> getOutputConnectors();
JSONObject serialize() throws JSONException;
void sendSignal(RoundInputConnector roundInputConnector);
void send(InputConnector roundInputConnector, WireDataType signal);
String get_id();

View File

@ -1,11 +1,13 @@
package com.codigoparallevar.minicards.types;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
public interface PartGrid {
Selectable getPartOn(int x, int y);
SignalInputConnector getSignalInputConnectorOn(int x, int y);
BooleanInputConnector getBooleanInputConnectorOn(int x, int y);
Tuple2<Integer,Integer> getCenteredOn();

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.BooleanInputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
public class BooleanWire extends Wire<BooleanSignal, BooleanInputConnector> implements Moveable, Drawable {
public BooleanWire(Dropper dropper, int xInit, int yInit) {
super(dropper, xInit, yInit);
}
public void send(BooleanSignal signal) {
if (_attachedTo != null) {
_attachedTo.send(signal);
}
}
}

View File

@ -1,5 +1,8 @@
package com.codigoparallevar.minicards.types;
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.SignalInputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal;
@ -8,9 +11,9 @@ public class SignalWire extends Wire<Signal, SignalInputConnector> implements Mo
super(dropper, xInit, yInit);
}
public void sendSignal() {
public void send(Signal signal) {
if (_attachedTo != null) {
_attachedTo.sendSignal();
_attachedTo.send(signal);
}
}
}

View File

@ -1,10 +1,13 @@
package com.codigoparallevar.minicards.types;
package com.codigoparallevar.minicards.types.connectors.Wiring;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
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.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.wireData.WireDataType;

View File

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

View File

@ -3,7 +3,7 @@ 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.connectors.Wiring.Wire;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
public interface InputConnector<T extends WireDataType, T1 extends InputConnector<T, T1>>
@ -19,4 +19,6 @@ public interface InputConnector<T extends WireDataType, T1 extends InputConnecto
Part getPart();
String getId();
void send(T data);
}

View File

@ -3,5 +3,4 @@ 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,11 @@
package com.codigoparallevar.minicards.types.connectors.output;
import com.codigoparallevar.minicards.types.connectors.Wiring.BooleanWire;
import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector;
import com.codigoparallevar.minicards.types.wireData.BooleanSignal;
public interface BooleanOutputConnector extends OutputConnector<
BooleanSignal,
BooleanInputConnector,
BooleanWire> {
}

View File

@ -4,7 +4,7 @@ import com.codigoparallevar.minicards.ScrolledCanvas;
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.Wiring.Wire;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.wireData.WireDataType;
@ -21,4 +21,6 @@ public interface OutputConnector<T extends WireDataType,
List<Tuple2<String, String>> getConnectionEndpoints();
void connectTo(T1 inputConnector);
void send(T data);
}

View File

@ -1,9 +1,8 @@
package com.codigoparallevar.minicards.types.connectors.output;
import com.codigoparallevar.minicards.types.SignalWire;
import com.codigoparallevar.minicards.types.connectors.Wiring.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,9 @@
package com.codigoparallevar.minicards.types.wireData;
public class BooleanSignal implements WireDataType {
public final boolean value;
public BooleanSignal(boolean value) {
this.value = value;
}
}