From e839199848cba6f3470a6f836b2b2992e4593104 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Wed, 15 Sep 2021 13:56:20 +1000 Subject: [PATCH] Add support for property groups --- godot-headers-temp/godot/gdnative_interface.h | 2 ++ include/godot_cpp/core/class_db.hpp | 2 ++ include/godot_cpp/core/object.hpp | 2 ++ src/core/class_db.cpp | 26 +++++++++++++++++-- test/demo/example.gdextension | 4 +-- test/demo/main.gd | 6 ++--- test/demo/main.tscn | 3 --- test/src/example.cpp | 5 +++- 8 files changed, 39 insertions(+), 11 deletions(-) diff --git a/godot-headers-temp/godot/gdnative_interface.h b/godot-headers-temp/godot/gdnative_interface.h index 30346f2..13aa273 100644 --- a/godot-headers-temp/godot/gdnative_interface.h +++ b/godot-headers-temp/godot/gdnative_interface.h @@ -438,6 +438,8 @@ typedef struct { void (*classdb_register_extension_class_method)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info); void (*classdb_register_extension_class_integer_constant)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value); void (*classdb_register_extension_class_property)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativePropertyInfo *p_info, const char *p_setter, const char *p_getter); + void (*classdb_register_extension_class_property_group)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_group_name, const char *p_prefix); + void (*classdb_register_extension_class_property_subgroup)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_subgroup_name, const char *p_prefix); void (*classdb_register_extension_class_signal)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count); void (*classdb_unregister_extension_class)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name); /* Unregistering a parent class before a class that inherits it will result in failure. Inheritors must be unregistered first. */ } GDNativeInterface; diff --git a/include/godot_cpp/core/class_db.hpp b/include/godot_cpp/core/class_db.hpp index 23c2cc8..aea95a7 100644 --- a/include/godot_cpp/core/class_db.hpp +++ b/include/godot_cpp/core/class_db.hpp @@ -108,6 +108,8 @@ public: static MethodBind *bind_method(N p_method_name, M p_method); template static MethodBind *bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const std::vector &p_default_args = std::vector{}, bool p_return_nil_is_variant = true); + static void add_property_group(const char *p_class, const char *p_name, const char *p_prefix); + static void add_property_subgroup(const char *p_class, const char *p_name, const char *p_prefix); static void add_property(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index = -1); static void add_signal(const char *p_class, const MethodInfo &p_signal); static void bind_integer_constant(const char *p_class, const char *p_enum, const char *p_name, GDNativeInt p_constant); diff --git a/include/godot_cpp/core/object.hpp b/include/godot_cpp/core/object.hpp index 61cedcb..dafe809 100644 --- a/include/godot_cpp/core/object.hpp +++ b/include/godot_cpp/core/object.hpp @@ -43,6 +43,8 @@ #include #define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal) +#define ADD_GROUP(m_name, m_prefix) ::ClassDB::add_property_group(get_class_static(), m_name, m_prefix) +#define ADD_SUBGROUP(m_name, m_prefix) ::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix) #define ADD_PROPERTY(m_property, m_setter, m_getter) ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter) namespace godot { diff --git a/src/core/class_db.cpp b/src/core/class_db.cpp index a1265da..5151080 100644 --- a/src/core/class_db.cpp +++ b/src/core/class_db.cpp @@ -52,6 +52,22 @@ MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) { return method; } +void ClassDB::add_property_group(const char *p_class, const char *p_name, const char *p_prefix) { + ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class."); + + ClassInfo &info = classes[p_class]; + + info.property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP)); +} + +void ClassDB::add_property_subgroup(const char *p_class, const char *p_name, const char *p_prefix) { + ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class."); + + ClassInfo &info = classes[p_class]; + + info.property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_SUBGROUP)); +} + void ClassDB::add_property(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index) { ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class."); @@ -262,9 +278,15 @@ void ClassDB::initialize(GDNativeInitializationLevel p_level) { property.usage, // DEFAULT //uint32_t usage; }; - const PropertySetGet &setget = cl.property_setget.find(property.name)->second; + if (info.usage == PROPERTY_USAGE_GROUP) { + internal::interface->classdb_register_extension_class_property_group(internal::library, cl.name, info.name, info.hint_string); + } else if (info.usage == PROPERTY_USAGE_SUBGROUP) { + internal::interface->classdb_register_extension_class_property_subgroup(internal::library, cl.name, info.name, info.hint_string); + } else { + const PropertySetGet &setget = cl.property_setget.find(property.name)->second; - internal::interface->classdb_register_extension_class_property(internal::library, cl.name, &info, setget.setter, setget.getter); + internal::interface->classdb_register_extension_class_property(internal::library, cl.name, &info, setget.setter, setget.getter); + } } for (const std::pair pair : cl.signal_map) { diff --git a/test/demo/example.gdextension b/test/demo/example.gdextension index d912e0e..5a70e99 100644 --- a/test/demo/example.gdextension +++ b/test/demo/example.gdextension @@ -4,5 +4,5 @@ entry_symbol = "example_library_init" [libraries] -Linux.64 = "bin/x11/libgdexample.so" -Windows.64 = "bin/win64/libgdexample.dll" +linux.64 = "bin/x11/libgdexample.so" +windows.64 = "bin/win64/libgdexample.dll" diff --git a/test/demo/main.gd b/test/demo/main.gd index ca3a7dd..afda267 100644 --- a/test/demo/main.gd +++ b/test/demo/main.gd @@ -13,9 +13,9 @@ func _ready(): prints("vararg args", $Example.varargs_func("some", "arguments", "to", "test")) # Use properties. - prints("custom postion is", $Example.custom_position) - $Example.custom_position = Vector2(50, 50) - prints("custom postion now is", $Example.custom_position) + prints("custom position is", $Example.group_subgroup_custom_position) + $Example.group_subgroup_custom_position = Vector2(50, 50) + prints("custom position now is", $Example.group_subgroup_custom_position) # Get constants prints("FIRST", $Example.FIRST) diff --git a/test/demo/main.tscn b/test/demo/main.tscn index e64674f..4b21141 100644 --- a/test/demo/main.tscn +++ b/test/demo/main.tscn @@ -7,9 +7,6 @@ script = ExtResource( "1_c326s" ) [node name="Example" type="Example" parent="."] script = null -__meta__ = { -"_edit_use_anchors_": false -} [node name="Label" type="Label" parent="Example"] offset_left = 194.0 diff --git a/test/src/example.cpp b/test/src/example.cpp index 7454289..313398d 100644 --- a/test/src/example.cpp +++ b/test/src/example.cpp @@ -53,9 +53,12 @@ void Example::_bind_methods() { } // Properties. + ADD_GROUP("Test group","group_"); + ADD_SUBGROUP("Test subgroup","group_subgroup_"); + ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position); ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "custom_position"), "set_custom_position", "get_custom_position"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position"); // Signals. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));