Add scrolling canvas.

This commit is contained in:
kenkeiras 2018-01-18 22:15:54 +01:00
parent a7a9149502
commit e47e1c61f9
9 changed files with 111 additions and 43 deletions

View File

@ -66,6 +66,12 @@ class CanvasView extends View implements PartGrid {
private Tuple4<Integer, Integer, Integer, Integer> _dropToRemoveZone = new Tuple4<>(0, 0, 0, 0); private Tuple4<Integer, Integer, Integer, Integer> _dropToRemoveZone = new Tuple4<>(0, 0, 0, 0);
private boolean _devMode = false; private boolean _devMode = false;
@NonNull
private Tuple2<Integer, Integer> _viewOrigin = new Tuple2<>(0, 0);
@Nullable
private Tuple2<Integer, Integer> _mouseDownPoint = null;
public CanvasView(Context context) { public CanvasView(Context context) {
super(context); super(context);
init(); init();
@ -182,16 +188,18 @@ class CanvasView extends View implements PartGrid {
@Override @Override
public void onDraw(Canvas canvas){ public void onDraw(Canvas canvas){
final long renderStartTime = System.currentTimeMillis(); final long renderStartTime = System.currentTimeMillis();
drawBackground(canvas); ScrolledCanvas scrolledCanvas = new ScrolledCanvas(canvas, _viewOrigin);
drawBackground(scrolledCanvas);
for (Part part : parts){ for (Part part : parts){
part.draw(canvas, _devMode); part.draw(scrolledCanvas, _devMode);
} }
Log.d("Render time", System.currentTimeMillis() - renderStartTime + "ms"); Log.d("Render time", System.currentTimeMillis() - renderStartTime + "ms");
} }
private void drawBackground(Canvas canvas) { private void drawBackground(ScrolledCanvas canvas) {
if (!_devMode){ if (!_devMode){
return; return;
} }
@ -217,14 +225,19 @@ class CanvasView extends View implements PartGrid {
@Override @Override
public boolean onTouchEvent(MotionEvent event){ public boolean onTouchEvent(MotionEvent event){
final int x = (int) event.getX() - this.getLeft(); final int xInScreen = (int) event.getX() - this.getLeft();
final int y = (int) event.getY() - this.getTop(); final int yInScreen = (int) event.getY() - this.getTop();
final int xInCanvas = xInScreen + _viewOrigin.item1;
final int yInCanvas = yInScreen + _viewOrigin.item2;
switch (event.getAction()){ switch (event.getAction()){
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
{ {
selectedPart = getPartOn(x, y); _mouseDownPoint = new Tuple2<>(xInScreen, yInScreen);
lastTouchedPosition.to(x, y);
selectedPart = getPartOn(xInCanvas, yInCanvas);
lastTouchedPosition.to(xInScreen, yInScreen);
lastTouchedTime = System.currentTimeMillis(); lastTouchedTime = System.currentTimeMillis();
if (selectedPart == null) { if (selectedPart == null) {
Log.d("Touched part", "not found"); Log.d("Touched part", "not found");
@ -249,9 +262,9 @@ class CanvasView extends View implements PartGrid {
} }
else if (motionMode == MotionMode.Type.LongTouch) { else if (motionMode == MotionMode.Type.LongTouch) {
if (selectedPart != null) { if (selectedPart != null) {
selectedPart.getMoveable().drop(x, y); selectedPart.getMoveable().drop(xInCanvas, yInCanvas);
if (inDropZone(x, y)) { if (inDropZone(xInScreen, yInScreen)) {
Log.d("Canvas", "Deleting element" + selectedPart); Log.d("Canvas", "Deleting element" + selectedPart);
parts.remove(selectedPart); parts.remove(selectedPart);
selectedPart.unlink(); selectedPart.unlink();
@ -273,25 +286,38 @@ class CanvasView extends View implements PartGrid {
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
{ {
if (selectedPart == null){
int xMovement = _mouseDownPoint.item1 - xInScreen;
int yMovement = _mouseDownPoint.item2 - yInScreen;
_viewOrigin = new Tuple2(
Math.max(_viewOrigin.item1 + xMovement, 0),
Math.max(_viewOrigin.item2 + yMovement, 0));
_mouseDownPoint = new Tuple2(xInScreen, yInScreen);
}
else {
if (!_devMode) { if (!_devMode) {
break; break;
} }
Log.i("Canvas", "X: " + x + " Y: " + y Log.i("Canvas", "X: " + xInScreen + " Y: " + yInScreen
+ " in drop zone " + _dropToRemoveZone + " : " + inDropZone(x, y)); + " in drop zone " + _dropToRemoveZone + " : "
if (motionMode == null){ + inDropZone(xInScreen, yInScreen));
final Selectable nowSelectedPart = getPartOn(x, y);
if (motionMode == null) {
final Selectable nowSelectedPart = getPartOn(xInCanvas, yInCanvas);
final boolean canWait = selectedPart == nowSelectedPart; final boolean canWait = selectedPart == nowSelectedPart;
motionMode = getMotionMode(canWait); motionMode = getMotionMode(canWait);
} }
if (motionMode == MotionMode.Type.LongTouch){ if (motionMode == MotionMode.Type.LongTouch) {
if (selectedPart != null){ if (selectedPart != null) {
_isDragging = true; _isDragging = true;
selectedPart.getMoveable().moveEnd(x, y); selectedPart.getMoveable().moveEnd(xInCanvas, yInCanvas);
invalidate(); invalidate();
} }
} }
} }
}
break; break;
default: default:

View File

@ -0,0 +1,44 @@
package com.codigoparallevar.minicards;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import com.codigoparallevar.minicards.types.Tuple2;
public class ScrolledCanvas {
private final Canvas canvas;
private final int xOrig;
private final int yOrig;
public ScrolledCanvas(Canvas canvas, Tuple2<Integer, Integer> viewOrigin) {
this.canvas = canvas;
this.xOrig = viewOrigin.item1;
this.yOrig = viewOrigin.item2;
}
public void drawLine(float startX, float startY, float stopX, float stopY,
@NonNull Paint paint) {
canvas.drawLine(startX - xOrig, startY - yOrig,
stopX - xOrig, stopY - yOrig, paint);
}
public void drawRect(@NonNull Rect r, @NonNull Paint paint) {
canvas.drawRect(r.left - xOrig, r.top - yOrig,
r.right - xOrig, r.bottom - yOrig,
paint);
}
public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint) {
canvas.drawCircle(cx - xOrig, cy - yOrig,
radius, paint);
}
public void drawPath(@NonNull Path path, @NonNull Paint paint) {
Path offsetPath = new Path(path);
offsetPath.offset(-xOrig, -yOrig);
canvas.drawPath(offsetPath, paint);
}
}

View File

@ -5,6 +5,7 @@ import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.util.Log; import android.util.Log;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.PartConnection; import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
@ -85,7 +86,7 @@ public class RoundButton implements Part {
} }
@Override @Override
public void draw(Canvas canvas, boolean devMode) { public void draw(ScrolledCanvas canvas, boolean devMode) {
Paint foregroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint foregroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
foregroundPaint.setColor(Color.WHITE); foregroundPaint.setColor(Color.WHITE);
@ -102,7 +103,7 @@ public class RoundButton implements Part {
canvas.drawCircle(_xCenter, _yCenter, _innerRadius, foregroundPaint); canvas.drawCircle(_xCenter, _yCenter, _innerRadius, foregroundPaint);
} }
private void drawConnector(Canvas canvas) { private void drawConnector(ScrolledCanvas canvas) {
Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
connectorPaint.setColor(Color.RED); connectorPaint.setColor(Color.RED);
@ -123,7 +124,7 @@ public class RoundButton implements Part {
return _innerRadius; return _innerRadius;
} }
private void drawWires(Canvas canvas, boolean devMode) { private void drawWires(ScrolledCanvas canvas, boolean devMode) {
for (OutputConnector outputConnector : _outputConnectors){ for (OutputConnector outputConnector : _outputConnectors){
outputConnector.drawWires(canvas, devMode); outputConnector.drawWires(canvas, devMode);
} }

View File

@ -6,7 +6,7 @@ import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.Rect;
import android.util.Log; import android.util.Log;
import com.codigoparallevar.minicards.types.Wire; import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
@ -79,7 +79,7 @@ public class ColorBox implements Part {
} }
@Override @Override
public void draw(Canvas canvas, boolean devMode) { public void draw(ScrolledCanvas canvas, boolean devMode) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
if (_activated){ if (_activated){
paint.setColor(Color.GREEN); paint.setColor(Color.GREEN);
@ -100,7 +100,7 @@ public class ColorBox implements Part {
paint); paint);
} }
private void drawConnector(Canvas canvas) { private void drawConnector(ScrolledCanvas canvas) {
Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint connectorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
connectorPaint.setColor(Color.YELLOW); connectorPaint.setColor(Color.YELLOW);

View File

@ -1,10 +1,10 @@
package com.codigoparallevar.minicards.parts.samples; package com.codigoparallevar.minicards.parts.samples;
import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.util.Log; import android.util.Log;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
@ -62,7 +62,7 @@ public class Placeholder implements Part {
} }
@Override @Override
public void draw(Canvas canvas, boolean devMode) { public void draw(ScrolledCanvas canvas, boolean devMode) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE); paint.setColor(Color.WHITE);

View File

@ -1,7 +1,7 @@
package com.codigoparallevar.minicards.types; package com.codigoparallevar.minicards.types;
import android.graphics.Canvas; import com.codigoparallevar.minicards.ScrolledCanvas;
public interface Drawable { public interface Drawable {
void draw(Canvas canvas, boolean devMode); void draw(ScrolledCanvas canvas, boolean devMode);
} }

View File

@ -1,13 +1,11 @@
package com.codigoparallevar.minicards.types; package com.codigoparallevar.minicards.types;
import android.graphics.Canvas;
import android.util.Log; import android.util.Log;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
import com.codigoparallevar.minicards.types.connectors.output.OutputConnector;
import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector; import com.codigoparallevar.minicards.types.connectors.output.SignalOutputConnector;
import com.codigoparallevar.minicards.types.wireData.Signal;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
@ -94,7 +92,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
} }
@Override @Override
public void drawWires(Canvas canvas, boolean devMode) { public void drawWires(ScrolledCanvas canvas, boolean devMode) {
for (SignalWire wire : _wires) { for (SignalWire wire : _wires) {
wire.draw(canvas, devMode); wire.draw(canvas, devMode);
} }
@ -141,7 +139,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
} }
@Override @Override
public void draw(Canvas canvas, boolean devMode) { public void draw(ScrolledCanvas canvas, boolean devMode) {
// TODO: Complete this part // TODO: Complete this part
} }

View File

@ -1,10 +1,10 @@
package com.codigoparallevar.minicards.types; package com.codigoparallevar.minicards.types;
import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path; import android.graphics.Path;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector; import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.wireData.WireDataType; import com.codigoparallevar.minicards.types.wireData.WireDataType;
@ -45,7 +45,7 @@ public class Wire<T extends WireDataType, InputConnectorType extends InputConnec
} }
@Override @Override
public void draw(Canvas canvas, boolean devMode) { public void draw(ScrolledCanvas canvas, boolean devMode) {
final Path samplePath = new Path(); final Path samplePath = new Path();
samplePath.moveTo(_xinit, _yinit); samplePath.moveTo(_xinit, _yinit);

View File

@ -1,7 +1,6 @@
package com.codigoparallevar.minicards.types.connectors.output; package com.codigoparallevar.minicards.types.connectors.output;
import android.graphics.Canvas; import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Dropper;
import com.codigoparallevar.minicards.types.Selectable; import com.codigoparallevar.minicards.types.Selectable;
import com.codigoparallevar.minicards.types.Tuple2; import com.codigoparallevar.minicards.types.Tuple2;
@ -16,7 +15,7 @@ public interface OutputConnector<T extends WireDataType,
T2 extends Wire<T, T1>> T2 extends Wire<T, T1>>
extends Selectable, Dropper<T2> { extends Selectable, Dropper<T2> {
void drawWires(Canvas canvas, boolean devMode); void drawWires(ScrolledCanvas canvas, boolean devMode);
void updatePosition(int x, int y); void updatePosition(int x, int y);
List<Tuple2<String, String>> getConnectionEndpoints(); List<Tuple2<String, String>> getConnectionEndpoints();