BaseNode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { Node, ButtonInput, TitleElement, ContextMenu } from '../../libs/flow.module.js';
  2. import { exportJSON } from '../NodeEditorUtils.js';
  3. export const onNodeValidElement = ( inputElement, outputElement ) => {
  4. const outputObject = outputElement.getObject();
  5. if ( ! outputObject || ! outputObject.isNode ) {
  6. return false;
  7. }
  8. };
  9. export class BaseNode extends Node {
  10. constructor( name, outputLength, value = null, width = 300 ) {
  11. super();
  12. const getObjectCallback = ( /*output = null*/ ) => {
  13. return this.value;
  14. };
  15. this.setWidth( width );
  16. const title = new TitleElement( name )
  17. .setObjectCallback( getObjectCallback )
  18. .setSerializable( false )
  19. .setOutput( outputLength );
  20. const contextButton = new ButtonInput().onClick( () => {
  21. context.open();
  22. } ).setIcon( 'ti ti-dots' );
  23. const onAddButtons = () => {
  24. context.removeEventListener( 'show', onAddButtons );
  25. if ( this.value && typeof this.value.toJSON === 'function' ) {
  26. this.context.add( new ButtonInput( 'Export' ).setIcon( 'ti ti-download' ).onClick( () => {
  27. exportJSON( this.value.toJSON(), this.constructor.name );
  28. } ) );
  29. }
  30. context.add( new ButtonInput( 'Remove' ).setIcon( 'ti ti-trash' ).onClick( () => {
  31. this.dispose();
  32. } ) );
  33. };
  34. const context = new ContextMenu( this.dom );
  35. context.addEventListener( 'show', onAddButtons );
  36. this.title = title;
  37. this.contextButton = contextButton;
  38. this.context = context;
  39. title.addButton( contextButton );
  40. this.add( title );
  41. this.setOutputColor( this.getColorValueFromValue( value ) );
  42. this.editor = null;
  43. this.value = value;
  44. this.onValidElement = onNodeValidElement;
  45. }
  46. getColor() {
  47. return ( this.getColorValueFromValue( this.value ) || '#777777' ) + 'BB';
  48. }
  49. serialize( data ) {
  50. super.serialize( data );
  51. delete data.width;
  52. }
  53. deserialize( data ) {
  54. delete data.width;
  55. super.deserialize( data );
  56. }
  57. setEditor( value ) {
  58. this.editor = value;
  59. return this;
  60. }
  61. getColorValueFromValue( value ) {
  62. if ( ! value ) return;
  63. if ( value.isMaterial === true ) {
  64. //return 'forestgreen';
  65. return '#228b22';
  66. } else if ( value.isObject3D === true ) {
  67. return '#ffa500';
  68. } else if ( value.isDataFile === true ) {
  69. return '#00ffff';
  70. }
  71. }
  72. add( element ) {
  73. element.onValid( ( source, target ) => this.onValidElement( source, target ) );
  74. return super.add( element );
  75. }
  76. setName( value ) {
  77. this.title.setTitle( value );
  78. return this;
  79. }
  80. getName() {
  81. return this.title.getTitle();
  82. }
  83. setObjectCallback( callback ) {
  84. this.title.setObjectCallback( callback );
  85. return this;
  86. }
  87. getObject( callback ) {
  88. return this.title.getObject( callback );
  89. }
  90. setColor( color ) {
  91. this.title.setColor( color );
  92. return this;
  93. }
  94. setOutputLength( length ) {
  95. this.title.setOutput( length );
  96. return this;
  97. }
  98. setOutputColor( color ) {
  99. this.title.setOutputColor( color );
  100. return this;
  101. }
  102. invalidate() {
  103. this.title.dispatchEvent( new Event( 'connect' ) );
  104. }
  105. dispose() {
  106. this.context.hide();
  107. super.dispose();
  108. }
  109. }