Sidebar.Script.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. editor.scripts[ object.uuid ] = [ 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. var scripts = editor.scripts[ object.uuid ];
  46. if ( scripts !== undefined ) {
  47. scriptSource.setValue( scripts[ 0 ] );
  48. } else {
  49. scriptSource.setValue( '' );
  50. }
  51. } else {
  52. container.setDisplay( 'none' );
  53. }
  54. } );
  55. return container;
  56. }