mirror of
https://github.com/tengge1/ShadowEditor.git
synced 2026-02-01 16:08:17 +00:00
编辑菜单中的事件。
This commit is contained in:
parent
71af39fc1d
commit
808487bd5c
@ -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),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
33
src/event/menu/edit/ClearHistoryEvent.js
Normal file
33
src/event/menu/edit/ClearHistoryEvent.js
Normal 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;
|
||||
38
src/event/menu/edit/CloneEvent.js
Normal file
38
src/event/menu/edit/CloneEvent.js
Normal 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;
|
||||
39
src/event/menu/edit/DeleteEvent.js
Normal file
39
src/event/menu/edit/DeleteEvent.js
Normal 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;
|
||||
87
src/event/menu/edit/MinifyShaderEvent.js
Normal file
87
src/event/menu/edit/MinifyShaderEvent.js
Normal 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;
|
||||
31
src/event/menu/edit/RedoEvent.js
Normal file
31
src/event/menu/edit/RedoEvent.js
Normal 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;
|
||||
31
src/event/menu/edit/UndoEvent.js
Normal file
31
src/event/menu/edit/UndoEvent.js
Normal 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;
|
||||
Loading…
x
Reference in New Issue
Block a user