ShadowEditor/ShadowEditor.Web/src/command/MoveObjectCommand.js
2019-01-17 20:30:21 +08:00

95 lines
2.5 KiB
JavaScript

import Command from './Command';
/**
* 移动物体命令
* @author dforrer / https://github.com/dforrer
* Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
* @param object THREE.Object3D
* @param newParent THREE.Object3D
* @param newBefore THREE.Object3D
* @constructor
*/
function MoveObjectCommand(object, newParent, newBefore) {
Command.call(this);
this.type = 'MoveObjectCommand';
this.name = L_MOVE_OBJECT;
this.object = object;
this.oldParent = (object !== undefined) ? object.parent : undefined;
this.oldIndex = (this.oldParent !== undefined) ? this.oldParent.children.indexOf(this.object) : undefined;
this.newParent = newParent;
if (newBefore !== undefined) {
this.newIndex = (newParent !== undefined) ? newParent.children.indexOf(newBefore) : undefined;
} else {
this.newIndex = (newParent !== undefined) ? newParent.children.length : undefined;
}
if (this.oldParent === this.newParent && this.newIndex > this.oldIndex) {
this.newIndex--;
}
this.newBefore = newBefore;
};
MoveObjectCommand.prototype = Object.create(Command.prototype);
Object.assign(MoveObjectCommand.prototype, {
constructor: MoveObjectCommand,
execute: function () {
this.oldParent.remove(this.object);
var children = this.newParent.children;
children.splice(this.newIndex, 0, this.object);
this.object.parent = this.newParent;
this.editor.app.call('sceneGraphChanged', this);
},
undo: function () {
this.newParent.remove(this.object);
var children = this.oldParent.children;
children.splice(this.oldIndex, 0, this.object);
this.object.parent = this.oldParent;
this.editor.app.call('sceneGraphChanged', this);
},
toJSON: function () {
var output = Command.prototype.toJSON.call(this);
output.objectUuid = this.object.uuid;
output.newParentUuid = this.newParent.uuid;
output.oldParentUuid = this.oldParent.uuid;
output.newIndex = this.newIndex;
output.oldIndex = this.oldIndex;
return output;
},
fromJSON: function (json) {
Command.prototype.fromJSON.call(this, json);
this.object = this.editor.objectByUuid(json.objectUuid);
this.oldParent = this.editor.objectByUuid(json.oldParentUuid);
if (this.oldParent === undefined) {
this.oldParent = this.editor.scene;
}
this.newParent = this.editor.objectByUuid(json.newParentUuid);
if (this.newParent === undefined) {
this.newParent = this.editor.scene;
}
this.newIndex = json.newIndex;
this.oldIndex = json.oldIndex;
}
});
export default MoveObjectCommand;