Added a map changed signal to the VertexLights2DServer.

This commit is contained in:
Relintai 2024-03-26 12:11:00 +01:00
parent d2ed14cf26
commit 862ab3c5ca
3 changed files with 86 additions and 11 deletions

View File

@ -33,8 +33,8 @@
#include "core/config/engine.h"
#include "vertex_lights_2d_server.h"
#include "vertex_light_2d.h"
#include "vertex_lights_2d_server.h"
VertexLights2DServer *vertex_lights_2d_server = NULL;
@ -43,19 +43,19 @@ void register_vertex_lights_2d_types(ModuleRegistrationLevel p_level) {
vertex_lights_2d_server = memnew(VertexLights2DServer);
ClassDB::register_class<VertexLights2DServer>();
Engine::get_singleton()->add_singleton(Engine::Singleton("VertexLights2DServer", VertexLights2DServer::get_singleton()));
}
if (p_level == MODULE_REGISTRATION_LEVEL_SCENE) {
} else if (p_level == MODULE_REGISTRATION_LEVEL_SCENE) {
ClassDB::register_class<VertexLight2D>();
} else if (p_level == MODULE_REGISTRATION_LEVEL_FINALIZE) {
vertex_lights_2d_server->init();
}
}
void unregister_vertex_lights_2d_types(ModuleRegistrationLevel p_level) {
if (p_level == MODULE_REGISTRATION_LEVEL_SINGLETON) {
if (vertex_lights_2d_server) {
memdelete(vertex_lights_2d_server);
} else if (p_level == MODULE_REGISTRATION_LEVEL_FINALIZE) {
vertex_lights_2d_server->finalize();
}
}
}

View File

