desktop.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import 'dart:math' as math;
  2. import 'package:fis_measure/interfaces/date_types/int_size.dart';
  3. import 'package:fis_measure/interfaces/process/player/play_controller.dart';
  4. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  5. import 'package:fis_measure/view/player/enums.dart';
  6. import 'package:fis_vid/data_host/data_host.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:get/get.dart';
  9. import '../canvas/active_canvas.dart';
  10. import '../canvas/records_canvas.dart';
  11. import '../gesture/mouse_gesture.dart';
  12. import '../player/controller.dart';
  13. import '../player/events.dart';
  14. import '../player/player.dart';
  15. import '../result/results_panel.dart';
  16. class MeasureMainView extends StatefulWidget {
  17. const MeasureMainView({Key? key}) : super(key: key);
  18. @override
  19. State<StatefulWidget> createState() => _MeasureMainViewState();
  20. }
  21. class _MeasureMainViewState extends State<MeasureMainView> {
  22. late final application = Get.find<IApplication>();
  23. late final playerController = Get.find<IPlayerController>();
  24. late bool canMeasure = application.canMeasure;
  25. IntSize get frameSize {
  26. if (application.frameData == null) return IntSize.empty;
  27. return IntSize.fill(
  28. application.frameData!.width,
  29. application.frameData!.height,
  30. );
  31. }
  32. @override
  33. void initState() {
  34. super.initState();
  35. WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
  36. onCanMeasureChanged(this, application.canMeasure);
  37. application.canMeasureChanged.addListener(onCanMeasureChanged);
  38. });
  39. }
  40. @override
  41. void dispose() {
  42. application.canMeasureChanged.removeListener(onCanMeasureChanged);
  43. super.dispose();
  44. }
  45. void onCanMeasureChanged(Object sender, bool e) {
  46. if (e != canMeasure) {
  47. setState(() {
  48. canMeasure = e;
  49. });
  50. }
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. return Container(
  55. color: Colors.black,
  56. child: CustomMultiChildLayout(
  57. delegate: _LayerLayoutDelegate(imageSize: frameSize),
  58. children: [
  59. LayoutId(
  60. id: _LayerLayoutIds.player,
  61. child: Container(
  62. decoration: BoxDecoration(
  63. // border: Border.all(color: Colors.white),
  64. ),
  65. child: VidPlayer(playerController as VidPlayerController),
  66. ),
  67. ),
  68. if (canMeasure) ...[
  69. LayoutId(
  70. id: _LayerLayoutIds.recordsCanvas,
  71. child: const MeasureRecordsCanvasPanel(),
  72. ),
  73. LayoutId(
  74. id: _LayerLayoutIds.result,
  75. child: const MeasureResultPanel(),
  76. ),
  77. LayoutId(
  78. id: _LayerLayoutIds.activeCanvas,
  79. child: const MeasureActiveCanvasPanel(),
  80. ),
  81. LayoutId(
  82. id: _LayerLayoutIds.gesture,
  83. child: const MeasureMouseGesturePanel(),
  84. ),
  85. ],
  86. ],
  87. ),
  88. );
  89. }
  90. }
  91. class _LayerLayoutDelegate extends MultiChildLayoutDelegate {
  92. final IntSize? imageSize;
  93. Offset? layoutOffset;
  94. Size? layoutSize;
  95. _LayerLayoutDelegate({required this.imageSize});
  96. @override
  97. void performLayout(Size size) {
  98. if (imageSize == null) return;
  99. if (!hasChild(_LayerLayoutIds.player)) return;
  100. /// 以Contain方式填充布局,计算定位偏移量
  101. calcSize(size);
  102. final offset = layoutOffset!;
  103. final renderSize = layoutSize!;
  104. Get.find<IApplication>().displaySize = renderSize;
  105. layoutLayer(_LayerLayoutIds.player, offset, renderSize);
  106. /// 其他层按播放器尺寸位置层叠布局
  107. layoutLayer(_LayerLayoutIds.recordsCanvas, offset, renderSize);
  108. layoutLayer(_LayerLayoutIds.result, offset, renderSize);
  109. layoutLayer(_LayerLayoutIds.activeCanvas, offset, renderSize);
  110. layoutLayer(_LayerLayoutIds.gesture, offset, renderSize);
  111. }
  112. void layoutLayer(_LayerLayoutIds layoutId, Offset offset, Size size) {
  113. if (hasChild(layoutId)) {
  114. layoutChild(
  115. layoutId,
  116. BoxConstraints.loose(size),
  117. );
  118. positionChild(layoutId, offset);
  119. }
  120. }
  121. void calcSize(Size size) {
  122. final parentWHRatio = size.width / size.height;
  123. final imageWHRatio = imageSize!.width / imageSize!.height;
  124. if (imageWHRatio < parentWHRatio) {
  125. // 高度撑满
  126. final layoutWidth = size.height * imageWHRatio;
  127. final layoutHeight = size.height;
  128. final offsetX = (size.width - layoutWidth) / 2;
  129. layoutOffset = Offset(offsetX, 0);
  130. layoutSize = Size(layoutWidth, layoutHeight);
  131. } else if (imageWHRatio > parentWHRatio) {
  132. // 宽度撑满
  133. final layoutWidth = size.width;
  134. final layoutHeight = size.width / imageWHRatio;
  135. final offsetY = (size.height - layoutHeight) / 2;
  136. layoutOffset = Offset(0, offsetY);
  137. layoutSize = Size(layoutWidth, layoutHeight);
  138. } else {
  139. layoutOffset = Offset.zero;
  140. layoutSize = size;
  141. }
  142. // final widthRatio = imageSize!.width / size.width;
  143. // final heightRatio = imageSize!.height / size.height;
  144. // if (widthRatio < heightRatio) {
  145. // final layoutWidth = size.width * heightRatio;
  146. // final layoutHeight = size.height;
  147. // final offsetX = (size.width - layoutWidth) / 2;
  148. // layoutOffset = Offset(offsetX, 0);
  149. // layoutSize = Size(layoutWidth, layoutHeight);
  150. // } else if (widthRatio > heightRatio) {
  151. // final layoutWidth = size.width;
  152. // final layoutHeight = size.height * widthRatio;
  153. // final offsetY = (size.height - layoutHeight) / 2;
  154. // layoutOffset = Offset(0, offsetY);
  155. // layoutSize = Size(layoutWidth, layoutHeight);
  156. // } else {
  157. // layoutOffset = Offset.zero;
  158. // layoutSize = size;
  159. // }
  160. }
  161. @override
  162. bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
  163. return false;
  164. }
  165. }
  166. enum _LayerLayoutIds {
  167. /// 播放器
  168. player,
  169. /// 测量记录画板
  170. recordsCanvas,
  171. /// 活动测量画板
  172. activeCanvas,
  173. /// 结果面板
  174. result,
  175. /// 手势面板
  176. gesture,
  177. }