123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- import 'package:cchess/cchess.dart';
- import 'package:flutter/material.dart';
- import 'chess_box.dart';
- import 'chess_pieces.dart';
- import 'chess_single_box.dart';
- import 'board.dart';
- import '../global.dart';
- import '../pages/home_page.dart';
- import '../models/game_manager.dart';
- /// 编辑局面
- class EditFen extends StatefulWidget {
- final String fen;
- const EditFen({Key? key, required this.fen}) : super(key: key);
- @override
- State<EditFen> createState() => EditFenState();
- }
- class EditFenState extends State<EditFen> {
- late ChessManual manual;
- GameManager? gamer;
- late List<ChessItem> items;
- ChessItem? activeItem;
- String activeChr = '';
- String dieChrs = '';
- @override
- void initState() {
- super.initState();
- manual = ChessManual();
- manual.setFen(widget.fen);
- items = manual.getChessItems();
- dieChrs = manual.currentFen.getDieChr();
- }
- @override
- void dispose() {
- super.dispose();
- }
- void editOK() {
- Navigator.of(context).pop<String>(manual.currentFen.fen);
- }
- bool onPointer(ChessPos toPosition) {
- ChessItem targetItem = items.firstWhere(
- (item) => !item.isBlank && item.position == toPosition,
- orElse: () => ChessItem('0'),
- );
- if (targetItem.isBlank) {
- if (activeItem != null) {
- manual.doMove('${activeItem!.position.toCode()}${toPosition.toCode()}');
- setState(() {
- activeItem!.position = toPosition;
- activeItem = null;
- });
- return true;
- } else if (activeChr.isNotEmpty) {
- manual.setItem(toPosition, activeChr);
- setState(() {
- items = manual.getChessItems();
- activeChr = '';
- dieChrs = manual.currentFen.getDieChr();
- });
- return true;
- }
- } else {
- if (activeItem != null) {
- if (activeItem!.position == toPosition) {
- manual.setItem(toPosition, '0');
- setState(() {
- items = manual.getChessItems();
- activeItem = null;
- dieChrs = manual.currentFen.getDieChr();
- });
- } else {
- //targetItem.position = ChessPos.fromCode('i4');
- //targetItem.isDie = true;
- manual
- .doMove('${activeItem!.position.toCode()}${toPosition.toCode()}');
- setState(() {
- items = manual.getChessItems();
- activeItem = null;
- });
- }
- return true;
- } else if (activeChr.isNotEmpty && activeChr != targetItem.code) {
- //targetItem.position = ChessPos.fromCode('i4');
- bool seted = manual.setItem(toPosition, activeChr);
- if (seted) {
- setState(() {
- items = manual.getChessItems();
- activeChr = '';
- dieChrs = manual.currentFen.getDieChr();
- });
- }
- } else {
- setState(() {
- activeItem = targetItem;
- activeChr = '';
- });
- }
- }
- return false;
- }
- void removeItem(ChessPos fromPosition) {
- manual.currentFen[fromPosition.y][fromPosition.x] = '0';
- setState(() {
- items = manual.getChessItems();
- activeItem = null;
- activeChr = '';
- });
- }
- void setActiveChr(String chr) {
- setState(() {
- activeItem = null;
- activeChr = chr;
- });
- }
- void clearAll() {
- manual.setFen('4k4/9/9/9/9/9/9/9/9/4K4');
- setState(() {
- items = manual.getChessItems();
- dieChrs = manual.currentFen.getDieChr();
- activeChr = '';
- activeItem = null;
- });
- }
- ChessPos pointTrans(Offset tapPoint) {
- int x = (tapPoint.dx - gamer!.skin.offset.dx * gamer!.scale) ~/
- (gamer!.skin.size * gamer!.scale);
- int y = 9 -
- (tapPoint.dy - gamer!.skin.offset.dy * gamer!.scale) ~/
- (gamer!.skin.size * gamer!.scale);
- return ChessPos(x, y);
- }
- @override
- Widget build(BuildContext context) {
- if (gamer == null) {
- HomePageState gameWrapper =
- context.findAncestorStateOfType<HomePageState>()!;
- gamer = gameWrapper.gamer;
- }
- return Scaffold(
- appBar: AppBar(
- title: Text(context.l10n.editCode),
- actions: [
- TextButton(
- onPressed: () {
- editOK();
- },
- child: Text(
- context.l10n.save,
- style: const TextStyle(color: Colors.white),
- ),
- )
- ],
- ),
- body: Center(
- child: gamer!.scale < 1 ? _mobileContainer() : _windowContainer(),
- ),
- );
- }
- Widget _mobileContainer() {
- return SizedBox(
- width: gamer!.skin.width * gamer!.scale,
- height: (gamer!.skin.height + gamer!.skin.size * 2 + 20) * gamer!.scale,
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.center,
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- children: [
- ChessSingleBox(
- width: gamer!.skin.width * gamer!.scale,
- itemChrs: dieChrs,
- activeChr: activeChr,
- ),
- const SizedBox(width: 10),
- GestureDetector(
- onTapUp: (detail) {
- onPointer(pointTrans(detail.localPosition));
- },
- onLongPressEnd: (detail) {
- logger.info('longPressEnd $detail');
- },
- onPanEnd: (detail) {},
- child: SizedBox(
- width: gamer!.skin.width * gamer!.scale,
- height: gamer!.skin.height * gamer!.scale,
- child: Stack(
- alignment: Alignment.center,
- children: [
- const Board(),
- ChessPieces(
- items: items,
- activeItem: activeItem,
- )
- ],
- ),
- ),
- ),
- const SizedBox(width: 10),
- ChessSingleBox(
- width: gamer!.skin.width * gamer!.scale,
- itemChrs: dieChrs,
- activeChr: activeChr,
- )
- ],
- ),
- );
- }
- Widget _windowContainer() {
- return SizedBox(
- width: gamer!.skin.width + 10 + gamer!.skin.size * 2 + 10,
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- GestureDetector(
- onTapUp: (detail) {
- onPointer(pointTrans(detail.localPosition));
- },
- onLongPressEnd: (detail) {
- logger.info('longPressEnd $detail');
- },
- onPanEnd: (detail) {},
- child: SizedBox(
- width: gamer!.skin.width,
- height: gamer!.skin.height,
- child: Stack(
- alignment: Alignment.center,
- children: [
- const Board(),
- ChessPieces(
- items: items,
- activeItem: activeItem,
- )
- ],
- ),
- ),
- ),
- const SizedBox(width: 10),
- ChessBox(
- height: gamer!.skin.height,
- itemChrs: dieChrs,
- activeChr: activeChr,
- )
- ],
- ),
- );
- }
- }
|