Sidebar.Script.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. var scripts = editor.scripts[ object.uuid ];
  33. if ( scripts !== undefined ) {
  34. for ( var i = 0; i < scripts.length; i ++ ) {
  35. ( function ( object, script ) {
  36. var name = new UI.Input( script.name ).setWidth( '160px' ).setFontSize( '12px' );
  37. name.onChange( function () {
  38. script.name = this.getValue();
  39. signals.scriptChanged.dispatch();
  40. } );
  41. scriptsContainer.add( name );
  42. var edit = new UI.Button( 'Edit' );
  43. edit.setMarginLeft( '4px' );
  44. edit.onClick( function () {
  45. signals.editScript.dispatch( object, script );
  46. } );
  47. scriptsContainer.add( edit );
  48. var remove = new UI.Button( 'Remove' );
  49. remove.setMarginLeft( '4px' );
  50. remove.onClick( function () {
  51. if ( confirm( 'Are you sure?' ) ) {
  52. editor.removeScript( editor.selected, script );
  53. }
  54. } );
  55. scriptsContainer.add( remove );
  56. scriptsContainer.add( new UI.Break() );
  57. } )( object, scripts[ i ] )
  58. }
  59. }
  60. }
  61. // signals
  62. signals.objectSelected.add( function ( object ) {
  63. if ( object !== null ) {
  64. container.setDisplay( 'block' );
  65. update();
  66. } else {
  67. container.setDisplay( 'none' );
  68. }
  69. } );
  70. signals.scriptAdded.add( update );
  71. signals.scriptRemoved.add( update );
  72. return container;
  73. };