Skip to content

Commit ffadba0

Browse files
KoBeWiajreckof
andcommitted
Allow to easily rename multiple nodes
Co-authored-by: ajreckof <tbonhoure@ymail.Com>
1 parent dad6c77 commit ffadba0

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

editor/gui/scene_tree_editor.cpp

+38-14
Original file line numberDiff line numberDiff line change
@@ -1030,8 +1030,13 @@ void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
10301030
}
10311031
}
10321032

1033-
void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
1034-
TreeItem *item = _find(tree->get_root(), p_node->get_path());
1033+
void SceneTreeEditor::rename_node(Node *p_node, const String &p_name, TreeItem *p_item) {
1034+
TreeItem *item;
1035+
if (p_item) {
1036+
item = p_item; // During batch rename the paths may change, so using _find() is unreliable.
1037+
} else {
1038+
item = _find(tree->get_root(), p_node->get_path());
1039+
}
10351040
ERR_FAIL_NULL(item);
10361041
String new_name = p_name.validate_node_name();
10371042

@@ -1069,7 +1074,7 @@ void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
10691074

10701075
// We previously made sure name is not the same as current name so that it won't complain about already used unique name when not changing name.
10711076
if (p_node->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name)) {
1072-
String text = TTR("Another node already uses this unique name in the scene.");
1077+
String text = vformat(TTR("A node with the unique name %s already exists in this scene."), new_name);
10731078
if (error->is_visible()) {
10741079
if (!error->get_meta("same_unique_name", false)) {
10751080
error->set_text(error->get_text() + "\n\n" + text);
@@ -1091,7 +1096,7 @@ void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
10911096
emit_signal(SNAME("node_renamed"));
10921097
} else {
10931098
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1094-
undo_redo->create_action("Rename Node", UndoRedo::MERGE_DISABLE, p_node);
1099+
undo_redo->create_action(TTR("Rename Node"), UndoRedo::MERGE_DISABLE, p_node);
10951100

10961101
emit_signal(SNAME("node_prerename"), p_node, new_name);
10971102

@@ -1108,17 +1113,37 @@ void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
11081113
}
11091114
}
11101115

1111-
void SceneTreeEditor::_renamed() {
1112-
TreeItem *which = tree->get_edited();
1113-
1116+
void SceneTreeEditor::_edited() {
1117+
TreeItem *which = tree->get_next_selected(nullptr);
11141118
ERR_FAIL_NULL(which);
1115-
NodePath np = which->get_metadata(0);
1116-
Node *n = get_node(np);
1117-
ERR_FAIL_NULL(n);
1119+
TreeItem *edited = tree->get_edited();
1120+
ERR_FAIL_NULL(edited);
1121+
1122+
if (is_scene_tree_dock && tree->get_next_selected(which)) {
1123+
List<Node *> nodes_to_rename;
1124+
for (TreeItem *item = which; item; item = tree->get_next_selected(item)) {
1125+
Node *n = get_node(item->get_metadata(0));
1126+
ERR_FAIL_NULL(n);
1127+
nodes_to_rename.push_back(n);
1128+
}
1129+
ERR_FAIL_COND(nodes_to_rename.is_empty());
11181130

1119-
String new_name = which->get_text(0);
1131+
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1132+
undo_redo->create_action(TTR("Rename Nodes"), UndoRedo::MERGE_DISABLE, nodes_to_rename.front()->get(), true);
1133+
1134+
TreeItem *item = which;
1135+
String new_name = edited->get_text(0);
1136+
for (Node *n : nodes_to_rename) {
1137+
rename_node(n, new_name, item);
1138+
item = tree->get_next_selected(item);
1139+
}
11201140

1121-
_rename_node(n, new_name);
1141+
undo_redo->commit_action();
1142+
} else {
1143+
Node *n = get_node(which->get_metadata(0));
1144+
ERR_FAIL_NULL(n);
1145+
rename_node(n, which->get_text(0));
1146+
}
11221147
}
11231148

11241149
Node *SceneTreeEditor::get_selected() {
@@ -1491,7 +1516,6 @@ void SceneTreeEditor::set_connecting_signal(bool p_enable) {
14911516

14921517
void SceneTreeEditor::_bind_methods() {
14931518
ClassDB::bind_method(D_METHOD("_update_tree"), &SceneTreeEditor::_update_tree, DEFVAL(false)); // Still used by UndoRedo.
1494-
ClassDB::bind_method("_rename_node", &SceneTreeEditor::_rename_node);
14951519

14961520
ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
14971521

@@ -1543,7 +1567,7 @@ SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_ope
15431567
}
15441568

15451569
tree->connect("cell_selected", callable_mp(this, &SceneTreeEditor::_selected_changed));
1546-
tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_renamed));
1570+
tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_edited));
15471571
tree->connect("multi_selected", callable_mp(this, &SceneTreeEditor::_cell_multi_selected));
15481572
tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
15491573
tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));

