pandemonium_engine/modules/props_2d/singleton/prop_2d_utils.cpp

161 lines
4.5 KiB
C++
Raw Normal View History

2023-12-17 22:59:50 +01:00
#include "prop_2d_utils.h"
#include "../props/prop_2d_data.h"
#include "../props/prop_2d_data_entry.h"
#include "core/math/quick_hull.h"
#include "scene/3d/portal.h"
#include "scene/3d/room.h"
#include "scene/3d/room_manager.h"
#include "scene/3d/mesh_instance.h"
#include "modules/modules_enabled.gen.h"
#ifdef MODULE_MESH_DATA_RESOURCE_ENABLED
#include "../../mesh_data_resource/nodes/mesh_data_instance.h"
#endif
2022-08-17 15:35:28 +02:00
#include "core/config/engine.h"
#include "scene/main/node_2d.h"
Prop2DUtils *Prop2DUtils::_instance;
2022-03-18 02:55:27 +01:00
Vector<Ref<Prop2DDataEntry>> Prop2DUtils::_processors;
Prop2DUtils *Prop2DUtils::get_singleton() {
return _instance;
}
Ref<Prop2DData> Prop2DUtils::convert_tree(Node *root) {
ERR_FAIL_COND_V(!ObjectDB::instance_validate(root), Ref<Prop2DData>());
Ref<Prop2DData> data;
data.instance();
Transform2D t;
_convert_tree(data, root, t);
return data;
}
void Prop2DUtils::_convert_tree(Ref<Prop2DData> prop_data, Node *node, const Transform2D &transform) {
ERR_FAIL_COND(!ObjectDB::instance_validate(node));
for (int i = 0; i < Prop2DUtils::_processors.size(); ++i) {
Ref<Prop2DDataEntry> proc = Prop2DUtils::_processors.get(i);
ERR_CONTINUE(!proc.is_valid());
if (proc->processor_handles(node)) {
proc->processor_process(prop_data, node, transform);
if (!proc->processor_evaluate_children()) {
return;
}
break;
}
}
Node2D *n2d = Object::cast_to<Node2D>(node);
if (!n2d) {
//reset transform
Transform2D t;
for (int i = 0; i < node->get_child_count(); ++i) {
Node *child = node->get_child(i);
if (Engine::get_singleton()->is_editor_hint()) {
//Skip it if it's hidden from the tree
if (child->get_owner() != NULL) {
_convert_tree(prop_data, node->get_child(i), t);
}
} else {
_convert_tree(prop_data, node->get_child(i), t);
}
}
} else {
for (int i = 0; i < node->get_child_count(); ++i) {
Node *child = node->get_child(i);
if (Engine::get_singleton()->is_editor_hint()) {
//Skip it if it's hidden from the tree
if (child->get_owner() != NULL) {
_convert_tree(prop_data, node->get_child(i), transform * n2d->get_transform());
}
} else {
_convert_tree(prop_data, node->get_child(i), transform * n2d->get_transform());
}
}
}
}
int Prop2DUtils::add_processor(const Ref<Prop2DDataEntry> &processor) {
ERR_FAIL_COND_V(!processor.is_valid(), 0);
Prop2DUtils::_processors.push_back(processor);
return Prop2DUtils::_processors.size() - 1;
}
Ref<Prop2DDataEntry> Prop2DUtils::get_processor(const int index) {
ERR_FAIL_INDEX_V(index, Prop2DUtils::_processors.size(), Ref<Prop2DDataEntry>());
return Prop2DUtils::_processors[index];
}
void Prop2DUtils::swap_processors(const int index1, const int index2) {
ERR_FAIL_INDEX(index1, Prop2DUtils::_processors.size());
ERR_FAIL_INDEX(index2, Prop2DUtils::_processors.size());
Ref<Prop2DDataEntry> a = Prop2DUtils::_processors.get(index1);
Prop2DUtils::_processors.set(index1, Prop2DUtils::_processors.get(index2));
Prop2DUtils::_processors.set(index2, a);
}
void Prop2DUtils::remove_processor(const int index) {
ERR_FAIL_INDEX(index, Prop2DUtils::_processors.size());
Prop2DUtils::_processors.remove(index);
}
int Prop2DUtils::get_processor_count() {
return Prop2DUtils::_processors.size();
}
Prop2DUtils::Prop2DUtils() {
_instance = this;
}
Prop2DUtils::~Prop2DUtils() {
_instance = NULL;
Prop2DUtils::_processors.clear();
}
void Prop2DUtils::_bind_methods() {
ClassDB::bind_method(D_METHOD("convert_tree", "root"), &Prop2DUtils::convert_tree);
ClassDB::bind_method(D_METHOD("add_processor", "processor"), &Prop2DUtils::_add_processor_bind);
ClassDB::bind_method(D_METHOD("get_processor", "index"), &Prop2DUtils::_get_processor_bind);
ClassDB::bind_method(D_METHOD("swap_processors", "index1", "index2"), &Prop2DUtils::_swap_processors_bind);
ClassDB::bind_method(D_METHOD("remove_processor", "index"), &Prop2DUtils::_remove_processor_bind);
ClassDB::bind_method(D_METHOD("get_processor_count"), &Prop2DUtils::_get_processor_count_bind);
}
int Prop2DUtils::_add_processor_bind(const Ref<Prop2DDataEntry> &processor) {
return Prop2DUtils::add_processor(processor);
}
Ref<Prop2DDataEntry> Prop2DUtils::_get_processor_bind(const int index) {
return Prop2DUtils::get_processor(index);
}
void Prop2DUtils::_swap_processors_bind(const int index1, const int index2) {
Prop2DUtils::swap_processors(index1, index2);
}
void Prop2DUtils::_remove_processor_bind(const int index) {
Prop2DUtils::remove_processor(index);
}
int Prop2DUtils::_get_processor_count_bind() {
return Prop2DUtils::get_processor_count();
}