index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <template>
  2. <div :class="{ focusAM: focusAM }" class="animation-layout">
  3. <Left
  4. :focus="focusAM"
  5. @update:focus="updateFocus"
  6. class="animation-left"
  7. @change-select="changeSelect"
  8. @delete="deleteAm"
  9. />
  10. <Right
  11. v-if="focusAM && !play"
  12. :am="focusAM"
  13. :frameAction="frameAction"
  14. @change-frame-action="(f) => (frameAction = f.action)"
  15. class="animation-right"
  16. v-model:activeAttrib="activeAttrib"
  17. @add-frame="add('frames', { duration: 0.1 })"
  18. @add-path="(preset) => add('paths', { reverse: false, ...preset })"
  19. @add-subtitle="add('subtitles', { content: '', background: '#000000' })"
  20. @add-action="(preset) => add('actions', preset)"
  21. @apply-global="(k) => applyGlobal(k)"
  22. />
  23. <GlobalFrame
  24. class="global-frame"
  25. v-if="activeAttrib?.key !== 'frames' && !play && focusAM && !currentIsFullView"
  26. :data="{ id: '0', name: 'global-frame', time: 0 }"
  27. :frame-action="frameAction"
  28. @change-frame-action="(action) => (frameAction = action.action)"
  29. />
  30. <BottomPano class="strengthen-top">
  31. <Bottom
  32. :am="focusAM"
  33. :follow="play || follow"
  34. v-model:current-time="currentTime"
  35. v-model:active="activeAttrib"
  36. />
  37. </BottomPano>
  38. </div>
  39. </template>
  40. <script lang="ts" setup>
  41. import Left from "./left.vue";
  42. import Right from "./right/index.vue";
  43. import GlobalFrame from "./right/frame.vue";
  44. import Bottom from "./bottom.vue";
  45. import BottomPano from "@/layout/bottom-pano.vue";
  46. import router, { back } from "@/router";
  47. import { enterEdit, sysBus } from "@/store";
  48. import { useViewStack } from "@/hook";
  49. import { ams, AnimationModel, autoSaveAnimationModel } from "@/store/animation";
  50. import { computed, nextTick, onUnmounted, reactive, ref, watch, watchEffect } from "vue";
  51. import { Active } from "./type";
  52. import {
  53. getAddTLItemAttr,
  54. getAddTLItemTimeByTime,
  55. } from "@/components/drawing-time-line/check";
  56. import { Dialog, Message } from "bill/expose-common";
  57. import { listener, mergeFuns, uuid } from "@/components/drawing/hook";
  58. import { title } from "./type";
  59. import {
  60. amMap,
  61. getAMKey,
  62. currentTime,
  63. play,
  64. animationGroup,
  65. } from "@/sdk/association/animation";
  66. import { sdk, AnimationModel3D } from "@/sdk";
  67. import {
  68. bottomBarHeightStack,
  69. showAMsStack,
  70. showBottomBarStack,
  71. showHeadBarStack,
  72. showLeftCtrlPanoStack,
  73. showLeftPanoStack,
  74. showModeTabStack,
  75. showRightPanoStack,
  76. showSearchStack,
  77. } from "@/env";
  78. import { clickListener } from "@/utils/event";
  79. import { useRMenus } from "@/components/right-menu";
  80. import { asyncTimeout } from "@/utils";
  81. import { currentIsFullView } from "@/utils/full";
  82. enterEdit(() => back());
  83. useViewStack(autoSaveAnimationModel);
  84. sysBus.on("leave", () => {
  85. updateFocus();
  86. });
  87. useViewStack(() => {
  88. const showRight = ref(false);
  89. animationGroup.enterEditPannel();
  90. return mergeFuns(
  91. showBottomBarStack.push(ref(true)),
  92. bottomBarHeightStack.push(ref("0px")),
  93. showRightPanoStack.push(showRight),
  94. showLeftPanoStack.push(ref(true)),
  95. showAMsStack.push(ref(true)),
  96. showHeadBarStack.push(computed(() => !play.value)),
  97. showLeftCtrlPanoStack.push(computed(() => !play.value)),
  98. showModeTabStack.push(computed(() => !play.value)),
  99. showSearchStack.push(computed(() => !play.value)),
  100. () => animationGroup.exitEditPannel(),
  101. watchEffect((onCleanup) => {
  102. const cleanups: any[] = [];
  103. if (play.value || activeAttrib.value?.key === "frames") {
  104. cleanups.push(showRightPanoStack.push(ref(false)));
  105. }
  106. if (play.value) {
  107. cleanups.push(showLeftPanoStack.push(ref(false)));
  108. }
  109. onCleanup(mergeFuns(cleanups));
  110. }),
  111. watchEffect(() => {
  112. showRight.value = !!focusAM.value;
  113. })
  114. );
  115. });
  116. const focusAM = ref<AnimationModel>();
  117. const activeAttrib = ref<Active>();
  118. const follow = ref(false);
  119. const frameAction = ref<string>();
  120. const applyGlobal = async (k: keyof AnimationModel) => {
  121. console.error(k, focusAM.value![k]);
  122. if (!(await Dialog.confirm("确定要将此属性应用到所有动画模型?"))) return;
  123. ams.value.forEach((am: any) => (am[k] = focusAM.value![k]));
  124. };
  125. const amM = computed(() => focusAM.value && amMap[getAMKey(focusAM.value)]);
  126. watch(play, (play, _, onCleanup) => {
  127. if (play) {
  128. const oldAction = frameAction.value;
  129. frameAction.value = undefined;
  130. onCleanup(() => (frameAction.value = oldAction));
  131. }
  132. });
  133. onUnmounted(() => {
  134. currentTime.value = 0;
  135. play.value = false;
  136. });
  137. const asyncOper = (item: AnimationModel, oper: (obj: AnimationModel3D) => void) => {
  138. let onCleanup = () => {};
  139. if (amMap[getAMKey(item)]?.am) {
  140. oper(amMap[getAMKey(item)]!.am!);
  141. } else {
  142. onCleanup = watchEffect(() => {
  143. if (amMap[getAMKey(item)]?.am!) {
  144. oper(amMap[getAMKey(item)]!.am!);
  145. onCleanup && onCleanup();
  146. }
  147. });
  148. }
  149. return onCleanup;
  150. };
  151. watchEffect((onCleanup) => {
  152. if (!amM.value) return;
  153. const am3d = amM.value.am;
  154. if (!am3d) return;
  155. const updateMat = (data: any) => {
  156. if (!data.byControl) return;
  157. const mat = JSON.parse(JSON.stringify(am3d.getModelPose()));
  158. if (data.quaAtPath) {
  159. focusAM.value!.mat = !focusAM.value!.mat
  160. ? { quaAtPath: data.quaAtPath }
  161. : {
  162. ...focusAM.value!.mat,
  163. quaAtPath: data.quaAtPath,
  164. };
  165. } else if (activeAttrib.value?.key === "frames") {
  166. const frame = focusAM.value!.frames[activeAttrib.value.ndx];
  167. frame.mat = mat;
  168. } else {
  169. focusAM.value!.mat = !focusAM.value!.mat
  170. ? mat
  171. : {
  172. quaAtPath: focusAM.value!.mat.quaAtPath,
  173. ...mat,
  174. };
  175. }
  176. };
  177. am3d.bus.on("transformChanged", updateMat);
  178. console.error(frameAction.value, am3d);
  179. switch (frameAction.value) {
  180. case "translate":
  181. am3d.enterMoveMode();
  182. break;
  183. case "rotate":
  184. am3d.enterRotateMode();
  185. break;
  186. case "scale":
  187. am3d.enterScaleMode();
  188. break;
  189. }
  190. onCleanup(() => {
  191. am3d.leaveTransform();
  192. am3d.bus.off("transformChanged", updateMat);
  193. });
  194. });
  195. const updateFocus = (am?: AnimationModel) => {
  196. if (focusAM.value && focusAM.value !== am) {
  197. asyncOper(focusAM.value, (item) => {
  198. item.changeSelect(false);
  199. });
  200. }
  201. activeAttrib.value = undefined;
  202. focusAM.value = am;
  203. am && asyncOper(am, (item) => item.changeSelect(true));
  204. };
  205. watch(
  206. () => [ams.value, ams.value.map((am) => amMap[getAMKey(am)]?.am)] as const,
  207. ([ams, am3ds], _, onCleanup) => {
  208. const cleanups = am3ds.map((am3d, ndx) => {
  209. const am = ams[ndx];
  210. console.log("监听", am.title, am3d);
  211. const update = (f: boolean) => {
  212. console.log("changeSelect", f);
  213. if (focusAM.value === am) {
  214. focusAM.value = f ? am : undefined;
  215. } else if (f) {
  216. focusAM.value = am;
  217. }
  218. };
  219. am3d?.bus.on("changeSelect", update);
  220. return () => {
  221. console.log("取消监听", am.title, am3d);
  222. am3d?.bus.off("changeSelect", update);
  223. };
  224. });
  225. onCleanup(mergeFuns(cleanups));
  226. },
  227. { immediate: true }
  228. );
  229. watch(
  230. activeAttrib,
  231. async (_a, _b, onCleanup) => {
  232. console.error("activeAttrib", activeAttrib.value);
  233. if (!activeAttrib.value) return;
  234. const cur = focusAM.value![activeAttrib.value.key][activeAttrib.value.ndx];
  235. const updateFocus = () => {
  236. const rang = [cur.time, cur.time + (cur.duration || 0)];
  237. if (currentTime.value < rang[0] || currentTime.value > rang[1]) {
  238. activeAttrib.value = undefined;
  239. }
  240. };
  241. follow.value = true;
  242. currentTime.value = cur.time!;
  243. nextTick(() => (follow.value = false));
  244. onCleanup(
  245. watch(
  246. () => [currentTime.value, cur.time, cur.time + (cur.duration || 0)],
  247. updateFocus,
  248. { flush: "sync" }
  249. )
  250. );
  251. },
  252. { flush: "sync" }
  253. );
  254. const add = <T extends Active["key"]>(
  255. key: T,
  256. preset: Partial<AnimationModel[T][0]> = {}
  257. ) => {
  258. const attr = getAddTLItemTimeByTime(
  259. focusAM.value![key],
  260. currentTime.value,
  261. typeof preset.duration === "number" ? preset.duration : 10
  262. );
  263. if (!attr) {
  264. Message.error("同一时间内请勿重复添加");
  265. } else {
  266. const item = reactive({
  267. id: uuid(),
  268. name: title[key],
  269. ...preset,
  270. ...attr,
  271. } as any);
  272. if (key === "frames") {
  273. asyncOper(
  274. focusAM.value!,
  275. (am3d) => (item.mat = JSON.parse(JSON.stringify(am3d.getModelPose())))
  276. );
  277. }
  278. focusAM.value![key].push(item);
  279. // currentTime.value = item.time;
  280. activeAttrib.value = {
  281. ndx: focusAM.value![key].length - 1,
  282. key,
  283. };
  284. }
  285. };
  286. let cleanSelect: (() => void) | null = null;
  287. const changeSelect = ({ select, unSelect }: Record<string, AnimationModel[]>) => {
  288. cleanSelect && cleanSelect();
  289. cleanSelect = mergeFuns(
  290. ...select.map((item) => asyncOper(item, (am) => am.changeShow(true))),
  291. ...unSelect.map((item) =>
  292. asyncOper(item, (am) => {
  293. if (item === focusAM.value) {
  294. updateFocus(undefined);
  295. }
  296. am.changeShow(false);
  297. })
  298. )
  299. );
  300. };
  301. const deleteAm = (am: AnimationModel) => {
  302. if (am === focusAM.value) {
  303. activeAttrib.value = undefined;
  304. focusAM.value = undefined;
  305. }
  306. ams.value.splice(ams.value.indexOf(am), 1);
  307. };
  308. let unMount: () => void;
  309. onUnmounted(
  310. mergeFuns([
  311. // listener(
  312. // document.querySelector("#layout-app .scene-canvas") as HTMLDivElement,
  313. // "contextmenu",
  314. // (ev) => {
  315. // ev.preventDefault();
  316. // }
  317. // ),
  318. clickListener(
  319. document.querySelector("#layout-app .scene-canvas") as HTMLDivElement,
  320. (pixel) => {
  321. const pos = sdk.getPositionByScreen(pixel);
  322. if (!focusAM.value) return;
  323. unMount && unMount();
  324. setTimeout(() => {
  325. unMount = useRMenus(pixel, [
  326. {
  327. label: "移动到这里",
  328. icon: "move",
  329. handler() {
  330. amMap[getAMKey(focusAM.value!)]?.am?.moveModelTo(pixel, pos?.worldPos);
  331. },
  332. },
  333. ]);
  334. });
  335. },
  336. 2
  337. ),
  338. ])
  339. );
  340. frameAction.value = "translate";
  341. </script>
  342. <style lang="scss" scoped>
  343. .animation-layout {
  344. --bottom-height: 70px;
  345. &.focusAM {
  346. --bottom-height: 225px;
  347. }
  348. }
  349. .animation-left {
  350. height: calc(100vh - var(--bottom-height));
  351. position: absolute;
  352. width: var(--left-pano-width);
  353. }
  354. .animation-right {
  355. height: calc(100vh - var(--bottom-height));
  356. padding-top: 0;
  357. position: absolute;
  358. right: 0;
  359. top: 0;
  360. }
  361. </style>
  362. <style>
  363. .animation-left .left-pano {
  364. bottom: 0 !important;
  365. }
  366. </style>