From a6152db71771b935cab5b57e399face9c06b3011 Mon Sep 17 00:00:00 2001 From: Vixelz Date: Thu, 21 Feb 2019 14:23:48 +0000 Subject: [PATCH 1/2] Include global class resources in Resource property inspector Much like how script defined global classes can be created from the "New Resource..." button in the FileSystem panel, this allows the creation of script defined resources to be embedded. --- editor/editor_properties.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index 2b8d5bf6c913..fae4718c3965 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -2287,6 +2287,16 @@ void EditorPropertyResource::_update_menu_items() { E = E->next(); } + List global_classes; + ScriptServer::get_global_class_list(&global_classes); + E = global_classes.front(); + while (E) { + if (EditorNode::get_editor_data().script_class_is_parent(E->get(), base_type)) { + valid_inheritors.insert(E->get()); + } + E = E->next(); + } + for (Set::Element *F = valid_inheritors.front(); F; F = F->next()) { String t = F->get(); @@ -2303,7 +2313,7 @@ void EditorPropertyResource::_update_menu_items() { } } - if (!is_custom_resource && !ClassDB::can_instance(t)) + if (!is_custom_resource && !(ScriptServer::is_global_class(t) || ClassDB::can_instance(t))) continue; inheritors_array.push_back(t); From 2b7b64fb480227ca9aa80eeb611f8d6326a1251f Mon Sep 17 00:00:00 2001 From: Vixelz Date: Thu, 21 Feb 2019 18:14:22 +0000 Subject: [PATCH 2/2] GDScript support for exporting script defined global classes --- editor/editor_data.cpp | 8 ++ editor/editor_data.h | 1 + editor/editor_properties.cpp | 31 ++++-- editor/editor_properties.h | 2 + modules/gdscript/gdscript_parser.cpp | 148 ++++++++++++++++----------- 5 files changed, 127 insertions(+), 63 deletions(-) diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index b87b80dafde9..45958db001c3 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -853,6 +853,14 @@ void EditorData::get_plugin_window_layout(Ref p_layout) { } } +bool EditorData::class_equals_or_inherits(const String &p_class, const String &p_inherits) { + if (p_class == p_inherits) + return true; + if (ScriptServer::is_global_class(p_class)) + return script_class_is_parent(p_class, p_inherits); + return ClassDB::is_parent_class(p_class, p_inherits); +} + bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) { if (!ScriptServer::is_global_class(p_class)) return false; diff --git a/editor/editor_data.h b/editor/editor_data.h index 845878e07004..354bb8a3a8f4 100644 --- a/editor/editor_data.h +++ b/editor/editor_data.h @@ -214,6 +214,7 @@ class EditorData { void notify_edited_scene_changed(); void notify_resource_saved(const Ref &p_resource); + bool class_equals_or_inherits(const String &p_class, const String &p_inherits); bool script_class_is_parent(const String &p_class, const String &p_inherits); StringName script_class_get_base(const String &p_class) const; Object *script_class_instance(const String &p_class); diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index fae4718c3965..ee31d27812ea 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -1994,15 +1994,16 @@ void EditorPropertyResource::_file_selected(const String &p_path) { if (!property_types.empty()) { bool any_type_matches = false; const Vector split_property_types = property_types.split(","); + String res_class = _get_file_script_name_or_default(res); for (int i = 0; i < split_property_types.size(); ++i) { - if (res->is_class(split_property_types[i])) { + if (EditorNode::get_editor_data().class_equals_or_inherits(res_class, split_property_types[i])) { any_type_matches = true; break; } } if (!any_type_matches) - EditorNode::get_singleton()->show_warning(vformat(TTR("The selected resource (%s) does not match any type expected for this property (%s)."), res->get_class(), property_types)); + EditorNode::get_singleton()->show_warning(vformat(TTR("The selected resource (%s) does not match any type expected for this property (%s)."), res_class, property_types)); } emit_changed(get_edited_property(), res); @@ -2022,6 +2023,9 @@ void EditorPropertyResource::_menu_option(int p_which) { } file->set_mode(EditorFileDialog::MODE_OPEN_FILE); String type = base_type; + while (ScriptServer::is_global_class(type)) { + type = ScriptServer::get_global_class_base(type); + } List extensions; for (int i = 0; i < type.get_slice_count(","); i++) { @@ -2579,7 +2583,7 @@ void EditorPropertyResource::update_property() { assign->set_text(res->get_path().get_file()); assign->set_tooltip(res->get_path()); } else { - assign->set_text(res->get_class()); + assign->set_text(_get_file_script_name_or_default(res)); } if (res->get_path().is_resource_file()) { @@ -2712,10 +2716,12 @@ bool EditorPropertyResource::_is_drop_valid(const Dictionary &p_drag_data) const String ftype = EditorFileSystem::get_singleton()->get_file_type(file); if (ftype != "") { + RES res = ResourceLoader::load(file); + ftype = _get_file_script_name_or_default(res); for (int i = 0; i < allowed_type.get_slice_count(","); i++) { String at = allowed_type.get_slice(",", i).strip_edges(); - if (ClassDB::is_parent_class(ftype, at)) { + if (EditorNode::get_editor_data().class_equals_or_inherits(ftype, at)) { return true; } } @@ -2764,6 +2770,20 @@ void EditorPropertyResource::set_use_sub_inspector(bool p_enable) { use_sub_inspector = p_enable; } +String EditorPropertyResource::_get_file_script_name_or_default(const RES &p_resource) const { + Ref