Sidebar.Script.js 2.9 KB

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