editor/gui/scene_tree_editor.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class SceneTreeEditor : public Control {
8989
void _notification(int p_what);
9090
void _selected_changed();
9191
void _deselect_items();
92-
void _rename_node(Node *p_node, const String &p_name);
9392

9493
void _cell_collapsed(Object *p_obj);
9594

@@ -101,7 +100,8 @@ class SceneTreeEditor : public Control {
101100
bool show_enabled_subscene = false;
102101
bool is_scene_tree_dock = false;
103102

104-
void _renamed();
103+
void _edited();
104+
void _renamed(TreeItem *p_item, TreeItem *p_batch_item, Node *p_node = nullptr);
105105

106106
HashSet<Node *> marked;
107107
bool marked_selectable = false;
@@ -147,6 +147,8 @@ class SceneTreeEditor : public Control {
147147
// Public for use with callable_mp.
148148
void _update_tree(bool p_scroll_to_selected = false);
149149

150+
void rename_node(Node *p_node, const String &p_name, TreeItem *p_item = nullptr);
151+
150152
void set_filter(const String &p_filter);
151153
String get_filter() const;
152154
String get_filter_term_warning();

editor/rename_dialog.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,7 @@ void RenameDialog::rename() {
599599
ERR_PRINT("Skipping missing node: " + to_rename[i].first.get_concatenated_subnames());
600600
continue;
601601
}
602-
603-
scene_tree_editor->call("_rename_node", n, new_name);
602+
scene_tree_editor->rename_node(n, new_name);
604603
}
605604

606605
undo_redo->commit_action();

editor/scene_tree_dock.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -3370,12 +3370,7 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
33703370
}
33713371

33723372
if (profile_allow_editing) {
3373-
bool add_separator = false;
3374-
3375-
if (full_selection.size() == 1) {
3376-
add_separator = true;
3377-
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Rename")), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
3378-
}
3373+
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Rename")), ED_GET_SHORTCUT("scene_tree/rename"), TOOL_RENAME);
33793374

33803375
bool can_replace = true;
33813376
for (Node *E : selection) {
@@ -3386,14 +3381,11 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
33863381
}
33873382

33883383
if (can_replace) {
3389-
add_separator = true;
33903384
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Reload")), ED_GET_SHORTCUT("scene_tree/change_node_type"), TOOL_REPLACE);
33913385
}
33923386

33933387
if (scene_tree->get_selected() != edited_scene) {
3394-
if (add_separator) {
3395-
menu->add_separator();
3396-
}
3388+
menu->add_separator();
33973389
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("MoveUp")), ED_GET_SHORTCUT("scene_tree/move_up"), TOOL_MOVE_UP);
33983390
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("MoveDown")), ED_GET_SHORTCUT("scene_tree/move_down"), TOOL_MOVE_DOWN);
33993391
menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Duplicate")), ED_GET_SHORTCUT("scene_tree/duplicate"), TOOL_DUPLICATE);

0 commit comments

Comments
 (0)