spine-data.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. const { readJSONSync } = require('fs-extra');
  3. exports.template = /* html */`
  4. <section class="spine-data">
  5. <ui-section class="atlas-section" expand>
  6. <ui-prop slot="header">
  7. <ui-label slot="label" value="i18n:ENGINE.assets.spine_data.atlas"></ui-label>
  8. <ui-asset slot="content" class="atlas" droppable="cc.Asset" @confirm.stop></ui-asset>
  9. </ui-prop>
  10. <div class="textures"></div>
  11. </ui-section>
  12. </section>`;
  13. exports.$ = {
  14. container: '.spine-data',
  15. atlasSection: '.atlas-section',
  16. atlas: '.atlas',
  17. textures: '.textures',
  18. };
  19. exports.style = `
  20. .spine-data {
  21. padding-right: 4px;
  22. }
  23. .spine-data > .atlas-section [slot="header"] {
  24. width: 100%;
  25. display: block;
  26. }
  27. .spine-data > .atlas-section[whole] {
  28. padding-left: 20px;
  29. }
  30. .spine-data > .atlas-section > .textures > {}
  31. .spine-data > .atlas-section > .textures > .texture-item {
  32. margin-left: 4px;
  33. }
  34. `;
  35. exports.ready = function() {
  36. const panel = this;
  37. function setAtlas(value) {
  38. panel.metaList.forEach((meta) => {
  39. meta.userData.atlasUuid = value;
  40. });
  41. panel.dispatch('change');
  42. panel.dispatch('snapshot');
  43. }
  44. panel.$.atlas.addEventListener('confirm', async (event) => {
  45. const atlas = event.target.value;
  46. if (!atlas) {
  47. setAtlas(atlas);
  48. return;
  49. }
  50. const assetInfo = await Editor.Message.request('asset-db', 'query-asset-info', atlas);
  51. if (!assetInfo) {
  52. panel.$.atlas.setAttribute('value', '');
  53. return;
  54. }
  55. if (!assetInfo.source.endsWith('.atlas')) {
  56. await Editor.Dialog.warn(Editor.I18n.t('ENGINE.assets.spine_data.atlas_warn'), {
  57. buttons: [
  58. Editor.I18n.t('ENGINE.dialog.confirm'),
  59. ],
  60. });
  61. panel.$.atlas.setAttribute('value', '');
  62. return;
  63. }
  64. setAtlas(atlas);
  65. });
  66. };
  67. exports.update = function(assetList, metaList) {
  68. const panel = this;
  69. panel.assetList = assetList;
  70. panel.metaList = metaList;
  71. panel.asset = assetList[0];
  72. panel.meta = metaList[0];
  73. while (panel.$.textures.firstChild) {
  74. panel.$.textures.removeChild(panel.$.textures.firstChild);
  75. }
  76. // 先隐藏箭头,等有数值才显示
  77. panel.$.atlasSection.setAttribute('whole', '');
  78. const libraryJSON = panel.asset.library['.json'];
  79. if (libraryJSON) {
  80. const spData = readJSONSync(libraryJSON);
  81. if (spData && spData.textures && spData.textures.length > 0) {
  82. createTextureSection(spData.textures, panel.$.textures);
  83. }
  84. }
  85. // 只允许显示 .atlas 的资源
  86. panel.$.atlas.setAttribute('filter', JSON.stringify({
  87. pattern: 'db://**/*.atlas',
  88. }));
  89. panel.$.atlas.setAttribute('value', panel.meta.userData.atlasUuid);
  90. };
  91. function createTextureSection(textures, parentElement) {
  92. for (let i = 0; i < textures.length; i++) {
  93. const texture = textures[i];
  94. let item = parentElement.querySelector('.texture-item');
  95. let uiAsset = item && item.querySelector('.ui-asset');
  96. if (!uiAsset) {
  97. item = document.createElement('ui-prop');
  98. item.classList.add('texture-item');
  99. item.setAttribute('readonly', '');
  100. parentElement.appendChild(item);
  101. let type = texture.__expectedType__.split('cc.');
  102. type = type.length > 0 ? type[1] : type[0];
  103. const label = document.createElement('ui-label');
  104. label.setAttribute('slot', 'label');
  105. label.setAttribute('value', i);
  106. item.appendChild(label);
  107. uiAsset = document.createElement('ui-asset');
  108. uiAsset.setAttribute('slot', 'content');
  109. item.appendChild(uiAsset);
  110. }
  111. uiAsset.setAttribute('readonly', '');
  112. uiAsset.setAttribute('value', texture.__uuid__);
  113. uiAsset.setAttribute('droppable', texture.__expectedType__);
  114. }
  115. }