added option to add_child, to use the same deduplication convention as in the editor, closes #3017

This commit is contained in:
Juan Linietsky 2015-12-08 11:21:12 -03:00
parent 5e2f327d4b
commit 3c33b705d1
3 changed files with 22 additions and 19 deletions

View File

@ -628,11 +628,11 @@ String Node::validate_child_name(const String& p_name) const {
} }
void Node::_validate_child_name(Node *p_child) { void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) {
/* Make sure the name is unique */ /* Make sure the name is unique */
if (node_hrcr) { if (node_hrcr || p_force_human_readable) {
//this approach to autoset node names is human readable but very slow //this approach to autoset node names is human readable but very slow
//it's turned on while running in the editor //it's turned on while running in the editor
@ -732,24 +732,27 @@ void Node::_add_child_nocheck(Node* p_child,const StringName& p_name) {
} }
void Node::add_child(Node *p_child) { void Node::add_child(Node *p_child, bool p_legible_unique_name) {
ERR_FAIL_NULL(p_child); ERR_FAIL_NULL(p_child);
/* Fail if node has a parent */ /* Fail if node has a parent */
ERR_EXPLAIN("Can't add child "+p_child->get_name()+" to itself.") if (p_child==this) {
ERR_FAIL_COND( p_child==this ); // adding to itself! ERR_EXPLAIN("Can't add child "+p_child->get_name()+" to itself.")
ERR_FAIL_COND( p_child==this ); // adding to itself!
}
ERR_EXPLAIN("Can't add child, already has a parent"); ERR_EXPLAIN("Can't add child, already has a parent");
ERR_FAIL_COND( p_child->data.parent ); ERR_FAIL_COND( p_child->data.parent );
ERR_EXPLAIN("Can't add child while a notification is happening"); ERR_EXPLAIN("Can't add child while a notification is happening");
ERR_FAIL_COND( data.blocked > 0 ); ERR_FAIL_COND( data.blocked > 0 );
/* Validate name */ /* Validate name */
_validate_child_name(p_child); _validate_child_name(p_child,p_legible_unique_name);
_add_child_nocheck(p_child,p_child->data.name); _add_child_nocheck(p_child,p_child->data.name);
} }
void Node::_propagate_validate_owner() { void Node::_propagate_validate_owner() {
if (data.owner) { if (data.owner) {
@ -1984,7 +1987,7 @@ void Node::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_name","name"),&Node::set_name); ObjectTypeDB::bind_method(_MD("set_name","name"),&Node::set_name);
ObjectTypeDB::bind_method(_MD("get_name"),&Node::get_name); ObjectTypeDB::bind_method(_MD("get_name"),&Node::get_name);
ObjectTypeDB::bind_method(_MD("add_child","node:Node"),&Node::add_child); ObjectTypeDB::bind_method(_MD("add_child","node:Node","legible_unique_name"),&Node::add_child,DEFVAL(false));
ObjectTypeDB::bind_method(_MD("remove_child","node:Node"),&Node::remove_child); ObjectTypeDB::bind_method(_MD("remove_child","node:Node"),&Node::remove_child);
//ObjectTypeDB::bind_method(_MD("remove_and_delete_child","node:Node"),&Node::remove_and_delete_child); //ObjectTypeDB::bind_method(_MD("remove_and_delete_child","node:Node"),&Node::remove_and_delete_child);
ObjectTypeDB::bind_method(_MD("get_child_count"),&Node::get_child_count); ObjectTypeDB::bind_method(_MD("get_child_count"),&Node::get_child_count);

View File

@ -122,7 +122,7 @@ private:
void _validate_child_name(Node *p_name); void _validate_child_name(Node *p_name, bool p_force_human_readable=false);
void _propagate_reverse_notification(int p_notification); void _propagate_reverse_notification(int p_notification);
void _propagate_deferred_notification(int p_notification, bool p_reverse); void _propagate_deferred_notification(int p_notification, bool p_reverse);
@ -187,7 +187,7 @@ public:
StringName get_name() const; StringName get_name() const;
void set_name(const String& p_name); void set_name(const String& p_name);
void add_child(Node *p_child); void add_child(Node *p_child,bool p_legible_unique_name=false);
void remove_child(Node *p_child); void remove_child(Node *p_child);
int get_child_count() const; int get_child_count() const;

View File

@ -961,7 +961,7 @@ void AnimationKeyEditor::_cleanup_animation(Ref<Animation> p_animation) {
Object *obj=NULL; Object *obj=NULL;
RES res; RES res;
Node *node = root->get_node_and_resource(animation->track_get_path(i),res); Node *node = root->get_node_and_resource(p_animation->track_get_path(i),res);
if (res.is_valid()) { if (res.is_valid()) {
obj=res.ptr(); obj=res.ptr();
@ -969,32 +969,32 @@ void AnimationKeyEditor::_cleanup_animation(Ref<Animation> p_animation) {
obj=node; obj=node;
} }
if (obj && animation->track_get_type(i)==Animation::TYPE_VALUE) { if (obj && p_animation->track_get_type(i)==Animation::TYPE_VALUE) {
valid_type=obj->get_static_property_type(animation->track_get_path(i).get_property(),&prop_exists); valid_type=obj->get_static_property_type(p_animation->track_get_path(i).get_property(),&prop_exists);
} }
if (!obj && cleanup_tracks->is_pressed()) { if (!obj && cleanup_tracks->is_pressed()) {
animation->remove_track(i); p_animation->remove_track(i);
i--; i--;
continue; continue;
} }
if (!prop_exists || animation->track_get_type(i)!=Animation::TYPE_VALUE || cleanup_keys->is_pressed()==false) if (!prop_exists || p_animation->track_get_type(i)!=Animation::TYPE_VALUE || cleanup_keys->is_pressed()==false)
continue; continue;
for(int j=0;j<animation->track_get_key_count(i);j++) { for(int j=0;j<p_animation->track_get_key_count(i);j++) {
Variant v = animation->track_get_key_value(i,j); Variant v = p_animation->track_get_key_value(i,j);
if (!Variant::can_convert(v.get_type(),valid_type)) { if (!Variant::can_convert(v.get_type(),valid_type)) {
animation->track_remove_key(i,j); p_animation->track_remove_key(i,j);
j--; j--;
} }
} }
if (animation->track_get_key_count(i)==0 && cleanup_tracks->is_pressed()) { if (p_animation->track_get_key_count(i)==0 && cleanup_tracks->is_pressed()) {
animation->remove_track(i); p_animation->remove_track(i);
i--; i--;
} }
} }