path.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { diffArrayChange, mount, shallowWatchArray } from "@/utils";
  2. import TaggingComponent from "@/components/path/list.vue";
  3. import { Path as PathData, paths } from "@/store/path";
  4. import { sdk, Path, SDK } from "../sdk";
  5. import { reactive, ref, watch, watchEffect } from "vue";
  6. import { groupProxy } from "@/store/group";
  7. import { isScenePlayRun, pauseScene, playScene } from "@/utils/full";
  8. import { analysisPose, setPose } from ".";
  9. import { custom, showPathsStack, showPathStack } from "@/env";
  10. // -----------------导览线关联--------------------
  11. export type PathNode = Path;
  12. export type PathsNode = { node: PathNode; id: PathData["id"] }[];
  13. const pathNodes = reactive(new Map<PathData, PathNode>());
  14. export const getPathNode = (
  15. path: PathData | PathData["id"]
  16. ): undefined | PathNode => {
  17. if (typeof path !== "object") {
  18. path = paths.value.find((item) => item.id === path)!;
  19. if (!path) return void 0;
  20. }
  21. return pathNodes.get(path);
  22. };
  23. export const pathsGroup = groupProxy(() => {
  24. const nodes = [] as PathNode[];
  25. for (const path of paths.value) {
  26. const node = getPathNode(path);
  27. if (node) {
  28. nodes.push(node);
  29. }
  30. }
  31. return nodes;
  32. });
  33. watchEffect(() => {
  34. pathsGroup.visibility(custom.showPaths);
  35. if (custom.showPath) {
  36. const node = getPathNode(custom.showPath);
  37. node?.visibility(true);
  38. node?.fly()
  39. console.log('a1', node, custom.showPath)
  40. }
  41. })
  42. export const playScenePath = async (
  43. path: PathData,
  44. forceFull = false,
  45. ) => {
  46. const node = getPathNode(path)
  47. if (!node) return null;
  48. showPathsStack.push(ref(false))
  49. showPathStack.push(ref(path.id))
  50. let initPose: any;
  51. await playScene({
  52. play: () => {
  53. return new Promise(resolve => {
  54. initPose = analysisPose(sdk.getPose());
  55. node.play(resolve)
  56. })
  57. },
  58. pause: () => {
  59. setPose(initPose)
  60. node.pause();
  61. }
  62. }, forceFull)
  63. showPathsStack.pop()
  64. showPathStack.pop()
  65. }
  66. export const pauseScenePath = pauseScene
  67. export const isScenePathPlayIng = isScenePlayRun
  68. export const associationPaths = (sdk: SDK, el: HTMLDivElement) => {
  69. mount<PathsNode>(el, TaggingComponent, { paths }, (pathsNode) => {
  70. watch(
  71. () => {
  72. if (!pathsNode) return []
  73. pathsNode.forEach(i => !!i?.id)
  74. return pathsNode
  75. },
  76. (nodes, oldNodes = []) => {
  77. const { added, deleted } = diffArrayChange<PathsNode>(
  78. nodes,
  79. oldNodes as PathsNode
  80. );
  81. for (const add of added) {
  82. if (add) {
  83. const path = paths.value.find((item) => item.id === add.id)!;
  84. pathNodes.set(path, add.node);
  85. }
  86. }
  87. for (const del of deleted) {
  88. if (del) {
  89. const path = paths.value.find((item) => item.id === del.id)!;
  90. pathNodes.delete(path);
  91. }
  92. }
  93. },
  94. {immediate: true}
  95. );
  96. });
  97. };