Sidebar.Script.Editor.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Sidebar.Script.Editor = function ( editor ) {
  5. var signals = editor.signals;
  6. var timeout;
  7. var scriptSource = new UI.TextArea( 'javascript' ).setWidth( '240px' ).setHeight( '180px' ).setFontSize( '12px' );
  8. scriptSource.onKeyUp( function () {
  9. clearTimeout( timeout );
  10. timeout = setTimeout( function () {
  11. var object = editor.selected;
  12. var source = scriptSource.getValue();
  13. try {
  14. var script = new Function( 'scene', 'time', source ).bind( object.clone() );
  15. script( new THREE.Scene(), 0 );
  16. scriptSource.dom.classList.add( 'success' );
  17. scriptSource.dom.classList.remove( 'fail' );
  18. } catch ( error ) {
  19. scriptSource.dom.classList.remove( 'success' );
  20. scriptSource.dom.classList.add( 'fail' );
  21. return;
  22. }
  23. editor.scripts[ object.uuid ] = source;
  24. editor.signals.objectChanged.dispatch( object );
  25. }, 500 );
  26. } );
  27. signals.objectSelected.add( function ( object ) {
  28. if ( object !== null ) {
  29. var script = editor.scripts[ object.uuid ];
  30. if ( script !== undefined ) {
  31. scriptSource.setValue( script );
  32. } else {
  33. scriptSource.setValue( '' );
  34. }
  35. }
  36. } );
  37. return scriptSource;
  38. }