top_item.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. bool isNeedRedraw = false;
  15. TopMeasureItem(ItemMeta meta) : super(meta) {
  16. workingChildChanged = FEventHandler<int>();
  17. }
  18. @override
  19. List<IMeasureItem> get childItems => _childItems;
  20. @override
  21. IMeasureItem get workingChild => _childItems[_childIndex];
  22. /// 子项全部完成
  23. @protected
  24. bool get childrenAllDone =>
  25. _childItems.every((e) => e.measuredFeatures.isNotEmpty);
  26. @override
  27. bool get finishAfterUnactive => false;
  28. @override
  29. bool get repeatableEditable => true;
  30. @override
  31. bool get isCrossFrameMode => false;
  32. @override
  33. late final FEventHandler<int> workingChildChanged;
  34. @override
  35. void switchChild(int index) {
  36. if (index == _childIndex) return;
  37. print('switchChild: $index');
  38. _childIndex = index;
  39. if (workingChild.measuredFeatures.isNotEmpty) {
  40. isNeedRedraw = true;
  41. } else {
  42. isNeedRedraw = false;
  43. }
  44. workingChildChanged.emit(this, index);
  45. }
  46. T buildFeature();
  47. @override
  48. bool onExecuteMouse(PointInfo args) {
  49. if (ifAutoStart) {
  50. if (feature == null) {
  51. feature = buildFeature();
  52. listenChildrenUpdate();
  53. }
  54. }
  55. if (args.pointType == PointInfoType.mouseDown) {
  56. if (feature == null) {
  57. feature = buildFeature();
  58. listenChildrenUpdate();
  59. }
  60. if (childrenAllDone) {
  61. workingChild.clear();
  62. }
  63. }
  64. feature?.hostVisualArea = args.hostVisualArea;
  65. final result = workingChild.execute(args);
  66. if (result) {
  67. doCalculate();
  68. }
  69. if (ifAutoFinish) {
  70. if (args.pointType == PointInfoType.mouseDown) {
  71. if (childrenAllDone) {
  72. application.autoStartAgain(meta);
  73. }
  74. }
  75. }
  76. return result;
  77. }
  78. @override
  79. bool onExecuteTouch(PointInfo args) {
  80. if (ifAutoStart) {
  81. if (feature == null) {
  82. feature = buildFeature();
  83. listenChildrenUpdate();
  84. }
  85. }
  86. if (args.pointType == PointInfoType.touchDown) {
  87. if (feature == null) {
  88. feature = buildFeature();
  89. listenChildrenUpdate();
  90. }
  91. if (childrenAllDone || workingChild.measuredFeatures.isNotEmpty) {
  92. workingChild.clear();
  93. }
  94. }
  95. feature?.hostVisualArea = args.hostVisualArea;
  96. final result = workingChild.execute(args);
  97. if (result) {
  98. doCalculate();
  99. }
  100. if (ifAutoFinish) {
  101. if (args.pointType == PointInfoType.touchUp) {
  102. if (childrenAllDone) {
  103. application.autoStartAgain(meta);
  104. }
  105. }
  106. }
  107. return result;
  108. }
  109. @protected
  110. void nextChild() {
  111. final count = _childItems.length;
  112. int nextIdx = -1;
  113. for (var i = 0; i < count; i++) {
  114. final child = _childItems[i];
  115. if (child.measuredFeatures.isEmpty) {
  116. nextIdx = i;
  117. break;
  118. }
  119. }
  120. if (nextIdx > -1) {
  121. switchChild(nextIdx);
  122. }
  123. }
  124. @protected
  125. void listenChildrenUpdate() {
  126. for (var item in childItems) {
  127. item.featureUpdated.addListener((sender, e) {
  128. _onChildFeatureUpdated();
  129. });
  130. }
  131. }
  132. void _onChildFeatureUpdated() {
  133. if (workingChild.measuredFeatures.isNotEmpty && !isNeedRedraw) {
  134. nextChild();
  135. }
  136. doFeatureUpdate();
  137. }
  138. }