-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from lichess-org/pointer-tool-mode
Board editor with pointer tool mode
- Loading branch information
Showing
34 changed files
with
1,865 additions
and
896 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
import 'package:board_example/board_theme.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:chessground/chessground.dart'; | ||
import 'package:dartchess/dartchess.dart' as dc; | ||
import 'package:collection/collection.dart'; | ||
|
||
class BoardEditorPage extends StatefulWidget { | ||
const BoardEditorPage({super.key}); | ||
|
||
@override | ||
State<StatefulWidget> createState() => _BoardEditorPageState(); | ||
} | ||
|
||
class _BoardEditorPageState extends State<BoardEditorPage> { | ||
Pieces pieces = readFen(dc.kInitialFEN); | ||
|
||
/// The piece to add when a square is touched. If null, will delete the piece. | ||
dc.Piece? pieceToAddOnTouch; | ||
|
||
PointerToolMode pointerMode = PointerToolMode.drag; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final double screenWidth = MediaQuery.of(context).size.width; | ||
|
||
const PieceSet pieceSet = PieceSet.merida; | ||
|
||
final settings = BoardEditorSettings( | ||
pieceAssets: pieceSet.assets, | ||
colorScheme: BoardTheme.blue.colors, | ||
enableCoordinates: true, | ||
); | ||
final boardEditor = ChessBoardEditor( | ||
size: screenWidth, | ||
orientation: dc.Side.white, | ||
pieces: pieces, | ||
settings: settings, | ||
pointerToolMode: pointerMode, | ||
onTouchedSquare: (squareId) => setState(() { | ||
if (pieceToAddOnTouch != null) { | ||
pieces[squareId] = pieceToAddOnTouch!; | ||
} else { | ||
pieces.remove(squareId); | ||
} | ||
}), | ||
onDiscardedPiece: (squareId) => setState(() { | ||
pieces.remove(squareId); | ||
}), | ||
onDroppedPiece: (origin, destination, piece) => setState(() { | ||
pieces[destination] = piece; | ||
if (origin != null && origin != destination) { | ||
pieces.remove(origin); | ||
} | ||
}), | ||
); | ||
|
||
makePieceMenu(side) => PieceMenu( | ||
side: side, | ||
pieceSet: pieceSet, | ||
squareSize: boardEditor.squareSize, | ||
settings: settings, | ||
pieceEdition: | ||
pointerMode == PointerToolMode.edit ? pieceToAddOnTouch : null, | ||
pieceTapped: (role) => setState(() { | ||
pieceToAddOnTouch = dc.Piece(role: role, color: side); | ||
pointerMode = PointerToolMode.edit; | ||
}), | ||
pointerMode: pointerMode, | ||
deleteTapped: () => setState(() { | ||
pieceToAddOnTouch = null; | ||
pointerMode = PointerToolMode.edit; | ||
}), | ||
pointerModeTapped: () => setState(() { | ||
pointerMode = PointerToolMode.drag; | ||
}), | ||
); | ||
|
||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Board Editor'), | ||
), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: [ | ||
makePieceMenu(dc.Side.black), | ||
boardEditor, | ||
makePieceMenu(dc.Side.white), | ||
Padding( | ||
padding: const EdgeInsets.symmetric(horizontal: 8.0), | ||
child: | ||
SizedBox(height: 50, child: Text('FEN: ${writeFen(pieces)}')), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class PieceMenu extends StatelessWidget { | ||
const PieceMenu({ | ||
super.key, | ||
required this.side, | ||
required this.pieceSet, | ||
required this.squareSize, | ||
required this.pieceEdition, | ||
required this.pointerMode, | ||
required this.settings, | ||
required this.pieceTapped, | ||
required this.deleteTapped, | ||
required this.pointerModeTapped, | ||
}); | ||
|
||
final dc.Side side; | ||
final PieceSet pieceSet; | ||
final double squareSize; | ||
|
||
/// The piece that is currently being edited. | ||
/// | ||
/// If null while [pointerMode] is [PointerToolMode.edit], the user is in delete mode. | ||
final dc.Piece? pieceEdition; | ||
|
||
final PointerToolMode pointerMode; | ||
final BoardEditorSettings settings; | ||
final Function(dc.Role role) pieceTapped; | ||
final Function() deleteTapped; | ||
final Function() pointerModeTapped; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
color: Colors.grey, | ||
child: Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Container( | ||
color: pointerMode == PointerToolMode.drag | ||
? Colors.green | ||
: Colors.transparent, | ||
child: GestureDetector( | ||
onTap: () => pointerModeTapped(), | ||
child: Icon( | ||
Icons.pan_tool_alt_outlined, | ||
size: squareSize, | ||
), | ||
), | ||
), | ||
...dc.Role.values.mapIndexed( | ||
(i, role) { | ||
final piece = dc.Piece(role: role, color: side); | ||
final pieceWidget = PieceWidget( | ||
piece: piece, | ||
size: squareSize, | ||
pieceAssets: pieceSet.assets, | ||
); | ||
|
||
return Container( | ||
color: pieceEdition == piece ? Colors.blue : Colors.transparent, | ||
child: GestureDetector( | ||
onTap: () => pieceTapped(role), | ||
child: Draggable( | ||
data: piece, | ||
feedback: PieceDragFeedback( | ||
scale: settings.dragFeedbackScale, | ||
squareSize: squareSize, | ||
piece: piece, | ||
pieceAssets: pieceSet.assets, | ||
), | ||
child: pieceWidget), | ||
), | ||
); | ||
}, | ||
).toList(), | ||
Container( | ||
color: pointerMode == PointerToolMode.edit && pieceEdition == null | ||
? Colors.red | ||
: Colors.transparent, | ||
child: GestureDetector( | ||
onTap: () => deleteTapped(), | ||
child: Icon( | ||
Icons.delete_outline, | ||
size: squareSize, | ||
), | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import 'package:dartchess/dartchess.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:fast_immutable_collections/fast_immutable_collections.dart'; | ||
import 'package:chessground/chessground.dart' as cg; | ||
|
||
const cg.PieceAssets maestroPieceSet = IMapConst({ | ||
cg.PieceKind.blackRook: AssetImage('lib/piece_set/maestro/bR.png'), | ||
cg.PieceKind.blackPawn: AssetImage('lib/piece_set/maestro/bP.png'), | ||
cg.PieceKind.blackKnight: AssetImage('lib/piece_set/maestro/bN.png'), | ||
cg.PieceKind.blackBishop: AssetImage('lib/piece_set/maestro/bB.png'), | ||
cg.PieceKind.blackQueen: AssetImage('lib/piece_set/maestro/bQ.png'), | ||
cg.PieceKind.blackKing: AssetImage('lib/piece_set/maestro/bK.png'), | ||
cg.PieceKind.whiteRook: AssetImage('lib/piece_set/maestro/wR.png'), | ||
cg.PieceKind.whiteKnight: AssetImage('lib/piece_set/maestro/wN.png'), | ||
cg.PieceKind.whiteBishop: AssetImage('lib/piece_set/maestro/wB.png'), | ||
cg.PieceKind.whiteQueen: AssetImage('lib/piece_set/maestro/wQ.png'), | ||
cg.PieceKind.whiteKing: AssetImage('lib/piece_set/maestro/wK.png'), | ||
cg.PieceKind.whitePawn: AssetImage('lib/piece_set/maestro/wP.png'), | ||
kBlackRookKind: AssetImage('lib/piece_set/maestro/bR.png'), | ||
kBlackPawnKind: AssetImage('lib/piece_set/maestro/bP.png'), | ||
kBlackKnightKind: AssetImage('lib/piece_set/maestro/bN.png'), | ||
kBlackBishopKind: AssetImage('lib/piece_set/maestro/bB.png'), | ||
kBlackQueenKind: AssetImage('lib/piece_set/maestro/bQ.png'), | ||
kBlackKingKind: AssetImage('lib/piece_set/maestro/bK.png'), | ||
kWhiteRookKind: AssetImage('lib/piece_set/maestro/wR.png'), | ||
kWhiteKnightKind: AssetImage('lib/piece_set/maestro/wN.png'), | ||
kWhiteBishopKind: AssetImage('lib/piece_set/maestro/wB.png'), | ||
kWhiteQueenKind: AssetImage('lib/piece_set/maestro/wQ.png'), | ||
kWhiteKingKind: AssetImage('lib/piece_set/maestro/wK.png'), | ||
kWhitePawnKind: AssetImage('lib/piece_set/maestro/wP.png'), | ||
}); |
Oops, something went wrong.