diff options
Diffstat (limited to 'scene/main/node.cpp')
-rw-r--r-- | scene/main/node.cpp | 704 |
1 files changed, 304 insertions, 400 deletions
diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 6b46ffd8ad..c9d430c656 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -51,22 +51,16 @@ VARIANT_ENUM_CAST(Node::PauseMode); int Node::orphan_node_count = 0; void Node::_notification(int p_notification) { - switch (p_notification) { - case NOTIFICATION_PROCESS: { - if (get_script_instance()) { - Variant time = get_process_delta_time(); const Variant *ptr[1] = { &time }; get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_process, ptr, 1); } } break; case NOTIFICATION_PHYSICS_PROCESS: { - if (get_script_instance()) { - Variant time = get_physics_process_delta_time(); const Variant *ptr[1] = { &time }; get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_physics_process, ptr, 1); @@ -78,21 +72,24 @@ void Node::_notification(int p_notification) { ERR_FAIL_COND(!get_tree()); if (data.pause_mode == PAUSE_MODE_INHERIT) { - - if (data.parent) + if (data.parent) { data.pause_owner = data.parent->data.pause_owner; - else + } else { data.pause_owner = nullptr; + } } else { data.pause_owner = this; } - if (data.input) + if (data.input) { add_to_group("_vp_input" + itos(get_viewport()->get_instance_id())); - if (data.unhandled_input) + } + if (data.unhandled_input) { add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id())); - if (data.unhandled_key_input) + } + if (data.unhandled_key_input) { add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id())); + } get_tree()->node_count++; orphan_node_count--; @@ -105,12 +102,15 @@ void Node::_notification(int p_notification) { get_tree()->node_count--; orphan_node_count++; - if (data.input) + if (data.input) { remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id())); - if (data.unhandled_input) + } + if (data.unhandled_input) { remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id())); - if (data.unhandled_key_input) + } + if (data.unhandled_key_input) { remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id())); + } data.pause_owner = nullptr; if (data.path_cache) { @@ -119,16 +119,13 @@ void Node::_notification(int p_notification) { } } break; case NOTIFICATION_PATH_CHANGED: { - if (data.path_cache) { memdelete(data.path_cache); data.path_cache = nullptr; } } break; case NOTIFICATION_READY: { - if (get_script_instance()) { - if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_input)) { set_process_input(true); } @@ -157,22 +154,18 @@ void Node::_notification(int p_notification) { data.in_constructor = false; } break; case NOTIFICATION_PREDELETE: { - set_owner(nullptr); while (data.owned.size()) { - data.owned.front()->get()->set_owner(nullptr); } if (data.parent) { - data.parent->remove_child(this); } // kill children as cleanly as possible while (data.children.size()) { - Node *child = data.children[data.children.size() - 1]; //begin from the end because its faster and more consistent with creation remove_child(child); memdelete(child); @@ -183,11 +176,9 @@ void Node::_notification(int p_notification) { } void Node::_propagate_ready() { - data.ready_notified = true; data.blocked++; for (int i = 0; i < data.children.size(); i++) { - data.children[i]->_propagate_ready(); } data.blocked--; @@ -208,13 +199,13 @@ void Node::_propagate_enter_tree() { data.tree = data.parent->data.tree; data.depth = data.parent->data.depth + 1; } else { - data.depth = 1; } data.viewport = Object::cast_to<Viewport>(this); - if (!data.viewport && data.parent) + if (!data.viewport && data.parent) { data.viewport = data.parent->data.viewport; + } data.inside_tree = true; @@ -225,7 +216,6 @@ void Node::_propagate_enter_tree() { notification(NOTIFICATION_ENTER_TREE); if (get_script_instance()) { - get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_enter_tree, nullptr, 0); } @@ -237,9 +227,9 @@ void Node::_propagate_enter_tree() { //block while adding children for (int i = 0; i < data.children.size(); i++) { - - if (!data.children[i]->is_inside_tree()) // could have been added in enter_tree + if (!data.children[i]->is_inside_tree()) { // could have been added in enter_tree data.children[i]->_propagate_enter_tree(); + } } data.blocked--; @@ -251,7 +241,6 @@ void Node::_propagate_enter_tree() { } void Node::_propagate_after_exit_tree() { - data.blocked++; for (int i = 0; i < data.children.size(); i++) { data.children[i]->_propagate_after_exit_tree(); @@ -261,7 +250,6 @@ void Node::_propagate_after_exit_tree() { } void Node::_propagate_exit_tree() { - //block while removing children #ifdef DEBUG_ENABLED @@ -270,21 +258,20 @@ void Node::_propagate_exit_tree() { data.blocked++; for (int i = data.children.size() - 1; i >= 0; i--) { - data.children[i]->_propagate_exit_tree(); } data.blocked--; if (get_script_instance()) { - get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_exit_tree, nullptr, 0); } emit_signal(SceneStringNames::get_singleton()->tree_exiting); notification(NOTIFICATION_EXIT_TREE, true); - if (data.tree) + if (data.tree) { data.tree->node_removed(this); + } // exit groups @@ -295,8 +282,9 @@ void Node::_propagate_exit_tree() { data.viewport = nullptr; - if (data.tree) + if (data.tree) { data.tree->tree_changed(); + } data.inside_tree = false; data.ready_notified = false; @@ -305,7 +293,6 @@ void Node::_propagate_exit_tree() { } void Node::move_child(Node *p_child, int p_pos) { - ERR_FAIL_NULL(p_child); ERR_FAIL_INDEX_MSG(p_pos, data.children.size() + 1, "Invalid new child position: " + itos(p_pos) + "."); ERR_FAIL_COND_MSG(p_child->data.parent != this, "Child is not a child of this node."); @@ -313,11 +300,13 @@ void Node::move_child(Node *p_child, int p_pos) { // Specifying one place beyond the end // means the same as moving to the last position - if (p_pos == data.children.size()) + if (p_pos == data.children.size()) { p_pos--; + } - if (p_child->data.pos == p_pos) + if (p_child->data.pos == p_pos) { return; //do nothing + } int motion_from = MIN(p_pos, p_child->data.pos); int motion_to = MAX(p_pos, p_child->data.pos); @@ -332,7 +321,6 @@ void Node::move_child(Node *p_child, int p_pos) { data.blocked++; //new pos first for (int i = motion_from; i <= motion_to; i++) { - data.children[i]->data.pos = i; } // notification second @@ -341,94 +329,94 @@ void Node::move_child(Node *p_child, int p_pos) { data.children[i]->notification(NOTIFICATION_MOVED_IN_PARENT); } for (const Map<StringName, GroupData>::Element *E = p_child->data.grouped.front(); E; E = E->next()) { - if (E->get().group) + if (E->get().group) { E->get().group->changed = true; + } } data.blocked--; } void Node::raise() { - - if (!data.parent) + if (!data.parent) { return; + } data.parent->move_child(this, data.parent->data.children.size() - 1); } void Node::add_child_notify(Node *p_child) { - // to be used when not wanted } void Node::remove_child_notify(Node *p_child) { - // to be used when not wanted } void Node::move_child_notify(Node *p_child) { - // to be used when not wanted } void Node::set_physics_process(bool p_process) { - - if (data.physics_process == p_process) + if (data.physics_process == p_process) { return; + } data.physics_process = p_process; - if (data.physics_process) + if (data.physics_process) { add_to_group("physics_process", false); - else + } else { remove_from_group("physics_process"); + } _change_notify("physics_process"); } bool Node::is_physics_processing() const { - return data.physics_process; } void Node::set_physics_process_internal(bool p_process_internal) { - - if (data.physics_process_internal == p_process_internal) + if (data.physics_process_internal == p_process_internal) { return; + } data.physics_process_internal = p_process_internal; - if (data.physics_process_internal) + if (data.physics_process_internal) { add_to_group("physics_process_internal", false); - else + } else { remove_from_group("physics_process_internal"); + } _change_notify("physics_process_internal"); } bool Node::is_physics_processing_internal() const { - return data.physics_process_internal; } void Node::set_pause_mode(PauseMode p_mode) { - - if (data.pause_mode == p_mode) + if (data.pause_mode == p_mode) { return; + } bool prev_inherits = data.pause_mode == PAUSE_MODE_INHERIT; data.pause_mode = p_mode; - if (!is_inside_tree()) + if (!is_inside_tree()) { return; //pointless - if ((data.pause_mode == PAUSE_MODE_INHERIT) == prev_inherits) + } + if ((data.pause_mode == PAUSE_MODE_INHERIT) == prev_inherits) { return; ///nothing changed + } Node *owner = nullptr; if (data.pause_mode == PAUSE_MODE_INHERIT) { - - if (data.parent) + if (data.parent) { owner = data.parent->data.pause_owner; + } } else { owner = this; } @@ -437,40 +425,34 @@ void Node::set_pause_mode(PauseMode p_mode) { } Node::PauseMode Node::get_pause_mode() const { - return data.pause_mode; } void Node::_propagate_pause_owner(Node *p_owner) { - - if (this != p_owner && data.pause_mode != PAUSE_MODE_INHERIT) + if (this != p_owner && data.pause_mode != PAUSE_MODE_INHERIT) { return; + } data.pause_owner = p_owner; for (int i = 0; i < data.children.size(); i++) { - data.children[i]->_propagate_pause_owner(p_owner); } } void Node::set_network_master(int p_peer_id, bool p_recursive) { - data.network_master = p_peer_id; if (p_recursive) { for (int i = 0; i < data.children.size(); i++) { - data.children[i]->set_network_master(p_peer_id, true); } } } int Node::get_network_master() const { - return data.network_master; } bool Node::is_network_master() const { - ERR_FAIL_COND_V(!is_inside_tree(), false); return get_multiplayer()->get_network_unique_id() == data.network_master; @@ -479,7 +461,6 @@ bool Node::is_network_master() const { /***** RPC CONFIG ********/ uint16_t Node::rpc_config(const StringName &p_method, MultiplayerAPI::RPCMode p_mode) { - uint16_t mid = get_node_rpc_method_id(p_method); if (mid == UINT16_MAX) { // It's new @@ -496,7 +477,6 @@ uint16_t Node::rpc_config(const StringName &p_method, MultiplayerAPI::RPCMode p_ } uint16_t Node::rset_config(const StringName &p_property, MultiplayerAPI::RPCMode p_mode) { - uint16_t pid = get_node_rset_property_id(p_property); if (pid == UINT16_MAX) { // It's new @@ -515,13 +495,13 @@ uint16_t Node::rset_config(const StringName &p_property, MultiplayerAPI::RPCMode /***** RPC FUNCTIONS ********/ void Node::rpc(const StringName &p_method, VARIANT_ARG_DECLARE) { - VARIANT_ARGPTRS; int argc = 0; for (int i = 0; i < VARIANT_ARG_MAX; i++) { - if (argptr[i]->get_type() == Variant::NIL) + if (argptr[i]->get_type() == Variant::NIL) { break; + } argc++; } @@ -529,13 +509,13 @@ void Node::rpc(const StringName &p_method, VARIANT_ARG_DECLARE) { } void Node::rpc_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_DECLARE) { - VARIANT_ARGPTRS; int argc = 0; for (int i = 0; i < VARIANT_ARG_MAX; i++) { - if (argptr[i]->get_type() == Variant::NIL) + if (argptr[i]->get_type() == Variant::NIL) { break; + } argc++; } @@ -543,13 +523,13 @@ void Node::rpc_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_DECLARE } void Node::rpc_unreliable(const StringName &p_method, VARIANT_ARG_DECLARE) { - VARIANT_ARGPTRS; int argc = 0; for (int i = 0; i < VARIANT_ARG_MAX; i++) { - if (argptr[i]->get_type() == Variant::NIL) + if (argptr[i]->get_type() == Variant::NIL) { break; + } argc++; } @@ -557,13 +537,13 @@ void Node::rpc_unreliable(const StringName &p_method, VARIANT_ARG_DECLARE) { } void Node::rpc_unreliable_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_DECLARE) { - VARIANT_ARGPTRS; int argc = 0; for (int i = 0; i < VARIANT_ARG_MAX; i++) { - if (argptr[i]->get_type() == Variant::NIL) + if (argptr[i]->get_type() == Variant::NIL) { break; + } argc++; } @@ -571,7 +551,6 @@ void Node::rpc_unreliable_id(int p_peer_id, const StringName &p_method, VARIANT_ } Variant Node::_rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 1) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 1; @@ -594,7 +573,6 @@ Variant Node::_rpc_bind(const Variant **p_args, int p_argcount, Callable::CallEr } Variant Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 2; @@ -625,7 +603,6 @@ Variant Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Callable::Cal } Variant Node::_rpc_unreliable_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 1) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 1; @@ -648,7 +625,6 @@ Variant Node::_rpc_unreliable_bind(const Variant **p_args, int p_argcount, Calla } Variant Node::_rpc_unreliable_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 2; @@ -690,31 +666,29 @@ void Node::rsetp(int p_peer_id, bool p_unreliable, const StringName &p_property, /******** RSET *********/ void Node::rset(const StringName &p_property, const Variant &p_value) { - rsetp(0, false, p_property, p_value); } void Node::rset_id(int p_peer_id, const StringName &p_property, const Variant &p_value) { - rsetp(p_peer_id, false, p_property, p_value); } void Node::rset_unreliable(const StringName &p_property, const Variant &p_value) { - rsetp(0, true, p_property, p_value); } void Node::rset_unreliable_id(int p_peer_id, const StringName &p_property, const Variant &p_value) { - rsetp(p_peer_id, true, p_property, p_value); } //////////// end of rpc Ref<MultiplayerAPI> Node::get_multiplayer() const { - if (multiplayer.is_valid()) + if (multiplayer.is_valid()) { return multiplayer; - if (!is_inside_tree()) + } + if (!is_inside_tree()) { return Ref<MultiplayerAPI>(); + } return get_tree()->get_multiplayer(); } @@ -723,7 +697,6 @@ Ref<MultiplayerAPI> Node::get_custom_multiplayer() const { } void Node::set_custom_multiplayer(Ref<MultiplayerAPI> p_multiplayer) { - multiplayer = p_multiplayer; } @@ -742,8 +715,9 @@ StringName Node::get_node_rpc_method(const uint16_t p_rpc_method_id) const { // Make sure this is a node generated ID. if (((1 << 15) & p_rpc_method_id) > 0) { int mid = (~(1 << 15)) & p_rpc_method_id; - if (mid < data.rpc_methods.size()) + if (mid < data.rpc_methods.size()) { return data.rpc_methods[mid].name; + } } return StringName(); } @@ -752,8 +726,9 @@ MultiplayerAPI::RPCMode Node::get_node_rpc_mode_by_id(const uint16_t p_rpc_metho // Make sure this is a node generated ID. if (((1 << 15) & p_rpc_method_id) > 0) { int mid = (~(1 << 15)) & p_rpc_method_id; - if (mid < data.rpc_methods.size()) + if (mid < data.rpc_methods.size()) { return data.rpc_methods[mid].mode; + } } return MultiplayerAPI::RPC_MODE_DISABLED; } @@ -777,8 +752,9 @@ StringName Node::get_node_rset_property(const uint16_t p_rset_property_id) const // Make sure this is a node generated ID. if (((1 << 15) & p_rset_property_id) > 0) { int mid = (~(1 << 15)) & p_rset_property_id; - if (mid < data.rpc_properties.size()) + if (mid < data.rpc_properties.size()) { return data.rpc_properties[mid].name; + } } return StringName(); } @@ -786,8 +762,9 @@ StringName Node::get_node_rset_property(const uint16_t p_rset_property_id) const MultiplayerAPI::RPCMode Node::get_node_rset_mode_by_id(const uint16_t p_rset_property_id) const { if (((1 << 15) & p_rset_property_id) > 0) { int mid = (~(1 << 15)) & p_rset_property_id; - if (mid < data.rpc_properties.size()) + if (mid < data.rpc_properties.size()) { return data.rpc_properties[mid].mode; + } } return MultiplayerAPI::RPC_MODE_DISABLED; } @@ -819,35 +796,41 @@ String Node::get_rpc_md5() const { bool Node::can_process_notification(int p_what) const { switch (p_what) { - case NOTIFICATION_PHYSICS_PROCESS: return data.physics_process; - case NOTIFICATION_PROCESS: return data.idle_process; - case NOTIFICATION_INTERNAL_PROCESS: return data.idle_process_internal; - case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: return data.physics_process_internal; + case NOTIFICATION_PHYSICS_PROCESS: + return data.physics_process; + case NOTIFICATION_PROCESS: + return data.idle_process; + case NOTIFICATION_INTERNAL_PROCESS: + return data.idle_process_internal; + case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: + return data.physics_process_internal; } return true; } bool Node::can_process() const { - ERR_FAIL_COND_V(!is_inside_tree(), false); if (get_tree()->is_paused()) { - - if (data.pause_mode == PAUSE_MODE_STOP) + if (data.pause_mode == PAUSE_MODE_STOP) { return false; - if (data.pause_mode == PAUSE_MODE_PROCESS) + } + if (data.pause_mode == PAUSE_MODE_PROCESS) { return true; + } if (data.pause_mode == PAUSE_MODE_INHERIT) { - - if (!data.pause_owner) + if (!data.pause_owner) { return false; //clearly no pause owner by default + } - if (data.pause_owner->data.pause_mode == PAUSE_MODE_PROCESS) + if (data.pause_owner->data.pause_mode == PAUSE_MODE_PROCESS) { return true; + } - if (data.pause_owner->data.pause_mode == PAUSE_MODE_STOP) + if (data.pause_owner->data.pause_mode == PAUSE_MODE_STOP) { return false; + } } } @@ -855,58 +838,58 @@ bool Node::can_process() const { } float Node::get_physics_process_delta_time() const { - - if (data.tree) + if (data.tree) { return data.tree->get_physics_process_time(); - else + } else { return 0; + } } float Node::get_process_delta_time() const { - - if (data.tree) + if (data.tree) { return data.tree->get_idle_process_time(); - else + } else { return 0; + } } void Node::set_process(bool p_idle_process) { - - if (data.idle_process == p_idle_process) + if (data.idle_process == p_idle_process) { return; + } data.idle_process = p_idle_process; - if (data.idle_process) + if (data.idle_process) { add_to_group("idle_process", false); - else + } else { remove_from_group("idle_process"); + } _change_notify("idle_process"); } bool Node::is_processing() const { - return data.idle_process; } void Node::set_process_internal(bool p_idle_process_internal) { - - if (data.idle_process_internal == p_idle_process_internal) + if (data.idle_process_internal == p_idle_process_internal) { return; + } data.idle_process_internal = p_idle_process_internal; - if (data.idle_process_internal) + if (data.idle_process_internal) { add_to_group("idle_process_internal", false); - else + } else { remove_from_group("idle_process_internal"); + } _change_notify("idle_process_internal"); } bool Node::is_processing_internal() const { - return data.idle_process_internal; } @@ -936,23 +919,24 @@ void Node::set_process_priority(int p_priority) { } int Node::get_process_priority() const { - return data.process_priority; } void Node::set_process_input(bool p_enable) { - - if (p_enable == data.input) + if (p_enable == data.input) { return; + } data.input = p_enable; - if (!is_inside_tree()) + if (!is_inside_tree()) { return; + } - if (p_enable) + if (p_enable) { add_to_group("_vp_input" + itos(get_viewport()->get_instance_id())); - else + } else { remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id())); + } } bool Node::is_processing_input() const { @@ -960,17 +944,19 @@ bool Node::is_processing_input() const { } void Node::set_process_unhandled_input(bool p_enable) { - - if (p_enable == data.unhandled_input) + if (p_enable == data.unhandled_input) { return; + } data.unhandled_input = p_enable; - if (!is_inside_tree()) + if (!is_inside_tree()) { return; + } - if (p_enable) + if (p_enable) { add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id())); - else + } else { remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id())); + } } bool Node::is_processing_unhandled_input() const { @@ -978,17 +964,19 @@ bool Node::is_processing_unhandled_input() const { } void Node::set_process_unhandled_key_input(bool p_enable) { - - if (p_enable == data.unhandled_key_input) + if (p_enable == data.unhandled_key_input) { return; + } data.unhandled_key_input = p_enable; - if (!is_inside_tree()) + if (!is_inside_tree()) { return; + } - if (p_enable) + if (p_enable) { add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id())); - else + } else { remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id())); + } } bool Node::is_processing_unhandled_key_input() const { @@ -996,12 +984,10 @@ bool Node::is_processing_unhandled_key_input() const { } StringName Node::get_name() const { - return data.name; } void Node::_set_name_nocheck(const StringName &p_name) { - data.name = p_name; } @@ -1019,7 +1005,6 @@ bool Node::_validate_node_name(String &p_name) { } void Node::set_name(const String &p_name) { - String name = p_name; _validate_node_name(name); @@ -1027,14 +1012,12 @@ void Node::set_name(const String &p_name) { data.name = name; if (data.parent) { - data.parent->_validate_child_name(this); } propagate_notification(NOTIFICATION_PATH_CHANGED); if (is_inside_tree()) { - emit_signal("renamed"); get_tree()->node_renamed(this); get_tree()->tree_changed(); @@ -1049,13 +1032,11 @@ void Node::init_node_hrcr() { } void Node::set_human_readable_collision_renaming(bool p_enabled) { - node_hrcr = p_enabled; } #ifdef TOOLS_ENABLED String Node::validate_child_name(Node *p_child) { - StringName name = p_child->data.name; _generate_serial_child_name(p_child, name); return name; @@ -1063,11 +1044,9 @@ String Node::validate_child_name(Node *p_child) { #endif void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { - /* Make sure the name is unique */ if (node_hrcr || 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 @@ -1076,7 +1055,6 @@ void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { p_child->data.name = name; } else { - //this approach to autoset node names is fast but not as readable //it's the default and reserves the '@' character for unique names. @@ -1091,8 +1069,9 @@ void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { int cc = data.children.size(); for (int i = 0; i < cc; i++) { - if (children[i] == p_child) + if (children[i] == p_child) { continue; + } if (children[i]->data.name == p_child->data.name) { unique = false; break; @@ -1101,7 +1080,6 @@ void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { } if (!unique) { - ERR_FAIL_COND(!node_hrcr_count.ref()); String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get()); p_child->data.name = name; @@ -1111,7 +1089,6 @@ void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) { // Return s + 1 as if it were an integer String increase_numeric_string(const String &s) { - String res = s; bool carry = res.length() > 0; @@ -1136,7 +1113,6 @@ String increase_numeric_string(const String &s) { } void Node::_generate_serial_child_name(const Node *p_child, StringName &name) const { - if (name == StringName()) { //no name and a new nade is needed, create one. @@ -1161,7 +1137,6 @@ void Node::_generate_serial_child_name(const Node *p_child, StringName &name) co const Node *const *children_ptr = data.children.ptr(); { - bool exists = false; for (int i = 0; i < cc; i++) { @@ -1248,7 +1223,6 @@ void Node::_add_child_nocheck(Node *p_child, const StringName &p_name) { } void Node::add_child(Node *p_child, bool p_legible_unique_name) { - ERR_FAIL_NULL(p_child); ERR_FAIL_COND_MSG(p_child == this, "Can't add child '" + p_child->get_name() + "' to itself."); // adding to itself! ERR_FAIL_COND_MSG(p_child->data.parent, "Can't add child '" + p_child->get_name() + "' to '" + get_name() + "', already has a parent '" + p_child->data.parent->get_name() + "'."); //Fail if node has a parent @@ -1260,31 +1234,22 @@ void Node::add_child(Node *p_child, bool p_legible_unique_name) { _add_child_nocheck(p_child, p_child->data.name); } -void Node::add_child_below_node(Node *p_node, Node *p_child, bool p_legible_unique_name) { - - ERR_FAIL_NULL(p_node); - ERR_FAIL_NULL(p_child); +void Node::add_sibling(Node *p_sibling, bool p_legible_unique_name) { + ERR_FAIL_NULL(p_sibling); + ERR_FAIL_COND_MSG(p_sibling == this, "Can't add sibling '" + p_sibling->get_name() + "' to itself."); // adding to itself! + ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, add_sibling() failed. Consider using call_deferred(\"add_sibling\", sibling) instead."); - add_child(p_child, p_legible_unique_name); - - if (is_a_parent_of(p_node)) { - move_child(p_child, p_node->get_position_in_parent() + 1); - } else { - WARN_PRINT("Cannot move under node " + p_node->get_name() + " as " + p_child->get_name() + " does not share a parent."); - } + get_parent()->add_child(p_sibling, p_legible_unique_name); + get_parent()->move_child(p_sibling, this->get_index() + 1); } void Node::_propagate_validate_owner() { - if (data.owner) { - bool found = false; Node *parent = data.parent; while (parent) { - if (parent == data.owner) { - found = true; break; } @@ -1293,20 +1258,17 @@ void Node::_propagate_validate_owner() { } if (!found) { - data.owner->data.owned.erase(data.OW); data.owner = nullptr; } } for (int i = 0; i < data.children.size(); i++) { - data.children[i]->_propagate_validate_owner(); } } void Node::remove_child(Node *p_child) { - ERR_FAIL_NULL(p_child); ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, remove_node() failed. Consider using call_deferred(\"remove_child\", child) instead."); @@ -1322,9 +1284,7 @@ void Node::remove_child(Node *p_child) { if (idx == -1) { //maybe removed while unparenting or something and index was not updated, so just in case the above fails, try this. for (int i = 0; i < child_count; i++) { - if (children[i] == p_child) { - idx = i; break; } @@ -1349,7 +1309,6 @@ void Node::remove_child(Node *p_child) { children = data.children.ptrw(); for (int i = idx; i < child_count; i++) { - children[i]->data.pos = i; children[i]->notification(NOTIFICATION_MOVED_IN_PARENT); } @@ -1366,31 +1325,29 @@ void Node::remove_child(Node *p_child) { } int Node::get_child_count() const { - return data.children.size(); } -Node *Node::get_child(int p_index) const { +Node *Node::get_child(int p_index) const { ERR_FAIL_INDEX_V(p_index, data.children.size(), nullptr); return data.children[p_index]; } Node *Node::_get_child_by_name(const StringName &p_name) const { - int cc = data.children.size(); Node *const *cd = data.children.ptr(); for (int i = 0; i < cc; i++) { - if (cd[i]->data.name == p_name) + if (cd[i]->data.name == p_name) { return cd[i]; + } } return nullptr; } Node *Node::get_node_or_null(const NodePath &p_path) const { - if (p_path.is_empty()) { return nullptr; } @@ -1403,14 +1360,13 @@ Node *Node::get_node_or_null(const NodePath &p_path) const { if (!p_path.is_absolute()) { current = const_cast<Node *>(this); //start from this } else { - root = const_cast<Node *>(this); - while (root->data.parent) + while (root->data.parent) { root = root->data.parent; //start from root + } } for (int i = 0; i < p_path.get_name_count(); i++) { - StringName name = p_path.get_name(i); Node *next = nullptr; @@ -1420,25 +1376,23 @@ Node *Node::get_node_or_null(const NodePath &p_path) const { } else if (name == SceneStringNames::get_singleton()->doubledot) { // .. - if (current == nullptr || !current->data.parent) + if (current == nullptr || !current->data.parent) { return nullptr; + } next = current->data.parent; } else if (current == nullptr) { - - if (name == root->get_name()) + if (name == root->get_name()) { next = root; + } } else { - next = nullptr; for (int j = 0; j < current->data.children.size(); j++) { - Node *child = current->data.children[j]; if (child->data.name == name) { - next = child; break; } @@ -1454,49 +1408,48 @@ Node *Node::get_node_or_null(const NodePath &p_path) const { } Node *Node::get_node(const NodePath &p_path) const { - Node *node = get_node_or_null(p_path); ERR_FAIL_COND_V_MSG(!node, nullptr, "Node not found: " + p_path + "."); return node; } bool Node::has_node(const NodePath &p_path) const { - return get_node_or_null(p_path) != nullptr; } Node *Node::find_node(const String &p_mask, bool p_recursive, bool p_owned) const { - Node *const *cptr = data.children.ptr(); int ccount = data.children.size(); for (int i = 0; i < ccount; i++) { - if (p_owned && !cptr[i]->data.owner) + if (p_owned && !cptr[i]->data.owner) { continue; - if (cptr[i]->data.name.operator String().match(p_mask)) + } + if (cptr[i]->data.name.operator String().match(p_mask)) { return cptr[i]; + } - if (!p_recursive) + if (!p_recursive) { continue; + } Node *ret = cptr[i]->find_node(p_mask, true, p_owned); - if (ret) + if (ret) { return ret; + } } return nullptr; } Node *Node::get_parent() const { - return data.parent; } Node *Node::find_parent(const String &p_mask) const { - Node *p = data.parent; while (p) { - - if (p->data.name.operator String().match(p_mask)) + if (p->data.name.operator String().match(p_mask)) { return p; + } p = p->data.parent; } @@ -1504,13 +1457,12 @@ Node *Node::find_parent(const String &p_mask) const { } bool Node::is_a_parent_of(const Node *p_node) const { - ERR_FAIL_NULL_V(p_node, false); Node *p = p_node->data.parent; while (p) { - - if (p == this) + if (p == this) { return true; + } p = p->data.parent; } @@ -1518,7 +1470,6 @@ bool Node::is_a_parent_of(const Node *p_node) const { } bool Node::is_greater_than(const Node *p_node) const { - ERR_FAIL_NULL_V(p_node, false); ERR_FAIL_COND_V(!data.inside_tree, false); ERR_FAIL_COND_V(!p_node->data.inside_tree, false); @@ -1561,7 +1512,6 @@ bool Node::is_greater_than(const Node *p_node) const { bool res; while (true) { - // using -2 since out-of-tree or nonroot nodes have -1 int this_idx = (idx >= data.depth) ? -2 : this_stack[idx]; int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx]; @@ -1583,18 +1533,19 @@ bool Node::is_greater_than(const Node *p_node) const { } void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) { - - if (data.owner == p_by) + if (data.owner == p_by) { p_owned->push_back(this); + } - for (int i = 0; i < get_child_count(); i++) + for (int i = 0; i < get_child_count(); i++) { get_child(i)->get_owned_by(p_by, p_owned); + } } void Node::_set_owner_nocheck(Node *p_owner) { - - if (data.owner == p_owner) + if (data.owner == p_owner) { return; + } ERR_FAIL_COND(data.owner); data.owner = p_owner; @@ -1603,9 +1554,7 @@ void Node::_set_owner_nocheck(Node *p_owner) { } void Node::set_owner(Node *p_owner) { - if (data.owner) { - data.owner->data.owned.erase(data.OW); data.OW = nullptr; data.owner = nullptr; @@ -1613,14 +1562,14 @@ void Node::set_owner(Node *p_owner) { ERR_FAIL_COND(p_owner == this); - if (!p_owner) + if (!p_owner) { return; + } Node *check = this->get_parent(); bool owner_valid = false; while (check) { - if (check == p_owner) { owner_valid = true; break; @@ -1633,22 +1582,21 @@ void Node::set_owner(Node *p_owner) { _set_owner_nocheck(p_owner); } -Node *Node::get_owner() const { +Node *Node::get_owner() const { return data.owner; } Node *Node::find_common_parent_with(const Node *p_node) const { - - if (this == p_node) + if (this == p_node) { return const_cast<Node *>(p_node); + } Set<const Node *> visited; const Node *n = this; while (n) { - visited.insert(n); n = n->data.parent; } @@ -1656,31 +1604,31 @@ Node *Node::find_common_parent_with(const Node *p_node) const { const Node *common_parent = p_node; while (common_parent) { - - if (visited.has(common_parent)) + if (visited.has(common_parent)) { break; + } common_parent = common_parent->data.parent; } - if (!common_parent) + if (!common_parent) { return nullptr; + } return const_cast<Node *>(common_parent); } NodePath Node::get_path_to(const Node *p_node) const { - ERR_FAIL_NULL_V(p_node, NodePath()); - if (this == p_node) + if (this == p_node) { return NodePath("."); + } Set<const Node *> visited; const Node *n = this; while (n) { - visited.insert(n); n = n->data.parent; } @@ -1688,9 +1636,9 @@ NodePath Node::get_path_to(const Node *p_node) const { const Node *common_parent = p_node; while (common_parent) { - - if (visited.has(common_parent)) + if (visited.has(common_parent)) { break; + } common_parent = common_parent->data.parent; } @@ -1703,7 +1651,6 @@ NodePath Node::get_path_to(const Node *p_node) const { n = p_node; while (n != common_parent) { - path.push_back(n->get_name()); n = n->data.parent; } @@ -1712,7 +1659,6 @@ NodePath Node::get_path_to(const Node *p_node) const { StringName up = String(".."); while (n != common_parent) { - path.push_back(up); n = n->data.parent; } @@ -1723,11 +1669,11 @@ NodePath Node::get_path_to(const Node *p_node) const { } NodePath Node::get_path() const { - ERR_FAIL_COND_V_MSG(!is_inside_tree(), NodePath(), "Cannot get path of node as it is not in a scene tree."); - if (data.path_cache) + if (data.path_cache) { return *data.path_cache; + } const Node *n = this; @@ -1746,16 +1692,15 @@ NodePath Node::get_path() const { } bool Node::is_in_group(const StringName &p_identifier) const { - return data.grouped.has(p_identifier); } void Node::add_to_group(const StringName &p_identifier, bool p_persistent) { - ERR_FAIL_COND(!p_identifier.operator String().length()); - if (data.grouped.has(p_identifier)) + if (data.grouped.has(p_identifier)) { return; + } GroupData gd; @@ -1771,21 +1716,20 @@ void Node::add_to_group(const StringName &p_identifier, bool p_persistent) { } void Node::remove_from_group(const StringName &p_identifier) { - ERR_FAIL_COND(!data.grouped.has(p_identifier)); Map<StringName, GroupData>::Element *E = data.grouped.find(p_identifier); ERR_FAIL_COND(!E); - if (data.tree) + if (data.tree) { data.tree->remove_from_group(E->key(), this); + } data.grouped.erase(E); } Array Node::_get_groups() const { - Array groups; List<GroupInfo> gi; get_groups(&gi); @@ -1797,7 +1741,6 @@ Array Node::_get_groups() const { } void Node::get_groups(List<GroupInfo> *p_groups) const { - for (const Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) { GroupInfo gi; gi.name = E->key(); @@ -1807,7 +1750,6 @@ void Node::get_groups(List<GroupInfo> *p_groups) const { } int Node::get_persistent_group_count() const { - int count = 0; for (const Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) { @@ -1818,8 +1760,8 @@ int Node::get_persistent_group_count() const { return count; } -void Node::_print_tree_pretty(const String &prefix, const bool last) { +void Node::_print_tree_pretty(const String &prefix, const bool last) { String new_prefix = last ? String::utf8(" ┖╴") : String::utf8(" ┠╴"); print_line(prefix + new_prefix + String(get_name())); for (int i = 0; i < data.children.size(); i++) { @@ -1833,21 +1775,19 @@ void Node::print_tree_pretty() { } void Node::print_tree() { - _print_tree(this); } void Node::_print_tree(const Node *p_node) { print_line(String(p_node->get_path_to(this))); - for (int i = 0; i < data.children.size(); i++) + for (int i = 0; i < data.children.size(); i++) { data.children[i]->_print_tree(p_node); + } } void Node::_propagate_reverse_notification(int p_notification) { - data.blocked++; for (int i = data.children.size() - 1; i >= 0; i--) { - data.children[i]->_propagate_reverse_notification(p_notification); } @@ -1856,70 +1796,70 @@ void Node::_propagate_reverse_notification(int p_notification) { } void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) { - ERR_FAIL_COND(!is_inside_tree()); data.blocked++; - if (!p_reverse) + if (!p_reverse) { MessageQueue::get_singleton()->push_notification(this, p_notification); + } for (int i = 0; i < data.children.size(); i++) { - data.children[i]->_propagate_deferred_notification(p_notification, p_reverse); } - if (p_reverse) + if (p_reverse) { MessageQueue::get_singleton()->push_notification(this, p_notification); + } data.blocked--; } void Node::propagate_notification(int p_notification) { - data.blocked++; notification(p_notification); for (int i = 0; i < data.children.size(); i++) { - data.children[i]->propagate_notification(p_notification); } data.blocked--; } void Node::propagate_call(const StringName &p_method, const Array &p_args, const bool p_parent_first) { - data.blocked++; - if (p_parent_first && has_method(p_method)) + if (p_parent_first && has_method(p_method)) { callv(p_method, p_args); + } for (int i = 0; i < data.children.size(); i++) { data.children[i]->propagate_call(p_method, p_args, p_parent_first); } - if (!p_parent_first && has_method(p_method)) + if (!p_parent_first && has_method(p_method)) { callv(p_method, p_args); + } data.blocked--; } void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) { - if (get_owner() == p_owner) + if (get_owner() == p_owner) { set_owner(p_by_owner); + } data.blocked++; - for (int i = 0; i < data.children.size(); i++) + for (int i = 0; i < data.children.size(); i++) { data.children[i]->_propagate_replace_owner(p_owner, p_by_owner); + } data.blocked--; } int Node::get_index() const { - return data.pos; } -void Node::remove_and_skip() { +void Node::remove_and_skip() { ERR_FAIL_COND(!data.parent); Node *new_owner = get_owner(); @@ -1927,12 +1867,12 @@ void Node::remove_and_skip() { List<Node *> children; while (true) { - bool clear = true; for (int i = 0; i < data.children.size(); i++) { Node *c_node = data.children[i]; - if (!c_node->get_owner()) + if (!c_node->get_owner()) { continue; + } remove_child(c_node); c_node->_propagate_replace_owner(this, nullptr); @@ -1941,12 +1881,12 @@ void Node::remove_and_skip() { break; } - if (clear) + if (clear) { break; + } } while (!children.empty()) { - Node *c_node = children.front()->get(); data.parent->add_child(c_node); c_node->_propagate_replace_owner(nullptr, new_owner); @@ -1957,20 +1897,18 @@ void Node::remove_and_skip() { } void Node::set_filename(const String &p_filename) { - data.filename = p_filename; } -String Node::get_filename() const { +String Node::get_filename() const { return data.filename; } void Node::set_editor_description(const String &p_editor_description) { - set_meta("_editor_description_", p_editor_description); } -String Node::get_editor_description() const { +String Node::get_editor_description() const { if (has_meta("_editor_description_")) { return get_meta("_editor_description_"); } else { @@ -1979,7 +1917,6 @@ String Node::get_editor_description() const { } void Node::set_editable_instance(Node *p_node, bool p_editable) { - ERR_FAIL_NULL(p_node); ERR_FAIL_COND(!is_a_parent_of(p_node)); NodePath p = get_path_to(p_node); @@ -1994,80 +1931,65 @@ void Node::set_editable_instance(Node *p_node, bool p_editable) { } bool Node::is_editable_instance(const Node *p_node) const { - - if (!p_node) + if (!p_node) { return false; //easier, null is never editable :) + } ERR_FAIL_COND_V(!is_a_parent_of(p_node), false); NodePath p = get_path_to(p_node); return data.editable_instances.has(p); } void Node::set_editable_instances(const HashMap<NodePath, int> &p_editable_instances) { - data.editable_instances = p_editable_instances; } HashMap<NodePath, int> Node::get_editable_instances() const { - return data.editable_instances; } void Node::set_scene_instance_state(const Ref<SceneState> &p_state) { - data.instance_state = p_state; } Ref<SceneState> Node::get_scene_instance_state() const { - return data.instance_state; } void Node::set_scene_inherited_state(const Ref<SceneState> &p_state) { - data.inherited_state = p_state; } Ref<SceneState> Node::get_scene_inherited_state() const { - return data.inherited_state; } void Node::set_scene_instance_load_placeholder(bool p_enable) { - data.use_placeholder = p_enable; } bool Node::get_scene_instance_load_placeholder() const { - return data.use_placeholder; } -int Node::get_position_in_parent() const { - - return data.pos; -} - Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const { - Node *node = nullptr; bool instanced = false; if (Object::cast_to<InstancePlaceholder>(this)) { - const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this); InstancePlaceholder *nip = memnew(InstancePlaceholder); nip->set_instance_path(ip->get_instance_path()); node = nip; } else if ((p_flags & DUPLICATE_USE_INSTANCING) && get_filename() != String()) { - Ref<PackedScene> res = ResourceLoader::load(get_filename()); ERR_FAIL_COND_V(res.is_null(), nullptr); PackedScene::GenEditState ges = PackedScene::GEN_EDIT_STATE_DISABLED; #ifdef TOOLS_ENABLED - if (p_flags & DUPLICATE_FROM_EDITOR) + if (p_flags & DUPLICATE_FROM_EDITOR) { ges = PackedScene::GEN_EDIT_STATE_INSTANCE; + } #endif node = res->instance(ges); ERR_FAIL_COND_V(!node, nullptr); @@ -2075,12 +1997,12 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const instanced = true; } else { - Object *obj = ClassDB::instance(get_class()); ERR_FAIL_COND_V(!obj, nullptr); node = Object::cast_to<Node>(obj); - if (!node) + if (!node) { memdelete(obj); + } ERR_FAIL_COND_V(!node, nullptr); } @@ -2100,13 +2022,13 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) { for (int i = 0; i < N->get()->get_child_count(); ++i) { - Node *descendant = N->get()->get_child(i); // Skip nodes not really belonging to the instanced hierarchy; they'll be processed normally later // but remember non-instanced nodes that are hidden below instanced ones if (descendant->data.owner != this) { - if (descendant->get_parent() && descendant->get_parent() != this && descendant->get_parent()->data.owner == this && descendant->data.owner != descendant->get_parent()) + if (descendant->get_parent() && descendant->get_parent() != this && descendant->get_parent()->data.owner == this && descendant->data.owner != descendant->get_parent()) { hidden_roots.push_back(descendant); + } continue; } @@ -2116,7 +2038,6 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const } for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) { - Node *current_node = node->get_node(get_path_to(N->get())); ERR_CONTINUE(!current_node); @@ -2132,24 +2053,23 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const N->get()->get_property_list(&plist); for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { - - if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { continue; + } String name = E->get().name; - if (name == script_property_name) + if (name == script_property_name) { continue; + } Variant value = N->get()->get(name).duplicate(true); if (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE) { - Resource *res = Object::cast_to<Resource>(value); if (res) { // Duplicate only if it's a resource current_node->set(name, res->duplicate()); } } else { - current_node->set(name, value); } } @@ -2160,18 +2080,19 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const } #ifdef TOOLS_ENABLED - if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) + if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) { r_duplimap->insert(this, node); + } #endif if (p_flags & DUPLICATE_GROUPS) { List<GroupInfo> gi; get_groups(&gi); for (List<GroupInfo>::Element *E = gi.front(); E; E = E->next()) { - #ifdef TOOLS_ENABLED - if ((p_flags & DUPLICATE_FROM_EDITOR) && !E->get().persistent) + if ((p_flags & DUPLICATE_FROM_EDITOR) && !E->get().persistent) { continue; + } #endif node->add_to_group(E->get().name, E->get().persistent); @@ -2179,15 +2100,15 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const } for (int i = 0; i < get_child_count(); i++) { - - if (get_child(i)->data.parent_owned) + if (get_child(i)->data.parent_owned) { continue; - if (instanced && get_child(i)->data.owner == this) + } + if (instanced && get_child(i)->data.owner == this) { continue; //part of instance + } Node *dup = get_child(i)->_duplicate(p_flags, r_duplimap); if (!dup) { - memdelete(node); return nullptr; } @@ -2199,26 +2120,22 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const } for (List<const Node *>::Element *E = hidden_roots.front(); E; E = E->next()) { - Node *parent = node->get_node(get_path_to(E->get()->data.parent)); if (!parent) { - memdelete(node); return nullptr; } Node *dup = E->get()->_duplicate(p_flags, r_duplimap); if (!dup) { - memdelete(node); return nullptr; } parent->add_child(dup); - int pos = E->get()->get_position_in_parent(); + int pos = E->get()->get_index(); if (pos < parent->get_child_count() - 1) { - parent->move_child(dup, pos); } } @@ -2227,7 +2144,6 @@ Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const } Node *Node::duplicate(int p_flags) const { - Node *dupe = _duplicate(p_flags); if (dupe && (p_flags & DUPLICATE_SIGNALS)) { @@ -2239,7 +2155,6 @@ Node *Node::duplicate(int p_flags) const { #ifdef TOOLS_ENABLED Node *Node::duplicate_from_editor(Map<const Node *, Node *> &r_duplimap) const { - Node *dupe = _duplicate(DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANCING | DUPLICATE_FROM_EDITOR, &r_duplimap); // Duplication of signals must happen after all the node descendants have been copied, @@ -2252,20 +2167,18 @@ Node *Node::duplicate_from_editor(Map<const Node *, Node *> &r_duplimap) const { #endif void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p_reown_map) const { - - if (get_owner() != get_parent()->get_owner()) + if (get_owner() != get_parent()->get_owner()) { return; + } Node *node = nullptr; if (get_filename() != "") { - Ref<PackedScene> res = ResourceLoader::load(get_filename()); ERR_FAIL_COND_MSG(res.is_null(), "Cannot load scene: " + get_filename()); node = res->instance(); ERR_FAIL_COND(!node); } else { - Object *obj = ClassDB::instance(get_class()); ERR_FAIL_COND_MSG(!obj, "Node: Could not duplicate: " + String(get_class()) + "."); node = Object::cast_to<Node>(obj); @@ -2280,9 +2193,9 @@ void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p get_property_list(&plist); for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { - - if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { continue; + } String name = E->get().name; Variant value = get(name).duplicate(true); @@ -2293,16 +2206,18 @@ void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p List<GroupInfo> groups; get_groups(&groups); - for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) + for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) { node->add_to_group(E->get().name, E->get().persistent); + } node->set_name(get_name()); p_new_parent->add_child(node); Node *owner = get_owner(); - if (p_reown_map.has(owner)) + if (p_reown_map.has(owner)) { owner = p_reown_map[owner]; + } if (owner) { NodePath p = get_path_to(owner); @@ -2315,7 +2230,6 @@ void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p } for (int i = 0; i < get_child_count(); i++) { - get_child(i)->_duplicate_and_reown(node, p_reown_map); } } @@ -2324,15 +2238,14 @@ void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p // because re-targeting of connections from some descendant to another is not possible // if the emitter node comes later in tree order than the receiver void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const { - - if (this != p_original && (get_owner() != p_original && get_owner() != p_original->get_owner())) + if (this != p_original && (get_owner() != p_original && get_owner() != p_original->get_owner())) { return; + } List<Connection> conns; get_all_signal_connections(&conns); for (List<Connection>::Element *E = conns.front(); E; E = E->next()) { - if (E->get().flags & CONNECT_PERSIST) { //user connected NodePath p = p_original->get_path_to(this); @@ -2350,8 +2263,9 @@ void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const { // of the duplicated and not yet parented hierarchy then at least try to connect // to the same target as the original - if (p_copy->has_node(ptarget)) + if (p_copy->has_node(ptarget)) { copytarget = p_copy->get_node(ptarget); + } if (copy && copytarget) { const Callable copy_callable = Callable(copytarget, E->get().callable.get_method()); @@ -2368,7 +2282,6 @@ void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const { } Node *Node::duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const { - ERR_FAIL_COND_V(get_filename() != "", nullptr); Object *obj = ClassDB::instance(get_class()); @@ -2386,9 +2299,9 @@ Node *Node::duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const { get_property_list(&plist); for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { - - if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { continue; + } String name = E->get().name; node->set(name, get(name)); } @@ -2396,11 +2309,11 @@ Node *Node::duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const { List<GroupInfo> groups; get_groups(&groups); - for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) + for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) { node->add_to_group(E->get().name, E->get().persistent); + } for (int i = 0; i < get_child_count(); i++) { - get_child(i)->_duplicate_and_reown(node, p_reown_map); } @@ -2412,24 +2325,21 @@ Node *Node::duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const { } static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) { - - if (p_node->get_owner() == p_by) + if (p_node->get_owner() == p_by) { p_owned->push_back(p_node); + } for (int i = 0; i < p_node->get_child_count(); i++) { - find_owned_by(p_by, p_node->get_child(i), p_owned); } } struct _NodeReplaceByPair { - String name; Variant value; }; void Node::replace_by(Node *p_node, bool p_keep_data) { - ERR_FAIL_NULL(p_node); ERR_FAIL_COND(p_node->data.parent); @@ -2440,15 +2350,14 @@ void Node::replace_by(Node *p_node, bool p_keep_data) { List<_NodeReplaceByPair> replace_data; if (p_keep_data) { - List<PropertyInfo> plist; get_property_list(&plist); for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { - _NodeReplaceByPair rd; - if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { continue; + } rd.name = E->get().name; rd.value = get(rd.name); } @@ -2456,29 +2365,29 @@ void Node::replace_by(Node *p_node, bool p_keep_data) { List<GroupInfo> groups; get_groups(&groups); - for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) + for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) { p_node->add_to_group(E->get().name, E->get().persistent); + } } _replace_connections_target(p_node); if (data.owner) { - for (int i = 0; i < get_child_count(); i++) + for (int i = 0; i < get_child_count(); i++) { find_owned_by(data.owner, get_child(i), &owned_by_owner); + } } Node *parent = data.parent; int pos_in_parent = data.pos; if (data.parent) { - parent->remove_child(this); parent->add_child(p_node); parent->move_child(p_node, pos_in_parent); } while (get_child_count()) { - Node *child = get_child(0); remove_child(child); if (!child->is_owned_by_parent()) { @@ -2488,27 +2397,26 @@ void Node::replace_by(Node *p_node, bool p_keep_data) { } p_node->set_owner(owner); - for (int i = 0; i < owned.size(); i++) + for (int i = 0; i < owned.size(); i++) { owned[i]->set_owner(p_node); + } - for (int i = 0; i < owned_by_owner.size(); i++) + for (int i = 0; i < owned_by_owner.size(); i++) { owned_by_owner[i]->set_owner(owner); + } p_node->set_filename(get_filename()); for (List<_NodeReplaceByPair>::Element *E = replace_data.front(); E; E = E->next()) { - p_node->set(E->get().name, E->get().value); } } void Node::_replace_connections_target(Node *p_new_target) { - List<Connection> cl; get_signals_connected_to_this(&cl); for (List<Connection>::Element *E = cl.front(); E; E = E->next()) { - Connection &c = E->get(); if (c.flags & CONNECT_PERSIST) { @@ -2521,41 +2429,45 @@ void Node::_replace_connections_target(Node *p_new_target) { } Vector<Variant> Node::make_binds(VARIANT_ARG_DECLARE) { - Vector<Variant> ret; - if (p_arg1.get_type() == Variant::NIL) + if (p_arg1.get_type() == Variant::NIL) { return ret; - else + } else { ret.push_back(p_arg1); + } - if (p_arg2.get_type() == Variant::NIL) + if (p_arg2.get_type() == Variant::NIL) { return ret; - else + } else { ret.push_back(p_arg2); + } - if (p_arg3.get_type() == Variant::NIL) + if (p_arg3.get_type() == Variant::NIL) { return ret; - else + } else { ret.push_back(p_arg3); + } - if (p_arg4.get_type() == Variant::NIL) + if (p_arg4.get_type() == Variant::NIL) { return ret; - else + } else { ret.push_back(p_arg4); + } - if (p_arg5.get_type() == Variant::NIL) + if (p_arg5.get_type() == Variant::NIL) { return ret; - else + } else { ret.push_back(p_arg5); + } return ret; } bool Node::has_node_and_resource(const NodePath &p_path) const { - - if (!has_node(p_path)) + if (!has_node(p_path)) { return false; + } RES res; Vector<StringName> leftover_path; Node *node = get_node_and_resource(p_path, res, leftover_path, false); @@ -2564,21 +2476,22 @@ bool Node::has_node_and_resource(const NodePath &p_path) const { } Array Node::_get_node_and_resource(const NodePath &p_path) { - RES res; Vector<StringName> leftover_path; Node *node = get_node_and_resource(p_path, res, leftover_path, false); Array result; - if (node) + if (node) { result.push_back(node); - else + } else { result.push_back(Variant()); + } - if (res.is_valid()) + if (res.is_valid()) { result.push_back(res); - else + } else { result.push_back(Variant()); + } result.push_back(NodePath(Vector<StringName>(), leftover_path, false)); @@ -2586,15 +2499,14 @@ Array Node::_get_node_and_resource(const NodePath &p_path) { } Node *Node::get_node_and_resource(const NodePath &p_path, RES &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property) const { - Node *node = get_node(p_path); r_res = RES(); r_leftover_subpath = Vector<StringName>(); - if (!node) + if (!node) { return nullptr; + } if (p_path.get_subname_count()) { - int j = 0; // If not p_last_is_property, we shouldn't consider the last one as part of the resource for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) { @@ -2622,7 +2534,6 @@ Node *Node::get_node_and_resource(const NodePath &p_path, RES &r_res, Vector<Str } void Node::_set_tree(SceneTree *p_tree) { - SceneTree *tree_changed_a = nullptr; SceneTree *tree_changed_b = nullptr; @@ -2637,7 +2548,6 @@ void Node::_set_tree(SceneTree *p_tree) { data.tree = p_tree; if (data.tree) { - _propagate_enter_tree(); if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready _propagate_ready(); //reverse_notification(NOTIFICATION_READY); @@ -2646,21 +2556,24 @@ void Node::_set_tree(SceneTree *p_tree) { tree_changed_b = data.tree; } - if (tree_changed_a) + if (tree_changed_a) { tree_changed_a->tree_changed(); - if (tree_changed_b) + } + if (tree_changed_b) { tree_changed_b->tree_changed(); + } } #ifdef DEBUG_ENABLED static void _Node_debug_sn(Object *p_obj) { - Node *n = Object::cast_to<Node>(p_obj); - if (!n) + if (!n) { return; + } - if (n->is_inside_tree()) + if (n->is_inside_tree()) { return; + } Node *p = n; while (p->get_parent()) { @@ -2668,28 +2581,26 @@ static void _Node_debug_sn(Object *p_obj) { } String path; - if (p == n) + if (p == n) { path = n->get_name(); - else + } else { path = String(p->get_name()) + "/" + p->get_path_to(n); + } print_line(itos(p_obj->get_instance_id()) + " - Stray Node: " + path + " (Type: " + n->get_class() + ")"); } #endif // DEBUG_ENABLED void Node::_print_stray_nodes() { - print_stray_nodes(); } void Node::print_stray_nodes() { - #ifdef DEBUG_ENABLED ObjectDB::debug_objects(_Node_debug_sn); #endif } void Node::queue_delete() { - if (is_inside_tree()) { get_tree()->queue_delete(this); } else { @@ -2697,26 +2608,24 @@ void Node::queue_delete() { } } -Array Node::_get_children() const { - - Array arr; +TypedArray<Node> Node::_get_children() const { + TypedArray<Node> arr; int cc = get_child_count(); arr.resize(cc); - for (int i = 0; i < cc; i++) + for (int i = 0; i < cc; i++) { arr[i] = get_child(i); + } return arr; } void Node::set_import_path(const NodePath &p_import_path) { - #ifdef TOOLS_ENABLED data.import_path = p_import_path; #endif } NodePath Node::get_import_path() const { - #ifdef TOOLS_ENABLED return data.import_path; #else @@ -2725,15 +2634,15 @@ NodePath Node::get_import_path() const { } static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) { - #ifdef TOOLS_ENABLED const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\""; #else const String quote_style = "\""; #endif - if (p_node != p_base && !p_node->get_owner()) + if (p_node != p_base && !p_node->get_owner()) { return; + } String n = p_base->get_path_to(p_node); r_options->push_back(quote_style + n + quote_style); for (int i = 0; i < p_node->get_child_count(); i++) { @@ -2742,17 +2651,14 @@ static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<S } void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { - String pf = p_function; if ((pf == "has_node" || pf == "get_node") && p_idx == 0) { - _add_nodes_to_options(this, this, r_options); } Object::get_argument_options(p_function, p_idx, r_options); } void Node::clear_internal_tree_resource_paths() { - clear_internal_resource_paths(); for (int i = 0; i < data.children.size(); i++) { data.children[i]->clear_internal_tree_resource_paths(); @@ -2760,7 +2666,6 @@ void Node::clear_internal_tree_resource_paths() { } String Node::get_configuration_warning() const { - if (get_script_instance() && get_script_instance()->get_script().is_valid() && get_script_instance()->get_script()->is_tool() && get_script_instance()->has_method("_get_configuration_warning")) { return get_script_instance()->call("_get_configuration_warning"); @@ -2769,10 +2674,10 @@ String Node::get_configuration_warning() const { } void Node::update_configuration_warning() { - #ifdef TOOLS_ENABLED - if (!is_inside_tree()) + if (!is_inside_tree()) { return; + } if (get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root() == this || get_tree()->get_edited_scene_root()->is_a_parent_of(this))) { get_tree()->emit_signal(SceneStringNames::get_singleton()->node_configuration_warning_changed, this); } @@ -2788,7 +2693,6 @@ void Node::set_display_folded(bool p_folded) { } bool Node::is_displayed_folded() const { - return data.display_folded; } @@ -2797,13 +2701,12 @@ void Node::request_ready() { } void Node::_bind_methods() { - GLOBAL_DEF("node/name_num_separator", 0); ProjectSettings::get_singleton()->set_custom_property_info("node/name_num_separator", PropertyInfo(Variant::INT, "node/name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash")); GLOBAL_DEF("node/name_casing", NAME_CASING_PASCAL_CASE); ProjectSettings::get_singleton()->set_custom_property_info("node/name_casing", PropertyInfo(Variant::INT, "node/name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case")); - ClassDB::bind_method(D_METHOD("add_child_below_node", "preceding_node", "node", "legible_unique_name"), &Node::add_child_below_node, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("add_sibling", "sibling", "legible_unique_name"), &Node::add_sibling, DEFVAL(false)); ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name); ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name); @@ -2860,7 +2763,6 @@ void Node::_bind_methods() { ClassDB::bind_method(D_METHOD("get_pause_mode"), &Node::get_pause_mode); ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process); ClassDB::bind_method(D_METHOD("print_stray_nodes"), &Node::_print_stray_nodes); - ClassDB::bind_method(D_METHOD("get_position_in_parent"), &Node::get_position_in_parent); ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded); ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded); @@ -2999,16 +2901,19 @@ void Node::_bind_methods() { String Node::_get_name_num_separator() { switch (ProjectSettings::get_singleton()->get("node/name_num_separator").operator int()) { - case 0: return ""; - case 1: return " "; - case 2: return "_"; - case 3: return "-"; + case 0: + return ""; + case 1: + return " "; + case 2: + return "_"; + case 3: + return "-"; } return " "; } Node::Node() { - data.pos = -1; data.depth = -1; data.blocked = 0; @@ -3042,7 +2947,6 @@ Node::Node() { } Node::~Node() { - data.grouped.clear(); data.owned.clear(); data.children.clear(); |