From a5befed62c524af43ed2ab00eb474320442a94c1 Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 3 Jul 2023 19:41:12 +0200 Subject: [PATCH] Make promotions work. --- lib/api/move.dart | 17 +++-- lib/api/websocket_message.dart | 7 +- lib/chess/chess_app.dart | 2 +- lib/chess/chess_square.dart | 20 +++--- lib/chess_bloc/chess_bloc.dart | 58 ++++++++++++++-- lib/chess_bloc/chess_events.dart | 18 +++++ lib/chess_bloc/chess_position.dart | 82 ++++++++++++---------- lib/chess_bloc/promotion_bloc.dart | 51 +++++++++----- lib/connection/ws_connection.dart | 15 +++- lib/pages/chess_game.dart | 2 +- lib/pages/host_game.dart | 4 +- lib/pages/join_game.dart | 2 +- lib/pages/lobby_selector.dart | 37 +++++----- lib/pages/prepare_random_game.dart | 4 -- lib/utils/chess_utils.dart | 19 +++++ lib/utils/widgets/promotion_dialog.dart | 92 ++++++++++++++++--------- 16 files changed, 292 insertions(+), 138 deletions(-) diff --git a/lib/api/move.dart b/lib/api/move.dart index c269ae4..0c769f6 100644 --- a/lib/api/move.dart +++ b/lib/api/move.dart @@ -1,21 +1,30 @@ class ApiMove { final ApiCoordinate startSquare; final ApiCoordinate endSquare; + String? promotionToPiece; - const ApiMove({ + ApiMove({ required this.startSquare, required this.endSquare, + this.promotionToPiece, }); factory ApiMove.fromJson(Map json) { final startSquare = ApiCoordinate.fromJson(json['startSquare']); final endSquare = ApiCoordinate.fromJson(json['endSquare']); + final promotionToPiece = json['promotionToPiece']; - return ApiMove(startSquare: startSquare, endSquare: endSquare); + return ApiMove( + startSquare: startSquare, + endSquare: endSquare, + promotionToPiece: promotionToPiece); } - Map toJson() => - {'startSquare': startSquare, 'endSquare': endSquare}; + Map toJson() => { + 'startSquare': startSquare, + 'endSquare': endSquare, + 'promotionToPiece': promotionToPiece + }; } class ApiCoordinate { diff --git a/lib/api/websocket_message.dart b/lib/api/websocket_message.dart index 23f559b..2cd1349 100644 --- a/lib/api/websocket_message.dart +++ b/lib/api/websocket_message.dart @@ -58,6 +58,9 @@ class ApiWebsocketMessage { return ret; } - Map toJson() => - {'messageType': type, 'move': move, 'color': color}; + Map toJson() => { + 'messageType': type, + 'move': move, + 'color': color, + }; } diff --git a/lib/chess/chess_app.dart b/lib/chess/chess_app.dart index 14ba820..e491605 100644 --- a/lib/chess/chess_app.dart +++ b/lib/chess/chess_app.dart @@ -19,7 +19,7 @@ class ChessApp extends StatelessWidget { create: (context) => ChessBloc.getInstance(), ), BlocProvider( - create: (context) => PromotionUiBloc.getInstance(), + create: (context) => PromotionBloc.getInstance(), ) ], child: MaterialApp.router( diff --git a/lib/chess/chess_square.dart b/lib/chess/chess_square.dart index 2bfc450..e46b7e2 100644 --- a/lib/chess/chess_square.dart +++ b/lib/chess/chess_square.dart @@ -66,18 +66,20 @@ class ChessSquare extends StatelessWidget { } return true; }, - onAccept: (move) { + onAccept: (pieceDragged) { // Replace the dummy value with the actual target of the move. - move.toSquare = coordinate; + pieceDragged.toSquare = coordinate; - if (isPromotionMove(move)) { - PromotionUiBloc.getInstance().add(PawnMovedToPromotionField( - endSquare: move.toSquare, colorMoved: ChessBloc.myColor!)); - } else if (coordinate != move.fromSquare) { + if (isPromotionMove(pieceDragged)) { + var move = ChessMove( + from: pieceDragged.fromSquare, to: pieceDragged.toSquare); + PromotionBloc.getInstance().add(PawnMovedToPromotionField( + move: move, colorMoved: ChessBloc.myColor!)); + } else if (coordinate != pieceDragged.fromSquare) { ChessBloc.getInstance().add(OwnPieceMoved( - startSquare: move.fromSquare, - endSquare: move.toSquare, - piece: containedPiece!)); + startSquare: pieceDragged.fromSquare, + endSquare: pieceDragged.toSquare, + piece: pieceDragged.movedPiece!)); } }, builder: (context, candidateData, rejectedData) { diff --git a/lib/chess_bloc/chess_bloc.dart b/lib/chess_bloc/chess_bloc.dart index 80986d8..ba47f5b 100644 --- a/lib/chess_bloc/chess_bloc.dart +++ b/lib/chess_bloc/chess_bloc.dart @@ -1,7 +1,6 @@ import 'dart:convert'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:mchess/api/move.dart'; import 'package:mchess/api/websocket_message.dart'; import 'package:mchess/chess_bloc/chess_events.dart'; import 'package:mchess/chess_bloc/chess_position.dart'; @@ -22,7 +21,9 @@ class ChessBloc extends Bloc { on(initBoard); on(flipBoard); on(moveHandler); + on(promotionHandler); on(ownMoveHandler); + on(ownPromotionHandler); on(invalidMoveHandler); } @@ -64,14 +65,40 @@ class ChessBloc extends Bloc { ); } + void promotionHandler( + ReceivedPromotion event, Emitter emit) { + var pieceAtStartSquare = ChessPosition.getInstance().getPieceAt( + ChessCoordinate(event.startSquare.column, event.startSquare.row)); + if (pieceAtStartSquare == null) { + log('received a promotion but piece on start square was empty'); + return; + } + + ChessPieceClass pieceClass = ChessPieceClass.none; + for (var piece in chessPiecesShortName.entries) { + if (piece.value.toLowerCase() == event.piece) { + pieceClass = piece.key.pieceClass; + break; + } + } + var newPosition = ChessPosition.getInstance().currentPosition; + newPosition[ + ChessCoordinate(event.startSquare.column, event.startSquare.row)] = + const ChessPiece.none(); + newPosition[ChessCoordinate(event.endSquare.column, event.endSquare.row)] = + ChessPiece(pieceClass, pieceAtStartSquare.color); + + emit(ChessBoardState( + state.bottomColor, + turnColor, + newPosition, + )); + } + void ownMoveHandler(OwnPieceMoved event, Emitter emit) { log('ownMoveHandler()'); - - var start = ApiCoordinate( - col: event.startSquare.column, row: event.startSquare.row); - var end = - ApiCoordinate(col: event.endSquare.column, row: event.endSquare.row); - var apiMove = ApiMove(startSquare: start, endSquare: end); + var apiMove = + ChessMove(from: event.startSquare, to: event.endSquare).toApiMove(); var apiMessage = ApiWebsocketMessage( type: MessageType.move, move: apiMove, color: null, reason: null); @@ -92,6 +119,23 @@ class ChessBloc extends Bloc { ); } + void ownPromotionHandler( + OwnPromotionPlayed event, Emitter emit) { + var apiMove = event.move.toApiMove(); + var shorNameForPiece = chessPiecesShortName[ + ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor!)]! + .toLowerCase(); + apiMove.promotionToPiece = shorNameForPiece; + var message = ApiWebsocketMessage( + type: MessageType.move, + move: apiMove, + color: null, + reason: null, + ); + log(jsonEncode(message)); + ServerConnection.getInstance().send(jsonEncode(message)); + } + void invalidMoveHandler( InvalidMovePlayed event, Emitter emit) { emit( diff --git a/lib/chess_bloc/chess_events.dart b/lib/chess_bloc/chess_events.dart index 9d64f92..ce21f44 100644 --- a/lib/chess_bloc/chess_events.dart +++ b/lib/chess_bloc/chess_events.dart @@ -9,6 +9,17 @@ class ReceivedMove extends ChessEvent { ReceivedMove({required this.startSquare, required this.endSquare}); } +class ReceivedPromotion extends ChessEvent { + final ChessCoordinate startSquare; + final ChessCoordinate endSquare; + final String piece; + + ReceivedPromotion( + {required this.startSquare, + required this.endSquare, + required this.piece}); +} + class OwnPieceMoved extends ChessEvent { final ChessCoordinate startSquare; final ChessCoordinate endSquare; @@ -20,6 +31,13 @@ class OwnPieceMoved extends ChessEvent { required this.piece}); } +class OwnPromotionPlayed extends ChessEvent { + final ChessPieceClass pieceClass; + final ChessMove move; + + OwnPromotionPlayed({required this.pieceClass, required this.move}); +} + class InitBoard extends ChessEvent { InitBoard(); } diff --git a/lib/chess_bloc/chess_position.dart b/lib/chess_bloc/chess_position.dart index 5a5cd1b..252c71d 100644 --- a/lib/chess_bloc/chess_position.dart +++ b/lib/chess_bloc/chess_position.dart @@ -30,51 +30,61 @@ class ChessPosition { ChessPositionType pos = {}; for (int i = 1; i <= 8; i++) { - // pos[ChessCoordinate(i, 7)] = - // ChessPiece(ChessPieceName.blackPawn, ChessColor.black); - // pos[ChessCoordinate(i, 2)] = - // ChessPiece(ChessPieceName.whitePawn, ChessColor.white); + pos[ChessCoordinate(i, 7)] = + ChessPiece(ChessPieceClass.pawn, ChessColor.black); + pos[ChessCoordinate(i, 2)] = + ChessPiece(ChessPieceClass.pawn, ChessColor.white); } pos[ChessCoordinate(1, 7)] = ChessPiece(ChessPieceClass.pawn, ChessColor.white); + pos[ChessCoordinate(2, 7)] = + ChessPiece(ChessPieceClass.pawn, ChessColor.white); + pos[ChessCoordinate(8, 4)] = + ChessPiece(ChessPieceClass.king, ChessColor.white); + pos[ChessCoordinate(8, 6)] = + ChessPiece(ChessPieceClass.king, ChessColor.black); - // pos[ChessCoordinate(1, 8)] = - // ChessPiece(ChessPieceName.blackRook, ChessColor.black); - // pos[ChessCoordinate(2, 8)] = - // ChessPiece(ChessPieceName.blackKnight, ChessColor.black); - // pos[ChessCoordinate(3, 8)] = - // ChessPiece(ChessPieceName.blackBishop, ChessColor.black); - // pos[ChessCoordinate(4, 8)] = - // ChessPiece(ChessPieceName.blackQueen, ChessColor.black); - // pos[ChessCoordinate(5, 8)] = - // ChessPiece(ChessPieceName.blackKing, ChessColor.black); - // pos[ChessCoordinate(6, 8)] = - // ChessPiece(ChessPieceName.blackBishop, ChessColor.black); - // pos[ChessCoordinate(7, 8)] = - // ChessPiece(ChessPieceName.blackKnight, ChessColor.black); - // pos[ChessCoordinate(8, 8)] = - // ChessPiece(ChessPieceName.blackRook, ChessColor.black); + pos[ChessCoordinate(1, 8)] = + ChessPiece(ChessPieceClass.rook, ChessColor.black); + pos[ChessCoordinate(2, 8)] = + ChessPiece(ChessPieceClass.knight, ChessColor.black); + pos[ChessCoordinate(3, 8)] = + ChessPiece(ChessPieceClass.bishop, ChessColor.black); + pos[ChessCoordinate(4, 8)] = + ChessPiece(ChessPieceClass.queen, ChessColor.black); + pos[ChessCoordinate(5, 8)] = + ChessPiece(ChessPieceClass.king, ChessColor.black); + pos[ChessCoordinate(6, 8)] = + ChessPiece(ChessPieceClass.bishop, ChessColor.black); + pos[ChessCoordinate(7, 8)] = + ChessPiece(ChessPieceClass.knight, ChessColor.black); + pos[ChessCoordinate(8, 8)] = + ChessPiece(ChessPieceClass.rook, ChessColor.black); - // pos[ChessCoordinate(1, 1)] = - // ChessPiece(ChessPieceName.whiteRook, ChessColor.white); - // pos[ChessCoordinate(2, 1)] = - // ChessPiece(ChessPieceName.whiteKnight, ChessColor.white); - // pos[ChessCoordinate(3, 1)] = - // ChessPiece(ChessPieceName.whiteBishop, ChessColor.white); - // pos[ChessCoordinate(4, 1)] = - // ChessPiece(ChessPieceName.whiteQueen, ChessColor.white); - // pos[ChessCoordinate(5, 1)] = - // ChessPiece(ChessPieceName.whiteKing, ChessColor.white); - // pos[ChessCoordinate(6, 1)] = - // ChessPiece(ChessPieceName.whiteBishop, ChessColor.white); - // pos[ChessCoordinate(7, 1)] = - // ChessPiece(ChessPieceName.whiteKnight, ChessColor.white); - // pos[ChessCoordinate(8, 1)] = - // ChessPiece(ChessPieceName.whiteRook, ChessColor.white); + pos[ChessCoordinate(1, 1)] = + ChessPiece(ChessPieceClass.rook, ChessColor.white); + pos[ChessCoordinate(2, 1)] = + ChessPiece(ChessPieceClass.knight, ChessColor.white); + pos[ChessCoordinate(3, 1)] = + ChessPiece(ChessPieceClass.bishop, ChessColor.white); + pos[ChessCoordinate(4, 1)] = + ChessPiece(ChessPieceClass.queen, ChessColor.white); + pos[ChessCoordinate(5, 1)] = + ChessPiece(ChessPieceClass.king, ChessColor.white); + pos[ChessCoordinate(6, 1)] = + ChessPiece(ChessPieceClass.bishop, ChessColor.white); + pos[ChessCoordinate(7, 1)] = + ChessPiece(ChessPieceClass.knight, ChessColor.white); + pos[ChessCoordinate(8, 1)] = + ChessPiece(ChessPieceClass.rook, ChessColor.white); return pos; } + ChessPiece? getPieceAt(ChessCoordinate coordinate) { + return position[ChessCoordinate(coordinate.column, coordinate.row)]; + } + void resetToStartingPosition() { history = ChessMoveHistory.empty(growable: true); _instance = ChessPosition(position: _getStartingPosition()); diff --git a/lib/chess_bloc/promotion_bloc.dart b/lib/chess_bloc/promotion_bloc.dart index 5af0f7f..4a6498c 100644 --- a/lib/chess_bloc/promotion_bloc.dart +++ b/lib/chess_bloc/promotion_bloc.dart @@ -1,26 +1,40 @@ import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:mchess/chess_bloc/chess_bloc.dart'; +import 'package:mchess/chess_bloc/chess_events.dart'; import 'package:mchess/utils/chess_utils.dart'; -class PromotionUiBloc extends Bloc { - static final PromotionUiBloc _instance = PromotionUiBloc._internal(); +class PromotionBloc extends Bloc { + static final PromotionBloc _instance = PromotionBloc._internal(); + static late ChessMove move; - PromotionUiBloc._internal() : super(PromotionUiState.init()) { + PromotionBloc._internal() : super(PromotionState.init()) { on(promotionMoveHandler); + on(pieceChosenHandler); } void promotionMoveHandler( PawnMovedToPromotionField event, - Emitter emit, + Emitter emit, ) { - emit(PromotionUiState( + move = event.move; + emit(PromotionState( showPromotionDialog: true, colorMoved: event.colorMoved)); } - factory PromotionUiBloc.getInstance() { - return PromotionUiBloc(); + void pieceChosenHandler( + PieceChosen event, + Emitter emit, + ) { + ChessBloc.getInstance() + .add(OwnPromotionPlayed(pieceClass: event.pieceClass, move: move)); + emit(PromotionState(showPromotionDialog: false, colorMoved: event.color)); } - factory PromotionUiBloc() { + factory PromotionBloc.getInstance() { + return PromotionBloc(); + } + + factory PromotionBloc() { return _instance; } } @@ -28,22 +42,27 @@ class PromotionUiBloc extends Bloc { abstract class PromotionEvent {} class PawnMovedToPromotionField extends PromotionEvent { - final ChessCoordinate endSquare; + final ChessMove move; final ChessColor colorMoved; - PawnMovedToPromotionField( - {required this.endSquare, required this.colorMoved}); + PawnMovedToPromotionField({required this.move, required this.colorMoved}); } -class PromotionUiState { +class PieceChosen extends PromotionEvent { + final ChessPieceClass pieceClass; + final ChessColor color; + + PieceChosen({required this.pieceClass, required this.color}); +} + +class PromotionState { final bool showPromotionDialog; final ChessColor colorMoved; - PromotionUiState( - {required this.showPromotionDialog, required this.colorMoved}); + PromotionState({required this.showPromotionDialog, required this.colorMoved}); - factory PromotionUiState.init() { - return PromotionUiState( + factory PromotionState.init() { + return PromotionState( showPromotionDialog: false, colorMoved: ChessColor.white); } } diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 3f51648..605f21b 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -77,15 +77,24 @@ class ServerConnection { } void handleIncomingColorDeterminedMessage(ApiWebsocketMessage apiMessage) { + ConnectionCubit.getInstance().opponentConnected(); + ChessBloc.getInstance().add(InitBoard()); ChessBloc.getInstance().add( ColorDetermined(myColor: ChessColor.fromApiColor(apiMessage.color!))); - ConnectionCubit.getInstance().opponentConnected(); } void handleIncomingMoveMessage(ApiWebsocketMessage apiMessage) { var move = ChessMove.fromApiMove(apiMessage.move!); - ChessBloc.getInstance() - .add(ReceivedMove(startSquare: move.from, endSquare: move.to)); + + if (apiMessage.move?.promotionToPiece?.isNotEmpty ?? false) { + ChessBloc.getInstance().add(ReceivedPromotion( + startSquare: move.from, + endSquare: move.to, + piece: apiMessage.move!.promotionToPiece!)); + } else { + ChessBloc.getInstance() + .add(ReceivedMove(startSquare: move.from, endSquare: move.to)); + } } void handleInvalidMoveMessage(ApiWebsocketMessage apiMessage) { diff --git a/lib/pages/chess_game.dart b/lib/pages/chess_game.dart index d5d5580..d48928e 100644 --- a/lib/pages/chess_game.dart +++ b/lib/pages/chess_game.dart @@ -43,7 +43,7 @@ class _ChessGameState extends State { if (kDebugMode) const ServerLogWidget(textColor: Colors.white), Container( margin: const EdgeInsets.all(20), - child: BlocListener( + child: BlocListener( listener: (context, state) { if (state.showPromotionDialog) { promotionDialogBuilder(context, state.colorMoved); diff --git a/lib/pages/host_game.dart b/lib/pages/host_game.dart index 8d0e0ea..f5f9c61 100644 --- a/lib/pages/host_game.dart +++ b/lib/pages/host_game.dart @@ -27,8 +27,6 @@ class _HostGameWidgetState extends State { @override void initState() { - ChessBloc.getInstance().add(InitBoard()); - registerResponse = hostPrivateGame(); connectToWebsocket(registerResponse); @@ -130,7 +128,7 @@ class _HostGameWidgetState extends State { Future.delayed(const Duration(seconds: 2), () { ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar(snackBar); - context.pop(); + context.push('/'); // We go back to the lobby selector }); return null; } diff --git a/lib/pages/join_game.dart b/lib/pages/join_game.dart index 99f6a42..4c447f2 100644 --- a/lib/pages/join_game.dart +++ b/lib/pages/join_game.dart @@ -100,7 +100,7 @@ class _JoinGameWidgetState extends State { Future.delayed(const Duration(seconds: 2), () { ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar(snackBar); - context.pop(); + context.push('/'); // We go back to lobby selector }); return null; } diff --git a/lib/pages/lobby_selector.dart b/lib/pages/lobby_selector.dart index 6aad49a..0e4a187 100644 --- a/lib/pages/lobby_selector.dart +++ b/lib/pages/lobby_selector.dart @@ -13,24 +13,25 @@ class LobbySelector extends StatelessWidget { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - ElevatedButton( - onPressed: () { - context.push('/prepareRandom'); - }, - child: const Row( - mainAxisSize: MainAxisSize.min, - children: [ - Icon(Icons.question_mark), - SizedBox( - width: 10, - ), - Text('Random') - ], - ), - ), - const SizedBox( - height: 25, - ), + //We deactivate random lobbies for now. + // ElevatedButton( + // onPressed: () { + // context.push('/prepareRandom'); + // }, + // child: const Row( + // mainAxisSize: MainAxisSize.min, + // children: [ + // Icon(Icons.question_mark), + // SizedBox( + // width: 10, + // ), + // Text('Random') + // ], + // ), + // ), + // const SizedBox( + // height: 25, + // ), ElevatedButton( onPressed: () { _dialogBuilder(context); diff --git a/lib/pages/prepare_random_game.dart b/lib/pages/prepare_random_game.dart index a2b9aaf..f4e0782 100644 --- a/lib/pages/prepare_random_game.dart +++ b/lib/pages/prepare_random_game.dart @@ -24,12 +24,8 @@ class _PrepareRandomGameWidgetState extends State { @override void initState() { - ChessBloc.getInstance().add(InitBoard()); - randomGameResponse = registerForRandomGame(); - goToGameWhenResponseIsHere(randomGameResponse as Future); - super.initState(); } diff --git a/lib/utils/chess_utils.dart b/lib/utils/chess_utils.dart index 763de69..ac5b2c8 100644 --- a/lib/utils/chess_utils.dart +++ b/lib/utils/chess_utils.dart @@ -44,6 +44,14 @@ enum ChessColor { return white; } } + + ChessColor getOpposite() { + if (name == 'black') { + return white; + } else { + return black; + } + } } Map chessPiecesAssets = { @@ -173,6 +181,10 @@ class ChessCoordinate { return ChessCoordinate(apiCoordinate.col, apiCoordinate.row); } + ApiCoordinate toApiCoordinate() { + return ApiCoordinate(col: column, row: row); + } + ChessCoordinate.copyFrom(ChessCoordinate original) : column = original.column, row = original.row; @@ -276,6 +288,13 @@ class ChessMove { return ChessMove(from: start, to: end); } + ApiMove toApiMove() { + var toSquare = to.toApiCoordinate(); + var fromSquare = from.toApiCoordinate(); + return ApiMove( + startSquare: fromSquare, endSquare: toSquare, promotionToPiece: null); + } + @override operator ==(other) { return other is ChessMove && other.from == from && other.to == to; diff --git a/lib/utils/widgets/promotion_dialog.dart b/lib/utils/widgets/promotion_dialog.dart index 415fa67..3f38cf1 100644 --- a/lib/utils/widgets/promotion_dialog.dart +++ b/lib/utils/widgets/promotion_dialog.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:go_router/go_router.dart'; +import 'package:mchess/chess_bloc/promotion_bloc.dart'; import 'package:mchess/utils/chess_utils.dart'; class PromotionDialog extends StatelessWidget { @@ -8,40 +10,64 @@ class PromotionDialog extends StatelessWidget { @override Widget build(BuildContext context) { - return Card( - child: Column(children: [ - Expanded( - flex: 1, - child: Row( - children: [ - ElevatedButton( - onPressed: () {}, - child: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( - pieceClass: ChessPieceClass.queen, color: sideColor)]!), - ), - ElevatedButton( - onPressed: () {}, - child: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( - pieceClass: ChessPieceClass.rook, color: sideColor)]!), - ), - ElevatedButton( - onPressed: () {}, - child: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( - pieceClass: ChessPieceClass.knight, color: sideColor)]!), - ), - ElevatedButton( - onPressed: () {}, - child: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( - pieceClass: ChessPieceClass.bishop, color: sideColor)]!), - ) - ], + double windowWidth = MediaQuery.of(context).size.width; + double windowHeight = MediaQuery.of(context).size.height; + double iconSize; + + if (windowWidth < windowHeight) { + iconSize = 0.15 * windowWidth; + } else { + iconSize = 0.15 * windowHeight; + } + + return Dialog( + child: Row( + mainAxisSize: MainAxisSize.min, + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + IconButton( + onPressed: () { + pieceChosen(ChessPieceClass.queen); + context.pop(); + }, + icon: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( + pieceClass: ChessPieceClass.queen, color: sideColor)]!), + iconSize: 200, ), - ), - const Expanded( - flex: 3, - child: ColoredBox(color: Colors.red), - ) - ]), + IconButton( + onPressed: () { + pieceChosen(ChessPieceClass.rook); + context.pop(); + }, + icon: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( + pieceClass: ChessPieceClass.rook, color: sideColor)]!), + iconSize: 100, + ), + IconButton( + onPressed: () { + pieceChosen(ChessPieceClass.knight); + context.pop(); + }, + icon: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( + pieceClass: ChessPieceClass.knight, color: sideColor)]!), + iconSize: 10, + ), + IconButton( + onPressed: () { + pieceChosen(ChessPieceClass.bishop); + context.pop(); + }, + icon: SvgPicture.asset(chessPiecesAssets[ChessPieceAssetKey( + pieceClass: ChessPieceClass.bishop, color: sideColor)]!), + iconSize: iconSize, + ) + ], + ), ); } + + void pieceChosen(ChessPieceClass pieceClass) { + PromotionBloc.getInstance() + .add(PieceChosen(pieceClass: pieceClass, color: sideColor)); + } }