Modern Apps git

← Modern-Apps.git

games-chess: add drag-to-move to shared board

Commit
8f276bd6c3d61b3becd05f0df92d83d331b3b6a4
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:10 UTC (28 days ago)
Parents
504d3d7
BoardGrid (shared by the game, puzzle, learn and online boards) gains opt-in drag-to-move on top of the untouched tap path. Pressing a movable piece and dragging lifts it under the pointer in an unrotated overlay (so a flipped board needs no extra rotation math), and dropping is expressed as the same two taps a player would make: the drag start taps the piece square so the owning ViewModel selects it, and the drop taps the destination, so move validation, puzzle/learn failure rules and the promotion picker all run exactly as before. A drag ending on a promotion square therefore lands the pawn there and the existing PawnPromotionDialog appears.

While dragging, legal destinations highlight with the same move-hint dots as tap selection (BoardGrid reuses its isValidMove destination computation; Learn passes its pseudo-legal variant for kingless/illegal-move lessons so drag accepts exactly what a tap would). Dropping off-board, on the origin square, on an illegal square, or cancelling animates the piece home over library.ui Motion.drop() via the animatedFloat helper (no raw compose-animation at the call site) and undoes only the selection the drag itself created. Tap-to-move, tap-to-select and tap-to-deselect are unchanged: the drag detector only consumes the gesture after touch slop, so plain taps still reach the squares' click handlers.

Enabled for the main game board (only on the human turn and while the game is live), the puzzle board (while Solving) and the learn board (while Playing) since all three share BoardGrid with no special-casing. The online board keeps the default (tap-only): its ViewModel was out of scope for this change.

Verified: :games:chess:testDevUnitTest and :games:chess:compileDevKotlin pass (compose UI tests cover the tap flows through the new board hierarchy), :games:chess:lint passes, and :games:chess:metadata regenerates the store-listing previews with no golden changes, confirming the board's static rendering is pixel-identical.

Diff

