Add wiring.
This commit is contained in:
commit
a7a9149502
@ -1,6 +1,5 @@
|
||||
package com.codigoparallevar.minicards;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
@ -13,11 +12,19 @@ import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import com.codigoparallevar.minicards.motion.MotionMode;
|
||||
import com.codigoparallevar.minicards.parts.Part;
|
||||
import com.codigoparallevar.minicards.parts.Placeholder;
|
||||
import com.codigoparallevar.minicards.types.PartConnection;
|
||||
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
|
||||
import com.codigoparallevar.minicards.parts.types.Position;
|
||||
import com.getbase.floatingactionbutton.FloatingActionsMenu;
|
||||
import com.codigoparallevar.minicards.parts.samples.ColorBox;
|
||||
import com.codigoparallevar.minicards.parts.samples.Placeholder;
|
||||
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;
|
||||
import com.codigoparallevar.minicards.types.Selectable;
|
||||
import com.codigoparallevar.minicards.types.Tuple2;
|
||||
import com.codigoparallevar.minicards.types.Tuple4;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@ -28,14 +35,19 @@ import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class CanvasView extends View {
|
||||
class CanvasView extends View implements PartGrid {
|
||||
|
||||
@NonNull
|
||||
ArrayList<Part> parts = new ArrayList<>();
|
||||
|
||||
@Nullable
|
||||
Part selectedPart;
|
||||
Selectable selectedPart;
|
||||
|
||||
@Nullable
|
||||
final Position lastTouchedPosition = new Position();
|
||||
@ -51,7 +63,7 @@ class CanvasView extends View {
|
||||
private final static float touchTimeForLongTouchInMillis = 500;
|
||||
private boolean _isDragging = false;
|
||||
private MainActivity parentActivity = null;
|
||||
private Tuple<Integer, Integer, Integer, Integer> _dropZone = new Tuple<>(0, 0, 0, 0);
|
||||
private Tuple4<Integer, Integer, Integer, Integer> _dropToRemoveZone = new Tuple4<>(0, 0, 0, 0);
|
||||
private boolean _devMode = false;
|
||||
|
||||
public CanvasView(Context context) {
|
||||
@ -68,8 +80,9 @@ class CanvasView extends View {
|
||||
this.setBackgroundColor(Color.rgb(4, 69, 99));
|
||||
|
||||
if (!loadState()){
|
||||
parts.add(new Placeholder(50, 50, 750, 500));
|
||||
parts.add(new RoundButton(500, 1200, 80, 100));
|
||||
parts.add(new Placeholder(this, 50, 50, 750, 500));
|
||||
parts.add(new ColorBox(this, 600, 1000, 700, 1100));
|
||||
parts.add(new RoundButton(this, 300, 1200, 80, 100));
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,46 +90,95 @@ class CanvasView extends View {
|
||||
File filesDir = getContext().getFilesDir();
|
||||
File file = new File(filesDir + "/" + name);
|
||||
FileReader fileIn = null;
|
||||
List<PartConnection> connections = new LinkedList<>();
|
||||
|
||||
try {
|
||||
fileIn = new FileReader(file);
|
||||
char[] data = new char[(int) file.length()];
|
||||
fileIn.read(data);
|
||||
JSONArray jsonParts = new JSONArray(new String(data ));
|
||||
for (int i = 0; i < jsonParts.length(); i++){
|
||||
deserializeObject(jsonParts.getJSONObject(i));
|
||||
connections.addAll(deserializeObject(jsonParts.getJSONObject(i)));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
parts.clear();
|
||||
Log.w("PartCanvasView", e.getMessage());
|
||||
Log.w("CanvasView", e.getMessage(), e);
|
||||
return false;
|
||||
} catch (JSONException e) {
|
||||
parts.clear();
|
||||
Log.w("PartCanvasView", e.getMessage());
|
||||
Log.w("CanvasView", e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
|
||||
resolveConnections(connections);
|
||||
|
||||
try {
|
||||
fileIn.close();
|
||||
} catch (IOException e) {
|
||||
Log.w("PartCanvasView", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void deserializeObject(JSONObject jsonObject) throws JSONException {
|
||||
private List<PartConnection> deserializeObject(JSONObject jsonObject) throws JSONException {
|
||||
String type = jsonObject.getString("_type");
|
||||
|
||||
if(type.equals(RoundButton.class.getName())) {
|
||||
parts.add(RoundButton.deserialize(jsonObject.getJSONObject("_data")));
|
||||
Tuple2<Part, List<PartConnection>> buttonInfo = RoundButton.deserialize(
|
||||
this,
|
||||
jsonObject.getJSONObject("_data"));
|
||||
|
||||
parts.add(buttonInfo.item1);
|
||||
return buttonInfo.item2;
|
||||
}
|
||||
else if (type.equals(Placeholder.class.getName())) {
|
||||
parts.add(Placeholder.deserialize(jsonObject.getJSONObject("_data")));
|
||||
parts.add(Placeholder.deserialize(this, jsonObject.getJSONObject("_data")));
|
||||
return Collections.emptyList();
|
||||
}
|
||||
else if (type.equals(ColorBox.class.getName())){
|
||||
parts.add(ColorBox.deserialize(this, jsonObject.getJSONObject("_data")));
|
||||
return Collections.emptyList();
|
||||
}
|
||||
else {
|
||||
throw new JSONException("Expected known class, found " + type);
|
||||
}
|
||||
}
|
||||
|
||||
private void resolveConnections(List<PartConnection> connections) {
|
||||
Map<String, Part> partsById = buildPartsById();
|
||||
for (PartConnection connection : connections){
|
||||
if (!partsById.containsKey(connection.inputPartId)){
|
||||
Log.e("Canvas view", "Key '" + connection.inputPartId
|
||||
+ "' not found on deserialization");
|
||||
continue;
|
||||
}
|
||||
|
||||
Part inputPart = partsById.get(connection.inputPartId);
|
||||
InputConnector inputConnector = inputPart.getConnectorWithId(connection.inputConnectorId);
|
||||
|
||||
if (inputConnector == null){
|
||||
Log.e("Canvas view", "Connector ID '" + connection.inputConnectorId
|
||||
+ "' not found on deserialization");
|
||||
continue;
|
||||
}
|
||||
|
||||
OutputConnector outputConnector = connection.outputConnector;
|
||||
outputConnector.connectTo(inputConnector);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Part> buildPartsById() {
|
||||
Map<String, Part> partsById = new HashMap<>(parts.size());
|
||||
for (Part part : parts) {
|
||||
partsById.put(part.getId(), part);
|
||||
Log.w("CanvasView", "Added part ID: " + part.getId() + " - " + part);
|
||||
}
|
||||
|
||||
return partsById;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas){
|
||||
final long renderStartTime = System.currentTimeMillis();
|
||||
@ -180,15 +242,21 @@ class CanvasView extends View {
|
||||
}
|
||||
if (motionMode != MotionMode.Type.LongTouch) {
|
||||
if (selectedPart != null){
|
||||
selectedPart.touched();
|
||||
if (selectedPart instanceof Part){
|
||||
((Part) selectedPart).touched();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (motionMode == MotionMode.Type.LongTouch) {
|
||||
if (selectedPart != null) {
|
||||
selectedPart.getMoveable().drop(x, y);
|
||||
|
||||
if (inDropZone(x, y)) {
|
||||
Log.d("Canvas", "Deleting element" + selectedPart);
|
||||
parts.remove(selectedPart);
|
||||
selectedPart.unlink();
|
||||
}
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
@ -210,16 +278,17 @@ class CanvasView extends View {
|
||||
}
|
||||
|
||||
Log.i("Canvas", "X: " + x + " Y: " + y
|
||||
+ " in drop zone " + _dropZone + " : " + inDropZone(x, y));
|
||||
+ " in drop zone " + _dropToRemoveZone + " : " + inDropZone(x, y));
|
||||
if (motionMode == null){
|
||||
final Part nowSelectedPart = getPartOn(x, y);
|
||||
final Selectable nowSelectedPart = getPartOn(x, y);
|
||||
final boolean canWait = selectedPart == nowSelectedPart;
|
||||
motionMode = getMotionMode(canWait);
|
||||
}
|
||||
if (motionMode == MotionMode.Type.LongTouch){
|
||||
if (selectedPart != null){
|
||||
_isDragging = true;
|
||||
selectedPart.move(x, y);
|
||||
selectedPart.getMoveable().moveEnd(x, y);
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -239,8 +308,8 @@ class CanvasView extends View {
|
||||
}
|
||||
|
||||
private boolean inDropZone(int x, int y) {
|
||||
return (x >= _dropZone._x1) && (x <= _dropZone._x2)
|
||||
&& (y >= _dropZone._y1) && (y <= _dropZone._y2);
|
||||
return (x >= _dropToRemoveZone._x1) && (x <= _dropToRemoveZone._x2)
|
||||
&& (y >= _dropToRemoveZone._y1) && (y <= _dropToRemoveZone._y2);
|
||||
}
|
||||
|
||||
private void saveState() throws IOException {
|
||||
@ -288,15 +357,50 @@ class CanvasView extends View {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Part getPartOn(int x, int y) {
|
||||
// Look in the list, in reverse so top-most elements are checked before
|
||||
public Selectable getPartOn(int x, int y) {
|
||||
// Look in the list of parts, in reverse so top-most elements are checked before
|
||||
for (int i = parts.size() - 1; i >= 0; i--){
|
||||
final Part part = parts.get(i);
|
||||
if ((x >= part.getLeft()) && (part.getRight() >= x)
|
||||
&& (y >= part.getTop()) && (part.getBottom() >= y)){
|
||||
if (part.containsPoint(x, y)){
|
||||
return part;
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
// First try with output connectors
|
||||
for (OutputConnector outputConnector : part.getOutputConnectors()){
|
||||
if (outputConnector.containsPoint(x, y)){
|
||||
return outputConnector;
|
||||
}
|
||||
}
|
||||
// Then with input ones
|
||||
for (InputConnector inputConnector : part.getSignalInputConnectors()){
|
||||
if (inputConnector.containsPoint(x, y)){
|
||||
return inputConnector;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
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 (SignalInputConnector inputConnector : part.getSignalInputConnectors()){
|
||||
if (inputConnector.containsPoint(x, y)){
|
||||
return inputConnector;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -314,31 +418,11 @@ class CanvasView extends View {
|
||||
}
|
||||
|
||||
public void setDropZone(float x1, float x2, float y1, float y2) {
|
||||
_dropZone = new Tuple<>((int) x1, (int) x2, (int) y1, (int) y2);
|
||||
_dropToRemoveZone = new Tuple4<>((int) x1, (int) x2, (int) y1, (int) y2);
|
||||
}
|
||||
|
||||
public void setDevMode(boolean devMode) {
|
||||
_devMode = devMode;
|
||||
this.invalidate();
|
||||
}
|
||||
|
||||
private class Tuple<T, T1, T2, T3> {
|
||||
|
||||
final T _x1;
|
||||
final T1 _x2;
|
||||
final T2 _y1;
|
||||
final T3 _y2;
|
||||
|
||||
public Tuple(T x1, T1 x2, T2 y1, T3 y2) {
|
||||
_x1 = x1;
|
||||
_x2 = x2;
|
||||
_y1 = y1;
|
||||
_y2 = y2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "(" + _x1 + " - " + _x2 + ", " + _y1 + " - " + _y2 + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.codigoparallevar.minicards;
|
||||
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
@ -10,6 +9,8 @@ import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
|
||||
import com.codigoparallevar.minicards.parts.samples.ColorBox;
|
||||
import com.codigoparallevar.minicards.types.Part;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@ -58,7 +59,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (canvasView != null) {
|
||||
canvasView.addPart(new RoundButton(500, 500, 50, 100));
|
||||
Part buttonPart = new RoundButton(
|
||||
canvasView,
|
||||
500, 1200,
|
||||
80, 100);
|
||||
canvasView.addPart(buttonPart);
|
||||
|
||||
Part boxPart = new ColorBox(canvasView,
|
||||
400, 1100,
|
||||
500, 1200);
|
||||
canvasView.addPart(boxPart);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -1,21 +0,0 @@
|
||||
package com.codigoparallevar.minicards.parts;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public interface Part {
|
||||
int getLeft();
|
||||
int getRight();
|
||||
int getTop();
|
||||
int getBottom();
|
||||
|
||||
void draw(Canvas canvas, boolean devMode);
|
||||
|
||||
void move(int x, int y);
|
||||
|
||||
void touched();
|
||||
|
||||
JSONObject serialize() throws JSONException;
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
package com.codigoparallevar.minicards.parts;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Placeholder implements Part {
|
||||
private int _left;
|
||||
private int _top;
|
||||
private int _right;
|
||||
private int _bottom;
|
||||
|
||||
public Placeholder(int left, int top, int right, int bottom) {
|
||||
_left = left;
|
||||
_top = top;
|
||||
_right = right;
|
||||
_bottom = bottom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeft() {
|
||||
return _left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRight() {
|
||||
return _right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTop() {
|
||||
return _top;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBottom() {
|
||||
return _bottom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, boolean devMode) {
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
paint.setColor(Color.WHITE);
|
||||
|
||||
// Top
|
||||
canvas.drawLine(_left, _top, _right, _top, paint);
|
||||
// Bottom
|
||||
canvas.drawLine(_left, _bottom, _right, _bottom, paint);
|
||||
// Left
|
||||
canvas.drawLine(_left, _top, _left, _bottom, paint);
|
||||
// Right
|
||||
canvas.drawLine(_right, _top, _right, _bottom, paint);
|
||||
// Cross, top-left, bottom-right
|
||||
canvas.drawLine(_left, _top, _right, _bottom, paint);
|
||||
// Cross, top-right, bottom-left
|
||||
canvas.drawLine(_right, _top, _left, _bottom, paint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void touched() {
|
||||
Log.d("Placeholder", "Placeholder touched");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialize() throws JSONException {
|
||||
JSONObject serialized = new JSONObject();
|
||||
|
||||
serialized.put("left", _left);
|
||||
serialized.put("top", _top);
|
||||
serialized.put("right", _right);
|
||||
serialized.put("bottom", _bottom);
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
public static Part deserialize(JSONObject data) throws JSONException {
|
||||
int left = data.getInt("left");
|
||||
int top = data.getInt("top");
|
||||
int right = data.getInt("right");
|
||||
int bottom = data.getInt("bottom");
|
||||
|
||||
return new Placeholder(left, top, right, bottom);
|
||||
}
|
||||
}
|
@ -5,25 +5,63 @@ import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.util.Log;
|
||||
|
||||
import com.codigoparallevar.minicards.parts.Part;
|
||||
import com.codigoparallevar.minicards.types.PartConnection;
|
||||
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.utils.Serializations;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RoundButton implements Part {
|
||||
|
||||
|
||||
private final String _id;
|
||||
private final PartGrid _partGrid;
|
||||
private int _xCenter;
|
||||
private int _yCenter;
|
||||
private final int _innerRadius;
|
||||
private final int _outerRadius;
|
||||
private final int _outerRadiusThickness = 10;
|
||||
private final int _pathRunWay = 200;
|
||||
private List<OutputConnector> _outputConnectors;
|
||||
private final RoundOutputConnector _pressedOuputConnector;
|
||||
|
||||
public RoundButton(int xCenter, int yCenter, int innerRadius, int outerRadius) {
|
||||
|
||||
private RoundButton(String id, PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) {
|
||||
_id = id;
|
||||
|
||||
_partGrid = partGrid;
|
||||
_xCenter = xCenter;
|
||||
_yCenter = yCenter;
|
||||
_innerRadius = innerRadius;
|
||||
_outerRadius = outerRadius;
|
||||
|
||||
// Create connectors
|
||||
_pressedOuputConnector = new RoundOutputConnector(
|
||||
this,
|
||||
_partGrid,
|
||||
getOutputConnectorCenterX(), getOutputConnectorCenterY(),
|
||||
getOutputConnectRadius());
|
||||
|
||||
_outputConnectors = new LinkedList<>();
|
||||
_outputConnectors.add(_pressedOuputConnector);
|
||||
}
|
||||
|
||||
public RoundButton(PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) {
|
||||
this(UUID.randomUUID().toString(), partGrid, xCenter, yCenter, innerRadius, outerRadius);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,42 +92,160 @@ public class RoundButton implements Part {
|
||||
Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
backgroundPaint.setColor(Color.BLACK);
|
||||
|
||||
if (devMode){
|
||||
drawConnector(canvas);
|
||||
drawWires(canvas, devMode);
|
||||
}
|
||||
|
||||
canvas.drawCircle(_xCenter, _yCenter, _outerRadius, foregroundPaint);
|
||||
canvas.drawCircle(_xCenter, _yCenter, _outerRadius - _outerRadiusThickness, backgroundPaint);
|
||||
canvas.drawCircle(_xCenter, _yCenter, _innerRadius, foregroundPaint);
|
||||
}
|
||||
|
||||
private void drawConnector(Canvas canvas) {
|
||||
Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
connectorPaint.setColor(Color.RED);
|
||||
|
||||
canvas.drawCircle(getOutputConnectorCenterX(), getOutputConnectorCenterY(),
|
||||
getOutputConnectRadius(),
|
||||
connectorPaint);
|
||||
}
|
||||
|
||||
private int getOutputConnectorCenterX() {
|
||||
return _xCenter + _outerRadius;
|
||||
}
|
||||
|
||||
private int getOutputConnectorCenterY() {
|
||||
return _yCenter;
|
||||
}
|
||||
|
||||
private int getOutputConnectRadius() {
|
||||
return _innerRadius;
|
||||
}
|
||||
|
||||
private void drawWires(Canvas canvas, boolean devMode) {
|
||||
for (OutputConnector outputConnector : _outputConnectors){
|
||||
outputConnector.drawWires(canvas, devMode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move(int x, int y) {
|
||||
public void moveEnd(int x, int y) {
|
||||
_xCenter = x;
|
||||
_yCenter = y;
|
||||
|
||||
// Move connectors too
|
||||
_pressedOuputConnector.updatePosition(
|
||||
getOutputConnectorCenterX(),
|
||||
getOutputConnectorCenterY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(int x, int y) {
|
||||
moveEnd(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void touched() {
|
||||
Log.d("RoundButton", "Round button touched");
|
||||
|
||||
_pressedOuputConnector.sendSignal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SignalInputConnector> getSignalInputConnectors() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OutputConnector> getOutputConnectors() {
|
||||
return _outputConnectors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialize() throws JSONException {
|
||||
JSONObject serialized = new JSONObject();
|
||||
|
||||
serialized.put("id", _id);
|
||||
serialized.put("x_center", _xCenter);
|
||||
serialized.put("y_center", _yCenter);
|
||||
serialized.put("inner_radius", _innerRadius);
|
||||
serialized.put("outer_radius", _outerRadius);
|
||||
serialized.put("outer_radius_thickness", _outerRadiusThickness);
|
||||
serialized.put("on_pressed_output_connector",
|
||||
Serializations.serialize(serializeConnectionEndpoints()));
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
public static Part deserialize(JSONObject data) throws JSONException {
|
||||
private List<Map<String, String>> serializeConnectionEndpoints() {
|
||||
List<Map<String, String>> serializedData = new LinkedList<>();
|
||||
|
||||
for (Tuple2<String, String> endpoint : _pressedOuputConnector.getConnectionEndpoints()){
|
||||
serializedData.add(PartConnection.serialize(endpoint.item1, endpoint.item2));
|
||||
}
|
||||
|
||||
return serializedData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSignal(RoundInputConnector roundInputConnector) {
|
||||
// @TODO: REMOVE THE NEED FOR THIS
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return _id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputConnector getConnectorWithId(String inputConnectorId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConnectorId(InputConnector inputConnector) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Tuple2<Part, List<PartConnection>> deserialize(PartGrid partGrid, JSONObject data) throws JSONException {
|
||||
String id = data.getString("id");
|
||||
int xCenter = data.getInt("x_center");
|
||||
int yCenter = data.getInt("y_center");
|
||||
|
||||
int innerRadius = data.getInt("inner_radius");
|
||||
int outerRadius = data.getInt("outer_radius");
|
||||
|
||||
return new RoundButton(xCenter, yCenter, innerRadius, outerRadius);
|
||||
RoundButton button = new RoundButton(id, partGrid, xCenter, yCenter, innerRadius, outerRadius);
|
||||
|
||||
List<PartConnection> connections = new LinkedList<>();
|
||||
|
||||
JSONArray connectorOuts = data.getJSONArray("on_pressed_output_connector");
|
||||
for (int i = 0; i < connectorOuts.length(); i++){
|
||||
connections.add(PartConnection.deserialize(
|
||||
button._pressedOuputConnector,
|
||||
connectorOuts.getJSONObject(i)));
|
||||
}
|
||||
|
||||
return new Tuple2<Part, List<PartConnection>>(button, connections);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPoint(int x, int y) {
|
||||
return ((Math.abs(x - _xCenter) <= _outerRadius)
|
||||
&& (Math.abs(y - _yCenter) <= _outerRadius));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Moveable getMoveable() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void unlink() {
|
||||
for (InputConnector input : getSignalInputConnectors()) {
|
||||
input.unlink();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,231 @@
|
||||
package com.codigoparallevar.minicards.parts.samples;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.util.Log;
|
||||
|
||||
import com.codigoparallevar.minicards.types.Wire;
|
||||
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
|
||||
import com.codigoparallevar.minicards.types.Moveable;
|
||||
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
|
||||
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 org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ColorBox implements Part {
|
||||
private static final String TOGGLE_INPUT_CONNECTOR_ID = "toggle_input_connector";
|
||||
private final String _id;
|
||||
private final PartGrid _partGrid;
|
||||
private boolean _activated;
|
||||
private int _left;
|
||||
private int _top;
|
||||
private int _right;
|
||||
private int _bottom;
|
||||
private List<SignalInputConnector> inputConnectors;
|
||||
private SignalInputConnector _toggleInputConnector;
|
||||
|
||||
private ColorBox(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;
|
||||
|
||||
_toggleInputConnector = new RoundInputConnector(
|
||||
this,
|
||||
getInputConnectorCenterX(),
|
||||
getInputConnectorCenterY(),
|
||||
getInputConnectRadius());
|
||||
inputConnectors = new LinkedList<>();
|
||||
inputConnectors.add(_toggleInputConnector);
|
||||
|
||||
}
|
||||
|
||||
public ColorBox(PartGrid partGrid, int left, int top, int right, int bottom) {
|
||||
this(UUID.randomUUID().toString(), partGrid, left, top, right, bottom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeft() {
|
||||
return _left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRight() {
|
||||
return _right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTop() {
|
||||
return _top;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBottom() {
|
||||
return _bottom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, boolean devMode) {
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
if (_activated){
|
||||
paint.setColor(Color.GREEN);
|
||||
}
|
||||
else {
|
||||
paint.setColor(Color.RED);
|
||||
}
|
||||
|
||||
if (devMode){
|
||||
drawConnector(canvas);
|
||||
// drawWires(canvas, devMode);
|
||||
}
|
||||
|
||||
|
||||
canvas.drawRect(
|
||||
new Rect(_left, _top,
|
||||
_right, _bottom),
|
||||
paint);
|
||||
}
|
||||
|
||||
private void drawConnector(Canvas canvas) {
|
||||
Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
connectorPaint.setColor(Color.YELLOW);
|
||||
|
||||
canvas.drawCircle(
|
||||
getInputConnectorCenterX(), getInputConnectorCenterY(),
|
||||
getInputConnectRadius(),
|
||||
connectorPaint);
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(int x, int y) {
|
||||
moveEnd(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void touched() {
|
||||
Log.d("Placeholder", "Placeholder touched");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SignalInputConnector> getSignalInputConnectors() {
|
||||
return inputConnectors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OutputConnector> getOutputConnectors() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSignal(RoundInputConnector roundInputConnector) {
|
||||
_activated = !_activated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return _id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputConnector getConnectorWithId(String inputConnectorId) {
|
||||
switch (inputConnectorId){
|
||||
case ColorBox.TOGGLE_INPUT_CONNECTOR_ID:
|
||||
return _toggleInputConnector;
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConnectorId(InputConnector inputConnector) {
|
||||
if (inputConnector == _toggleInputConnector){
|
||||
return ColorBox.TOGGLE_INPUT_CONNECTOR_ID;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Part 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");
|
||||
|
||||
return new ColorBox(id, partGrid, left, top, right, bottom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPoint(int x, int y) {
|
||||
return (x >= getLeft()) && (x <= getRight())
|
||||
&& (y >= getTop()) && (y <= getBottom());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Moveable getMoveable() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlink() {
|
||||
for (InputConnector input : getSignalInputConnectors()) {
|
||||
input.unlink();
|
||||
}
|
||||
}
|
||||
|
||||
public int getInputConnectorCenterX() {
|
||||
return getLeft();
|
||||
}
|
||||
|
||||
private int getInputConnectRadius() {
|
||||
return (getRight() - getLeft()) / 2;
|
||||
}
|
||||
|
||||
public int getInputConnectorCenterY() {
|
||||
return (getTop() + getBottom()) / 2;
|
||||
}
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
package com.codigoparallevar.minicards.parts.samples;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.util.Log;
|
||||
|
||||
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 org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Placeholder implements Part {
|
||||
private final String _id;
|
||||
private final PartGrid _partGrid;
|
||||
private int _left;
|
||||
private int _top;
|
||||
private int _right;
|
||||
private int _bottom;
|
||||
|
||||
public Placeholder(String id, PartGrid partGrid, int left, int top, int right, int bottom) {
|
||||
_id = id;
|
||||
_partGrid = partGrid;
|
||||
_left = left;
|
||||
_top = top;
|
||||
_right = right;
|
||||
_bottom = bottom;
|
||||
}
|
||||
|
||||
public Placeholder(PartGrid partGrid, int left, int top, int right, int bottom) {
|
||||
this(UUID.randomUUID().toString(), partGrid, left, top, right, bottom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLeft() {
|
||||
return _left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRight() {
|
||||
return _right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTop() {
|
||||
return _top;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBottom() {
|
||||
return _bottom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, boolean devMode) {
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
paint.setColor(Color.WHITE);
|
||||
|
||||
// Top
|
||||
canvas.drawLine(_left, _top, _right, _top, paint);
|
||||
// Bottom
|
||||
canvas.drawLine(_left, _bottom, _right, _bottom, paint);
|
||||
// Left
|
||||
canvas.drawLine(_left, _top, _left, _bottom, paint);
|
||||
// Right
|
||||
canvas.drawLine(_right, _top, _right, _bottom, paint);
|
||||
// Cross, top-left, bottom-right
|
||||
canvas.drawLine(_left, _top, _right, _bottom, paint);
|
||||
// Cross, top-right, bottom-left
|
||||
canvas.drawLine(_right, _top, _left, _bottom, paint);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(int x, int y) {
|
||||
moveEnd(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void touched() {
|
||||
Log.d("Placeholder", "Placeholder touched");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SignalInputConnector> getSignalInputConnectors() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OutputConnector> getOutputConnectors() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
return serialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSignal(RoundInputConnector roundInputConnector) {
|
||||
// @TODO: REMOVE THE NEED FOR THIS
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return _id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputConnector getConnectorWithId(String inputConnectorId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConnectorId(InputConnector inputConnector) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Part 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");
|
||||
|
||||
return new Placeholder(id, partGrid, left, top, right, bottom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPoint(int x, int y) {
|
||||
return (x >= getLeft()) && (x <= getRight())
|
||||
&& (y >= getTop()) && (y <= getBottom());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Moveable getMoveable() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlink() {
|
||||
for (InputConnector input : getSignalInputConnectors()) {
|
||||
input.unlink();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
|
||||
public interface Drawable {
|
||||
void draw(Canvas canvas, boolean devMode);
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
public interface Dropper<T extends Wire> {
|
||||
void drop(T wire);
|
||||
void unlinkWire(T wire);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
public interface Moveable {
|
||||
void moveEnd(int x, int y);
|
||||
|
||||
void drop(int x, int y);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
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 org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Part extends Selectable, Moveable, Drawable {
|
||||
int getLeft();
|
||||
int getRight();
|
||||
int getTop();
|
||||
int getBottom();
|
||||
|
||||
void touched();
|
||||
|
||||
List<SignalInputConnector> getSignalInputConnectors();
|
||||
List<OutputConnector> getOutputConnectors();
|
||||
|
||||
JSONObject serialize() throws JSONException;
|
||||
|
||||
void sendSignal(RoundInputConnector roundInputConnector);
|
||||
|
||||
String getId();
|
||||
|
||||
InputConnector getConnectorWithId(String inputConnectorId);
|
||||
|
||||
String getConnectorId(InputConnector inputConnector);
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PartConnection {
|
||||
private static final String INPUT_CONNECTOR_ID_KEY = "input_connector_id";
|
||||
private static final String INPUT_PART_ID_KEY = "input_part_id";
|
||||
public final OutputConnector outputConnector;
|
||||
public final String inputPartId;
|
||||
public final String inputConnectorId;
|
||||
|
||||
|
||||
public PartConnection(OutputConnector outputConnector, String inputPartId,
|
||||
String inputConnectorId) {
|
||||
this.outputConnector = outputConnector;
|
||||
this.inputPartId = inputPartId;
|
||||
this.inputConnectorId = inputConnectorId;
|
||||
}
|
||||
|
||||
public static PartConnection deserialize(OutputConnector outputConnector, JSONObject metadata) throws JSONException {
|
||||
String inputConnectorId = metadata.getString(PartConnection.INPUT_CONNECTOR_ID_KEY);
|
||||
String inputPartId = metadata.getString(PartConnection.INPUT_PART_ID_KEY);
|
||||
|
||||
return new PartConnection(outputConnector, inputPartId, inputConnectorId);
|
||||
}
|
||||
|
||||
public static Map<String, String> serialize(final String inputConnectorId, final String inputPartId) {
|
||||
|
||||
Map<String, String> serialized = new LinkedHashMap<>(2);
|
||||
serialized.put(PartConnection.INPUT_CONNECTOR_ID_KEY, inputConnectorId);
|
||||
serialized.put(PartConnection.INPUT_PART_ID_KEY, inputPartId);
|
||||
|
||||
return serialized;
|
||||
}
|
||||
}
|
@ -0,0 +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);
|
||||
|
||||
SignalInputConnector getSignalInputConnectorOn(int x, int y);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.codigoparallevar.minicards.parts.types;
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
public class Position {
|
||||
private int _x;
|
@ -0,0 +1,95 @@
|
||||
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 SignalInputConnector {
|
||||
private final Part _part;
|
||||
private int _xposition;
|
||||
private int _yposition;
|
||||
private final int _radius;
|
||||
private final List<Wire> _attachments = new LinkedList<>();
|
||||
|
||||
public RoundInputConnector(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 sendSignal() {
|
||||
_part.sendSignal(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void getAttachment(Wire<Signal, SignalInputConnector> 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
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.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class RoundOutputConnector implements Drawable, SignalOutputConnector {
|
||||
private PartGrid _partGrid;
|
||||
private int _centerX;
|
||||
private int _centerY;
|
||||
private final int _radius;
|
||||
private final Part _part;
|
||||
private SignalWire _currentWire = null;
|
||||
private final List<SignalWire> _wires;
|
||||
private final HashSet<InputConnector> _connections;
|
||||
|
||||
public RoundOutputConnector(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 SignalWire(this, _centerX, _centerY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(SignalWire wire) {
|
||||
|
||||
if (wire == _currentWire){
|
||||
_currentWire = null;
|
||||
|
||||
SignalInputConnector resultPoint = _partGrid.getSignalInputConnectorOn(
|
||||
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(SignalWire wire) {
|
||||
_wires.remove(wire);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawWires(Canvas canvas, boolean devMode) {
|
||||
for (SignalWire 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 (SignalWire wire : _wires){
|
||||
wire.moveStart(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tuple2<String, String>> getConnectionEndpoints() {
|
||||
List<Tuple2<String, String>> endpointIds = new LinkedList<>();
|
||||
|
||||
for (SignalWire wire : _wires) {
|
||||
InputConnector inputConnector = wire.getAttachedTo();
|
||||
Part endPart = inputConnector.getPart();
|
||||
endpointIds.add(new Tuple2<>(inputConnector.getId(), endPart.getId()));
|
||||
}
|
||||
|
||||
return endpointIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectTo(SignalInputConnector inputConnector) {
|
||||
if (_connections.contains(inputConnector)) {
|
||||
return;
|
||||
}
|
||||
|
||||
_connections.add(inputConnector);
|
||||
|
||||
SignalWire wire = new SignalWire(this, _centerX, _centerY);
|
||||
wire.attachTo(inputConnector);
|
||||
_wires.add(wire);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, boolean devMode) {
|
||||
// TODO: Complete this part
|
||||
}
|
||||
|
||||
public void sendSignal() {
|
||||
for (SignalWire wire : _wires){
|
||||
wire.sendSignal();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
public interface Selectable {
|
||||
boolean containsPoint(int x, int y);
|
||||
Moveable getMoveable();
|
||||
void unlink();
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
public class Tuple2<T1, T2> {
|
||||
public final T1 item1;
|
||||
public final T2 item2;
|
||||
|
||||
public Tuple2(T1 item1, T2 item2) {
|
||||
this.item1 = item1;
|
||||
this.item2 = item2;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
public class Tuple4<T, T1, T2, T3> {
|
||||
|
||||
public final T _x1;
|
||||
public final T1 _x2;
|
||||
public final T2 _y1;
|
||||
public final T3 _y2;
|
||||
|
||||
public Tuple4(T x1, T1 x2, T2 y1, T3 y2) {
|
||||
_x1 = x1;
|
||||
_x2 = x2;
|
||||
_y1 = y1;
|
||||
_y2 = y2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "(" + _x1 + " - " + _x2 + ", " + _y1 + " - " + _y2 + ")";
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package com.codigoparallevar.minicards.types;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
|
||||
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;
|
||||
protected InputConnectorType _attachedTo = null;
|
||||
|
||||
public Wire(Dropper dropper, int xInit, int yInit) {
|
||||
_dropper = dropper;
|
||||
_xinit = xInit;
|
||||
_yinit = yInit;
|
||||
|
||||
_xend = xInit;
|
||||
_yend = yInit;
|
||||
}
|
||||
|
||||
public void moveStart(int x, int y){
|
||||
_xinit = x;
|
||||
_yinit = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveEnd(int x, int y) {
|
||||
_xend = x;
|
||||
_yend = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(int x, int y) {
|
||||
_dropper.drop(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, boolean devMode) {
|
||||
final Path samplePath = new Path();
|
||||
|
||||
samplePath.moveTo(_xinit, _yinit);
|
||||
samplePath.cubicTo(
|
||||
_xinit + _pathRunWay, _yinit,
|
||||
_xend - _pathRunWay, _yend,
|
||||
_xend, _yend);
|
||||
|
||||
Paint pathPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
pathPaint.setColor(Color.GREEN);
|
||||
pathPaint.setStrokeWidth(5.0f);
|
||||
pathPaint.setStrokeCap(Paint.Cap.ROUND);
|
||||
pathPaint.setStyle(Paint.Style.STROKE);
|
||||
|
||||
canvas.drawPath(samplePath, pathPaint);
|
||||
}
|
||||
|
||||
public int getXEnd() {
|
||||
return _xend;
|
||||
}
|
||||
|
||||
public int getYEnd() {
|
||||
return _yend;
|
||||
}
|
||||
|
||||
public void attachTo(InputConnectorType resultPoint) {
|
||||
_attachedTo = resultPoint;
|
||||
_xend = resultPoint.getX();
|
||||
_yend = resultPoint.getY();
|
||||
resultPoint.getAttachment(this);
|
||||
}
|
||||
|
||||
public InputConnector getAttachedTo() {
|
||||
return _attachedTo;
|
||||
}
|
||||
|
||||
public void unlink() {
|
||||
_attachedTo = null;
|
||||
_dropper.unlinkWire(this);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
}
|
@ -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);
|
||||
}
|
@ -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();
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.codigoparallevar.minicards.types.wireData;
|
||||
|
||||
public class Signal implements WireDataType {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.codigoparallevar.minicards.types.wireData;
|
||||
|
||||
public interface WireDataType {
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.codigoparallevar.minicards.utils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Serializations {
|
||||
|
||||
public static JSONArray serialize(List<Map<String,String>> data) {
|
||||
JSONArray array = new JSONArray();
|
||||
for (Map<String, String> dict : data) {
|
||||
array.put(new JSONObject(dict));
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.0.0-alpha5'
|
||||
classpath 'com.android.tools.build:gradle:3.0.0-beta2'
|
||||
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
|
Loading…
Reference in New Issue
Block a user