chess_skin.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import 'dart:convert';
  2. import 'package:cchess/cchess.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter/material.dart';
  5. import '../global.dart';
  6. import 'game_manager.dart';
  7. /// 棋子和棋盘的皮肤
  8. class ChessSkin {
  9. GameManager manager;
  10. String folder = "";
  11. double width = 521;
  12. double height = 577;
  13. double size = 57;
  14. Offset offset = const Offset(4, 3);
  15. String board = "board.jpg";
  16. String blank = "blank.png";
  17. Map<String, String> redMap = {
  18. "K": "rk.gif",
  19. "A": "ra.png",
  20. "B": "rb.png",
  21. "C": "rc.png",
  22. "N": "rn.png",
  23. "R": "rr.png",
  24. "P": "rp.png"
  25. };
  26. Map<String, String> blackMap = {
  27. "k": "bk.png",
  28. "a": "ba.png",
  29. "b": "bb.png",
  30. "c": "bc.png",
  31. "n": "bn.png",
  32. "r": "br.png",
  33. "p": "bp.png"
  34. };
  35. late ValueNotifier<bool> readyNotifier;
  36. ChessSkin(this.folder, this.manager) {
  37. readyNotifier = ValueNotifier(false);
  38. String jsonfile = "assets/skins/$folder/config.json";
  39. rootBundle.loadString(jsonfile).then((String fileContents) {
  40. loadJson(fileContents);
  41. }).catchError((error) {
  42. logger.warning('Skin file $jsonfile error', error);
  43. readyNotifier.value = true;
  44. });
  45. }
  46. void loadJson(String content) {
  47. Map<String, dynamic> json = jsonDecode(content);
  48. json.forEach((key, value) {
  49. switch (key) {
  50. case 'width':
  51. width = value.toDouble();
  52. break;
  53. case 'height':
  54. height = value.toDouble();
  55. break;
  56. case 'size':
  57. size = value.toDouble();
  58. break;
  59. case 'board':
  60. board = value.toString();
  61. break;
  62. case 'blank':
  63. blank = value.toString();
  64. break;
  65. case 'offset':
  66. offset = Offset(value['dx'].toDouble(), value['dy'].toDouble());
  67. break;
  68. case 'red':
  69. redMap = value.cast<String, String>();
  70. break;
  71. case 'black':
  72. blackMap = value.cast<String, String>();
  73. break;
  74. }
  75. });
  76. readyNotifier.value = true;
  77. }
  78. String get boardImage => "assets/skins/$folder/$board";
  79. String getRedChess(String code) {
  80. if (!redMap.containsKey(code.toUpperCase())) {
  81. logger.info('Code error: $code');
  82. return "assets/skins/$folder/$blank";
  83. }
  84. return "assets/skins/$folder/${redMap[code.toUpperCase()]}";
  85. }
  86. String getBlackChess(String code) {
  87. if (!blackMap.containsKey(code.toLowerCase())) {
  88. logger.info('Code error: $code');
  89. return "assets/skins/$folder/$blank";
  90. }
  91. return "assets/skins/$folder/${blackMap[code.toLowerCase()]}";
  92. }
  93. Alignment getAlign(ChessPos? pos) {
  94. if (pos == null) {
  95. return const Alignment(1.2, 0);
  96. }
  97. final x = ((pos.x * size + offset.dx) * 2) / (width - size) - 1;
  98. final y = ((((9 - pos.y) * size + offset.dy) * 2) / (height - size) - 1);
  99. return Alignment(
  100. manager.isFlip ? -x : x,
  101. manager.isFlip ? -y : y,
  102. );
  103. }
  104. }