label_annotation.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import 'dart:ui' as ui;
  2. import 'package:fis_measure/interfaces/date_types/point.dart';
  3. import 'package:fis_measure/interfaces/enums/annotation.dart';
  4. import 'package:fis_measure/interfaces/process/visuals/visual_area.dart';
  5. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  6. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  7. import 'package:fis_measure/utils/canvas.dart';
  8. import 'package:fis_measure/values/colors.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter/painting.dart';
  11. import 'package:get/get.dart';
  12. import 'annotation.dart';
  13. /// 文本标签注释
  14. class LabelAnnotation extends AnnotationItem<LabelAnnotationItemFeature> {
  15. LabelAnnotation() : super(AnnotationType.label);
  16. @override
  17. bool onExecuteMouse(PointInfo args) {
  18. if (state == AnnotationStates.finish || state == AnnotationStates.waiting) {
  19. if (args.pointType == PointInfoType.mouseMove) {
  20. state = AnnotationStates.running;
  21. }
  22. return false;
  23. }
  24. if (state == AnnotationStates.running) {
  25. if (args.pointType == PointInfoType.mouseMove) return false;
  26. if (args.pointType == PointInfoType.mouseDown) {
  27. if (feature != null) {
  28. doFeatureFinish();
  29. }
  30. }
  31. if (args.pointType == PointInfoType.mouseUp) {
  32. feature = LabelAnnotationItemFeature(this, args);
  33. doFeatureFinish();
  34. }
  35. }
  36. return true;
  37. }
  38. @override
  39. bool onExecuteTouch(PointInfo args) {
  40. if (state == AnnotationStates.finish || state == AnnotationStates.waiting) {
  41. if (args.pointType == PointInfoType.touchMove) {
  42. state = AnnotationStates.running;
  43. }
  44. return false;
  45. }
  46. if (state == AnnotationStates.running) {
  47. if (args.pointType == PointInfoType.touchMove) return false;
  48. if (args.pointType == PointInfoType.touchUp) {
  49. feature = LabelAnnotationItemFeature(this, args);
  50. if (feature != null) {
  51. doFeatureFinish();
  52. }
  53. }
  54. }
  55. return true;
  56. }
  57. }
  58. class LabelAnnotationItemFeature extends AnnotationItemFeature {
  59. late final String? text;
  60. late final IVisualArea? visualArea;
  61. LabelAnnotationItemFeature(LabelAnnotation ref, PointInfo pointInfo)
  62. : super(ref) {
  63. text = ref.text;
  64. visualArea = pointInfo.hostVisualArea;
  65. points.add(pointInfo);
  66. }
  67. @override
  68. void paint(Canvas canvas, Size size) {
  69. if (text == null) return;
  70. double fontSize = 14 * Get.find<IApplication>().displayScaleRatio;
  71. final style = TextStyle(
  72. fontSize: fontSize,
  73. color: isActive ? MeasureColors.ActiveCaliper : MeasureColors.Primary,
  74. );
  75. final offset = convert2ViewPoint(size, position);
  76. canvas.drawText(
  77. text!,
  78. offset.toOffset(),
  79. style: style,
  80. );
  81. }
  82. }