From 200393ac76fcd25f2f5b2224fcb44be24822be24 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 1 Feb 2024 11:37:00 +0100 Subject: [PATCH] Fix tapping on opponent's piece With the previous change of starting a tap on a drag, we removed the GestureDetector from all squares with any pieces. This included the opponent's pieces. Tapping and taking an opponent's piece was not possible anymore. This was a bug, since we still want to detect a tap and take an opponent's piece. --- lib/chess/chess_square.dart | 4 +++- lib/chess/chess_square_outer_dragtarget.dart | 4 ++-- lib/chess_bloc/chess_bloc.dart | 6 +++--- lib/chess_bloc/tap_bloc.dart | 4 ++-- lib/pages/lobby_selector.dart | 6 ++++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/chess/chess_square.dart b/lib/chess/chess_square.dart index ac0ee0e..509c4ca 100644 --- a/lib/chess/chess_square.dart +++ b/lib/chess/chess_square.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:mchess/chess/chess_square_outer_dragtarget.dart'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess_bloc/tap_bloc.dart'; import '../utils/chess_utils.dart'; @@ -66,7 +67,8 @@ class _ChessSquareState extends State { containedPiece: widget.containedPiece ?? const ChessPiece.none()), ); - if (widget.containedPiece == null) { + if (widget.containedPiece == null || + widget.containedPiece!.color != ChessBloc.getMyColor()) { return GestureDetector( child: dragTarget, onTap: () { diff --git a/lib/chess/chess_square_outer_dragtarget.dart b/lib/chess/chess_square_outer_dragtarget.dart index 221bb9a..2fb6322 100644 --- a/lib/chess/chess_square_outer_dragtarget.dart +++ b/lib/chess/chess_square_outer_dragtarget.dart @@ -30,13 +30,13 @@ class ChessSquareOuterDragTarget extends StatelessWidget { if (isPromotionMove( details.data.movedPiece!.pieceClass, - ChessBloc.myColor!, + ChessBloc.getMyColor(), details.data.toSquare, )) { var move = ChessMove( from: details.data.fromSquare, to: details.data.toSquare); PromotionBloc().add(PawnMovedToPromotionField( - move: move, colorMoved: ChessBloc.myColor!)); + move: move, colorMoved: ChessBloc.myColor)); } else if (coordinate != details.data.fromSquare) { ChessBloc().add(OwnPieceMoved( startSquare: details.data.fromSquare, diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 8eee842..ab63ebe 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -11,9 +11,9 @@ import 'package:mchess/utils/chess_utils.dart'; class ChessBloc extends Bloc { static final ChessBloc _instance = ChessBloc._internal(); static ChessColor turnColor = ChessColor.white; - static ChessColor? myColor = ChessColor.white; + static ChessColor myColor = ChessColor.white; - static ChessColor? getSidesColor() { + static ChessColor getMyColor() { return myColor; } @@ -126,7 +126,7 @@ class ChessBloc extends Bloc { OwnPromotionPlayed event, Emitter emit) { var apiMove = event.move.toApiMove(); var shorNameForPiece = chessPiecesShortName[ - ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor!)]!; + ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor)]!; apiMove.promotionToPiece = shorNameForPiece; var message = ApiWebsocketMessage( type: MessageType.move, diff --git a/lib/chess_bloc/tap_bloc.dart b/lib/chess_bloc/tap_bloc.dart index cf343f9..6d9bcf4 100644 --- a/lib/chess_bloc/tap_bloc.dart +++ b/lib/chess_bloc/tap_bloc.dart @@ -51,12 +51,12 @@ class TapBloc extends Bloc { state.firstSquareTapped != event.tapped) { if (isPromotionMove( state.pieceOnFirstTappedSquare!.pieceClass, - ChessBloc.myColor!, + ChessBloc.myColor, event.tapped, )) { PromotionBloc().add(PawnMovedToPromotionField( move: ChessMove(from: state.firstSquareTapped!, to: event.tapped), - colorMoved: ChessBloc.myColor!)); + colorMoved: ChessBloc.myColor)); emit(TapState.init()); return; } else { diff --git a/lib/pages/lobby_selector.dart b/lib/pages/lobby_selector.dart index 58393cc..67f38aa 100644 --- a/lib/pages/lobby_selector.dart +++ b/lib/pages/lobby_selector.dart @@ -168,8 +168,10 @@ class _LobbySelectorState extends State { backgroundColor: Colors.amberAccent, content: Text("mChess server is not responding. Try again or give up"), ); - ScaffoldMessenger.of(context).clearSnackBars(); - ScaffoldMessenger.of(context).showSnackBar(snackBar); + if (mounted) { + ScaffoldMessenger.of(context).clearSnackBars(); + ScaffoldMessenger.of(context).showSnackBar(snackBar); + } return null; }