From 44f25f9c85f6b66b20f378ebfddbf3d0ca8a191f Mon Sep 17 00:00:00 2001 From: liteng <930372551@qq.com> Date: Thu, 30 Aug 2018 21:17:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=84=9A=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ShadowEditor.Web/src/editor/Editor.js | 3 +- .../src/editor/script/window/ScriptWindow.js | 17 +++ .../src/editor/sidebar/ScriptPanel.js | 141 ++++++++---------- 3 files changed, 83 insertions(+), 78 deletions(-) diff --git a/ShadowEditor.Web/src/editor/Editor.js b/ShadowEditor.Web/src/editor/Editor.js index 73f7ffbf..ed5c5c11 100644 --- a/ShadowEditor.Web/src/editor/Editor.js +++ b/ShadowEditor.Web/src/editor/Editor.js @@ -54,7 +54,8 @@ function Editor(app) { // 物体 this.objects = []; - // 脚本 格式:{ uuid: { id: 'mongoDB id', name: 'Script Name', source: 'Source Code' } },其中,uuid是随机产生,id是mongo数据库ID字段,脚本名称随便填 + // 脚本 格式:{ uuid: { id: 'mongoDB id', name: 'Script Name', type: 'Script Type', source: 'Source Code' }} + // 其中,uuid是创建脚本时自动生成,id是mongo数据库ID字段;name:随便填写;type:javascript,vertexShader, fragmentShader, json;source:源码。 this.scripts = {}; // 帮助器 diff --git a/ShadowEditor.Web/src/editor/script/window/ScriptWindow.js b/ShadowEditor.Web/src/editor/script/window/ScriptWindow.js index 8fe57527..f0cd0f7a 100644 --- a/ShadowEditor.Web/src/editor/script/window/ScriptWindow.js +++ b/ShadowEditor.Web/src/editor/script/window/ScriptWindow.js @@ -11,7 +11,10 @@ import JsonStarter from '../code/JsonStarter'; */ function ScriptWindow(options) { UI.Control.call(this, options); + options = options || {}; + this.app = options.app; + this.onChange = options.onChange || null; } ScriptWindow.prototype = Object.create(UI.Control.prototype); @@ -118,6 +121,20 @@ ScriptWindow.prototype.onCreateScript = function () { } this.app.script.open(scriptName, scriptType, initCode, scriptName); + + var uuid = THREE.Math.generateUUID(); + + this.app.editor.scripts[uuid] = { + id: 0, + name: scriptName, + type: scriptType, + source: initCode, + uuid: uuid + }; + + if (this.onChange) { + this.onChange(); + } }; ScriptWindow.prototype.onCancelScript = function () { diff --git a/ShadowEditor.Web/src/editor/sidebar/ScriptPanel.js b/ShadowEditor.Web/src/editor/sidebar/ScriptPanel.js index 103c1f7e..3d6b66ec 100644 --- a/ShadowEditor.Web/src/editor/sidebar/ScriptPanel.js +++ b/ShadowEditor.Web/src/editor/sidebar/ScriptPanel.js @@ -32,14 +32,6 @@ ScriptPanel.prototype.render = function () { }, text: '常用脚本' }] - }, { - xtype: 'row', - id: 'commonScriptContainer' - }, { - xtype: 'button', - id: 'newCommonScript', - text: '新建脚本', - onClick: this.createNewCommonScript.bind(this) }, { xtype: 'row', style: { @@ -55,12 +47,12 @@ ScriptPanel.prototype.render = function () { }] }, { xtype: 'row', - id: 'customScriptContainer' + id: 'scriptsContainer' }, { xtype: 'button', id: 'newCustomScript', text: '新建脚本', - onClick: this.createNewCustomScript.bind(this) + onClick: this.createNewScript.bind(this) }] }; @@ -68,89 +60,84 @@ ScriptPanel.prototype.render = function () { control.render(); }; -ScriptPanel.prototype.createNewCommonScript = function () { +ScriptPanel.prototype.createNewScript = function () { if (this.window == null) { - this.window = new ScriptWindow({ app: this.app }); + this.window = new ScriptWindow({ + app: this.app, + onChange: this.update.bind(this) + }); this.window.render(); } this.window.reset(); this.window.show(); }; -ScriptPanel.prototype.createNewCustomScript = function () { - if (this.window == null) { - this.window = new ScriptWindow({ app: this.app }); - this.window.render(); - } - this.window.reset(); - this.window.show(); -}; - - ScriptPanel.prototype.update = function () { - var scriptsContainer = UI.get('scriptsContainer'); - var editor = this.app.editor; - var _this = this; + var container = UI.get('scriptsContainer'); + container.dom.innerHTML = ''; + container.dom.style.display = 'none'; - scriptsContainer.dom.innerHTML = ''; - scriptsContainer.dom.style.display = 'none'; + var scripts = this.app.editor.scripts; - var object = editor.selected; - if (object === null) { + if (Object.keys(scripts).length === 0) { return; } - var scripts = editor.scripts[object.uuid]; + container.dom.style.display = 'block'; - if (scripts !== undefined) { - scriptsContainer.dom.style.display = 'block'; + Object.keys(scripts).forEach(n => { + var script = scripts[n]; + var name = script.name; + var extension; - for (var i = 0; i < scripts.length; i++) { - (function (object, script) { - var data = { - xtype: 'container', - parent: scriptsContainer.dom, - children: [{ - xtype: 'input', - value: script.name, - style: { - width: '130px', - fontSize: '12px' - }, - onChange: function () { - editor.execute(new SetScriptValueCommand(editor.selected, script, 'name', this.getValue())); - } - }, { - xtype: 'button', - text: '编辑', - style: { - marginLeft: '4px' - }, - onClick: function () { - _this.app.call('editScript', _this, object, script); - } - }, { - xtype: 'button', - text: '删除', - style: { - marginLeft: '4px' - }, - onClick: function () { - UI.confirm('询问', '确定要删除吗?', function (event, btn) { - if (btn === 'ok') { - editor.execute(new RemoveScriptCommand(editor.selected, script)); - } - }); - } - }, { - xtype: 'br' - }] - }; - - UI.create(data).render(); - })(object, scripts[i]) + switch (script.type) { + case 'javascript': + extension = '.js'; + break; + case 'vertexShader': + case 'fragmentShader': + extension = '.glsl'; + break; + case 'json': + extension = '.json'; + break; } - } + + var data = { + xtype: 'container', + parent: container.dom, + children: [{ + xtype: 'text', + text: name + extension, + style: { + width: '130px', + fontSize: '12px' + } + }, { + xtype: 'button', + text: '编辑', + style: { + marginLeft: '4px' + }, + onClick: function () { + + } + }, { + xtype: 'button', + text: '删除', + style: { + marginLeft: '4px' + }, + onClick: function () { + + } + }, { + xtype: 'br' + }] + }; + + UI.create(data).render(); + }); }; export default ScriptPanel; \ No newline at end of file