games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.ktindex 01d9d9cdef..824d5b4210 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessGameScreens.kt@@ -4,6 +4,7 @@ import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable+import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints@@ -16,6 +17,7 @@ import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width@@ -29,25 +31,34 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.produceState import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip+import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.window.DialogProperties+import kotlin.math.roundToInt+import kotlinx.coroutines.delay import com.vayunmathur.games.chess.data.Move import com.vayunmathur.games.chess.data.Piece import com.vayunmathur.games.chess.data.PieceColor@@ -69,7 +80,9 @@ import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.Icon import com.vayunmathur.library.ui.IconButton import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Motion import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.library.ui.animatedFloat import com.vayunmathur.library.ui.SegmentedButton import com.vayunmathur.library.ui.SegmentedButtonDefaults import com.vayunmathur.library.ui.SingleChoiceSegmentedButtonRow@@ -392,12 +405,19 @@ private fun GameBoardGrid(     state: ChessUiState,     actions: ChessActions, ) {+    // Drag only while the human side is actually to move: never when the+    // game is over or while the AI is thinking.+    val dragEnabled = state.gameResult == null && when (val mode = state.gameMode) {+        GameMode.TwoPlayer -> true+        is GameMode.VsAI -> state.turn == mode.playerColor+    }     BoardGrid(         board = state.board,         selectedPiece = state.selectedPiece,         isFlipped = state.isBoardFlipped,         turn = state.turn,-        onSquareClick = actions::onSquareClick+        onSquareClick = actions::onSquareClick,+        dragEnabled = dragEnabled,     ) } @@ -648,6 +668,27 @@ private fun legalDestinations(     } } +/**+ * The shared 8x8 board: tap-to-select / tap-to-move, plus optional+ * drag-to-move ([dragEnabled]).+ *+ * Dragging is layered on the exact tap path so every mode keeps its own+ * semantics: a drag starts by "tapping" the piece square (the owning+ * ViewModel selects it, which also lights the legal destinations exactly+ * as a tap does) and a drop on a legal square "taps" that square, so+ * validation, puzzle/learn failure rules and the promotion picker all run+ * unchanged. Dropping anywhere else — or cancelling — animates the piece+ * back to its square and undoes the drag's own selection; a piece that+ * was already tapped-selected stays selected. Only pieces of the side to+ * move ([turn]) can be picked up, and callers gate [dragEnabled] on+ * their input state (game in progress / AI thinking, puzzle solving,+ * lesson playing). The default keeps the online board tap-only.+ *+ * [dragMoveLegal] decides which squares count as legal drops while+ * dragging (and are highlighted). It defaults to [Board.isValidMove];+ * Learn passes its pseudo-legal variant for kingless/illegal-move+ * lessons, mirroring LearnViewModel.+ */ @Composable fun BoardGrid(     board: com.vayunmathur.games.chess.data.Board,@@ -655,7 +696,9 @@ fun BoardGrid(     isFlipped: Boolean,     turn: PieceColor,     onSquareClick: (Position) -> Unit,-    showLastMove: Boolean = false+    showLastMove: Boolean = false,+    dragEnabled: Boolean = false,+    dragMoveLegal: ((Position, Position) -> Boolean)? = null, ) {     // The squares the selected piece can legally move to (for move-hint dots).     val destinations = remember(board, selectedPiece) {@@ -663,23 +706,178 @@ fun BoardGrid(     }     // The puzzle board seeds a synthetic zero-length lastMove to encode turn; skip it.     val lastMove = board.lastMove?.takeIf { showLastMove && it.start != it.end }-    Column(++    val click by rememberUpdatedState(onSquareClick)+    val liveBoard by rememberUpdatedState(board)+    val liveTurn by rememberUpdatedState(turn)+    val liveFlipped by rememberUpdatedState(isFlipped)+    val liveSelected by rememberUpdatedState(selectedPiece)+    val legal by rememberUpdatedState(dragMoveLegal)++    var boardSizePx by remember { mutableIntStateOf(0) }+    var dragFrom by remember { mutableStateOf<Position?>(null) }+    var dragPiece by remember { mutableStateOf<Piece?>(null) }+    var dragPoint by remember { mutableStateOf(Offset.Zero) }+    var dragAutoSelected by remember { mutableStateOf(false) }+    var settleFrom by remember { mutableStateOf<Offset?>(null) }+    var settling by remember { mutableStateOf(false) }+    // Shared motion helper (library.ui), never a raw compose-animation call:+    // 1f while the finger is down, tweened to 0f over Motion.DropMillis on+    // a snap-back, easing the overlay from the release point home.+    val settleProgress = animatedFloat(if (settling) 0f else 1f, Motion.drop())++    LaunchedEffect(settling) {+        if (settling) {+            delay(Motion.DropMillis.toLong())+            settling = false+            settleFrom = null+            dragFrom = null+            dragPiece = null+        }+    }++    fun positionAt(point: Offset): Position? {+        if (boardSizePx <= 0) return null+        val cell = boardSizePx / BOARD_SQUARES.toFloat()+        val visualCol = (point.x / cell).toInt()+        val visualRow = (point.y / cell).toInt()+        if (visualCol !in 0 until BOARD_SQUARES || visualRow !in 0 until BOARD_SQUARES) {+            return null+        }+        return if (liveFlipped) {+            Position(BOARD_SQUARES - 1 - visualRow, BOARD_SQUARES - 1 - visualCol)+        } else {+            Position(visualRow, visualCol)+        }+    }++    fun originCenter(pos: Position): Offset {+        val cell = boardSizePx / BOARD_SQUARES.toFloat()+        val visualRow = if (isFlipped) BOARD_SQUARES - 1 - pos.row else pos.row+        val visualCol = if (isFlipped) BOARD_SQUARES - 1 - pos.col else pos.col+        return Offset((visualCol + 0.5f) * cell, (visualRow + 0.5f) * cell)+    }++    fun finishDrag(commit: Boolean) {+        val from = dragFrom ?: return+        val to = if (commit) positionAt(dragPoint) else null+        val isLegal = to != null && to != from &&+            (legal?.invoke(from, to) ?: liveBoard.isValidMove(from, to))+        if (isLegal) {+            // Commit through the tap path; the ViewModel validates again+            // and owns promotion/failure handling.+            settling = false+            settleFrom = null+            dragFrom = null+            dragPiece = null+            click(to)+        } else {+            // Snap back: undo only the selection this drag created.+            if (dragAutoSelected) click(from)+            settleFrom = dragPoint+            settling = true+        }+        dragAutoSelected = false+    }++    val dragDestinations = remember(board, dragFrom, dragMoveLegal) {+        val from = dragFrom ?: return@remember emptySet()+        buildSet {+            for (i in 0 until BOARD_SQUARES) {+                for (j in 0 until BOARD_SQUARES) {+                    val end = Position(i, j)+                    if (dragMoveLegal?.invoke(from, end) ?: board.isValidMove(from, end)) {+                        add(end)+                    }+                }+            }+        }+    }+    val shownDestinations = if (dragFrom != null && !settling) dragDestinations else destinations++    Box(         Modifier             .fillMaxWidth()             .aspectRatio(1f)-            .graphicsLayer { if (isFlipped) rotationZ = FLIPPED_ROTATION }+            .onSizeChanged { boardSizePx = it.width }+            .pointerInput(dragEnabled) {+                if (!dragEnabled) return@pointerInput+                detectDragGestures(+                    onDragStart = { offset ->+                        if (settling) return@detectDragGestures+                        val from = positionAt(offset) ?: return@detectDragGestures+                        val piece = liveBoard.pieces[from.row][from.col]+                            ?: return@detectDragGestures+                        if (piece.color != liveTurn) return@detectDragGestures+                        dragFrom = from+                        dragPiece = piece+                        dragPoint = offset+                        dragAutoSelected = liveSelected != from+                        if (dragAutoSelected) click(from)+                    },+                    onDrag = { change, _ ->+                        if (dragFrom != null && !settling) {+                            change.consume()+                            dragPoint = change.position+                        }+                    },+                    onDragEnd = { finishDrag(commit = true) },+                    onDragCancel = { finishDrag(commit = false) },+                )+            }     ) {-        for (i in board.pieces.indices) {-            BoardRow(-                board = board,-                row = i,-                selectedPiece = selectedPiece,-                isFlipped = isFlipped,-                turn = turn,-                lastMove = lastMove,-                destinations = destinations,-                onSquareClick = onSquareClick-            )+        Column(+            Modifier+                .fillMaxSize()+                .graphicsLayer { if (isFlipped) rotationZ = FLIPPED_ROTATION }+        ) {+            for (i in board.pieces.indices) {+                BoardRow(+                    board = board,+                    row = i,+                    selectedPiece = selectedPiece,+                    isFlipped = isFlipped,+                    turn = turn,+                    lastMove = lastMove,+                    destinations = shownDestinations,+                    hiddenPiece = dragFrom,+                    onSquareClick = onSquareClick+                )+            }+        }+        // The lifted piece, drawn in unrotated board space above the grid.+        val from = dragFrom+        val piece = dragPiece+        if (from != null && piece != null && boardSizePx > 0) {+            val cellPx = boardSizePx / BOARD_SQUARES.toFloat()+            val center = if (settling) {+                val startPoint = settleFrom ?: dragPoint+                val home = originCenter(from)+                Offset(+                    home.x + (startPoint.x - home.x) * settleProgress,+                    home.y + (startPoint.y - home.y) * settleProgress,+                )+            } else {+                dragPoint+            }+            val cellDp = with(LocalDensity.current) { cellPx.toDp() }+            Box(+                Modifier+                    .offset {+                        IntOffset(+                            (center.x - cellPx / 2f).roundToInt(),+                            (center.y - cellPx / 2f).roundToInt(),+                        )+                    }+                    .size(cellDp)+                    .graphicsLayer {+                        scaleX = 1.08f+                        scaleY = 1.08f+                        alpha = 0.95f+                    }+            ) {+                ChessPiece(piece)+            }         }     } }@@ -693,6 +891,7 @@ private fun ColumnScope.BoardRow(     turn: PieceColor,     lastMove: com.vayunmathur.games.chess.data.Move?,     destinations: Set<Position>,+    hiddenPiece: Position?,     onSquareClick: (Position) -> Unit, ) {     Row(modifier = Modifier.weight(1f)) {@@ -709,8 +908,11 @@ private fun ColumnScope.BoardRow(                     }                     .then(highlightModifier(highlight))             ) {-                piece?.let {-                    ChessPiece(it, isFlipped = isFlipped)+                // The dragged piece is drawn by BoardGrid's overlay instead.+                if (Position(row, j) != hiddenPiece) {+                    piece?.let {+                        ChessPiece(it, isFlipped = isFlipped)+                    }                 }                 if (Position(row, j) in destinations) {                     MoveHintDot(piece != null)
games/chess/src/main/java/com/vayunmathur/games/chess/ChessLearnScreens.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessLearnScreens.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessLearnScreens.ktindex 719b7dafca..1e26abccb7 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessLearnScreens.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessLearnScreens.kt@@ -512,12 +512,22 @@ private const val STAR_STEP_DIVISOR = 5 fun LearnBoard(ui: LearnUiState, onSquareClick: (com.vayunmathur.games.chess.data.Position) -> Unit) {     // Capped like the game board so a desktop window doesn't blow the grid up.     Box(Modifier.widthIn(max = ChessBoardMaxWidth).fillMaxWidth().aspectRatio(1f)) {+        // Mirror LearnViewModel's legality choice so drag highlights and+        // drops accept exactly the moves a tap would on kingless /+        // illegal-move-offer lessons.+        val pseudoLegal = ui.level?.offerIllegalMove == true || ui.board.kingCount() < 2         BoardGrid(             board = ui.board,             selectedPiece = ui.selectedPiece,             isFlipped = ui.isFlipped,             turn = ui.playerColor,-            onSquareClick = onSquareClick+            onSquareClick = onSquareClick,+            dragEnabled = ui.status == LearnStatus.Playing,+            dragMoveLegal = if (pseudoLegal) {+                { from, to -> ui.board.isPseudoLegalMove(from, to) }+            } else {+                { from, to -> ui.board.isValidMove(from, to) }+            },         )         Canvas(             Modifier
games/chess/src/main/java/com/vayunmathur/games/chess/ChessPuzzleScreens.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessPuzzleScreens.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessPuzzleScreens.ktindex 2c0a2407af..9f7fa6fd1f 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/ChessPuzzleScreens.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/ChessPuzzleScreens.kt@@ -99,7 +99,10 @@ fun PuzzleBoardScreen(state: PuzzleUiState, actions: PuzzleActions) {                     isFlipped = state.isBoardFlipped,                     turn = state.playerColor,                     onSquareClick = actions::onSquareClick,-                    showLastMove = true+                    showLastMove = true,+                    // Same shared board: drag is just press-drag-release+                    // over the tap path, enabled only while solving.+                    dragEnabled = state.status == PuzzleStatus.Solving,                 )             }             Spacer(Modifier.height(16.dp))