Sidebar.Script.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ) {
  36. if ( scripts.length > 0 ) {
  37. scriptsContainer.setDisplay( 'block' );
  38. }
  39. for ( var i = 0; i < scripts.length; i ++ ) {
  40. ( function ( object, script ) {
  41. var name = new UI.Input( script.name ).setWidth( '130px' ).setFontSize( '12px' );
  42. name.onChange( function () {
  43. editor.execute( new SetScriptValueCommand( editor.selected, script, 'name', this.getValue() ) );
  44. } );
  45. scriptsContainer.add( name );
  46. var edit = new UI.Button( strings.getKey( 'sidebar/script/edit' ) );
  47. edit.setMarginLeft( '4px' );
  48. edit.onClick( function () {
  49. signals.editScript.dispatch( object, script );
  50. } );
  51. scriptsContainer.add( edit );
  52. var remove = new UI.Button( strings.getKey( 'sidebar/script/remove' ) );
  53. remove.setMarginLeft( '4px' );
  54. remove.onClick( function () {
  55. if ( confirm( 'Are you sure?' ) ) {
  56. editor.execute( new RemoveScriptCommand( editor.selected, script ) );
  57. }
  58. } );
  59. scriptsContainer.add( remove );
  60. scriptsContainer.add( new UI.Break() );
  61. } )( object, scripts[ i ] )
  62. }
  63. }
  64. }
  65. // signals
  66. signals.objectSelected.add( function ( object ) {
  67. if ( object !== null && editor.camera !== object ) {
  68. container.setDisplay( 'block' );
  69. update();
  70. } else {
  71. container.setDisplay( 'none' );
  72. }
  73. } );
  74. signals.scriptAdded.add( update );
  75. signals.scriptRemoved.add( update );
  76. signals.scriptChanged.add( update );
  77. return container;
  78. };
粤ICP备19079148号