本章介绍场景编辑中关于脚本相关的ui部分,相关的文件为editor\js\Sidebar.Script.js,后续的三个章节会逐个介绍脚本相关的三个命令editor\ js\commands\AddScriptCommand.js、editor\js\commands\SetScriptValueCommand.js、editor\js\commands\RemoveScriptCommand.js。
Sidebar.Script.js脚本主要封装了编辑脚本相关的ui,以及ui绑定的事件,ui中包含的控件有新建按钮、名称编辑框、编辑按钮、移除按钮。
//脚本界面
Sidebar.Script = function ( editor ) {
//获取字符串用于设置ui的文本
var strings = editor.strings;
//获取信号
var signals = editor.signals;
//创建一个div,默认不显示(点击对象时才显示)
var container = new UI.Panel();
container.setDisplay( 'none' );
//添加文本、添加两层换行
container.add( new UI.Text( strings.getKey( 'sidebar/script' ) ).setTextTransform( 'uppercase' ) );
container.add( new UI.Break() );
container.add( new UI.Break() );
//
//添加一个脚本的容器(动态添加 文本框、编辑按钮、删除按钮)
var scriptsContainer = new UI.Row();
container.add( scriptsContainer );
//添加一个按钮,设定点击事件
var newScript = new UI.Button( strings.getKey( 'sidebar/script/new' ) );
newScript.onClick( function () {
//传入的脚本内容、执行命令
var script = { name: '', source: 'function update( event ) {}' };
//创建一个添加命令
editor.execute( new AddScriptCommand( editor.selected, script ) );
} );
//添加到容器
container.add( newScript );
/*
var loadScript = new UI.Button( 'Load' );
loadScript.setMarginLeft( '4px' );
container.add( loadScript );
*/
//当添加、编辑、移除脚本时都会触发这个函数
function update() {
//清空脚本编辑的容器
scriptsContainer.clear();
scriptsContainer.setDisplay( 'none' );
//获取选择的对象
var object = editor.selected;
//没有对象就返回
if ( object === null ) {
return;
}
//获取对象的脚本集合
var scripts = editor.scripts[ object.uuid ];
//对象存在脚本
if ( scripts !== undefined && scripts.length > 0 ) {
//显示脚本
scriptsContainer.setDisplay( 'block' );
//遍历该对象的所有脚本
for ( var i = 0; i < scripts.length; i ++ ) {
//立即执行下面的函数
( function ( object, script ) {
//添加一个输入框,脚本名称,设置宽高,字体大小
var name = new UI.Input( script.name ).setWidth( '130px' ).setFontSize( '12px' );
name.onChange( function () { //添加文本框改变事件
//创建一个编辑命名
editor.execute( new SetScriptValueCommand( editor.selected, script, 'name', this.getValue() ) );
} );
//添加文本框
scriptsContainer.add( name );
//添加编辑按钮
var edit = new UI.Button( strings.getKey( 'sidebar/script/edit' ) );
edit.setMarginLeft( '4px' );
edit.onClick( function () { //添加编辑按钮处理
//派发编辑脚本消息
signals.editScript.dispatch( object, script );
} );
//添加编辑按钮
scriptsContainer.add( edit );
//添加移出按钮
var remove = new UI.Button( strings.getKey( 'sidebar/script/remove' ) );
remove.setMarginLeft( '4px' );
remove.onClick( function () { //移除按钮处理
if ( confirm( 'Are you sure?' ) ) { //确定吗
//创建一个移除命令
editor.execute( new RemoveScriptCommand( editor.selected, script ) );
}
} );
// 添加移除按钮
scriptsContainer.add( remove );
//添加换行
scriptsContainer.add( new UI.Break() );
} )( object, scripts[ i ] )
}
}
}
// signals
//对象被选中时的消息处理
signals.objectSelected.add( function ( object ) {
//选中对象、对象不是相机
if ( object !== null && editor.camera !== object ) {
//显示
container.setDisplay( 'block' );
//创建编辑框等
update();
} else {
//相机上不能添加处理事件
container.setDisplay( 'none' );
}
} );
//注册scriptAdded、scriptRemoved、scriptChanged事件响应
signals.scriptAdded.add( update );
signals.scriptRemoved.add( update );
signals.scriptChanged.add( update );
return container;
};