animation.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. import {
  2. AnimationModel,
  3. AnimationModelAction,
  4. AnimationModelFrame,
  5. AnimationModelPath,
  6. AnimationModelSubtitle,
  7. } from "@/api";
  8. import {
  9. AnimationGroup,
  10. AnimationModel3D,
  11. AnimationModelAction3D,
  12. AnimationModelFrame3D,
  13. AnimationModelPath3D,
  14. SDK,
  15. sdk as _sdk,
  16. } from "../sdk";
  17. import { computed, nextTick, reactive, ref, watch, watchEffect } from "vue";
  18. import { ams } from "@/store/animation";
  19. import { mergeFuns } from "@/components/drawing/hook";
  20. import { getPathNode } from "./path";
  21. import { diffArrayChange, mount } from "@/utils";
  22. import { Pos } from "@/utils/event";
  23. import Subtitle from "@/components/subtitle/index.vue";
  24. import { Size } from "@/components/drawing/dec";
  25. export let animationGroup: AnimationGroup;
  26. export const getAMKey = (am: AnimationModel) => am.key || am.id;
  27. export const amMap: Record<
  28. string,
  29. {
  30. am?: AnimationModel3D;
  31. frames: Record<string, AnimationModelFrame3D>;
  32. actions: Record<string, AnimationModelAction3D>;
  33. paths: Record<string, AnimationModelPath3D>;
  34. subtitles: Record<string, () => void>;
  35. }
  36. > = reactive({});
  37. export const addAM = (data: AnimationModel): Promise<AnimationModel3D> => {
  38. const key = getAMKey(data);
  39. const stopLoad = watch(
  40. () => {
  41. const exixts = ams.value.some((am) => getAMKey(am) === key);
  42. return [key, exixts] as const;
  43. },
  44. ([key, exixts]) => {
  45. if (!exixts) {
  46. const des = amMap[key];
  47. if (!des) return;
  48. Object.values(des.frames || {}).forEach((frame) => frame.destroy());
  49. Object.values(des.actions || {}).forEach((frame) => frame.destroy());
  50. Object.values(des.paths || {}).forEach((frame) => frame.destroy());
  51. des.am?.destroy();
  52. delete amMap[key];
  53. } else if (!amMap[key]) {
  54. amMap[key] = {
  55. frames: {},
  56. actions: {},
  57. paths: {},
  58. subtitles: {},
  59. };
  60. const am = animationGroup.addAnimationModel(data);
  61. am.bus.on("loadDone", () => {
  62. amMap[key].am = am;
  63. });
  64. }
  65. },
  66. { immediate: true }
  67. );
  68. const stopAttrib = mergeFuns(
  69. watchEffect(() =>
  70. amMap[key].am?.changeVisibilityRange(
  71. data.globalVisibility ? undefined : data.visibilityRange
  72. )
  73. ),
  74. watchEffect(() => amMap[key].am?.changeTitle(data.title)),
  75. watchEffect(() => amMap[key].am?.visibilityTitle(data.showTitle)),
  76. watchEffect(() => amMap[key].am?.changeFontSize(data.fontSize))
  77. );
  78. const stopWatch = watch(
  79. () => ams.value.includes(data),
  80. (exists) => {
  81. if (!exists) {
  82. stopLoad();
  83. stopAttrib();
  84. stopWatch();
  85. }
  86. },
  87. { flush: "post" }
  88. );
  89. return new Promise((resolve) => {
  90. const stopWatch = watchEffect(() => {
  91. if (amMap[key]?.am) {
  92. resolve(amMap[key]!.am!);
  93. nextTick(() => stopWatch());
  94. }
  95. });
  96. });
  97. };
  98. export const addFrame = (
  99. data: AnimationModelFrame
  100. ): Promise<AnimationModelFrame3D> => {
  101. const am = ams.value.find((item) =>
  102. item.frames.find(({ id }) => id === data.id)
  103. );
  104. if (!am) {
  105. throw "找不到am数据";
  106. }
  107. const key = getAMKey(am);
  108. const stopLoad = watch(
  109. () => {
  110. const exists = am.frames.some(({ id }) => id === data.id);
  111. return [amMap[key], exists] as const;
  112. },
  113. ([map, exists]) => {
  114. if (!map.am) return;
  115. if (exists && !map.frames[data.id]) {
  116. map.frames[data.id] = map.am.addFrame(data);
  117. } else if (!exists && map.frames[data.id]) {
  118. map.frames[data.id].destroy();
  119. delete map.frames[data.id];
  120. }
  121. },
  122. { immediate: true }
  123. );
  124. const stopAttrib = mergeFuns(
  125. watchEffect(() => amMap[key].frames[data.id]?.changeTime(data.time)),
  126. watchEffect(() => data.mat && amMap[key].frames[data.id]?.setMat(data.mat))
  127. );
  128. const stopWatch = watch(
  129. () => am.frames.includes(data),
  130. (exists) => {
  131. if (!exists) {
  132. stopLoad();
  133. stopAttrib();
  134. stopWatch();
  135. }
  136. },
  137. { flush: "post" }
  138. );
  139. return new Promise((resolve) => {
  140. const stopWatch = watchEffect(() => {
  141. if (amMap[key]?.frames[data.id]) {
  142. resolve(amMap[key].frames[data.id]);
  143. nextTick(() => stopWatch());
  144. }
  145. });
  146. });
  147. };
  148. export const addAction = (
  149. data: AnimationModelAction
  150. ): Promise<AnimationModelAction3D> => {
  151. const am = ams.value.find((item) =>
  152. item.actions.find(({ id }) => id === data.id)
  153. );
  154. if (!am) {
  155. throw "找不到am数据";
  156. }
  157. const key = getAMKey(am);
  158. const stopLoad = watch(
  159. () => {
  160. const exists = am.actions.some(({ id }) => id === data.id);
  161. return [amMap[key], exists] as const;
  162. },
  163. ([map, exists]) => {
  164. if (!map.am) return;
  165. if (exists && !map.actions[data.id]) {
  166. map.actions[data.id] = map.am.addAction(data);
  167. } else if (!exists && map.actions[data.id]) {
  168. map.actions[data.id].destroy();
  169. delete map.actions[data.id];
  170. }
  171. },
  172. { immediate: true }
  173. );
  174. const stopAttrib = mergeFuns(
  175. watchEffect(() => amMap[key].actions[data.id]?.changeTime(data.time)),
  176. watchEffect(() => {
  177. amMap[key].actions[data.id]?.changeAmplitude(data.amplitude)
  178. }),
  179. watchEffect(() => amMap[key].actions[data.id]?.changeSpeed(data.speed)),
  180. watchEffect(() =>
  181. amMap[key].actions[data.id]?.changeDuration(data.duration)
  182. )
  183. );
  184. const stopWatch = watch(
  185. () => am.actions.includes(data),
  186. (exists) => {
  187. if (!exists) {
  188. stopLoad();
  189. stopAttrib();
  190. stopWatch();
  191. }
  192. },
  193. { flush: "post" }
  194. );
  195. return new Promise((resolve) => {
  196. const stopWatch = watchEffect(() => {
  197. if (amMap[key]?.actions[data.id]) {
  198. resolve(amMap[key].actions[data.id]);
  199. nextTick(() => stopWatch());
  200. }
  201. });
  202. });
  203. };
  204. export const addPath = (
  205. data: AnimationModelPath
  206. ): Promise<AnimationModelPath3D> => {
  207. const am = ams.value.find((item) =>
  208. item.paths.find(({ id }) => id === data.id)
  209. );
  210. if (!am) {
  211. throw "找不到am数据";
  212. }
  213. const path = computed(() =>
  214. data.pathId ? getPathNode(data.pathId) : undefined
  215. );
  216. const key = getAMKey(am);
  217. const stopLoad = watch(
  218. () => {
  219. const exists = am.paths.some(({ id }) => id === data.id);
  220. return [amMap[key], exists, path.value] as const;
  221. },
  222. ([map, exists, path]) => {
  223. if (!map.am || !path) return;
  224. if (exists && !map.paths[data.id]) {
  225. map.paths[data.id] = map.am.addPath({ ...data, path });
  226. } else if (!exists && map.paths[data.id]) {
  227. map.paths[data.id].destroy();
  228. delete map.paths[data.id];
  229. }
  230. },
  231. { immediate: true }
  232. );
  233. const stopAttrib = mergeFuns(
  234. watchEffect(() => amMap[key].paths[data.id]?.changeTime(data.time)),
  235. watchEffect(() => amMap[key].paths[data.id]?.changeReverse(data.reverse)),
  236. watchEffect(() => amMap[key].paths[data.id]?.changeDuration(data.duration)),
  237. watchEffect(() => amMap[key].paths[data.id]?.changePath(path.value))
  238. );
  239. const stopWatch = watch(
  240. () => am.paths.includes(data),
  241. (exists) => {
  242. if (!exists) {
  243. stopLoad();
  244. stopAttrib();
  245. stopWatch();
  246. }
  247. },
  248. { flush: "post" }
  249. );
  250. return new Promise((resolve) => {
  251. const stopWatch = watchEffect(() => {
  252. if (amMap[key]?.paths[data.id]) {
  253. resolve(amMap[key].paths[data.id]);
  254. nextTick(() => stopWatch());
  255. }
  256. });
  257. });
  258. };
  259. export const addSubtitle = (data: AnimationModelSubtitle) => {
  260. const am = ams.value.find((item) =>
  261. item.subtitles.find(({ id }) => id === data.id)
  262. );
  263. if (!am) {
  264. throw "找不到am数据";
  265. }
  266. const key = getAMKey(am);
  267. const size = ref({ width: 0, height: 0 });
  268. const show = ref(false);
  269. const pixel = ref<Pos>();
  270. const stopLoad = watch(
  271. () => {
  272. const exists = am.subtitles.some(({ id }) => id === data.id);
  273. return [amMap[key], exists] as const;
  274. },
  275. ([map, exists]) => {
  276. if (!map.am) return;
  277. if (exists && !map.subtitles[data.id]) {
  278. map.subtitles[data.id] = mount(
  279. document.querySelector("#app")!,
  280. Subtitle,
  281. reactive({
  282. pixel,
  283. show,
  284. data,
  285. sizeChang: (csize: Size) => (size.value = csize),
  286. })
  287. );
  288. } else if (!exists && map.subtitles[data.id]) {
  289. map.subtitles[data.id]();
  290. delete map.subtitles[data.id];
  291. }
  292. },
  293. { immediate: true }
  294. );
  295. const stopAttrib = mergeFuns(
  296. watch([currentTime, () => amMap[am.id].am, size], () => {
  297. if (
  298. currentTime.value >= data.time &&
  299. currentTime.value <= (data.time + data.duration)
  300. ) {
  301. pixel.value = amMap[am.id].am?.getCurrentSubtitlePixel(size.value);
  302. show.value = true;
  303. } else {
  304. show.value = false;
  305. }
  306. }, {immediate: true})
  307. );
  308. const stopWatch = watch(
  309. () => am.subtitles.includes(data),
  310. (exists) => {
  311. if (!exists) {
  312. stopLoad();
  313. stopAttrib();
  314. stopWatch();
  315. }
  316. },
  317. { flush: "post" }
  318. );
  319. };
  320. export const currentTime = ref(0);
  321. export const associationAnimation = (sdk: SDK, el: HTMLDivElement) => {
  322. animationGroup = sdk.createAnimationGroup();
  323. watchEffect(() => {
  324. animationGroup.setCurrentTime(currentTime.value);
  325. });
  326. animationGroup.bus.on("currentTime", (time) => {
  327. currentTime.value = time;
  328. });
  329. watch(
  330. () => [...ams.value],
  331. (newv, oldv = []) => {
  332. const { added } = diffArrayChange(newv, oldv);
  333. added.forEach(addAM);
  334. }
  335. );
  336. watch(
  337. () => ams.value.flatMap((am) => am.frames),
  338. (newv, oldv = []) => {
  339. const { added } = diffArrayChange(newv, oldv);
  340. added.forEach(addFrame);
  341. }
  342. );
  343. watch(
  344. () => ams.value.flatMap((am) => am.actions),
  345. (newv, oldv = []) => {
  346. const { added } = diffArrayChange(newv, oldv);
  347. added.forEach(addAction);
  348. }
  349. );
  350. watch(
  351. () => ams.value.flatMap((am) => am.paths),
  352. (newv, oldv = []) => {
  353. const { added } = diffArrayChange(newv, oldv);
  354. added.forEach(addPath);
  355. }
  356. );
  357. watch(
  358. () => ams.value.flatMap((am) => am.subtitles),
  359. (newv, oldv = []) => {
  360. const { added } = diffArrayChange(newv, oldv);
  361. added.forEach(addSubtitle);
  362. }
  363. );
  364. };