2
0

NodeSwitch.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.NodeSwitch = function( a, component ) {
  5. THREE.NodeGL.call( this, 'fv1' );
  6. this.component = component || 'x';
  7. this.a = a;
  8. };
  9. THREE.NodeSwitch.prototype = Object.create( THREE.NodeGL.prototype );
  10. THREE.NodeSwitch.prototype.constructor = THREE.NodeSwitch;
  11. THREE.NodeSwitch.prototype.getType = function( builder ) {
  12. return builder.getFormatByLength( this.component.length );
  13. };
  14. THREE.NodeSwitch.prototype.generate = function( builder, output ) {
  15. var type = this.a.getType( builder );
  16. var inputLength = builder.getFormatLength( type ) - 1;
  17. var a = this.a.build( builder, type );
  18. var outputLength = 0;
  19. var i, len = this.component.length;
  20. // get max length
  21. for (i = 0; i < len; i++) {
  22. outputLength = Math.max( outputLength, builder.getElementIndex( this.component.charAt(i) ) );
  23. }
  24. if (outputLength > inputLength) outputLength = inputLength;
  25. // build switch
  26. a += '.';
  27. for (i = 0; i < len; i++) {
  28. var elm = this.component.charAt(i);
  29. var idx = builder.getElementIndex( this.component.charAt(i) );
  30. if (idx > outputLength) idx = outputLength;
  31. if (builder.getElementByIndex( idx ) == undefined) {
  32. console.log( builder.getElementByIndex( idx ) );
  33. }
  34. a += builder.getElementByIndex( idx );
  35. }
  36. return builder.format( a, this.type, output );
  37. };