trace.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 'area_abstract.dart';
  11. /// 手势轨迹图形
  12. class Trace extends AreaItemAbstract with AutoSnapMixin {
  13. Trace(ItemMeta meta, IMeasureItem? parent) : super(meta, parent);
  14. @override
  15. bool onExecuteMouse(PointInfo args) {
  16. if (state == ItemStates.finished) {
  17. if (args.pointType == PointInfoType.mouseDown) {
  18. state = ItemStates.waiting;
  19. }
  20. }
  21. if (state == ItemStates.waiting) {
  22. if (args.pointType == PointInfoType.mouseDown) {
  23. handleMouseDownWhileWaiting(args);
  24. }
  25. } else if (state == ItemStates.running) {
  26. if (args.pointType == PointInfoType.mouseUp) return false;
  27. feature?.adopt(args);
  28. doCalculate();
  29. if (args.pointType == PointInfoType.mouseDown) {
  30. doFeatureFinish();
  31. } else {
  32. checkAutoSnap(args);
  33. }
  34. }
  35. return true;
  36. }
  37. PointInfo? startPoint;
  38. bool isFirstPointNeedOffset = false; // 第一个点是否需要施加偏移
  39. DPoint traceTouchStartPoint = DPoint(0, 0); // 轨迹触摸起始点
  40. @override
  41. bool onExecuteTouch(PointInfo args) {
  42. if (state == ItemStates.finished) {
  43. if (args.pointType == PointInfoType.touchDown) {
  44. state = ItemStates.waiting;
  45. }
  46. }
  47. if (state == ItemStates.waiting) {
  48. if (isFirstPointNeedOffset) args.addOffset(0, -0.2);
  49. switch (args.pointType) {
  50. case PointInfoType.touchDown:
  51. isFirstPointNeedOffset = false;
  52. handleTouchBeforeStart(args);
  53. break;
  54. case PointInfoType.touchUp:
  55. state = ItemStates.running;
  56. startPoint = args; // 设置线段起点
  57. feature?.innerPoints.first = args;
  58. break; // 按下立即抬起无事发生
  59. case PointInfoType.touchMove:
  60. isFirstPointNeedOffset = true;
  61. if (isMoveTargetOutOfRange(args)) return true;
  62. feature?.innerPoints.first = args;
  63. break;
  64. default:
  65. break;
  66. }
  67. } else if (state == ItemStates.running) {
  68. // if (isFirstPointNeedOffset) args.addOffset(0, -0.2);
  69. if (args.pointType == PointInfoType.touchDown) {
  70. traceTouchStartPoint = args;
  71. }
  72. if (args.pointType == PointInfoType.touchUp) {
  73. doFeatureFinish();
  74. }
  75. if (args.pointType == PointInfoType.touchMove) {
  76. PointInfo newPoint = PointInfo.fromOffset(
  77. startPoint!
  78. .clone()
  79. .addVector(args - traceTouchStartPoint)
  80. .toOffset(),
  81. startPoint!.pointType);
  82. newPoint.hostVisualArea = args.hostVisualArea;
  83. if (isMoveTargetOutOfRange(newPoint)) return true;
  84. feature?.adopt(newPoint);
  85. doCalculate();
  86. checkAutoSnap(newPoint);
  87. }
  88. }
  89. return true;
  90. }
  91. @override
  92. void doFeatureFinish() {
  93. super.doFeatureFinish();
  94. }
  95. void handleMouseDownWhileWaiting(PointInfo args) {
  96. // TODO: 判断是否当前area
  97. // 转换为Area逻辑位置
  98. feature = TraceFeature(this);
  99. if (args.hostVisualArea != null) {
  100. feature!.hostVisualArea = args.hostVisualArea;
  101. }
  102. final point = args.toAreaLogicPoint();
  103. feature!.adopt(point);
  104. state = ItemStates.running;
  105. }
  106. void handleTouchBeforeStart(PointInfo args) {
  107. // TODO: 判断是否当前area
  108. // 转换为Area逻辑位置
  109. feature = TraceFeature(this);
  110. if (args.hostVisualArea != null) {
  111. feature!.hostVisualArea = args.hostVisualArea;
  112. }
  113. final point = args.toAreaLogicPoint();
  114. feature!.adopt(point);
  115. }
  116. static Trace createAreaPerimeter(
  117. ItemMeta meta, [
  118. IMeasureItem? parent,
  119. ]) {
  120. Trace trace = Trace(meta, parent);
  121. trace.calculator = AreaPerimeterCal(trace);
  122. return trace;
  123. }
  124. static Trace createCurveLength(
  125. ItemMeta meta, [
  126. IMeasureItem? parent,
  127. ]) {
  128. Trace trace = Trace(meta, parent);
  129. trace.calculator = CurveLengthCal(trace);
  130. trace.isClosed = false;
  131. return trace;
  132. }
  133. }
  134. class TraceFeature extends AreaItemFeatureAbstract {
  135. TraceFeature(AreaItemAbstract refItem) : super(refItem);
  136. @override
  137. void paint(Canvas canvas, Size size) {
  138. if (innerPoints.isEmpty) return;
  139. drawId(canvas, size);
  140. final points = innerPoints.map((e) => convert2ViewPoint(size, e)).toList();
  141. final startPoint = points.first;
  142. drawVertex(canvas, startPoint.toOffset(), points.length == 1);
  143. if (points.length > 1) {
  144. final Path path = Path();
  145. path.moveTo(startPoint.x, startPoint.y);
  146. for (var i = 1; i < points.length; i++) {
  147. final point = points[i];
  148. path.lineTo(point.x, point.y);
  149. }
  150. if (isClosed) {
  151. path.lineTo(startPoint.x, startPoint.y);
  152. }
  153. canvas.drawPath(
  154. dashPath(path, dashArray: CircularIntervalList([2.0, 10.0])),
  155. paintLinePan,
  156. );
  157. }
  158. drawVertex(canvas, points.last.toOffset(), isActive);
  159. }
  160. /// 绘制灌注图
  161. @override
  162. void paintPerfusion(Canvas canvas, Size size) {}
  163. }