top_item.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. _childIndex = index;
  53. if (workingChild.measuredFeatures.isNotEmpty) {
  54. isNeedRedraw = true;
  55. // workingChild.frameIndex = 1;
  56. IApplication.current!.frameData!.index;
  57. } else {
  58. isNeedRedraw = false;
  59. }
  60. workingChildChanged.emit(this, index);
  61. }
  62. @override
  63. IMeasureItem findChildByName(String name) {
  64. try {
  65. final item = _childItems.firstWhere((e) => e.meta.name == name);
  66. return item;
  67. } catch (e) {
  68. throw ArgumentError("Child item not found.", name);
  69. }
  70. }
  71. T buildFeature();
  72. @override
  73. bool onExecuteMouse(PointInfo args) {
  74. if (ifAutoStart) {
  75. if (feature == null) {
  76. feature = buildFeature();
  77. listenChildrenUpdate();
  78. }
  79. }
  80. if (args.pointType == PointInfoType.mouseDown) {
  81. if (feature == null) {
  82. feature = buildFeature();
  83. listenChildrenUpdate();
  84. }
  85. if (childrenAllDone) {
  86. workingChild.clear();
  87. }
  88. }
  89. feature?.hostVisualArea = args.hostVisualArea;
  90. final result = workingChild.execute(args);
  91. if (result) {
  92. doCalculate();
  93. }
  94. if (ifAutoFinish) {
  95. if (args.pointType == PointInfoType.mouseDown) {
  96. if (childrenAllDone) {
  97. application.autoStartAgain(meta);
  98. }
  99. }
  100. }
  101. return result;
  102. }
  103. @override
  104. bool onExecuteTouch(PointInfo args) {
  105. if (ifAutoStart) {
  106. if (feature == null) {
  107. feature = buildFeature();
  108. listenChildrenUpdate();
  109. }
  110. }
  111. if (args.pointType == PointInfoType.touchDown) {
  112. if (feature == null) {
  113. feature = buildFeature();
  114. listenChildrenUpdate();
  115. }
  116. if (childrenAllDone || workingChild.measuredFeatures.isNotEmpty) {
  117. workingChild.clear();
  118. }
  119. }
  120. feature?.hostVisualArea = args.hostVisualArea;
  121. final result = workingChild.execute(args);
  122. if (result) {
  123. doCalculate();
  124. }
  125. if (ifAutoFinish) {
  126. if (args.pointType == PointInfoType.touchUp) {
  127. if (childrenAllDone) {
  128. application.autoStartAgain(meta);
  129. }
  130. }
  131. }
  132. return result;
  133. }
  134. @protected
  135. void nextChild() {
  136. final count = _childItems.length;
  137. int nextIdx = -1;
  138. for (var i = 0; i < count; i++) {
  139. final child = _childItems[i];
  140. if (child.measuredFeatures.isEmpty) {
  141. nextIdx = i;
  142. break;
  143. }
  144. }
  145. if (nextIdx > -1) {
  146. switchChild(nextIdx);
  147. }
  148. }
  149. @protected
  150. void listenChildrenUpdate() {
  151. for (var item in childItems) {
  152. item.featureUpdated.addListener((sender, e) {
  153. _onChildFeatureUpdated();
  154. });
  155. }
  156. }
  157. void _onChildFeatureUpdated() {
  158. if (workingChild.measuredFeatures.isNotEmpty && !isNeedRedraw) {
  159. nextChild();
  160. }
  161. doFeatureUpdate();
  162. }
  163. }