diff --git a/binding_generator/src/main.rs b/binding_generator/src/main.rs index cd62c7c1..f9a53b59 100644 --- a/binding_generator/src/main.rs +++ b/binding_generator/src/main.rs @@ -33,6 +33,7 @@ struct GodotMethod { is_noscript: bool, is_const: bool, is_virtual: bool, + has_varargs: bool, is_from_script: bool, arguments: Vec } @@ -295,6 +296,13 @@ fn generate_class_header(used_classes: &HashSet<&String>, class: &GodotClass) -> } } + if method.has_varargs { + if method.arguments.len() > 0 { + contents += ", "; + } + contents = contents + "const Array& __var_args = Array()"; + } + contents = contents + ")" + if method.is_const && !class.singleton { " const" } else { "" } + ";\n"; // contents = contents + ")" + if method.is_const { " const" } else { "" } + ";\n"; } @@ -416,6 +424,13 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us } } + if method.has_varargs { + if method.arguments.len() > 0 { + contents += ", "; + } + contents = contents + "const Array& __var_args"; + } + contents = contents + ")" + if method.is_const && !class.singleton { " const" } else { "" } + "\n{\n"; @@ -425,7 +440,7 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us contents = contents + "\t}\n\n"; } - if method.is_virtual { + if method.is_virtual || method.has_varargs { contents = contents + "\tArray __args;\n"; @@ -434,6 +449,12 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us contents = contents + "\t__args.append(" + escape_cpp(&arg.name) + ");\n"; } + if method.has_varargs { + contents = contents + "\tfor (int i = 0; i < __var_args.size(); i++) {\n"; + contents = contents + "\t\t__args.append(__var_args[i]);\n"; + contents = contents + "\t}\n"; + } + contents = contents + "\t"; if method.return_type != "void" { diff --git a/include/godot_cpp/Godot.hpp b/include/godot_cpp/Godot.hpp index 80715b8f..ad8fdfd8 100644 --- a/include/godot_cpp/Godot.hpp +++ b/include/godot_cpp/Godot.hpp @@ -38,6 +38,25 @@ namespace godot { +template +struct _ArgCast { + static T _arg_cast(Variant a) + { + return (T) a; + } +}; + +template +struct _ArgCast { + static T *_arg_cast(Variant a) + { + return (T *) ((Object *) a); + } +}; + + + + template T *as(Object *obj) { @@ -188,7 +207,7 @@ struct _WrappedMethod1 { Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; - *var = (obj->*(method->f))(*arg[0]); + *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0])); return v; } @@ -207,7 +226,7 @@ struct _WrappedMethod1 { Variant **arg = (Variant **) args; - (obj->*(method->f))(*arg[0]); + (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0])); return v; } @@ -253,7 +272,7 @@ struct _WrappedMethod2 { Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; - *var = (obj->*(method->f))(*arg[0], *arg[1]); + *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1])); return v; } @@ -272,7 +291,7 @@ struct _WrappedMethod2 { Variant **arg = (Variant **) args; - (obj->*(method->f))(*arg[0], *arg[1]); + (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1])); return v; } @@ -318,7 +337,7 @@ struct _WrappedMethod3 { Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; - *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2]); + *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2])); return v; } @@ -337,7 +356,7 @@ struct _WrappedMethod3 { Variant **arg = (Variant **) args; - (obj->*(method->f))(*arg[0], *arg[1], *arg[2]); + (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2])); return v; } @@ -382,7 +401,7 @@ struct _WrappedMethod4 { Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; - *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]); + *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3])); return v; } @@ -401,7 +420,7 @@ struct _WrappedMethod4 { Variant **arg = (Variant **) args; - (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]); + (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3])); return v; } @@ -445,7 +464,7 @@ struct _WrappedMethod5 { Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; - *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]); + *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3]), _ArgCast::_arg_cast(*arg[4])); return v; } @@ -464,7 +483,7 @@ struct _WrappedMethod5 { Variant **arg = (Variant **) args; - (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]); + (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3]), _ArgCast::_arg_cast(*arg[4])); return v; } diff --git a/include/godot_cpp/core/NodePath.cpp b/include/godot_cpp/core/NodePath.cpp index 3dbfd4a7..e5e784b4 100644 --- a/include/godot_cpp/core/NodePath.cpp +++ b/include/godot_cpp/core/NodePath.cpp @@ -9,7 +9,15 @@ namespace godot { NodePath::NodePath() { + String from = ""; + godot_node_path_new(&_node_path, (godot_string *) &from); +} +NodePath::NodePath(const NodePath &other) +{ + String from = other; + godot_node_path_new(&_node_path, (godot_string *) &from); + godot_node_path_copy(&_node_path, &other._node_path); } NodePath::NodePath(const String &from) @@ -17,6 +25,12 @@ NodePath::NodePath(const String &from) godot_node_path_new(&_node_path, (godot_string *) &from); } +NodePath::NodePath(const char *contents) +{ + String from = contents; + godot_node_path_new(&_node_path, (godot_string *) &from); +} + String NodePath::get_name(const int idx) const { godot_string str = godot_node_path_get_name(&_node_path, idx); @@ -63,6 +77,11 @@ NodePath::operator String() const return *(String *) &str; } +void NodePath::operator =(const NodePath& other) +{ + godot_node_path_copy(&_node_path, &other._node_path); +} + NodePath::~NodePath() { godot_node_path_destroy(&_node_path); diff --git a/include/godot_cpp/core/NodePath.hpp b/include/godot_cpp/core/NodePath.hpp index c941ad0f..3f75ca17 100644 --- a/include/godot_cpp/core/NodePath.hpp +++ b/include/godot_cpp/core/NodePath.hpp @@ -24,7 +24,11 @@ class GD_CPP_CORE_API NodePath public: NodePath(); - NodePath(const String &from); + NodePath(const NodePath &other); + + NodePath(const String& from); + + NodePath(const char *contents); String get_name(const int idx) const; @@ -42,6 +46,8 @@ public: operator String() const; + void operator =(const NodePath& other); + ~NodePath(); }; diff --git a/include/godot_cpp/core/String.cpp b/include/godot_cpp/core/String.cpp index af83f2e6..9b344c79 100644 --- a/include/godot_cpp/core/String.cpp +++ b/include/godot_cpp/core/String.cpp @@ -1,5 +1,7 @@ #include "String.hpp" +#include "NodePath.hpp" + namespace godot { @@ -119,6 +121,11 @@ bool String::operator >=(const String &s) return !(*this < s); } +String::operator NodePath() const +{ + return NodePath(*this); +} + const wchar_t *String::c_string() const { return godot_string_c_str(&_godot_string); diff --git a/include/godot_cpp/core/String.hpp b/include/godot_cpp/core/String.hpp index 00790ae8..1beaf215 100644 --- a/include/godot_cpp/core/String.hpp +++ b/include/godot_cpp/core/String.hpp @@ -15,6 +15,8 @@ namespace godot { +class NodePath; + class GD_CPP_CORE_API String { godot_string _godot_string; @@ -61,6 +63,8 @@ public: bool operator >=(const String &s); + operator NodePath() const; + const wchar_t *c_string() const; }; diff --git a/include/godot_cpp/core/Variant.cpp b/include/godot_cpp/core/Variant.cpp index 394d97d2..6442bc28 100644 --- a/include/godot_cpp/core/Variant.cpp +++ b/include/godot_cpp/core/Variant.cpp @@ -385,6 +385,10 @@ Variant::operator PoolColorArray() const godot_pool_color_array s = godot_variant_as_pool_color_array(&_godot_variant); return *(PoolColorArray *) &s; } +Variant::operator Object*() const { + godot_object *o = godot_variant_as_object(&_godot_variant); + return (Object *) o; +} Variant::Type Variant::get_type() const { diff --git a/include/godot_cpp/core/Variant.hpp b/include/godot_cpp/core/Variant.hpp index 68e12101..baa1498d 100644 --- a/include/godot_cpp/core/Variant.hpp +++ b/include/godot_cpp/core/Variant.hpp @@ -205,7 +205,7 @@ public: operator NodePath() const; operator RID() const; operator InputEvent() const; - operator Object() const; + operator Object*() const; operator Dictionary() const; operator Array() const;