diff options
Diffstat (limited to 'scene/main/node.cpp')
-rw-r--r-- | scene/main/node.cpp | 115 |
1 files changed, 85 insertions, 30 deletions
diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 1f54040de4..9b592a77d5 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -462,7 +462,7 @@ void Node::_set_name_nocheck(const StringName& p_name) { void Node::set_name(const String& p_name) { - String name=p_name.replace(":","").replace("/",""); + String name=p_name.replace(":","").replace("/","").replace("@",""); ERR_FAIL_COND(name==""); data.name=name; @@ -479,45 +479,99 @@ void Node::set_name(const String& p_name) { } } +static bool node_hrcr=false; +static SafeRefCount node_hrcr_count; + +void Node::init_node_hrcr() { + node_hrcr_count.init(1); +} + +void Node::set_human_readable_collision_renaming(bool p_enabled) { + + node_hrcr=p_enabled; +} + + void Node::_validate_child_name(Node *p_child) { /* Make sure the name is unique */ - String basename = p_child->data.name; - if (basename=="") { - - basename = p_child->get_type(); - } - - int val=1; - - for(;;) { - - String attempted = val > 1 ? (basename + " " +itos(val) ) : basename; + if (node_hrcr) { - bool found=false; - - for (int i=0;i<data.children.size();i++) { - - if (data.children[i]==p_child) + //this approach to autoset node names is human readable but very slow + //it's turned on while running in the editor + + String basename = p_child->data.name; + + if (basename=="") { + + basename = p_child->get_type(); + } + + int val=1; + + for(;;) { + + String attempted = val > 1 ? (basename + " " +itos(val) ) : basename; + + bool found=false; + + for (int i=0;i<data.children.size();i++) { + + if (data.children[i]==p_child) + continue; + if (data.children[i]->get_name() == attempted) { + found=true; + break; + } + + } + + if (found) { + + val++; continue; - if (data.children[i]->get_name() == attempted) { - found=true; - break; } - + + p_child->data.name=attempted; + break; } - - if (found) { - - val++; - continue; + } else { + + //this approach to autoset node names is fast but not as readable + //it's the default and reserves the '@' character for unique names. + + bool unique=true; + + if (p_child->data.name==StringName() || p_child->data.name.operator String()[0]=='@') { + //new unique name must be assigned + unique=false; + } else { + //check if exists + Node **childs=data.children.ptr(); + int cc = data.children.size(); + + for(int i=0;i<cc;i++) { + if (childs[i]==p_child) + continue; + if (childs[i]->data.name==p_child->data.name) { + unique=false; + break; + } + } + } + + if (!unique) { + + node_hrcr_count.ref(); +#ifdef DEBUG_ENABLED + String name = "@"+String(p_child->get_type_name())+itos(node_hrcr_count.get()); +#else + String name = "@"+itos(node_hrcr_count.get()); +#endif + p_child->data.name=name; } - - p_child->data.name=attempted; - break; } - } void Node::_add_child_nocheck(Node* p_child,const StringName& p_name) { @@ -541,6 +595,7 @@ void Node::_add_child_nocheck(Node* p_child,const StringName& p_name) { } + void Node::add_child(Node *p_child) { ERR_FAIL_NULL(p_child); |