StructNode.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.StructNode = function ( src ) {
  5. THREE.TempNode.call( this);
  6. this.eval( src );
  7. };
  8. THREE.StructNode.rDeclaration = /^struct\s*([a-z_0-9]+)\s*{\s*((.|\n)*?)}/img;
  9. THREE.StructNode.rProperties = /\s*(\w*?)\s*(\w*?)(\=|\;)/img;
  10. THREE.StructNode.prototype = Object.create( THREE.TempNode.prototype );
  11. THREE.StructNode.prototype.constructor = THREE.StructNode;
  12. THREE.StructNode.prototype.nodeType = "Struct";
  13. THREE.StructNode.prototype.getType = function ( builder ) {
  14. return builder.getTypeByFormat( this.name );
  15. };
  16. THREE.StructNode.prototype.getInputByName = function ( name ) {
  17. var i = this.inputs.length;
  18. while ( i -- ) {
  19. if ( this.inputs[ i ].name === name )
  20. return this.inputs[ i ];
  21. }
  22. };
  23. THREE.StructNode.prototype.generate = function ( builder, output ) {
  24. if ( output === 'source' ) {
  25. return this.src + ';';
  26. } else {
  27. return builder.format( "(" + src + ")", this.getType( builder ), output );
  28. }
  29. };
  30. THREE.StructNode.prototype.eval = function ( src ) {
  31. this.src = src || '';
  32. this.inputs = [];
  33. var declaration = THREE.StructNode.rDeclaration.exec( this.src );
  34. if (declaration) {
  35. var properties = declaration[2], matchType, matchName;
  36. while ( matchType = THREE.FunctionNode.rProperties.exec( properties ) ) {
  37. matchName = THREE.FunctionNode.rProperties.exec( properties )[0];
  38. this.inputs.push( {
  39. name: matchName,
  40. type: matchType
  41. } );
  42. }
  43. this.name = declaration[1];
  44. } else {
  45. this.name = '';
  46. }
  47. this.type = this.name;
  48. };
  49. THREE.StructNode.prototype.toJSON = function ( meta ) {
  50. var data = this.getJSONNode( meta );
  51. if ( ! data ) {
  52. data = this.createJSONNode( meta );
  53. data.src = this.src;
  54. }
  55. return data;
  56. };