资源菜单事件全部改完。

This commit is contained in:
liteng 2018-06-22 22:01:19 +08:00
parent 946b3b7b71
commit 481f2aaa90
8 changed files with 249 additions and 1 deletions

View File

@ -65,6 +65,11 @@ import AddAmbientLightEvent from './menu/add/AddAmbientLightEvent';
import AddPerspectiveCameraEvent from './menu/add/AddPerspectiveCameraEvent';
import ImportAssetEvent from './menu/asset/ImportAssetEvent';
import ExportGeometryEvent from './menu/asset/ExportGeometryEvent';
import ExportObjectEvent from './menu/asset/ExportObjectEvent';
import ExportSceneEvent from './menu/asset/ExportSceneEvent';
import ExportOBJEvent from './menu/asset/ExportOBJEvent';
import ExportSTLEvent from './menu/asset/ExportSTLEvent';
/**
* 事件执行器
@ -140,6 +145,11 @@ function EventDispatcher(app) {
new AddPerspectiveCameraEvent(this.app),
new ImportAssetEvent(this.app),
new ExportGeometryEvent(this.app),
new ExportObjectEvent(this.app),
new ExportSceneEvent(this.app),
new ExportOBJEvent(this.app),
new ExportSTLEvent(this.app),
];
}

View File

@ -0,0 +1,55 @@
import MenuEvent from '../MenuEvent';
import StringUtils from '../../../utils/StringUtils';
/**
* 导出几何体事件
* @param {*} app
*/
function ExportGeometryEvent(app) {
MenuEvent.call(this, app);
}
ExportGeometryEvent.prototype = Object.create(MenuEvent.prototype);
ExportGeometryEvent.prototype.constructor = ExportGeometryEvent;
ExportGeometryEvent.prototype.start = function () {
var _this = this;
this.app.on('mExportGeometry.' + this.id, function () {
_this.onExportGeometry();
});
};
ExportGeometryEvent.prototype.stop = function () {
this.app.on('mExportGeometry.' + this.id, null);
};
ExportGeometryEvent.prototype.onExportGeometry = function () {
var editor = this.app.editor;
var object = editor.selected;
if (object === null) {
alert('请选择物体');
return;
}
var geometry = object.geometry;
if (geometry === undefined) {
alert('选中的对象不具有Geometry属性。');
return;
}
var output = geometry.toJSON();
try {
output = JSON.stringify(output, parseNumber, '\t');
output = output.replace(/[\n\t]+([\d\.e\-\[\]]+)/g, '$1');
} catch (e) {
output = JSON.stringify(output);
}
StringUtils.saveString(output, 'geometry.json');
};
export default ExportGeometryEvent;

View File

@ -0,0 +1,40 @@
import MenuEvent from '../MenuEvent';
import StringUtils from '../../../utils/StringUtils';
/**
* 导出obj文件事件
* @param {*} app
*/
function ExportOBJEvent(app) {
MenuEvent.call(this, app);
}
ExportOBJEvent.prototype = Object.create(MenuEvent.prototype);
ExportOBJEvent.prototype.constructor = ExportOBJEvent;
ExportOBJEvent.prototype.start = function () {
var _this = this;
this.app.on('mExportOBJ.' + this.id, function () {
_this.onExportOBJ();
});
};
ExportOBJEvent.prototype.stop = function () {
this.app.on('mExportOBJ.' + this.id, null);
};
ExportOBJEvent.prototype.onExportOBJ = function () {
var editor = this.app.editor;
var object = editor.selected;
if (object === null) {
alert('请选择对象');
return;
}
var exporter = new THREE.OBJExporter();
StringUtils.saveString(exporter.parse(object), 'model.obj');
};
export default ExportOBJEvent;

View File

