2020-04-03 13:45:55 +02:00
|
|
|
/*
|
2020-06-27 22:29:19 +02:00
|
|
|
Copyright (c) 2019-2020 Péter Magyar
|
2020-04-03 13:45:55 +02: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.
|
|
|
|
*/
|
|
|
|
|
2020-06-27 22:29:19 +02:00
|
|
|
#include "prop_utils.h"
|
2020-04-03 13:45:55 +02:00
|
|
|
|
2020-06-27 23:13:26 +02:00
|
|
|
#include "../props/prop_data.h"
|
2020-07-05 21:55:08 +02:00
|
|
|
#include "../props/prop_data_entry.h"
|
2020-07-06 20:11:23 +02:00
|
|
|
#include "core/engine.h"
|
2020-06-27 23:13:26 +02:00
|
|
|
|
2020-07-29 00:03:05 +02:00
|
|
|
#include "core/version.h"
|
|
|
|
|
2020-06-27 22:29:19 +02:00
|
|
|
PropUtils *PropUtils::_instance;
|
2020-07-29 00:03:05 +02:00
|
|
|
Vector<Ref<PropDataEntry>> PropUtils::_processors;
|
2020-04-03 13:45:55 +02:00
|
|
|
|
2020-06-27 22:29:19 +02:00
|
|
|
PropUtils *PropUtils::get_singleton() {
|
|
|
|
return _instance;
|
|
|
|
}
|
2020-04-03 13:45:55 +02:00
|
|
|
|
2020-06-27 23:13:26 +02:00
|
|
|
Ref<PropData> PropUtils::convert_tree(Node *root) {
|
2020-07-29 00:03:05 +02:00
|
|
|
#if VERSION_MAJOR < 4
|
2020-07-02 23:04:25 +02:00
|
|
|
ERR_FAIL_COND_V(!ObjectDB::instance_validate(root), Ref<PropData>());
|
2020-07-29 00:03:05 +02:00
|
|
|
#endif
|
2020-07-02 23:04:25 +02:00
|
|
|
|
|
|
|
Ref<PropData> data;
|
|
|
|
data.instance();
|
|
|
|
Transform t;
|
|
|
|
|
|
|
|
_convert_tree(data, root, t);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropUtils::_convert_tree(Ref<PropData> prop_data, Node *node, const Transform &transform) {
|
2020-07-29 00:03:05 +02:00
|
|
|
#if VERSION_MAJOR < 4
|
2020-07-02 23:04:25 +02:00
|
|
|
ERR_FAIL_COND(!ObjectDB::instance_validate(node));
|
2020-07-29 00:03:05 +02:00
|
|
|
#endif
|
2020-07-02 23:04:25 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < PropUtils::_processors.size(); ++i) {
|
2020-07-05 21:55:08 +02:00
|
|
|
Ref<PropDataEntry> proc = PropUtils::_processors.get(i);
|
2020-07-02 23:04:25 +02:00
|
|
|
|
|
|
|
ERR_CONTINUE(!proc.is_valid());
|
|
|
|
|
2020-07-05 21:55:08 +02:00
|
|
|
if (proc->processor_handles(node)) {
|
|
|
|
proc->processor_process(prop_data, node, transform);
|
2020-07-06 13:30:21 +02:00
|
|
|
|
|
|
|
if (!proc->processor_evaluate_children()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-02 23:04:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Spatial *sp = Object::cast_to<Spatial>(node);
|
|
|
|
|
|
|
|
if (!sp) {
|
|
|
|
//reset transform
|
|
|
|
Transform t;
|
|
|
|
|
|
|
|
for (int i = 0; i < node->get_child_count(); ++i) {
|
2020-07-06 20:11:23 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-07-02 23:04:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < node->get_child_count(); ++i) {
|
2020-07-06 20:11:23 +02:00
|
|
|
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 * sp->get_transform());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_convert_tree(prop_data, node->get_child(i), transform * sp->get_transform());
|
|
|
|
}
|
2020-07-02 23:04:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-27 23:13:26 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:55:08 +02:00
|
|
|
int PropUtils::add_processor(const Ref<PropDataEntry> &processor) {
|
2020-07-02 23:04:25 +02:00
|
|
|
ERR_FAIL_COND_V(!processor.is_valid(), 0);
|
|
|
|
|
2020-07-02 17:57:11 +02:00
|
|
|
PropUtils::_processors.push_back(processor);
|
|
|
|
|
|
|
|
return PropUtils::_processors.size() - 1;
|
|
|
|
}
|
2020-07-05 21:55:08 +02:00
|
|
|
Ref<PropDataEntry> PropUtils::get_processor(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, PropUtils::_processors.size(), Ref<PropDataEntry>());
|
2020-07-02 17:57:11 +02:00
|
|
|
|
|
|
|
return PropUtils::_processors[index];
|
|
|
|
}
|
|
|
|
void PropUtils::swap_processors(const int index1, const int index2) {
|
|
|
|
ERR_FAIL_INDEX(index1, PropUtils::_processors.size());
|
|
|
|
ERR_FAIL_INDEX(index2, PropUtils::_processors.size());
|
|
|
|
|
2020-07-05 21:55:08 +02:00
|
|
|
Ref<PropDataEntry> a = PropUtils::_processors.get(index1);
|
2020-07-02 17:57:11 +02:00
|
|
|
PropUtils::_processors.set(index1, PropUtils::_processors.get(index2));
|
|
|
|
PropUtils::_processors.set(index2, a);
|
|
|
|
}
|
|
|
|
void PropUtils::remove_processor(const int index) {
|
|
|
|
ERR_FAIL_INDEX(index, PropUtils::_processors.size());
|
|
|
|
|
|
|
|
PropUtils::_processors.remove(index);
|
|
|
|
}
|
|
|
|
int PropUtils::get_processor_count() {
|
|
|
|
return PropUtils::_processors.size();
|
|
|
|
}
|
|
|
|
|
2020-06-27 22:29:19 +02:00
|
|
|
PropUtils::PropUtils() {
|
|
|
|
_instance = this;
|
|
|
|
}
|
2020-04-03 13:45:55 +02:00
|
|
|
|
2020-06-27 22:29:19 +02:00
|
|
|
PropUtils::~PropUtils() {
|
|
|
|
_instance = NULL;
|
2020-07-02 17:57:11 +02:00
|
|
|
|
|
|
|
PropUtils::_processors.clear();
|
2020-06-27 22:29:19 +02:00
|
|
|
}
|
2020-04-03 13:45:55 +02:00
|
|
|
|
2020-06-27 22:29:19 +02:00
|
|
|
void PropUtils::_bind_methods() {
|
2020-06-27 23:13:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("convert_tree", "root"), &PropUtils::convert_tree);
|
2020-07-02 17:57:11 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("add_processor", "processor"), &PropUtils::_add_processor_bind);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_processor", "index"), &PropUtils::_get_processor_bind);
|
|
|
|
ClassDB::bind_method(D_METHOD("swap_processors", "index1", "index2"), &PropUtils::_swap_processors_bind);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_processor", "index"), &PropUtils::_remove_processor_bind);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_processor_count"), &PropUtils::_get_processor_count_bind);
|
|
|
|
}
|
|
|
|
|
2020-07-05 21:55:08 +02:00
|
|
|
int PropUtils::_add_processor_bind(const Ref<PropDataEntry> &processor) {
|
2020-07-02 17:57:11 +02:00
|
|
|
return PropUtils::add_processor(processor);
|
|
|
|
}
|
2020-07-05 21:55:08 +02:00
|
|
|
Ref<PropDataEntry> PropUtils::_get_processor_bind(const int index) {
|
2020-07-02 17:57:11 +02:00
|
|
|
return PropUtils::get_processor(index);
|
|
|
|
}
|
|
|
|
void PropUtils::_swap_processors_bind(const int index1, const int index2) {
|
|
|
|
PropUtils::swap_processors(index1, index2);
|
|
|
|
}
|
|
|
|
void PropUtils::_remove_processor_bind(const int index) {
|
|
|
|
PropUtils::remove_processor(index);
|
|
|
|
}
|
|
|
|
int PropUtils::_get_processor_count_bind() {
|
|
|
|
return PropUtils::get_processor_count();
|
2020-06-27 22:29:19 +02:00
|
|
|
}
|