diff --git a/editor/inspector/components/class.js b/editor/inspector/components/class.js index 2de470258f..d1d1694c81 100644 --- a/editor/inspector/components/class.js +++ b/editor/inspector/components/class.js @@ -35,8 +35,7 @@ exports.methods = { $group.querySelectorAll('.tab-content').forEach((child) => { if (child.getAttribute('name') === tabName) { child.style.display = 'block'; - } - else { + } else { child.style.display = 'none'; } }); @@ -58,8 +57,10 @@ exports.methods = { $content.setAttribute('class', 'tab-content'); $content.setAttribute('name', tabName); $group.appendChild($content); + const $label = document.createElement('ui-label'); - $label.value = tabName; + $label.value = this.getName(tabName); + const $button = document.createElement('ui-button'); $button.setAttribute('name', tabName); $button.appendChild($label); @@ -74,11 +75,19 @@ exports.methods = { }); if (child) { child.before(newChild); - } - else { + } else { parent.appendChild(newChild); } }, + getName(name) { + name = name.trim().replace(/^\S/, (str) => str.toUpperCase()); + name = name.replace(/_/g, ' '); + name = name.replace(/ \S/g, (str) => ` ${str.toUpperCase()}`); + // 驼峰转中间空格 + name = name.replace(/([a-z])([A-Z])/g, '$1 $2'); + + return name.trim(); + }, }; /** * 自动渲染组件的方法 @@ -123,12 +132,10 @@ async function update(dump) { } } $panel.appendChildByDisplayOrder($panel.$groups[key].tabs[name], $prop, info.displayOrder); - } - else { + } else { $panel.appendChildByDisplayOrder($section, $prop, info.displayOrder); } - } - else if (!$prop.isConnected || !$prop.parentElement) { + } else if (!$prop.isConnected || !$prop.parentElement) { $panel.appendChildByDisplayOrder($section, $prop, info.displayOrder); } $prop.render(info); diff --git a/editor/inspector/contributions/asset.css b/editor/inspector/contributions/asset.css index cba3abb583..8858151e43 100644 --- a/editor/inspector/contributions/asset.css +++ b/editor/inspector/contributions/asset.css @@ -3,6 +3,7 @@ flex-direction: column; overflow: hidden; } + :host > .container { display: flex; flex: 1; @@ -13,24 +14,29 @@ :host > .container[whole] { max-height: 100%; } + :host > .container > .header { display: flex; flex: 1; } + :host > .container > .header > .icon { width: 20px; font-size: 14px; cursor: pointer; } + :host > .container > .header > .image { width: 20px; height: 24px; cursor: pointer; } + :host > .container > .header > .icon:hover, :host > .container > .header > .image:hover { opacity: 0.8; } + :host > .container > .header > .name { flex: 1; padding-left: 5px; @@ -38,6 +44,12 @@ overflow: hidden; white-space: nowrap; } + +:host > .container > .header > .help { + display: none; + margin-right: 10px; +} + :host > .container > .header > .lock { display: none; font-size: 12px; @@ -46,6 +58,7 @@ margin-right: 12px; cursor: auto; } + :host > .container > .header > ui-button { display: none; padding: 0 6px; @@ -53,28 +66,34 @@ margin-left: 6px; margin-right: 6px; } + :host > .container > .header[dirty] > ui-button { display: inline-block; } + :host > .container > .content { display: flex; padding-top: 5px; flex: 1; flex-direction: column; } + :host > .container > .content > section { display: flex; flex-direction: column; overflow: hidden; } + :host > .container > .content > .content-section { flex: 1; overflow: auto; } + :host > .container > .content > section > ui-panel { display: flex; flex-direction: column; } + :host > .container > .content > section > ui-panel:only-child { flex: 1; } @@ -95,7 +114,3 @@ top: 0; z-index: 1; } - -.document { - margin-right: 10px; -} diff --git a/editor/inspector/contributions/asset.js b/editor/inspector/contributions/asset.js index 058982cc3c..cd30cc624f 100644 --- a/editor/inspector/contributions/asset.js +++ b/editor/inspector/contributions/asset.js @@ -1,16 +1,20 @@ 'use strict'; -const fs = require("fs"); -const path = require("path"); +const fs = require('fs'); +const path = require('path'); + const showImage = ['cc.ImageAsset', 'cc.SpriteFrame', 'cc.Texture2D']; + exports.listeners = {}; + exports.style = fs.readFileSync(path.join(__dirname, './asset.css'), 'utf8'); + exports.template = `
-
`; + $section.dump = component; $section.__panels__ = []; $section.__type__ = component.type; + const $active = $section.querySelector('ui-checkbox'); $active.value = component.value.enabled.value; $active.dump = component.value.enabled; $active.addEventListener('change', (event) => { event.stopPropagation(); + const value = !!$active.value; const dump = $active.dump; + dump.value = value; + if ('values' in dump) { dump.values.forEach((val, index) => { dump.values[index] = value; @@ -666,6 +746,7 @@ const Elements = { } $active.dispatch('change-dump'); }); + const $link = $section.querySelector('.link'); const url = panel.getHelpUrl(component.editor); if (url) { @@ -674,15 +755,19 @@ const Elements = { event.stopPropagation(); }); } + const $menu = $section.querySelector('.menu'); $menu.addEventListener('click', (event) => { event.stopPropagation(); exports.methods.componentContextMenu(panel.uuidList, $section.dump, componentList.length, i, panel.dumps); }); + sectionBody.__sections__[i] = $section; sectionBody.appendChild($section); + // 再处理内部 let renderList = panel.renderMap.section[$section.__type__]; + // 如果都没有渲染模板,使用默认 cc.Class 模板 if (!renderList || !renderList.length) { // 判断继承 @@ -694,6 +779,7 @@ const Elements = { renderList = panel.renderMap.section['cc.Class']; } } + renderList.forEach((file) => { const $panel = document.createElement('ui-panel'); $panel.setAttribute('src', file); @@ -717,6 +803,7 @@ const Elements = { $panel.shadowRoot.addEventListener('reset-dump', (event) => { exports.listeners['reset-dump'].call(panel, event); }); + $section.appendChild($panel); $section.__panels__.push($panel); $panel.dump = component; @@ -724,9 +811,11 @@ const Elements = { }); }); } + // 自定义 node 数据 if (panel.renderMap.section && panel.renderMap.section['cc.Node']) { - const array = panel.$.nodeSection.__node_panels__ = panel.$.nodeSection.__node_panels__ || []; + const array = (panel.$.nodeSection.__node_panels__ = panel.$.nodeSection.__node_panels__ || []); + panel.renderMap.section['cc.Node'].forEach((file, index) => { if (!array[index]) { array[index] = document.createElement('ui-panel'); @@ -735,12 +824,13 @@ const Elements = { array[index].setAttribute('src', file); array[index].update(panel.dump); }); + for (let i = panel.renderMap.section['cc.Node'].length; i < array.length; i++) { array[i].remove(); } + array.length = panel.renderMap.section['cc.Node'].length; - } - else if (panel.$.nodeSection.__node_panels__) { + } else if (panel.$.nodeSection.__node_panels__) { panel.$.nodeSection.__node_panels__.forEach((dom) => { dom.remove(); }); @@ -751,20 +841,25 @@ const Elements = { missingComponent: { ready() { const panel = this; + const sectionMissing = panel.$.sectionMissing; sectionMissing.addEventListener('click', (event) => { if (event.target.tagName !== 'UI-ICON') { return; } + if (!Array.isArray(panel.dump.removedComponents)) { return; } + const i = event.target.getAttribute('index'); const type = event.target.getAttribute('value'); + const info = panel.dump.removedComponents[i]; if (!info) { return; } + const uuidList = panel.uuidList; switch (type) { case 'save-o': { @@ -780,15 +875,20 @@ const Elements = { }, update() { const panel = this; + if (!panel.dump || panel.dump.isScene) { return; } + const uuidList = panel.uuidList; + const sectionMissing = panel.$.sectionMissing; sectionMissing.__sections__ = sectionMissing.__sections__ || []; + if (!panel.dump.removedComponents || uuidList.length !== 1) { panel.dump.removedComponents = []; } + for (let i = 0; i < panel.dump.removedComponents.length; i++) { let $section = sectionMissing.__sections__[i]; if (!$section) { @@ -798,10 +898,11 @@ const Elements = { } $section.innerHTML = ` ${panel.dump.removedComponents[i].name} - - + + `; } + while (sectionMissing.__sections__.length > panel.dump.removedComponents.length) { const $section = sectionMissing.__sections__.pop(); $section.parentElement.removeChild($section); @@ -811,6 +912,7 @@ const Elements = { layer: { ready() { const panel = this; + panel.$.nodeLayerButton.addEventListener('change', (event) => { event.stopPropagation(); Editor.Message.send('project', 'open-settings', 'project', 'layer'); @@ -818,18 +920,14 @@ const Elements = { }, async update() { const panel = this; + if (!panel.dump || panel.dump.isScene) { return; } + const layerDump = panel.dump.layer; const enumList = layerDump.enumList || []; - // const userLayers = await Editor.Profile.getProject('project', 'layer'); - // if (Array.isArray(userLayers)) { - // enumList.unshift(...userLayers); - // enumList.sort((a: any, b: any) => { - // return a.value - b.value; - // }); - // } + let optionHtml = ''; if (enumList) { for (const item of enumList) { @@ -838,10 +936,10 @@ const Elements = { } panel.$.nodeLayerSelect.innerHTML = optionHtml; panel.$.nodeLayerSelect.value = layerDump.value; + if (layerDump.values && layerDump.values.some((value) => value !== layerDump.value)) { panel.$.nodeLayerSelect.invalid = true; - } - else { + } else { panel.$.nodeLayerSelect.invalid = false; } panel.$.nodeLayer.setReadonly(layerDump, panel.$.nodeLayerSelect); @@ -850,6 +948,7 @@ const Elements = { footer: { ready() { const panel = this; + panel.$.componentAdd.addEventListener('click', () => { const rawTimestamp = Date.now(); Editor.Panel._kitControl.open({ @@ -871,21 +970,26 @@ const Elements = { }); }); }, - update() { }, + update() {}, }, materials: { async update() { const panel = this; + const materialPanels = []; const materialPanelType = 'asset'; + const oldChildren = Array.from(panel.$.sectionAsset.children); + const materialUuids = panel.assets['cc.Material']; let materialPrevPanel = null; + for (const materialUuid in materialUuids) { const recode = materialUuids[materialUuid]; if (!recode) { continue; } + let materialPanel = oldChildren.find((child) => child.getAttribute('uuid') === materialUuid); if (!materialPanel) { // 添加新的 @@ -896,17 +1000,18 @@ const Elements = { materialPanel.panelObject.$.container.removeAttribute('whole'); materialPanel.panelObject.$.container.setAttribute('cache-expand', materialUuid); materialPanel.update([materialUuid], panel.renderManager[materialPanelType]); + // 按数组顺序放置 if (materialPrevPanel) { materialPrevPanel.after(materialPanel); - } - else { + } else { panel.$.sectionAsset.prepend(materialPanel); } } materialPanels.push(materialPanel); materialPrevPanel = materialPanel; } + // 删除多余的 for (const oldChild of oldChildren) { if (oldChild && materialPanels.indexOf(oldChild) === -1) { @@ -917,22 +1022,24 @@ const Elements = { }, async beforeClose() { const panel = this; - const children = Array.from(panel.$.sectionAsset.children); - for (const materialPanel of children) { + const children = Array.from(panel.$.sectionAsset.children); + + for (const materialPanel of children) { const next = await materialPanel.panel.beforeClose.call(materialPanel.panelObject); + if (next === false) { return false; - } - else { - + } else { materialPanel.remove(); } } + return true; }, }, }; + exports.methods = { /** * 获取组件帮助菜单的 url @@ -940,9 +1047,11 @@ exports.methods = { */ getHelpUrl(editor) { if (!editor || !editor.help) { - return; + return ''; } + const help = editor.help; + /** * 约定的规则 * 翻译的都需要 i18n: 开头 @@ -951,11 +1060,13 @@ exports.methods = { if (!help.startsWith('i18n:')) { return help; } + const i18nKey = help.substr(5); const url = Editor.I18n.t('ENGINE.help.' + i18nKey); if (url) { return url; } + /** * 再在编辑器内部查找翻译一次 * 结果可能为空,也是一种需求,即组件配置了但没有合适的文档配置 @@ -968,17 +1079,18 @@ exports.methods = { componentContextMenu(uuidList, dump, total, index, nodeDumps) { // 是否多选节点 const isMultiple = uuidList.length > 1 ? true : false; + const uuid = uuidList[0]; - // const total = vm.total; - // const index = vm.index; + const clipboardComponentInfo = Editor.Clipboard.read('_dump_component_'); + Editor.Menu.popup({ menu: [ { label: Editor.I18n.t('inspector.menu.reset_component'), click() { Editor.Message.send('scene', 'snapshot'); - // const comp = dump.__comps__[index]; + (dump.value.uuid.values || [dump.value.uuid.value]).forEach((compUuid) => { Editor.Message.send('scene', 'reset-component', { uuid: compUuid, @@ -1048,6 +1160,7 @@ exports.methods = { enabled: !!(clipboardComponentInfo && clipboardComponentInfo.type === dump.type), async click() { Editor.Message.send('scene', 'snapshot'); + (dump.value.uuid.values || [dump.value.uuid.value]).forEach((value) => { nodeDumps.forEach((nodeDump) => { const uuid = nodeDump.uuid.value; @@ -1070,18 +1183,21 @@ exports.methods = { enabled: !!clipboardComponentInfo, async click() { Editor.Message.send('scene', 'snapshot'); + (dump.value.uuid.values || [dump.value.uuid.value]).forEach(async (dump, index) => { const uuid = uuidList[index]; await Editor.Message.request('scene', 'create-component', { uuid, component: clipboardComponentInfo.type, }); + // 检查是否创建成功,是的话,给赋值 const nodeDump = await Editor.Message.request('scene', 'query-node', uuid); const length = nodeDump.__comps__ && nodeDump.__comps__.length; if (length) { const lastIndex = length - 1; const lastComp = nodeDump.__comps__[lastIndex]; + if (lastComp && lastComp.type === clipboardComponentInfo.type) { await Editor.Message.request('scene', 'set-property', { uuid, @@ -1098,11 +1214,14 @@ exports.methods = { }, nodeContextMenu(uuidList, dumps) { const dump = dumps[0]; + // 是否多选节点 const isMultiple = dump.length > 1 ? true : false; + const clipboardNodeInfo = Editor.Clipboard.read('_dump_node_'); const clipboardNodeWorldTransform = Editor.Clipboard.read('_dump_node_world_transform_'); const clipboardComponentInfo = Editor.Clipboard.read('_dump_component_'); + Editor.Menu.popup({ menu: [ { @@ -1110,6 +1229,7 @@ exports.methods = { enabled: !dump.position.readonly && !dump.rotation.readonly && !dump.scale.readonly, click() { Editor.Message.send('scene', 'snapshot'); + for (const uuid of uuidList) { Editor.Message.send('scene', 'reset-node', { uuid, @@ -1134,6 +1254,7 @@ exports.methods = { enabled: !!clipboardNodeInfo, async click() { Editor.Message.send('scene', 'snapshot'); + for (const uuid of uuidList) { clipboardNodeInfo.attrs.forEach((attr) => { Editor.Message.send('scene', 'set-property', { @@ -1155,6 +1276,7 @@ exports.methods = { method: 'queryNodeWorldTransform', args: [dump.uuid.value], }); + if (data) { Editor.Clipboard.write('_dump_node_world_transform_', { data, @@ -1167,6 +1289,7 @@ exports.methods = { enabled: !!clipboardNodeWorldTransform, async click() { Editor.Message.send('scene', 'snapshot'); + if (clipboardNodeWorldTransform.data) { for (const uuid of uuidList) { Editor.Message.request('scene', 'execute-scene-script', { @@ -1184,17 +1307,20 @@ exports.methods = { enabled: !!clipboardComponentInfo, async click() { Editor.Message.send('scene', 'snapshot'); - uuidList.forEach(async (uuid, index) => { + + uuidList.forEach(async (uuid) => { await Editor.Message.request('scene', 'create-component', { uuid, component: clipboardComponentInfo.type, }); + // 检查是否创建成功,是的话,给赋值 const nodeDump = await Editor.Message.request('scene', 'query-node', uuid); const length = nodeDump.__comps__ && nodeDump.__comps__.length; if (length) { const lastIndex = length - 1; const lastComp = nodeDump.__comps__[lastIndex]; + if (lastComp && lastComp.type === clipboardComponentInfo.type) { await Editor.Message.request('scene', 'set-property', { uuid, @@ -1212,6 +1338,7 @@ exports.methods = { enabled: !dump.position.readonly && JSON.stringify(dump.position.value) !== JSON.stringify(dump.position.default), click() { Editor.Message.send('scene', 'snapshot'); + for (const uuid of uuidList) { Editor.Message.send('scene', 'reset-property', { uuid, @@ -1225,6 +1352,7 @@ exports.methods = { enabled: !dump.rotation.readonly && JSON.stringify(dump.rotation.value) !== JSON.stringify(dump.rotation.default), click() { Editor.Message.send('scene', 'snapshot'); + for (const uuid of uuidList) { Editor.Message.send('scene', 'reset-property', { uuid, @@ -1238,6 +1366,7 @@ exports.methods = { enabled: !dump.rotation.readonly && JSON.stringify(dump.scale.value) !== JSON.stringify(dump.scale.default), click() { Editor.Message.send('scene', 'snapshot'); + for (const uuid of uuidList) { Editor.Message.send('scene', 'reset-property', { uuid, @@ -1250,44 +1379,52 @@ exports.methods = { }); }, }; -async function update(uuidList, renderMap, typeManager, renderManager) { + +exports.update = async function update(uuidList, renderMap, dropConfig, typeManager, renderManager) { const panel = this; + panel.uuidList = uuidList || []; panel.renderMap = renderMap; + panel.dropConfig = dropConfig; panel.typeManager = typeManager; panel.renderManager = renderManager; + for (const prop in Elements) { const element = Elements[prop]; if (element.update) { await element.update.call(panel); } } -} -exports.update = update; -async function ready() { +}; + +exports.ready = async function ready() { const panel = this; + // 为了避免把 ui-num-input, ui-color 的连续 change 进行 snapshot panel.snapshotLock = false; + for (const prop in Elements) { const element = Elements[prop]; if (element.ready) { element.ready.call(panel); } } -} -exports.ready = ready; -async function close() { +}; + +exports.close = async function close() { const panel = this; + for (const prop in Elements) { const element = Elements[prop]; if (element.close) { element.close.call(panel); } } -} -exports.close = close; -async function beforeClose() { +}; + +exports.beforeClose = async function beforeClose() { const panel = this; + for (const prop in Elements) { const element = Elements[prop]; if (element.beforeClose) { @@ -1297,9 +1434,9 @@ async function beforeClose() { } } } + return true; -} -exports.beforeClose = beforeClose; +}; exports.config = { section: require('../components.js'), diff --git a/editor/inspector/contributions/utils.js b/editor/inspector/contributions/utils.js index f8e2dc826d..1a1b43a90c 100644 --- a/editor/inspector/contributions/utils.js +++ b/editor/inspector/contributions/utils.js @@ -52,8 +52,7 @@ function translate(dump, path, dumps, assets) { }); // 仅当存在 value 数据时才添加 values item.values = values; - } - catch (error) { + } catch (error) { values = undefined; } } @@ -87,11 +86,18 @@ function collectGroups(dump) { if (!dump.groups) { dump.groups = {}; } + for (const name in dump.value) { const info = dump.value[name]; if (!info || !info.group) { + if (info.isArray) { + info.value.forEach((item) => { + collectGroups(item); + }); + } continue; } + if (typeof info.group !== 'object') { info.group = { id: 'default', @@ -143,8 +149,7 @@ function translationDump(dump, dumps, assets) { if (allow) { collectGroups(component); translate(component.value, component.path, dumps ? dumps.map((dump) => dump.__comps__[i].value) : undefined, assets); - } - else { + } else { break; } }