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

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

View File

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

View File

@ -1,10 +1,10 @@
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.ScrolledCanvas;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
@ -62,7 +62,7 @@ public class Placeholder implements Part {
}
@Override
public void draw(Canvas canvas, boolean devMode) {
public void draw(ScrolledCanvas canvas, boolean devMode) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);

View File

@ -1,7 +1,7 @@
package com.codigoparallevar.minicards.types;
import android.graphics.Canvas;
import com.codigoparallevar.minicards.ScrolledCanvas;
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;
import android.graphics.Canvas;
import android.util.Log;
import com.codigoparallevar.minicards.ScrolledCanvas;
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;
@ -94,7 +92,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
}
@Override
public void drawWires(Canvas canvas, boolean devMode) {
public void drawWires(ScrolledCanvas canvas, boolean devMode) {
for (SignalWire wire : _wires) {
wire.draw(canvas, devMode);
}
@ -141,7 +139,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector {
}
@Override
public void draw(Canvas canvas, boolean devMode) {
public void draw(ScrolledCanvas canvas, boolean devMode) {
// TODO: Complete this part
}

View File

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

View File

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