sign.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <!-- <template v-if="show">
  3. <div :style="linePixel">
  4. {{ path.name }}
  5. </div>
  6. <template v-for="(p, i) in pointPixels">
  7. <span :style="p" v-if="p && path.points[i].name">
  8. {{ path.points[i].name }}
  9. </span>
  10. </template>
  11. </template> -->
  12. </template>
  13. <script lang="ts" setup>
  14. import { computed, onUnmounted, ref, watch, watchEffect } from "vue";
  15. import { sdk } from "@/sdk";
  16. import type { Path } from "@/store";
  17. import { inRevise } from "bill/utils";
  18. export type SignProps = { path: Path };
  19. const props = defineProps<SignProps>();
  20. const emit = defineEmits<{
  21. (e: "delete"): void;
  22. (e: "updateLinePosition", val: Path["linePosition"]): void;
  23. (e: "updatePoints", val: Path["points"]): void;
  24. }>();
  25. const getLineProps = () => ({
  26. width: props.path.lineWidth,
  27. color: props.path.lineColor,
  28. altitudeAboveGround: props.path.lineAltitudeAboveGround,
  29. position: props.path.linePosition?.position || props.path.points[0]?.position,
  30. normal: props.path.linePosition?.normal || { x: 0, y: 0, z: 1 },
  31. modelId: props.path.linePosition?.modelId || props.path.points[0]?.modelId,
  32. });
  33. const path = sdk.createPath({
  34. name: props.path.name,
  35. showName: props.path.showName,
  36. fontSize: props.path.fontSize,
  37. showDirection: props.path.showDirection,
  38. reverseDirection: props.path.reverseDirection,
  39. line: getLineProps(),
  40. points: props.path.points,
  41. });
  42. path.changeCanEdit(false);
  43. watchEffect(() => path.visibilityName(props.path.showName));
  44. const toCameraDistance = ref(path.toCameraDistance);
  45. path.bus.on("toCameraDistanceChange", (v) => (toCameraDistance.value = v));
  46. const show = computed(
  47. () =>
  48. !props.path.points.length ||
  49. props.path.globalVisibility ||
  50. toCameraDistance.value <= Math.pow(props.path.visibilityRange, 2)
  51. );
  52. watchEffect(() => {
  53. console.log(props.path.globalVisibility, toCameraDistance.value);
  54. console.error("globalVisibility", show.value);
  55. // path.visibility(show.value);
  56. });
  57. let currentPoints = props.path.points;
  58. let changPointsTimeout: any;
  59. path.bus.on("changePoints", (points) => {
  60. clearTimeout(changPointsTimeout);
  61. currentPoints = points.map((p, ndx) => ({
  62. name: p.name || `标记点${ndx + 1}`,
  63. position: { ...p.position },
  64. modelId: p.modelId,
  65. }));
  66. console.log("updatePoints", currentPoints);
  67. emit("updatePoints", currentPoints);
  68. });
  69. watchEffect(() => {
  70. path.changeName(props.path.name);
  71. });
  72. watch(
  73. () => props.path.points.map((i) => ({ modelId: i.modelId, position: i.position })),
  74. (p) => {
  75. changPointsTimeout = setTimeout(() => {
  76. if (inRevise(props.path.points, currentPoints)) {
  77. path.changePathPoints(p);
  78. currentPoints = props.path.points;
  79. }
  80. }, 16);
  81. },
  82. { deep: true, flush: "post" }
  83. );
  84. watchEffect(() => {
  85. for (const point of props.path.points) {
  86. watchEffect(() => {
  87. const ndx = props.path.points.indexOf(point);
  88. if (~ndx) {
  89. path.changePointName(ndx, point.name);
  90. currentPoints[ndx].name = point.name;
  91. }
  92. });
  93. }
  94. });
  95. let currentLine = getLineProps();
  96. let changLineTimeout: any;
  97. watch(
  98. getLineProps,
  99. (val) => {
  100. changLineTimeout = setTimeout(() => {
  101. if (inRevise(val, currentLine)) {
  102. path.changeLine(val);
  103. currentLine = val;
  104. }
  105. }, 16);
  106. },
  107. { deep: true }
  108. );
  109. path.bus.on("linePositionChange", (position) => {
  110. const p = {
  111. position: { ...position.pos },
  112. normal: { ...position.normal },
  113. modelId: position.modelId,
  114. };
  115. currentLine = { ...getLineProps(), ...p };
  116. emit("updateLinePosition", p);
  117. });
  118. onUnmounted(() => {
  119. console.error(path, "destory");
  120. path.destroy();
  121. });
  122. defineExpose(path);
  123. </script>
  124. <style lang="scss" scoped></style>