auto_snap.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import 'package:fis_measure/interfaces/date_types/int_size.dart';
  2. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  3. import 'package:fis_measure/process/items/item.dart';
  4. import 'package:fis_measure/process/items/item_feature.dart';
  5. import 'package:flutter/services.dart';
  6. mixin AutoSnapMixin<T extends MeasureItemFeature> on MeasureItem<T> {
  7. bool _isSmartMove = false;
  8. bool _isAutoSnap = true;
  9. double _snapThreshold = 10.0;
  10. bool snapState = false;
  11. /// 是否启动智能定位
  12. bool get isSmartMove => _isSmartMove;
  13. set isSmartMove(bool value) {
  14. if (value != _isSmartMove) {
  15. _isSmartMove = value;
  16. }
  17. }
  18. bool get isAutoSnap => _isAutoSnap;
  19. set isAutoSnap(bool val) {
  20. if (val != _isAutoSnap) {
  21. _isAutoSnap = val;
  22. }
  23. }
  24. /// 自动结束阈值 pixel
  25. double get snapThreshold => _snapThreshold;
  26. set snapThreshold(double val) {
  27. if (val != _snapThreshold) {
  28. _snapThreshold = val;
  29. }
  30. }
  31. /// 自动结束检测
  32. bool checkAutoSnap(PointInfo current) {
  33. if (feature == null || feature!.innerPoints.length < 3) {
  34. isSmartMove = false;
  35. return _syncState(false);
  36. }
  37. // final viewport = feature!.hostVisualArea!.viewport!;
  38. if (isAutoSnap == false) return false;
  39. final pixelSize = IntSize.fill(
  40. application.frameData!.width,
  41. application.frameData!.height,
  42. ).toDoubleSize();
  43. final p1 = feature!.innerPoints.first.scale2Size(pixelSize);
  44. final p2 = current.scale2Size(pixelSize);
  45. final length = (p1 - p2).length;
  46. if (length > snapThreshold * 2.0 && !isSmartMove) {
  47. isSmartMove = true;
  48. }
  49. if (length < snapThreshold && isSmartMove) {
  50. feature!.innerPoints.last = feature!.innerPoints.first.clone();
  51. /// 此处的最后一个点,方便计算,但是绘制时要剔除
  52. // feature!.innerPoints.removeLast();
  53. HapticFeedback.heavyImpact();
  54. doCalculate();
  55. doFeatureFinish();
  56. isSmartMove = false;
  57. return _syncState(true);
  58. } else {
  59. return _syncState(false);
  60. }
  61. }
  62. bool _syncState(bool isSnap) {
  63. snapState = isSnap;
  64. return snapState;
  65. }
  66. }