1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-18 14:21:41 +00:00

Merge pull request #43015 from Xrayez/refactor-auto-instaprop

Refactor auto-instantiation of `Object` properties in editor
This commit is contained in:
Rémi Verschelde
2022-02-08 08:57:29 +01:00
committed by GitHub
6 changed files with 28 additions and 12 deletions

View File

@@ -1632,7 +1632,8 @@ Variant ClassDB::class_get_default_property_value(const StringName &p_class, con
// Some properties may have an instantiated Object as default value, // Some properties may have an instantiated Object as default value,
// (like Path2D's `curve` used to have), but that's not a good practice. // (like Path2D's `curve` used to have), but that's not a good practice.
// Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT
// to be auto-instantiated when created in the editor. // to be auto-instantiated when created in the editor with the following method:
// EditorNode::get_editor_data().instantiate_object_properties(obj);
if (var.get_type() == Variant::OBJECT) { if (var.get_type() == Variant::OBJECT) {
Object *obj = var.get_validated_object(); Object *obj = var.get_validated_object();
if (obj) { if (obj) {

View File

@@ -503,17 +503,7 @@ Variant CreateDialog::instance_selected() {
} else { } else {
obj = ClassDB::instantiate(selected->get_text(0)); obj = ClassDB::instantiate(selected->get_text(0));
} }
EditorNode::get_editor_data().instantiate_object_properties(obj);
// Check if any Object-type property should be instantiated.
List<PropertyInfo> pinfo;
((Object *)obj)->get_property_list(&pinfo);
for (const PropertyInfo &pi : pinfo) {
if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
Object *prop = ClassDB::instantiate(pi.class_name);
((Object *)obj)->set(pi.name, prop);
}
}
return obj; return obj;
} }

View File

@@ -520,6 +520,21 @@ void EditorData::remove_custom_type(const String &p_type) {
} }
} }
void EditorData::instantiate_object_properties(Object *p_object) {
ERR_FAIL_NULL(p_object);
// Check if any Object-type property should be instantiated.
List<PropertyInfo> pinfo;
p_object->get_property_list(&pinfo);
for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
PropertyInfo pi = E->get();
if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
Object *prop = ClassDB::instantiate(pi.class_name);
p_object->set(pi.name, prop);
}
}
}
int EditorData::add_edited_scene(int p_at_pos) { int EditorData::add_edited_scene(int p_at_pos) {
if (p_at_pos < 0) { if (p_at_pos < 0) {
p_at_pos = edited_scene.size(); p_at_pos = edited_scene.size();

View File

@@ -182,6 +182,8 @@ public:
void remove_custom_type(const String &p_type); void remove_custom_type(const String &p_type);
const Map<String, Vector<CustomType>> &get_custom_types() const { return custom_types; } const Map<String, Vector<CustomType>> &get_custom_types() const { return custom_types; }
void instantiate_object_properties(Object *p_object);
int add_edited_scene(int p_at_pos); int add_edited_scene(int p_at_pos);
void move_edited_scene_index(int p_idx, int p_to_idx); void move_edited_scene_index(int p_idx, int p_to_idx);
void remove_scene(int p_idx); void remove_scene(int p_idx);

View File

@@ -377,6 +377,8 @@ void EditorResourcePicker::_edit_menu_cbk(int p_which) {
Resource *resp = Object::cast_to<Resource>(obj); Resource *resp = Object::cast_to<Resource>(obj);
ERR_BREAK(!resp); ERR_BREAK(!resp);
EditorNode::get_editor_data().instantiate_object_properties(obj);
edited_resource = RES(resp); edited_resource = RES(resp);
emit_signal(SNAME("resource_changed"), edited_resource); emit_signal(SNAME("resource_changed"), edited_resource);
_update_resource(); _update_resource();

View File

@@ -269,7 +269,9 @@ void CustomPropertyEditor::_menu_option(int p_which) {
res->call("set_instance_base_type", owner->get_class()); res->call("set_instance_base_type", owner->get_class());
} }
EditorNode::get_editor_data().instantiate_object_properties(obj);
v = obj; v = obj;
emit_signal(SNAME("variant_changed")); emit_signal(SNAME("variant_changed"));
} break; } break;
@@ -1092,7 +1094,9 @@ void CustomPropertyEditor::_type_create_selected(int p_idx) {
ERR_FAIL_COND(!obj); ERR_FAIL_COND(!obj);
ERR_FAIL_COND(!Object::cast_to<Resource>(obj)); ERR_FAIL_COND(!Object::cast_to<Resource>(obj));
EditorNode::get_editor_data().instantiate_object_properties(obj);
v = obj; v = obj;
emit_signal(SNAME("variant_changed")); emit_signal(SNAME("variant_changed"));
hide(); hide();
} }
@@ -1283,7 +1287,9 @@ void CustomPropertyEditor::_action_pressed(int p_which) {
ERR_BREAK(!obj); ERR_BREAK(!obj);
ERR_BREAK(!Object::cast_to<Resource>(obj)); ERR_BREAK(!Object::cast_to<Resource>(obj));
EditorNode::get_editor_data().instantiate_object_properties(obj);
v = obj; v = obj;
emit_signal(SNAME("variant_changed")); emit_signal(SNAME("variant_changed"));
hide(); hide();
} }