编辑菜单中的事件。

This commit is contained in:
liteng 2018-06-21 12:32:11 +08:00
parent 71af39fc1d
commit 808487bd5c
7 changed files with 273 additions and 0 deletions

View File

@ -38,6 +38,13 @@ import LoadSceneEvent from './menu/scene/LoadSceneEvent';
import SaveSceneEvent from './menu/scene/SaveSceneEvent';
import PublishSceneEvent from './menu/scene/PublishSceneEvent';
import UndoEvent from './menu/edit/UndoEvent';
import RedoEvent from './menu/edit/RedoEvent';
import ClearHistoryEvent from './menu/edit/ClearHistoryEvent';
import CloneEvent from './menu/edit/CloneEvent';
import DeleteEvent from './menu/edit/DeleteEvent';
import MinifyShaderEvent from './menu/edit/MinifyShaderEvent';
/**
* 事件执行器
*/
@ -84,6 +91,13 @@ function EventDispatcher(app) {
new LoadSceneEvent(this.app),
new SaveSceneEvent(this.app),
new PublishSceneEvent(this.app),
new UndoEvent(this.app),
new RedoEvent(this.app),
new ClearHistoryEvent(this.app),
new CloneEvent(this.app),
new DeleteEvent(this.app),
new MinifyShaderEvent(this.app),
];
}

View File

@ -0,0 +1,33 @@
import MenuEvent from '../MenuEvent';
/**
* 重做事件
* @param {*} app
*/
function ClearHistoryEvent(app) {
MenuEvent.call(this, app);
}
ClearHistoryEvent.prototype = Object.create(MenuEvent.prototype);
ClearHistoryEvent.prototype.constructor = ClearHistoryEvent;
ClearHistoryEvent.prototype.start = function () {
var _this = this;
this.app.on('mClearHistory.' + this.id, function () {
_this.onClearHistory();
});
};
ClearHistoryEvent.prototype.stop = function () {
this.app.on('mClearHistory.' + this.id, null);
};
ClearHistoryEvent.prototype.onClearHistory = function () {
var editor = this.app.editor;
if (confirm('撤销/重做历史纪录将被清空。确定吗?')) {
editor.history.clear();
}
};
export default ClearHistoryEvent;

View File

@ -0,0 +1,38 @@
import MenuEvent from '../MenuEvent';
import AddObjectCommand from '../../../command/AddObjectCommand';
/**
* 拷贝事件
* @param {*} app
*/
function CloneEvent(app) {
MenuEvent.call(this, app);
}
CloneEvent.prototype = Object.create(MenuEvent.prototype);
CloneEvent.prototype.constructor = CloneEvent;
CloneEvent.prototype.start = function () {
var _this = this;
this.app.on('mClone.' + this.id, function () {
_this.onClone();
});
};
CloneEvent.prototype.stop = function () {
this.app.on('mClone.' + this.id, null);
};
CloneEvent.prototype.onClone = function () {
var editor = this.app.editor;
var object = editor.selected;
if (object.parent === null) return; // avoid cloning the camera or scene
object = object.clone();
editor.execute(new AddObjectCommand(object));
};
export default CloneEvent;

View File

@ -0,0 +1,39 @@
import MenuEvent from '../MenuEvent';
import RemoveObjectCommand from '../../../command/RemoveObjectCommand';
/**
* 删除事件
* @param {*} app
*/
function DeleteEvent(app) {
MenuEvent.call(this, app);
}
DeleteEvent.prototype = Object.create(MenuEvent.prototype);
DeleteEvent.prototype.constructor = DeleteEvent;
DeleteEvent.prototype.start = function () {
var _this = this;
this.app.on('mDelete.' + this.id, function () {
_this.onDelete();
});
};
DeleteEvent.prototype.stop = function () {
this.app.on('mDelete.' + this.id, null);
};
DeleteEvent.prototype.onDelete = function () {
var editor = this.app.editor;
var object = editor.selected;
if (confirm('删除 ' + object.name + '?') === false) return;
var parent = object.parent;
if (parent === undefined) return; // avoid deleting the camera or scene
editor.execute(new RemoveObjectCommand(object));
};
export default DeleteEvent;

