Add support for property groups

pull/602/head
Bastiaan Olij 2021-09-15 13:56:20 +10:00
parent 02802b1d0e
commit e839199848
8 changed files with 39 additions and 11 deletions

View File

@ -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_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_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)(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_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. */ 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; } GDNativeInterface;

View File

@ -108,6 +108,8 @@ public:
static MethodBind *bind_method(N p_method_name, M p_method); static MethodBind *bind_method(N p_method_name, M p_method);
template <class M> template <class M>
static MethodBind *bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const std::vector<Variant> &p_default_args = std::vector<Variant>{}, bool p_return_nil_is_variant = true); static MethodBind *bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const std::vector<Variant> &p_default_args = std::vector<Variant>{}, 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_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 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); static void bind_integer_constant(const char *p_class, const char *p_enum, const char *p_name, GDNativeInt p_constant);

View File

@ -43,6 +43,8 @@
#include <vector> #include <vector>
#define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal) #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) #define ADD_PROPERTY(m_property, m_setter, m_getter) ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter)
namespace godot { namespace godot {

View File

@ -52,6 +52,22 @@ MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) {
return method; 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) { 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."); ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class.");
@ -262,10 +278,16 @@ void ClassDB::initialize(GDNativeInitializationLevel p_level) {
property.usage, // DEFAULT //uint32_t usage; property.usage, // DEFAULT //uint32_t usage;
}; };
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; 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<std::string, MethodInfo> pair : cl.signal_map) { for (const std::pair<std::string, MethodInfo> pair : cl.signal_map) {
const MethodInfo &signal = pair.second; const MethodInfo &signal = pair.second;

View File

@ -4,5 +4,5 @@ entry_symbol = "example_library_init"
[libraries] [libraries]
Linux.64 = "bin/x11/libgdexample.so" linux.64 = "bin/x11/libgdexample.so"
Windows.64 = "bin/win64/libgdexample.dll" windows.64 = "bin/win64/libgdexample.dll"

View File

@ -13,9 +13,9 @@ func _ready():
prints("vararg args", $Example.varargs_func("some", "arguments", "to", "test")) prints("vararg args", $Example.varargs_func("some", "arguments", "to", "test"))
# Use properties. # Use properties.
prints("custom postion is", $Example.custom_position) prints("custom position is", $Example.group_subgroup_custom_position)
$Example.custom_position = Vector2(50, 50) $Example.group_subgroup_custom_position = Vector2(50, 50)
prints("custom postion now is", $Example.custom_position) prints("custom position now is", $Example.group_subgroup_custom_position)
# Get constants # Get constants
prints("FIRST", $Example.FIRST) prints("FIRST", $Example.FIRST)

View File

@ -7,9 +7,6 @@ script = ExtResource( "1_c326s" )
[node name="Example" type="Example" parent="."] [node name="Example" type="Example" parent="."]
script = null script = null
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Label" type="Label" parent="Example"] [node name="Label" type="Label" parent="Example"]
offset_left = 194.0 offset_left = 194.0

View File

@ -53,9 +53,12 @@ void Example::_bind_methods() {
} }
// Properties. // 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("get_custom_position"), &Example::get_custom_position);
ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_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. // Signals.
ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value"))); ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));