1、删除场景菜单事件。

2、发布场景不再报错。
This commit is contained in:
liteng 2018-09-15 17:21:58 +08:00
parent dbf9bce3f5
commit 57589e51f2
8 changed files with 157 additions and 309 deletions

View File

@ -1,4 +1,7 @@
import UI from '../../ui/UI';
import SceneWindow from '../window/SceneWindow';
import Converter from '../../serialization/Converter';
import Ajax from '../../utils/Ajax';
/**
* 场景菜单
@ -14,8 +17,6 @@ SceneMenu.prototype = Object.create(UI.Control.prototype);
SceneMenu.prototype.constructor = SceneMenu;
SceneMenu.prototype.render = function () {
var _this = this;
var container = UI.create({
xtype: 'div',
parent: this.parent,
@ -29,51 +30,185 @@ SceneMenu.prototype.render = function () {
cls: 'options',
children: [{
xtype: 'div',
id: 'mNewScene',
html: '新建',
cls: 'option',
onClick: function () {
_this.app.call('mNewScene');
}
onClick: this.newScene.bind(this)
}, {
xtype: 'div',
id: 'mLoadScene',
html: '载入',
cls: 'option',
onClick: function () {
_this.app.call('mLoadScene');
}
onClick: this.loadScene.bind(this)
}, {
xtype: 'div',
id: 'mSaveScene',
html: '保存',
cls: 'option',
onClick: function () {
_this.app.call('mSaveScene');
}
onClick: this.saveScene.bind(this)
}, {
xtype: 'div',
id: 'mSaveSceneAs',
html: '另存为',
cls: 'option',
onClick: function () {
_this.app.call('mSaveSceneAs');
}
onClick: this.saveAsScene.bind(this)
}, {
xtype: 'hr'
}, {
xtype: 'div',
id: 'mPublishScene',
html: '发布',
cls: 'option',
onClick: function () {
_this.app.call('mPublishScene');
}
onClick: this.publishScene.bind(this)
}]
}]
});
container.render();
this.link = document.createElement('a');
this.link.style.display = 'none';
document.body.appendChild(this.link); // Firefox workaround, see #6594
}
// ---------------------------- 新建场景 ---------------------------------
SceneMenu.prototype.newScene = function () {
var editor = this.app.editor;
if (editor.sceneName == null) {
editor.clear();
document.title = '未命名';
return;
}
UI.confirm('询问', '所有未保存数据将丢失,确定要新建场景吗?', function (event, btn) {
if (btn === 'ok') {
editor.clear();
editor.sceneID = null;
editor.sceneName = null;
document.title = '未命名';
}
});
};
// --------------------------- 载入场景 --------------------------------------
SceneMenu.prototype.loadScene = function () {
if (this.window == null) {
this.window = new SceneWindow({ app: this.app });
this.window.render();
}
this.window.show();
};
// --------------------------- 保存场景 ----------------------------------------
SceneMenu.prototype.saveScene = function () { // 保存场景
var editor = this.app.editor;
var sceneName = editor.sceneName;
if (sceneName == null) {
UI.prompt('保存场景', '名称', '新场景', (event, name) => {
this.app.editor.sceneName = name;
document.title = name;
this.commitSave(name);
});
} else {
this.commitSave(sceneName);
}
};
SceneMenu.prototype.commitSave = function (sceneName) {
var obj = (new Converter()).toJSON({
options: this.app.options,
camera: this.app.editor.camera,
renderer: this.app.editor.renderer,
scripts: this.app.editor.scripts,
scene: this.app.editor.scene
});
Ajax.post(this.app.options.server + '/api/Scene/Save', {
Name: sceneName,
Data: JSON.stringify(obj)
}, function (result) {
var obj = JSON.parse(result);
UI.msg(obj.Msg);
});
};
// --------------------------- 另存为场景 -------------------------------------
SceneMenu.prototype.saveAsScene = function () {
var sceneName = this.app.editor.sceneName;
if (sceneName == null) {
sceneName = '新场景';
}
UI.prompt('保存场景', '名称', sceneName, (event, name) => {
this.app.editor.sceneName = name;
document.title = name;
this.commitSaveAs(name);
});
};
SceneMenu.prototype.commitSaveAs = function (sceneName) {
var obj = (new Converter()).toJSON({
options: this.app.options,
camera: this.app.editor.camera,
renderer: this.app.editor.renderer,
scripts: this.app.editor.scripts,
scene: this.app.editor.scene
});
Ajax.post(this.app.options.server + '/api/Scene/Save', {
Name: sceneName,
Data: JSON.stringify(obj)
}, function (result) {
var obj = JSON.parse(result);
UI.msg(obj.Msg);
});
};
// ------------------------- 发布场景 ------------------------------
SceneMenu.prototype.publishScene = function () {
var editor = this.app.editor;
var zip = new JSZip();
//
var obj = (new Converter()).toJSON({
options: this.app.options,
camera: this.app.editor.camera,
renderer: this.app.editor.renderer,
scripts: this.app.editor.scripts,
scene: this.app.editor.scene
});
var output = JSON.stringify(obj);
zip.file('scene.json', output);
// 保存数据
var manager = new THREE.LoadingManager(() => {
this.savePublishScene(zip.generate({
type: 'blob'
}), `${editor.sceneName}.zip`);
});
var loader = new THREE.FileLoader(manager);
loader.load('index.html', content => {
zip.file('index.html', content);
});
loader.load('dist/ShadowEditor.js', function (content) {
zip.file('dist/ShadowEditor.js', content);
});
};
SceneMenu.prototype.savePublishScene = function (text, filename) {
var blob = new Blob([text], { type: 'text/plain' });
this.link.href = URL.createObjectURL(blob);
this.link.download = filename;
this.link.click();
};
export default SceneMenu;

View File

@ -20,12 +20,6 @@ import ModelEvent from './toolbar/ModelEvent';
import PathModeEvent from './toolbar/PathModeEvent';
// 菜单栏
import NewSceneEvent from './menu/scene/NewSceneEvent';
import LoadSceneEvent from './menu/scene/LoadSceneEvent';
import SaveSceneEvent from './menu/scene/SaveSceneEvent';
import SaveSceneAsEvent from './menu/scene/SaveSceneAsEvent';
import PublishSceneEvent from './menu/scene/PublishSceneEvent';
import UndoEvent from './menu/edit/UndoEvent';
import RedoEvent from './menu/edit/RedoEvent';
import ClearHistoryEvent from './menu/edit/ClearHistoryEvent';
@ -112,12 +106,6 @@ function EventDispatcher(app) {
new PathModeEvent(this.app),
// menubar中的事件
new NewSceneEvent(this.app),
new LoadSceneEvent(this.app),
new SaveSceneEvent(this.app),
new SaveSceneAsEvent(this.app),
new PublishSceneEvent(this.app),
new UndoEvent(this.app),
new RedoEvent(this.app),
new ClearHistoryEvent(this.app),

View File

@ -25,18 +25,11 @@ var EventList = [
'optionsChanged', // 配置改变事件
// 菜单栏事件
'mNewScene', // 新建
'mLoadScene', // 载入
'mSaveScene', // 保存
'mSaveSceneAs', // 另存为
'mPublishScene', // 发布
'mUndo', // 撤销
'mRedo', // 重做
'mClearHistory', // 清空历史记录
'mClone', // 复制
'mDelete', // 删除
'mMinifyShader', // 清除着色器
'mAddGroup', // 添加组
'mAddPlane', // 添加平板

View File

@ -1,33 +0,0 @@
import MenuEvent from '../MenuEvent';
import UI from '../../../ui/UI';
import SceneWindow from '../../../editor/window/SceneWindow';
/**
* 载入场景
* @author tengge / https://github.com/tengge1
* @param {*} app
*/
function LoadSceneEvent(app) {
MenuEvent.call(this, app);
}
LoadSceneEvent.prototype = Object.create(MenuEvent.prototype);
LoadSceneEvent.prototype.constructor = LoadSceneEvent;
LoadSceneEvent.prototype.start = function () {
this.app.on(`mLoadScene.${this.id}`, this.onLoadScene.bind(this));
};
LoadSceneEvent.prototype.stop = function () {
this.app.on(`mLoadScene.${this.id}`, null);
};
LoadSceneEvent.prototype.onLoadScene = function () {
if (this.window == null) {
this.window = new SceneWindow({ app: this.app });
this.window.render();
}
this.window.show();
};
export default LoadSceneEvent;

