Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drag and drop for NodePaths #55761

Merged
merged 1 commit into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions editor/editor_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2727,6 +2727,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());

Expand Down Expand Up @@ -2781,6 +2804,8 @@ void EditorPropertyNodePath::_notification(int p_what) {
}

void EditorPropertyNodePath::_bind_methods() {
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() {
Expand All @@ -2791,6 +2816,7 @@ EditorPropertyNodePath::EditorPropertyNodePath() {
assign->set_h_size_flags(SIZE_EXPAND_FILL);
assign->set_clip_text(true);
assign->connect("pressed", callable_mp(this, &EditorPropertyNodePath::_node_assign));
assign->set_drag_forwarding(this);
hbc->add_child(assign);

clear = memnew(Button);
Expand Down
4 changes: 4 additions & 0 deletions editor/editor_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,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:
virtual void _set_read_only(bool p_read_only) override;
static void _bind_methods();
Expand Down
30 changes: 18 additions & 12 deletions editor/scene_tree_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void SceneTreeDock::_perform_instantiate_scenes(const Vector<String> &p_files, N
}

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(SNAME("node_created"), instances[i]);
}
Expand Down Expand Up @@ -750,7 +750,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: {
Expand Down Expand Up @@ -854,7 +854,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;

Expand Down Expand Up @@ -1164,7 +1164,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);
}
}
}
Expand Down Expand Up @@ -1368,6 +1368,12 @@ void SceneTreeDock::_script_open_request(const Ref<Script> &p_script) {
editor->edit_resource(p_script);
}

void SceneTreeDock::_push_item(Object *p_object) {
if (!Input::get_singleton()->is_key_pressed(Key::ALT)) {
editor->push_item(p_object);
}
}

void SceneTreeDock::_node_selected() {
Node *node = scene_tree->get_selected();

Expand All @@ -1379,7 +1385,7 @@ void SceneTreeDock::_node_selected() {
restore_script_editor_on_drag = true;
}

editor->push_item(node);
_push_item(node);
}

void SceneTreeDock::_node_renamed() {
Expand Down Expand Up @@ -1950,7 +1956,7 @@ void SceneTreeDock::_script_created(Ref<Script> p_script) {

editor_data->get_undo_redo().commit_action();

editor->push_item(p_script.operator->());
_push_item(p_script.operator->());
_update_script_button();
}

Expand Down Expand Up @@ -2098,7 +2104,7 @@ void SceneTreeDock::_delete_confirm(bool p_cut) {
editor->get_viewport_control()->update();
}

editor->push_item(nullptr);
_push_item(nullptr);

// Fixes the EditorHistory from still offering deleted notes
EditorHistory *editor_history = EditorNode::get_singleton()->get_editor_history();
Expand Down Expand Up @@ -2142,9 +2148,9 @@ void SceneTreeDock::_selection_changed() {
//automatically turn on multi-edit
_tool_selected(TOOL_MULTI_EDIT);
} else if (selection_size == 1) {
editor->push_item(editor_selection->get_selection().front()->key());
_push_item(editor_selection->get_selection().front()->key());
} else if (selection_size == 0) {
editor->push_item(nullptr);
_push_item(nullptr);
}

_update_script_button();
Expand Down Expand Up @@ -2180,7 +2186,7 @@ void SceneTreeDock::_do_create(Node *p_parent) {
}

editor_data->get_undo_redo().commit_action();
editor->push_item(c);
_push_item(c);
editor_selection->clear();
editor_selection->add_node(child);
if (Object::cast_to<Control>(c)) {
Expand Down Expand Up @@ -2338,7 +2344,7 @@ void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_prop
memdelete(default_oldnode);
}

editor->push_item(nullptr);
_push_item(nullptr);

//reconnect signals
List<MethodInfo> sl;
Expand Down Expand Up @@ -2382,7 +2388,7 @@ void SceneTreeDock::replace_node(Node *p_node, Node *p_by_node, bool p_keep_prop
}
newnode->set_name(newname);

editor->push_item(newnode);
_push_item(newnode);

if (p_remove_old) {
memdelete(n);
Expand Down
1 change: 1 addition & 0 deletions editor/scene_tree_dock.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class SceneTreeDock : public VBoxContainer {
void _node_replace_owner(Node *p_base, Node *p_node, Node *p_root, ReplaceOwnerMode p_mode = MODE_BIDI);
void _load_request(const String &p_path);
void _script_open_request(const Ref<Script> &p_script);
void _push_item(Object *p_object);

bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);
bool _track_inherit(const String &p_target_scene_path, Node *p_desired_node);
Expand Down