Sidebar.Script.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Sidebar.Script = function ( editor ) {
  5. var signals = editor.signals;
  6. var container = new UI.CollapsiblePanel();
  7. container.setCollapsed( editor.config.getKey( 'ui/sidebar/script/collapsed' ) );
  8. container.onCollapsedChange( function ( boolean ) {
  9. editor.config.setKey( 'ui/sidebar/script/collapsed', boolean );
  10. } );
  11. container.setDisplay( 'none' );
  12. container.addStatic( new UI.Text( 'Script' ).setTextTransform( 'uppercase' ) );
  13. container.add( new UI.Break() );
  14. //
  15. var scriptsContainer = new UI.Panel();
  16. container.add( scriptsContainer );
  17. var newScript = new UI.Button( 'New' );
  18. newScript.setMarginLeft( '5px' );
  19. newScript.onClick( function () {
  20. var script = new UI.ScriptEditor();
  21. script.setValue( { name: '', source: 'return {\n\tupdate: function ( event ) {}\n};' } );
  22. script.onChange( function () {
  23. signals.scriptChanged.dispatch();
  24. } );
  25. scriptsContainer.add( script );
  26. } );
  27. container.add( newScript );
  28. var loadScript = new UI.Button( 'Load' );
  29. loadScript.setMarginLeft( '4px' );
  30. container.add( loadScript );
  31. // signals
  32. signals.objectSelected.add( function ( object ) {
  33. scriptsContainer.clear();
  34. if ( object !== null ) {
  35. container.setDisplay( 'block' );
  36. var sources = editor.scripts[ object.uuid ];
  37. if ( sources !== undefined ) {
  38. for ( var i = 0; i < sources.length; i ++ ) {
  39. var script = new UI.ScriptEditor();
  40. script.setValue( sources[ i ] );
  41. script.onChange( function () {
  42. signals.scriptChanged.dispatch();
  43. } );
  44. scriptsContainer.add( script );
  45. }
  46. }
  47. } else {
  48. container.setDisplay( 'none' );
  49. }
  50. } );
  51. signals.scriptChanged.add( function () {
  52. var array = [];
  53. var object = editor.selected;
  54. for ( var i = 0; i < scriptsContainer.children.length; i ++ ) {
  55. var script = scriptsContainer.children[ i ];
  56. array.push( script.getValue() );
  57. }
  58. editor.scripts[ object.uuid ] = array;
  59. } );
  60. return container;
  61. };