View File

@ -1,43 +0,0 @@
import MenuEvent from '../MenuEvent';
import UI from '../../../ui/UI';
/**
* 新建场景
* @author tengge / https://github.com/tengge1
* @param {*} app
*/
function NewSceneEvent(app) {
MenuEvent.call(this, app);
}
NewSceneEvent.prototype = Object.create(MenuEvent.prototype);
NewSceneEvent.prototype.constructor = NewSceneEvent;
NewSceneEvent.prototype.start = function () {
this.app.on(`mNewScene.${this.id}`, this.onNewScene.bind(this));
};
NewSceneEvent.prototype.stop = function () {
this.app.on(`mNewScene.${this.id}`, null);
};
NewSceneEvent.prototype.onNewScene = function () {
var editor = this.app.editor;
if (editor.sceneName == null) {
editor.clear();
document.title = '未命名';
return;
}
UI.confirm('询问', '所有未保存数据将丢失,确定要新建场景吗?', function (event, btn) {
if (btn === 'ok') {
editor.clear();
editor.sceneID = null;
editor.sceneName = null;
document.title = '未命名';
}
});
};
export default NewSceneEvent;

View File

@ -1,77 +0,0 @@
import MenuEvent from '../MenuEvent';
import MathUtils from '../../../utils/MathUtils';
/**
* 发布场景
* @author tengge / https://github.com/tengge1
* @param {*} app
*/
function PublishSceneEvent(app) {
MenuEvent.call(this, app);
}
PublishSceneEvent.prototype = Object.create(MenuEvent.prototype);
PublishSceneEvent.prototype.constructor = PublishSceneEvent;
PublishSceneEvent.prototype.start = function () {
this.app.on(`mPublishScene.${this.id}`, this.onPublishScene.bind(this));
this.link = document.createElement('a');
this.link.style.display = 'none';
document.body.appendChild(this.link); // Firefox workaround, see #6594
};
PublishSceneEvent.prototype.stop = function () {
this.app.on(`mPublishScene.${this.id}`, null);
};
PublishSceneEvent.prototype.onPublishScene = function () {
var editor = this.app.editor;
var zip = new JSZip();
//
var output = editor.toJSON();
output.metadata.type = 'App';
delete output.history;
output = JSON.stringify(output, MathUtils.parseNumber, '\t');
output = output.replace(/[\n\t]+([\d\.e\-\[\]]+)/g, '$1');
zip.file('app.json', output);
//
var _this = this;
var manager = new THREE.LoadingManager(function () {
_this.save(zip.generate({ type: 'blob' }), 'download.zip');
});
var loader = new THREE.FileLoader(manager);
loader.load('third_party/app/index.html', function (content) {
var includes = [];
content = content.replace('<!-- includes -->', includes.join('\n\t\t'));
zip.file('index.html', content);
});
loader.load('third_party/app.js', function (content) {
zip.file('js/app.js', content);
});
loader.load('node_modules/three/build/three.min.js', function (content) {
zip.file('js/three.min.js', content);
});
};
PublishSceneEvent.prototype.save = function (blob, filename) {
this.link.href = URL.createObjectURL(blob);
this.link.download = filename || 'data.json';
this.link.click();
// URL.revokeObjectURL( url ); breaks Firefox...
};
PublishSceneEvent.prototype.saveString = function (text, filename) {
this.save(new Blob([text], { type: 'text/plain' }), filename);
};
export default PublishSceneEvent;

