diff options
| author | David Snopek <dsnopek@gmail.com> | 2024-01-30 11:25:25 -0600 |
|---|---|---|
| committer | David Snopek <dsnopek@gmail.com> | 2024-02-12 13:30:07 -0600 |
| commit | 8fbb7cf79535a2b382d57ce2a094d4cbee316fd1 (patch) | |
| tree | 3b3ea3c4c80911752073ba50920600a348ab81de /include/godot_cpp | |
| parent | 36847f6af0be548bae96429fa84d59f407b51582 (diff) | |
| download | redot-cpp-8fbb7cf79535a2b382d57ce2a094d4cbee316fd1.tar.gz | |
Allow GDExtensions to register virtual methods and call them on scripts
Diffstat (limited to 'include/godot_cpp')
| -rw-r--r-- | include/godot_cpp/classes/wrapped.hpp | 31 | ||||
| -rw-r--r-- | include/godot_cpp/core/class_db.hpp | 3 | ||||
| -rw-r--r-- | include/godot_cpp/core/object.hpp | 2 | ||||
| -rw-r--r-- | include/godot_cpp/core/property_info.hpp | 11 | ||||
| -rw-r--r-- | include/godot_cpp/godot.hpp | 3 |
5 files changed, 50 insertions, 0 deletions
diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 32caf39..25a93df 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -36,6 +36,7 @@ #include <godot_cpp/core/property_info.hpp> #include <godot_cpp/templates/list.hpp> +#include <godot_cpp/templates/vector.hpp> #include <godot_cpp/godot.hpp> @@ -107,6 +108,26 @@ public: GodotObject *_owner = nullptr; }; +_FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr) { +} + +_FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr, const StringName &p_str) { + arr.push_back(p_str); +} + +template <class... P> +_FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr, const StringName &p_str, P... p_args) { + arr.push_back(p_str); + snarray_add_str(arr, p_args...); +} + +template <class... P> +_FORCE_INLINE_ Vector<StringName> snarray(P... p_args) { + Vector<StringName> arr; + snarray_add_str(arr, p_args...); + return arr; +} + namespace internal { GDExtensionPropertyInfo *create_c_property_list(const ::godot::List<::godot::PropertyInfo> &plist_cpp, uint32_t *r_size); @@ -445,4 +466,14 @@ private: // Don't use this for your classes, use GDCLASS() instead. #define GDEXTENSION_CLASS(m_class, m_inherits) GDEXTENSION_CLASS_ALIAS(m_class, m_class, m_inherits) +#define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call<false>(__VA_ARGS__) +#define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<false>(__VA_ARGS__) + +#define GDVIRTUAL_REQUIRED_CALL(m_name, ...) _gdvirtual_##m_name##_call<true>(__VA_ARGS__) +#define GDVIRTUAL_REQUIRED_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<true>(__VA_ARGS__) + +#define GDVIRTUAL_BIND(m_name, ...) ::godot::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), ::godot::snarray(__VA_ARGS__)); +#define GDVIRTUAL_IS_OVERRIDDEN(m_name) _gdvirtual_##m_name##_overridden() +#define GDVIRTUAL_IS_OVERRIDDEN_PTR(m_obj, m_name) m_obj->_gdvirtual_##m_name##_overridden() + #endif // GODOT_WRAPPED_HPP diff --git a/include/godot_cpp/core/class_db.hpp b/include/godot_cpp/core/class_db.hpp index af394f0..e5bccf3 100644 --- a/include/godot_cpp/core/class_db.hpp +++ b/include/godot_cpp/core/class_db.hpp @@ -165,7 +165,10 @@ public: static void add_property(const StringName &p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1); static void add_signal(const StringName &p_class, const MethodInfo &p_signal); static void bind_integer_constant(const StringName &p_class_name, const StringName &p_enum_name, const StringName &p_constant_name, GDExtensionInt p_constant_value, bool p_is_bitfield = false); + // Binds an implementation of a virtual method defined in Godot. static void bind_virtual_method(const StringName &p_class, const StringName &p_method, GDExtensionClassCallVirtual p_call); + // Add a new virtual method that can be implemented by scripts. + static void add_virtual_method(const StringName &p_class, const MethodInfo &p_method, const Vector<StringName> &p_arg_names = Vector<StringName>()); static MethodBind *get_method(const StringName &p_class, const StringName &p_method); diff --git a/include/godot_cpp/core/object.hpp b/include/godot_cpp/core/object.hpp index 79f8fbf..c1f1069 100644 --- a/include/godot_cpp/core/object.hpp +++ b/include/godot_cpp/core/object.hpp @@ -68,6 +68,8 @@ struct MethodInfo { int id = 0; std::vector<PropertyInfo> arguments; std::vector<Variant> default_arguments; + GDExtensionClassMethodArgumentMetadata return_val_metadata; + std::vector<GDExtensionClassMethodArgumentMetadata> arguments_metadata; inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; } inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); } diff --git a/include/godot_cpp/core/property_info.hpp b/include/godot_cpp/core/property_info.hpp index 0ecfa32..f610f3f 100644 --- a/include/godot_cpp/core/property_info.hpp +++ b/include/godot_cpp/core/property_info.hpp @@ -80,6 +80,17 @@ struct PropertyInfo { p_info->usage = usage; *(reinterpret_cast<StringName *>(p_info->class_name)) = class_name; } + + GDExtensionPropertyInfo _to_gdextension() const { + return { + (GDExtensionVariantType)type, + name._native_ptr(), + class_name._native_ptr(), + hint, + hint_string._native_ptr(), + usage, + }; + } }; } // namespace godot diff --git a/include/godot_cpp/godot.hpp b/include/godot_cpp/godot.hpp index c9e9022..1b420d3 100644 --- a/include/godot_cpp/godot.hpp +++ b/include/godot_cpp/godot.hpp @@ -165,6 +165,8 @@ extern "C" GDExtensionInterfaceObjectGetClassName gdextension_interface_object_g extern "C" GDExtensionInterfaceObjectCastTo gdextension_interface_object_cast_to; extern "C" GDExtensionInterfaceObjectGetInstanceFromId gdextension_interface_object_get_instance_from_id; extern "C" GDExtensionInterfaceObjectGetInstanceId gdextension_interface_object_get_instance_id; +extern "C" GDExtensionInterfaceObjectHasScriptMethod gdextension_interface_object_has_script_method; +extern "C" GDExtensionInterfaceObjectCallScriptMethod gdextension_interface_object_call_script_method; extern "C" GDExtensionInterfaceCallableCustomCreate gdextension_interface_callable_custom_create; extern "C" GDExtensionInterfaceCallableCustomGetUserData gdextension_interface_callable_custom_get_userdata; extern "C" GDExtensionInterfaceRefGetObject gdextension_interface_ref_get_object; @@ -177,6 +179,7 @@ extern "C" GDExtensionInterfaceClassdbGetMethodBind gdextension_interface_classd extern "C" GDExtensionInterfaceClassdbGetClassTag gdextension_interface_classdb_get_class_tag; extern "C" GDExtensionInterfaceClassdbRegisterExtensionClass2 gdextension_interface_classdb_register_extension_class2; extern "C" GDExtensionInterfaceClassdbRegisterExtensionClassMethod gdextension_interface_classdb_register_extension_class_method; +extern "C" GDExtensionInterfaceClassdbRegisterExtensionClassVirtualMethod gdextension_interface_classdb_register_extension_class_virtual_method; extern "C" GDExtensionInterfaceClassdbRegisterExtensionClassIntegerConstant gdextension_interface_classdb_register_extension_class_integer_constant; extern "C" GDExtensionInterfaceClassdbRegisterExtensionClassProperty gdextension_interface_classdb_register_extension_class_property; extern "C" GDExtensionInterfaceClassdbRegisterExtensionClassPropertyIndexed gdextension_interface_classdb_register_extension_class_property_indexed; |
