Merge pull request #76376 from ajreckof/fix_NodePath_update_from_batch_rename

Properly update nodepath with batch rename
This commit is contained in:
Rémi Verschelde 2023-06-15 10:39:48 +02:00
commit 2c51ecac5a
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 54 additions and 55 deletions

View File

@ -964,17 +964,54 @@ void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
}
}
void SceneTreeEditor::_rename_node(ObjectID p_node, const String &p_name) {
Object *o = ObjectDB::get_instance(p_node);
ERR_FAIL_COND(!o);
Node *n = Object::cast_to<Node>(o);
ERR_FAIL_COND(!n);
TreeItem *item = _find(tree->get_root(), n->get_path());
void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
TreeItem *item = _find(tree->get_root(), p_node->get_path());
ERR_FAIL_COND(!item);
String new_name = p_name.validate_node_name();
n->set_name(p_name);
item->set_metadata(0, n->get_path());
item->set_text(0, p_name);
if (new_name != p_name) {
error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::get_invalid_node_name_characters());
error->popup_centered();
if (new_name.is_empty()) {
item->set_text(0, p_node->get_name());
return;
}
item->set_text(0, new_name);
}
if (new_name == p_node->get_name()) {
if (item->get_text(0).is_empty()) {
item->set_text(0, new_name);
}
return;
}
// Trim leading/trailing whitespace to prevent node names from containing accidental whitespace, which would make it more difficult to get the node via `get_node()`.
new_name = new_name.strip_edges();
if (!is_scene_tree_dock) {
p_node->set_name(new_name);
item->set_metadata(0, p_node->get_path());
emit_signal(SNAME("node_renamed"));
} else {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action("Rename Node", UndoRedo::MERGE_DISABLE, p_node);
emit_signal(SNAME("node_prerename"), p_node, new_name);
undo_redo->add_undo_method(p_node, "set_name", p_node->get_name());
undo_redo->add_undo_method(item, "set_metadata", 0, p_node->get_path());
undo_redo->add_undo_method(item, "set_text", 0, p_node->get_name());
p_node->set_name(p_name);
undo_redo->add_do_method(p_node, "set_name", new_name);
undo_redo->add_do_method(item, "set_metadata", 0, p_node->get_path());
undo_redo->add_do_method(item, "set_text", 0, new_name);
undo_redo->commit_action();
}
}
void SceneTreeEditor::_renamed() {
@ -985,41 +1022,16 @@ void SceneTreeEditor::_renamed() {
Node *n = get_node(np);
ERR_FAIL_COND(!n);
String raw_new_name = which->get_text(0);
if (raw_new_name.strip_edges().is_empty()) {
String new_name = which->get_text(0);
if (new_name.strip_edges().is_empty()) {
// If name is empty, fallback to class name.
if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
raw_new_name = Node::adjust_name_casing(n->get_class());
new_name = Node::adjust_name_casing(n->get_class());
} else {
raw_new_name = n->get_class();
new_name = n->get_class();
}
}
String new_name = raw_new_name.validate_node_name();
if (new_name != raw_new_name) {
error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::get_invalid_node_name_characters());
error->popup_centered();
if (new_name.is_empty()) {
which->set_text(0, n->get_name());
return;
}
which->set_text(0, new_name);
}
if (new_name == n->get_name()) {
if (which->get_text(0).is_empty()) {
which->set_text(0, new_name);
}
return;
}
// Trim leading/trailing whitespace to prevent node names from containing accidental whitespace, which would make it more difficult to get the node via `get_node()`.
new_name = new_name.strip_edges();
if (n->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name) != nullptr) {
error->set_text(TTR("Another node already uses this unique name in the scene."));
error->popup_centered();
@ -1027,18 +1039,7 @@ void SceneTreeEditor::_renamed() {
return;
}
if (!is_scene_tree_dock) {
n->set_name(new_name);
which->set_metadata(0, n->get_path());
emit_signal(SNAME("node_renamed"));
} else {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Rename Node"), UndoRedo::MERGE_DISABLE, n);
emit_signal(SNAME("node_prerename"), n, new_name);
undo_redo->add_do_method(this, "_rename_node", n->get_instance_id(), new_name);
undo_redo->add_undo_method(this, "_rename_node", n->get_instance_id(), n->get_name());
undo_redo->commit_action();
}
_rename_node(n, new_name);
}
Node *SceneTreeEditor::get_selected() {

View File

@ -85,7 +85,7 @@ class SceneTreeEditor : public Control {
void _notification(int p_what);
void _selected_changed();
void _deselect_items();
void _rename_node(ObjectID p_node, const String &p_name);
void _rename_node(Node *p_node, const String &p_name);
void _cell_collapsed(Object *p_obj);

View File

@ -588,7 +588,7 @@ void RenameDialog::rename() {
if (!to_rename.is_empty()) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Batch Rename"));
undo_redo->create_action(TTR("Batch Rename"), UndoRedo::MERGE_DISABLE, root_node, true);
// Make sure to iterate reversed so that child nodes will find parents.
for (int i = to_rename.size() - 1; i >= 0; --i) {
@ -600,9 +600,7 @@ void RenameDialog::rename() {
continue;
}
scene_tree_editor->emit_signal(SNAME("node_prerename"), n, new_name);
undo_redo->add_do_method(scene_tree_editor, "_rename_node", n->get_instance_id(), new_name);
undo_redo->add_undo_method(scene_tree_editor, "_rename_node", n->get_instance_id(), n->get_name());
scene_tree_editor->call("_rename_node", n, new_name);
}
undo_redo->commit_action();