rokojori-godot/_old-js/cpp-class-member.js

325 lines
7.6 KiB
JavaScript
Raw Normal View History

class CppParameter
{
name = "";
type = "";
}
class CppClassMember
{
name = "";
isMethod = false;
isVirtual = false;
isStatic = false;
2024-05-12 17:02:28 +00:00
isSignal = false;
type = "void";
2024-05-19 15:48:40 +00:00
initialValue = null;
parameters = [];
2024-08-02 06:22:12 +00:00
accessModifier;
2024-08-02 06:22:12 +00:00
constructor( accessModifier, nameDefinition, body )
{
2024-08-02 06:22:12 +00:00
this.accessModifier = accessModifier;
2024-05-19 15:48:40 +00:00
if ( nameDefinition.indexOf( "()" ) != -1)
{
this.isMethod = true;
2024-05-19 15:48:40 +00:00
let typeRegex = /\(\)\s*(?:\:\s*(.+)\s*)?$/;
let result = typeRegex.exec( nameDefinition );
if ( result[ 1 ] )
{
this.type = result[ 1 ];
}
nameDefinition = nameDefinition.replace( typeRegex, "" );
}
let names = nameDefinition.split( /\s+/ );
this.name = names[ names.length -1 ];
for ( let i = 0; i < names.length - 1; i++ )
{
if ( "virtual" === names[ i ] )
{
this.isVirtual = true;
}
if ( "static" === names[ i ] )
{
this.isStatic = true;
}
2024-05-12 17:02:28 +00:00
if ( "signal" === names[ i ] )
{
this.isSignal = true;
}
}
if ( this.isMethod )
{
this.parseMethodBody( body );
}
2024-05-19 15:48:40 +00:00
else
{
this.parseVariableBody( body );
}
}
2024-05-19 15:48:40 +00:00
get isMemberWithInitialValue()
{
2024-05-19 15:48:40 +00:00
return ! this.isMethod && this.initialValue !== null;
}
getMemberInitializer()
{
return `${this.name} = ${this.initialValue};`;
}
parseVariableBody( body )
{
if ( typeof body === "boolean" )
{
2024-05-19 15:48:40 +00:00
this.type = "bool";
this.initialValue = body + "";
return;
}
2024-05-19 15:48:40 +00:00
if ( typeof body === "number" )
{
this.type = "float";
this.initialValue = body + "";
return;
}
let regex = /((?:\w|\<|\>)+)(?:\s*\=\s*(.+))?/;
let result = regex.exec( body );
2024-05-19 15:48:40 +00:00
this.type = result[ 1 ];
this.initialValue = result[ 2 ] || null;
}
2024-05-19 15:48:40 +00:00
parseMethodBody( body )
{
if ( ! body )
{
return;
}
2024-05-19 15:48:40 +00:00
if ( typeof body === "string" )
{
2024-05-19 15:48:40 +00:00
this.type = body;
2024-05-19 15:48:40 +00:00
return;
}
for ( let it in body )
{
if ( ! body.hasOwnProperty( it ) )
{
2024-05-19 15:48:40 +00:00
continue;
}
2024-05-19 15:48:40 +00:00
let cppParameter = new CppParameter();
cppParameter.name = it;
cppParameter.type = body[ it ];
this.parameters.push( cppParameter );
}
2024-05-19 15:48:40 +00:00
}
getMethodBinding( className )
{
2024-05-19 15:48:40 +00:00
if ( ! this.isMethod )
{
let type = stringToVariantType( this.type );
let bindings = [];
bindings.push( `/* ${this.name}: ${this.type} */` );
bindings.push( `ClassDB::bind_method(D_METHOD("get_${this.name}"), &${className}::get_${this.name});` );
bindings.push( `ClassDB::bind_method(D_METHOD("set_${this.name}"), &${className}::set_${this.name});` );
bindings.push( `ADD_PROPERTY(PropertyInfo(Variant::${type}, "${this.name}"), "set_${this.name}", "get_${this.name}");` );
bindings.push( ` ` );
//ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color);
//ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); // Color
return bindings.join( "\n " );
}
2024-05-12 17:02:28 +00:00
if ( this.isSignal )
{
//ADD_SIGNAL(MethodInfo("session_supported", PropertyInfo(Variant::STRING, "session_mode"), PropertyInfo(Variant::BOOL, "supported")));
let parameterInfos = "";
for ( let i = 0; i < this.parameters.length; i++ )
{
let type = stringToVariantType( this.parameters[ i ].type );
let name = this.parameters[ i ].name;
let pi = `, PropertyInfo(Variant::${type}, "${name}")`;
parameterInfos += pi;
}
return `ADD_SIGNAL (MethodInfo( "${this.name}" ${parameterInfos}) );`;
}
else if ( this.isVirtual )
{
2024-05-19 15:48:40 +00:00
let parameterInfos = "";
for ( let i = 0; i < this.parameters.length; i++ )
{
let name = `, "${this.parameters[ i ].name}"`;
parameterInfos += name;
}
return `GDVIRTUAL_BIND( ${this.name}${parameterInfos} );`;
}
else
{
return `ClassDB::bind_method( D_METHOD( "${this.name}" ) , &${className}::${this.name} );`
}
}
getHeaderDefinition()
{
2024-05-12 17:02:28 +00:00
if ( this.isSignal )
{
return `/* signal ${this.name} */`;
}
else if ( this.isMethod )
{
return this.getMethodHeaderDefinition();
}
else
{
return this.getVariableHeaderDefinition();
}
}
getParametersDefinition()
{
return this.parameters.map( p => p.type + " " + p.name ).join( ", " );
}
getMethodHeaderDefinition()
{
if ( this.isVirtual )
{
let numParameters = this.parameters.length;
let methodReturnType = this.type === "void" ? "" : "R";
let returnTypeDefinition = this.type === "void" ? "" : ( this.type + ", " ) ;
let parametersDefinition = "";
if ( numParameters > 0 )
{
parametersDefinition = ", " + this.parameters.map( p => p.type ).join( ", " );
}
return `GDVIRTUAL${numParameters}${methodReturnType}( ${returnTypeDefinition}${this.name}${parametersDefinition} );`
}
return `${this.type} ${this.name}(${this.parametersDefinition});`
}
getVariableHeaderDefinition()
2024-05-19 15:48:40 +00:00
{
return `${this.type} get_${this.name}(); void set_${this.name}( ${this.type} p_${this.name} );`
//return `${this.type} ${this.name};`
}
getPropertyHeaderDefinition()
{
return `${this.type} ${this.name};`
}
2024-05-19 15:48:40 +00:00
getPropertyImplementation( className )
{
let bindings = [];
bindings.push( `/* ${this.name}: ${this.type} */` );
bindings.push( `${this.type} ${className}::get_${this.name}() { return ${this.name}; }` );
bindings.push( `void ${className}::set_${this.name}( ${this.type} p_${this.name} ) { ${this.name} = p_${this.name}; }` );
bindings.push( ` ` );
return bindings.join( "\n" );
}
}
module.exports =
{
CppParameter,
CppClassMember
2024-05-12 17:02:28 +00:00
}
function stringToVariantType( stringType )
{
2024-05-19 15:48:40 +00:00
if ( /^TypedArray\<.+\>$/.test( stringType ) )
{
return "ARRAY";
}
2024-05-12 17:02:28 +00:00
switch( stringType )
{
case "bool": return "BOOL";
case "int": return "INT";
case "float": return "FLOAT";
case "String": return "STRING";
// Math types.
case "Vector2": return "VECTOR2";
case "Vector2i": return "VECTOR2I";
case "Rect2": return "RECT2";
case "Rect2i": return "RECT2I";
case "Transform2D": return "TRANSFORM2D";
case "Vector3": return "VECTOR3";
case "Vector3i": return "VECTOR3I";
case "Vector4": return "VECTOR4";
case "Vector4i": return "VECTOR4I";
case "Plane": return "PLANE";
case "AABB": return "AABB";
case "Quaternion": return "QUATERNION";
case "Basis": return "BASIS";
case "Transform3D": return "TRANSFORM3D";
case "Projection": return "PROJECTION";
// Miscellaneous types.
case "Color": return "COLOR";
case "RID": return "RID";
case "Object": return "OBJECT";
case "Callable": return "CALLABLE";
case "Signal": return "SIGNAL";
case "StringName": return "STRING_NAME";
case "NodePath": return "NODE_PATH";
case "Dictionary": return "DICTIONARY";
case "Array": return "ARRAY";
// Arrays.
case "PackedByteArray": return "PACKED_BYTE_ARRAY";
case "PackedInt32Array": return "PACKED_INT32_ARRAY";
case "PackedInt64Array": return "PACKED_INT64_ARRAY";
case "PackedFloat32Array": return "PACKED_FLOAT32_ARRAY";
case "PackedFloat64Array": return "PACKED_FLOAT64_ARRAY";
case "PackedStringArray": return "PACKED_STRING_ARRAY";
case "PackedVector2Array": return "PACKED_VECTOR2_ARRAY";
case "PackedVector3Array": return "PACKED_VECTOR3_ARRAY";
case "PackedColorArray": return "PACKED_COLOR_ARRAY";
}
2024-05-19 15:48:40 +00:00
console.error( "Type could not be mapped: ", stringType );
return "OBJECT";
}