Sidebar.Script.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.addScript( 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. script.name = this.getValue();
  42. signals.scriptChanged.dispatch();
  43. } );
  44. scriptsContainer.add( name );
  45. var edit = new UI.Button( 'Edit' );
  46. edit.setMarginLeft( '4px' );
  47. edit.onClick( function () {
  48. signals.editScript.dispatch( object, script );
  49. } );
  50. scriptsContainer.add( edit );
  51. var remove = new UI.Button( 'Remove' );
  52. remove.setMarginLeft( '4px' );
  53. remove.onClick( function () {
  54. if ( confirm( 'Are you sure?' ) ) {
  55. editor.removeScript( editor.selected, script );
  56. }
  57. } );
  58. scriptsContainer.add( remove );
  59. scriptsContainer.add( new UI.Break() );
  60. } )( object, scripts[ i ] )
  61. }
  62. }
  63. }
  64. // signals
  65. signals.objectSelected.add( function ( object ) {
  66. if ( object !== null ) {
  67. container.setDisplay( 'block' );
  68. update();
  69. } else {
  70. container.setDisplay( 'none' );
  71. }
  72. } );
  73. signals.scriptAdded.add( update );
  74. signals.scriptRemoved.add( update );
  75. return container;
  76. };