@ -33,6 +33,8 @@
#include "core/config/project_settings.h"
#include "scene/main/scene_tree.h"
// Defaults
Vector2i VertexLights2DServer::get_default_quadrant_size() const {
return _default_quadrant_size;
@ -61,6 +63,8 @@ void VertexLights2DServer::map_set_quadrant_size(RID p_map, const Vector2i &p_si
map->quadrant_size = p_size;
map->recreate_quadrants();
_map_changed(map);
}
Array VertexLights2DServer::map_get_lights(RID p_map) const {
@ -87,6 +91,8 @@ void VertexLights2DServer::map_clear(RID p_map) {
ERR_FAIL_COND(map == NULL);
map->clear();
_map_changed(map);
}
// Lights
@ -114,11 +120,13 @@ void VertexLights2DServer::light_set_map(RID p_light, RID p_map) {
VertexLightMap2D *map = map_owner.getornull(p_map);
if (light->map) {
_light_changed(light);
light->map->remove_light(light);
}
if (map) {
map->add_light(light);
_light_changed(light);
}
}
@ -133,6 +141,8 @@ void VertexLights2DServer::light_set_enabled(RID p_light, const bool p_enabled)
ERR_FAIL_COND(light == NULL);
light->enabled = p_enabled;
_light_changed(light);
}
Vector2 VertexLights2DServer::light_get_position(RID p_light) {
@ -152,6 +162,8 @@ void VertexLights2DServer::light_set_position(RID p_light, const Vector2 &p_posi
}
light->position = p_position;
_light_changed(light);
}
Vector2i VertexLights2DServer::light_get_range(RID p_light) {
@ -165,6 +177,8 @@ void VertexLights2DServer::light_set_range(RID p_light, const Vector2i &p_range)
ERR_FAIL_COND(light == NULL);
light->range = p_range;
_light_changed(light);
}
real_t VertexLights2DServer::light_get_attenuation(RID p_light) {
@ -178,6 +192,8 @@ void VertexLights2DServer::light_set_attenuation(RID p_light, const real_t p_att
ERR_FAIL_COND(light == NULL);
light->attenuation = p_attenuation;
_light_changed(light);
}
Color VertexLights2DServer::light_get_color(RID p_light) {
@ -191,6 +207,8 @@ void VertexLights2DServer::light_set_color(RID p_light, const Color &p_color) {
ERR_FAIL_COND(light == NULL);
light->color = p_color;
_light_changed(light);
}
VertexLights2DServer::VertexLight2DMode VertexLights2DServer::light_get_mode(RID p_light) {
@ -204,6 +222,8 @@ void VertexLights2DServer::light_set_mode(RID p_light, const VertexLights2DServe
ERR_FAIL_COND(light == NULL);
light->mode = p_mode;
_light_changed(light);
}
Vector2i VertexLights2DServer::light_get_z_range(RID p_light) {
@ -217,6 +237,8 @@ void VertexLights2DServer::light_set_z_range(RID p_light, const Vector2i &p_z_ra
ERR_FAIL_COND(light == NULL);
light->z_range = p_z_range;
_light_changed(light);
}
Vector2i VertexLights2DServer::light_get_layer_range(RID p_light) {
@ -230,6 +252,8 @@ void VertexLights2DServer::light_set_layer_range(RID p_light, const Vector2i &p_
ERR_FAIL_COND(light == NULL);
light->layer_range = p_layer_range;
_light_changed(light);
}
int VertexLights2DServer::light_get_item_cull_mask(RID p_light) {
@ -243,6 +267,8 @@ void VertexLights2DServer::light_set_item_cull_mask(RID p_light, const int p_ite
ERR_FAIL_COND(light == NULL);
light->item_cull_mask = p_item_cull_mask;
_light_changed(light);
}
// Sampling
@ -274,6 +300,7 @@ void VertexLights2DServer::free(RID p_rid) {
VertexLightData2D *light = light_owner.get(p_rid);
if (light->map) {
_light_changed(light);
light->map->remove_light(light);
}
@ -287,6 +314,23 @@ void VertexLights2DServer::free(RID p_rid) {
}
}
void VertexLights2DServer::init() {
if (SceneTree::get_singleton()) {
SceneTree::get_singleton()->add_idle_callback(VertexLights2DServer::_scene_tree_idle_callback);
}
}
void VertexLights2DServer::finalize() {
}
void VertexLights2DServer::flush_notifications() {
for (HashSet<RID>::Iterator iter = _changed_maps.begin(); iter; ++iter) {
emit_signal(_map_changed_name, iter.key());
}
_changed_maps.clear();
}
VertexLights2DServer::VertexLights2DServer() {
ERR_FAIL_COND(_self);
@ -294,6 +338,8 @@ VertexLights2DServer::VertexLights2DServer() {
GLOBAL_DEF("vertex_lights_2d/default_quadrant_size", Vector2i(256, 256));
_default_quadrant_size = GLOBAL_GET("vertex_lights_2d/default_quadrant_size");
_map_changed_name = "map_changed";
}
VertexLights2DServer::~VertexLights2DServer() {
@ -301,6 +347,8 @@ VertexLights2DServer::~VertexLights2DServer() {
}
void VertexLights2DServer::_bind_methods() {
ADD_SIGNAL(MethodInfo("map_changed", PropertyInfo(Variant::RID, "map")));
ClassDB::bind_method(D_METHOD("get_default_quadrant_size"), &VertexLights2DServer::get_default_quadrant_size);
ClassDB::bind_method(D_METHOD("set_default_quadrant_size", "size"), &VertexLights2DServer::set_default_quadrant_size);
@ -355,6 +403,8 @@ void VertexLights2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("free", "rid"), &VertexLights2DServer::free);
ClassDB::bind_method(D_METHOD("flush_notifications"), &VertexLights2DServer::flush_notifications);
BIND_ENUM_CONSTANT(VERTEX_LIGHT_2D_MODE_ADD);
BIND_ENUM_CONSTANT(VERTEX_LIGHT_2D_MODE_SUB);
BIND_ENUM_CONSTANT(VERTEX_LIGHT_2D_MODE_MIX);

View File

@ -35,6 +35,7 @@
#include "core/object/object.h"
#include "core/containers/hash_map.h"
#include "core/containers/hash_set.h"
#include "core/containers/list.h"
#include "core/containers/rid.h"
#include "core/containers/vector.h"
@ -110,6 +111,10 @@ public:
void free(RID p_rid);
void init();
void finalize();
void flush_notifications();
_FORCE_INLINE_ static VertexLights2DServer *get_singleton() {
return _self;
}
@ -118,6 +123,10 @@ public:
~VertexLights2DServer();
protected:
static void _scene_tree_idle_callback() {
VertexLights2DServer::get_singleton()->flush_notifications();
}
static void _bind_methods();
class VertexLightMap2D;
@ -202,11 +211,27 @@ protected:
}
};
_FORCE_INLINE_ void _light_changed(const VertexLightData2D *p_light) const {
if (p_light && p_light->map) {
_changed_maps.insert(p_light->map->self);
}
}
_FORCE_INLINE_ void _map_changed(const VertexLightMap2D *p_map) const {
if (p_map) {
_changed_maps.insert(p_map->self);
}
}
mutable RID_Owner<VertexLightMap2D> map_owner;
mutable RID_Owner<VertexLightData2D> light_owner;
Vector2i _default_quadrant_size;
// Maybe an api could be adde that's per quadrant
mutable HashSet<RID> _changed_maps;
StringName _map_changed_name;
static VertexLights2DServer *_self;
};