From be7eacddb7dfdbd7b6c74d06718ad5f512b1d09c Mon Sep 17 00:00:00 2001 From: kenkeiras Date: Tue, 4 Jul 2017 01:26:40 +0200 Subject: [PATCH] Differenciate touching and grabbing. --- .../minicards/PartCanvasView.java | 53 ++++++++++++++++++- .../minicards/motion/MotionMode.java | 9 ++++ .../minicards/parts/Part.java | 2 + .../minicards/parts/Placeholder.java | 6 +++ .../minicards/parts/buttons/RoundButton.java | 6 +++ .../minicards/parts/types/Position.java | 19 +++++++ 6 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/codigoparallevar/minicards/motion/MotionMode.java create mode 100644 app/src/main/java/com/codigoparallevar/minicards/parts/types/Position.java diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java index 6bff6ed..42b204d 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/PartCanvasView.java @@ -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 diff --git a/app/src/main/java/com/codigoparallevar/minicards/motion/MotionMode.java b/app/src/main/java/com/codigoparallevar/minicards/motion/MotionMode.java new file mode 100644 index 0000000..31a3333 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/motion/MotionMode.java @@ -0,0 +1,9 @@ +package com.codigoparallevar.minicards.motion; + +public class MotionMode { + public enum Type { + LongTouch, + ShortTouch, + Displacement, + } +} 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 99b4b95..ad06036 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/Part.java @@ -11,4 +11,6 @@ public interface Part { void draw(Canvas canvas); void move(int x, int y); + + void touched(); } 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 748e967..8b0ae48 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/Placeholder.java @@ -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"); + } } 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 6e5ad85..4cdbdb3 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 @@ -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"); + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/types/Position.java b/app/src/main/java/com/codigoparallevar/minicards/parts/types/Position.java new file mode 100644 index 0000000..95bfee2 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/types/Position.java @@ -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; + } +}