Skip to content

Commit 7001199

Browse files
committed
Starting to deal with tile touches on Gameboard. Ignore if tile is immovable or empty.
1 parent a032fca commit 7001199

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/com/tackmobile/GameTile.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.tackmobile;
22

3-
import roboguice.util.Ln;
4-
53
import com.tackmobile.GameboardView.Coordinate;
64

75
import android.content.Context;
86
import android.graphics.Color;
97
import android.graphics.drawable.Drawable;
108
import android.graphics.drawable.ShapeDrawable;
119
import android.graphics.drawable.shapes.RoundRectShape;
12-
import android.view.MotionEvent;
1310
import android.view.View;
1411

1512
public class GameTile extends View {
@@ -27,12 +24,6 @@ public GameTile(Context context, Coordinate coordinate) {
2724
setBackgroundDrawable(drawable);
2825
}
2926

30-
@Override
31-
public boolean onTouchEvent(MotionEvent event) {
32-
Ln.d("%s\n\trecieved touch event: %s", this, event);
33-
return super.onTouchEvent(event);
34-
}
35-
3627
@Override
3728
public String toString() {
3829
return String.format("<GameTile at row: %d, col: %d", coordinate.row, coordinate.column);
@@ -47,4 +38,8 @@ public void setEmpty(boolean empty) {
4738
if (empty) setBackgroundDrawable(null);
4839
}
4940

41+
public boolean isInRowOrColumnOf(GameTile otherTile) {
42+
return (coordinate.sharesAxisWith(otherTile.coordinate));
43+
}
44+
5045
}

src/com/tackmobile/GameboardView.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import android.content.Context;
77
import android.graphics.Rect;
88
import android.util.AttributeSet;
9+
import android.view.MotionEvent;
10+
import android.view.View;
11+
import android.view.View.OnTouchListener;
912
import android.widget.RelativeLayout;
1013

11-
public class GameboardView extends RelativeLayout {
14+
public class GameboardView extends RelativeLayout implements OnTouchListener {
1215

1316
protected Size tileSize;
1417
protected Rect gameboardRect;
@@ -54,9 +57,25 @@ protected void createTiles() {
5457
}
5558
}
5659

60+
public boolean onTouch(View v, MotionEvent event) {
61+
try {
62+
GameTile touchedTile = (GameTile)v;
63+
Ln.d("%s\n\tTile touched: %s", touchedTile, event);
64+
if (touchedTile.isEmpty() || !touchedTile.isInRowOrColumnOf(emptyTile)) {
65+
Ln.d("Empty or immovable tile; ignoring");
66+
return false;
67+
} else {
68+
return true;
69+
}
70+
} catch (ClassCastException e) {
71+
return false;
72+
}
73+
}
74+
5775
protected GameTile createTileAtCoordinate(Coordinate coordinate) {
5876
GameTile tile = new GameTile(getContext(), coordinate);
5977
tiles.add(tile);
78+
tile.setOnTouchListener(this);
6079
return tile;
6180
}
6281

@@ -102,6 +121,10 @@ public Coordinate(int row, int column) {
102121
this.column = column;
103122
}
104123

124+
public boolean sharesAxisWith(Coordinate coordinate) {
125+
return (row == coordinate.row || column == coordinate.column);
126+
}
127+
105128
}
106129

107130
}

0 commit comments

Comments
 (0)