diff --git a/CHANGELOG.md b/CHANGELOG.md index 99bf42d..bc1a27c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,9 @@ ## 4.0.0 -- Add a `ChessBoardEditor` widget, intended to be used as the basis for a board editor like lichess.org/editor +- Add a `ChessboardEditor` widget, intended to be used as the basis for a board editor like lichess.org/editor - Chessground is now dependant on `dartchess`. It is only used to share common types: `Role`, `Side` and `Piece`. It is not used for any chess logic. - `SquareId` is now an extension type on String. -- `Board` was renamed to `ChessBoard`. -- `Move` was renamed to `BoardMove`. - Add the `writeFen` helper function. - Add the `legalMovesOf` helper function to convert a dartchess `Position` to a set of valid moves compatible with Chessground. diff --git a/README.md b/README.md index d8a9689..5650ccb 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ chess logic so you can use it with different chess variants. ## Getting started -This package exports a `ChessBoard` widget which can be interactable or not. It is +This package exports a `Chessboard` widget which can be interactable or not. It is entirely configurable with a `BoardSettings` object. ## Usage @@ -54,9 +54,9 @@ class _MyHomePageState extends State { title: const Text('Chessground demo'), ), body: Center( - child: ChessBoard( + child: Chessboard( size: screenWidth, - data: BoardData( + state: ChessboardState( interactableSide: InteractableSide.none, orientation: Side.white, fen: fen, diff --git a/example/lib/board_editor_page.dart b/example/lib/board_editor_page.dart index 14085d0..965d921 100644 --- a/example/lib/board_editor_page.dart +++ b/example/lib/board_editor_page.dart @@ -30,7 +30,7 @@ class _BoardEditorPageState extends State { colorScheme: BoardTheme.blue.colors, enableCoordinates: true, ); - final boardEditor = ChessBoardEditor( + final boardEditor = ChessboardEditor( size: screenWidth, orientation: dc.Side.white, pieces: pieces, diff --git a/example/lib/board_theme.dart b/example/lib/board_theme.dart index a69ea34..b6f58b2 100644 --- a/example/lib/board_theme.dart +++ b/example/lib/board_theme.dart @@ -2,33 +2,33 @@ import 'package:chessground/chessground.dart'; /// The chessboard theme. enum BoardTheme { - blue('Blue', BoardColorScheme.blue), - blue2('Blue2', BoardColorScheme.blue2), - blue3('Blue3', BoardColorScheme.blue3), - blueMarble('Blue Marble', BoardColorScheme.blueMarble), - canvas('Canvas', BoardColorScheme.canvas), - wood('Wood', BoardColorScheme.wood), - wood2('Wood2', BoardColorScheme.wood2), - wood3('Wood3', BoardColorScheme.wood3), - wood4('Wood4', BoardColorScheme.wood4), - maple('Maple', BoardColorScheme.maple), - maple2('Maple 2', BoardColorScheme.maple2), - brown('Brown', BoardColorScheme.brown), - leather('Leather', BoardColorScheme.leather), - green('Green', BoardColorScheme.green), - marble('Marble', BoardColorScheme.marble), - greenPlastic('Green Plastic', BoardColorScheme.greenPlastic), - grey('Grey', BoardColorScheme.grey), - metal('Metal', BoardColorScheme.metal), - olive('Olive', BoardColorScheme.olive), - newspaper('Newspaper', BoardColorScheme.newspaper), - purpleDiag('Purple-Diag', BoardColorScheme.purpleDiag), - pinkPyramid('Pink', BoardColorScheme.pinkPyramid), - horsey('Horsey', BoardColorScheme.horsey); + blue('Blue', ChessboardColorScheme.blue), + blue2('Blue2', ChessboardColorScheme.blue2), + blue3('Blue3', ChessboardColorScheme.blue3), + blueMarble('Blue Marble', ChessboardColorScheme.blueMarble), + canvas('Canvas', ChessboardColorScheme.canvas), + wood('Wood', ChessboardColorScheme.wood), + wood2('Wood2', ChessboardColorScheme.wood2), + wood3('Wood3', ChessboardColorScheme.wood3), + wood4('Wood4', ChessboardColorScheme.wood4), + maple('Maple', ChessboardColorScheme.maple), + maple2('Maple 2', ChessboardColorScheme.maple2), + brown('Brown', ChessboardColorScheme.brown), + leather('Leather', ChessboardColorScheme.leather), + green('Green', ChessboardColorScheme.green), + marble('Marble', ChessboardColorScheme.marble), + greenPlastic('Green Plastic', ChessboardColorScheme.greenPlastic), + grey('Grey', ChessboardColorScheme.grey), + metal('Metal', ChessboardColorScheme.metal), + olive('Olive', ChessboardColorScheme.olive), + newspaper('Newspaper', ChessboardColorScheme.newspaper), + purpleDiag('Purple-Diag', ChessboardColorScheme.purpleDiag), + pinkPyramid('Pink', ChessboardColorScheme.pinkPyramid), + horsey('Horsey', ChessboardColorScheme.horsey); final String label; - final BoardColorScheme colors; + final ChessboardColorScheme colors; const BoardTheme(this.label, this.colors); } diff --git a/example/lib/board_thumbnails.dart b/example/lib/board_thumbnails.dart index 47fe40a..aa2c1ef 100644 --- a/example/lib/board_thumbnails.dart +++ b/example/lib/board_thumbnails.dart @@ -19,9 +19,9 @@ class BoardThumbnailsPage extends StatelessWidget { children: [ for (final fen in positions) LayoutBuilder(builder: (context, constraints) { - return ChessBoard( + return Chessboard( size: constraints.biggest.width, - settings: BoardSettings( + settings: ChessboardSettings( enableCoordinates: false, borderRadius: BorderRadius.circular(5), boxShadow: [ @@ -43,7 +43,7 @@ class BoardThumbnailsPage extends StatelessWidget { ), ], ), - data: BoardData( + state: ChessboardState( interactableSide: InteractableSide.none, orientation: Side.white, fen: fen, diff --git a/example/lib/main.dart b/example/lib/main.dart index a084f19..f1f17b0 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -58,8 +58,8 @@ class _HomePageState extends State { dc.Position position = dc.Chess.initial; dc.Side orientation = dc.Side.white; String fen = dc.kInitialBoardFEN; - BoardMove? lastMove; - BoardMove? premove; + Move? lastMove; + Move? premove; ValidMoves validMoves = IMap(const {}); dc.Side sideToMove = dc.Side.white; PieceSet pieceSet = PieceSet.merida; @@ -134,9 +134,9 @@ class _HomePageState extends State { child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - ChessBoard( + Chessboard( size: screenWidth, - settings: BoardSettings( + settings: ChessboardSettings( pieceAssets: pieceSet.assets, colorScheme: boardTheme.colors, enableCoordinates: true, @@ -155,7 +155,7 @@ class _HomePageState extends State { ), pieceShiftMethod: pieceShiftMethod, ), - data: BoardData( + state: ChessboardState( interactableSide: playMode == Mode.botPlay ? InteractableSide.white : (position.turn == dc.Side.white @@ -369,13 +369,13 @@ class _HomePageState extends State { super.initState(); } - void _onSetPremove(BoardMove? move) { + void _onSetPremove(Move? move) { setState(() { premove = move; }); } - void _onUserMoveFreePlay(BoardMove move, {bool? isDrop, bool? isPremove}) { + void _onUserMoveFreePlay(Move move, {bool? isDrop, bool? isPremove}) { lastPos = position; final m = dc.Move.fromUci(move.uci)!; setState(() { @@ -386,8 +386,7 @@ class _HomePageState extends State { }); } - void _onUserMoveAgainstBot(BoardMove move, - {bool? isDrop, bool? isPremove}) async { + void _onUserMoveAgainstBot(Move move, {bool? isDrop, bool? isPremove}) async { lastPos = position; final m = dc.Move.fromUci(move.uci)!; setState(() { @@ -415,7 +414,7 @@ class _HomePageState extends State { final mv = (allMoves..shuffle()).first; setState(() { position = position.playUnchecked(mv); - lastMove = BoardMove( + lastMove = Move( from: SquareId(dc.toAlgebraic(mv.from)), to: SquareId(dc.toAlgebraic(mv.to))); fen = position.fen; diff --git a/lib/chessground.dart b/lib/chessground.dart index 7c4eaf8..d525097 100644 --- a/lib/chessground.dart +++ b/lib/chessground.dart @@ -5,9 +5,9 @@ library chessground; export 'src/board_color_scheme.dart'; export 'src/draw_shape_options.dart'; -export 'src/board_data.dart'; export 'src/board_editor_settings.dart'; export 'src/board_settings.dart'; +export 'src/board_state.dart'; export 'src/fen.dart'; export 'src/models.dart'; export 'src/piece_set.dart'; diff --git a/lib/src/board_color_scheme.dart b/lib/src/board_color_scheme.dart index 68e5a5d..310d140 100644 --- a/lib/src/board_color_scheme.dart +++ b/lib/src/board_color_scheme.dart @@ -5,13 +5,13 @@ import './models.dart'; const _boardsPath = 'assets/boards'; -/// Describes the color scheme of a [Board]. +/// Describes the color scheme of a [ChessboardBackground]. /// -/// Use the static const members to ensure flutter doesn't rebuild the board more -/// than once. +/// Use the `static const` members to ensure flutter doesn't rebuild the board +/// background more than necessary. @immutable -class BoardColorScheme { - const BoardColorScheme({ +class ChessboardColorScheme { + const ChessboardColorScheme({ required this.lightSquare, required this.darkSquare, required this.background, @@ -30,15 +30,15 @@ class BoardColorScheme { final Color darkSquare; /// Board background that defines light and dark square colors - final Background background; + final ChessboardBackground background; /// Board background that defines light and dark square colors and with white /// facing coordinates included - final Background whiteCoordBackground; + final ChessboardBackground whiteCoordBackground; /// Board background that defines light and dark square colors and with black /// facing coordinates included - final Background blackCoordBackground; + final ChessboardBackground blackCoordBackground; /// Color of highlighted last move final HighlightDetails lastMove; @@ -57,7 +57,7 @@ class BoardColorScheme { if (identical(this, other)) return true; if (other.runtimeType != runtimeType) return false; - return other is BoardColorScheme && + return other is ChessboardColorScheme && other.lightSquare == lightSquare && other.darkSquare == darkSquare && other.background == background && @@ -82,19 +82,19 @@ class BoardColorScheme { validPremoves, ); - static const brown = BoardColorScheme( + static const brown = ChessboardColorScheme( lightSquare: Color(0xfff0d9b6), darkSquare: Color(0xffb58863), - background: SolidColorBackground( + background: SolidColorChessboardBackground( lightSquare: Color(0xfff0d9b6), darkSquare: Color(0xffb58863), ), - whiteCoordBackground: SolidColorBackground( + whiteCoordBackground: SolidColorChessboardBackground( lightSquare: Color(0xfff0d9b6), darkSquare: Color(0xffb58863), coordinates: true, ), - blackCoordBackground: SolidColorBackground( + blackCoordBackground: SolidColorChessboardBackground( lightSquare: Color(0xfff0d9b6), darkSquare: Color(0xffb58863), coordinates: true, @@ -106,19 +106,19 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const blue = BoardColorScheme( + static const blue = ChessboardColorScheme( lightSquare: Color(0xffdee3e6), darkSquare: Color(0xff8ca2ad), - background: SolidColorBackground( + background: SolidColorChessboardBackground( lightSquare: Color(0xffdee3e6), darkSquare: Color(0xff8ca2ad), ), - whiteCoordBackground: SolidColorBackground( + whiteCoordBackground: SolidColorChessboardBackground( lightSquare: Color(0xffdee3e6), darkSquare: Color(0xff8ca2ad), coordinates: true, ), - blackCoordBackground: SolidColorBackground( + blackCoordBackground: SolidColorChessboardBackground( lightSquare: Color(0xffdee3e6), darkSquare: Color(0xff8ca2ad), coordinates: true, @@ -130,19 +130,19 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const green = BoardColorScheme( + static const green = ChessboardColorScheme( lightSquare: Color(0xffffffdd), darkSquare: Color(0xff86a666), - background: SolidColorBackground( + background: SolidColorChessboardBackground( lightSquare: Color(0xffffffdd), darkSquare: Color(0xff86a666), ), - whiteCoordBackground: SolidColorBackground( + whiteCoordBackground: SolidColorChessboardBackground( lightSquare: Color(0xffffffdd), darkSquare: Color(0xff86a666), coordinates: true, ), - blackCoordBackground: SolidColorBackground( + blackCoordBackground: SolidColorChessboardBackground( lightSquare: Color(0xffffffdd), darkSquare: Color(0xff86a666), coordinates: true, @@ -154,21 +154,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const blue2 = BoardColorScheme( + static const blue2 = ChessboardColorScheme( lightSquare: Color(0xff97b2c7), darkSquare: Color(0xff546f82), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xff97b2c7), darkSquare: Color(0xff546f82), image: AssetImage('$_boardsPath/blue2.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xff97b2c7), darkSquare: Color(0xff546f82), image: AssetImage('$_boardsPath/blue2.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xff97b2c7), darkSquare: Color(0xff546f82), image: AssetImage('$_boardsPath/blue2.jpg', package: 'chessground'), @@ -181,21 +181,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const blue3 = BoardColorScheme( + static const blue3 = ChessboardColorScheme( lightSquare: Color(0xffd9e0e6), darkSquare: Color(0xff315991), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffd9e0e6), darkSquare: Color(0xff315991), image: AssetImage('$_boardsPath/blue3.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd9e0e6), darkSquare: Color(0xff315991), image: AssetImage('$_boardsPath/blue3.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd9e0e6), darkSquare: Color(0xff315991), image: AssetImage('$_boardsPath/blue3.jpg', package: 'chessground'), @@ -208,21 +208,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const blueMarble = BoardColorScheme( + static const blueMarble = ChessboardColorScheme( lightSquare: Color(0xffeae6dd), darkSquare: Color(0xff7c7f87), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffeae6dd), darkSquare: Color(0xff7c7f87), image: AssetImage('$_boardsPath/blue-marble.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffeae6dd), darkSquare: Color(0xff7c7f87), image: AssetImage('$_boardsPath/blue-marble.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffeae6dd), darkSquare: Color(0xff7c7f87), image: AssetImage('$_boardsPath/blue-marble.jpg', package: 'chessground'), @@ -235,21 +235,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const canvas = BoardColorScheme( + static const canvas = ChessboardColorScheme( lightSquare: Color(0xffd7daeb), darkSquare: Color(0xff547388), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffd7daeb), darkSquare: Color(0xff547388), image: AssetImage('$_boardsPath/canvas2.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd7daeb), darkSquare: Color(0xff547388), image: AssetImage('$_boardsPath/canvas2.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd7daeb), darkSquare: Color(0xff547388), image: AssetImage('$_boardsPath/canvas2.jpg', package: 'chessground'), @@ -262,23 +262,23 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const greenPlastic = BoardColorScheme( + static const greenPlastic = ChessboardColorScheme( lightSquare: Color(0xfff2f9bb), darkSquare: Color(0xff59935d), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xfff2f9bb), darkSquare: Color(0xff59935d), image: AssetImage('$_boardsPath/green-plastic.png', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xfff2f9bb), darkSquare: Color(0xff59935d), image: AssetImage('$_boardsPath/green-plastic.png', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xfff2f9bb), darkSquare: Color(0xff59935d), image: @@ -292,22 +292,22 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const grey = BoardColorScheme( + static const grey = ChessboardColorScheme( lightSquare: Color(0xffb8b8b8), darkSquare: Color(0xff7d7d7d), lastMove: HighlightDetails(solidColor: Color(0x809cc700)), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffb8b8b8), darkSquare: Color(0xff7d7d7d), image: AssetImage('$_boardsPath/grey.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffb8b8b8), darkSquare: Color(0xff7d7d7d), image: AssetImage('$_boardsPath/grey.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffb8b8b8), darkSquare: Color(0xff7d7d7d), image: AssetImage('$_boardsPath/grey.jpg', package: 'chessground'), @@ -319,21 +319,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const horsey = BoardColorScheme( + static const horsey = ChessboardColorScheme( lightSquare: Color(0xfff0d9b5), darkSquare: Color(0xff946f51), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xfff0d9b5), darkSquare: Color(0xff946f51), image: AssetImage('$_boardsPath/horsey.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xfff0d9b5), darkSquare: Color(0xff946f51), image: AssetImage('$_boardsPath/horsey.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xfff0d9b5), darkSquare: Color(0xff946f51), image: AssetImage('$_boardsPath/horsey.jpg', package: 'chessground'), @@ -356,21 +356,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const leather = BoardColorScheme( + static const leather = ChessboardColorScheme( lightSquare: Color(0xffd1d1c9), darkSquare: Color(0xffc28e16), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffd1d1c9), darkSquare: Color(0xffc28e16), image: AssetImage('$_boardsPath/leather.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd1d1c9), darkSquare: Color(0xffc28e16), image: AssetImage('$_boardsPath/leather.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd1d1c9), darkSquare: Color(0xffc28e16), image: AssetImage('$_boardsPath/leather.jpg', package: 'chessground'), @@ -383,21 +383,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const maple = BoardColorScheme( + static const maple = ChessboardColorScheme( lightSquare: Color(0xffe8ceab), darkSquare: Color(0xffbc7944), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffe8ceab), darkSquare: Color(0xffbc7944), image: AssetImage('$_boardsPath/maple.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe8ceab), darkSquare: Color(0xffbc7944), image: AssetImage('$_boardsPath/maple.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe8ceab), darkSquare: Color(0xffbc7944), image: AssetImage('$_boardsPath/maple.jpg', package: 'chessground'), @@ -410,21 +410,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const maple2 = BoardColorScheme( + static const maple2 = ChessboardColorScheme( lightSquare: Color(0xffe2c89f), darkSquare: Color(0xff996633), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffe2c89f), darkSquare: Color(0xff996633), image: AssetImage('$_boardsPath/maple2.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe2c89f), darkSquare: Color(0xff996633), image: AssetImage('$_boardsPath/maple2.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe2c89f), darkSquare: Color(0xff996633), image: AssetImage('$_boardsPath/maple2.jpg', package: 'chessground'), @@ -437,21 +437,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const marble = BoardColorScheme( + static const marble = ChessboardColorScheme( lightSquare: Color(0xff93ab91), darkSquare: Color(0xff4f644e), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xff93ab91), darkSquare: Color(0xff4f644e), image: AssetImage('$_boardsPath/marble.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xff93ab91), darkSquare: Color(0xff4f644e), image: AssetImage('$_boardsPath/marble.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xff93ab91), darkSquare: Color(0xff4f644e), image: AssetImage('$_boardsPath/marble.jpg', package: 'chessground'), @@ -464,21 +464,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const metal = BoardColorScheme( + static const metal = ChessboardColorScheme( lightSquare: Color(0xffc9c9c9), darkSquare: Color(0xff727272), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffc9c9c9), darkSquare: Color(0xff727272), image: AssetImage('$_boardsPath/metal.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffc9c9c9), darkSquare: Color(0xff727272), image: AssetImage('$_boardsPath/metal.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffc9c9c9), darkSquare: Color(0xff727272), image: AssetImage('$_boardsPath/metal.jpg', package: 'chessground'), @@ -491,21 +491,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const newspaper = BoardColorScheme( + static const newspaper = ChessboardColorScheme( lightSquare: Color(0xffffffff), darkSquare: Color(0xff8d8d8d), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffffffff), darkSquare: Color(0xff8d8d8d), image: AssetImage('$_boardsPath/newspaper.png', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffffffff), darkSquare: Color(0xff8d8d8d), image: AssetImage('$_boardsPath/newspaper.png', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffffffff), darkSquare: Color(0xff8d8d8d), image: AssetImage('$_boardsPath/newspaper.png', package: 'chessground'), @@ -518,21 +518,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const olive = BoardColorScheme( + static const olive = ChessboardColorScheme( lightSquare: Color(0xffb8b19f), darkSquare: Color(0xff6d6655), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffb8b19f), darkSquare: Color(0xff6d6655), image: AssetImage('$_boardsPath/olive.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffb8b19f), darkSquare: Color(0xff6d6655), image: AssetImage('$_boardsPath/olive.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffb8b19f), darkSquare: Color(0xff6d6655), image: AssetImage('$_boardsPath/olive.jpg', package: 'chessground'), @@ -545,23 +545,23 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const pinkPyramid = BoardColorScheme( + static const pinkPyramid = ChessboardColorScheme( lightSquare: Color(0xffe8e9b7), darkSquare: Color(0xffed7272), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffe8e9b7), darkSquare: Color(0xffed7272), image: AssetImage('$_boardsPath/pink-pyramid.png', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe8e9b7), darkSquare: Color(0xffed7272), image: AssetImage('$_boardsPath/pink-pyramid.png', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe8e9b7), darkSquare: Color(0xffed7272), image: @@ -575,21 +575,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const purpleDiag = BoardColorScheme( + static const purpleDiag = ChessboardColorScheme( lightSquare: Color(0xffe5daf0), darkSquare: Color(0xff957ab0), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffe5daf0), darkSquare: Color(0xff957ab0), image: AssetImage('$_boardsPath/purple-diag.png', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe5daf0), darkSquare: Color(0xff957ab0), image: AssetImage('$_boardsPath/purple-diag.png', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffe5daf0), darkSquare: Color(0xff957ab0), image: AssetImage('$_boardsPath/purple-diag.png', package: 'chessground'), @@ -602,21 +602,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const wood = BoardColorScheme( + static const wood = ChessboardColorScheme( lightSquare: Color(0xffd8a45b), darkSquare: Color(0xff9b4d0f), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffd8a45b), darkSquare: Color(0xff9b4d0f), image: AssetImage('$_boardsPath/wood.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd8a45b), darkSquare: Color(0xff9b4d0f), image: AssetImage('$_boardsPath/wood.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd8a45b), darkSquare: Color(0xff9b4d0f), image: AssetImage('$_boardsPath/wood.jpg', package: 'chessground'), @@ -629,21 +629,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const wood2 = BoardColorScheme( + static const wood2 = ChessboardColorScheme( lightSquare: Color(0xffa38b5d), darkSquare: Color(0xff6c5017), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffa38b5d), darkSquare: Color(0xff6c5017), image: AssetImage('$_boardsPath/wood2.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffa38b5d), darkSquare: Color(0xff6c5017), image: AssetImage('$_boardsPath/wood2.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffa38b5d), darkSquare: Color(0xff6c5017), image: AssetImage('$_boardsPath/wood2.jpg', package: 'chessground'), @@ -656,21 +656,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const wood3 = BoardColorScheme( + static const wood3 = ChessboardColorScheme( lightSquare: Color(0xffd0ceca), darkSquare: Color(0xff755839), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffd0ceca), darkSquare: Color(0xff755839), image: AssetImage('$_boardsPath/wood3.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd0ceca), darkSquare: Color(0xff755839), image: AssetImage('$_boardsPath/wood3.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffd0ceca), darkSquare: Color(0xff755839), image: AssetImage('$_boardsPath/wood3.jpg', package: 'chessground'), @@ -683,21 +683,21 @@ class BoardColorScheme { validPremoves: Color(0x40203085), ); - static const wood4 = BoardColorScheme( + static const wood4 = ChessboardColorScheme( lightSquare: Color(0xffcaaf7d), darkSquare: Color(0xff7b5330), - background: ImageBackground( + background: ImageChessboardBackground( lightSquare: Color(0xffcaaf7d), darkSquare: Color(0xff7b5330), image: AssetImage('$_boardsPath/wood4.jpg', package: 'chessground'), ), - whiteCoordBackground: ImageBackground( + whiteCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffcaaf7d), darkSquare: Color(0xff7b5330), image: AssetImage('$_boardsPath/wood4.jpg', package: 'chessground'), coordinates: true, ), - blackCoordBackground: ImageBackground( + blackCoordBackground: ImageChessboardBackground( lightSquare: Color(0xffcaaf7d), darkSquare: Color(0xff7b5330), image: AssetImage('$_boardsPath/wood4.jpg', package: 'chessground'), diff --git a/lib/src/board_editor_settings.dart b/lib/src/board_editor_settings.dart index 32bb59f..dda0980 100644 --- a/lib/src/board_editor_settings.dart +++ b/lib/src/board_editor_settings.dart @@ -13,7 +13,7 @@ class BoardEditorSettings { /// Creates a new [BoardEditorSettings] with the provided values. const BoardEditorSettings({ // theme - this.colorScheme = BoardColorScheme.brown, + this.colorScheme = ChessboardColorScheme.brown, this.pieceAssets = PieceSet.cburnettAssets, // visual settings this.borderRadius = BorderRadius.zero, @@ -24,7 +24,7 @@ class BoardEditorSettings { }); /// Theme of the board. - final BoardColorScheme colorScheme; + final ChessboardColorScheme colorScheme; /// Piece set. final PieceAssets pieceAssets; @@ -75,7 +75,7 @@ class BoardEditorSettings { /// Creates a copy of this [BoardEditorSettings] but with the given fields replaced with the new values. BoardEditorSettings copyWith({ - BoardColorScheme? colorScheme, + ChessboardColorScheme? colorScheme, PieceAssets? pieceAssets, BorderRadiusGeometry? borderRadius, List? boxShadow, diff --git a/lib/src/board_settings.dart b/lib/src/board_settings.dart index ed149c4..c690c54 100644 --- a/lib/src/board_settings.dart +++ b/lib/src/board_settings.dart @@ -17,15 +17,15 @@ enum PieceShiftMethod { either; } -/// Board settings that control the theme, behavior and purpose of the board. +/// Board settings that controls visual aspects and behavior of the board. /// /// This is meant for fixed settings that don't change during a game. Sensible /// defaults are provided. @immutable -class BoardSettings { - const BoardSettings({ +class ChessboardSettings { + const ChessboardSettings({ // theme - this.colorScheme = BoardColorScheme.brown, + this.colorScheme = ChessboardColorScheme.brown, this.pieceAssets = PieceSet.cburnettAssets, // visual settings this.borderRadius = BorderRadius.zero, @@ -49,7 +49,7 @@ class BoardSettings { }); /// Theme of the board - final BoardColorScheme colorScheme; + final ChessboardColorScheme colorScheme; /// Piece set final PieceAssets pieceAssets; @@ -106,7 +106,7 @@ class BoardSettings { if (other.runtimeType != runtimeType) { return false; } - return other is BoardSettings && + return other is ChessboardSettings && other.colorScheme == colorScheme && other.pieceAssets == pieceAssets && other.borderRadius == borderRadius && @@ -145,8 +145,8 @@ class BoardSettings { drawShape, ); - BoardSettings copyWith({ - BoardColorScheme? colorScheme, + ChessboardSettings copyWith({ + ChessboardColorScheme? colorScheme, PieceAssets? pieceAssets, BorderRadiusGeometry? borderRadius, List? boxShadow, @@ -163,7 +163,7 @@ class BoardSettings { PieceShiftMethod? pieceShiftMethod, DrawShapeOptions? drawShape, }) { - return BoardSettings( + return ChessboardSettings( colorScheme: colorScheme ?? this.colorScheme, pieceAssets: pieceAssets ?? this.pieceAssets, borderRadius: borderRadius ?? this.borderRadius, diff --git a/lib/src/board_data.dart b/lib/src/board_state.dart similarity index 85% rename from lib/src/board_data.dart rename to lib/src/board_state.dart index e3dac19..5296fd7 100644 --- a/lib/src/board_data.dart +++ b/lib/src/board_state.dart @@ -4,27 +4,29 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart'; import 'models.dart'; -/// Board data. +/// The chessboard state. /// -/// Used to configure the board with state that will/may change during a game. +/// This state should be updated after every move during a game. +/// +/// To make a fixed board, set [interactableSide] to [InteractableSide.none]. @immutable -abstract class BoardData { - /// Creates a new [BoardData] with the provided values. - const factory BoardData({ +abstract class ChessboardState { + /// Creates a new [ChessboardState] with the provided values. + const factory ChessboardState({ required InteractableSide interactableSide, required Side orientation, required String fen, bool opponentsPiecesUpsideDown, Side? sideToMove, - BoardMove? premove, - BoardMove? lastMove, + Move? premove, + Move? lastMove, ValidMoves? validMoves, bool? isCheck, ISet? shapes, IMap? annotations, - }) = _BoardData; + }) = _ChessboardState; - const BoardData._({ + const ChessboardState._({ required this.interactableSide, required this.orientation, required this.fen, @@ -60,10 +62,10 @@ abstract class BoardData { final String fen; /// Registered premove. Will be played right after the next opponent move. - final BoardMove? premove; + final Move? premove; /// Last move played, used to highlight corresponding squares. - final BoardMove? lastMove; + final Move? lastMove; /// Set of [Move] allowed to be played by current side to move. final ValidMoves? validMoves; @@ -80,7 +82,7 @@ abstract class BoardData { @override bool operator ==(Object other) => identical(this, other) || - other is BoardData && + other is ChessboardState && runtimeType == other.runtimeType && interactableSide == other.interactableSide && orientation == other.orientation && @@ -109,15 +111,15 @@ abstract class BoardData { annotations, ); - /// Creates a copy of this [BoardData] but with the given fields replaced with the new values. - BoardData copyWith({ + /// Creates a copy of this [ChessboardState] but with the given fields replaced with the new values. + ChessboardState copyWith({ InteractableSide? interactableSide, Side? orientation, String? fen, bool? opponentsPiecesUpsideDown, Side? sideToMove, - BoardMove? premove, - BoardMove? lastMove, + Move? premove, + Move? lastMove, ValidMoves? validMoves, bool? isCheck, ISet? shapes, @@ -125,8 +127,8 @@ abstract class BoardData { }); } -class _BoardData extends BoardData { - const _BoardData({ +class _ChessboardState extends ChessboardState { + const _ChessboardState({ required super.interactableSide, required super.orientation, required super.fen, @@ -141,7 +143,7 @@ class _BoardData extends BoardData { }) : super._(); @override - BoardData copyWith({ + ChessboardState copyWith({ InteractableSide? interactableSide, Side? orientation, String? fen, @@ -154,7 +156,7 @@ class _BoardData extends BoardData { Object? shapes = _Undefined, Object? annotations = _Undefined, }) { - return BoardData( + return ChessboardState( interactableSide: interactableSide ?? this.interactableSide, orientation: orientation ?? this.orientation, opponentsPiecesUpsideDown: @@ -162,8 +164,8 @@ class _BoardData extends BoardData { fen: fen ?? this.fen, sideToMove: sideToMove == _Undefined ? this.sideToMove : sideToMove as Side?, - premove: premove == _Undefined ? this.premove : premove as BoardMove?, - lastMove: lastMove == _Undefined ? this.lastMove : lastMove as BoardMove?, + premove: premove == _Undefined ? this.premove : premove as Move?, + lastMove: lastMove == _Undefined ? this.lastMove : lastMove as Move?, validMoves: validMoves == _Undefined ? this.validMoves : validMoves as ValidMoves?, diff --git a/lib/src/models.dart b/lib/src/models.dart index ab6d311..76db7f5 100644 --- a/lib/src/models.dart +++ b/lib/src/models.dart @@ -143,7 +143,7 @@ class HighlightDetails { final AssetImage? image; } -/// Zero-based numeric board coordinate. +/// Zero-based numeric chessboard coordinate. /// /// For instance a1 is (0, 0), a2 is (0, 1), etc. @immutable @@ -218,14 +218,14 @@ class PositionedPiece { /// A chess move from one [SquareId] to another, with an optional promotion. @immutable -class BoardMove { - const BoardMove({ +class Move { + const Move({ required this.from, required this.to, this.promotion, }); - BoardMove.fromUci(String uci) + Move.fromUci(String uci) : from = SquareId(uci.substring(0, 2)), to = SquareId(uci.substring(2, 4)), promotion = uci.length > 4 ? _toRole(uci.substring(4)) : null; @@ -242,8 +242,8 @@ class BoardMove { return from == squareId || to == squareId; } - BoardMove withPromotion(Role promotion) { - return BoardMove( + Move withPromotion(Role promotion) { + return Move( from: from, to: to, promotion: promotion, @@ -253,7 +253,7 @@ class BoardMove { @override bool operator ==(Object other) { return identical(this, other) || - other is BoardMove && + other is Move && other.runtimeType == runtimeType && other.from == from && other.to == to && diff --git a/lib/src/widgets/background.dart b/lib/src/widgets/background.dart index fc225c6..7c9a7de 100644 --- a/lib/src/widgets/background.dart +++ b/lib/src/widgets/background.dart @@ -3,9 +3,9 @@ import 'package:flutter/widgets.dart'; /// Base widget for the background of the chessboard. /// -/// See [SolidColorBackground] and [ImageBackground] for concrete implementations. -abstract class Background extends StatelessWidget { - const Background({ +/// See [SolidColorChessboardBackground] and [ImageChessboardBackground] for concrete implementations. +abstract class ChessboardBackground extends StatelessWidget { + const ChessboardBackground({ super.key, this.coordinates = false, this.orientation = Side.white, @@ -20,8 +20,8 @@ abstract class Background extends StatelessWidget { } /// A chessboard background with solid color squares. -class SolidColorBackground extends Background { - const SolidColorBackground({ +class SolidColorChessboardBackground extends ChessboardBackground { + const SolidColorChessboardBackground({ super.key, super.coordinates, super.orientation, @@ -69,8 +69,8 @@ class SolidColorBackground extends Background { } /// A chessboard background made of an image. -class ImageBackground extends Background { - const ImageBackground({ +class ImageChessboardBackground extends ChessboardBackground { + const ImageChessboardBackground({ super.key, super.coordinates, super.orientation, diff --git a/lib/src/widgets/board.dart b/lib/src/widgets/board.dart index 9c1e358..8652912 100644 --- a/lib/src/widgets/board.dart +++ b/lib/src/widgets/board.dart @@ -15,15 +15,15 @@ import '../models.dart'; import '../fen.dart'; import '../premove.dart'; import '../board_settings.dart'; -import '../board_data.dart'; +import '../board_state.dart'; /// Number of logical pixels that have to be dragged before a drag starts. const double _kDragDistanceThreshold = 3.0; const _kCancelShapesDoubleTapDelay = Duration(milliseconds: 200); -/// A mixin that provides geometry information about the board. -mixin BoardGeometry { +/// A mixin that provides geometry information about the chessboard. +mixin ChessboardGeometry { /// Visual size of the board. double get size; @@ -61,12 +61,12 @@ mixin BoardGeometry { /// /// This widget can be used to display a static board, a dynamic board that /// shows a live game, or a full user interactable board. -class ChessBoard extends StatefulWidget with BoardGeometry { - const ChessBoard({ +class Chessboard extends StatefulWidget with ChessboardGeometry { + const Chessboard({ super.key, required this.size, - required this.data, - this.settings = const BoardSettings(), + required this.state, + this.settings = const ChessboardSettings(), this.onMove, this.onPremove, }); @@ -75,34 +75,34 @@ class ChessBoard extends StatefulWidget with BoardGeometry { final double size; @override - Side get orientation => data.orientation; + Side get orientation => state.orientation; /// Settings that control the theme, behavior and purpose of the board. - final BoardSettings settings; + final ChessboardSettings settings; - /// Data that represents the current state of the board. - final BoardData data; + /// Current state of the board. + final ChessboardState state; /// Callback called after a move has been made. - final void Function(BoardMove, {bool? isDrop, bool? isPremove})? onMove; + final void Function(Move, {bool? isDrop, bool? isPremove})? onMove; /// Callback called after a premove has been set/unset. /// /// If the callback is null, the board will not allow premoves. - final void Function(BoardMove?)? onPremove; + final void Function(Move?)? onPremove; @override // ignore: library_private_types_in_public_api _BoardState createState() => _BoardState(); } -class _BoardState extends State { +class _BoardState extends State { Pieces pieces = {}; Map translatingPieces = {}; Map fadingPieces = {}; SquareId? selected; - BoardMove? _promotionMove; - BoardMove? _lastDrop; + Move? _promotionMove; + Move? _lastDrop; Set? _premoveDests; bool _shouldDeselectOnTapUp = false; @@ -149,31 +149,31 @@ class _BoardState extends State { final colorScheme = widget.settings.colorScheme; final ISet moveDests = widget.settings.showValidMoves && selected != null && - widget.data.validMoves != null - ? widget.data.validMoves![selected!] ?? _emptyValidMoves + widget.state.validMoves != null + ? widget.state.validMoves![selected!] ?? _emptyValidMoves : _emptyValidMoves; final Set premoveDests = widget.settings.showValidMoves ? _premoveDests ?? {} : {}; - final shapes = widget.data.shapes ?? _emptyShapes; - final annotations = widget.data.annotations ?? _emptyAnnotations; - final checkSquare = widget.data.isCheck == true ? _getKingSquare() : null; - final premove = widget.data.premove; + final shapes = widget.state.shapes ?? _emptyShapes; + final annotations = widget.state.annotations ?? _emptyAnnotations; + final checkSquare = widget.state.isCheck == true ? _getKingSquare() : null; + final premove = widget.state.premove; final background = widget.settings.enableCoordinates - ? widget.data.orientation == Side.white + ? widget.state.orientation == Side.white ? colorScheme.whiteCoordBackground : colorScheme.blackCoordBackground : colorScheme.background; final List highlightedBackground = [ background, - if (widget.settings.showLastMove && widget.data.lastMove != null) - for (final squareId in widget.data.lastMove!.squares) + if (widget.settings.showLastMove && widget.state.lastMove != null) + for (final squareId in widget.state.lastMove!.squares) if (premove == null || !premove.hasSquare(squareId)) PositionedSquare( key: ValueKey('$squareId-lastMove'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: squareId, child: Highlight( size: widget.squareSize, @@ -181,12 +181,12 @@ class _BoardState extends State { ), ), if (premove != null && - widget.data.interactableSide != InteractableSide.none) + widget.state.interactableSide != InteractableSide.none) for (final squareId in premove.squares) PositionedSquare( key: ValueKey('$squareId-premove'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: squareId, child: Highlight( size: widget.squareSize, @@ -197,7 +197,7 @@ class _BoardState extends State { PositionedSquare( key: ValueKey('${selected!}-selected'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: selected!, child: Highlight( size: widget.squareSize, @@ -208,7 +208,7 @@ class _BoardState extends State { PositionedSquare( key: ValueKey('$dest-dest'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: dest, child: MoveDest( size: widget.squareSize, @@ -220,7 +220,7 @@ class _BoardState extends State { PositionedSquare( key: ValueKey('$dest-premove-dest'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: dest, child: MoveDest( size: widget.squareSize, @@ -232,7 +232,7 @@ class _BoardState extends State { PositionedSquare( key: ValueKey('$checkSquare-check'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: checkSquare, child: CheckHighlight(size: widget.squareSize), ), @@ -243,7 +243,7 @@ class _BoardState extends State { PositionedSquare( key: ValueKey('${entry.key}-${entry.value}-fading'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: entry.key, child: AnimatedPieceFadeOut( duration: widget.settings.animationDuration, @@ -263,7 +263,7 @@ class _BoardState extends State { PositionedSquare( key: ValueKey('${entry.key}-${entry.value}'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: entry.key, child: PieceWidget( piece: entry.value, @@ -277,12 +277,12 @@ class _BoardState extends State { PositionedSquare( key: ValueKey('${entry.key}-${entry.value.$1.piece}'), size: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: entry.key, child: AnimatedPieceTranslation( fromCoord: entry.value.$1.coord, toCoord: entry.value.$2.coord, - orientation: widget.data.orientation, + orientation: widget.state.orientation, duration: widget.settings.animationDuration, onComplete: () { translatingPieces.remove(entry.key); @@ -302,7 +302,7 @@ class _BoardState extends State { '${entry.key}-${entry.value.symbol}-${entry.value.color}', ), squareSize: widget.squareSize, - orientation: widget.data.orientation, + orientation: widget.state.orientation, squareId: entry.key, annotation: entry.value, ), @@ -310,18 +310,18 @@ class _BoardState extends State { ShapeWidget( shape: shape, boardSize: widget.size, - orientation: widget.data.orientation, + orientation: widget.state.orientation, ), if (_shapeAvatar != null) ShapeWidget( shape: _shapeAvatar!, boardSize: widget.size, - orientation: widget.data.orientation, + orientation: widget.state.orientation, ), ]; final interactable = - widget.data.interactableSide != InteractableSide.none || + widget.state.interactableSide != InteractableSide.none || widget.settings.drawShape.enable; return Listener( @@ -347,15 +347,15 @@ class _BoardState extends State { else ...highlightedBackground, ...objects, - if (_promotionMove != null && widget.data.sideToMove != null) + if (_promotionMove != null && widget.state.sideToMove != null) PromotionSelector( pieceAssets: widget.settings.pieceAssets, move: _promotionMove!, squareSize: widget.squareSize, - color: widget.data.sideToMove!, - orientation: widget.data.orientation, - piecesUpsideDown: widget.data.opponentsPiecesUpsideDown && - widget.data.sideToMove! != widget.data.orientation, + color: widget.state.sideToMove!, + orientation: widget.state.orientation, + piecesUpsideDown: widget.state.opponentsPiecesUpsideDown && + widget.state.sideToMove! != widget.state.orientation, onSelect: _onPromotionSelect, onCancel: _onPromotionCancel, ), @@ -368,7 +368,7 @@ class _BoardState extends State { @override void initState() { super.initState(); - pieces = readFen(widget.data.fen); + pieces = readFen(widget.state.fen); } @override @@ -378,7 +378,7 @@ class _BoardState extends State { } @override - void didUpdateWidget(ChessBoard oldBoard) { + void didUpdateWidget(Chessboard oldBoard) { super.didUpdateWidget(oldBoard); if (oldBoard.settings.drawShape.enable && !widget.settings.drawShape.enable) { @@ -386,7 +386,7 @@ class _BoardState extends State { _drawOrigin = null; _shapeAvatar = null; } - if (widget.data.interactableSide == InteractableSide.none) { + if (widget.state.interactableSide == InteractableSide.none) { _currentPointerDownEvent = null; _dragAvatar?.cancel(); _dragAvatar = null; @@ -394,25 +394,25 @@ class _BoardState extends State { selected = null; _premoveDests = null; } - if (oldBoard.data.sideToMove != widget.data.sideToMove) { + if (oldBoard.state.sideToMove != widget.state.sideToMove) { _premoveDests = null; _promotionMove = null; if (widget.onPremove != null && - widget.data.premove != null && - widget.data.sideToMove?.name == widget.data.interactableSide.name) { + widget.state.premove != null && + widget.state.sideToMove?.name == widget.state.interactableSide.name) { Timer.run(() { if (mounted) _tryPlayPremove(); }); } } - if (oldBoard.data.fen == widget.data.fen) { + if (oldBoard.state.fen == widget.state.fen) { _lastDrop = null; // as long as the fen is the same as before let's keep animations return; } translatingPieces = {}; fadingPieces = {}; - final newPieces = readFen(widget.data.fen); + final newPieces = readFen(widget.state.fen); final List newOnSquare = []; final List missingOnSquare = []; final Set animatedOrigins = {}; @@ -463,7 +463,7 @@ class _BoardState extends State { SquareId? _getKingSquare() { for (final square in pieces.keys) { - if (pieces[square]!.color == widget.data.sideToMove && + if (pieces[square]!.color == widget.state.sideToMove && pieces[square]!.role == Role.king) { return square; } @@ -476,7 +476,7 @@ class _BoardState extends State { final coord = widget.offsetCoord(localPosition); if (coord == null) return null; final localOffset = - coord.offset(widget.data.orientation, widget.squareSize); + coord.offset(widget.state.orientation, widget.squareSize); final tmpOffset = box.localToGlobal(localOffset); return Offset( tmpOffset.dx - widget.squareSize / 2, @@ -532,7 +532,7 @@ class _BoardState extends State { } } - if (widget.data.interactableSide == InteractableSide.none) return; + if (widget.state.interactableSide == InteractableSide.none) return; // From here on, we only allow 1 pointer to interact with the board. Other // pointers will cancel any current gesture. @@ -570,7 +570,7 @@ class _BoardState extends State { canCastle: widget.settings.enablePremoveCastling, ); }); - } else if (widget.data.premove != null) { + } else if (widget.state.premove != null) { widget.onPremove?.call(null); setState(() { selected = null; @@ -749,7 +749,7 @@ class _BoardState extends State { _shouldDeselectOnTapUp = false; } - void _onPromotionSelect(BoardMove move, Piece promoted) { + void _onPromotionSelect(Move move, Piece promoted) { setState(() { pieces[move.to] = promoted; _promotionMove = null; @@ -757,14 +757,14 @@ class _BoardState extends State { widget.onMove?.call(move.withPromotion(promoted.role), isDrop: true); } - void _onPromotionCancel(BoardMove move) { + void _onPromotionCancel(Move move) { setState(() { - pieces = readFen(widget.data.fen); + pieces = readFen(widget.state.fen); _promotionMove = null; }); } - void _openPromotionSelector(BoardMove move) { + void _openPromotionSelector(Move move) { setState(() { final pawn = pieces.remove(move.from); pieces[move.to] = pawn!; @@ -775,29 +775,29 @@ class _BoardState extends State { /// Whether the piece should be displayed upside down, according to the /// widget settings. bool _isUpsideDown(Piece piece) { - return widget.data.opponentsPiecesUpsideDown && - piece.color != widget.data.orientation; + return widget.state.opponentsPiecesUpsideDown && + piece.color != widget.state.orientation; } /// Whether the piece is movable by the current side to move. bool _isMovable(Piece? piece) { return piece != null && - (widget.data.interactableSide == InteractableSide.both || - widget.data.interactableSide.name == piece.color.name) && - widget.data.sideToMove == piece.color; + (widget.state.interactableSide == InteractableSide.both || + widget.state.interactableSide.name == piece.color.name) && + widget.state.sideToMove == piece.color; } /// Whether the piece is premovable by the current side to move. bool _isPremovable(Piece? piece) { return piece != null && (widget.onPremove != null && - widget.data.interactableSide.name == piece.color.name && - widget.data.sideToMove != piece.color); + widget.state.interactableSide.name == piece.color.name && + widget.state.sideToMove != piece.color); } /// Whether the piece is allowed to be moved to the target square. bool _canMoveTo(SquareId orig, SquareId dest) { - final validDests = widget.data.validMoves?[orig]; + final validDests = widget.state.validMoves?[orig]; return orig != dest && validDests != null && validDests.contains(dest); } @@ -822,7 +822,7 @@ class _BoardState extends State { bool _tryMoveOrPremoveTo(SquareId squareId, {bool drop = false}) { final selectedPiece = selected != null ? pieces[selected] : null; if (selectedPiece != null && _canMoveTo(selected!, squareId)) { - final move = BoardMove(from: selected!, to: squareId); + final move = Move(from: selected!, to: squareId); if (drop) { _lastDrop = move; } @@ -838,7 +838,7 @@ class _BoardState extends State { return true; } else if (_isPremovable(selectedPiece) && _canPremoveTo(selected!, squareId)) { - widget.onPremove?.call(BoardMove(from: selected!, to: squareId)); + widget.onPremove?.call(Move(from: selected!, to: squareId)); return true; } return false; @@ -846,7 +846,7 @@ class _BoardState extends State { /// Tries to play the premove if it is set and still valid. void _tryPlayPremove() { - final premove = widget.data.premove; + final premove = widget.state.premove; if (premove == null) { return; } diff --git a/lib/src/widgets/board_editor.dart b/lib/src/widgets/board_editor.dart index 36ffc17..a293f82 100644 --- a/lib/src/widgets/board_editor.dart +++ b/lib/src/widgets/board_editor.dart @@ -34,8 +34,8 @@ enum EditorPointerMode { /// /// A [writeFen] method is provided by this package to convert the current state /// of the board editor to a FEN string. -class ChessBoardEditor extends StatefulWidget with BoardGeometry { - const ChessBoardEditor({ +class ChessboardEditor extends StatefulWidget with ChessboardGeometry { + const ChessboardEditor({ super.key, required this.size, required this.orientation, @@ -93,10 +93,10 @@ class ChessBoardEditor extends StatefulWidget with BoardGeometry { final void Function(SquareId square)? onDiscardedPiece; @override - State createState() => _BoardEditorState(); + State createState() => _BoardEditorState(); } -class _BoardEditorState extends State { +class _BoardEditorState extends State { SquareId? draggedPieceOrigin; @override @@ -217,7 +217,7 @@ class _BoardEditorState extends State { /// The [Piece] to show under the pointer when a drag is under way. /// -/// You can use this to drag pieces onto a [ChessBoardEditor] with the same +/// You can use this to drag pieces onto a [ChessboardEditor] with the same /// appearance as when the pieces on the board are dragged. class PieceDragFeedback extends StatelessWidget { const PieceDragFeedback({ diff --git a/lib/src/widgets/promotion.dart b/lib/src/widgets/promotion.dart index ce2ae26..16cafd1 100644 --- a/lib/src/widgets/promotion.dart +++ b/lib/src/widgets/promotion.dart @@ -23,13 +23,13 @@ class PromotionSelector extends StatelessWidget { }); final PieceAssets pieceAssets; - final BoardMove move; + final Move move; final Side color; final double squareSize; final Side orientation; final bool piecesUpsideDown; - final void Function(BoardMove, Piece) onSelect; - final void Function(BoardMove) onCancel; + final void Function(Move, Piece) onSelect; + final void Function(Move) onCancel; SquareId get squareId => move.to; diff --git a/test/board_data_test.dart b/test/board_data_test.dart index 3d19861..361dea8 100644 --- a/test/board_data_test.dart +++ b/test/board_data_test.dart @@ -3,16 +3,16 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:chessground/chessground.dart'; void main() { - group('BoardData', () { + group('ChessboardState', () { test('implements hashCode/==', () { expect( - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', ), - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, @@ -20,13 +20,13 @@ void main() { ), ); expect( - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', ).hashCode, - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, @@ -35,14 +35,14 @@ void main() { ); expect( - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', ), isNot( - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, @@ -52,14 +52,14 @@ void main() { ); expect( - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', ).hashCode, isNot( - const BoardData( + const ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, @@ -70,7 +70,7 @@ void main() { }); test('copyWith', () { - const boardData = BoardData( + const boardData = ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, sideToMove: Side.white, @@ -113,12 +113,12 @@ void main() { }); test('copyWith, nullable values', () { - const boardData = BoardData( + const boardData = ChessboardState( interactableSide: InteractableSide.both, orientation: Side.white, fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', sideToMove: Side.white, - lastMove: BoardMove(from: SquareId('e2'), to: SquareId('e4')), + lastMove: Move(from: SquareId('e2'), to: SquareId('e4')), ); // pass null values to non-nullable fields should not change the field diff --git a/test/board_settings_test.dart b/test/board_settings_test.dart index 85ebe7a..4847ddb 100644 --- a/test/board_settings_test.dart +++ b/test/board_settings_test.dart @@ -2,16 +2,19 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:chessground/chessground.dart'; void main() { - group('BoardSettings', () { + group('ChessboardSettings', () { test('implements hashCode/==', () { - expect(const BoardSettings(), const BoardSettings()); - expect(const BoardSettings().hashCode, const BoardSettings().hashCode); + expect(const ChessboardSettings(), const ChessboardSettings()); + expect( + const ChessboardSettings().hashCode, + const ChessboardSettings().hashCode, + ); expect( - const BoardSettings(), + const ChessboardSettings(), isNot( - const BoardSettings( - colorScheme: BoardColorScheme.blue, + const ChessboardSettings( + colorScheme: ChessboardColorScheme.blue, ), ), ); @@ -19,17 +22,17 @@ void main() { test('copyWith', () { expect( - const BoardSettings().copyWith(), - const BoardSettings(), + const ChessboardSettings().copyWith(), + const ChessboardSettings(), ); expect( - const BoardSettings() + const ChessboardSettings() .copyWith( - colorScheme: BoardColorScheme.blue, + colorScheme: ChessboardColorScheme.blue, ) .colorScheme, - BoardColorScheme.blue, + ChessboardColorScheme.blue, ); }); }); diff --git a/test/widgets/board_editor_test.dart b/test/widgets/board_editor_test.dart index 9b15465..3955283 100644 --- a/test/widgets/board_editor_test.dart +++ b/test/widgets/board_editor_test.dart @@ -11,7 +11,7 @@ void main() { group('BoardEditor', () { testWidgets('empty board has no pieces', (WidgetTester tester) async { await tester.pumpWidget(buildBoard(pieces: {})); - expect(find.byType(ChessBoardEditor), findsOneWidget); + expect(find.byType(ChessboardEditor), findsOneWidget); expect(find.byType(PieceWidget), findsNothing); for (final square in allSquares) { @@ -192,7 +192,7 @@ void main() { MaterialApp( home: Column( children: [ - ChessBoardEditor( + ChessboardEditor( size: boardSize, orientation: Side.white, pieces: const {}, @@ -259,7 +259,7 @@ Widget buildBoard({ void Function(SquareId square)? onDiscardedPiece, }) { return MaterialApp( - home: ChessBoardEditor( + home: ChessboardEditor( size: boardSize, orientation: orientation, pointerMode: pointerMode, diff --git a/test/widgets/board_test.dart b/test/widgets/board_test.dart index e836959..046e980 100644 --- a/test/widgets/board_test.dart +++ b/test/widgets/board_test.dart @@ -14,14 +14,14 @@ void main() { group('Non-interactable board', () { const viewOnlyBoard = Directionality( textDirection: TextDirection.ltr, - child: ChessBoard( + child: Chessboard( size: boardSize, - data: BoardData( + state: ChessboardState( interactableSide: InteractableSide.none, orientation: Side.white, fen: dc.kInitialFEN, ), - settings: BoardSettings( + settings: ChessboardSettings( drawShape: DrawShapeOptions(enable: true), ), ), @@ -30,7 +30,7 @@ void main() { testWidgets('initial position display', (WidgetTester tester) async { await tester.pumpWidget(viewOnlyBoard); - expect(find.byType(ChessBoard), findsOneWidget); + expect(find.byType(Chessboard), findsOneWidget); expect(find.byType(PieceWidget), findsNWidgets(32)); }); @@ -372,7 +372,7 @@ void main() { testWidgets('promotion, auto queen enabled', (WidgetTester tester) async { await tester.pumpWidget( buildBoard( - settings: const BoardSettings(autoQueenPromotion: true), + settings: const ChessboardSettings(autoQueenPromotion: true), initialInteractableSide: InteractableSide.both, initialFen: '8/5P2/2RK2P1/8/4k3/8/8/7r w - - 0 1', ), @@ -931,7 +931,7 @@ Future makeMove(WidgetTester tester, String from, String to) async { Widget buildBoard({ required InteractableSide initialInteractableSide, - BoardSettings? settings, + ChessboardSettings? settings, Side orientation = Side.white, String initialFen = dc.kInitialFEN, ISet? initialShapes, @@ -944,14 +944,14 @@ Widget buildBoard({ InteractableSide interactableSide = initialInteractableSide; dc.Position position = dc.Chess.fromSetup(dc.Setup.parseFen(initialFen)); - BoardMove? lastMove; - BoardMove? premove; + Move? lastMove; + Move? premove; ISet shapes = initialShapes ?? ISet(); return MaterialApp( home: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { - final defaultSettings = BoardSettings( + final defaultSettings = ChessboardSettings( drawShape: DrawShapeOptions( enable: enableDrawingShapes, onCompleteShape: (shape) { @@ -965,10 +965,10 @@ Widget buildBoard({ pieceShiftMethod: pieceShiftMethod, ); - return ChessBoard( + return Chessboard( size: boardSize, settings: settings ?? defaultSettings, - data: BoardData( + state: ChessboardState( interactableSide: interactableSide, orientation: orientation, fen: position.fen, @@ -980,7 +980,7 @@ Widget buildBoard({ premove: premove, shapes: shapes, ), - onMove: (BoardMove move, {bool? isDrop, bool? isPremove}) { + onMove: (Move move, {bool? isDrop, bool? isPremove}) { setState(() { position = position.playUnchecked(dc.Move.fromUci(move.uci)!); if (position.isGameOver) { @@ -1002,12 +1002,12 @@ Widget buildBoard({ if (position.isGameOver) { interactableSide = InteractableSide.none; } - lastMove = BoardMove.fromUci(opponentMove.uci); + lastMove = Move.fromUci(opponentMove.uci); }); }); } }, - onPremove: (BoardMove? move) { + onPremove: (Move? move) { setState(() { premove = move; });