1515import android .graphics .drawable .BitmapDrawable ;
1616import android .graphics .drawable .Drawable ;
1717import android .util .AttributeSet ;
18+ import android .util .Log ;
1819import android .util .Pair ;
1920import android .view .MotionEvent ;
2021import android .view .View ;
@@ -148,6 +149,7 @@ public boolean onTouch(View v, MotionEvent event) {
148149 if (event .getActionMasked () == MotionEvent .ACTION_DOWN ) {
149150 movedTile = touchedTile ;
150151 currentMotionDescriptors = getTilesBetweenEmptyTileAndTile (movedTile );
152+ movedTile .numberOfDrags = 0 ;
151153 // during the gesture
152154 } else if (event .getActionMasked () == MotionEvent .ACTION_MOVE ) {
153155 if (lastDragPoint != null ) {
@@ -158,13 +160,9 @@ public boolean onTouch(View v, MotionEvent event) {
158160 } else if (event .getActionMasked () == MotionEvent .ACTION_UP ) {
159161 // reload the motion descriptors in case of position change.
160162 currentMotionDescriptors = getTilesBetweenEmptyTileAndTile (movedTile );
161- // if drag was over 50%, do the move
162- if (lastDragPoint != null && lastDragMovedAtLeastHalfWay ()) {
163+ // if drag was over 50% or it's click , do the move
164+ if (lastDragMovedAtLeastHalfWay () || isClick ()) {
163165 animateTilesToEmptySpace ();
164- // if it was a click, do the move
165- } else if (lastDragPoint == null || lastDragMovedMinimally ()) {
166- animateTilesToEmptySpace ();
167- // if it was a drag less than 50%, animate tiles back
168166 } else {
169167 animateTilesBackToOrigin ();
170168 }
@@ -180,7 +178,7 @@ public boolean onTouch(View v, MotionEvent event) {
180178 * @return Whether last drag moved with the tile more than 50% of its size
181179 */
182180 private boolean lastDragMovedAtLeastHalfWay () {
183- if (currentMotionDescriptors != null && currentMotionDescriptors .size () > 0 ) {
181+ if (lastDragPoint != null && currentMotionDescriptors != null && currentMotionDescriptors .size () > 0 ) {
184182 GameTileMotionDescriptor firstMotionDescriptor = currentMotionDescriptors .get (0 );
185183 if (firstMotionDescriptor .axialDelta > tileSize / 2 ) {
186184 return true ;
@@ -190,12 +188,17 @@ private boolean lastDragMovedAtLeastHalfWay() {
190188 }
191189
192190 /**
193- * @return Whether last drag moved just a little = involuntary move during
194- * click
191+ * Detects click - either true click (no drags) or small involuntary drag
192+ *
193+ * @return Whether last gesture was a click
195194 */
196- private boolean lastDragMovedMinimally () {
197- if (currentMotionDescriptors != null && currentMotionDescriptors .size () > 0 ) {
195+ private boolean isClick () {
196+ if (lastDragPoint == null ) {
197+ return true ; // no drag
198+ }
199+ if (currentMotionDescriptors != null && currentMotionDescriptors .size () > 0 && movedTile .numberOfDrags < 5 ) {
198200 GameTileMotionDescriptor firstMotionDescriptor = currentMotionDescriptors .get (0 );
201+ // just very small drag counts as click
199202 if (firstMotionDescriptor .axialDelta < tileSize / 20 ) {
200203 return true ;
201204 }
@@ -214,11 +217,13 @@ private void followFinger(MotionEvent event) {
214217 float dxEvent = event .getRawX () - lastDragPoint .x ;
215218 float dyEvent = event .getRawY () - lastDragPoint .y ;
216219 TileView tile ;
220+ movedTile .numberOfDrags ++;
217221 for (GameTileMotionDescriptor descriptor : currentMotionDescriptors ) {
218222 tile = descriptor .tile ;
219223 Pair <Float , Float > xy = getXY (tile , dxEvent , dyEvent , descriptor .direction );
220224 // detect if this move is valid
221- RectF candidateRect = new RectF (xy .first , xy .second , xy .first + tile .getWidth (), xy .second + tile .getHeight ());
225+ RectF candidateRect = new RectF (xy .first , xy .second , xy .first + tile .getWidth (), xy .second
226+ + tile .getHeight ());
222227 ArrayList <TileView > tilesToCheck = null ;
223228 if (tile .coordinate .row == emptyTile .coordinate .row ) {
224229 tilesToCheck = allTilesInRow (tile .coordinate .row );
@@ -244,10 +249,15 @@ private void followFinger(MotionEvent event) {
244249
245250 /**
246251 * Computes new x,y coordinates for given tile in given direction (x or y).
247- * @param tile tile to check
248- * @param dxEvent change of x coordinate from touch gesture
249- * @param dyEvent change of y coordinate from touch gesture
250- * @param direction x or y direction
252+ *
253+ * @param tile
254+ * tile to check
255+ * @param dxEvent
256+ * change of x coordinate from touch gesture
257+ * @param dyEvent
258+ * change of y coordinate from touch gesture
259+ * @param direction
260+ * x or y direction
251261 * @return pair of first x coordinates, second y coordinates
252262 */
253263 private Pair <Float , Float > getXY (TileView tile , float dxEvent , float dyEvent , Direction direction ) {
0 commit comments