Fix select the edited node when dropping

This commit is contained in:
AeioMuch 2026-02-20 16:08:30 +01:00
parent 8120fb1242
commit 86e80c6b80
2 changed files with 23 additions and 36 deletions

View file

@ -72,6 +72,7 @@
void SceneTreeDock::_nodes_drag_begin() {
pending_click_select = nullptr;
edited_object_at_drag_start = InspectorDock::get_inspector_singleton()->get_edited_object();
}
void SceneTreeDock::_quick_open(const String &p_file_path) {
@ -96,7 +97,6 @@ void SceneTreeDock::_inspect_hovered_node() {
return;
}
select_node_hovered_at_end_of_drag = true;
TreeItem *item = tree->get_item_with_metadata(node_hovered_now->get_path());
_restore_treeitem_custom_color(tree_item_inspected);
@ -1819,30 +1819,31 @@ void SceneTreeDock::_notification(int p_what) {
if (tree_item_inspected) {
_restore_treeitem_custom_color(tree_item_inspected);
tree_item_inspected = nullptr;
} else {
return;
}
if (!hovered_but_reparenting) {
InspectorDock *inspector_dock = InspectorDock::get_singleton();
if (!inspector_dock->get_rect().has_point(inspector_dock->get_local_mouse_position())) {
List<Node *> full_selection = editor_selection->get_full_selected_node_list();
editor_selection->clear();
for (Node *E : full_selection) {
editor_selection->add_node(E);
}
InspectorDock *inspector_dock = InspectorDock::get_singleton();
if (!inspector_dock->get_rect().has_point(inspector_dock->get_local_mouse_position())) {
Node *node_edited = Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object());
if (editor_selection->get_full_selected_node_list().size() == 1 && !editor_selection->is_selected(node_edited)) {
Node *node_selected = scene_tree->get_selected();
EditorNode::get_singleton()->push_node_item(node_selected);
return;
}
if (select_node_hovered_at_end_of_drag) {
Node *node_inspected = Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object());
if (node_inspected) {
editor_selection->clear();
editor_selection->add_node(node_inspected);
scene_tree->set_selected(node_inspected);
select_node_hovered_at_end_of_drag = false;
}
if (edited_object_at_drag_start) {
EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
editor_history->add_object(edited_object_at_drag_start->get_instance_id());
InspectorDock::get_inspector_singleton()->edit(edited_object_at_drag_start);
InspectorDock::get_singleton()->update(edited_object_at_drag_start);
}
return;
}
Node *node_edited = Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object());
if (node_edited) {
editor_selection->clear();
editor_selection->add_node(node_edited);
scene_tree->set_selected(node_edited);
}
hovered_but_reparenting = false;
} break;
}
}
@ -2456,9 +2457,6 @@ void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, V
return; // Position and parent didn't change.
}
// Prevent selecting the hovered node and keep the reparented node(s) selected instead.
hovered_but_reparenting = true;
Node *validate = p_new_parent;
while (validate) {
ERR_FAIL_COND_MSG(p_nodes.has(validate), "Selection changed at some point. Can't reparent.");
@ -2475,7 +2473,6 @@ void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, V
Vector<StringName> former_names;
int inc = 0;
bool need_edit = false;
for (int ni = 0; ni < p_nodes.size(); ni++) {
// No undo implemented for this yet.
@ -2496,11 +2493,7 @@ void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, V
inc--; // If the child will generate a gap when moved, adjust.
}
if (same_parent) {
// When node is reparented to the same parent, EditorSelection does not change.
// After hovering another node, the inspector has to be manually updated in this case.
need_edit = select_node_hovered_at_end_of_drag;
} else {
if (!same_parent) {
undo_redo->add_do_method(node->get_parent(), "remove_child", node);
undo_redo->add_do_method(p_new_parent, "add_child", node, true);
}
@ -2616,11 +2609,6 @@ void SceneTreeDock::_do_reparent(Node *p_new_parent, int p_position_in_parent, V
undo_redo->add_undo_method(editor_selection, "add_node", E);
}
if (need_edit) {
EditorNode::get_singleton()->edit_current();
editor_selection->clear();
}
undo_redo->commit_action();
}

View file

@ -242,8 +242,7 @@ class SceneTreeDock : public EditorDock {
TreeItem *tree_item_inspected = nullptr;
Node *node_hovered_now = nullptr;
Node *node_hovered_previously = nullptr;
bool select_node_hovered_at_end_of_drag = false;
bool hovered_but_reparenting = false;
Object *edited_object_at_drag_start = nullptr;
virtual void input(const Ref<InputEvent> &p_event) override;
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;