Skip to content

Commit

Permalink
Script path type support in editor:
Browse files Browse the repository at this point in the history
Allow script path type hints to be used in drag and drop
and scene tree popup
  • Loading branch information
SaracenOne committed Sep 30, 2023
1 parent 4c3dc26 commit 8927682
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions editor/editor_properties.cpp
Expand Up @@ -2858,6 +2858,14 @@ bool EditorPropertyNodePath::is_drop_valid(const Dictionary &p_drag_data) const
if (dropped_node->is_class(E) ||
EditorNode::get_singleton()->is_object_of_custom_type(dropped_node, E)) {
return true;
} else {
Ref<Script> dropped_node_script = dropped_node->get_script();
while (dropped_node_script.is_valid()) {
if (dropped_node_script->get_path() == E) {
return true;
}
dropped_node_script = dropped_node_script->get_base_script();
}
}
}

Expand Down
44 changes: 42 additions & 2 deletions editor/gui/scene_tree_editor.cpp
Expand Up @@ -498,6 +498,18 @@ void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {
EditorNode::get_singleton()->is_object_of_custom_type(p_node, E)) {
valid = true;
break;
} else {
Ref<Script> node_script = p_node->get_script();
while (node_script.is_valid()) {
if (node_script->get_path() == E) {
valid = true;
break;
}
node_script = node_script->get_base_script();
}
if (valid) {
break;
}
}
}

Expand Down Expand Up @@ -656,6 +668,18 @@ bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_select
EditorNode::get_singleton()->is_object_of_custom_type(n, E)) {
selectable = true;
break;
} else {
Ref<Script> dropped_node_script = n->get_script();
while (dropped_node_script.is_valid()) {
if (dropped_node_script->get_path() == E) {
selectable = true;
break;
}
dropped_node_script = dropped_node_script->get_base_script();
}
if (selectable) {
break;
}
}
}
}
Expand Down Expand Up @@ -1548,16 +1572,32 @@ void SceneTreeDialog::set_valid_types(const Vector<StringName> &p_valid) {
HBoxContainer *hb = memnew(HBoxContainer);
hflow->add_child(hb);

// Attempt to get the correct name and icon for script path types
String type_name = type;
String type_icon = type;
if (ResourceLoader::exists(type, "Script")) {
Ref<Script> script = ResourceLoader::load(type);

Check failure on line 1579 in editor/gui/scene_tree_editor.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

declaration of 'script' shadows a member of 'SceneTreeDialog' [-Werror=shadow]

Check failure on line 1579 in editor/gui/scene_tree_editor.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

declaration of 'script' shadows a member of 'SceneTreeDialog' [-Werror=shadow]
if (script.is_valid()) {
if (script->get_global_name() == "") {
type_name = type_name.get_file();
type_icon = script->get_instance_base_type();
} else {
type_name = script->get_global_name();
type_icon = type_name;
}
}
}

TextureRect *trect = memnew(TextureRect);
hb->add_child(trect);
trect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
trect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
trect->set_meta("type", type);
trect->set_meta("type", type_icon);
valid_type_icons.push_back(trect);

Label *label = memnew(Label);
hb->add_child(label);
label->set_text(type);
label->set_text(type_name);
label->set_auto_translate(false);
}

Expand Down

0 comments on commit 8927682

Please sign in to comment.