ui.editor.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. UI.ScriptEditor = function () {
  5. UI.Panel.call( this );
  6. var scope = this;
  7. var timeout;
  8. var textarea = new UI.TextArea();
  9. textarea.setWidth( '240px' );
  10. textarea.setHeight( '100px' );
  11. textarea.onKeyUp( function () {
  12. clearTimeout( timeout );
  13. timeout = setTimeout( function () {
  14. var object = editor.selected;
  15. var source = scope.getValue();
  16. try {
  17. var script = new Function( 'scene', 'time', source ).bind( object.clone() );
  18. script( new THREE.Scene(), 0 );
  19. textarea.dom.classList.add( 'success' );
  20. textarea.dom.classList.remove( 'fail' );
  21. } catch ( error ) {
  22. textarea.dom.classList.remove( 'success' );
  23. textarea.dom.classList.add( 'fail' );
  24. return;
  25. }
  26. if ( scope.onChangeCallback !== undefined ) {
  27. scope.onChangeCallback();
  28. }
  29. }, 500 );
  30. } );
  31. this.add( textarea );
  32. this.textarea = textarea;
  33. };
  34. UI.ScriptEditor.prototype = Object.create( UI.Panel.prototype );
  35. UI.ScriptEditor.prototype.constructor = UI.ScriptEditor;
  36. UI.ScriptEditor.prototype.getValue = function () {
  37. return this.textarea.getValue();
  38. };
  39. UI.ScriptEditor.prototype.setValue = function ( value ) {
  40. this.textarea.setValue( value );
  41. return this;
  42. };
  43. UI.ScriptEditor.prototype.onChange = function ( callback ) {
  44. this.onChangeCallback = callback;
  45. return this;
  46. };