poyline.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/workspace/point_info.dart';
  6. import 'package:fis_measure/view/canvas/utils.dart';
  7. import 'package:path_drawing/path_drawing.dart';
  8. import '../calcuators/area.dart';
  9. import '../calcuators/perimeter.dart';
  10. import '../items/item.dart';
  11. import '../items/item_feature.dart';
  12. class PolyLine extends MeasureItem<PolyLineFeature> {
  13. final bool _initialClosed = false;
  14. PolyLine(ItemMeta meta, IMeasureItem? parent) : super(meta, parent);
  15. @override
  16. bool onExecuteMouse(PointInfo args) {
  17. if (state == ItemStates.finished) {
  18. if (args.pointType == PointInfoType.mouseDown) {
  19. state = ItemStates.waiting;
  20. }
  21. }
  22. if (state == ItemStates.waiting) {
  23. if (args.pointType == PointInfoType.mouseDown) {
  24. handleMouseDownWhileWaiting(args);
  25. }
  26. } else if (state == ItemStates.running) {
  27. if (args.pointType == PointInfoType.mouseUp) return false;
  28. feature?.adopt(args);
  29. doCalculate();
  30. if (args.pointType == PointInfoType.mouseDown) {
  31. doFeatureFinish();
  32. } else {
  33. _checkAutoFinish();
  34. }
  35. }
  36. return true;
  37. }
  38. void _checkAutoFinish() {
  39. double autoSnapDistance = 10; //TODO: from config
  40. bool isAutoSnap = true; //TODO: from config
  41. if (feature == null) return;
  42. final logicStart = feature!.innerPoints.first;
  43. final logicEnd = feature!.innerPoints.last;
  44. if (1 == 2) {
  45. feature!.innerPoints.add(feature!.innerPoints.first); // 强制闭合
  46. }
  47. }
  48. void handleMouseDownWhileWaiting(PointInfo args) {
  49. // TODO: 判断是否当前area
  50. // 转换为Area逻辑位置
  51. feature = PolyLineFeature(this);
  52. feature!.isClosed = _initialClosed;
  53. if (args.hostVisualArea != null) {
  54. feature!.hostVisualArea = args.hostVisualArea;
  55. }
  56. final point = args.toAreaLogicPoint();
  57. feature!.adopt(point);
  58. state = ItemStates.running;
  59. }
  60. @override
  61. bool onExecuteTouch(PointInfo args) {
  62. return true;
  63. }
  64. /// 创建面积测量
  65. static PolyLine createArea(
  66. ItemMeta meta, [
  67. IMeasureItem? parent,
  68. ]) {
  69. PolyLine poyline = PolyLine(meta, parent);
  70. poyline.calculator = PolyLineAreaCal(poyline);
  71. return poyline;
  72. }
  73. /// 创建周长测量
  74. static PolyLine createPerimeter(
  75. ItemMeta meta, [
  76. IMeasureItem? parent,
  77. ]) {
  78. PolyLine poyline = PolyLine(meta, parent);
  79. poyline.calculator = PolyLinePerimeterCal(poyline);
  80. return poyline;
  81. }
  82. }
  83. class PolyLineFeature extends MeasureItemFeature {
  84. bool _isClosed = false;
  85. PolyLineFeature(IMeasureItem refItem) : super(refItem);
  86. /// 是否闭合?抄自超声机
  87. bool get isClosed => _isClosed;
  88. set isClosed(bool value) {
  89. if (value != _isClosed) {
  90. _isClosed = value;
  91. }
  92. }
  93. /// 首节点
  94. DPoint get firstPoint => innerPoints.first;
  95. /// 接收新坐标
  96. void adopt(DPoint point) {
  97. innerPoints.add(point);
  98. }
  99. @override
  100. void paint(Canvas canvas, Size size) {
  101. if (innerPoints.isEmpty) return;
  102. // TODO: from style config
  103. const double vertexSize = 10;
  104. final points = innerPoints.map((e) => convert2ViewPoint(size, e)).toList();
  105. final startPoint = points.first;
  106. canvas.drawVertex(
  107. startPoint.toOffset(),
  108. vertexSize,
  109. active: points.length == 1,
  110. );
  111. if (points.length > 1) {
  112. final Path path = Path();
  113. path.moveTo(startPoint.x, startPoint.y);
  114. for (var i = 1; i < points.length; i++) {
  115. final point = points[i];
  116. path.lineTo(point.x, point.y);
  117. }
  118. path.lineTo(startPoint.x, startPoint.y);
  119. canvas.drawPath(
  120. dashPath(path, dashArray: CircularIntervalList([2.0, 10.0])),
  121. paintPan,
  122. );
  123. }
  124. canvas.drawVertex(points.last.toOffset(), vertexSize, active: isActive);
  125. }
  126. }