(function (global) { 'use strict'; /** * 属性面板(壳侧):通过 Bridge 读写节点属性。 * @constructor */ function InspectorPanel() { this.el = null; this.body = null; this._bridge = null; this._uuid = ''; this._props = null; } InspectorPanel.prototype.setBridge = function (bridge) { this._bridge = bridge; }; InspectorPanel.prototype.mount = function () { var root = document.createElement('div'); root.className = 'pw-panel-inner pw-inspector'; root.style.height = '100%'; root.style.display = 'flex'; root.style.flexDirection = 'column'; root.innerHTML = '
' + '
未选中节点
'; this.el = root; this.body = root.querySelector('#pw-inspector-body'); return root; }; InspectorPanel.prototype.loadUuid = function (uuid) { var self = this; this._uuid = uuid || ''; if (!this._uuid || !this._bridge) { this._props = null; if (this.body) this.body.innerHTML = '
未选中节点
'; return; } this._bridge.call('node.get', { uuid: this._uuid }).then(function (props) { self._props = props; self.render(); }).catch(function () { if (self.body) self.body.innerHTML = '
读取失败
'; }); }; InspectorPanel.prototype.render = function () { var body = this.body; var p = this._props; if (!body) return; if (!p) { body.innerHTML = '
未选中节点
'; return; } var html = []; html.push('
'); html.push('
节点
'); html.push('
'); html.push(rowText('名称', 'name', p.name)); html.push(rowCheck('激活', 'active', !!p.active)); html.push(rowNum('Layer', 'layer', p.layer)); html.push(rowVec3('位置', ['px', 'py', 'pz'], [p.pos.x, p.pos.y, p.pos.z])); html.push(rowVec3('旋转', ['rx', 'ry', 'rz'], [p.euler.x, p.euler.y, p.euler.z])); html.push(rowVec3('缩放', ['sx', 'sy', 'sz'], [p.scale.x, p.scale.y, p.scale.z])); html.push('
'); if (p.ui) { html.push('
'); html.push('
UITransform
'); html.push('
'); html.push(rowVec2('尺寸', ['uiw', 'uih'], [p.ui.width, p.ui.height], ['W', 'H'])); html.push(rowVec2('锚点', ['uax', 'uay'], [p.ui.anchorX, p.ui.anchorY], ['X', 'Y'])); html.push('
'); } var comps = p.comps || []; for (var i = 0; i < comps.length; i++) { var c = comps[i]; html.push('
'); html.push('
' + escapeHtml(c.name) + '
'); html.push('
'); var fields = c.fields || []; if (!fields.length) html.push('
无可编辑公开字段
'); for (var f = 0; f < fields.length; f++) { var field = fields[f]; var fid = 'c' + c.index + '_' + field.key; if (field.type === 'number') html.push(rowNum(field.key, fid, field.value)); else if (field.type === 'boolean') html.push(rowCheck(field.key, fid, field.value)); else html.push(rowText(field.key, fid, field.value)); } html.push('
'); } body.innerHTML = html.join(''); this._bindFields(); this._bindSections(); }; InspectorPanel.prototype._bindSections = function () { var heads = this.body.querySelectorAll('.pw-insp-sec-head[data-toggle]'); for (var i = 0; i < heads.length; i++) { heads[i].addEventListener('click', function (ev) { var sec = ev.currentTarget.parentNode; if (sec) sec.classList.toggle('open'); }); } }; InspectorPanel.prototype._bindFields = function () { var self = this; var body = this.body; if (!body || !this._bridge || !this._uuid) return; var inputs = body.querySelectorAll('[data-fid]'); for (var i = 0; i < inputs.length; i++) { (function (el) { var run = function () { var fid = el.getAttribute('data-fid'); var value; if (el.type === 'checkbox') value = !!el.checked; else if (el.type === 'number') value = parseFloat(el.value); else value = el.value; self._bridge.call('node.set', { uuid: self._uuid, fid: fid, value: value }); }; el.addEventListener('change', run); el.addEventListener('input', run); })(inputs[i]); } }; function rowText(label, id, value) { return '
'; } function rowNum(label, id, value) { return '
'; } function rowCheck(label, id, value) { return '
'; } function rowVec3(label, ids, values) { return '
' + '' + '
' + axis('X', ids[0], values[0]) + axis('Y', ids[1], values[1]) + axis('Z', ids[2], values[2]) + '
'; } function rowVec2(label, ids, values, axisNames) { return '
' + '' + '
' + axis(axisNames[0], ids[0], values[0]) + axis(axisNames[1], ids[1], values[1]) + '
'; } function axis(name, id, value) { return '
' + name + '
'; } function escapeHtml(s) { return String(s).replace(/&/g, '&').replace(//g, '>'); } function escapeAttr(s) { return escapeHtml(s).replace(/"/g, '"'); } global.PWInspectorPanel = InspectorPanel; })(window);