2
0

Sidebar.Script.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Sidebar.Script = function ( editor ) {
  5. var signals = editor.signals;
  6. var container = new UI.CollapsiblePanel();
  7. container.setCollapsed( editor.config.getKey( 'ui/sidebar/script/collapsed' ) );
  8. container.onCollapsedChange( function ( boolean ) {
  9. editor.config.setKey( 'ui/sidebar/script/collapsed', boolean );
  10. } );
  11. container.setDisplay( 'none' );
  12. container.addStatic( new UI.Text( 'Script' ).setTextTransform( 'uppercase' ) );
  13. container.add( new UI.Break() );
  14. //
  15. var scriptsContainer = new UI.Panel();
  16. container.add( scriptsContainer );
  17. var newScript = new UI.Button( 'New' );
  18. newScript.onClick( function () {
  19. var script = { name: '', source: 'function update( event ) {}' };
  20. editor.execute( new AddScriptCommand( editor.selected, script ) );
  21. } );
  22. container.add( newScript );
  23. /*
  24. var loadScript = new UI.Button( 'Load' );
  25. loadScript.setMarginLeft( '4px' );
  26. container.add( loadScript );
  27. */
  28. //
  29. function update() {
  30. scriptsContainer.clear();
  31. var object = editor.selected;
  32. if ( object === null ) {
  33. return;
  34. }
  35. var scripts = editor.scripts[ object.uuid ];
  36. if ( scripts !== undefined ) {
  37. for ( var i = 0; i < scripts.length; i ++ ) {
  38. ( function ( object, script ) {
  39. var name = new UI.Input( script.name ).setWidth( '130px' ).setFontSize( '12px' );
  40. name.onChange( function () {
  41. editor.execute( new SetScriptValueCommand( editor.selected, script, 'name', this.getValue() ) );
  42. } );
  43. scriptsContainer.add( name );
  44. var edit = new UI.Button( 'Edit' );
  45. edit.setMarginLeft( '4px' );
  46. edit.onClick( function () {
  47. signals.editScript.dispatch( object, script );
  48. } );
  49. scriptsContainer.add( edit );
  50. var remove = new UI.Button( 'Remove' );
  51. remove.setMarginLeft( '4px' );
  52. remove.onClick( function () {
  53. if ( confirm( 'Are you sure?' ) ) {
  54. editor.execute( new RemoveScriptCommand( editor.selected, script ) );
  55. }
  56. } );
  57. scriptsContainer.add( remove );
  58. scriptsContainer.add( new UI.Break() );
  59. } )( object, scripts[ i ] )
  60. }
  61. }
  62. }
  63. // signals
  64. signals.objectSelected.add( function ( object ) {
  65. if ( object !== null ) {
  66. container.setDisplay( 'block' );
  67. update();
  68. } else {
  69. container.setDisplay( 'none' );
  70. }
  71. } );
  72. signals.scriptAdded.add( update );
  73. signals.scriptRemoved.add( update );
  74. signals.scriptChanged.add( update );
  75. return container;
  76. };