three.js 场景编辑器 源码解析(十二)

本文深入探讨了RemoveScriptCommand.js的功能,详细介绍了如何通过该命令移除与还原脚本,包括执行、撤销操作及序列化过程。

本章讲解脚本移除命令RemoveScriptCommand.js,这个文件的主要功能就是移除脚本execute、还原脚本undo。

//删除脚本命令
var RemoveScriptCommand = function ( object, script ) {
	//继承自Command,只是调用一下Command方法,将this传递过去
	Command.call( this );
	//命令类型
	this.type = 'RemoveScriptCommand';
	//命令名称
	this.name = 'Remove Script';

	//操作的对象
	this.object = object;
	//需要移除的脚本(保存当前脚本,撤销时需要恢复脚本)
	this.script = script;
	//对象脚本都存在时,找到脚本的索引
	if ( this.object && this.script ) {
		this.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
	}

};

RemoveScriptCommand.prototype = {
	//执行命令
	execute: function () {
		//查找对象的脚本
		if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
		//脚本的索引存在
		if ( this.index !== - 1 ) {
			//移除脚本
			this.editor.scripts[ this.object.uuid ].splice( this.index, 1 );

		}

		this.editor.signals.scriptRemoved.dispatch( this.script );

	},
	//撤销命令
	undo: function () {
		//脚本不存在
		if ( this.editor.scripts[ this.object.uuid ] === undefined ) {
			//建立脚本列表
			this.editor.scripts[ this.object.uuid ] = [];
		}
		//将原来的脚本放入到脚本列表
		this.editor.scripts[ this.object.uuid ].splice( this.index, 0, this.script );
		//更新脚本ui
		this.editor.signals.scriptAdded.dispatch( this.script );

	},
	//序列化
	toJSON: function () {

		var output = Command.prototype.toJSON.call( this );

		output.objectUuid = this.object.uuid;
		output.script = this.script;
		output.index = this.index;

		return output;

	},
	//反序列化
	fromJSON: function ( json ) {

		Command.prototype.fromJSON.call( this, json );

		this.script = json.script;
		this.index = json.index;
		this.object = this.editor.objectByUuid( json.objectUuid );

	}

};

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值