diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2016-10-09 14:55:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-09 14:55:53 +0200 |
commit | e0e21984d8f5153892e3b7eff4feb64a2c976cf9 (patch) | |
tree | 35016ad61640b77a408da613fddb494559cc6877 /scene/main | |
parent | b9e010fb7fa337f0aeb4a8262f61664300593847 (diff) | |
parent | 9b2e2935d06f870171353cb0e3f52caa6fc74c5b (diff) |
Merge pull request #6747 from RandomShaper/unify-serial-naming
Make node serial naming per-project and universal
Diffstat (limited to 'scene/main')
-rw-r--r-- | scene/main/node.cpp | 145 | ||||
-rw-r--r-- | scene/main/node.h | 9 |
2 files changed, 75 insertions, 79 deletions
diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 1892240426..0c1b8e52e9 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -1249,51 +1249,12 @@ void Node::set_human_readable_collision_renaming(bool p_enabled) { } +#ifdef TOOLS_ENABLED +String Node::validate_child_name(Node* p_child) { -String Node::validate_child_name(const String& p_name) const { - - //this approach to autoset node names is human readable but very slow - //it's turned on while running in the editor - - String basename = p_name; - - if (basename==String()) { - - return String(); - } - - 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; - } - - return attempted; - break; - } - - return basename; - + return _generate_serial_child_name(p_child); } +#endif void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { @@ -1304,41 +1265,8 @@ void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { //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; - } + p_child->data.name=_generate_serial_child_name(p_child); - } - - if (found) { - - val++; - continue; - } - - p_child->data.name=attempted; - break; - } } else { //this approach to autoset node names is fast but not as readable @@ -1373,6 +1301,54 @@ void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { } } +String Node::_generate_serial_child_name(Node *p_child) { + + String name = p_child->data.name; + + if (name=="") { + + name = p_child->get_type(); + } + + String nums; + for(int i=name.length()-1;i>=0;i--) { + CharType n=name[i]; + if (n>='0' && n<='9') { + nums=String::chr(name[i])+nums; + } else { + break; + } + } + + int num=nums.to_int(); + if (num<1) + num=1; + + String nnsep=_get_name_num_separator(); + name = name.substr(0,name.length()-nums.length()).strip_edges(); + if ( name.substr(name.length()-nnsep.length(),nnsep.length()) == nnsep) { + name = name.substr(0,name.length()-nnsep.length()); + } + + for(;;) { + String attempt = (name + (num > 1 ? nnsep + itos(num) : "")).strip_edges(); + bool found=false; + for(int i=0;i<data.children.size();i++) { + if (data.children[i]==p_child) + continue; + if (data.children[i]->data.name==attempt) { + found=true; + break; + } + } + if (!found) { + return attempt; + } else { + num++; + } + } +} + void Node::_add_child_nocheck(Node* p_child,const StringName& p_name) { //add a child node quickly, without name validation @@ -2811,6 +2787,10 @@ bool Node::is_displayed_folded() const { void Node::_bind_methods() { + _GLOBAL_DEF("node/name_num_separator",0); + Globals::get_singleton()->set_custom_property_info("node/name_num_separator",PropertyInfo(Variant::INT,"node/name_num_separator",PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash")); + + ObjectTypeDB::bind_method(_MD("_add_child_below_node","node:Node","child_node:Node","legible_unique_name"),&Node::add_child_below_node,DEFVAL(false)); ObjectTypeDB::bind_method(_MD("set_name","name"),&Node::set_name); @@ -2979,6 +2959,17 @@ void Node::_bind_methods() { } +String Node::_get_name_num_separator() { + switch(Globals::get_singleton()->get("node/name_num_separator").operator int()) { + case 0: return ""; + case 1: return " "; + case 2: return "_"; + case 3: return "-"; + } + return " "; +} + + Node::Node() { data.pos=-1; diff --git a/scene/main/node.h b/scene/main/node.h index 3568da2ab0..f18dc81195 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -29,6 +29,7 @@ #ifndef NODE_H #define NODE_H +#include "globals.h" #include "object.h" #include "path_db.h" #include "map.h" @@ -150,7 +151,8 @@ private: void _replace_connections_target(Node* p_new_target); - void _validate_child_name(Node *p_name, bool p_force_human_readable=false); + void _validate_child_name(Node *p_child, bool p_force_human_readable=false); + String _generate_serial_child_name(Node *p_child); void _propagate_reverse_notification(int p_notification); void _propagate_deferred_notification(int p_notification, bool p_reverse); @@ -193,6 +195,7 @@ protected: void _propagate_replace_owner(Node *p_owner,Node* p_by_owner); static void _bind_methods(); + static String _get_name_num_separator(); friend class SceneState; @@ -335,7 +338,9 @@ public: static void print_stray_nodes(); - String validate_child_name(const String& p_name) const; +#ifdef TOOLS_ENABLED + String validate_child_name(Node* p_child); +#endif void queue_delete(); |