123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- import { BaseNodeEditor } from '../BaseNodeEditor.js';
- import { CodeEditorElement } from '../elements/CodeEditorElement.js';
- import { disposeScene, createElementFromJSON, isGPUNode, onValidType } from '../NodeEditorUtils.js';
- import { global, scriptable, js, scriptableValue } from 'three/nodes';
- import { getColorFromType, setInputAestheticsFromType, setOutputAestheticsFromType } from '../DataTypeLib.js';
- const defaultTitle = 'Scriptable';
- const defaultWidth = 500;
- export class ScriptableEditor extends BaseNodeEditor {
- constructor( source = null, enableEditor = true ) {
- let codeNode = null;
- let scriptableNode = null;
- if ( source && source.isCodeNode ) {
- codeNode = source;
- } else {
- codeNode = js( source || '' );
- }
- scriptableNode = scriptable( codeNode );
- super( defaultTitle, scriptableNode, defaultWidth );
- this.scriptableNode = scriptableNode;
- this.editorCodeNode = codeNode;
- this.editorOutput = null;
- this.editorOutputAdded = null;
- this.layout = null;
- this.editorElement = null;
- this.layoutJSON = '';
- this.initCacheKey = '';
- this.initId = 0;
- this.waitToLayoutJSON = null;
- this.hasInternalEditor = false;
- this._updating = false;
- this.onValidElement = () => {};
- if ( enableEditor ) {
- this.title.setSerializable( true );
- this._initExternalConnection();
- this._toInternal();
- }
- const defaultOutput = this.scriptableNode.getDefaultOutput();
- defaultOutput.events.addEventListener( 'refresh', () => {
- this.update();
- } );
- this.update();
- }
- getColor() {
- const color = getColorFromType( this.layout ? this.layout.outputType : null );
- return color ? color + 'BB' : null;
- }
- hasJSON() {
- return true;
- }
- exportJSON() {
- return this.scriptableNode.toJSON();
- }
- setSource( source ) {
- this.editorCodeNode.code = source;
- this.update();
- return this;
- }
- update( force = false ) {
- if ( this._updating === true ) return;
- this._updating = true;
- this.scriptableNode.codeNode = this.codeNode;
- this.scriptableNode.needsUpdate = true;
- let layout = null;
- let scriptableValueOutput = null;
- try {
- const object = this.scriptableNode.getObject();
- layout = this.scriptableNode.getLayout();
- this.updateLayout( layout, force );
- scriptableValueOutput = this.scriptableNode.getDefaultOutput();
- const initCacheKey = typeof object.init === 'function' ? object.init.toString() : '';
- if ( initCacheKey !== this.initCacheKey ) {
- this.initCacheKey = initCacheKey;
- const initId = ++ this.initId;
- this.scriptableNode.callAsync( 'init' ).then( () => {
- if ( initId === this.initId ) {
- this.update();
- if ( this.editor ) this.editor.tips.message( 'ScriptEditor: Initialized.' );
- }
- } );
- }
- } catch ( e ) {
- console.error( e );
- if ( this.editor ) this.editor.tips.error( e.message );
- }
- const editorOutput = scriptableValueOutput ? scriptableValueOutput.value : null;
- this.value = isGPUNode( editorOutput ) ? this.scriptableNode : scriptableValueOutput;
- this.layout = layout;
- this.editorOutput = editorOutput;
- this.updateOutputInEditor();
- this.updateOutputConnection();
- this.invalidate();
- this._updating = false;
- }
- updateOutputConnection() {
- const layout = this.layout;
- if ( layout ) {
- const outputType = layout.outputType;
- setOutputAestheticsFromType( this.title, outputType );
- } else {
- this.title.setOutput( 0 );
- }
- }
- updateOutputInEditor() {
- const { editor, editorOutput, editorOutputAdded } = this;
- if ( editor && editorOutput === editorOutputAdded ) return;
- const scene = global.get( 'scene' );
- const composer = global.get( 'composer' );
- if ( editor ) {
- if ( editorOutputAdded && editorOutputAdded.isObject3D === true ) {
- editorOutputAdded.removeFromParent();
- disposeScene( editorOutputAdded );
- } else if ( composer && editorOutputAdded && editorOutputAdded.isPass === true ) {
- composer.removePass( editorOutputAdded );
- }
- if ( editorOutput && editorOutput.isObject3D === true ) {
- scene.add( editorOutput );
- } else if ( composer && editorOutput && editorOutput.isPass === true ) {
- composer.addPass( editorOutput );
- }
- this.editorOutputAdded = editorOutput;
- } else {
- if ( editorOutputAdded && editorOutputAdded.isObject3D === true ) {
- editorOutputAdded.removeFromParent();
- disposeScene( editorOutputAdded );
- } else if ( composer && editorOutputAdded && editorOutputAdded.isPass === true ) {
- composer.removePass( editorOutputAdded );
- }
- this.editorOutputAdded = null;
- }
- }
- setEditor( editor ) {
- super.setEditor( editor );
- this.updateOutputInEditor();
- }
- clearParameters() {
- this.layoutJSON = '';
- this.scriptableNode.clearParameters();
- for ( const element of this.elements.concat() ) {
- if ( element !== this.editorElement && element !== this.title ) {
- this.remove( element );
- }
- }
- }
- addElementFromJSON( json ) {
- const { id, element, inputNode, outputType } = createElementFromJSON( json );
- this.add( element );
- this.scriptableNode.setParameter( id, inputNode );
- if ( outputType ) {
- element.setObjectCallback( () => {
- return this.scriptableNode.getOutput( id );
- } );
- }
- //
- const onUpdate = () => {
- const value = element.value;
- const paramValue = value && value.isScriptableValueNode ? value : scriptableValue( value );
- this.scriptableNode.setParameter( id, paramValue );
- this.update();
- };
- element.addEventListener( 'changeInput', onUpdate );
- element.onConnect( onUpdate, true );
- //element.onConnect( () => this.getScriptable().call( 'onDeepChange' ), true );
- return element;
- }
- updateLayout( layout = null, force = false ) {
- const needsUpdateWidth = this.hasExternalEditor || this.editorElement === null;
- if ( this.waitToLayoutJSON !== null ) {
- if ( this.waitToLayoutJSON === JSON.stringify( layout || '{}' ) ) {
- this.waitToLayoutJSON = null;
- if ( needsUpdateWidth ) this.setWidth( layout.width );
- } else {
- return;
- }
- }
- if ( layout ) {
- const layoutCacheKey = JSON.stringify( layout );
- if ( this.layoutJSON !== layoutCacheKey || force === true ) {
- this.clearParameters();
- if ( layout.name ) {
- this.setName( layout.name );
- }
- if ( layout.icon ) {
- this.setIcon( layout.icon );
- }
- if ( needsUpdateWidth ) {
- if ( layout.width !== undefined ) {
- this.setWidth( layout.width );
- } else {
- this.setWidth( defaultWidth );
- }
- }
- if ( layout.elements ) {
- for ( const element of layout.elements ) {
- this.addElementFromJSON( element );
- }
- if ( this.editorElement ) {
- this.remove( this.editorElement );
- this.add( this.editorElement );
- }
- }
- this.layoutJSON = layoutCacheKey;
- }
- } else {
- this.setName( defaultTitle );
- this.setIcon( null );
- this.setWidth( defaultWidth );
- this.clearParameters();
- }
- this.updateOutputConnection();
- }
- get hasExternalEditor() {
- return this.title.getLinkedObject() !== null;
- }
- get codeNode() {
- return this.hasExternalEditor ? this.title.getLinkedObject() : this.editorCodeNode;
- }
- _initExternalConnection() {
- setInputAestheticsFromType(this.title, 'CodeNode' ).onValid( onValidType( 'CodeNode' ) ).onConnect( () => {
- this.hasExternalEditor ? this._toExternal() : this._toInternal();
- this.update();
- }, true );
- }
- _toInternal() {
- if ( this.hasInternalEditor === true ) return;
- if ( this.editorElement === null ) {
- this.editorElement = new CodeEditorElement( this.editorCodeNode.code );
- this.editorElement.addEventListener( 'change', () => {
- this.setSource( this.editorElement.source );
- this.editorElement.focus();
- } );
- this.add( this.editorElement );
- }
- this.setResizable( true );
- this.editorElement.setVisible( true );
- this.hasInternalEditor = true;
- this.update( /*true*/ );
- }
- _toExternal() {
- if ( this.hasInternalEditor === false ) return;
- this.editorElement.setVisible( false );
- this.setResizable( false );
- this.hasInternalEditor = false;
- this.update( /*true*/ );
- }
- serialize( data ) {
- super.serialize( data );
- data.layoutJSON = this.layoutJSON;
- }
- deserialize( data ) {
- this.updateLayout( JSON.parse( data.layoutJSON || '{}' ), true );
- this.waitToLayoutJSON = data.layoutJSON;
- super.deserialize( data );
- }
- }
|