Ported: SceneTreeDock Toggling unique name in owner for all selected nodes - kleonc

6417b999ee
This commit is contained in:
Relintai 2022-09-16 12:16:37 +02:00
parent 7dd2bce61b
commit 8265045030

View File

@ -30,26 +30,26 @@
#include "scene_tree_dock.h" #include "scene_tree_dock.h"
#include "core/variant/array.h" #include "core/config/project_settings.h"
#include "core/object/class_db.h"
#include "core/variant/dictionary.h"
#include "core/error/error_list.h" #include "core/error/error_list.h"
#include "core/error/error_macros.h" #include "core/error/error_macros.h"
#include "core/input/input.h"
#include "core/input/input_event.h"
#include "core/io/resource_loader.h" #include "core/io/resource_loader.h"
#include "core/io/resource_saver.h" #include "core/io/resource_saver.h"
#include "core/math/vector2.h" #include "core/math/vector2.h"
#include "core/object/class_db.h"
#include "core/object/ref_ptr.h"
#include "core/object/script_language.h"
#include "core/object/undo_redo.h"
#include "core/os/file_access.h" #include "core/os/file_access.h"
#include "core/input/input.h"
#include "core/input/input_event.h"
#include "core/os/keyboard.h" #include "core/os/keyboard.h"
#include "core/os/memory.h" #include "core/os/memory.h"
#include "core/os/os.h" #include "core/os/os.h"
#include "core/config/project_settings.h"
#include "core/object/ref_ptr.h"
#include "core/object/script_language.h"
#include "core/string/string_name.h" #include "core/string/string_name.h"
#include "core/typedefs.h" #include "core/typedefs.h"
#include "core/object/undo_redo.h" #include "core/variant/array.h"
#include "core/variant/dictionary.h"
#include "core/variant/variant.h" #include "core/variant/variant.h"
#include "editor/animation_track_editor.h" #include "editor/animation_track_editor.h"
#include "editor/create_dialog.h" #include "editor/create_dialog.h"
@ -1175,24 +1175,65 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
} }
} break; } break;
case TOOL_TOGGLE_SCENE_UNIQUE_NAME: { case TOOL_TOGGLE_SCENE_UNIQUE_NAME: {
List<Node *> selection = editor_selection->get_selected_node_list(); // Enabling/disabling based on the same node based on which the checkbox in the menu is checked/unchecked.
List<Node *>::Element *e = selection.front(); List<Node *>::Element *first_selected = editor_selection->get_selected_node_list().front();
if (e) { if (first_selected == nullptr) {
UndoRedo *undo_redo = &editor_data->get_undo_redo(); return;
Node *node = e->get(); }
bool enabled = node->is_unique_name_in_owner(); bool enabling = !first_selected->get()->is_unique_name_in_owner();
if (!enabled && get_tree()->get_edited_scene_root()->get_node_or_null("%" + String(node->get_name())) != nullptr) {
accept->set_text(TTR("Another node already uses this unique name in the scene.")); List<Node *> full_selection = editor_selection->get_full_selected_node_list();
UndoRedo *undo_redo = &editor_data->get_undo_redo();
if (enabling) {
Vector<Node *> new_unique_nodes;
Vector<StringName> new_unique_names;
Vector<StringName> cant_be_set_unique_names;
for (List<Node *>::Element *e = full_selection.front(); e; e = e->next()) {
Node *node = e->get();
if (node->is_unique_name_in_owner()) {
continue;
}
StringName name = node->get_name();
if (new_unique_names.find(name) != -1 || get_tree()->get_edited_scene_root()->get_node_or_null(UNIQUE_NODE_PREFIX + String(name)) != nullptr) {
cant_be_set_unique_names.push_back(name);
} else {
new_unique_nodes.push_back(node);
new_unique_names.push_back(name);
}
}
if (new_unique_nodes.size()) {
undo_redo->create_action(TTR("Enable Scene Unique Name(s)"));
for (int i = 0; i < new_unique_nodes.size(); i++) {
undo_redo->add_do_method(new_unique_nodes[i], "set_unique_name_in_owner", true);
undo_redo->add_undo_method(new_unique_nodes[i], "set_unique_name_in_owner", false);
}
undo_redo->commit_action();
}
if (cant_be_set_unique_names.size()) {
String popup_text = TTR("Unique names already used by another node in the scene:");
popup_text += "\n";
for (int i = 0; i < cant_be_set_unique_names.size(); i++) {
popup_text += "\n" + String(cant_be_set_unique_names[i]);
}
accept->set_text(popup_text);
accept->popup_centered(); accept->popup_centered();
return;
} }
if (!enabled) { } else { // Disabling.
undo_redo->create_action(TTR("Enable Scene Unique Name")); undo_redo->create_action(TTR("Disable Scene Unique Name(s)"));
} else { for (List<Node *>::Element *e = full_selection.front(); e; e = e->next()) {
undo_redo->create_action(TTR("Disable Scene Unique Name")); Node *node = e->get();
if (!node->is_unique_name_in_owner()) {
continue;
}
undo_redo->add_do_method(node, "set_unique_name_in_owner", false);
undo_redo->add_undo_method(node, "set_unique_name_in_owner", true);
} }
undo_redo->add_do_method(node, "set_unique_name_in_owner", !enabled);
undo_redo->add_undo_method(node, "set_unique_name_in_owner", enabled);
undo_redo->commit_action(); undo_redo->commit_action();
} }
} break; } break;
@ -2863,9 +2904,18 @@ void SceneTreeDock::_tree_rmb(const Vector2 &p_menu_pos) {
} }
if (profile_allow_editing) { if (profile_allow_editing) {
if (selection[0]->get_owner() == EditorNode::get_singleton()->get_edited_scene()) { // Allow multi-toggling scene unique names but only if all selected nodes are owned by the edited scene root.
// Only for nodes owned by the edited scene root. bool all_owned = true;
for (List<Node *>::Element *e = full_selection.front(); e; e = e->next()) {
Node *node = e->get();
if (node->get_owner() != EditorNode::get_singleton()->get_edited_scene()) {
all_owned = false;
break;
}
}
if (all_owned) {
menu->add_icon_check_item(get_theme_icon("SceneUniqueName", "EditorIcons"), TTR("Access as Scene Unique Name"), TOOL_TOGGLE_SCENE_UNIQUE_NAME); menu->add_icon_check_item(get_theme_icon("SceneUniqueName", "EditorIcons"), TTR("Access as Scene Unique Name"), TOOL_TOGGLE_SCENE_UNIQUE_NAME);
// Checked based on `selection[0]` because `full_selection` has undesired ordering.
menu->set_item_checked(menu->get_item_index(TOOL_TOGGLE_SCENE_UNIQUE_NAME), selection[0]->is_unique_name_in_owner()); menu->set_item_checked(menu->get_item_index(TOOL_TOGGLE_SCENE_UNIQUE_NAME), selection[0]->is_unique_name_in_owner());
menu->add_separator(); menu->add_separator();
} }