summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob L. Kreuze <jakob@memeware.net>2019-01-12 14:32:42 -0500
committerJakob L. Kreuze <jakob@memeware.net>2019-01-12 14:32:42 -0500
commitef496487ae49a1e98ea3c9de0531ca4716f7cebf (patch)
tree139ac23a0c82f3b935dfc3f0aeee7d56cd3630bb
parentcc84e4eaec11580fcaf0b70a7782566917ed286e (diff)
Extract Grid to be primarily a member of GameActivity
-rw-r--r--app/src/main/java/space/jakob/mines/GameActivity.kt28
-rw-r--r--app/src/main/java/space/jakob/mines/GameView.kt16
2 files changed, 17 insertions, 27 deletions
diff --git a/app/src/main/java/space/jakob/mines/GameActivity.kt b/app/src/main/java/space/jakob/mines/GameActivity.kt
index 5a44720..2ef2b70 100644
--- a/app/src/main/java/space/jakob/mines/GameActivity.kt
+++ b/app/src/main/java/space/jakob/mines/GameActivity.kt
@@ -21,20 +21,28 @@ import android.view.MotionEvent
import android.util.Log
class GameActivity : AppCompatActivity() {
+ lateinit var grid : Grid
lateinit var gameView: GameView
lateinit var gestureDetector: GestureDetectorCompat
lateinit var scaleGestureDetector: ScaleGestureDetector
// Implements the callbacks for gestureDetector.
val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
+ // Reveal a tile if it's tapped.
override fun onSingleTapUp(e: MotionEvent): Boolean {
- with (gameView) {
- revealAt(e.x.toInt(), e.y.toInt())
- invalidate()
- }
+ val (x, y) = gameView.toGridCoordinates(e.x.toInt(), e.y.toInt())
+ grid.reveal(x, y)
+ gameView.invalidate()
return false
}
+ // Flag a tile if it's pressed and held.
+ override fun onLongPress(e: MotionEvent) {
+ val (x, y) = gameView.toGridCoordinates(e.x.toInt(), e.y.toInt())
+ grid.flag(x, y)
+ gameView.invalidate()
+ }
+
override fun onScroll(e1: MotionEvent, e2: MotionEvent, dx: Float, dy: Float): Boolean {
with (gameView) {
moveCamera(dx.toDouble(), dy.toDouble())
@@ -42,13 +50,6 @@ class GameActivity : AppCompatActivity() {
}
return false
}
-
- override fun onLongPress(e: MotionEvent) {
- with (gameView) {
- flagAt(e.x.toInt(), e.y.toInt())
- invalidate()
- }
- }
}
// Implements the callbacks for scaleGestureDetector.
@@ -83,10 +84,11 @@ class GameActivity : AppCompatActivity() {
mines = it.getIntExtra("MINES", mines)
}
+ grid = Grid(width, height, mines)
gameView = findViewById(R.id.view) as GameView
- gameView.grid = Grid(width, height, mines)
-
gestureDetector = GestureDetectorCompat(this, gestureListener)
scaleGestureDetector = ScaleGestureDetector(this, scaleListener)
+
+ gameView.grid = grid
}
}
diff --git a/app/src/main/java/space/jakob/mines/GameView.kt b/app/src/main/java/space/jakob/mines/GameView.kt
index d0a7ae0..84877e6 100644
--- a/app/src/main/java/space/jakob/mines/GameView.kt
+++ b/app/src/main/java/space/jakob/mines/GameView.kt
@@ -38,6 +38,7 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
val scaledTileWidth: Int
get() = (minOf(width, height) / cameraBreadth).toInt()
+ // Reference to GameActivity's grid that should be treated as read-only.
var grid: Grid? = null
// All of the state for maintaining the view's current positioning. For an
@@ -85,23 +86,10 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
}
/**
- * Reveal and @return the tile at (@param x, @param y).
- */
- fun revealAt(x: Int, y: Int): Tile? = grid?.let {
- val (x, y) = toGridCoordinates(x.toInt(), y.toInt())
- if (it.valid(x, y)) {
- it.reveal(x, y)
- return it[x, y]
- } else {
- return null
- }
- }
-
- /**
* Flag and @return the tile at (@param x, @param y).
*/
fun flagAt(x: Int, y: Int): Tile? = grid?.let {
- val (x, y) = toGridCoordinates(x.toInt(), y.toInt())
+
if (it.valid(x, y)) {
it.flag(x, y)
return it[x, y]