FunctionCallNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from './TempNode.js';
  5. function FunctionCallNode( func, inputs ) {
  6. TempNode.call( this );
  7. this.setFunction( func, inputs );
  8. };
  9. FunctionCallNode.prototype = Object.create( TempNode.prototype );
  10. FunctionCallNode.prototype.constructor = FunctionCallNode;
  11. FunctionCallNode.prototype.nodeType = "FunctionCall";
  12. FunctionCallNode.prototype.setFunction = function ( func, inputs ) {
  13. this.value = func;
  14. this.inputs = inputs || [];
  15. };
  16. FunctionCallNode.prototype.getFunction = function () {
  17. return this.value;
  18. };
  19. FunctionCallNode.prototype.getType = function ( builder ) {
  20. return this.value.getType( builder );
  21. };
  22. FunctionCallNode.prototype.generate = function ( builder, output ) {
  23. var material = builder.material;
  24. var type = this.getType( builder );
  25. var func = this.value;
  26. var code = func.build( builder, output ) + '( ';
  27. var params = [];
  28. for ( var i = 0; i < func.inputs.length; i ++ ) {
  29. var inpt = func.inputs[ i ];
  30. var param = this.inputs[ i ] || this.inputs[ inpt.name ];
  31. params.push( param.build( builder, builder.getTypeByFormat( inpt.type ) ) );
  32. }
  33. code += params.join( ', ' ) + ' )';
  34. return builder.format( code, type, output );
  35. };
  36. FunctionCallNode.prototype.copy = function ( source ) {
  37. TempNode.prototype.copy.call( this, source );
  38. for ( var prop in source.inputs ) {
  39. this.inputs[ prop ] = source.inputs[ prop ];
  40. }
  41. this.value = source.value;
  42. };
  43. FunctionCallNode.prototype.toJSON = function ( meta ) {
  44. var data = this.getJSONNode( meta );
  45. if ( ! data ) {
  46. var func = this.value;
  47. data = this.createJSONNode( meta );
  48. data.value = this.value.toJSON( meta ).uuid;
  49. if ( func.inputs.length ) {
  50. data.inputs = {};
  51. for ( var i = 0; i < func.inputs.length; i ++ ) {
  52. var inpt = func.inputs[ i ];
  53. var node = this.inputs[ i ] || this.inputs[ inpt.name ];
  54. data.inputs[ inpt.name ] = node.toJSON( meta ).uuid;
  55. }
  56. }
  57. }
  58. return data;
  59. };
  60. export { FunctionCallNode };