ui.editor.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 event = new UI.Text( '' );
  9. this.add( event );
  10. var remove = new UI.Text( 'x' );
  11. remove.setPosition( 'absolute' );
  12. remove.setRight( '8px' );
  13. remove.setCursor( 'pointer' );
  14. remove.onClick( function () {
  15. if ( confirm( 'Are you sure?' ) ) {
  16. scope.parent.remove( scope );
  17. if ( scope.onChangeCallback !== undefined ) {
  18. scope.onChangeCallback();
  19. }
  20. }
  21. } );
  22. this.add( remove );
  23. this.add( new UI.Break() );
  24. var textarea = new UI.TextArea();
  25. textarea.setWidth( '100%' );
  26. textarea.setHeight( '100px' );
  27. textarea.setMarginTop( '8px' );
  28. textarea.onKeyUp( function () {
  29. clearTimeout( timeout );
  30. timeout = setTimeout( function () {
  31. var object = editor.selected;
  32. var source = textarea.getValue();
  33. try {
  34. ( new Function( 'scene', 'event', source ).bind( object.clone() ) )( new THREE.Scene(), {} );
  35. textarea.dom.classList.add( 'success' );
  36. textarea.dom.classList.remove( 'fail' );
  37. } catch ( error ) {
  38. console.error( error );
  39. textarea.dom.classList.remove( 'success' );
  40. textarea.dom.classList.add( 'fail' );
  41. return;
  42. }
  43. if ( scope.onChangeCallback !== undefined ) {
  44. scope.onChangeCallback();
  45. }
  46. }, 500 );
  47. } );
  48. this.add( textarea );
  49. this.event = event;
  50. this.textarea = textarea;
  51. };
  52. UI.ScriptEditor.prototype = Object.create( UI.Panel.prototype );
  53. UI.ScriptEditor.prototype.constructor = UI.ScriptEditor;
  54. UI.ScriptEditor.prototype.getValue = function () {
  55. return { event: this.event.getValue(), source: this.textarea.getValue() };
  56. };
  57. UI.ScriptEditor.prototype.setValue = function ( value ) {
  58. this.event.setValue( value.event );
  59. this.textarea.setValue( value.source );
  60. return this;
  61. };
  62. UI.ScriptEditor.prototype.onChange = function ( callback ) {
  63. this.onChangeCallback = callback;
  64. return this;
  65. };