Add basic part movement mechanism.

This commit is contained in:
kenkeiras 2017-07-04 01:09:37 +02:00
parent 5e71d29a39
commit 1eb830b71d
4 changed files with 70 additions and 17 deletions

View File

@ -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<Part> 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){
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: " + touchedPart);
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());
}
}
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;

View File

@ -9,4 +9,6 @@ public interface Part {
public int getBottom();
public void draw(Canvas canvas);
void move(int x, int y);
}

View File

@ -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;
}
}

View File

@ -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;
}
}