Sidebar.Script.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. var scriptsRow = new UI.Panel();
  15. container.add( scriptsRow );
  16. // source
  17. var timeout;
  18. var scriptSourceRow = new UI.Panel();
  19. var scriptSource = new UI.TextArea( 'javascript' ).setWidth( '240px' ).setHeight( '180px' ).setFontSize( '12px' );
  20. scriptSource.onKeyUp( function () {
  21. clearTimeout( timeout );
  22. timeout = setTimeout( function () {
  23. var object = editor.selected;
  24. var source = scriptSource.getValue();
  25. try {
  26. var script = new Function( 'scene', 'time', source ).bind( object.clone() );
  27. script( new THREE.Scene(), 0 );
  28. scriptSource.dom.classList.add( 'success' );
  29. scriptSource.dom.classList.remove( 'fail' );
  30. } catch ( error ) {
  31. scriptSource.dom.classList.remove( 'success' );
  32. scriptSource.dom.classList.add( 'fail' );
  33. return;
  34. }
  35. object.script = new THREE.Script( source );
  36. editor.signals.objectChanged.dispatch( object );
  37. }, 500 );
  38. } );
  39. scriptSourceRow.add( scriptSource );
  40. container.add( scriptSourceRow );
  41. //
  42. signals.objectSelected.add( function ( object ) {
  43. if ( object !== null ) {
  44. container.setDisplay( 'block' );
  45. if ( object.script !== undefined ) {
  46. scriptSource.setValue( object.script.source );
  47. } else {
  48. scriptSource.setValue( '' );
  49. }
  50. } else {
  51. container.setDisplay( 'none' );
  52. }
  53. } );
  54. return container;
  55. }