trace.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. feature?.innerPoints.first = args;
  62. break;
  63. default:
  64. break;
  65. }
  66. } else if (state == ItemStates.running) {
  67. // if (isFirstPointNeedOffset) args.addOffset(0, -0.2);
  68. if (args.pointType == PointInfoType.touchDown) {
  69. traceTouchStartPoint = args;
  70. }
  71. if (args.pointType == PointInfoType.touchUp) {
  72. doFeatureFinish();
  73. }
  74. if (args.pointType == PointInfoType.touchMove) {
  75. PointInfo newPoint = PointInfo.fromOffset(
  76. startPoint!
  77. .clone()
  78. .addVector(args - traceTouchStartPoint)
  79. .toOffset(),
  80. startPoint!.pointType);
  81. feature?.adopt(newPoint);
  82. doCalculate();
  83. checkAutoSnap(newPoint);
  84. }
  85. }
  86. return true;
  87. }
  88. @override
  89. void doFeatureFinish() {
  90. super.doFeatureFinish();
  91. }
  92. void handleMouseDownWhileWaiting(PointInfo args) {
  93. // TODO: 判断是否当前area
  94. // 转换为Area逻辑位置
  95. feature = TraceFeature(this);
  96. if (args.hostVisualArea != null) {
  97. feature!.hostVisualArea = args.hostVisualArea;
  98. }
  99. final point = args.toAreaLogicPoint();
  100. feature!.adopt(point);
  101. state = ItemStates.running;
  102. }
  103. void handleTouchBeforeStart(PointInfo args) {
  104. // TODO: 判断是否当前area
  105. // 转换为Area逻辑位置
  106. feature = TraceFeature(this);
  107. if (args.hostVisualArea != null) {
  108. feature!.hostVisualArea = args.hostVisualArea;
  109. }
  110. final point = args.toAreaLogicPoint();
  111. feature!.adopt(point);
  112. }
  113. static Trace createAreaPerimeter(
  114. ItemMeta meta, [
  115. IMeasureItem? parent,
  116. ]) {
  117. Trace trace = Trace(meta, parent);
  118. trace.calculator = AreaPerimeterCal(trace);
  119. return trace;
  120. }
  121. static Trace createCurveLength(
  122. ItemMeta meta, [
  123. IMeasureItem? parent,
  124. ]) {
  125. Trace trace = Trace(meta, parent);
  126. trace.calculator = CurveLengthCal(trace);
  127. trace.isClosed = false;
  128. return trace;
  129. }
  130. }
  131. class TraceFeature extends AreaItemFeatureAbstract {
  132. TraceFeature(AreaItemAbstract refItem) : super(refItem);
  133. @override
  134. void paint(Canvas canvas, Size size) {
  135. if (innerPoints.isEmpty) return;
  136. drawId(canvas, size);
  137. final points = innerPoints.map((e) => convert2ViewPoint(size, e)).toList();
  138. final startPoint = points.first;
  139. drawVertex(canvas, startPoint.toOffset(), points.length == 1);
  140. if (points.length > 1) {
  141. final Path path = Path();
  142. path.moveTo(startPoint.x, startPoint.y);
  143. for (var i = 1; i < points.length; i++) {
  144. final point = points[i];
  145. path.lineTo(point.x, point.y);
  146. }
  147. if (isClosed) {
  148. path.lineTo(startPoint.x, startPoint.y);
  149. }
  150. canvas.drawPath(
  151. dashPath(path, dashArray: CircularIntervalList([2.0, 10.0])),
  152. paintLinePan,
  153. );
  154. }
  155. drawVertex(canvas, points.last.toOffset(), isActive);
  156. }
  157. }