本章講解腳本的編輯命令SetScriptValueCommand.js,其中包含的功能主要是老腳本的保存和新腳本的設置,execute函數的功能是將新的腳本作爲當前對象的腳本,undo函數處理的過程是將老的腳本重新替換爲當前對象的當前腳本。每一個腳本的名稱是this.attributeName。
//腳本命令(對象、腳本、腳本名稱、新的值)
var SetScriptValueCommand = function ( object, script, attributeName, newValue ) {
//命令基類
Command.call( this );
//命令類型
this.type = 'SetScriptValueCommand';
//命令描述
this.name = 'Set Script.' + attributeName;
//是否可更新??
this.updatable = true;
//操作的對象、操作的腳本
this.object = object;
this.script = script; //這是一個對象的腳本,一個對象可以有多個腳本
//腳本名稱
this.attributeName = attributeName;
//存儲上次更改的這個腳本
this.oldValue = ( script !== undefined ) ? script[ this.attributeName ] : undefined;
//設置新的腳本
this.newValue = newValue;
};
//腳本原型
SetScriptValueCommand.prototype = {
//執行
execute: function () {
//將新腳本綁定到當前對象的腳本的this.attributeName上
this.script[ this.attributeName ] = this.newValue;
//腳本改變後派發消息
this.editor.signals.scriptChanged.dispatch();
},
//撤銷操作
undo: function () {
//恢復上次的腳本
this.script[ this.attributeName ] = this.oldValue;
//腳本改變時觸發
this.editor.signals.scriptChanged.dispatch();
},
//更新,將原來的腳本更新爲現在的腳本
update: function ( cmd ) {
//更新新的腳本
this.newValue = cmd.newValue;
},
//將腳本信息存儲爲json
toJSON: function () {
//獲取基本信息
var output = Command.prototype.toJSON.call( this );
//添加自身信息
output.objectUuid = this.object.uuid;
//腳本索引
output.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
//腳本名稱
output.attributeName = this.attributeName;
//之前的腳本
output.oldValue = this.oldValue;
//現在的腳本
output.newValue = this.newValue;
//返回信息
return output;
},
//將json文件傳入
fromJSON: function ( json ) {
//將基本信息添加到父類中
Command.prototype.fromJSON.call( this, json );
//複製信息
this.oldValue = json.oldValue;
this.newValue = json.newValue;
this.attributeName = json.attributeName;
this.object = this.editor.objectByUuid( json.objectUuid );
//哪一個對象的哪一個腳本
this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
}
};