rokojori-godot/rokojori-cpp-generator/builds/CppClassMember.js

174 lines
6.6 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CppClassMember = exports.CppParameter = void 0;
const GodotTypes_1 = require("./GodotTypes");
class CppParameter {
constructor() {
this.name = "";
this.type = "";
}
}
exports.CppParameter = CppParameter;
class CppClassMember {
constructor(accessModifier, nameDefinition, body) {
this.name = "";
this.isMethod = false;
this.isVirtual = false;
this.isStatic = false;
this.isSignal = false;
this.type = "void";
this.initialValue = null;
this.parameters = [];
this.accessModifier = "";
this.parametersDefinition = "";
this.accessModifier = accessModifier;
if (nameDefinition.indexOf("()") != -1) {
this.isMethod = true;
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;
}
if ("signal" === names[i]) {
this.isSignal = true;
}
}
if (this.isMethod) {
this.parseMethodBody(body);
}
else {
this.parseVariableBody(body);
}
}
get isMemberWithInitialValue() {
return !this.isMethod && this.initialValue !== null;
}
getMemberInitializer() {
return `${this.name} = ${this.initialValue};`;
}
parseVariableBody(body) {
if (typeof body === "boolean") {
this.type = "bool";
this.initialValue = body + "";
return;
}
if (typeof body === "number") {
this.type = "float";
this.initialValue = body + "";
return;
}
let regex = /((?:\w|\<|\>)+)(?:\s*\=\s*(.+))?/;
let result = regex.exec(body);
this.type = result[1];
this.initialValue = result[2] || null;
}
parseMethodBody(body) {
if (!body) {
return;
}
if (typeof body === "string") {
this.type = body;
return;
}
for (let it in body) {
if (!body.hasOwnProperty(it)) {
continue;
}
let cppParameter = new CppParameter();
cppParameter.name = it;
cppParameter.type = body[it];
this.parameters.push(cppParameter);
}
}
getMethodBinding(className) {
if (!this.isMethod) {
let type = GodotTypes_1.GodotTypes.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 ");
}
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 = GodotTypes_1.GodotTypes.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) {
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() {
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() {
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};`;
}
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");
}
}
exports.CppClassMember = CppClassMember;
//# sourceMappingURL=CppClassMember.js.map