OperatorNode.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { TempNode } from '../core/TempNode.js';
  2. function OperatorNode( a, b, op ) {
  3. TempNode.call( this );
  4. this.a = a;
  5. this.b = b;
  6. this.op = op;
  7. }
  8. OperatorNode.ADD = '+';
  9. OperatorNode.SUB = '-';
  10. OperatorNode.MUL = '*';
  11. OperatorNode.DIV = '/';
  12. OperatorNode.prototype = Object.create( TempNode.prototype );
  13. OperatorNode.prototype.constructor = OperatorNode;
  14. OperatorNode.prototype.nodeType = 'Operator';
  15. OperatorNode.prototype.getType = function ( builder ) {
  16. var a = this.a.getType( builder ),
  17. b = this.b.getType( builder );
  18. if ( builder.isTypeMatrix( a ) ) {
  19. return 'v4';
  20. } else if ( builder.getTypeLength( b ) > builder.getTypeLength( a ) ) {
  21. // use the greater length vector
  22. return b;
  23. }
  24. return a;
  25. };
  26. OperatorNode.prototype.generate = function ( builder, output ) {
  27. var type = this.getType( builder );
  28. var a = this.a.build( builder, type ),
  29. b = this.b.build( builder, type );
  30. return builder.format( '( ' + a + ' ' + this.op + ' ' + b + ' )', type, output );
  31. };
  32. OperatorNode.prototype.copy = function ( source ) {
  33. TempNode.prototype.copy.call( this, source );
  34. this.a = source.a;
  35. this.b = source.b;
  36. this.op = source.op;
  37. return this;
  38. };
  39. OperatorNode.prototype.toJSON = function ( meta ) {
  40. var data = this.getJSONNode( meta );
  41. if ( ! data ) {
  42. data = this.createJSONNode( meta );
  43. data.a = this.a.toJSON( meta ).uuid;
  44. data.b = this.b.toJSON( meta ).uuid;
  45. data.op = this.op;
  46. }
  47. return data;
  48. };
  49. export { OperatorNode };