Add basic part movement mechanism.
This commit is contained in:
parent
5e71d29a39
commit
1eb830b71d
@ -4,36 +4,43 @@ import android.content.Context;
|
|||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import com.codigoparallevar.minicards.parts.Part;
|
import com.codigoparallevar.minicards.parts.Part;
|
||||||
|
import com.codigoparallevar.minicards.parts.Placeholder;
|
||||||
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
|
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
class PartCanvasView extends View {
|
class PartCanvasView extends View {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
ArrayList<Part> parts = new ArrayList<>();
|
ArrayList<Part> parts = new ArrayList<>();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
Part selectedPart;
|
||||||
|
|
||||||
public PartCanvasView(Context context) {
|
public PartCanvasView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.setBackgroundColor(Color.rgb(4, 69, 99));
|
this.setBackgroundColor(Color.rgb(4, 69, 99));
|
||||||
|
parts.add(new Placeholder(50, 50, 750, 500));
|
||||||
parts.add(new RoundButton(500, 1200, 80, 100));
|
parts.add(new RoundButton(500, 1200, 80, 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDraw(Canvas canvas){
|
public void onDraw(Canvas canvas){
|
||||||
long time = System.currentTimeMillis();
|
final long renderStartTime = System.currentTimeMillis();
|
||||||
drawBackground(canvas);
|
drawBackground(canvas);
|
||||||
|
|
||||||
for (Part part : parts){
|
for (Part part : parts){
|
||||||
part.draw(canvas);
|
part.draw(canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("Render time", System.currentTimeMillis() - time + "ms");
|
Log.d("Render time", System.currentTimeMillis() - renderStartTime + "ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawBackground(Canvas canvas) {
|
private void drawBackground(Canvas canvas) {
|
||||||
@ -58,16 +65,42 @@ class PartCanvasView extends View {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event){
|
public boolean onTouchEvent(MotionEvent event){
|
||||||
int x = (int) event.getX() - this.getLeft();
|
final int x = (int) event.getX() - this.getLeft();
|
||||||
int y = (int) event.getY() - this.getTop();
|
final int y = (int) event.getY() - this.getTop();
|
||||||
|
|
||||||
Part touchedPart = getPartOn(x, y);
|
switch (event.getAction()){
|
||||||
if (touchedPart == null){
|
case MotionEvent.ACTION_DOWN:
|
||||||
Log.d("Touched part", "not found");
|
{
|
||||||
return false;
|
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();
|
invalidate();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -76,7 +109,7 @@ class PartCanvasView extends View {
|
|||||||
private Part getPartOn(int x, int y) {
|
private Part getPartOn(int x, int y) {
|
||||||
// Look in the list, in reverse so top-most elements are checked before
|
// Look in the list, in reverse so top-most elements are checked before
|
||||||
for (int i = parts.size() - 1; i >= 0; i--){
|
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)
|
if ((x >= part.getLeft()) && (part.getRight() >= x)
|
||||||
&& (y >= part.getTop()) && (part.getBottom() >= y)){
|
&& (y >= part.getTop()) && (part.getBottom() >= y)){
|
||||||
return part;
|
return part;
|
||||||
|
@ -9,4 +9,6 @@ public interface Part {
|
|||||||
public int getBottom();
|
public int getBottom();
|
||||||
|
|
||||||
public void draw(Canvas canvas);
|
public void draw(Canvas canvas);
|
||||||
|
|
||||||
|
void move(int x, int y);
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,10 @@ import android.graphics.Color;
|
|||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
|
||||||
public class Placeholder implements Part {
|
public class Placeholder implements Part {
|
||||||
private final int _left;
|
private int _left;
|
||||||
private final int _top;
|
private int _top;
|
||||||
private final int _right;
|
private int _right;
|
||||||
private final int _bottom;
|
private int _bottom;
|
||||||
|
|
||||||
public Placeholder(int left, int top, int right, int bottom) {
|
public Placeholder(int left, int top, int right, int bottom) {
|
||||||
_left = left;
|
_left = left;
|
||||||
@ -43,7 +43,7 @@ public class Placeholder implements Part {
|
|||||||
paint.setColor(Color.WHITE);
|
paint.setColor(Color.WHITE);
|
||||||
|
|
||||||
// Top
|
// Top
|
||||||
canvas.drawLine(_left,_top, _right, _top, paint);
|
canvas.drawLine(_left, _top, _right, _top, paint);
|
||||||
// Bottom
|
// Bottom
|
||||||
canvas.drawLine(_left, _bottom, _right, _bottom, paint);
|
canvas.drawLine(_left, _bottom, _right, _bottom, paint);
|
||||||
// Left
|
// Left
|
||||||
@ -55,4 +55,16 @@ public class Placeholder implements Part {
|
|||||||
// Cross, top-right, bottom-left
|
// Cross, top-right, bottom-left
|
||||||
canvas.drawLine(_right, _top, _left, _bottom, paint);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@ import com.codigoparallevar.minicards.parts.Part;
|
|||||||
public class RoundButton implements Part {
|
public class RoundButton implements Part {
|
||||||
|
|
||||||
|
|
||||||
private final int _xCenter;
|
private int _xCenter;
|
||||||
private final int _yCenter;
|
private int _yCenter;
|
||||||
private final int _innerRadius;
|
private final int _innerRadius;
|
||||||
private final int _outerRadius;
|
private final int _outerRadius;
|
||||||
private final int _outerRadiusThickness = 10;
|
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, _outerRadius - _outerRadiusThickness, backgroundPaint);
|
||||||
canvas.drawCircle(_xCenter, _yCenter, _innerRadius, foregroundPaint);
|
canvas.drawCircle(_xCenter, _yCenter, _innerRadius, foregroundPaint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void move(int x, int y) {
|
||||||
|
_xCenter = x;
|
||||||
|
_yCenter = y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user