ui.editor.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. UI.ScriptEditor = function () {
  5. UI.Panel.call( this );
  6. var scope = this;
  7. var name = new UI.Input().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  8. if ( scope.onChangeCallback !== undefined ) {
  9. scope.onChangeCallback();
  10. }
  11. } );
  12. this.add( name );
  13. var remove = new UI.Text( 'x' );
  14. remove.setPosition( 'absolute' );
  15. remove.setRight( '8px' );
  16. remove.setCursor( 'pointer' );
  17. remove.onClick( function () {
  18. if ( confirm( 'Are you sure?' ) ) {
  19. scope.parent.remove( scope );
  20. if ( scope.onChangeCallback !== undefined ) {
  21. scope.onChangeCallback();
  22. }
  23. }
  24. } );
  25. this.add( remove );
  26. this.add( new UI.Break() );
  27. var timeout;
  28. var textarea = new UI.TextArea();
  29. textarea.setWidth( '100%' );
  30. textarea.setHeight( '150px' );
  31. textarea.setMarginTop( '8px' );
  32. textarea.onKeyUp( function () {
  33. clearTimeout( timeout );
  34. timeout = setTimeout( function () {
  35. var object = editor.selected;
  36. var source = textarea.getValue();
  37. try {
  38. ( new Function( 'scene', source ).bind( object.clone() ) )( new THREE.Scene() );
  39. textarea.dom.classList.add( 'success' );
  40. textarea.dom.classList.remove( 'fail' );
  41. } catch ( error ) {
  42. console.error( error );
  43. textarea.dom.classList.remove( 'success' );
  44. textarea.dom.classList.add( 'fail' );
  45. return;
  46. }
  47. if ( scope.onChangeCallback !== undefined ) {
  48. scope.onChangeCallback();
  49. }
  50. }, 500 );
  51. } );
  52. this.add( textarea );
  53. this.name = name;
  54. this.textarea = textarea;
  55. };
  56. UI.ScriptEditor.prototype = Object.create( UI.Panel.prototype );
  57. UI.ScriptEditor.prototype.constructor = UI.ScriptEditor;
  58. UI.ScriptEditor.prototype.getValue = function () {
  59. return {
  60. name: this.name.getValue(),
  61. source: this.textarea.getValue()
  62. };
  63. };
  64. UI.ScriptEditor.prototype.setValue = function ( value ) {
  65. this.name.setValue( value.name );
  66. this.textarea.setValue( value.source );
  67. return this;
  68. };
  69. UI.ScriptEditor.prototype.onChange = function ( callback ) {
  70. this.onChangeCallback = callback;
  71. return this;
  72. };