Sidebar.Script.js 2.3 KB

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