format method

dynamic format(
  1. dynamic snippet,
  2. dynamic fromType,
  3. dynamic toType
)

Implementation

format(snippet, fromType, toType) {
  fromType = this.getVectorType(fromType);
  toType = this.getVectorType(toType);

  var typeToType = "${fromType} to ${toType}";

  switch (typeToType) {
    case 'int to float':
      return "${this.getType('float')}( ${snippet} )";
    case 'int to vec2':
      return "${this.getType('vec2')}( ${this.getType('float')}( ${snippet} ) )";
    case 'int to vec3':
      return "${this.getType('vec3')}( ${this.getType('float')}( ${snippet} ) )";
    case 'int to vec4':
      return "${this.getType('vec4')}( ${this.getType('vec3')}( ${this.getType('float')}( ${snippet} ) ), 1.0 )";

    case 'float to int':
      return "${this.getType('int')}( ${snippet} )";
    case 'float to vec2':
      return "${this.getType('vec2')}( ${snippet} )";
    case 'float to vec3':
      return "${this.getType('vec3')}( ${snippet} )";
    case 'float to vec4':
      return "${this.getType('vec4')}( ${this.getType('vec3')}( ${snippet} ), 1.0 )";

    case 'vec2 to int':
      return "${this.getType('int')}( ${snippet}.x )";
    case 'vec2 to float':
      return "${snippet}.x";
    case 'vec2 to vec3':
      return "${this.getType('vec3')}( ${snippet}, 0.0 )";
    case 'vec2 to vec4':
      return "${this.getType('vec4')}( ${snippet}.xy, 0.0, 1.0 )";

    case 'vec3 to int':
      return "${this.getType('int')}( ${snippet}.x )";
    case 'vec3 to float':
      return "${snippet}.x";
    case 'vec3 to vec2':
      return "${snippet}.xy";
    case 'vec3 to vec4':
      return "${this.getType('vec4')}( ${snippet}, 1.0 )";

    case 'vec4 to int':
      return "${this.getType('int')}( ${snippet}.x )";
    case 'vec4 to float':
      return "${snippet}.x";
    case 'vec4 to vec2':
      return "${snippet}.xy";
    case 'vec4 to vec3':
      return "${snippet}.xyz";

    case 'mat3 to int':
      return "${this.getType('int')}( ${snippet} * ${this.getType('vec3')}( 1.0 ) ).x";
    case 'mat3 to float':
      return "( ${snippet} * ${this.getType('vec3')}( 1.0 ) ).x";
    case 'mat3 to vec2':
      return "( ${snippet} * ${this.getType('vec3')}( 1.0 ) ).xy";
    case 'mat3 to vec3':
      return "( ${snippet} * ${this.getType('vec3')}( 1.0 ) ).xyz";
    case 'mat3 to vec4':
      return "${this.getType('vec4')}( ${snippet} * ${this.getType('vec3')}( 1.0 ), 1.0 )";

    case 'mat4 to int':
      return "${this.getType('int')}( ${snippet} * ${this.getType('vec4')}( 1.0 ) ).x";
    case 'mat4 to float':
      return "( ${snippet} * ${this.getType('vec4')}( 1.0 ) ).x";
    case 'mat4 to vec2':
      return "( ${snippet} * ${this.getType('vec4')}( 1.0 ) ).xy";
    case 'mat4 to vec3':
      return "( ${snippet} * ${this.getType('vec4')}( 1.0 ) ).xyz";
    case 'mat4 to vec4':
      return "( ${snippet} * ${this.getType('vec4')}( 1.0 ) )";
  }

  return snippet;
}