From e40ae9cd03bb8287c076148e09cdc9b536a83633 Mon Sep 17 00:00:00 2001 From: liteng <930372551@qq.com> Date: Sun, 17 Jun 2018 11:56:25 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=B8=85=E7=A9=BA=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E3=80=82=202=E3=80=81=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E4=BA=8B=E4=BB=B6=E3=80=82=203=E3=80=81?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=9C=BA=E6=99=AF=E4=BA=8B=E4=BB=B6=E3=80=82?= =?UTF-8?q?=204=E3=80=81=E9=80=89=E6=8B=A9=E4=BA=8B=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editor/Editor.js | 123 ++++++++------------------------ src/editor/Toolbar.js | 2 +- src/event/EventDispatcher.js | 8 +++ src/event/EventList.js | 4 ++ src/event/editor/ClearEvent.js | 51 +++++++++++++ src/event/editor/LoadEvent.js | 29 ++++++++ src/event/editor/SaveEvent.js | 40 +++++++++++ src/event/editor/SelectEvent.js | 42 +++++++++++ 8 files changed, 204 insertions(+), 95 deletions(-) create mode 100644 src/event/editor/ClearEvent.js create mode 100644 src/event/editor/LoadEvent.js create mode 100644 src/event/editor/SaveEvent.js create mode 100644 src/event/editor/SelectEvent.js diff --git a/src/editor/Editor.js b/src/editor/Editor.js index e7e8b1e5..c334e03b 100644 --- a/src/editor/Editor.js +++ b/src/editor/Editor.js @@ -57,6 +57,10 @@ Editor.prototype = { // ------------------ 物体 -------------------------- + objectByUuid: function (uuid) { // 根据uuid获取物体 + return this.scene.getObjectByProperty('uuid', uuid, true); + }, + addObject: function (object) { // 添加物体 this.app.call('addObject', this, object); }, @@ -113,120 +117,61 @@ Editor.prototype = { this.app.call('removeScript', this, object, script); }, - // - - select: function (object) { - - if (this.selected === object) return; - - var uuid = null; - - if (object !== null) { - - uuid = object.uuid; - - } - - this.selected = object; - - this.config.setKey('selected', uuid); - this.signals.objectSelected.dispatch(object); + // ----------------------- 选择事件 --------------------------- + select: function (object) { // 选中物体 + this.app.call('select', this, object); }, - selectById: function (id) { - + selectById: function (id) { // 根据id选中物体 if (id === this.camera.id) { - this.select(this.camera); return; - } this.select(this.scene.getObjectById(id, true)); - }, - selectByUuid: function (uuid) { - - var scope = this; - + selectByUuid: function (uuid) { // 根据uuid选中物体 + var _this = this; this.scene.traverse(function (child) { - if (child.uuid === uuid) { - - scope.select(child); - + _this.select(child); } - }); - }, - deselect: function () { - + deselect: function () { // 取消选中物体 this.select(null); - }, - focus: function (object) { + // -------------------- 设置焦点事件 -------------------------- + focus: function (object) { // 设置焦点 this.signals.objectFocused.dispatch(object); - }, - focusById: function (id) { - + focusById: function (id) { // 根据id设置交点 this.focus(this.scene.getObjectById(id, true)); - }, - clear: function () { - - this.history.clear(); - this.storage.clear(); - - this.camera.copy(this.DEFAULT_CAMERA); - this.scene.background.setHex(0xaaaaaa); - this.scene.fog = null; - - var objects = this.scene.children; - - while (objects.length > 0) { - - this.removeObject(objects[0]); - - } - - this.geometries = {}; - this.materials = {}; - this.textures = {}; - this.scripts = {}; - - this.deselect(); - - this.signals.editorCleared.dispatch(); + // -------------------------- 场景事件 --------------------------- + clear: function () { // 清空场景 + this.app.call('clear', this); }, - load: function () { - alert('开发中'); + load: function () { // 加载场景 + this.app.call('load', this); }, - save: function () { - var obj = SceneUtils.toJSON(this.scene); - Ajax.post(this.app.options.server + '/Service/SceneService.ashx?cmd=Save', { - name: 'Scene1', - data: JSON.stringify(obj) - }, function (result) { - var obj = JSON.parse(result); - alert(obj.Msg); - }); + save: function () { // 保存场景 + this.app.call('save', this); }, - // + // --------------------------- 场景序列化 ------------------------------ - fromJSON: function (json) { + fromJSON: function (json) { // 根据json创建场景 var loader = new THREE.ObjectLoader(); @@ -252,7 +197,7 @@ Editor.prototype = { }, - toJSON: function () { + toJSON: function () { // 将json转换为场景 // scripts clean up @@ -291,28 +236,18 @@ Editor.prototype = { }, - objectByUuid: function (uuid) { - - return this.scene.getObjectByProperty('uuid', uuid, true); - - }, - - execute: function (cmd, optionalName) { + // ----------------------- 命令 --------------------------- + execute: function (cmd, optionalName) { // 执行事件 this.history.execute(cmd, optionalName); - }, - undo: function () { - + undo: function () { // 撤销事件 this.history.undo(); - }, - redo: function () { - + redo: function () { // 重做事件 this.history.redo(); - } }; diff --git a/src/editor/Toolbar.js b/src/editor/Toolbar.js index fa22d6cb..97d20a06 100644 --- a/src/editor/Toolbar.js +++ b/src/editor/Toolbar.js @@ -78,7 +78,7 @@ function Toolbar(editor) { function update() { signals.snapChanged.dispatch(snap.getValue() === true ? grid.getValue() : null); - signals.spaceChanged.dispatch(local.getValue() === true ? "本地" : "世界"); + signals.spaceChanged.dispatch(local.getValue() === true ? "local" : "world"); signals.showGridChanged.dispatch(showGrid.getValue()); } diff --git a/src/event/EventDispatcher.js b/src/event/EventDispatcher.js index 717d64e2..4cb5aa76 100644 --- a/src/event/EventDispatcher.js +++ b/src/event/EventDispatcher.js @@ -26,6 +26,10 @@ import AddHelperEvent from './editor/AddHelperEvent'; import RemoveHelperEvent from './editor/RemoveHelperEvent'; import AddScriptEvent from './editor/AddScriptEvent'; import RemoveScriptEvent from './editor/RemoveScriptEvent'; +import SelectEvent from './editor/SelectEvent'; +import ClearEvent from './editor/ClearEvent'; +import LoadEvent from './editor/LoadEvent'; +import SaveEvent from './editor/SaveEvent'; /** * 事件执行器 @@ -62,6 +66,10 @@ function EventDispatcher(app) { new RemoveHelperEvent(this.app), new AddScriptEvent(this.app), new RemoveScriptEvent(this.app), + new SelectEvent(this.app), + new ClearEvent(this.app), + new LoadEvent(this.app), + new SaveEvent(this.app), ]; } diff --git a/src/event/EventList.js b/src/event/EventList.js index 4d1dadd4..ef319c0a 100644 --- a/src/event/EventList.js +++ b/src/event/EventList.js @@ -32,6 +32,10 @@ var EventList = [ 'removeHelper', // 移除脚本 'addScript', // 添加脚本 'removeScript', // 移除脚本 + 'select', // 选中事件 + 'clear', // 清空场景 + 'load', // 加载场景 + 'save', // 保存场景 ]; export default EventList; \ No newline at end of file diff --git a/src/event/editor/ClearEvent.js b/src/event/editor/ClearEvent.js new file mode 100644 index 00000000..572885fc --- /dev/null +++ b/src/event/editor/ClearEvent.js @@ -0,0 +1,51 @@ +import BaseEvent from '../BaseEvent'; + +/** + * 清空场景事件 + * @param {*} app + */ +function ClearEvent(app) { + BaseEvent.call(this, app); +} + +ClearEvent.prototype = Object.create(BaseEvent.prototype); +ClearEvent.prototype.constructor = ClearEvent; + +ClearEvent.prototype.start = function () { + var _this = this; + this.app.on('clear.' + this.id, function () { + _this.onClear(); + }); +}; + +ClearEvent.prototype.stop = function () { + this.app.on('clear.' + this.id, null); +}; + +ClearEvent.prototype.onClear = function () { + var editor = this.app.editor; + + editor.history.clear(); + editor.storage.clear(); + + editor.camera.copy(editor.DEFAULT_CAMERA); + editor.scene.background.setHex(0xaaaaaa); + editor.scene.fog = null; + + var objects = editor.scene.children; + + while (objects.length > 0) { + editor.removeObject(objects[0]); + } + + editor.geometries = {}; + editor.materials = {}; + editor.textures = {}; + editor.scripts = {}; + + editor.deselect(); + + editor.signals.editorCleared.dispatch(); +}; + +export default ClearEvent; \ No newline at end of file diff --git a/src/event/editor/LoadEvent.js b/src/event/editor/LoadEvent.js new file mode 100644 index 00000000..00032f12 --- /dev/null +++ b/src/event/editor/LoadEvent.js @@ -0,0 +1,29 @@ +import BaseEvent from '../BaseEvent'; + +/** + * 加载场景事件 + * @param {*} app + */ +function LoadEvent(app) { + BaseEvent.call(this, app); +} + +LoadEvent.prototype = Object.create(BaseEvent.prototype); +LoadEvent.prototype.constructor = LoadEvent; + +LoadEvent.prototype.start = function () { + var _this = this; + this.app.on('load.' + this.id, function () { + _this.onLoad(); + }); +}; + +LoadEvent.prototype.stop = function () { + this.app.on('load.' + this.id, null); +}; + +LoadEvent.prototype.onLoad = function () { + alert('加载场景成功!'); +}; + +export default LoadEvent; \ No newline at end of file diff --git a/src/event/editor/SaveEvent.js b/src/event/editor/SaveEvent.js new file mode 100644 index 00000000..40182f0d --- /dev/null +++ b/src/event/editor/SaveEvent.js @@ -0,0 +1,40 @@ +import BaseEvent from '../BaseEvent'; +import SceneUtils from '../../utils/SceneUtils'; +import Ajax from '../../utils/Ajax'; + +/** + * 保存场景事件 + * @param {*} app + */ +function SaveEvent(app) { + BaseEvent.call(this, app); +} + +SaveEvent.prototype = Object.create(BaseEvent.prototype); +SaveEvent.prototype.constructor = SaveEvent; + +SaveEvent.prototype.start = function () { + var _this = this; + this.app.on('save.' + this.id, function () { + _this.onSave(); + }); +}; + +SaveEvent.prototype.stop = function () { + this.app.on('save.' + this.id, null); +}; + +SaveEvent.prototype.onSave = function () { + var editor = this.app.editor; + + var obj = SceneUtils.toJSON(editor.scene); + Ajax.post(this.app.options.server + '/Service/SceneService.ashx?cmd=Save', { + name: 'Scene1', + data: JSON.stringify(obj) + }, function (result) { + var obj = JSON.parse(result); + alert(obj.Msg); + }); +}; + +export default SaveEvent; \ No newline at end of file diff --git a/src/event/editor/SelectEvent.js b/src/event/editor/SelectEvent.js new file mode 100644 index 00000000..e514d039 --- /dev/null +++ b/src/event/editor/SelectEvent.js @@ -0,0 +1,42 @@ +import BaseEvent from '../BaseEvent'; + +/** + * 选中事件 + * @param {*} app + */ +function SelectEvent(app) { + BaseEvent.call(this, app); +} + +SelectEvent.prototype = Object.create(BaseEvent.prototype); +SelectEvent.prototype.constructor = SelectEvent; + +SelectEvent.prototype.start = function () { + var _this = this; + this.app.on('select.' + this.id, function (object) { + _this.onSelect(object); + }); +}; + +SelectEvent.prototype.stop = function () { + this.app.on('select.' + this.id, null); +}; + +SelectEvent.prototype.onSelect = function (object) { + var editor = this.app.editor; + + if (editor.selected === object) return; + + var uuid = null; + + if (object !== null) { + uuid = object.uuid; + } + + editor.selected = object; + + editor.config.setKey('selected', uuid); + editor.signals.objectSelected.dispatch(object); +}; + +export default SelectEvent; \ No newline at end of file