From 57589e51f2f92b104f0c11c337766b69dc6fa690 Mon Sep 17 00:00:00 2001 From: liteng <930372551@qq.com> Date: Sat, 15 Sep 2018 17:21:58 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=88=A0=E9=99=A4=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E4=BA=8B=E4=BB=B6=E3=80=82=202=E3=80=81?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E5=9C=BA=E6=99=AF=E4=B8=8D=E5=86=8D=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/editor/menubar/SceneMenu.js | 179 +++++++++++++++--- ShadowEditor.Web/src/event/EventDispatcher.js | 12 -- ShadowEditor.Web/src/event/EventList.js | 7 - .../src/event/menu/scene/LoadSceneEvent.js | 33 ---- .../src/event/menu/scene/NewSceneEvent.js | 43 ----- .../src/event/menu/scene/PublishSceneEvent.js | 77 -------- .../src/event/menu/scene/SaveSceneAsEvent.js | 57 ------ .../src/event/menu/scene/SaveSceneEvent.js | 58 ------ 8 files changed, 157 insertions(+), 309 deletions(-) delete mode 100644 ShadowEditor.Web/src/event/menu/scene/LoadSceneEvent.js delete mode 100644 ShadowEditor.Web/src/event/menu/scene/NewSceneEvent.js delete mode 100644 ShadowEditor.Web/src/event/menu/scene/PublishSceneEvent.js delete mode 100644 ShadowEditor.Web/src/event/menu/scene/SaveSceneAsEvent.js delete mode 100644 ShadowEditor.Web/src/event/menu/scene/SaveSceneEvent.js diff --git a/ShadowEditor.Web/src/editor/menubar/SceneMenu.js b/ShadowEditor.Web/src/editor/menubar/SceneMenu.js index 963b7730..ce60108c 100644 --- a/ShadowEditor.Web/src/editor/menubar/SceneMenu.js +++ b/ShadowEditor.Web/src/editor/menubar/SceneMenu.js @@ -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; \ No newline at end of file diff --git a/ShadowEditor.Web/src/event/EventDispatcher.js b/ShadowEditor.Web/src/event/EventDispatcher.js index 470b898c..1bd8a03b 100644 --- a/ShadowEditor.Web/src/event/EventDispatcher.js +++ b/ShadowEditor.Web/src/event/EventDispatcher.js @@ -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), diff --git a/ShadowEditor.Web/src/event/EventList.js b/ShadowEditor.Web/src/event/EventList.js index 308e543e..4abacc40 100644 --- a/ShadowEditor.Web/src/event/EventList.js +++ b/ShadowEditor.Web/src/event/EventList.js @@ -25,18 +25,11 @@ var EventList = [ 'optionsChanged', // 配置改变事件 // 菜单栏事件 - 'mNewScene', // 新建 - 'mLoadScene', // 载入 - 'mSaveScene', // 保存 - 'mSaveSceneAs', // 另存为 - 'mPublishScene', // 发布 - 'mUndo', // 撤销 'mRedo', // 重做 'mClearHistory', // 清空历史记录 'mClone', // 复制 'mDelete', // 删除 - 'mMinifyShader', // 清除着色器 'mAddGroup', // 添加组 'mAddPlane', // 添加平板 diff --git a/ShadowEditor.Web/src/event/menu/scene/LoadSceneEvent.js b/ShadowEditor.Web/src/event/menu/scene/LoadSceneEvent.js deleted file mode 100644 index d5650738..00000000 --- a/ShadowEditor.Web/src/event/menu/scene/LoadSceneEvent.js +++ /dev/null @@ -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; \ No newline at end of file diff --git a/ShadowEditor.Web/src/event/menu/scene/NewSceneEvent.js b/ShadowEditor.Web/src/event/menu/scene/NewSceneEvent.js deleted file mode 100644 index d9d53b5c..00000000 --- a/ShadowEditor.Web/src/event/menu/scene/NewSceneEvent.js +++ /dev/null @@ -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; \ No newline at end of file diff --git a/ShadowEditor.Web/src/event/menu/scene/PublishSceneEvent.js b/ShadowEditor.Web/src/event/menu/scene/PublishSceneEvent.js deleted file mode 100644 index 40cb549b..00000000 --- a/ShadowEditor.Web/src/event/menu/scene/PublishSceneEvent.js +++ /dev/null @@ -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.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; \ No newline at end of file diff --git a/ShadowEditor.Web/src/event/menu/scene/SaveSceneAsEvent.js b/ShadowEditor.Web/src/event/menu/scene/SaveSceneAsEvent.js deleted file mode 100644 index 758b7b89..00000000 --- a/ShadowEditor.Web/src/event/menu/scene/SaveSceneAsEvent.js +++ /dev/null @@ -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; \ No newline at end of file diff --git a/ShadowEditor.Web/src/event/menu/scene/SaveSceneEvent.js b/ShadowEditor.Web/src/event/menu/scene/SaveSceneEvent.js deleted file mode 100644 index 50f7dd7e..00000000 --- a/ShadowEditor.Web/src/event/menu/scene/SaveSceneEvent.js +++ /dev/null @@ -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; \ No newline at end of file