创建脚本。

This commit is contained in:
liteng 2018-08-30 21:17:23 +08:00
parent d3392d01b8
commit 44f25f9c85
3 changed files with 83 additions and 78 deletions

View File

@ -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随便填写typejavascriptvertexShader, fragmentShader, jsonsource源码。
this.scripts = {};
// 帮助器

View File

@ -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 () {

View File

@ -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;