Sidebar.Script.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { UIPanel, UIBreak, UIButton, UIRow, UIInput } from './libs/ui.js';
  2. import { AddScriptCommand } from './commands/AddScriptCommand.js';
  3. import { SetScriptValueCommand } from './commands/SetScriptValueCommand.js';
  4. import { RemoveScriptCommand } from './commands/RemoveScriptCommand.js';
  5. function SidebarScript( editor ) {
  6. const strings = editor.strings;
  7. const signals = editor.signals;
  8. const container = new UIPanel();
  9. container.setBorderTop( '0' );
  10. container.setPaddingTop( '20px' );
  11. container.setDisplay( 'none' );
  12. //
  13. const scriptsContainer = new UIRow();
  14. container.add( scriptsContainer );
  15. const newScript = new UIButton( strings.getKey( 'sidebar/script/new' ) );
  16. newScript.onClick( function () {
  17. const script = { name: '', source: 'function update( event ) {}' };
  18. editor.execute( new AddScriptCommand( editor, editor.selected, script ) );
  19. } );
  20. container.add( newScript );
  21. /*
  22. let 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. const object = editor.selected;
  31. if ( object === null ) {
  32. return;
  33. }
  34. const scripts = editor.scripts[ object.uuid ];
  35. if ( scripts !== undefined && scripts.length > 0 ) {
  36. scriptsContainer.setDisplay( 'block' );
  37. for ( let i = 0; i < scripts.length; i ++ ) {
  38. ( function ( object, script ) {
  39. const name = new UIInput( script.name ).setWidth( '130px' ).setFontSize( '12px' );
  40. name.onChange( function () {
  41. editor.execute( new SetScriptValueCommand( editor, editor.selected, script, 'name', this.getValue() ) );
  42. } );
  43. scriptsContainer.add( name );
  44. const edit = new UIButton( 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. const remove = new UIButton( strings.getKey( 'sidebar/script/remove' ) );
  51. remove.setMarginLeft( '4px' );
  52. remove.onClick( function () {
  53. if ( confirm( strings.getKey( 'prompt/script/remove' ) ) ) {
  54. editor.execute( new RemoveScriptCommand( editor, editor.selected, script ) );
  55. }
  56. } );
  57. scriptsContainer.add( remove );
  58. scriptsContainer.add( new UIBreak() );
  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. }
  77. export { SidebarScript };