SwitchNode.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.SwitchNode = function( a, component ) {
  5. THREE.GLNode.call( this, 'fv1' );
  6. this.component = component || 'x';
  7. this.a = a;
  8. };
  9. THREE.SwitchNode.prototype = Object.create( THREE.GLNode.prototype );
  10. THREE.SwitchNode.prototype.constructor = THREE.SwitchNode;
  11. THREE.SwitchNode.prototype.getType = function( builder ) {
  12. return builder.getFormatByLength( this.component.length );
  13. };
  14. THREE.SwitchNode.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.getType( builder ), output );
  37. };