top_item.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/application.dart';
  5. import 'package:fis_measure/interfaces/process/workspace/point_info.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'item.dart';
  8. import 'item_feature.dart';
  9. abstract class TopMeasureItem<T extends MeasureItemFeature>
  10. extends MeasureItem<T> implements ITopMeasureItem {
  11. final List<IMeasureItem> _childItems = [];
  12. int _childIndex = 0;
  13. bool _isCrossFrameMode = false;
  14. bool _canChildOutputSelf = true;
  15. bool get ifAutoStart => false;
  16. bool get ifAutoFinish => false;
  17. bool isNeedRedraw = false;
  18. TopMeasureItem(ItemMeta meta) : super(meta) {
  19. workingChildChanged = FEventHandler<int>();
  20. }
  21. @override
  22. List<IMeasureItem> get childItems => _childItems;
  23. @override
  24. IMeasureItem get workingChild => _childItems[_childIndex];
  25. @override
  26. int get workingChildIndex => _childIndex;
  27. /// 子项全部完成
  28. @protected
  29. bool get childrenAllDone =>
  30. _childItems.every((e) => e.measuredFeatures.isNotEmpty);
  31. @override
  32. bool get finishAfterUnactive => false;
  33. @override
  34. bool get repeatableEditable => true;
  35. @override
  36. bool get isCrossFrameMode => _isCrossFrameMode;
  37. @protected
  38. set isCrossFrameMode(bool val) => _isCrossFrameMode = val;
  39. @override
  40. bool get canChildOutputSelf => _canChildOutputSelf;
  41. set canChildOutputSelf(bool val) => _canChildOutputSelf = val;
  42. @override
  43. late final FEventHandler<int> workingChildChanged;
  44. @override
  45. void switchChild(int index) {
  46. if (index == _childIndex) return;
  47. print('switchChild: $index');
  48. _childIndex = index;
  49. if (workingChild.measuredFeatures.isNotEmpty) {
  50. isNeedRedraw = true;
  51. // workingChild.frameIndex = 1;
  52. IApplication.current!.frameData!.index;
  53. } else {
  54. isNeedRedraw = false;
  55. }
  56. workingChildChanged.emit(this, index);
  57. }
  58. @override
  59. IMeasureItem findChildByName(String name) {
  60. try {
  61. final item = _childItems.firstWhere((e) => e.meta.name == name);
  62. return item;
  63. } catch (e) {
  64. throw ArgumentError("Child item not found.", name);
  65. }
  66. }
  67. T buildFeature();
  68. @override
  69. bool onExecuteMouse(PointInfo args) {
  70. if (ifAutoStart) {
  71. if (feature == null) {
  72. feature = buildFeature();
  73. listenChildrenUpdate();
  74. }
  75. }
  76. if (args.pointType == PointInfoType.mouseDown) {
  77. if (feature == null) {
  78. feature = buildFeature();
  79. listenChildrenUpdate();
  80. }
  81. if (childrenAllDone) {
  82. workingChild.clear();
  83. }
  84. }
  85. feature?.hostVisualArea = args.hostVisualArea;
  86. final result = workingChild.execute(args);
  87. if (result) {
  88. doCalculate();
  89. }
  90. if (ifAutoFinish) {
  91. if (args.pointType == PointInfoType.mouseDown) {
  92. if (childrenAllDone) {
  93. application.autoStartAgain(meta);
  94. }
  95. }
  96. }
  97. return result;
  98. }
  99. @override
  100. bool onExecuteTouch(PointInfo args) {
  101. if (ifAutoStart) {
  102. if (feature == null) {
  103. feature = buildFeature();
  104. listenChildrenUpdate();
  105. }
  106. }
  107. if (args.pointType == PointInfoType.touchDown) {
  108. if (feature == null) {
  109. feature = buildFeature();
  110. listenChildrenUpdate();
  111. }
  112. if (childrenAllDone || workingChild.measuredFeatures.isNotEmpty) {
  113. workingChild.clear();
  114. }
  115. }
  116. feature?.hostVisualArea = args.hostVisualArea;
  117. final result = workingChild.execute(args);
  118. if (result) {
  119. doCalculate();
  120. }
  121. if (ifAutoFinish) {
  122. if (args.pointType == PointInfoType.touchUp) {
  123. if (childrenAllDone) {
  124. application.autoStartAgain(meta);
  125. }
  126. }
  127. }
  128. return result;
  129. }
  130. @protected
  131. void nextChild() {
  132. final count = _childItems.length;
  133. int nextIdx = -1;
  134. for (var i = 0; i < count; i++) {
  135. final child = _childItems[i];
  136. if (child.measuredFeatures.isEmpty) {
  137. nextIdx = i;
  138. break;
  139. }
  140. }
  141. if (nextIdx > -1) {
  142. switchChild(nextIdx);
  143. }
  144. }
  145. @protected
  146. void listenChildrenUpdate() {
  147. for (var item in childItems) {
  148. item.featureUpdated.addListener((sender, e) {
  149. _onChildFeatureUpdated();
  150. });
  151. }
  152. }
  153. void _onChildFeatureUpdated() {
  154. if (workingChild.measuredFeatures.isNotEmpty && !isNeedRedraw) {
  155. nextChild();
  156. }
  157. doFeatureUpdate();
  158. }
  159. }