trace.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/curve.dart';
  8. import 'package:fis_measure/process/primitives/utils/auto_snap.dart';
  9. import 'package:path_drawing/path_drawing.dart';
  10. import '../items/item.dart';
  11. import '../items/item_feature.dart';
  12. import 'area_abstract.dart';
  13. /// 手势轨迹图形
  14. class Trace extends AreaItemAbstract with AutoSnapMixin {
  15. PointInfo? _firstPoint;
  16. Trace(ItemMeta meta, IMeasureItem? parent) : super(meta, parent);
  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. } else {
  35. checkAutoSnap(args);
  36. }
  37. }
  38. return true;
  39. }
  40. @override
  41. void doFeatureFinish() {
  42. super.doFeatureFinish();
  43. _firstPoint = null;
  44. }
  45. void handleMouseDownWhileWaiting(PointInfo args) {
  46. // TODO: 判断是否当前area
  47. // 转换为Area逻辑位置
  48. feature = TraceFeature(this);
  49. if (args.hostVisualArea != null) {
  50. feature!.hostVisualArea = args.hostVisualArea;
  51. }
  52. final point = args.toAreaLogicPoint();
  53. feature!.adopt(point);
  54. _firstPoint = args;
  55. state = ItemStates.running;
  56. }
  57. PointInfo? startPoint;
  58. @override
  59. bool onExecuteTouch(PointInfo args) {
  60. if (state == ItemStates.finished) {
  61. if (args.pointType == PointInfoType.touchDown) {
  62. state = ItemStates.waiting;
  63. }
  64. }
  65. if (state == ItemStates.waiting) {
  66. switch (args.pointType) {
  67. case PointInfoType.touchDown:
  68. startPoint = args; // 设置线段起点
  69. break;
  70. case PointInfoType.touchUp:
  71. break; // 按下立即抬起无事发生
  72. case PointInfoType.touchMove:
  73. handleMouseDownWhileWaiting(startPoint!); // 通过设置的起点开始一个绘制事件
  74. break;
  75. default:
  76. break;
  77. }
  78. } else if (state == ItemStates.running) {
  79. if (args.pointType == PointInfoType.touchUp) {
  80. doFeatureFinish();
  81. }
  82. if (args.pointType == PointInfoType.touchMove) {
  83. feature?.adopt(args);
  84. doCalculate();
  85. checkAutoSnap(args);
  86. }
  87. }
  88. return true;
  89. }
  90. static Trace createAreaPerimeter(
  91. ItemMeta meta, [
  92. IMeasureItem? parent,
  93. ]) {
  94. Trace trace = Trace(meta, parent);
  95. trace.calculator = AreaPerimeterCal(trace);
  96. return trace;
  97. }
  98. static Trace createCurveLength(
  99. ItemMeta meta, [
  100. IMeasureItem? parent,
  101. ]) {
  102. Trace trace = Trace(meta, parent);
  103. trace.calculator = CurveLengthCal(trace);
  104. trace.isClosed = false;
  105. return trace;
  106. }
  107. }
  108. class TraceFeature extends AreaItemFeatureAbstract {
  109. TraceFeature(AreaItemAbstract refItem) : super(refItem);
  110. @override
  111. void paint(Canvas canvas, Size size) {
  112. if (innerPoints.isEmpty) return;
  113. drawId(canvas, size);
  114. final points = innerPoints.map((e) => convert2ViewPoint(size, e)).toList();
  115. final startPoint = points.first;
  116. drawVertex(canvas, startPoint.toOffset(), points.length == 1);
  117. if (points.length > 1) {
  118. final Path path = Path();
  119. path.moveTo(startPoint.x, startPoint.y);
  120. for (var i = 1; i < points.length; i++) {
  121. final point = points[i];
  122. path.lineTo(point.x, point.y);
  123. }
  124. if (isClosed) {
  125. path.lineTo(startPoint.x, startPoint.y);
  126. }
  127. canvas.drawPath(
  128. dashPath(path, dashArray: CircularIntervalList([2.0, 10.0])),
  129. paintLinePan,
  130. );
  131. }
  132. drawVertex(canvas, points.last.toOffset(), isActive);
  133. }
  134. }