2
0

Sidebar.Script.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Sidebar.Script = function ( editor ) {
  5. var strings = editor.strings;
  6. var signals = editor.signals;
  7. var container = new UI.Panel();
  8. container.setDisplay( 'none' );
  9. container.add( new UI.Text( strings.getKey( 'sidebar/script' ) ).setTextTransform( 'uppercase' ) );
  10. container.add( new UI.Break() );
  11. container.add( new UI.Break() );
  12. //
  13. var scriptsContainer = new UI.Row();
  14. container.add( scriptsContainer );
  15. var newScript = new UI.Button( strings.getKey( 'sidebar/script/new' ) );
  16. newScript.onClick( function () {
  17. var script = { name: '', source: 'function update( event ) {}' };
  18. editor.execute( new AddScriptCommand( editor.selected, script ) );
  19. } );
  20. container.add( newScript );
  21. /*
  22. var loadScript = new UI.Button( 'Load' );
  23. loadScript.setMarginLeft( '4px' );
  24. container.add( loadScript );
  25. */
  26. //
  27. function update() {
  28. scriptsContainer.clear();
  29. scriptsContainer.setDisplay( 'none' );
  30. var object = editor.selected;
  31. if ( object === null ) {
  32. return;
  33. }
  34. var scripts = editor.scripts[ object.uuid ];
  35. if ( scripts !== undefined && scripts.length > 0 ) {
  36. scriptsContainer.setDisplay( 'block' );
  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( strings.getKey( 'sidebar/script/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( strings.getKey( 'sidebar/script/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 && editor.camera !== object ) {
  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. };