Skip to content

Commit

Permalink
Allow to easily rename multiple nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Mar 1, 2024
1 parent dad6c77 commit f4439ea
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
84 changes: 73 additions & 11 deletions editor/gui/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,8 +1030,13 @@ void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
}
}

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

Expand Down Expand Up @@ -1091,7 +1096,7 @@ void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
emit_signal(SNAME("node_renamed"));
} else {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action("Rename Node", UndoRedo::MERGE_DISABLE, p_node);
undo_redo->create_action(TTR("Rename Node"), UndoRedo::MERGE_DISABLE, p_node);

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

Expand All @@ -1108,17 +1113,74 @@ void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
}
}

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

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

if (is_scene_tree_dock && tree->get_next_selected(which)) {
List<Node *> nodes_to_rename;
for (TreeItem *item = which; item; item = tree->get_next_selected(item)) {
Node *n = get_node(item->get_metadata(0));
ERR_FAIL_NULL(n);
nodes_to_rename.push_back(n);
}
ERR_FAIL_COND(nodes_to_rename.is_empty());

EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Rename Nodes"), UndoRedo::MERGE_DISABLE, nodes_to_rename.front()->get(), true);

TreeItem *item = which;
for (Node *n : nodes_to_rename) {
_renamed(item, which, n);
item = tree->get_next_selected(item);
}
which->remove_meta("use_class");
undo_redo->commit_action();
} else {
_renamed(which, nullptr);
}
}

void SceneTreeEditor::_renamed(TreeItem *p_item, TreeItem *p_batch_item, Node *p_node) {
Node *n;
if (p_node) {
n = p_node; // During batch rename the paths may change, so using metadata is unreliable.
} else {
n = get_node(p_item->get_metadata(0));
}
ERR_FAIL_NULL(n);

String new_name = which->get_text(0);
String new_name;
if (p_batch_item) {
if (!p_batch_item->get_meta(SNAME("use_class"), false)) {
new_name = p_batch_item->get_text(0);
}
} else {
new_name = p_item->get_text(0);
}

if (new_name.strip_edges().is_empty()) {
// If name is empty, fallback to class name.
if (int(GLOBAL_GET("editor/naming/node_name_casing")) != NAME_CASING_PASCAL_CASE) {
new_name = Node::adjust_name_casing(n->get_class());
} else {
new_name = n->get_class();
}

// When doing batch rename, rename all nodes to their respective class.
if (p_batch_item == p_item) {
p_batch_item->set_meta("use_class", true);
}
}

if (n->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name) != nullptr) {
error->set_text(vformat(TTR("A node with the unique name %s already exists in this scene."), new_name));
error->popup_centered();
p_item->set_text(0, n->get_name());
return;
}

_rename_node(n, new_name);
rename_node(n, new_name, p_item);
}

Node *SceneTreeEditor::get_selected() {
Expand Down Expand Up @@ -1491,7 +1553,7 @@ void SceneTreeEditor::set_connecting_signal(bool p_enable) {

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

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

Expand Down Expand Up @@ -1543,7 +1605,7 @@ SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_ope
}

tree->connect("cell_selected", callable_mp(this, &SceneTreeEditor::_selected_changed));
tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_renamed));
tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_edited));
tree->connect("multi_selected", callable_mp(this, &SceneTreeEditor::_cell_multi_selected));
tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));
Expand Down
6 changes: 4 additions & 2 deletions editor/gui/scene_tree_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class SceneTreeEditor : public Control {
void _notification(int p_what);
void _selected_changed();
void _deselect_items();
void _rename_node(Node *p_node, const String &p_name);

void _cell_collapsed(Object *p_obj);

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

void _renamed();
void _edited();
void _renamed(TreeItem *p_item, TreeItem *p_batch_item, Node *p_node = nullptr);

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

void rename_node(Node *p_node, const String &p_name, TreeItem *p_item = nullptr);

void set_filter(const String &p_filter);
String get_filter() const;
String get_filter_term_warning();
Expand Down
3 changes: 1 addition & 2 deletions editor/rename_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ void RenameDialog::rename() {
ERR_PRINT("Skipping missing node: " + to_rename[i].first.get_concatenated_subnames());
continue;
}

scene_tree_editor->call("_rename_node", n, new_name);
scene_tree_editor->rename_node(n, new_name);
}

undo_redo->commit_action();
Expand Down

0 comments on commit f4439ea

Please sign in to comment.