multiple_trace.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import 'dart:ui';
  2. import 'package:fis_measure/interfaces/date_types/point.dart';
  3. import 'package:fis_measure/interfaces/enums/items.dart';
  4. import 'package:fis_measure/interfaces/process/items/item.dart';
  5. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  6. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  7. import 'package:fis_measure/process/calcuators/trace.dart';
  8. import 'package:fis_measure/process/items/item.dart';
  9. import 'package:fis_measure/process/items/item_feature.dart';
  10. import 'package:fis_measure/utils/canvas.dart';
  11. import 'package:fis_measure/view/gesture/positioned_touch_cursor.dart';
  12. import 'package:get/get.dart';
  13. /// 手势轨迹图形
  14. class MultiTrace extends TraceItemAbstract {
  15. MultiTrace(ItemMeta meta, IMeasureItem? parent) : super(meta, parent);
  16. late final touchState = Get.find<ITouchPointState>();
  17. @override
  18. bool onExecuteMouse(PointInfo args) {
  19. if (state == ItemStates.finished) {
  20. if (args.pointType == PointInfoType.mouseDown) {
  21. state = ItemStates.waiting;
  22. }
  23. }
  24. if (state == ItemStates.waiting) {
  25. if (args.pointType == PointInfoType.mouseDown) {
  26. handleMouseDownWhileWaiting(args);
  27. }
  28. } else if (state == ItemStates.running) {
  29. if (args.pointType == PointInfoType.mouseUp) return false;
  30. feature?.adopt(args);
  31. doCalculate();
  32. if (args.pointType == PointInfoType.mouseDown) {
  33. doFeatureFinish();
  34. }
  35. }
  36. return true;
  37. }
  38. @override
  39. void doFeatureFinish() {
  40. super.doFeatureFinish();
  41. }
  42. void handleMouseDownWhileWaiting(PointInfo args) {
  43. // TODO: 判断是否当前area
  44. // 转换为Area逻辑位置
  45. feature = MultiTraceFeature(this);
  46. if (args.hostVisualArea != null) {
  47. feature!.hostVisualArea = args.hostVisualArea;
  48. }
  49. final point = args.toAreaLogicPoint();
  50. feature!.adopt(point);
  51. state = ItemStates.running;
  52. }
  53. void handleTouchDownWhileWaiting(PointInfo args) {
  54. // TODO: 判断是否当前area
  55. // 转换为Area逻辑位置
  56. feature = MultiTraceFeature(this);
  57. if (args.hostVisualArea != null) {
  58. feature!.hostVisualArea = args.hostVisualArea;
  59. }
  60. final point = args.toAreaLogicPoint();
  61. feature!.adopt(point);
  62. // state = ItemStates.running;
  63. }
  64. PointInfo? startPoint;
  65. DPoint touchStartPosition = DPoint(0, 0); // 相对位移起始触摸点
  66. bool isFirstPointMove = false;
  67. @override
  68. bool onExecuteTouch(PointInfo args) {
  69. if (state == ItemStates.finished) {
  70. if (args.pointType == PointInfoType.touchDown) {
  71. state = ItemStates.waiting;
  72. }
  73. }
  74. if (state == ItemStates.waiting) {
  75. if (isFirstPointMove) {
  76. args.addOffset(0, -0.2);
  77. }
  78. switch (args.pointType) {
  79. case PointInfoType.touchDown:
  80. isFirstPointMove = false;
  81. startPoint = args; // 设置线段起点
  82. handleTouchDownWhileWaiting(startPoint!); // 通过设置的起点开始一个绘制事件
  83. break;
  84. case PointInfoType.touchUp:
  85. startPoint = args; // 设置线段起点
  86. state = ItemStates.running;
  87. touchState.touchOffset = Offset.zero;
  88. break; // 按下立即抬起无事发生
  89. case PointInfoType.touchMove:
  90. if (isMoveTargetOutOfRange(args)) return true;
  91. isFirstPointMove = true;
  92. final pixelSize = application.displaySize;
  93. touchState.touchOffset =
  94. DPoint(0, -0.2).scale2Size(pixelSize).toOffset();
  95. feature?.innerPoints.first = args;
  96. break;
  97. default:
  98. break;
  99. }
  100. } else if (state == ItemStates.running) {
  101. if (args.pointType == PointInfoType.touchDown) {
  102. touchStartPosition = args;
  103. final pixelSize = application.displaySize;
  104. touchState.touchOffset = startPoint!.scale2Size(pixelSize).toOffset() -
  105. args.scale2Size(pixelSize).toOffset();
  106. }
  107. if (args.pointType == PointInfoType.touchUp) {
  108. touchState.touchOffset = Offset.zero;
  109. doFeatureFinish();
  110. }
  111. if (args.pointType == PointInfoType.touchMove) {
  112. PointInfo newPoint = PointInfo.fromOffset(
  113. startPoint!.clone().addVector(args - touchStartPosition).toOffset(),
  114. startPoint!.pointType);
  115. if (isMoveTargetOutOfRange(newPoint)) return true;
  116. feature?.adopt(newPoint);
  117. doCalculate();
  118. }
  119. }
  120. return true;
  121. }
  122. static MultiTrace createTrace(
  123. ItemMeta meta, [
  124. IMeasureItem? parent,
  125. ]) {
  126. MultiTrace trace = MultiTrace(meta, parent);
  127. trace.calculator = TraceCal(trace);
  128. return trace;
  129. }
  130. }
  131. class MultiTraceFeature extends TraceItemFeatureAbstract {
  132. MultiTraceFeature(TraceItemAbstract refItem) : super(refItem);
  133. final greenPen = Paint()
  134. ..color = const Color.fromARGB(255, 0, 255, 0)
  135. ..isAntiAlias = true
  136. ..strokeWidth = 1
  137. ..style = PaintingStyle.stroke;
  138. final dashLinePan = Paint()
  139. ..color = const Color.fromARGB(255, 255, 255, 0)
  140. ..isAntiAlias = false
  141. ..strokeWidth = 1
  142. ..style = PaintingStyle.stroke;
  143. DPoint furthestPoint = DPoint(0, 0);
  144. @override
  145. void paint(Canvas canvas, Size size) {
  146. final double areaTop = hostVisualArea!.displayRegion.top * size.height;
  147. final double areaBottom =
  148. hostVisualArea!.displayRegion.bottom * size.height;
  149. if (innerPoints.isEmpty) return;
  150. if (innerPoints.length == 1) {
  151. drawVertex(canvas, convert2ViewPoint(size, innerPoints[0]).toOffset());
  152. drawId(canvas, size);
  153. return;
  154. }
  155. double maxDistance = 0;
  156. drawId(canvas, size);
  157. final points = innerPoints.map((e) => convert2ViewPoint(size, e)).toList();
  158. final startOffset = convert2ViewPoint(size, startPoint);
  159. final endOffset = convert2ViewPoint(size, endPoint);
  160. canvas.drawDashLine(Offset(startOffset.x, areaTop),
  161. Offset(startOffset.x, areaBottom), 3, 3, dashLinePan);
  162. canvas.drawDashLine(Offset(endOffset.x, areaTop),
  163. Offset(endOffset.x, areaBottom), 3, 3, dashLinePan);
  164. if (points.length > 1) {
  165. final Path path = Path();
  166. path.moveTo(startOffset.x, startOffset.y);
  167. for (var i = 1; i < points.length; i++) {
  168. final point = points[i];
  169. path.lineTo(point.x, point.y);
  170. final distance = (point.y - startOffset.y).abs();
  171. if (distance > maxDistance) {
  172. maxDistance = distance;
  173. furthestPoint = point;
  174. }
  175. }
  176. drawCrossVertex(canvas, furthestPoint.toOffset());
  177. canvas.drawPath(
  178. path,
  179. greenPen,
  180. );
  181. }
  182. }
  183. }
  184. abstract class TraceItemFeatureAbstract extends MeasureItemFeature {
  185. TraceItemFeatureAbstract(TraceItemAbstract refItem) : super(refItem);
  186. @override
  187. TraceItemAbstract get refItem => super.refItem as TraceItemAbstract;
  188. DPoint get startPoint => innerPoints.first;
  189. DPoint get endPoint => innerPoints.last;
  190. bool ifRightSide = true;
  191. /// 接收新坐标
  192. void adopt(DPoint point) {
  193. if (innerPoints.isEmpty) {
  194. innerPoints.add(point);
  195. }
  196. if (point.x > startPoint.x) {
  197. handleChangeSide(point);
  198. if (point.x < innerPoints.last.x) {
  199. clearRight(point.x);
  200. } else if (point.x > innerPoints.last.x) {
  201. innerPoints.add(point);
  202. }
  203. } else {
  204. handleChangeSide(point);
  205. if (point.x > innerPoints.last.x) {
  206. clearLeft(point.x);
  207. } else if (point.x < innerPoints.last.x) {
  208. innerPoints.add(point);
  209. }
  210. }
  211. }
  212. void clearRight(double X) {
  213. if (innerPoints.isEmpty) return;
  214. for (var i = innerPoints.length - 1; i >= 0; i--) {
  215. if (innerPoints[i].x >= X) {
  216. innerPoints.removeAt(i);
  217. }
  218. }
  219. }
  220. void clearLeft(double X) {
  221. if (innerPoints.isEmpty) return;
  222. for (var i = innerPoints.length - 1; i >= 0; i--) {
  223. if (innerPoints[i].x <= X) {
  224. innerPoints.removeAt(i);
  225. }
  226. }
  227. }
  228. void handleChangeSide(point) {
  229. if (ifRightSide) {
  230. if (point.x < startPoint.x) {
  231. ifRightSide = false;
  232. innerPoints.clear();
  233. innerPoints.add(point);
  234. }
  235. } else {
  236. if (point.x > startPoint.x) {
  237. ifRightSide = true;
  238. innerPoints.clear();
  239. innerPoints.add(point);
  240. }
  241. }
  242. }
  243. }
  244. abstract class TraceItemAbstract extends MeasureItem<TraceItemFeatureAbstract> {
  245. TraceItemAbstract(ItemMeta meta, IMeasureItem? parent) : super(meta, parent);
  246. }