SwitchNode.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.SwitchNode = function( node, components ) {
  5. THREE.GLNode.call( this );
  6. this.node = node;
  7. this.components = components || 'x';
  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.getFormatFromLength( this.components.length );
  13. };
  14. THREE.SwitchNode.prototype.generate = function( builder, output ) {
  15. var type = this.node.getType( builder );
  16. var inputLength = builder.getFormatLength( type ) - 1;
  17. var node = this.node.build( builder, type );
  18. if ( inputLength > 0 ) {
  19. // get max length
  20. var outputLength = 0;
  21. var components = builder.colorToVector( this.components );
  22. var i, len = components.length;
  23. for ( i = 0; i < len; i ++ ) {
  24. outputLength = Math.max( outputLength, builder.getIndexByElement( components.charAt( i ) ) );
  25. }
  26. if ( outputLength > inputLength ) outputLength = inputLength;
  27. // split
  28. node += '.';
  29. for ( i = 0; i < len; i ++ ) {
  30. var elm = components.charAt( i );
  31. var idx = builder.getIndexByElement( components.charAt( i ) );
  32. if ( idx > outputLength ) idx = outputLength;
  33. node += builder.getElementByIndex( idx );
  34. }
  35. return builder.format( node, this.getType( builder ), output );
  36. } else {
  37. // join
  38. return builder.format( node, type, output )
  39. }
  40. };