View File

@ -1,57 +0,0 @@
import MenuEvent from '../MenuEvent';
import Converter from '../../../serialization/Converter';
import Ajax from '../../../utils/Ajax';
/**
* 场景另存为
* @author tengge / https://github.com/tengge1
* @param {*} app
*/
function SaveSceneAsEvent(app) {
MenuEvent.call(this, app);
}
SaveSceneAsEvent.prototype = Object.create(MenuEvent.prototype);
SaveSceneAsEvent.prototype.constructor = SaveSceneAsEvent;
SaveSceneAsEvent.prototype.start = function () {
this.app.on(`mSaveSceneAs.${this.id}`, this.onSaveSceneAs.bind(this));
};
SaveSceneAsEvent.prototype.stop = function () {
this.app.on(`mSaveSceneAs.${this.id}`, null);
};
SaveSceneAsEvent.prototype.onSaveSceneAs = function () {
var sceneName = this.app.editor.sceneName;
if (sceneName == null) {
sceneName = '新场景';
}
UI.prompt('保存场景', '名称', sceneName, (event, name) => {
this.app.editor.sceneName = name;
document.title = name;
this.commitSave(name);
});
};
SaveSceneAsEvent.prototype.commitSave = function (sceneName) {
var obj = (new Converter()).toJSON({
options: this.app.options,
camera: this.app.editor.camera,
renderer: this.app.editor.renderer,
scripts: this.app.editor.scripts,
scene: this.app.editor.scene
});
Ajax.post(this.app.options.server + '/api/Scene/Save', {
Name: sceneName,
Data: JSON.stringify(obj)
}, function (result) {
var obj = JSON.parse(result);
UI.msg(obj.Msg);
});
};
export default SaveSceneAsEvent;

View File

@ -1,58 +0,0 @@
import MenuEvent from '../MenuEvent';
import Converter from '../../../serialization/Converter';
import Ajax from '../../../utils/Ajax';
/**
* 保存场景
* @author tengge / https://github.com/tengge1
* @param {*} app
*/
function SaveSceneEvent(app) {
MenuEvent.call(this, app);
}
SaveSceneEvent.prototype = Object.create(MenuEvent.prototype);
SaveSceneEvent.prototype.constructor = SaveSceneEvent;
SaveSceneEvent.prototype.start = function () {
this.app.on(`mSaveScene.${this.id}`, this.onSaveScene.bind(this));
};
SaveSceneEvent.prototype.stop = function () {
this.app.on(`mSaveScene.${this.id}`, null);
};
SaveSceneEvent.prototype.onSaveScene = function () {
var editor = this.app.editor;
var sceneName = editor.sceneName;
if (sceneName == null) {
UI.prompt('保存场景', '名称', '新场景', (event, name) => {
this.app.editor.sceneName = name;
document.title = name;
this.commitSave(name);
});
} else {
this.commitSave(sceneName);
}
};
SaveSceneEvent.prototype.commitSave = function (sceneName) {
var obj = (new Converter()).toJSON({
options: this.app.options,
camera: this.app.editor.camera,
renderer: this.app.editor.renderer,
scripts: this.app.editor.scripts,
scene: this.app.editor.scene
});
Ajax.post(this.app.options.server + '/api/Scene/Save', {
Name: sceneName,
Data: JSON.stringify(obj)
}, function (result) {
var obj = JSON.parse(result);
UI.msg(obj.Msg);
});
};
export default SaveSceneEvent;