Skip to content

Commit

Permalink
Merge pull request #67055 from GuilhermeGSousa/custom-node-export
Browse files Browse the repository at this point in the history
Added custom node export
  • Loading branch information
akien-mga committed Oct 31, 2022
2 parents 87545bf + 5d06843 commit f4f98c4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
21 changes: 21 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4392,6 +4392,27 @@ Ref<Texture2D> EditorNode::get_class_icon(const String &p_class, const String &p
return nullptr;
}

bool EditorNode::is_object_of_custom_type(const Object *p_object, const StringName &p_class) {
ERR_FAIL_COND_V(!p_object, false);

Ref<Script> scr = p_object->get_script();
if (scr.is_null() && Object::cast_to<Script>(p_object)) {
scr = p_object;
}

if (scr.is_valid()) {
Ref<Script> base_script = scr;
while (base_script.is_valid()) {
StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
if (name == p_class) {
return true;
}
base_script = base_script->get_base_script();
}
}
return false;
}

void EditorNode::progress_add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
if (singleton->cmdline_export_mode) {
print_line(p_task + ": begin: " + p_label + " steps: " + itos(p_steps));
Expand Down
2 changes: 2 additions & 0 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,8 @@ class EditorNode : public Node {
Ref<Texture2D> get_object_icon(const Object *p_object, const String &p_fallback = "Object");
Ref<Texture2D> get_class_icon(const String &p_class, const String &p_fallback = "Object") const;

bool is_object_of_custom_type(const Object *p_object, const StringName &p_class);

void show_accept(const String &p_text, const String &p_title);
void show_save_accept(const String &p_text, const String &p_title);
void show_warning(const String &p_text, const String &p_title = TTR("Warning!"));
Expand Down
3 changes: 2 additions & 1 deletion editor/editor_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3699,7 +3699,8 @@ bool EditorPropertyNodePath::is_drop_valid(const Dictionary &p_drag_data) const
}

for (const StringName &E : valid_types) {
if (dropped_node->is_class(E)) {
if (dropped_node->is_class(E) ||
EditorNode::get_singleton()->is_object_of_custom_type(dropped_node, E)) {
return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions editor/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,9 @@ void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {

if (valid_types.size()) {
bool valid = false;
for (int i = 0; i < valid_types.size(); i++) {
if (p_node->is_class(valid_types[i])) {
for (const StringName &E : valid_types) {
if (p_node->is_class(E) ||
EditorNode::get_singleton()->is_object_of_custom_type(p_node, E)) {
valid = true;
break;
}
Expand Down
16 changes: 11 additions & 5 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3765,13 +3765,19 @@ bool GDScriptParser::export_annotations(const AnnotationNode *p_annotation, Node
break;
case GDScriptParser::DataType::CLASS:
// Can assume type is a global GDScript class.
if (!ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {
push_error(R"(Exported script type must extend Resource.)");
if (ClassDB::is_parent_class(export_type.native_type, SNAME("Resource"))) {
variable->export_info.type = Variant::OBJECT;
variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;
variable->export_info.hint_string = export_type.class_type->identifier->name;
} else if (ClassDB::is_parent_class(export_type.native_type, SNAME("Node"))) {
variable->export_info.type = Variant::OBJECT;
variable->export_info.hint = PROPERTY_HINT_NODE_TYPE;
variable->export_info.hint_string = export_type.class_type->identifier->name;
} else {
push_error(R"(Export type can only be built-in, a resource, a node or an enum.)", variable);
return false;
}
variable->export_info.type = Variant::OBJECT;
variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;
variable->export_info.hint_string = export_type.class_type->identifier->name;

break;
case GDScriptParser::DataType::SCRIPT: {
StringName class_name;
Expand Down

0 comments on commit f4f98c4

Please sign in to comment.