Cancel an in-progress input move if the piece being held changes#165
Cancel an in-progress input move if the piece being held changes#165thearst3rd wants to merge 1 commit into
Conversation
|
Thanks for the PR — this is a real rough edge and cancelling the move via the existing callback is the right instinct. A few things I'd like to see addressed before merging: 1. The reset path can corrupt the position. if (this.fromSquare && !this.toSquare && this.movedPiece) {
this.chessboard.state.position.setPiece(this.fromSquare, this.movedPiece)
}That stamps the old held piece back onto 2. Only 3. Minor: The |
|
This is great feedback thanks, not sure how I missed The reset path corruption is absolutely worth looking into - in my testing it seems to work out ok but I'll see if something I'm doing is masking the issue. |
|
One more thing I noticed while looking at the reset path, to round out the review above: 4. The cancel callback fires with this.setMoveInputState(MOVE_INPUT_STATE.reset) // sets this.fromSquare = null
this.moveInputCanceledCallback(this.fromSquare, null, MOVE_CANCELED_REASON.movedPieceChanged)The reset case nulls const moveStartSquare = this.fromSquare
this.setMoveInputState(MOVE_INPUT_STATE.reset)
this.moveInputCanceledCallback(moveStartSquare, null, MOVE_CANCELED_REASON.movedOutOfBoard)So the fix for this path likely looks like: remember No rush on this — just capturing it here so the review is complete. |
Previously, if you click a piece to select it or start dragging it, and that piece gets changed when the position changes (i.e. the piece you're holding gets captured), the chessboard ends up in a weird state where an invalid piece is still being held. If it's being dragged, that will make the new piece turn invisible (since dragged pieces become invisible in their original spot until the drag finishes) and is otherwise kinda glitchy.
Since there's already a well defined callback for a move getting cancelled, this seems like a good place to use it. Now, when the position changes and there's a piece being held, it checks if the piece on that square is different than what it was and if so, it will cancel the move.