From 1eb830b71d531a949876a96ee1bd52f56db2b0b5 Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Tue, 4 Jul 2017 01:09:37 +0200 Subject: [PATCH] Add basic part movement mechanism. --- .../minicards/PartCanvasView.java | 53 +++++++++++++++---- .../minicards/parts/Part.java | 2 + .../minicards/parts/Placeholder.java | 22 ++++++-- .../minicards/parts/buttons/RoundButton.java | 10 +++- 4 files changed, 70 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java index 51128e1..6bff6ed 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java @@ -4,36 +4,43 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; import android.view.MotionEvent; import android.view.View; import com.codigoparallevar.minicards.parts.Part; +import com.codigoparallevar.minicards.parts.Placeholder; import com.codigoparallevar.minicards.parts.buttons.RoundButton; import java.util.ArrayList; class PartCanvasView extends View { + @NonNull ArrayList parts = new ArrayList<>(); + @Nullable + Part selectedPart; + public PartCanvasView(Context context) { super(context); this.setBackgroundColor(Color.rgb(4, 69, 99)); + parts.add(new Placeholder(50, 50, 750, 500)); parts.add(new RoundButton(500, 1200, 80, 100)); } @Override public void onDraw(Canvas canvas){ - long time = System.currentTimeMillis(); + final long renderStartTime = System.currentTimeMillis(); drawBackground(canvas); for (Part part : parts){ part.draw(canvas); } - Log.d("Render time", System.currentTimeMillis() - time + "ms"); + Log.d("Render time", System.currentTimeMillis() - renderStartTime + "ms"); } private void drawBackground(Canvas canvas) { @@ -58,16 +65,42 @@ class PartCanvasView extends View { @Override public boolean onTouchEvent(MotionEvent event){ - int x = (int) event.getX() - this.getLeft(); - int y = (int) event.getY() - this.getTop(); + final int x = (int) event.getX() - this.getLeft(); + final int y = (int) event.getY() - this.getTop(); - Part touchedPart = getPartOn(x, y); - if (touchedPart == null){ - Log.d("Touched part", "not found"); - return false; + switch (event.getAction()){ + case MotionEvent.ACTION_DOWN: + { + selectedPart = getPartOn(x, y); + if (selectedPart == null) { + Log.d("Touched part", "not found"); + return false; + } + + Log.d("Touched part", "Part: " + selectedPart); + } + break; + + case MotionEvent.ACTION_UP: + { + selectedPart = null; + } + break; + + case MotionEvent.ACTION_MOVE: + { + if (selectedPart != null){ + selectedPart.move(x, y); + } + } + break; + + default: + { + Log.d("PartCanvasView", "Unhandled action: " + event.getAction()); + } } - Log.d("Touched part", "Part: " + touchedPart); invalidate(); return true; } @@ -76,7 +109,7 @@ class PartCanvasView extends View { private Part getPartOn(int x, int y) { // Look in the list, in reverse so top-most elements are checked before for (int i = parts.size() - 1; i >= 0; i--){ - Part part = parts.get(i); + final Part part = parts.get(i); if ((x >= part.getLeft()) && (part.getRight() >= x) && (y >= part.getTop()) && (part.getBottom() >= y)){ return part; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java b/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java index b4923ac..dc77299 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java @@ -9,4 +9,6 @@ public interface Part { public int getBottom(); public void draw(Canvas canvas); + + void move(int x, int y); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java b/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java index 1ea1d62..748e967 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java @@ -5,10 +5,10 @@ import android.graphics.Color; import android.graphics.Paint; public class Placeholder implements Part { - private final int _left; - private final int _top; - private final int _right; - private final int _bottom; + private int _left; + private int _top; + private int _right; + private int _bottom; public Placeholder(int left, int top, int right, int bottom) { _left = left; @@ -43,7 +43,7 @@ public class Placeholder implements Part { paint.setColor(Color.WHITE); // Top - canvas.drawLine(_left,_top, _right, _top, paint); + canvas.drawLine(_left, _top, _right, _top, paint); // Bottom canvas.drawLine(_left, _bottom, _right, _bottom, paint); // Left @@ -55,4 +55,16 @@ public class Placeholder implements Part { // 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; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java index 6f95f7e..6e5ad85 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java @@ -9,8 +9,8 @@ import com.codigoparallevar.minicards.parts.Part; public class RoundButton implements Part { - private final int _xCenter; - private final int _yCenter; + private int _xCenter; + private int _yCenter; private final int _innerRadius; private final int _outerRadius; private final int _outerRadiusThickness = 10; @@ -54,4 +54,10 @@ public class RoundButton implements Part { canvas.drawCircle(_xCenter, _yCenter, _outerRadius - _outerRadiusThickness, backgroundPaint); canvas.drawCircle(_xCenter, _yCenter, _innerRadius, foregroundPaint); } + + @Override + public void move(int x, int y) { + _xCenter = x; + _yCenter = y; + } }