From 02fe45b08a4d2bd99e23ca91be944234b395db54 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 24 Mar 2024 22:05:00 +0100 Subject: [PATCH] Added enabled property for VertexLight2Ds. --- modules/vertex_lights_2d/vertex_light_2d.cpp | 29 ++++++++++++++++++- modules/vertex_lights_2d/vertex_light_2d.h | 8 ++++- modules/vertex_lights_2d/vertex_light_data.h | 4 ++- .../vertex_lights_2d_server.cpp | 18 +++++++++++- .../vertex_lights_2d_server.h | 3 ++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/modules/vertex_lights_2d/vertex_light_2d.cpp b/modules/vertex_lights_2d/vertex_light_2d.cpp index 4c98fea9a..78d2d6d45 100644 --- a/modules/vertex_lights_2d/vertex_light_2d.cpp +++ b/modules/vertex_lights_2d/vertex_light_2d.cpp @@ -31,6 +31,17 @@ #include "vertex_light_2d.h" +#include "core/config/engine.h" + +bool VertexLight2D::get_is_enabled() { + return _enabled; +} +void VertexLight2D::set_enabled(const bool p_enabled) { + _enabled = p_enabled; + + _update_light_visibility(); +} + Color VertexLight2D::get_color() { return _color; } @@ -78,7 +89,8 @@ void VertexLight2D::set_item_cull_mask(const int p_item_cull_mask) { VertexLight2D::VertexLight2D() { _vertex_light = RID_PRIME(VertexLights2DServer::get_singleton()->light_create()); - + + _enabled = true; _color = Color(1, 1, 1, 1); _item_cull_mask = 1; _z_range = Vector2i(-1024, 1024); @@ -96,10 +108,25 @@ void VertexLight2D::_notification(int p_what) { case NOTIFICATION_TRANSFORM_CHANGED: { VertexLights2DServer::get_singleton()->light_set_position(_vertex_light, get_global_transform().get_origin()); } break; + case NOTIFICATION_VISIBILITY_CHANGED: { + _update_light_visibility(); + } break; } } +void VertexLight2D::_update_light_visibility() { + if (!is_inside_tree()) { + return; + } + + VertexLights2DServer::get_singleton()->light_set_enabled(_vertex_light, _enabled && is_visible_in_tree()); +} + void VertexLight2D::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_is_enabled"), &VertexLight2D::get_is_enabled); + ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &VertexLight2D::set_enabled); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_is_enabled"); + ClassDB::bind_method(D_METHOD("get_color"), &VertexLight2D::get_color); ClassDB::bind_method(D_METHOD("set_color", "color"), &VertexLight2D::set_color); ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); diff --git a/modules/vertex_lights_2d/vertex_light_2d.h b/modules/vertex_lights_2d/vertex_light_2d.h index 12dbf89fb..8acb0e238 100644 --- a/modules/vertex_lights_2d/vertex_light_2d.h +++ b/modules/vertex_lights_2d/vertex_light_2d.h @@ -52,6 +52,9 @@ public: //VERTEX_LIGHT_2D_MODE_MASK = VertexLights2DServer::VERTEX_LIGHT_2D_MODE_MASK }; + bool get_is_enabled(); + void set_enabled(const bool p_enabled); + Color get_color(); void set_color(const Color &p_color); @@ -72,11 +75,14 @@ public: protected: void _notification(int p_what); + + void _update_light_visibility(); static void _bind_methods(); RID _vertex_light; - + + bool _enabled; Color _color; VertexLight2DMode _mode; Vector2i _z_range; diff --git a/modules/vertex_lights_2d/vertex_light_data.h b/modules/vertex_lights_2d/vertex_light_data.h index b3d9df5a0..743c3b598 100644 --- a/modules/vertex_lights_2d/vertex_light_data.h +++ b/modules/vertex_lights_2d/vertex_light_data.h @@ -47,6 +47,7 @@ class VertexLightData2D; class VertexLightData2D : public RID_Data { public: + bool enabled; Vector2 position; Color color; VertexLights2DServer::VertexLight2DMode mode; @@ -62,7 +63,8 @@ public: VertexLightData2D() { map = NULL; quadrant = NULL; - + + enabled = true; color = Color(1, 1, 1, 1); item_cull_mask = 1; z_range = Vector2i(-1024, 1024); diff --git a/modules/vertex_lights_2d/vertex_lights_2d_server.cpp b/modules/vertex_lights_2d/vertex_lights_2d_server.cpp index f9a1faf9d..b0089a62c 100644 --- a/modules/vertex_lights_2d/vertex_lights_2d_server.cpp +++ b/modules/vertex_lights_2d/vertex_lights_2d_server.cpp @@ -124,6 +124,19 @@ void VertexLights2DServer::light_set_map(RID p_light, RID p_map) { } } +bool VertexLights2DServer::light_get_is_enabled(RID p_light) { + const VertexLightData2D *light = light_owner.getornull(p_light); + ERR_FAIL_COND_V(light == NULL, false); + + return light->enabled; +} +void VertexLights2DServer::light_set_enabled(RID p_light, const bool p_enabled) { + VertexLightData2D *light = light_owner.getornull(p_light); + ERR_FAIL_COND(light == NULL); + + light->enabled = p_enabled; +} + Vector2 VertexLights2DServer::light_get_position(RID p_light) { const VertexLightData2D *light = light_owner.getornull(p_light); ERR_FAIL_COND_V(light == NULL, Vector2()); @@ -230,7 +243,7 @@ void VertexLights2DServer::free(RID p_rid) { if (light->map) { light->map->remove_light(light); } - + light->self = RID(); light_owner.free(p_rid); @@ -274,6 +287,9 @@ void VertexLights2DServer::_bind_methods() { ClassDB::bind_method(D_METHOD("light_get_map", "light"), &VertexLights2DServer::light_get_map); ClassDB::bind_method(D_METHOD("light_set_map", "light", "map"), &VertexLights2DServer::light_set_map); + ClassDB::bind_method(D_METHOD("light_get_is_enabled", "light"), &VertexLights2DServer::light_get_is_enabled); + ClassDB::bind_method(D_METHOD("light_set_enabled", "light", "enabled"), &VertexLights2DServer::light_set_enabled); + ClassDB::bind_method(D_METHOD("light_get_position", "light"), &VertexLights2DServer::light_get_position); ClassDB::bind_method(D_METHOD("light_set_position", "light", "position"), &VertexLights2DServer::light_set_position); diff --git a/modules/vertex_lights_2d/vertex_lights_2d_server.h b/modules/vertex_lights_2d/vertex_lights_2d_server.h index aac128a5e..1a3a06a77 100644 --- a/modules/vertex_lights_2d/vertex_lights_2d_server.h +++ b/modules/vertex_lights_2d/vertex_lights_2d_server.h @@ -76,6 +76,9 @@ public: RID light_get_map(RID p_light); void light_set_map(RID p_light, RID p_map); + + bool light_get_is_enabled(RID p_light); + void light_set_enabled(RID p_light, const bool p_enabled); Vector2 light_get_position(RID p_light); void light_set_position(RID p_light, const Vector2 &p_position);