top_item.dart 4.6 KB

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