-
-
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.
- Loading branch information
1 parent
a079cfb
commit e225abf
Showing
12 changed files
with
722 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
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); | ||
|
||
Piece? pieceToAddOnTap; | ||
bool deleteOnTap = false; | ||
|
||
@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 = BoardEditor( | ||
size: screenWidth, | ||
orientation: Side.white, | ||
pieces: pieces, | ||
settings: settings, | ||
onTappedSquare: (squareId) => setState(() { | ||
if (deleteOnTap) { | ||
pieces.remove(squareId); | ||
} else if (pieceToAddOnTap != null) { | ||
pieces[squareId] = pieceToAddOnTap!; | ||
} | ||
}), | ||
onDiscardedPiece: (squareId) => setState(() { | ||
pieces.remove(squareId); | ||
}), | ||
onDroppedPiece: (origin, destination, piece) => setState(() { | ||
pieces[destination] = piece; | ||
if (origin != null) { | ||
pieces.remove(origin); | ||
} | ||
}), | ||
); | ||
|
||
makePieceMenu(side) => PieceMenu( | ||
side: side, | ||
pieceSet: pieceSet, | ||
squareSize: boardEditor.squareSize, | ||
settings: settings, | ||
selectedPiece: pieceToAddOnTap, | ||
pieceTapped: (role) => setState(() { | ||
pieceToAddOnTap = Piece(role: role, color: side); | ||
deleteOnTap = false; | ||
}), | ||
deleteSelected: deleteOnTap, | ||
deleteTapped: () => setState(() { | ||
pieceToAddOnTap = null; | ||
deleteOnTap = !deleteOnTap; | ||
}), | ||
); | ||
|
||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Board Editor'), | ||
), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: [ | ||
makePieceMenu(Side.white), | ||
boardEditor, | ||
makePieceMenu(Side.black), | ||
Text('FEN: ${writeFen(pieces)}'), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class PieceMenu extends StatelessWidget { | ||
const PieceMenu({ | ||
super.key, | ||
required this.side, | ||
required this.pieceSet, | ||
required this.squareSize, | ||
required this.selectedPiece, | ||
required this.deleteSelected, | ||
required this.settings, | ||
required this.pieceTapped, | ||
required this.deleteTapped, | ||
}); | ||
|
||
final Side side; | ||
final PieceSet pieceSet; | ||
final double squareSize; | ||
final Piece? selectedPiece; | ||
final bool deleteSelected; | ||
final BoardEditorSettings settings; | ||
final Function(Role role) pieceTapped; | ||
final Function() deleteTapped; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
color: Colors.grey, | ||
child: Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
...Role.values.mapIndexed( | ||
(i, role) { | ||
final piece = Piece(role: role, color: side); | ||
final pieceWidget = PieceWidget( | ||
piece: piece, | ||
size: squareSize, | ||
pieceAssets: pieceSet.assets, | ||
); | ||
|
||
return Container( | ||
color: | ||
selectedPiece == piece ? Colors.blue : Colors.transparent, | ||
child: GestureDetector( | ||
onTap: () => pieceTapped(role), | ||
child: Draggable( | ||
data: piece, | ||
feedback: PieceDragFeedback( | ||
piece: piece, | ||
pieceAssets: pieceSet.assets, | ||
squareSize: squareSize, | ||
), | ||
child: pieceWidget), | ||
), | ||
); | ||
}, | ||
).toList(), | ||
Container( | ||
color: deleteSelected ? Colors.red : Colors.transparent, | ||
child: GestureDetector( | ||
onTap: () => deleteTapped(), | ||
child: Icon( | ||
Icons.delete, | ||
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
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,94 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'board_color_scheme.dart'; | ||
import 'models.dart'; | ||
import 'piece_set.dart'; | ||
|
||
/// Board editor settings that control the theme, behavior and purpose of the board editor. | ||
/// | ||
/// This is meant for fixed settings that don't change while editing the board. Sensible | ||
/// defaults are provided. | ||
@immutable | ||
class BoardEditorSettings { | ||
const BoardEditorSettings({ | ||
// theme | ||
this.colorScheme = BoardColorScheme.brown, | ||
this.pieceAssets = PieceSet.cburnettAssets, | ||
// visual settings | ||
this.borderRadius = BorderRadius.zero, | ||
this.boxShadow = const <BoxShadow>[], | ||
this.enableCoordinates = true, | ||
this.dragFeedbackSize = 2.0, | ||
this.dragFeedbackOffset = const Offset(0.0, -1.0), | ||
}); | ||
|
||
/// Theme of the board | ||
final BoardColorScheme colorScheme; | ||
|
||
/// Piece set | ||
final PieceAssets pieceAssets; | ||
|
||
/// Border radius of the board | ||
final BorderRadiusGeometry borderRadius; | ||
|
||
/// Box shadow of the board | ||
final List<BoxShadow> boxShadow; | ||
|
||
/// Whether to show board coordinates | ||
final bool enableCoordinates; | ||
|
||
// Scale up factor for the piece currently under drag | ||
final double dragFeedbackSize; | ||
|
||
// Offset for the piece currently under drag | ||
final Offset dragFeedbackOffset; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) { | ||
return true; | ||
} | ||
if (other.runtimeType != runtimeType) { | ||
return false; | ||
} | ||
return other is BoardEditorSettings && | ||
other.colorScheme == colorScheme && | ||
other.pieceAssets == pieceAssets && | ||
other.borderRadius == borderRadius && | ||
other.boxShadow == boxShadow && | ||
other.enableCoordinates == enableCoordinates && | ||
other.dragFeedbackSize == dragFeedbackSize && | ||
other.dragFeedbackOffset == dragFeedbackOffset; | ||
} | ||
|
||
@override | ||
int get hashCode => Object.hash( | ||
colorScheme, | ||
pieceAssets, | ||
borderRadius, | ||
boxShadow, | ||
enableCoordinates, | ||
dragFeedbackSize, | ||
dragFeedbackOffset, | ||
); | ||
|
||
BoardEditorSettings copyWith({ | ||
BoardColorScheme? colorScheme, | ||
PieceAssets? pieceAssets, | ||
BorderRadiusGeometry? borderRadius, | ||
List<BoxShadow>? boxShadow, | ||
bool? enableCoordinates, | ||
double? dragFeedbackSize, | ||
Offset? dragFeedbackOffset, | ||
}) { | ||
return BoardEditorSettings( | ||
colorScheme: colorScheme ?? this.colorScheme, | ||
pieceAssets: pieceAssets ?? this.pieceAssets, | ||
borderRadius: borderRadius ?? this.borderRadius, | ||
boxShadow: boxShadow ?? this.boxShadow, | ||
enableCoordinates: enableCoordinates ?? this.enableCoordinates, | ||
dragFeedbackSize: dragFeedbackSize ?? this.dragFeedbackSize, | ||
dragFeedbackOffset: dragFeedbackOffset ?? this.dragFeedbackOffset, | ||
); | ||
} | ||
} |
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
Oops, something went wrong.