123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- import 'package:fis_common/logger/logger.dart';
- import 'package:fis_measure/interfaces/date_types/point.dart';
- import 'package:fis_measure/interfaces/process/calculators/values.dart';
- import 'package:fis_measure/interfaces/process/items/item.dart';
- import 'package:fis_measure/interfaces/process/items/item_feature.dart';
- import 'package:fis_measure/interfaces/process/items/item_metas.dart';
- import 'package:fis_measure/interfaces/process/visuals/visual_area.dart';
- import 'package:fis_measure/process/workspace/measure_data_controller.dart';
- import 'package:fis_measure/utils/canvas.dart';
- import 'package:fis_measure/values/colors.dart';
- import 'package:flutter/widgets.dart';
- import 'package:get/get.dart';
- import 'package:vid/us/vid_us_unit.dart';
- import 'item.dart';
- abstract class MeasureItemFeature implements IMeasureItemFeature {
- late IMeasureItem _refItem;
- late List<DPoint> _innerPoints;
- late FeatureStyle _featureStyle;
- late String _measureModeName;
- IVisualArea? _hostVisualArea;
- bool _isActive = true;
- int _id = 0;
- List<ValueBase> _values = [];
- late final measureData = Get.find<MeasureDataController>();
- int _activeIndex = -1;
- Rect? zoomRect; // 当前缩放区域的归一化坐标
- @override
- int? frameIndex;
- @override
- int? imageBelongSign;
- Paint get paintPan => measureData.paintPen;
- Paint get paintLinePan => measureData.paintLinePan;
- Paint get paintPointPan => measureData.paintPointPan;
- double get annotationFontSize =>
- measureData.measureSystemSetting.annotationFontSize.toDouble();
- bool get needRecordHistory => true;
- MeasureItemFeature(IMeasureItem refItem) {
- _refItem = refItem;
- if (refItem.parent != null) {
- _id = refItem.parent!.feature?.id ?? 0;
- } else {
- _id = refItem.assignId();
- }
- _innerPoints = [];
- _measureModeName = "";
- if (needRecordHistory) {
- _recordHistory();
- }
- try {
- final measureData = Get.find<MeasureDataController>();
- _featureStyle = FeatureStyle(
- showBriefAnnotation:
- measureData.measureSystemSetting.showBriefAnnotation);
- } catch (e) {
- logger.e("Init meature item's feature style failed: $e");
- }
- }
- int get activeIndex => _activeIndex;
- set activeIndex(int val) {
- if (val != _activeIndex) {
- _activeIndex = val;
- }
- }
- @override
- FeatureStyle get featureStyle => _featureStyle;
- @override
- List<DPoint> get innerPoints => _innerPoints;
- @override
- MeasureItem get refItem => _refItem as MeasureItem;
- @override
- String get measureModeName => _measureModeName;
- /// 所在区域
- IVisualArea? get hostVisualArea => _hostVisualArea;
- set hostVisualArea(IVisualArea? value) {
- if (value != _hostVisualArea) {
- _hostVisualArea = value;
- }
- }
- @override
- bool get isActive => _isActive;
- set isActive(bool value) {
- if (value != _isActive) {
- _isActive = value;
- }
- }
- @override
- int get id => _id;
- /// 获取画布轨迹起点的角标文案,如果测量结果显示简洁注释,则会在轨迹起点显示简洁注释
- String get idText => _getIdText();
- String _getIdText() {
- if (featureStyle.showBriefAnnotation) {
- if (refItem.briefAnnotation.isNotEmpty) {
- return '$id.${refItem.briefAnnotation}';
- }
- }
- return '$id';
- }
- /// 顶点尺寸
- double get vertexSize =>
- measureData.measureSystemSetting.shapeCursorSize * refItem.scaleRatio;
- @override
- List<ValueBase> get values => _values;
- @override
- ValueBase? get value => _values.isNotEmpty ? _values.first : null;
- /// 更新浮点型结果值
- FloatValue? updateFloatValue(
- ItemOutputMeta outputMeta,
- double value,
- VidUsUnit unit,
- ) {
- int index = values.indexWhere((e) => e.meta.name == outputMeta.name);
- if (index < 0) {
- final floatValue = FloatValue(outputMeta, value, unit);
- values.add(floatValue);
- return floatValue;
- } else {
- ValueBase valueBase = values[index];
- if (valueBase is FloatValue) {
- final floatValue = valueBase;
- floatValue.value = value;
- return floatValue;
- } else {
- final newValue = FloatValue(outputMeta, value, unit);
- values[index] = newValue;
- return newValue;
- }
- }
- }
- void initValues() {
- _values = [];
- }
- /// 更新字符串结果值
- StringValue updateStringValue(
- ItemOutputMeta outputMeta,
- String value, [
- VidUsUnit unit = VidUsUnit.None,
- ]) {
- int index = values.indexWhere((e) => e.meta.name == outputMeta.name);
- if (index < 0) {
- final stringValue = StringValue(outputMeta, value, unit);
- values.add(stringValue);
- return stringValue;
- } else {
- ValueBase valueBase = values[index];
- if (valueBase is StringValue) {
- final floatValue = valueBase;
- floatValue.value = value;
- return floatValue;
- } else {
- final newValue = StringValue(outputMeta, value, unit);
- values[index] = newValue;
- return newValue;
- }
- }
- }
- @protected
- DPoint convert2ViewPoint(Size size, DPoint logicPoint) {
- if (zoomRect != null) {
- DPoint zoomedLogicPoint = getZoomedPoint(zoomRect!, logicPoint);
- final x = size.width * zoomedLogicPoint.x;
- final y = size.height * zoomedLogicPoint.y;
- return DPoint(x, y);
- }
- final x = size.width * logicPoint.x;
- final y = size.height * logicPoint.y;
- return DPoint(x, y);
- }
- // 获取点 p 在 Rect r 中的归一化坐标
- DPoint getZoomedPoint(Rect r, DPoint p) {
- double xPctInR = (p.x - r.left) / r.width;
- double yPctInR = (p.y - r.top) / r.height;
- return DPoint(xPctInR, yPctInR);
- }
- @override
- void setZoomRect(Rect? rect) {
- zoomRect = rect;
- }
- @override
- void paintPerfusion(
- Canvas canvas,
- Size size,
- ) {}
- /// 画序号
- ///
- /// [text] 自定义序号内容
- @protected
- void drawId(Canvas canvas, Size size, [String? text]) {
- final String displayText;
- if (refItem.parent == null) {
- displayText = text ?? id.toString();
- } else {
- displayText = '$id.${refItem.description}';
- }
- final point = innerPoints[0];
- var offset = convert2ViewPoint(size, point).toOffset();
- return drawCustomId(canvas, size, offset, displayText);
- }
- /// 画自定义位置的序号
- /// [offset] 位置
- /// [text] 自定义序号内容
- @protected
- void drawCustomId(Canvas canvas, Size size, Offset offset, [String? text]) {
- final displayText = text ?? id.toString();
- final fontSize = annotationFontSize * refItem.scaleRatio;
- final fontOffsetY = 4.0 * refItem.scaleRatio;
- final letterSpacing = 0.0 * refItem.scaleRatio;
- final style = TextStyle(
- fontSize: fontSize,
- color: MeasureColors.Primary,
- // fontWeight: FontWeight.bold,
- letterSpacing: letterSpacing,
- );
- final fontPlace = boundingTextSize(displayText, style);
- double transY = 0;
- double transX = 0;
- final vertexOffsetW = vertexSize / 2;
- if (offset.dx < fontPlace.width) {
- transX = fontSize + vertexOffsetW;
- } else {
- transX = -fontPlace.width - vertexOffsetW;
- }
- if (offset.dy < fontPlace.height + fontOffsetY) {
- transY = fontOffsetY;
- } else {
- transY = -fontPlace.height - fontOffsetY;
- }
- offset = offset.translate(transX, transY);
- canvas.drawText(
- displayText,
- offset,
- style: style,
- );
- }
- /// 画顶点
- void drawVertex(Canvas canvas, Offset offset, [bool active = false]) {
- canvas.drawVertex(offset, vertexSize, active: active);
- }
- /// 画小顶点(1/2 大小)由 measureData.measureSystemSetting.minCursorDistance 来控制是否触发
- void drawMiniVertex(Canvas canvas, Offset offset, [bool active = false]) {
- canvas.drawVertex(offset, vertexSize / 2, active: active);
- }
- /// 画顶点
- void drawCrossVertex(Canvas canvas, Offset offset, [bool active = false]) {
- canvas.drawCrossVertex(offset, vertexSize, active: active);
- }
- /// 画短横标记
- void drawMark(Canvas canvas, Offset offset,
- [bool active = false, bool ifHorizontal = true]) {
- canvas.drawMark(offset, vertexSize,
- active: active, ifHorizontal: ifHorizontal);
- }
- /// 计算文本长度
- static Size boundingTextSize(
- String text,
- TextStyle style, {
- int maxLines = 2 ^ 31,
- double maxWidth = double.infinity,
- }) {
- // if (Get.context == null) return Size.zero;
- if (text.isEmpty) return Size.zero;
- final TextPainter textPainter = TextPainter(
- textDirection: TextDirection.ltr,
- // locale: Localizations.localeOf(Get.context!),
- text: TextSpan(text: text, style: style),
- maxLines: maxLines,
- )..layout(maxWidth: maxWidth);
- return textPainter.size;
- }
- void _recordHistory() {
- if (refItem.parent == null) {
- final recorder = refItem.application.recorder;
- recorder.recordMeasureItem(refItem.meta.name);
- _measureModeName = refItem.application.currentMode.modeType.name;
- }
- }
- @override
- bool checkCanPaint() {
- if (refItem is ITopMeasureItem) {
- return true;
- }
- bool result = true;
- final app = refItem.application;
- if (app.crossFrameContext != null && !app.crossFrameContext!.isOver) {
- final imgSign = app.crossFrameContext!.currentUrl.hashCode;
- final frameIndex = app.frameData!.index;
- result = result && checkFeatureCanPaintInCrossFrame(imgSign, frameIndex);
- }
- return result;
- }
- /// 判断跨帧状态中Future是否可绘制
- bool checkFeatureCanPaintInCrossFrame(
- int imgSign,
- int index,
- ) {
- if (imageBelongSign != null && imageBelongSign != imgSign) {
- return false;
- }
- if (frameIndex != null && frameIndex != index) {
- return false;
- }
- return true;
- }
- }
|