preview.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. 'use strict';
  2. const animation = require('./animation');
  3. const events = require('./events');
  4. const eventEditor = require('./event-editor');
  5. exports.template = /* html */`
  6. <div class="multiple tips">
  7. <ui-label class="big" value="i18n:ENGINE.assets.fbx.modelPreview"></ui-label>
  8. <ui-label value="i18n:ENGINE.assets.multipleWarning"></ui-label>
  9. </div>
  10. <ui-drag-area class="preview" droppable="cc.Asset">
  11. <div class= "noModel tips">
  12. <ui-label class="big" value="i18n:ENGINE.assets.fbx.no_model_tips"></ui-label>
  13. <ui-label value="i18n:ENGINE.assets.fbx.drag_model_tips"></ui-label>
  14. </div>
  15. <div class="preview-container">
  16. <div class="animation-info">
  17. <div class="flex">
  18. <div class="toolbar" id="timeCtrl">
  19. <ui-icon value="rewind" name="jump_first_frame"></ui-icon>
  20. <ui-icon value="prev-play" name="jump_prev_frame"></ui-icon>
  21. <ui-icon id="playButtonIcon" value="play" name="play"></ui-icon>
  22. <ui-icon value="next-play" name="jump_next_frame"></ui-icon>
  23. <ui-icon value="forward" name="jump_last_frame"></ui-icon>
  24. <ui-icon value="stop" name="stop"></ui-icon>
  25. <ui-icon value="event" name="add_event"></ui-icon>
  26. </div>
  27. <div class="time flex toolbar f1">
  28. <ui-label value="Frame"></ui-label>
  29. <ui-num-input id="currentTime" step="1" preci="0"></ui-num-input>
  30. <div class="duration"></div>
  31. </div>
  32. </div>
  33. <div class="time-line">
  34. <ui-scale-plate tooltip="Frame" id="animationTime"></ui-scale-plate>
  35. <div class="events"></div>
  36. </div>
  37. </div>
  38. <div class="model-info">
  39. <ui-label value="Vertices:0" class="vertices"></ui-label>
  40. <ui-label value="Triangles:0" class="triangles"></ui-label>
  41. </div>
  42. <div class="event-editor">
  43. </div>
  44. <div class="image">
  45. <canvas class="canvas"></canvas>
  46. </div>
  47. </div>
  48. </ui-drag-area>
  49. `;
  50. exports.style = /* css*/`
  51. .tips {
  52. text-align: center;
  53. min-height: 200px;
  54. padding-top: 16px;
  55. border-top: 1px solid var(--color-normal-border);
  56. }
  57. .tips > ui-label {
  58. display: block;
  59. }
  60. .tips > ui-label.big {
  61. font-size: 14px;
  62. }
  63. .flex {
  64. display: flex;
  65. }
  66. .f1 {
  67. flex: 1;
  68. }
  69. .preview-container {
  70. min-height: 200px;
  71. }
  72. .preview-container > .animation-info {
  73. padding-right: 4px;
  74. }
  75. .preview[hoving] > .preview-container {
  76. outline: 2px solid var(--color-focus-fill-weaker);
  77. outline-offset: -1px;
  78. }
  79. .preview[hoving] > .tips {
  80. outline: 2px solid var(--color-focus-fill-weaker);
  81. outline-offset: -2px;
  82. }
  83. .preview-container > .model-info {
  84. display: none;
  85. padding: 2px 4px;
  86. }
  87. .preview-container > .model-info > ui-label {
  88. margin-right: 6px;
  89. }
  90. .preview-container > .image {
  91. height: var(--inspector-footer-preview-height, 200px);
  92. overflow: hidden;
  93. display: flex;
  94. flex: 1;
  95. }
  96. .preview-container >.image > .canvas {
  97. flex: 1;
  98. }
  99. .preview-container .toolbar {
  100. display: flex;
  101. margin-top: 4px;
  102. justify-content: space-between;
  103. }
  104. .preview-container .toolbar ui-num-input {
  105. display: flex;
  106. flex: 1;
  107. }
  108. .preview-container .toolbar > * {
  109. margin-left: 4px;
  110. }
  111. ui-icon {
  112. cursor: pointer;
  113. }
  114. .time-line {
  115. position: relative;
  116. padding: 4px;
  117. }
  118. .time-line .events {
  119. position: absolute;
  120. bottom: 2px;
  121. z-index: 1;
  122. box-sizing: border-box;
  123. padding: 0 8px;
  124. width: 100%;
  125. }
  126. .time-line ui-scale-plate {
  127. width: 100%;
  128. }
  129. .events ui-icon {
  130. position: absolute;
  131. bottom: 0;
  132. }
  133. .events ui-icon:hover {
  134. color: white;
  135. cursor: pointer;
  136. }
  137. .events ui-icon[active] {
  138. color: var(--color-focus-fill);
  139. }
  140. .mask {
  141. position: absolute;
  142. z-index: 2;
  143. width: 100%;
  144. height: 100vh;
  145. top: 0;
  146. right: 0;
  147. bottom: 0;
  148. left: 0;
  149. background: var(--color-normal-fill);
  150. opacity: 0.3;
  151. }
  152. .event-editor {
  153. background: var(--color-normal-fill);
  154. }
  155. #event-editor {
  156. position: relative;
  157. line-height: 20px;
  158. width: 100%;
  159. height: 70%;
  160. overflow: hidden;
  161. margin: 0 auto;
  162. background: #312f2f;
  163. display: flex;
  164. flex-direction: column;
  165. border: var(--color-normal-fill-emphasis) 1px solid;
  166. box-sizing: border-box;
  167. }
  168. #event-editor > header {
  169. display: flex;
  170. justify-content: space-between;
  171. height: 20px;
  172. background: var(--color-normal-fill-emphasis);
  173. padding: 2px 4px;
  174. }
  175. #event-editor > header .title {
  176. color: var(--color-focus-fill);
  177. }
  178. #event-editor > .header .name {
  179. margin: 0 4px;
  180. }
  181. #event-editor > .header .dirty {
  182. color: var(--color-focus-fill);
  183. margin: 0 2px;
  184. }
  185. #event-editor > .functions {
  186. overflow-y: auto;
  187. flex: 1;
  188. height: 100%;
  189. padding-top: 0;
  190. background: var(--color-normal-fill-normal);
  191. margin-bottom: 2px;
  192. }
  193. #event-editor > .functions .header,
  194. #event-editor > .functions .line {
  195. display: flex;
  196. justify-content: space-between;
  197. flex: 1;
  198. background: unset;
  199. margin: 4px 0;
  200. }
  201. #event-editor > .functions .line .name {
  202. width: 40px;
  203. text-align: center;
  204. }
  205. #event-editor > .functions .line ui-select {
  206. margin-right: 8px;
  207. }
  208. #event-editor > .functions .line .operate {
  209. visibility: hidden;
  210. }
  211. #event-editor > .functions .line:hover .operate {
  212. visibility: visible;
  213. }
  214. #event-editor > .functions .header ui-input {
  215. background-color: transparent;
  216. border-color: transparent;
  217. }
  218. #event-editor > .functions .header ui-input:hover {
  219. background-color: var(--color-normal-fill-emphasis);
  220. }
  221. #event-editor ui-input,
  222. #event-editor ui-checkbox,
  223. #event-editor ui-num-input {
  224. flex: 1;
  225. margin-left: 4px;
  226. }
  227. #event-editor ui-section {
  228. width: 100%;
  229. }
  230. #event-editor ui-icon:hover {
  231. cursor: pointer;
  232. color: #cccccc;
  233. }
  234. #event-editor .tools {
  235. display: flex;
  236. padding: 4px 0;
  237. }
  238. #event-editor .tools ui-icon {
  239. margin: 0 4px;
  240. }
  241. #event-editor .params {
  242. }
  243. #event-editor .header ui-icon,
  244. #event-editor .params ui-icon {
  245. margin: 0 5px;
  246. }
  247. #event-editor .empty {
  248. font-style: italic;
  249. text-align: center;
  250. }
  251. #event-editor .toast {
  252. position: absolute;
  253. top: 54px;
  254. right: 4px;
  255. z-index: 1;
  256. padding: 0 4px;
  257. background-color: var(--color-normal-fill-emphasis);
  258. color: var(--color-warn-fill);
  259. }
  260. `;
  261. exports.$ = {
  262. noModel: '.noModel',
  263. multiple: '.multiple',
  264. container: '.preview',
  265. previewContainer: '.preview-container',
  266. vertices: '.vertices',
  267. triangles: '.triangles',
  268. image: '.image',
  269. canvas: '.canvas',
  270. animationInfo: '.animation-info',
  271. modelInfo: '.model-info',
  272. playButtonIcon: '#playButtonIcon',
  273. animationTime: '#animationTime',
  274. currentTime: '#currentTime',
  275. events: ".events",
  276. duration: ".duration",
  277. eventEditor: ".event-editor",
  278. timeCtrl: "#timeCtrl",
  279. };
  280. const PLAY_STATE = {
  281. STOP: 0,
  282. PLAYING: 1,
  283. PAUSE: 2,
  284. };
  285. async function callModelPreviewFunction(funcName, ...args) {
  286. return await Editor.Message.request('scene', 'call-preview-function', 'scene:model-preview', funcName, ...args);
  287. }
  288. const Elements = {
  289. container: {
  290. ready() {
  291. const panel = this;
  292. function observer() {
  293. window.requestAnimationFrame(() => {
  294. panel.isPreviewDataDirty = true;
  295. panel.updateEventInfo();
  296. });
  297. }
  298. panel.resizeObserver = new window.ResizeObserver(observer);
  299. panel.resizeObserver.observe(panel.$.container);
  300. // Identify dragged FBX resources
  301. panel.$.container.addEventListener('drop', async (event) => {
  302. event.preventDefault();
  303. event.stopPropagation();
  304. // Multiple drag-and-drop options are not supported, and only the first value is taken
  305. const values = [];
  306. const { additional, value } = JSON.parse(JSON.stringify(Editor.UI.DragArea.currentDragInfo)) || {};
  307. if (additional) {
  308. additional.forEach((info) => {
  309. if (info.type === 'cc.Prefab') {
  310. values.push(info.value);
  311. }
  312. if (Array.isArray(info.extends)) {
  313. info.extends.forEach(v => {
  314. if (v === 'cc.Prefab') {
  315. values.push(info.value);
  316. }
  317. });
  318. }
  319. if (info.subAssets) {
  320. Object.values(info.subAssets).forEach(subAsset => {
  321. if (subAsset.type === 'cc.Prefab') {
  322. values.push(subAsset.value);
  323. }
  324. });
  325. }
  326. });
  327. }
  328. if (value && !values.includes(value)) {
  329. values.push(value);
  330. }
  331. if (!values.length) {
  332. return;
  333. }
  334. const info = await callModelPreviewFunction('setModel', values[0]);
  335. panel.infoUpdate(info);
  336. });
  337. },
  338. close() {
  339. const panel = this;
  340. panel.resizeObserver.unobserve(panel.$.container);
  341. },
  342. },
  343. preview: {
  344. ready() {
  345. const panel = this;
  346. let _isPreviewDataDirty = false;
  347. Object.defineProperty(panel, 'isPreviewDataDirty', {
  348. get() {
  349. return _isPreviewDataDirty;
  350. },
  351. set(value) {
  352. if (value !== _isPreviewDataDirty) {
  353. _isPreviewDataDirty = value;
  354. value && panel.refreshPreview();
  355. }
  356. },
  357. });
  358. panel.$.canvas.addEventListener('mousedown', async (event) => {
  359. await callModelPreviewFunction('onMouseDown', { x: event.x, y: event.y, button: event.button });
  360. async function mousemove(event) {
  361. await callModelPreviewFunction('onMouseMove', {
  362. movementX: event.movementX,
  363. movementY: event.movementY,
  364. });
  365. panel.isPreviewDataDirty = true;
  366. }
  367. async function mouseup(event) {
  368. await callModelPreviewFunction('onMouseUp', {
  369. x: event.x,
  370. y: event.y,
  371. });
  372. document.removeEventListener('mousemove', mousemove);
  373. document.removeEventListener('mouseup', mouseup);
  374. panel.isPreviewDataDirty = false;
  375. }
  376. document.addEventListener('mousemove', mousemove);
  377. document.addEventListener('mouseup', mouseup);
  378. panel.isPreviewDataDirty = true;
  379. });
  380. panel.$.canvas.addEventListener('wheel', async (event) => {
  381. await callModelPreviewFunction('onMouseWheel', {
  382. wheelDeltaY: event.wheelDeltaY,
  383. wheelDeltaX: event.wheelDeltaX,
  384. });
  385. panel.isPreviewDataDirty = true;
  386. });
  387. const GlPreview = Editor._Module.require('PreviewExtends').default;
  388. panel.glPreview = new GlPreview('scene:model-preview', 'query-model-preview-data');
  389. panel.isPreviewDataDirty = true;
  390. },
  391. async update() {
  392. const panel = this;
  393. if (!panel.$.canvas) {
  394. return;
  395. }
  396. await panel.glPreview.init({ width: panel.$.canvas.clientWidth, height: panel.$.canvas.clientHeight });
  397. const prefabAsset = Object.values(panel.asset.subAssets).find((asset) => asset.type === 'cc.Prefab');
  398. if (prefabAsset) {
  399. const info = await callModelPreviewFunction('setModel', prefabAsset.uuid);
  400. panel.infoUpdate(info);
  401. } else {
  402. this.updatePanelHidden(false);
  403. }
  404. panel.isPreviewDataDirty = true;
  405. },
  406. },
  407. modelInfo: {
  408. ready() {
  409. this.infoUpdate = Elements.modelInfo.update.bind(this);
  410. },
  411. update(info) {
  412. if (!info) {
  413. return;
  414. }
  415. this.$.vertices.value = `Vertices:${info.vertices}`;
  416. this.$.triangles.value = `Triangles:${info.polygons}`;
  417. this.isPreviewDataDirty = true;
  418. this.updatePanelHidden(true);
  419. },
  420. close() {
  421. callModelPreviewFunction('hide');
  422. },
  423. },
  424. animationTime: {
  425. ready() {
  426. const timeline = this.$.animationTime;
  427. timeline.addEventListener('change', this.onAnimationTimeChange.bind(this));
  428. timeline.addEventListener('transform', this.updateEventInfo.bind(this));
  429. },
  430. },
  431. currentTime: {
  432. ready() {
  433. const currentTime = this.$.currentTime;
  434. currentTime.addEventListener('confirm', this.onAnimationTimeChange.bind(this));
  435. },
  436. },
  437. timeCtrl: {
  438. ready() {
  439. this.$.timeCtrl.addEventListener('click', this.onTimeCtrlClick.bind(this));
  440. },
  441. },
  442. };
  443. exports.methods = {
  444. /**
  445. *
  446. * @param {boolean} hasModel
  447. */
  448. updatePanelHidden(hasModel) {
  449. this.$.noModel.hidden = hasModel;
  450. this.$.previewContainer.hidden = this.isMultiple || !hasModel;
  451. this.$.multiple.hidden = !this.isMultiple;
  452. },
  453. async apply() {
  454. // save animation event info
  455. await this.events.apply.call(this);
  456. },
  457. async refreshPreview() {
  458. const panel = this;
  459. // After await, the panel no longer exists
  460. if (!panel.$.canvas) {
  461. return;
  462. }
  463. const doDraw = async () => {
  464. try {
  465. const canvas = panel.$.canvas;
  466. const image = panel.$.image;
  467. const width = image.clientWidth;
  468. const height = image.clientHeight;
  469. if (canvas.width !== width || canvas.height !== height) {
  470. canvas.width = width;
  471. canvas.height = height;
  472. await panel.glPreview.initGL(canvas, { width, height });
  473. await panel.glPreview.resizeGL(width, height);
  474. }
  475. const info = await panel.glPreview.queryPreviewData({
  476. width: canvas.width,
  477. height: canvas.height,
  478. });
  479. panel.glPreview.drawGL(info);
  480. } catch (e) {
  481. console.warn(e);
  482. }
  483. };
  484. if (panel.isPreviewDataDirty || this.curPlayState === PLAY_STATE.PLAYING) {
  485. requestAnimationFrame(async () => {
  486. await doDraw();
  487. panel.isPreviewDataDirty = false;
  488. });
  489. }
  490. },
  491. async onTabChanged(activeTab) {
  492. if (typeof activeTab === 'string') {
  493. this.activeTab = activeTab;
  494. const isAnimationTab = this.activeTab === 'animation';
  495. this.$.animationInfo.style.display = isAnimationTab ? 'block' : 'none';
  496. if (!isAnimationTab) {
  497. this.eventEditorVm.show = false;
  498. }
  499. this.$.modelInfo.style.display = this.activeTab === 'model' ? 'block' : 'none';
  500. await this.stopAnimation();
  501. }
  502. },
  503. onTimeCtrlClick(event) {
  504. const name = event.target.getAttribute('name');
  505. if (!name || !this.curEditClipInfo) {
  506. return;
  507. }
  508. switch (name) {
  509. case 'play':
  510. this.onPlayButtonClick();
  511. break;
  512. case 'stop':
  513. this.stopAnimation();
  514. break;
  515. case 'jump_first_frame':
  516. this.setCurrentFrame(0);
  517. break;
  518. case 'jump_next_frame':
  519. this.setCurrentFrame(this.$.currentTime.value + 1);
  520. break;
  521. case 'jump_prev_frame':
  522. this.setCurrentFrame(this.$.currentTime.value - 1);
  523. break;
  524. case 'jump_last_frame':
  525. this.setCurrentFrame(this.curTotalFrames);
  526. break;
  527. case 'add_event':
  528. if (this.checkDisabledEditEvent()) {
  529. return;
  530. }
  531. this.addEventToCurTime();
  532. break;
  533. }
  534. },
  535. checkDisabledEditEvent() {
  536. if (!this.curEditClipInfo.userData) {
  537. Editor.Dialog.info(Editor.I18n.t('ENGINE.assets.fbx.addEvent.shouldSave'), {
  538. buttons: [Editor.I18n.t('ENGINE.assets.fbx.addEvent.ok')],
  539. });
  540. return true;
  541. }
  542. return false;
  543. },
  544. addEventToCurTime() {
  545. this.events.addEvent.call(this, this.$.animationTime.value / this.curEditClipInfo.fps);
  546. },
  547. updateEventInfo() {
  548. let eventInfos = [];
  549. if (this.curEditClipInfo && this.curEditClipInfo.userData) {
  550. const events = this.curEditClipInfo.userData.events;
  551. if (Array.isArray(events)) {
  552. eventInfos = events.map((info) => {
  553. return {
  554. info,
  555. x: this.$.animationTime.valueToPixel(info.frame * this.curEditClipInfo.fps),
  556. };
  557. });
  558. }
  559. }
  560. this.events.update.call(this, eventInfos);
  561. },
  562. async stopAnimation() {
  563. if (!this.curEditClipInfo) {
  564. return;
  565. }
  566. await callModelPreviewFunction('stop');
  567. },
  568. async onPlayButtonClick() {
  569. if (!this.curEditClipInfo) {
  570. return;
  571. }
  572. switch (this.curPlayState) {
  573. case PLAY_STATE.PAUSE:
  574. await callModelPreviewFunction('resume');
  575. break;
  576. case PLAY_STATE.PLAYING:
  577. await callModelPreviewFunction('pause');
  578. break;
  579. case PLAY_STATE.STOP:
  580. await callModelPreviewFunction('play', this.curEditClipInfo.rawClipUUID);
  581. break;
  582. default:
  583. break;
  584. }
  585. this.isPreviewDataDirty = true;
  586. },
  587. async onAnimationTimeChange(event) {
  588. event.stopPropagation();
  589. if (!this.curEditClipInfo) {
  590. return;
  591. }
  592. const frame = event.target.value;
  593. await this.setCurrentFrame(frame);
  594. this.isPreviewDataDirty = true;
  595. },
  596. async setCurrentFrame(frame) {
  597. frame = Editor.Utils.Math.clamp(frame, 0, this.curTotalFrames);
  598. const curTime = frame / this.curEditClipInfo.fps + this.curEditClipInfo.from;
  599. await callModelPreviewFunction('setCurEditTime', curTime);
  600. },
  601. onModelAnimationUpdate(time) {
  602. if (!this.curEditClipInfo) {
  603. return;
  604. }
  605. if (this.$.animationTime) {
  606. let timeFromRangeStart = Math.max(time - this.curEditClipInfo.from, 0);
  607. this.$.animationTime.value = Math.round(timeFromRangeStart * this.curEditClipInfo.fps);
  608. this.$.currentTime.value = this.$.animationTime.value;
  609. }
  610. this.isPreviewDataDirty = true;
  611. },
  612. setCurPlayState(state) {
  613. this.curPlayState = state;
  614. let buttonIconName = '';
  615. switch (state) {
  616. case PLAY_STATE.STOP:
  617. buttonIconName = 'play';
  618. break;
  619. case PLAY_STATE.PLAYING:
  620. buttonIconName = 'pause';
  621. break;
  622. case PLAY_STATE.PAUSE:
  623. buttonIconName = 'play';
  624. break;
  625. default:
  626. break;
  627. }
  628. if (this.$.playButtonIcon) {
  629. this.$.playButtonIcon.value = buttonIconName;
  630. }
  631. this.isPreviewDataDirty = true;
  632. },
  633. async setCurEditClipInfo(clipInfo) {
  634. this.curEditClipInfo = clipInfo;
  635. this.curTotalFrames = 0;
  636. if (clipInfo) {
  637. this.curTotalFrames = Math.round(clipInfo.duration * clipInfo.fps);
  638. // update animation events, clipInfo.clipUUID may be undefined
  639. if (clipInfo.clipUUID) {
  640. const subId = clipInfo.clipUUID.match(/@(.*)/)[1];
  641. this.curEditClipInfo.userData = this.meta.subMetas[subId] && this.meta.subMetas[subId].userData || {};
  642. }
  643. await callModelPreviewFunction(
  644. 'setPlaybackRange',
  645. clipInfo.from,
  646. clipInfo.to,
  647. );
  648. await callModelPreviewFunction(
  649. 'setClipConfig',
  650. {
  651. wrapMode: clipInfo.wrapMode,
  652. speed: clipInfo.speed,
  653. }
  654. );
  655. await this.stopAnimation();
  656. }
  657. this.$.animationTime.setConfig({
  658. max: this.curTotalFrames,
  659. });
  660. this.$.duration.innerHTML = `Totals: ${this.curTotalFrames}`;
  661. this.updateEventInfo();
  662. },
  663. onAnimationPlayStateChanged(state) {
  664. this.setCurPlayState(state);
  665. },
  666. addAssetChangeListener(add = true) {
  667. if (!add && this.hasListenAssetsChange) {
  668. Editor.Message.__protected__.removeBroadcastListener('asset-db:asset-change', this.onAssetChangeBind);
  669. this.hasListenAssetsChange = false;
  670. return;
  671. }
  672. Editor.Message.__protected__.addBroadcastListener('asset-db:asset-change', this.onAssetChangeBind);
  673. this.hasListenAssetsChange = true;
  674. },
  675. async onAssetChange(uuid) {
  676. if (this.asset.uuid === uuid) {
  677. // Update the animation dump when the parent assets changes
  678. this.meta = await Editor.Message.request('asset-db', 'query-asset-meta', this.asset.uuid);
  679. const clipInfo = animation.methods.getCurClipInfo.call(this);
  680. await this.onEditClipInfoChanged(clipInfo);
  681. }
  682. },
  683. };
  684. exports.ready = function() {
  685. this.gridWidth = 0;
  686. this.gridTableWith = 0;
  687. this.activeTab = 'animation';
  688. this.isPreviewDataDirty = true;
  689. this.curEditClipInfo = null;
  690. this.curPlayState = PLAY_STATE.STOP;
  691. this.curTotalFrames = 0;
  692. this.onTabChangedBind = this.onTabChanged.bind(this);
  693. this.onModelAnimationUpdateBind = this.onModelAnimationUpdate.bind(this);
  694. this.onAnimationPlayStateChangedBind = this.onAnimationPlayStateChanged.bind(this);
  695. this.onEditClipInfoChanged = async (clipInfo) => {
  696. if (clipInfo) {
  697. await callModelPreviewFunction('setEditClip', clipInfo.rawClipUUID, clipInfo.rawClipIndex);
  698. await this.setCurEditClipInfo(clipInfo);
  699. }
  700. };
  701. Editor.Message.addBroadcastListener('scene:model-preview-animation-time-change', this.onModelAnimationUpdateBind);
  702. Editor.Message.addBroadcastListener('scene:model-preview-animation-state-change', this.onAnimationPlayStateChangedBind);
  703. Editor.Message.addBroadcastListener('fbx-inspector:change-tab', this.onTabChangedBind);
  704. Editor.Message.addBroadcastListener('fbx-inspector:animation-change', this.onEditClipInfoChanged);
  705. for (const prop in Elements) {
  706. const element = Elements[prop];
  707. if (element.ready) {
  708. element.ready.call(this);
  709. }
  710. }
  711. this.onAssetChangeBind = this.onAssetChange.bind(this);
  712. this.addAssetChangeListener(true);
  713. this.events = events;
  714. this.events.ready.call(this);
  715. this.eventEditor = eventEditor;
  716. this.events.eventsMap = {};
  717. this.eventEditor.ready.call(this);
  718. };
  719. exports.update = async function(assetList, metaList) {
  720. this.assetList = assetList;
  721. this.metaList = metaList;
  722. this.isMultiple = this.assetList.length > 1;
  723. this.$.previewContainer.hidden = this.isMultiple;
  724. this.$.multiple.hidden = !this.isMultiple;
  725. this.$.noModel.hidden = true;
  726. if (this.isMultiple) {
  727. return;
  728. }
  729. this.asset = assetList[0];
  730. this.meta = metaList[0];
  731. for (const prop in Elements) {
  732. const element = Elements[prop];
  733. if (element.update) {
  734. element.update.call(this);
  735. }
  736. }
  737. animation.methods.initAnimationNameToUUIDMap.call(this);
  738. animation.methods.initAnimationInfos.call(this);
  739. if (this.animationInfos) {
  740. this.rawClipIndex = 0;
  741. this.splitClipIndex = 0;
  742. const clipInfo = animation.methods.getCurClipInfo.call(this);
  743. await this.onEditClipInfoChanged(clipInfo);
  744. } else {
  745. await this.setCurEditClipInfo();
  746. }
  747. this.eventEditorVm.show = false;
  748. this.setCurPlayState(PLAY_STATE.STOP);
  749. this.isPreviewDataDirty = true;
  750. };
  751. exports.close = function() {
  752. for (const prop in Elements) {
  753. const element = Elements[prop];
  754. if (element.close) {
  755. element.close.call(this);
  756. }
  757. }
  758. Editor.Message.removeBroadcastListener('scene:model-preview-animation-time-change', this.onModelAnimationUpdateBind);
  759. Editor.Message.removeBroadcastListener('scene:model-preview-animation-state-change', this.onAnimationPlayStateChangedBind);
  760. Editor.Message.removeBroadcastListener('fbx-inspector:change-tab', this.onTabChangedBind);
  761. Editor.Message.removeBroadcastListener('fbx-inspector:animation-change', this.onEditClipInfoChanged);
  762. this.addAssetChangeListener(false);
  763. };