diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2018-05-02 22:21:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-02 22:21:31 +0200 |
commit | 741637b966dd644263032c7f937d990cfba52411 (patch) | |
tree | a4fd2924e160161977ab951337dcbed8273d4901 | |
parent | a02fec47a05913318c9b55d7f5f692194d1de5f1 (diff) | |
parent | 6758b6c1317bbdabd04a2c9f8dc16876429b6bfb (diff) |
Merge pull request #18451 from volzhs/node-name
Check invalid node name
-rw-r--r-- | editor/scene_tree_editor.cpp | 7 | ||||
-rw-r--r-- | scene/main/node.cpp | 16 | ||||
-rw-r--r-- | scene/main/node.h | 6 |
3 files changed, 25 insertions, 4 deletions
diff --git a/editor/scene_tree_editor.cpp b/editor/scene_tree_editor.cpp index e38347a653..64d278c0c5 100644 --- a/editor/scene_tree_editor.cpp +++ b/editor/scene_tree_editor.cpp @@ -644,11 +644,12 @@ void SceneTreeEditor::_renamed() { ERR_FAIL_COND(!n); String new_name = which->get_text(0); - if (new_name.find(".") != -1 || new_name.find("/") != -1) { + if (!Node::_validate_node_name(new_name)) { - error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n \".\", \"/\""); + error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + Node::invalid_character); error->popup_centered_minsize(); - new_name = n->get_name(); + + which->set_text(0, new_name); } if (new_name == n->get_name()) diff --git a/scene/main/node.cpp b/scene/main/node.cpp index fcf8768094..001b8cfd0b 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -979,9 +979,23 @@ void Node::_set_name_nocheck(const StringName &p_name) { data.name = p_name; } +String Node::invalid_character = ". : @ / \""; + +bool Node::_validate_node_name(String &p_name) { + String name = p_name; + Vector<String> chars = Node::invalid_character.split(" "); + for (int i = 0; i < chars.size(); i++) { + name = name.replace(chars[i], ""); + } + bool is_valid = name == p_name; + p_name = name; + return is_valid; +} + void Node::set_name(const String &p_name) { - String name = p_name.replace(":", "").replace("/", "").replace("@", ""); + String name = p_name; + _validate_node_name(name); ERR_FAIL_COND(name == ""); data.name = name; diff --git a/scene/main/node.h b/scene/main/node.h index b9bafb1ed1..a0b6399486 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -190,6 +190,12 @@ private: void _set_tree(SceneTree *p_tree); +#ifdef TOOLS_ENABLED + friend class SceneTreeEditor; +#endif + static String invalid_character; + static bool _validate_node_name(String &p_name); + protected: void _block() { data.blocked++; } void _unblock() { data.blocked--; } |