123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- UI.ScriptEditor = function ( editor ) {
- UI.Panel.call( this );
- var scope = this;
- var name = new UI.Input().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
- if ( scope.onChangeCallback !== undefined ) {
- scope.onChangeCallback();
- }
- } );
- this.add( name );
- var remove = new UI.Text( 'x' );
- remove.setPosition( 'absolute' );
- remove.setRight( '8px' );
- remove.setCursor( 'pointer' );
- remove.onClick( function () {
- if ( confirm( 'Are you sure?' ) ) {
- scope.parent.remove( scope );
- if ( scope.onChangeCallback !== undefined ) {
- scope.onChangeCallback();
- }
- }
- } );
- this.add( remove );
- this.add( new UI.Break() );
- var object = editor.selected.clone();
- var scene = editor.scene.clone();
- var timeout;
- var textarea = new UI.TextArea();
- textarea.setWidth( '100%' );
- textarea.setHeight( '150px' );
- textarea.setMarginTop( '8px' );
- textarea.onKeyUp( function () {
- clearTimeout( timeout );
- textarea.dom.classList.remove( 'success' );
- textarea.dom.classList.remove( 'fail' );
- timeout = setTimeout( function () {
- var source = textarea.getValue();
- try {
- ( 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 );
- textarea.dom.classList.add( 'success' );
- textarea.dom.classList.remove( 'fail' );
- } catch ( error ) {
- console.error( error );
- textarea.dom.classList.remove( 'success' );
- textarea.dom.classList.add( 'fail' );
- return;
- }
- if ( scope.onChangeCallback !== undefined ) {
- scope.onChangeCallback();
- }
- }, 500 );
- } );
- this.add( textarea );
- this.name = name;
- this.textarea = textarea;
- };
- UI.ScriptEditor.prototype = Object.create( UI.Panel.prototype );
- UI.ScriptEditor.prototype.constructor = UI.ScriptEditor;
- UI.ScriptEditor.prototype.getValue = function () {
- return {
- name: this.name.getValue(),
- source: this.textarea.getValue()
- };
- };
- UI.ScriptEditor.prototype.setValue = function ( value ) {
- this.name.setValue( value.name );
- this.textarea.setValue( value.source );
- return this;
- };
- UI.ScriptEditor.prototype.onChange = function ( callback ) {
- this.onChangeCallback = callback;
- return this;
- };
|