top_item.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'package:fis_common/event/event_type.dart';
  2. import 'package:fis_measure/interfaces/process/items/item.dart';
  3. import 'package:fis_measure/interfaces/process/items/item_metas.dart';
  4. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'item.dart';
  7. import 'item_feature.dart';
  8. abstract class TopMeasureItem<T extends MeasureItemFeature>
  9. extends MeasureItem<T> implements ITopMeasureItem {
  10. final List<IMeasureItem> _childItems = [];
  11. int _childIndex = 0;
  12. bool get ifAutoStart => false;
  13. bool get ifAutoFinish => false;
  14. TopMeasureItem(ItemMeta meta) : super(meta) {
  15. workingChildChanged = FEventHandler<int>();
  16. }
  17. @override
  18. List<IMeasureItem> get childItems => _childItems;
  19. @override
  20. IMeasureItem get workingChild => _childItems[_childIndex];
  21. /// 子项全部完成
  22. @protected
  23. bool get childrenAllDone =>
  24. _childItems.every((e) => e.measuredFeatures.isNotEmpty);
  25. @override
  26. late final FEventHandler<int> workingChildChanged;
  27. @override
  28. void switchChild(int index) {
  29. if (index == _childIndex) return;
  30. _childIndex = index;
  31. workingChildChanged.emit(this, index);
  32. }
  33. T buildFeature();
  34. @override
  35. bool onExecuteMouse(PointInfo args) {
  36. if (ifAutoStart) {
  37. if (feature == null) {
  38. feature = buildFeature();
  39. listenChildrenUpdate();
  40. }
  41. }
  42. if (args.pointType == PointInfoType.mouseDown) {
  43. if (feature == null) {
  44. feature = buildFeature();
  45. listenChildrenUpdate();
  46. }
  47. if (childrenAllDone) {
  48. workingChild.clear();
  49. }
  50. }
  51. feature?.hostVisualArea = args.hostVisualArea;
  52. final result = workingChild.execute(args);
  53. if (result) {
  54. doCalculate();
  55. }
  56. if (ifAutoFinish) {
  57. if (args.pointType == PointInfoType.mouseDown) {
  58. if (childrenAllDone) {
  59. application.switchItem(meta);
  60. }
  61. }
  62. }
  63. return result;
  64. }
  65. @override
  66. bool onExecuteTouch(PointInfo args) {
  67. return workingChild.execute(args);
  68. }
  69. @protected
  70. void nextChild() {
  71. final count = _childItems.length;
  72. int nextIdx = -1;
  73. for (var i = 0; i < count; i++) {
  74. final child = _childItems[i];
  75. if (child.measuredFeatures.isEmpty) {
  76. nextIdx = i;
  77. break;
  78. }
  79. }
  80. if (nextIdx > -1) {
  81. switchChild(nextIdx);
  82. }
  83. }
  84. @protected
  85. void listenChildrenUpdate() {
  86. for (var item in childItems) {
  87. item.featureUpdated.addListener((sender, e) {
  88. _onChildFeatureUpdated();
  89. });
  90. }
  91. }
  92. void _onChildFeatureUpdated() {
  93. if (workingChild.measuredFeatures.isNotEmpty) {
  94. nextChild();
  95. }
  96. doFeatureUpdate();
  97. }
  98. }