2021-11-23 16:53:47 +01:00
|
|
|
/*
|
2022-02-21 17:09:00 +01:00
|
|
|
Copyright (c) 2019-2022 Péter Magyar
|
2021-11-23 16:53:47 +01:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2022-02-21 22:02:46 +01:00
|
|
|
#include "prop_2d_utils.h"
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2022-02-21 22:02:46 +01:00
|
|
|
#include "../props/prop_2d_data.h"
|
|
|
|
#include "../props/prop_2d_data_entry.h"
|
2021-11-23 16:53:47 +01:00
|
|
|
|
|
|
|
#include "core/version.h"
|
|
|
|
|
|
|
|
#if VERSION_MINOR >= 4
|
|
|
|
#include "core/math/quick_hull.h"
|
|
|
|
#include "scene/3d/portal.h"
|
|
|
|
#include "scene/3d/room.h"
|
|
|
|
#include "scene/3d/room_manager.h"
|
|
|
|
#endif
|
|
|
|
|
2023-01-09 20:06:10 +01:00
|
|
|
#include "scene/3d/mesh_instance_3d.h"
|
2021-11-23 16:53:47 +01:00
|
|
|
|
|
|
|
#if MESH_DATA_RESOURCE_PRESENT
|
|
|
|
#include "../../mesh_data_resource/nodes/mesh_data_instance.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if VERSION_MAJOR > 3
|
|
|
|
#include "core/config/engine.h"
|
|
|
|
#else
|
|
|
|
#include "core/engine.h"
|
|
|
|
#endif
|
|
|
|
|
2022-02-22 19:05:10 +01:00
|
|
|
#include "scene/2d/node_2d.h"
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Prop2DUtils *Prop2DUtils::_instance;
|
|
|
|
Vector<Ref<Prop2DDataEntry> > Prop2DUtils::_processors;
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Prop2DUtils *Prop2DUtils::get_singleton() {
|
2021-11-23 16:53:47 +01:00
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Ref<Prop2DData> Prop2DUtils::convert_tree(Node *root) {
|
2021-11-23 16:53:47 +01:00
|
|
|
#if VERSION_MAJOR < 4
|
2022-02-21 22:12:25 +01:00
|
|
|
ERR_FAIL_COND_V(!ObjectDB::instance_validate(root), Ref<Prop2DData>());
|
2021-11-23 16:53:47 +01:00
|
|
|
#endif
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Ref<Prop2DData> data;
|
2023-01-09 01:17:02 +01:00
|
|
|
data.instantiate();
|
2022-02-22 19:05:10 +01:00
|
|
|
Transform2D t;
|
2021-11-23 16:53:47 +01:00
|
|
|
|
|
|
|
_convert_tree(data, root, t);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-02-22 19:05:10 +01:00
|
|
|
void Prop2DUtils::_convert_tree(Ref<Prop2DData> prop_data, Node *node, const Transform2D &transform) {
|
2021-11-23 16:53:47 +01:00
|
|
|
#if VERSION_MAJOR < 4
|
|
|
|
ERR_FAIL_COND(!ObjectDB::instance_validate(node));
|
|
|
|
#endif
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
for (int i = 0; i < Prop2DUtils::_processors.size(); ++i) {
|
|
|
|
Ref<Prop2DDataEntry> proc = Prop2DUtils::_processors.get(i);
|
2021-11-23 16:53:47 +01:00
|
|
|
|
|
|
|
ERR_CONTINUE(!proc.is_valid());
|
|
|
|
|
|
|
|
if (proc->processor_handles(node)) {
|
|
|
|
proc->processor_process(prop_data, node, transform);
|
|
|
|
|
|
|
|
if (!proc->processor_evaluate_children()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 19:05:10 +01:00
|
|
|
Node2D *n2d = Object::cast_to<Node2D>(node);
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2022-02-22 19:05:10 +01:00
|
|
|
if (!n2d) {
|
2021-11-23 16:53:47 +01:00
|
|
|
//reset transform
|
2022-02-22 19:05:10 +01:00
|
|
|
Transform2D t;
|
2021-11-23 16:53:47 +01:00
|
|
|
|
|
|
|
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) {
|
2022-02-22 19:05:10 +01:00
|
|
|
_convert_tree(prop_data, node->get_child(i), transform * n2d->get_transform());
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-22 19:05:10 +01:00
|
|
|
_convert_tree(prop_data, node->get_child(i), transform * n2d->get_transform());
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
int Prop2DUtils::add_processor(const Ref<Prop2DDataEntry> &processor) {
|
2021-11-23 16:53:47 +01:00
|
|
|
ERR_FAIL_COND_V(!processor.is_valid(), 0);
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Prop2DUtils::_processors.push_back(processor);
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
return Prop2DUtils::_processors.size() - 1;
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
Ref<Prop2DDataEntry> Prop2DUtils::get_processor(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, Prop2DUtils::_processors.size(), Ref<Prop2DDataEntry>());
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
return Prop2DUtils::_processors[index];
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
void Prop2DUtils::swap_processors(const int index1, const int index2) {
|
|
|
|
ERR_FAIL_INDEX(index1, Prop2DUtils::_processors.size());
|
|
|
|
ERR_FAIL_INDEX(index2, Prop2DUtils::_processors.size());
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Ref<Prop2DDataEntry> a = Prop2DUtils::_processors.get(index1);
|
|
|
|
Prop2DUtils::_processors.set(index1, Prop2DUtils::_processors.get(index2));
|
|
|
|
Prop2DUtils::_processors.set(index2, a);
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
void Prop2DUtils::remove_processor(const int index) {
|
|
|
|
ERR_FAIL_INDEX(index, Prop2DUtils::_processors.size());
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2023-01-09 20:06:10 +01:00
|
|
|
Prop2DUtils::_processors.remove_at(index);
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
int Prop2DUtils::get_processor_count() {
|
|
|
|
return Prop2DUtils::_processors.size();
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Prop2DUtils::Prop2DUtils() {
|
2021-11-23 16:53:47 +01:00
|
|
|
_instance = this;
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Prop2DUtils::~Prop2DUtils() {
|
2021-11-23 16:53:47 +01:00
|
|
|
_instance = NULL;
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
Prop2DUtils::_processors.clear();
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
void Prop2DUtils::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("convert_tree", "root"), &Prop2DUtils::convert_tree);
|
2021-11-23 16:53:47 +01:00
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
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);
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 22:12:25 +01:00
|
|
|
int Prop2DUtils::_add_processor_bind(const Ref<Prop2DDataEntry> &processor) {
|
|
|
|
return Prop2DUtils::add_processor(processor);
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
Ref<Prop2DDataEntry> Prop2DUtils::_get_processor_bind(const int index) {
|
|
|
|
return Prop2DUtils::get_processor(index);
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
void Prop2DUtils::_swap_processors_bind(const int index1, const int index2) {
|
|
|
|
Prop2DUtils::swap_processors(index1, index2);
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
void Prop2DUtils::_remove_processor_bind(const int index) {
|
|
|
|
Prop2DUtils::remove_processor(index);
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|
2022-02-21 22:12:25 +01:00
|
|
|
int Prop2DUtils::_get_processor_count_bind() {
|
|
|
|
return Prop2DUtils::get_processor_count();
|
2021-11-23 16:53:47 +01:00
|
|
|
}
|