View File

@ -0,0 +1,87 @@
import MenuEvent from '../MenuEvent';
import RemoveObjectCommand from '../../../command/RemoveObjectCommand';
import SetMaterialValueCommand from '../../../command/SetMaterialValueCommand';
import MultiCmdsCommand from '../../../command/MultiCmdsCommand';
/**
* 缩小着色器事件
* @param {*} app
*/
function MinifyShaderEvent(app) {
MenuEvent.call(this, app);
}
MinifyShaderEvent.prototype = Object.create(MenuEvent.prototype);
MinifyShaderEvent.prototype.constructor = MinifyShaderEvent;
MinifyShaderEvent.prototype.start = function () {
var _this = this;
this.app.on('mMinifyShader.' + this.id, function () {
_this.onMinifyShader();
});
};
MinifyShaderEvent.prototype.stop = function () {
this.app.on('mMinifyShader.' + this.id, null);
};
MinifyShaderEvent.prototype.onMinifyShader = function () {
var editor = this.app.editor;
var root = editor.selected || editor.scene;
var errors = [];
var nMaterialsChanged = 0;
var path = [];
function getPath(object) {
path.length = 0;
var parent = object.parent;
if (parent !== undefined) getPath(parent);
path.push(object.name || object.uuid);
return path;
}
var cmds = [];
root.traverse(function (object) {
var material = object.material;
if (material instanceof THREE.ShaderMaterial) {
try {
var shader = glslprep.minifyGlsl([
material.vertexShader, material.fragmentShader]);
cmds.push(new SetMaterialValueCommand(object, 'vertexShader', shader[0]));
cmds.push(new SetMaterialValueCommand(object, 'fragmentShader', shader[1]));
++nMaterialsChanged;
} catch (e) {
var path = getPath(object).join("/");
if (e instanceof glslprep.SyntaxError)
errors.push(path + ":" +
e.line + ":" + e.column + ": " + e.message);
else {
errors.push(path +
" 未预料到的错误(详情请见控制台)。");
console.error(e.stack || e);
}
}
}
});
if (nMaterialsChanged > 0) {
editor.execute(new MultiCmdsCommand(cmds), 'Minify Shaders');
}
window.alert(nMaterialsChanged +
"材质已经改变。\n" + errors.join("\n"));
};
export default MinifyShaderEvent;

View File

@ -0,0 +1,31 @@
import MenuEvent from '../MenuEvent';
/**
* 重做事件
* @param {*} app
*/
function RedoEvent(app) {
MenuEvent.call(this, app);
}
RedoEvent.prototype = Object.create(MenuEvent.prototype);
RedoEvent.prototype.constructor = RedoEvent;
RedoEvent.prototype.start = function () {
var _this = this;
this.app.on('mRedo.' + this.id, function () {
_this.onRedo();
});
};
RedoEvent.prototype.stop = function () {
this.app.on('mRedo.' + this.id, null);
};
RedoEvent.prototype.onRedo = function () {
var editor = this.app.editor;
editor.redo();
};
export default RedoEvent;

View File

@ -0,0 +1,31 @@
import MenuEvent from '../MenuEvent';
/**
* 撤销事件
* @param {*} app
*/
function UndoEvent(app) {
MenuEvent.call(this, app);
}
UndoEvent.prototype = Object.create(MenuEvent.prototype);
UndoEvent.prototype.constructor = UndoEvent;
UndoEvent.prototype.start = function () {
var _this = this;
this.app.on('mUndo.' + this.id, function () {
_this.onUndo();
});
};
UndoEvent.prototype.stop = function () {
this.app.on('mUndo.' + this.id, null);
};
UndoEvent.prototype.onUndo = function () {
var editor = this.app.editor;
editor.undo();
};
export default UndoEvent;