Sidebar.Script.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. Sidebar.Script = function ( editor ) {
  2. var signals = editor.signals;
  3. var container = new UI.CollapsiblePanel();
  4. container.setCollapsed( editor.config.getKey( 'ui/sidebar/script/collapsed' ) );
  5. container.onCollapsedChange( function ( boolean ) {
  6. editor.config.setKey( 'ui/sidebar/script/collapsed', boolean );
  7. } );
  8. container.setDisplay( 'none' );
  9. container.addStatic( new UI.Text( 'Script' ).setTextTransform( 'uppercase' ) );
  10. container.add( new UI.Break() );
  11. var scriptsRow = new UI.Panel();
  12. container.add( scriptsRow );
  13. // source
  14. var timeout;
  15. var scriptSourceRow = new UI.Panel();
  16. var scriptSource = new UI.TextArea( 'javascript' ).setWidth( '240px' ).setHeight( '180px' ).setFontSize( '12px' );
  17. scriptSource.onKeyUp( function () {
  18. clearTimeout( timeout );
  19. timeout = setTimeout( function () {
  20. var object = editor.selected;
  21. var source = scriptSource.getValue();
  22. try {
  23. var script = new Function( 'scene', 'time', source ).bind( object.clone() );
  24. script( new THREE.Scene(), 0 );
  25. scriptSource.dom.classList.add( 'success' );
  26. scriptSource.dom.classList.remove( 'fail' );
  27. } catch ( error ) {
  28. scriptSource.dom.classList.remove( 'success' );
  29. scriptSource.dom.classList.add( 'fail' );
  30. return;
  31. }
  32. object.script = new THREE.Script( source );
  33. editor.signals.objectChanged.dispatch( object );
  34. }, 500 );
  35. } );
  36. scriptSourceRow.add( scriptSource );
  37. container.add( scriptSourceRow );
  38. //
  39. signals.objectSelected.add( function ( object ) {
  40. if ( object !== null ) {
  41. container.setDisplay( 'block' );
  42. if ( object.script !== undefined ) {
  43. scriptSource.setValue( object.script.source );
  44. } else {
  45. scriptSource.setValue( '' );
  46. }
  47. } else {
  48. container.setDisplay( 'none' );
  49. }
  50. } );
  51. return container;
  52. }