auto_snap.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/painting.dart';
  6. import 'package:flutter/services.dart';
  7. mixin AutoSnapMixin<T extends MeasureItemFeature> on MeasureItem<T> {
  8. bool _isSmartMove = false;
  9. /// 是否启动智能定位
  10. bool get isSmartMove => _isSmartMove;
  11. set isSmartMove(bool value) {
  12. if (value != _isSmartMove) {
  13. _isSmartMove = value;
  14. }
  15. }
  16. /// 自动结束检测
  17. bool checkAutoFinish(PointInfo current) {
  18. if (feature == null || feature!.innerPoints.length < 3) {
  19. isSmartMove = false;
  20. return false;
  21. }
  22. // final viewport = feature!.hostVisualArea!.viewport!;
  23. double autoSnapThreshold = 10; //TODO: from config, pixel
  24. bool isAutoSnap = true; //TODO: from config
  25. if (isAutoSnap == false) return false;
  26. final pixelSize = IntSize.fill(
  27. application.frameData!.width,
  28. application.frameData!.height,
  29. ).toDoubleSize();
  30. final p1 = feature!.innerPoints.first.scale2Size(pixelSize);
  31. final p2 = current.scale2Size(pixelSize);
  32. final length = (p1 - p2).length;
  33. if (length > autoSnapThreshold * 2.0 && !isSmartMove) {
  34. isSmartMove = true;
  35. }
  36. if (length < autoSnapThreshold && isSmartMove) {
  37. HapticFeedback.heavyImpact();
  38. doFeatureFinish();
  39. isSmartMove = false;
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. }