Add signal passing prototype.

This commit is contained in:
kenkeiras 2017-07-22 16:31:21 -04:00
parent 888e32f7ec
commit dcbc0e5cad
14 changed files with 415 additions and 41 deletions

View File

@ -13,10 +13,12 @@ import android.view.View;
import com.codigoparallevar.minicards.motion.MotionMode;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.parts.samples.Placeholder;
import com.codigoparallevar.minicards.types.InputConnector;
import com.codigoparallevar.minicards.types.OutputConnector;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Position;
import com.codigoparallevar.minicards.types.Selectable;
@ -30,7 +32,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class CanvasView extends View {
class CanvasView extends View implements PartGrid {
@NonNull
ArrayList<Part> parts = new ArrayList<>();
@ -52,7 +54,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 Tuple<Integer, Integer, Integer, Integer> _dropToRemoveZone = new Tuple<>(0, 0, 0, 0);
private boolean _devMode = false;
public CanvasView(Context context) {
@ -69,8 +71,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, 250, 250, 100, 100));
parts.add(new RoundButton(this, 500, 1200, 80, 100));
}
}
@ -108,10 +111,13 @@ class CanvasView extends View {
private void deserializeObject(JSONObject jsonObject) throws JSONException {
String type = jsonObject.getString("_type");
if(type.equals(RoundButton.class.getName())) {
parts.add(RoundButton.deserialize(jsonObject.getJSONObject("_data")));
parts.add(RoundButton.deserialize(this, jsonObject.getJSONObject("_data")));
}
else if (type.equals(Placeholder.class.getName())) {
parts.add(Placeholder.deserialize(jsonObject.getJSONObject("_data")));
parts.add(Placeholder.deserialize(this, jsonObject.getJSONObject("_data")));
}
else if (type.equals(ColorBox.class.getName())){
parts.add(ColorBox.deserialize(this, jsonObject.getJSONObject("_data")));
}
else {
throw new JSONException("Expected known class, found " + type);
@ -216,7 +222,7 @@ 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 Selectable nowSelectedPart = getPartOn(x, y);
final boolean canWait = selectedPart == nowSelectedPart;
@ -225,7 +231,7 @@ class CanvasView extends View {
if (motionMode == MotionMode.Type.LongTouch){
if (selectedPart != null){
_isDragging = true;
selectedPart.getMoveable().move(x, y);
selectedPart.getMoveable().moveEnd(x, y);
invalidate();
}
}
@ -246,8 +252,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 {
@ -295,7 +301,7 @@ class CanvasView extends View {
}
@Nullable
private Selectable getPartOn(int x, int y) {
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);
@ -324,6 +330,24 @@ class CanvasView extends View {
return null;
}
@Override
@Nullable
public InputConnector getInputConnectorOn(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.containsPoint(x, y)){
return inputConnector;
}
}
}
return null;
}
public void addPart(Part part) {
parts.add(part);
invalidate();
@ -338,7 +362,7 @@ 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 Tuple<>((int) x1, (int) x2, (int) y1, (int) y2);
}
public void setDevMode(boolean devMode) {

View File

@ -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,7 @@ 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 {
@ -59,10 +59,16 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
if (canvasView != null) {
Part part = new RoundButton(
Part buttonPart = new RoundButton(
canvasView,
500, 1200,
80, 100);
canvasView.addPart(part);
canvasView.addPart(buttonPart);
Part boxPart = new ColorBox(canvasView,
400, 1100,
500, 1200);
canvasView.addPart(boxPart);
}
}
});

View File

@ -3,13 +3,14 @@ package com.codigoparallevar.minicards.parts.buttons;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import com.codigoparallevar.minicards.types.InputConnector;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.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 org.json.JSONException;
@ -21,6 +22,7 @@ import java.util.List;
public class RoundButton implements Part {
private final PartGrid _partGrid;
private int _xCenter;
private int _yCenter;
private final int _innerRadius;
@ -28,22 +30,24 @@ public class RoundButton implements Part {
private final int _outerRadiusThickness = 10;
private final int _pathRunWay = 200;
private List<OutputConnector> _outputConnectors;
private final RoundOutputConnector pressedOuputConnector;
private final RoundOutputConnector _pressedOuputConnector;
public RoundButton(int xCenter, int yCenter, int innerRadius, int outerRadius) {
public RoundButton(PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) {
_partGrid = partGrid;
_xCenter = xCenter;
_yCenter = yCenter;
_innerRadius = innerRadius;
_outerRadius = outerRadius;
// Create connectors
pressedOuputConnector = new RoundOutputConnector(
_pressedOuputConnector = new RoundOutputConnector(
this,
_partGrid,
getOutputConnectorCenterX(), getOutputConnectorCenterY(),
getOutputConnectRadius());
_outputConnectors = new LinkedList<>();
_outputConnectors.add(pressedOuputConnector);
_outputConnectors.add(_pressedOuputConnector);
}
@ -103,7 +107,7 @@ public class RoundButton implements Part {
}
private int getOutputConnectRadius() {
return _innerRadius / 2;
return _innerRadius;
}
private void drawWires(Canvas canvas, boolean devMode) {
@ -113,24 +117,26 @@ public class RoundButton implements Part {
}
@Override
public void move(int x, int y) {
public void moveEnd(int x, int y) {
_xCenter = x;
_yCenter = y;
// Move connectors too
pressedOuputConnector.updatePosition(
_pressedOuputConnector.updatePosition(
getOutputConnectorCenterX(),
getOutputConnectorCenterY());
}
@Override
public void drop(int x, int y) {
move(x, y);
moveEnd(x, y);
}
@Override
public void touched() {
Log.d("RoundButton", "Round button touched");
_pressedOuputConnector.sendSignal();
}
@Override
@ -156,14 +162,19 @@ public class RoundButton implements Part {
return serialized;
}
public static Part deserialize(JSONObject data) throws JSONException {
@Override
public void sendSignal(RoundInputConnector roundInputConnector) {
// @TODO: REMOVE THE NEED FOR THIS
}
public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException {
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);
return new RoundButton(partGrid, xCenter, yCenter, innerRadius, outerRadius);
}
@Override

View File

@ -0,0 +1,186 @@
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.InputConnector;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.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;
public class ColorBox implements Part {
private final PartGrid _partGrid;
private boolean _activated;
private int _left;
private int _top;
private int _right;
private int _bottom;
private List<InputConnector> inputConnectors;
private InputConnector _toggleInputConnector;
public ColorBox(PartGrid partGrid, int left, int top, int right, int bottom) {
_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);
}
@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<InputConnector> getInputConnectors() {
return inputConnectors;
}
@Override
public List<OutputConnector> getOutputConnectors() {
return Collections.emptyList();
}
@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;
}
@Override
public void sendSignal(RoundInputConnector roundInputConnector) {
_activated = !_activated;
}
public static Part deserialize(PartGrid partGrid, 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 ColorBox(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;
}
public int getInputConnectorCenterX() {
return getLeft();
}
private int getInputConnectRadius() {
return (getRight() - getLeft()) / 2;
}
public int getInputConnectorCenterY() {
return (getTop() + getBottom()) / 2;
}
}

View File

@ -9,6 +9,8 @@ import com.codigoparallevar.minicards.types.InputConnector;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.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;
@ -17,12 +19,14 @@ import java.util.Collections;
import java.util.List;
public class Placeholder implements Part {
private final PartGrid _partGrid;
private int _left;
private int _top;
private int _right;
private int _bottom;
public Placeholder(int left, int top, int right, int bottom) {
public Placeholder(PartGrid partGrid, int left, int top, int right, int bottom) {
_partGrid = partGrid;
_left = left;
_top = top;
_right = right;
@ -69,7 +73,7 @@ public class Placeholder implements Part {
}
@Override
public void move(int x, int y) {
public void moveEnd(int x, int y) {
final int width = _right - _left;
final int height = _bottom - _top;
@ -82,7 +86,7 @@ public class Placeholder implements Part {
@Override
public void drop(int x, int y) {
move(x, y);
moveEnd(x, y);
}
@Override
@ -112,13 +116,18 @@ public class Placeholder implements Part {
return serialized;
}
public static Part deserialize(JSONObject data) throws JSONException {
@Override
public void sendSignal(RoundInputConnector roundInputConnector) {
// @TODO: REMOVE THE NEED FOR THIS
}
public static Part deserialize(PartGrid partGrid, 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);
return new Placeholder(partGrid, left, top, right, bottom);
}
@Override

View File

@ -0,0 +1,5 @@
package com.codigoparallevar.minicards.types;
interface Dropper {
void drop(Wire wire);
}

View File

@ -1,5 +1,12 @@
package com.codigoparallevar.minicards.types;
public interface InputConnector extends Selectable {
public interface InputConnector extends Selectable, Dropper {
void updatePosition(int x, int y);
int getX();
int getY();
void sendSignal();
void getAttachment(Wire wire);
}

View File

@ -1,7 +1,7 @@
package com.codigoparallevar.minicards.types;
public interface Moveable {
void move(int x, int y);
void moveEnd(int x, int y);
void drop(int x, int y);
}

View File

@ -2,8 +2,7 @@ package com.codigoparallevar.minicards.types;
import android.graphics.Canvas;
public interface OutputConnector extends Selectable {
void drop(Wire wire);
public interface OutputConnector extends Selectable, Dropper {
void drawWires(Canvas canvas, boolean devMode);
void updatePosition(int x, int y);
}

View File

@ -19,4 +19,6 @@ public interface Part extends Selectable, Moveable, Drawable {
List<OutputConnector> getOutputConnectors();
JSONObject serialize() throws JSONException;
void sendSignal(RoundInputConnector roundInputConnector);
}

View File

@ -0,0 +1,7 @@
package com.codigoparallevar.minicards.types;
public interface PartGrid {
Selectable getPartOn(int x, int y);
InputConnector getInputConnectorOn(int x, int y);
}

View File

@ -0,0 +1,69 @@
package com.codigoparallevar.minicards.types;
import android.util.Log;
import java.util.LinkedList;
import java.util.List;
public class RoundInputConnector implements InputConnector {
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 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 wire) {
_attachments.add(wire);
}
@Override
public void drop(Wire wire) {
Log.d("InputConnector", "Dropped wire " + wire);
}
}

View File

@ -7,6 +7,7 @@ import java.util.LinkedList;
import java.util.List;
public class RoundOutputConnector implements OutputConnector, Drawable {
private PartGrid _partGrid;
private int _centerX;
private int _centerY;
private final int _radius;
@ -14,8 +15,9 @@ public class RoundOutputConnector implements OutputConnector, Drawable {
private Wire _currentWire = null;
private final List<Wire> _wires;
public RoundOutputConnector(Part part, int centerX, int centerY, int radius) {
public RoundOutputConnector(Part part, PartGrid partGrid, int centerX, int centerY, int radius) {
_part = part;
_partGrid = partGrid;
_centerX = centerX;
_centerY = centerY;
_radius = radius;
@ -46,6 +48,16 @@ public class RoundOutputConnector implements OutputConnector, Drawable {
public void drop(Wire wire) {
if (wire == _currentWire){
_currentWire = null;
InputConnector resultPoint = _partGrid.getInputConnectorOn(wire.getXEnd(), wire.getYEnd());
Log.d("RoundOutputConnector", "Dropped wire on " + resultPoint);
if (resultPoint == null){
return;
}
wire.attachTo(resultPoint);
_wires.add(wire);
}
else {
@ -70,10 +82,20 @@ public class RoundOutputConnector implements OutputConnector, Drawable {
public void updatePosition(int x, int y) {
_centerX = x;
_centerY = y;
for (Wire wire : _wires){
wire.moveStart(x, y);
}
}
@Override
public void draw(Canvas canvas, boolean devMode) {
}
public void sendSignal() {
for (Wire wire : _wires){
wire.sendSignal();
}
}
}

View File

@ -6,14 +6,15 @@ import android.graphics.Paint;
import android.graphics.Path;
public class Wire implements Moveable, Drawable {
private final OutputConnector _dropper;
private final int _xinit;
private final int _yinit;
private final Dropper _dropper;
private int _xinit;
private int _yinit;
private int _xend;
private int _yend;
private final static int _pathRunWay = 100;
private InputConnector _attachedTo = null;
public Wire(OutputConnector dropper, int xInit, int yInit) {
public Wire(Dropper dropper, int xInit, int yInit) {
super();
_dropper = dropper;
_xinit = xInit;
@ -23,8 +24,13 @@ public class Wire implements Moveable, Drawable {
_yend = yInit;
}
public void moveStart(int x, int y){
_xinit = x;
_yinit = y;
}
@Override
public void move(int x, int y) {
public void moveEnd(int x, int y) {
_xend = x;
_yend = y;
}
@ -52,4 +58,25 @@ public class Wire implements Moveable, Drawable {
canvas.drawPath(samplePath, pathPaint);
}
public int getXEnd() {
return _xend;
}
public int getYEnd() {
return _yend;
}
public void attachTo(InputConnector resultPoint) {
_attachedTo = resultPoint;
_xend = resultPoint.getX();
_yend = resultPoint.getY();
resultPoint.getAttachment(this);
}
public void sendSignal() {
if (_attachedTo != null) {
_attachedTo.sendSignal();
}
}
}