ui.editor.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. UI.ScriptEditor = function ( editor ) {
  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 object = editor.selected.clone();
  28. var scene = editor.scene.clone();
  29. var timeout;
  30. var textarea = new UI.TextArea();
  31. textarea.setWidth( '100%' );
  32. textarea.setHeight( '150px' );
  33. textarea.setMarginTop( '8px' );
  34. textarea.onKeyUp( function () {
  35. clearTimeout( timeout );
  36. textarea.dom.classList.remove( 'success' );
  37. textarea.dom.classList.remove( 'fail' );
  38. timeout = setTimeout( function () {
  39. var source = textarea.getValue();
  40. try {
  41. ( new Function( 'scene', 'keydown', 'keyup', 'mousedown', 'mouseup', 'mousemove', 'update', source + '\nreturn { keydown: keydown, keyup: keyup, mousedown: mousedown, mouseup: mouseup, mousemove: mousemove, update: update };' ).bind( object ) )( scene );
  42. textarea.dom.classList.add( 'success' );
  43. textarea.dom.classList.remove( 'fail' );
  44. } catch ( error ) {
  45. console.error( error );
  46. textarea.dom.classList.remove( 'success' );
  47. textarea.dom.classList.add( 'fail' );
  48. return;
  49. }
  50. if ( scope.onChangeCallback !== undefined ) {
  51. scope.onChangeCallback();
  52. }
  53. }, 500 );
  54. } );
  55. this.add( textarea );
  56. this.name = name;
  57. this.textarea = textarea;
  58. };
  59. UI.ScriptEditor.prototype = Object.create( UI.Panel.prototype );
  60. UI.ScriptEditor.prototype.constructor = UI.ScriptEditor;
  61. UI.ScriptEditor.prototype.getValue = function () {
  62. return {
  63. name: this.name.getValue(),
  64. source: this.textarea.getValue()
  65. };
  66. };
  67. UI.ScriptEditor.prototype.setValue = function ( value ) {
  68. this.name.setValue( value.name );
  69. this.textarea.setValue( value.source );
  70. return this;
  71. };
  72. UI.ScriptEditor.prototype.onChange = function ( callback ) {
  73. this.onChangeCallback = callback;
  74. return this;
  75. };