top_item.dart 3.6 KB

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