Skip to content

Commit 443c771

Browse files
unknownunknown
authored andcommitted
gray background, better click detection
1 parent d835571 commit 443c771

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
lines changed

res/layout/activity_about.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
android:background="@color/background"
56
android:gravity="center_horizontal"
67
android:orientation="vertical"
78
android:padding="5dp" >
@@ -40,8 +41,8 @@
4041
<ImageView
4142
android:layout_width="wrap_content"
4243
android:layout_height="wrap_content"
43-
android:src="@drawable/settle_up"
44-
android:contentDescription="@string/settle_up" />
44+
android:contentDescription="@string/settle_up"
45+
android:src="@drawable/settle_up" />
4546

4647
<TextView
4748
android:id="@+id/settle_up"

res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
android:background="@color/background"
56
android:padding="5dp" >
67

78
<cz.destil.sliderpuzzle.ui.GameBoardView

res/values/colors.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<color name="background">#3b3b3b</color>
5+
6+
</resources>

src/cz/destil/sliderpuzzle/ui/GameboardView.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.graphics.drawable.BitmapDrawable;
1616
import android.graphics.drawable.Drawable;
1717
import android.util.AttributeSet;
18+
import android.util.Log;
1819
import android.util.Pair;
1920
import android.view.MotionEvent;
2021
import 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) {

src/cz/destil/sliderpuzzle/ui/TileView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class TileView extends ImageView {
1919

2020
public Coordinate coordinate;
2121
public int originalIndex;
22+
public int numberOfDrags;
2223
private boolean empty;
2324

2425
public TileView(Context context, int originalIndex) {

0 commit comments

Comments
 (0)