@ -0,0 +1,48 @@
import MenuEvent from '../MenuEvent';
import StringUtils from '../../../utils/StringUtils';
/**
* 导出物体事件
* @param {*} app
*/
function ExportObjectEvent(app) {
MenuEvent.call(this, app);
}
ExportObjectEvent.prototype = Object.create(MenuEvent.prototype);
ExportObjectEvent.prototype.constructor = ExportObjectEvent;
ExportObjectEvent.prototype.start = function () {
var _this = this;
this.app.on('mExportObject.' + this.id, function () {
_this.onExportObject();
});
};
ExportObjectEvent.prototype.stop = function () {
this.app.on('mExportObject.' + this.id, null);
};
ExportObjectEvent.prototype.onExportObject = function () {
var editor = this.app.editor;
var object = editor.selected;
if (object === null) {
alert('请选择对象');
return;
}
var output = object.toJSON();
try {
output = JSON.stringify(output, parseNumber, '\t');
output = output.replace(/[\n\t]+([\d\.e\-\[\]]+)/g, '$1');
} catch (e) {
output = JSON.stringify(output);
}
StringUtils.saveString(output, 'model.json');
};
export default ExportObjectEvent;

View File

@ -0,0 +1,34 @@
import MenuEvent from '../MenuEvent';
import StringUtils from '../../../utils/StringUtils';
/**
* 导出stl文件事件
* @param {*} app
*/
function ExportSTLEvent(app) {
MenuEvent.call(this, app);
}
ExportSTLEvent.prototype = Object.create(MenuEvent.prototype);
ExportSTLEvent.prototype.constructor = ExportSTLEvent;
ExportSTLEvent.prototype.start = function () {
var _this = this;
this.app.on('mExportSTL.' + this.id, function () {
_this.onExportSTL();
});
};
ExportSTLEvent.prototype.stop = function () {
this.app.on('mExportSTL.' + this.id, null);
};
ExportSTLEvent.prototype.onExportSTL = function () {
var editor = this.app.editor;
var exporter = new THREE.STLExporter();
StringUtils.saveString(exporter.parse(editor.scene), 'model.stl');
};
export default ExportSTLEvent;

View File

@ -0,0 +1,41 @@
import MenuEvent from '../MenuEvent';
import StringUtils from '../../../utils/StringUtils';
/**
* 导出场景事件
* @param {*} app
*/
function ExportSceneEvent(app) {
MenuEvent.call(this, app);
}
ExportSceneEvent.prototype = Object.create(MenuEvent.prototype);
ExportSceneEvent.prototype.constructor = ExportSceneEvent;
ExportSceneEvent.prototype.start = function () {
var _this = this;
this.app.on('mExportScene.' + this.id, function () {
_this.onExportScene();
});
};
ExportSceneEvent.prototype.stop = function () {
this.app.on('mExportScene.' + this.id, null);
};
ExportSceneEvent.prototype.onExportScene = function () {
var editor = this.app.editor;
var output = editor.scene.toJSON();
try {
output = JSON.stringify(output, parseNumber, '\t');
output = output.replace(/[\n\t]+([\d\.e\-\[\]]+)/g, '$1');
} catch (e) {
output = JSON.stringify(output);
}
StringUtils.saveString(output, 'scene.json');
};
export default ExportSceneEvent;

View File

@ -1,5 +1,4 @@
import MenuEvent from '../MenuEvent';
import AddObjectCommand from '../../../command/AddObjectCommand';
/**
* 导入资源事件

21
src/utils/StringUtils.js Normal file
View File

@ -0,0 +1,21 @@
var link = document.createElement('a');
link.style.display = 'none';
document.body.appendChild(link); // Firefox workaround, see #6594
function save(blob, filename) {
link.href = URL.createObjectURL(blob);
link.download = filename || 'data.json';
link.click();
// URL.revokeObjectURL( url ); breaks Firefox...
}
function saveString(text, filename) {
save(new Blob([text], { type: 'text/plain' }), filename);
}
const StringUtils = {
save: save,
saveString: saveString
};
export default StringUtils;