From ab644a991340f5bd2baf02f371c8ff67409db879 Mon Sep 17 00:00:00 2001 From: BimDav <67792882+BimDav@users.noreply.github.com> Date: Tue, 19 Jul 2022 10:56:47 +0200 Subject: [PATCH] Add option in VisibilityEnabler2D to hide the parent for better performance --- doc/classes/VisibilityEnabler2D.xml | 7 ++++++- scene/2d/visibility_notifier_2d.cpp | 24 ++++++++++++++++++++++++ scene/2d/visibility_notifier_2d.h | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/doc/classes/VisibilityEnabler2D.xml b/doc/classes/VisibilityEnabler2D.xml index 115f7a2429c..a25f665a4f8 100644 --- a/doc/classes/VisibilityEnabler2D.xml +++ b/doc/classes/VisibilityEnabler2D.xml @@ -47,6 +47,9 @@ If [code]true[/code], the parent's [method Node._process] will be stopped. + + If [code]true[/code] and the parent is a [CanvasItem], the parent will be hidden. + @@ -67,7 +70,9 @@ This enabler will stop [AnimatedSprite] nodes animations. - + + + Represents the size of the [enum Enabler] enum. diff --git a/scene/2d/visibility_notifier_2d.cpp b/scene/2d/visibility_notifier_2d.cpp index 5d414204809..4d9ff910821 100644 --- a/scene/2d/visibility_notifier_2d.cpp +++ b/scene/2d/visibility_notifier_2d.cpp @@ -153,6 +153,13 @@ void VisibilityEnabler2D::_screen_enter() { if (enabler[ENABLER_PARENT_PROCESS] && get_parent()) { get_parent()->set_process(true); } + if (enabler[ENABLER_PARENT_VISIBILITY] && get_parent()) { + CanvasItem *ci = Object::cast_to(get_parent()); + + if (ci) { + ci->set_visible(true); + } + } visible = true; } @@ -168,6 +175,13 @@ void VisibilityEnabler2D::_screen_exit() { if (enabler[ENABLER_PARENT_PROCESS] && get_parent()) { get_parent()->set_process(false); } + if (enabler[ENABLER_PARENT_VISIBILITY] && get_parent()) { + CanvasItem *ci = Object::cast_to(get_parent()); + + if (ci) { + ci->set_visible(false); + } + } visible = false; } @@ -248,6 +262,14 @@ void VisibilityEnabler2D::_notification(int p_what) { get_parent()->connect(SceneStringNames::get_singleton()->ready, get_parent(), "set_process", varray(false), CONNECT_REFERENCE_COUNTED); } + if (enabler[ENABLER_PARENT_VISIBILITY] && get_parent()) { + CanvasItem *ci = Object::cast_to(get_parent()); + + if (ci) { + ci->connect(SceneStringNames::get_singleton()->ready, + ci, "set_visible", varray(false), CONNECT_REFERENCE_COUNTED); + } + } } if (p_what == NOTIFICATION_EXIT_TREE) { @@ -336,6 +358,7 @@ void VisibilityEnabler2D::_bind_methods() { ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animated_sprites"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATED_SPRITES); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_PROCESS); ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "physics_process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_PHYSICS_PROCESS); + ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "visibility_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_VISIBILITY); BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATIONS); BIND_ENUM_CONSTANT(ENABLER_FREEZE_BODIES); @@ -343,6 +366,7 @@ void VisibilityEnabler2D::_bind_methods() { BIND_ENUM_CONSTANT(ENABLER_PARENT_PROCESS); BIND_ENUM_CONSTANT(ENABLER_PARENT_PHYSICS_PROCESS); BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATED_SPRITES); + BIND_ENUM_CONSTANT(ENABLER_PARENT_VISIBILITY); BIND_ENUM_CONSTANT(ENABLER_MAX); } diff --git a/scene/2d/visibility_notifier_2d.h b/scene/2d/visibility_notifier_2d.h index c964f8a9d7f..5a7da01a206 100644 --- a/scene/2d/visibility_notifier_2d.h +++ b/scene/2d/visibility_notifier_2d.h @@ -78,6 +78,7 @@ public: ENABLER_PARENT_PROCESS, ENABLER_PARENT_PHYSICS_PROCESS, ENABLER_PAUSE_ANIMATED_SPRITES, + ENABLER_PARENT_VISIBILITY, ENABLER_MAX };