animation-mask.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. 'use strict';
  2. const { updateElementReadonly } = require('../utils/assets');
  3. exports.template = /* html */`
  4. <section class="asset-animation-mask">
  5. <div class="header">
  6. <ui-button class="import">
  7. <ui-icon value="import"></ui-icon>
  8. <ui-label value="i18n:ENGINE.assets.animationMask.importSkeleton"></ui-label>
  9. </ui-button>
  10. <ui-button class="clear">
  11. <ui-icon value="del" color="true"></ui-icon>
  12. <ui-label value="i18n:ENGINE.assets.animationMask.clearAllNodes"></ui-label>
  13. </ui-button>
  14. </div>
  15. <div class="content">
  16. <ui-tree class="tree"></ui-tree>
  17. </div>
  18. <ui-label class="multiple-warn-tip" value="i18n:ENGINE.assets.multipleWarning"></ui-label>
  19. </section>
  20. `;
  21. exports.style = /* css */`
  22. .asset-animation-mask {
  23. display: flex;
  24. height: 100%;
  25. flex-direction: column;
  26. }
  27. .asset-animation-mask[multiple-invalid] > *:not(.multiple-warn-tip) {
  28. display: none!important;
  29. }
  30. .asset-animation-mask[multiple-invalid] > .multiple-warn-tip {
  31. display: block;
  32. }
  33. .asset-animation-mask .multiple-warn-tip {
  34. display: none;
  35. text-align: center;
  36. color: var(--color-focus-contrast-weakest);
  37. margin-top: 8px;
  38. }
  39. .asset-animation-mask > .header {
  40. margin-top: 4px;
  41. margin-right: 4px;
  42. display: flex;
  43. }
  44. .asset-animation-mask > .header ui-button {
  45. text-align: center;
  46. }
  47. .asset-animation-mask > .header .import {
  48. flex: 1;
  49. margin-left: 10px;
  50. }
  51. .asset-animation-mask > .header .clear {
  52. margin-left: 10px;
  53. }
  54. .asset-animation-mask > .content {
  55. flex: 1;
  56. margin-top: 10px;
  57. }
  58. .asset-animation-mask > .content .tree {
  59. height: 100%;
  60. }
  61. `;
  62. exports.$ = {
  63. container: '.asset-animation-mask',
  64. import: '.import',
  65. clear: '.clear',
  66. tree: '.tree',
  67. };
  68. exports.methods = {
  69. record() {
  70. return JSON.stringify(this.queryData);
  71. },
  72. async restore(record) {
  73. record = JSON.parse(record);
  74. if (!record || typeof record !== 'object') {
  75. return false;
  76. }
  77. await this.change(record);
  78. return true;
  79. },
  80. async query(uuid) {
  81. return await Editor.Message.request('scene', 'query-animation-mask', uuid);
  82. },
  83. async apply() {
  84. this.reset();
  85. await Editor.Message.request('scene', 'apply-animation-mask', this.asset.uuid);
  86. },
  87. abort() {
  88. this.reset();
  89. },
  90. reset() {
  91. this.dirtyData.uuid = '';
  92. },
  93. async clear() {
  94. this.queryData = await Editor.Message.request('scene', 'change-animation-mask', {
  95. method: 'clear-nodes',
  96. uuid: this.asset.uuid,
  97. });
  98. this.changed();
  99. },
  100. async import(uuid) {
  101. this.queryData = await Editor.Message.request('scene', 'change-animation-mask', {
  102. method: 'import-skeleton',
  103. uuid: uuid,
  104. });
  105. this.changed();
  106. },
  107. async change(dump) {
  108. this.queryData = await Editor.Message.request('scene', 'change-animation-mask', {
  109. method: 'change-dump',
  110. dump,
  111. });
  112. this.changed();
  113. },
  114. changed() {
  115. this.updateInterface();
  116. this.setDirtyData();
  117. this.dispatch('change');
  118. /**
  119. * 由于编辑项中不需要区分 change 和 confirm 的情况,
  120. * 所以可以在 change 后 snapshot
  121. */
  122. this.dispatch('snapshot');
  123. },
  124. updateInterface() {
  125. const convertData = this.convertData(this.queryData.joints.value);
  126. this.flatData = convertData.flatData;
  127. this.$.tree.tree = convertData.treeData;
  128. updateElementReadonly.call(this, this.$.import);
  129. updateElementReadonly.call(this, this.$.clear);
  130. updateElementReadonly.call(this, this.$.tree);
  131. },
  132. setDirtyData() {
  133. this.dirtyData.realtime = JSON.stringify(this.queryData);
  134. if (!this.dirtyData.origin) {
  135. this.dirtyData.origin = this.dirtyData.realtime;
  136. }
  137. },
  138. isDirty() {
  139. const isDirty = this.dirtyData.origin !== this.dirtyData.realtime;
  140. return isDirty;
  141. },
  142. convertData(jointArr) {
  143. const collator = new Intl.Collator('en', {
  144. numeric: true,
  145. sensitivity: 'base',
  146. });
  147. jointArr.sort((a, b) => {
  148. return collator.compare(a.value.path.value, b.value.path.value);
  149. });
  150. const flatData = {};
  151. for (const joint of jointArr) {
  152. const key = joint.value.path.value;
  153. if (!key) {
  154. continue;
  155. }
  156. let childKey = key;
  157. let parentKey = childKey.substr(0, childKey.lastIndexOf('/'));
  158. while (parentKey) {
  159. if (!flatData[parentKey]) {
  160. flatData[parentKey] = { children: [childKey] };
  161. } else {
  162. if (!flatData[parentKey].children.includes(childKey)) {
  163. flatData[parentKey].children.push(childKey);
  164. }
  165. }
  166. childKey = parentKey;
  167. parentKey = childKey.substr(0, childKey.lastIndexOf('/'));
  168. }
  169. if (!flatData[key]) {
  170. flatData[key] = { origin: joint, children: [] };
  171. } else {
  172. flatData[key].origin = joint;
  173. }
  174. }
  175. const treeData = [];
  176. const keys = Object.keys(flatData);
  177. keys.sort((a, b) => {
  178. return collator.compare(a, b);
  179. });
  180. const roots = [];
  181. for (const key of keys) {
  182. // 一级根路径
  183. if (key.indexOf('/') === -1) {
  184. roots.push(key);
  185. }
  186. }
  187. treeDataFromFlatData(treeData, roots);
  188. function treeDataFromFlatData(treeArr, keys) {
  189. if (!Array.isArray(keys)) {
  190. return;
  191. }
  192. keys.forEach((key) => {
  193. const { origin, children } = flatData[key];
  194. const treeNode = {
  195. detail: {
  196. key,
  197. name: key.substr(key.lastIndexOf('/') + 1),
  198. disabled: true,
  199. },
  200. children: [],
  201. };
  202. if (origin) {
  203. treeNode.detail.disabled = false;
  204. }
  205. treeArr.push(treeNode);
  206. treeDataFromFlatData(treeNode.children, children);
  207. });
  208. }
  209. return { flatData, treeData };
  210. },
  211. jointEnableChange(key, checked, loop) {
  212. const { children, origin } = this.flatData[key];
  213. origin.value.enabled.value = checked;
  214. if (loop) {
  215. children.forEach((childKey) => {
  216. this.jointEnableChange(childKey, checked, loop);
  217. });
  218. }
  219. },
  220. };
  221. exports.ready = function() {
  222. const panel = this;
  223. panel.$.import.addEventListener('change', (event) => {
  224. Editor.Panel.__protected__.openKit('ui-kit.searcher', {
  225. elem: event.target,
  226. params: [
  227. {
  228. type: 'asset',
  229. assetFilter: {
  230. importer: ['gltf-scene'],
  231. },
  232. },
  233. ],
  234. listeners: {
  235. async confirm(detail) {
  236. if (!detail) { return; }
  237. await panel.import(detail.value);
  238. },
  239. },
  240. });
  241. });
  242. panel.$.clear.addEventListener('change', async () => {
  243. const result = await Editor.Dialog.warn(Editor.I18n.t('ENGINE.assets.animationMask.clearAllNodesWarn'), {
  244. title: Editor.I18n.t('ENGINE.assets.animationMask.clearAllNodes'),
  245. buttons: [Editor.I18n.t('ENGINE.dialog.confirm'), Editor.I18n.t('ENGINE.dialog.cancel')],
  246. default: 0,
  247. cancel: 1,
  248. });
  249. if (result.response === 0) {
  250. await this.clear();
  251. }
  252. });
  253. // Used to determine whether the material has been modified in isDirty()
  254. panel.dirtyData = {
  255. uuid: '',
  256. origin: '',
  257. realtime: '',
  258. };
  259. panel.$.tree.setTemplate('left', '<ui-checkbox tooltip="i18n:ENGINE.assets.animationMask.nodeEnableTip"></ui-checkbox>');
  260. panel.$.tree.setTemplateInit('left', ($left) => {
  261. $left.$checkbox = $left.querySelector('ui-checkbox');
  262. $left.$checkbox.addEventListener('click', async (event) => {
  263. const key = $left.data.detail.key;
  264. const origin = panel.flatData[key].origin;
  265. panel.jointEnableChange(key, !origin.value.enabled.value, !event.altKey);
  266. await panel.change(this.queryData);
  267. });
  268. });
  269. panel.$.tree.setRender('left', ($left) => {
  270. const { key, disabled } = $left.data.detail;
  271. if (disabled) {
  272. $left.$checkbox.setAttribute('disabled', true);
  273. $left.$checkbox.value = true;
  274. } else {
  275. $left.$checkbox.removeAttribute('disabled');
  276. }
  277. const origin = panel.flatData[key].origin;
  278. if (origin) {
  279. $left.$checkbox.value = origin.value.enabled.value;
  280. }
  281. });
  282. panel.$.tree.setTemplate('text', `<span class="name"></span>`);
  283. panel.$.tree.setTemplateInit('text', ($text) => {
  284. $text.$name = $text.querySelector('.name');
  285. });
  286. panel.$.tree.setRender('text', ($text, data) => {
  287. $text.$name.innerHTML = data.detail.name || data.detail.root;
  288. });
  289. };
  290. exports.update = async function(assetList, metaList) {
  291. this.assetList = assetList;
  292. this.metaList = metaList;
  293. this.asset = assetList[0];
  294. this.meta = metaList[0];
  295. if (assetList.length > 1) {
  296. this.$.container.setAttribute('multiple-invalid', '');
  297. return;
  298. } else {
  299. this.$.container.removeAttribute('multiple-invalid');
  300. }
  301. if (this.dirtyData.uuid !== this.asset.uuid) {
  302. this.dirtyData.uuid = this.asset.uuid;
  303. this.dirtyData.origin = '';
  304. }
  305. this.queryData = await this.query(this.asset.uuid);
  306. this.updateInterface();
  307. this.setDirtyData();
  308. };
  309. exports.close = function() {
  310. // Used to determine whether the material has been modified in isDirty()
  311. this.dirtyData = {
  312. uuid: '',
  313. origin: '',
  314. realtime: '',
  315. };
  316. };