1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-26 15:46:23 +00:00

Refactor node naming APIs used by editor

Make 'name_num_separator' a project setting
Make all node operations separator-aware
This commit is contained in:
Pedro J. Estébanez
2016-10-07 20:25:29 +02:00
parent 88a32c11f1
commit 9b2e2935d0
4 changed files with 81 additions and 128 deletions

View File

@@ -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;
p_child->data.name=_generate_serial_child_name(p_child);
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;
}
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;