Skip to content

Commit

Permalink
feat: support toggling dragged piece shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
anthisse committed Oct 2, 2024
1 parent 25de908 commit 43a95d3
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 141 deletions.
176 changes: 97 additions & 79 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class _HomePageState extends State<HomePage> {
bool drawMode = true;
bool pieceAnimation = true;
bool dragMagnify = true;
bool pieceShadow = true;
Mode playMode = Mode.botPlay;
Position<Chess>? lastPos;
ISet<Shape> shapes = ISet();
Expand Down Expand Up @@ -165,6 +166,22 @@ class _HomePageState extends State<HomePage> {
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
ElevatedButton(
child: Text(
'Shadow under dragged piece: ${pieceShadow ? 'ON' : 'OFF'}'),
onPressed: () {
setState(() {
pieceShadow = !pieceShadow;
});
}
),
const SizedBox(width: 8),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
Expand Down Expand Up @@ -194,11 +211,11 @@ class _HomePageState extends State<HomePage> {
child: IconButton(
onPressed: lastPos != null
? () => setState(() {
position = lastPos!;
fen = position.fen;
validMoves = makeLegalMoves(position);
lastPos = null;
})
position = lastPos!;
fen = position.fen;
validMoves = makeLegalMoves(position);
lastPos = null;
})
: null,
icon: const Icon(Icons.chevron_left_sharp))),
];
Expand All @@ -222,67 +239,67 @@ class _HomePageState extends State<HomePage> {
return Scaffold(
appBar: AppBar(
title: switch (playMode) {
Mode.botPlay => const Text('Random Bot'),
Mode.inputMove => const Text('Enter opponent move'),
Mode.freePlay => const Text('Free Play'),
}),
Mode.botPlay => const Text('Random Bot'),
Mode.inputMove => const Text('Enter opponent move'),
Mode.freePlay => const Text('Free Play'),
}),
drawer: Drawer(
child: ListView(
children: [
ListTile(
title: const Text('Random Bot'),
onTap: () {
setState(() {
playMode = Mode.botPlay;
});
if (position.turn == Side.black) {
_playBlackMove();
}
Navigator.pop(context);
},
),
ListTile(
title: const Text('Enter opponent move'),
onTap: () {
setState(() {
playMode = Mode.inputMove;
});
Navigator.pop(context);
},
),
ListTile(
title: const Text('Free Play'),
onTap: () {
setState(() {
playMode = Mode.freePlay;
});
Navigator.pop(context);
},
),
ListTile(
title: const Text('Board Editor'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const BoardEditorPage(),
),
);
},
),
ListTile(
title: const Text('Board Thumbnails'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const BoardThumbnailsPage(),
),
);
},
),
],
)),
children: [
ListTile(
title: const Text('Random Bot'),
onTap: () {
setState(() {
playMode = Mode.botPlay;
});
if (position.turn == Side.black) {
_playBlackMove();
}
Navigator.pop(context);
},
),
ListTile(
title: const Text('Enter opponent move'),
onTap: () {
setState(() {
playMode = Mode.inputMove;
});
Navigator.pop(context);
},
),
ListTile(
title: const Text('Free Play'),
onTap: () {
setState(() {
playMode = Mode.freePlay;
});
Navigator.pop(context);
},
),
ListTile(
title: const Text('Board Editor'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const BoardEditorPage(),
),
);
},
),
ListTile(
title: const Text('Board Thumbnails'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const BoardThumbnailsPage(),
),
);
},
),
],
)),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expand All @@ -296,6 +313,7 @@ class _HomePageState extends State<HomePage> {
? const Duration(milliseconds: 200)
: Duration.zero,
dragFeedbackScale: dragMagnify ? 2.0 : 1.0,
pieceShadow: pieceShadow,
drawShape: DrawShapeOptions(
enable: drawMode,
onCompleteShape: _onCompleteShape,
Expand All @@ -316,29 +334,29 @@ class _HomePageState extends State<HomePage> {
lastMove: lastMove,
game: GameData(
playerSide:
(playMode == Mode.botPlay || playMode == Mode.inputMove)
? PlayerSide.white
: (position.turn == Side.white
? PlayerSide.white
: PlayerSide.black),
(playMode == Mode.botPlay || playMode == Mode.inputMove)
? PlayerSide.white
: (position.turn == Side.white
? PlayerSide.white
: PlayerSide.black),
validMoves: validMoves,
sideToMove: position.turn == Side.white ? Side.white : Side.black,
isCheck: position.isCheck,
promotionMove: promotionMove,
onMove:
playMode == Mode.botPlay ? _onUserMoveAgainstBot : _playMove,
playMode == Mode.botPlay ? _onUserMoveAgainstBot : _playMove,
onPromotionSelection: _onPromotionSelection,
premovable: (
onSetPremove: _onSetPremove,
premove: premove,
onSetPremove: _onSetPremove,
premove: premove,
),
),
shapes: shapes.isNotEmpty ? shapes : null,
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children:
playMode == Mode.inputMove ? inputMoveWidgets : settingsWidgets,
playMode == Mode.inputMove ? inputMoveWidgets : settingsWidgets,
),
],
),
Expand Down Expand Up @@ -367,12 +385,12 @@ class _HomePageState extends State<HomePage> {
}

void _showChoicesPicker<T extends Enum>(
BuildContext context, {
required List<T> choices,
required T selectedItem,
required Widget Function(T choice) labelBuilder,
required void Function(T choice) onSelectedItemChanged,
}) {
BuildContext context, {
required List<T> choices,
required T selectedItem,
required Widget Function(T choice) labelBuilder,
required void Function(T choice) onSelectedItemChanged,
}) {
showDialog<void>(
context: context,
builder: (context) {
Expand Down
18 changes: 9 additions & 9 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.19.0"
version: "1.18.0"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -195,10 +195,10 @@ packages:
dependency: transitive
description:
name: string_scanner
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "1.2.0"
term_glyph:
dependency: transitive
description:
Expand All @@ -211,10 +211,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.7.3"
version: "0.7.2"
vector_math:
dependency: transitive
description:
Expand All @@ -227,10 +227,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.4"
version: "14.2.5"
sdks:
dart: ">=3.4.0 <4.0.0"
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
Loading

0 comments on commit 43a95d3

Please sign in to comment.