Skip to content

Commit

Permalink
Merge pull request #76376 from ajreckof/fix_NodePath_update_from_batc…
Browse files Browse the repository at this point in the history
…h_rename

Properly update nodepath with batch rename
  • Loading branch information
akien-mga committed Jun 15, 2023
2 parents 0b30785 + 5c15083 commit 2c51eca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
101 changes: 51 additions & 50 deletions editor/gui/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,81 +964,82 @@ 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);
}

void SceneTreeEditor::_renamed() {
TreeItem *which = tree->get_edited();

ERR_FAIL_COND(!which);
NodePath np = which->get_metadata(0);
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()) {
// 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());
} else {
raw_new_name = n->get_class();
}
}

String new_name = raw_new_name.validate_node_name();

if (new_name != raw_new_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()) {
which->set_text(0, n->get_name());
item->set_text(0, p_node->get_name());
return;
}

which->set_text(0, new_name);
item->set_text(0, new_name);
}

if (new_name == n->get_name()) {
if (which->get_text(0).is_empty()) {
which->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() {
TreeItem *which = tree->get_edited();

ERR_FAIL_COND(!which);
NodePath np = which->get_metadata(0);
Node *n = get_node(np);
ERR_FAIL_COND(!n);

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) {
new_name = Node::adjust_name_casing(n->get_class());
} else {
new_name = n->get_class();
}
}

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();
which->set_text(0, n->get_name());
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() {
Expand Down
2 changes: 1 addition & 1 deletion editor/gui/scene_tree_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 2 additions & 4 deletions editor/rename_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down

0 comments on commit 2c51eca

Please sign in to comment.