From 664f36308a6671de21edaac1f60ac9cd462e11de Mon Sep 17 00:00:00 2001 From: kobewi Date: Thu, 9 Dec 2021 18:50:57 +0100 Subject: [PATCH] Add drag and drop for NodePaths (cherry picked from commit acf563e59f14ed650ee04b4a91731bfd3ad61247) --- editor/editor_properties.cpp | 26 ++++++++++++++++++++++++++ editor/editor_properties.h | 4 ++++ editor/scene_tree_dock.cpp | 30 ++++++++++++++++++------------ editor/scene_tree_dock.h | 1 + 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index a5a2bec77f9..9af7d23d1c6 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -2139,6 +2139,29 @@ void EditorPropertyNodePath::_node_clear() { update_property(); } +bool EditorPropertyNodePath::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { + return !is_read_only() && is_drop_valid(p_data); +} + +void EditorPropertyNodePath::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { + ERR_FAIL_COND(!is_drop_valid(p_data)); + Dictionary data = p_data; + Array nodes = data["nodes"]; + Node *node = get_tree()->get_edited_scene_root()->get_node(nodes[0]); + + if (node) { + _node_selected(node->get_path()); + } +} + +bool EditorPropertyNodePath::is_drop_valid(const Dictionary &p_drag_data) const { + if (p_drag_data["type"] != "nodes") { + return false; + } + Array nodes = p_drag_data["nodes"]; + return nodes.size() == 1; +} + void EditorPropertyNodePath::update_property() { NodePath p = get_edited_object()->get(get_edited_property()); @@ -2196,6 +2219,8 @@ void EditorPropertyNodePath::_bind_methods() { ClassDB::bind_method(D_METHOD("_node_selected"), &EditorPropertyNodePath::_node_selected); ClassDB::bind_method(D_METHOD("_node_assign"), &EditorPropertyNodePath::_node_assign); ClassDB::bind_method(D_METHOD("_node_clear"), &EditorPropertyNodePath::_node_clear); + ClassDB::bind_method(D_METHOD("_can_drop_data_fw", "position", "data", "from"), &EditorPropertyNodePath::can_drop_data_fw); + ClassDB::bind_method(D_METHOD("_drop_data_fw", "position", "data", "from"), &EditorPropertyNodePath::drop_data_fw); } EditorPropertyNodePath::EditorPropertyNodePath() { @@ -2206,6 +2231,7 @@ EditorPropertyNodePath::EditorPropertyNodePath() { assign->set_h_size_flags(SIZE_EXPAND_FILL); assign->set_clip_text(true); assign->connect("pressed", this, "_node_assign"); + assign->set_drag_forwarding(this); hbc->add_child(assign); clear = memnew(Button); diff --git a/editor/editor_properties.h b/editor/editor_properties.h index 23f950b9b90..1e6b3cac1f4 100644 --- a/editor/editor_properties.h +++ b/editor/editor_properties.h @@ -522,6 +522,10 @@ class EditorPropertyNodePath : public EditorProperty { void _node_assign(); void _node_clear(); + bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const; + void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from); + bool is_drop_valid(const Dictionary &p_drag_data) const; + protected: static void _bind_methods(); void _notification(int p_what); diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index 9f2dd986ec6..7ddffa8cdfd 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -228,7 +228,7 @@ void SceneTreeDock::_perform_instance_scenes(const Vector &p_files, Node } editor_data->get_undo_redo().commit_action(); - editor->push_item(instances[instances.size() - 1]); + _push_item(instances[instances.size() - 1]); for (int i = 0; i < instances.size(); i++) { emit_signal("node_created", instances[i]); } @@ -748,7 +748,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { editor_data->get_undo_redo().commit_action(); if (dupsingle) { - editor->push_item(dupsingle); + _push_item(dupsingle); } } break; case TOOL_REPARENT: { @@ -853,7 +853,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { mne->add_node(root->get_path_to(E->key())); } - EditorNode::get_singleton()->push_item(mne.ptr()); + _push_item(mne.ptr()); } break; @@ -1172,7 +1172,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) { Object *obj = ObjectDB::get_instance(subresources[idx]); ERR_FAIL_COND(!obj); - editor->push_item(obj); + _push_item(obj); } } } @@ -1362,6 +1362,12 @@ void SceneTreeDock::_script_open_request(const Ref