Sidebar.Script.js 2.8 KB

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