OperatorNode.js 1.5 KB

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