Differenciate touching and grabbing.

This commit is contained in:
kenkeiras 2017-07-04 01:26:40 +02:00
parent bfcb25e1a2
commit be7eacddb7
6 changed files with 93 additions and 2 deletions

View File

@ -10,9 +10,11 @@ import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.codigoparallevar.minicards.motion.MotionMode;
import com.codigoparallevar.minicards.parts.Part;
import com.codigoparallevar.minicards.parts.Placeholder;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.types.Position;
import java.util.ArrayList;
@ -24,6 +26,17 @@ class PartCanvasView extends View {
@Nullable
Part selectedPart;
@Nullable
final Position lastTouchedPosition = new Position();
@Nullable
long lastTouchedTime;
@Nullable
private MotionMode.Type motionMode;
private final static float touchTimeForLongTouchInMillis = 500;
public PartCanvasView(Context context) {
super(context);
this.setBackgroundColor(Color.rgb(4, 69, 99));
@ -72,6 +85,8 @@ class PartCanvasView extends View {
case MotionEvent.ACTION_DOWN:
{
selectedPart = getPartOn(x, y);
lastTouchedPosition.to(x, y);
lastTouchedTime = System.currentTimeMillis();
if (selectedPart == null) {
Log.d("Touched part", "not found");
return false;
@ -83,14 +98,31 @@ class PartCanvasView extends View {
case MotionEvent.ACTION_UP:
{
if (motionMode == null){
motionMode = getMotionMode(false);
}
if (motionMode != MotionMode.Type.LongTouch) {
if (selectedPart != null){
selectedPart.touched();
}
}
motionMode = null;
selectedPart = null;
}
break;
case MotionEvent.ACTION_MOVE:
{
if (selectedPart != null){
selectedPart.move(x, y);
if (motionMode == null){
final Part nowSelectedPart = getPartOn(x, y);
final boolean canWait = selectedPart == nowSelectedPart;
motionMode = getMotionMode(canWait);
}
if (motionMode == MotionMode.Type.LongTouch){
if (selectedPart != null){
selectedPart.move(x, y);
}
}
}
break;
@ -105,6 +137,23 @@ class PartCanvasView extends View {
return true;
}
@Nullable
private MotionMode.Type getMotionMode(boolean canWait) {
if (selectedPart == null){
return MotionMode.Type.Displacement;
}
if ((System.currentTimeMillis() - lastTouchedTime) >= touchTimeForLongTouchInMillis){
return MotionMode.Type.LongTouch;
}
if (canWait) {
return null;
}
return MotionMode.Type.ShortTouch;
}
@Nullable
private Part getPartOn(int x, int y) {
// Look in the list, in reverse so top-most elements are checked before

View File

@ -0,0 +1,9 @@
package com.codigoparallevar.minicards.motion;
public class MotionMode {
public enum Type {
LongTouch,
ShortTouch,
Displacement,
}
}

View File

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

View File

@ -3,6 +3,7 @@ package com.codigoparallevar.minicards.parts;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
public class Placeholder implements Part {
private int _left;
@ -67,4 +68,9 @@ public class Placeholder implements Part {
_top = y - height / 2;
_bottom = _top + height;
}
@Override
public void touched() {
Log.d("Placeholder", "Placeholder touched");
}
}

View File

@ -3,6 +3,7 @@ package com.codigoparallevar.minicards.parts.buttons;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import com.codigoparallevar.minicards.parts.Part;
@ -60,4 +61,9 @@ public class RoundButton implements Part {
_xCenter = x;
_yCenter = y;
}
@Override
public void touched() {
Log.d("RoundButton", "Round button touched");
}
}

View File

@ -0,0 +1,19 @@
package com.codigoparallevar.minicards.parts.types;
public class Position {
private int _x;
private int _y;
public void to(int x, int y) {
_x = x;
_y = y;
}
public int get_x() {
return _x;
}
public int get_y() {
return _y;
}
}