layer_cp.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import 'dart:math';
  2. import 'package:trackoffical_app/logger.dart';
  3. import 'package:trackoffical_app/screen.dart';
  4. import 'package:trackoffical_app/service/app.dart';
  5. import 'layer.dart';
  6. import '../../../model/m_control_point.dart';
  7. class _LayerParams {
  8. bool isHideRouteBeforeStart = true;
  9. List<MControlPoint> wantList = [];
  10. List<Offset> wantListOnScreen = [];
  11. int? nextPlanCPIndex;
  12. double radiusPx = 0;
  13. bool isStarted = false;
  14. double numRadians=0;
  15. }
  16. class LayerCP extends GetView<LayerController> {
  17. LayerCP({
  18. super.key,
  19. required bool isHideRouteBeforeStart,
  20. required List<MControlPoint> wantList,
  21. int? nextPlanCPIndex,
  22. double? numRadians
  23. }) : _params = _LayerParams()
  24. ..isHideRouteBeforeStart = isHideRouteBeforeStart
  25. ..wantList = wantList
  26. ..numRadians=numRadians??0
  27. ..nextPlanCPIndex = nextPlanCPIndex;
  28. final _LayerParams _params;
  29. final _userProfile = App.to.userProfile;
  30. Color get colorCP => _userProfile.gameSettingsCpColor.value;
  31. @override
  32. Widget build(BuildContext context) {
  33. return Obx(() {
  34. _params.wantListOnScreen.clear();
  35. for (var one in _params.wantList) {
  36. final p = controller.mapOffsetToScreen(one.onMap);
  37. _params.wantListOnScreen.add(p);
  38. if (one.isStart) {
  39. _params.isStarted = one.isSuccess;
  40. }
  41. }
  42. _params.radiusPx =
  43. controller.meterToOnScreen(controller.cpRadiusMeter.value);
  44. final children = <Widget>[
  45. _LayerCPContent(
  46. params: _params,
  47. )
  48. ];
  49. wCPNum(children);
  50. return Layer(children: children);
  51. });
  52. }
  53. wCPNum(List<Widget> children) {
  54. if ((!_params.isStarted) && _params.isHideRouteBeforeStart) {
  55. return const SizedBox();
  56. }
  57. final style = TextStyle(
  58. color: colorCP, fontSize: 22, fontWeight: FontWeight.w700);
  59. var h = _params.radiusPx * 1.1;
  60. if(h.isNaN){
  61. return;
  62. }
  63. const textHeight = 30.0;
  64. var points = _params.wantList;
  65. for (var i = 0; i < points.length; i++) {
  66. final one = points[i];
  67. var xy = _params.wantListOnScreen[i];
  68. var x = xy.dx;
  69. var y = xy.dy;
  70. if (!one.isStart && !one.isFinish) {
  71. final height = h + textHeight;
  72. children.add(Positioned(
  73. top: y - height,
  74. left: x - 40,
  75. width: 80,
  76. height: height,
  77. child: Transform.rotate(
  78. angle: _params.numRadians,
  79. alignment: Alignment.bottomCenter,
  80. child: Container(
  81. alignment: Alignment.topCenter,
  82. // color: Colors.white,
  83. child: Text(one.sn, style: style))),
  84. ));
  85. }
  86. }
  87. }
  88. }
  89. class _LayerCPContent extends StatefulWidget {
  90. const _LayerCPContent({
  91. required _LayerParams params,
  92. }) : _params = params;
  93. final _LayerParams _params;
  94. @override
  95. State<StatefulWidget> createState() {
  96. return _LayerCPContentState();
  97. }
  98. }
  99. class _LayerCPContentState extends State<_LayerCPContent>
  100. with SingleTickerProviderStateMixin {
  101. late AnimationController _animationController;
  102. late Animation<int> _animation;
  103. @override
  104. void initState() {
  105. super.initState();
  106. _animationController = AnimationController(
  107. vsync: this,
  108. duration: const Duration(milliseconds: 500),
  109. reverseDuration: const Duration(milliseconds: 500),
  110. )..repeat(reverse: true);
  111. _animation = IntTween(begin: 255, end: 0).animate(_animationController)
  112. ..addListener(() {
  113. setState(() {
  114. // The state that has changed here is the animation object’s value.
  115. });
  116. });
  117. }
  118. @override
  119. Widget build(BuildContext context) {
  120. return SizedBox(
  121. width: context.width,
  122. height: context.height,
  123. child: CustomPaint(
  124. painter: _RoutePointsPainter(
  125. widget._params,
  126. _animation.value,
  127. ),
  128. ));
  129. }
  130. @override
  131. void dispose() {
  132. _animationController.dispose();
  133. super.dispose();
  134. }
  135. }
  136. class _RoutePointsPainter extends CustomPainter {
  137. final _userProfile = App.to.userProfile;
  138. Color get colorCP => _userProfile.gameSettingsCpColor.value;
  139. Color get colorCPTarget => _userProfile.gameSettingsCpTargetColor.value;
  140. Color get colorCPJump => _userProfile.gameSettingsCpJumpColor.value;
  141. Color get colorCPPunched => _userProfile.gameSettingsCpPunchedColor.value;
  142. final _LayerParams params;
  143. final int nextAlpha;
  144. _RoutePointsPainter(
  145. this.params,
  146. this.nextAlpha,
  147. );
  148. @override
  149. void paint(Canvas canvas, Size size) {
  150. final paint = Paint();
  151. final radiusPx = params.radiusPx;
  152. final nextPlanIndex = params.nextPlanCPIndex;
  153. final strokeWidthColor = radiusPx * 0.15;
  154. final strokeWidthWhite = strokeWidthColor * 2;
  155. paint.strokeWidth = strokeWidthColor;
  156. final textSize = 8.0.wp;
  157. // const textSize = 5.0;
  158. const isShowText = false;
  159. var lastXY = Offset.zero;
  160. for (var i = params.wantList.length - 1; i >= 0; i--) {
  161. if ((!params.isStarted) && params.isHideRouteBeforeStart && i != 0) {
  162. continue;
  163. }
  164. final one = params.wantList[i];
  165. if (one.onMap != Offset.zero) {
  166. var xy = params.wantListOnScreen[i];
  167. var x = xy.dx;
  168. var y = xy.dy;
  169. var isNext = false;
  170. var isJumped = false;
  171. if (nextPlanIndex == null) {
  172. isNext = one.isNext;
  173. } else {
  174. isNext = i == nextPlanIndex;
  175. isJumped = !one.isSuccess && i < nextPlanIndex;
  176. }
  177. var color = colorCP;
  178. var strokeColor = Colors.white;
  179. if (one.isSuccess) {
  180. color = colorCPPunched;
  181. }
  182. if (isNext) {
  183. color = colorCPTarget.withAlpha(nextAlpha);
  184. strokeColor = Colors.white.withAlpha(nextAlpha);
  185. // paint.color = const Color(0xffff4d00);
  186. }
  187. if (isJumped) {
  188. color = colorCPJump;
  189. }
  190. paint.style = PaintingStyle.fill;
  191. final thisXY = Offset(x, y);
  192. // canvas.drawCircle(thisXY, 2, paint);
  193. // if (lastXY != Offset.zero) {
  194. if (i - 1 >= 0) {
  195. final next = params.wantList[i - 1];
  196. lastXY = params.wantListOnScreen[i - 1];
  197. final dx = thisXY.dx - lastXY.dx;
  198. final dy = thisXY.dy - lastXY.dy;
  199. final len = sqrt(dx * dx + dy * dy);
  200. final dx1 = dx / len * radiusPx;
  201. final dy1 = dy / len * radiusPx;
  202. var p1 = lastXY + Offset(dx1, dy1);
  203. var p2 = thisXY - Offset(dx1, dy1);
  204. if (p1.dx.isNaN || p1.dy.isNaN || p2.dx.isNaN || p2.dy.isNaN) {
  205. continue;
  206. }
  207. paint.color = strokeColor;
  208. paint.strokeWidth = strokeWidthWhite;
  209. canvas.drawLine(p1, p2, paint);
  210. paint.color = color;
  211. paint.strokeWidth = strokeWidthColor;
  212. canvas.drawLine(p1, p2, paint);
  213. }
  214. lastXY = thisXY;
  215. if (one.isStart) {
  216. final path = Path();
  217. final widthLeft = radiusPx * 0.9;
  218. final widthRight = radiusPx * 1.1;
  219. final heightHalf = radiusPx;
  220. path.moveTo(x - widthLeft, y - heightHalf);
  221. path.lineTo(x + widthRight, y);
  222. path.lineTo(x - widthLeft, y + heightHalf);
  223. path.close();
  224. paint.style = PaintingStyle.stroke;
  225. paint.color = strokeColor;
  226. paint.strokeWidth = strokeWidthWhite;
  227. canvas.drawPath(path, paint);
  228. paint.color = color;
  229. paint.strokeWidth = strokeWidthColor;
  230. canvas.drawPath(path, paint);
  231. } else if (one.isFinish) {
  232. paint.style = PaintingStyle.stroke;
  233. paint.color = strokeColor;
  234. paint.strokeWidth = strokeWidthWhite;
  235. canvas.drawCircle(thisXY, radiusPx, paint);
  236. canvas.drawCircle(thisXY, radiusPx * 0.7, paint);
  237. paint.color = color;
  238. paint.strokeWidth = strokeWidthColor;
  239. canvas.drawCircle(thisXY, radiusPx, paint);
  240. canvas.drawCircle(thisXY, radiusPx * 0.7, paint);
  241. } else {
  242. paint.style = PaintingStyle.stroke;
  243. paint.color = strokeColor;
  244. paint.strokeWidth = strokeWidthWhite;
  245. canvas.drawCircle(thisXY, radiusPx, paint);
  246. paint.color = color;
  247. paint.strokeWidth = strokeWidthColor;
  248. canvas.drawCircle(thisXY, radiusPx, paint);
  249. }
  250. if (!one.isStart && !one.isFinish && isShowText) {
  251. var textPainter = TextPainter(
  252. text: TextSpan(
  253. text: one.sn,
  254. style: TextStyle(
  255. color: paint.color,
  256. fontSize: textSize,
  257. fontWeight: FontWeight.w700)),
  258. textDirection: TextDirection.rtl,
  259. textWidthBasis: TextWidthBasis.longestLine,
  260. maxLines: 1,
  261. )..layout();
  262. textPainter.paint(
  263. canvas,
  264. Offset(x - textPainter.width / 2,
  265. y - textPainter.height - radiusPx));
  266. }
  267. }
  268. }
  269. }
  270. @override
  271. bool shouldRepaint(_RoutePointsPainter oldDelegate) => true;
  272. }