Implement prop conversion logic.

This commit is contained in:
Relintai 2020-07-02 23:04:25 +02:00
parent d1a782c37c
commit d6d034b8e9
4 changed files with 85 additions and 5 deletions

View File

@ -22,19 +22,56 @@ SOFTWARE.
#include "prop_editor_plugin.h"
void PropEditorPlugin::convert_scene_to_prop_data(Variant param) {
#include "../props/prop_data.h"
#include "../singleton/prop_utils.h"
void PropEditorPlugin::convert_active_scene_to_prop_data(Variant param) {
SceneTree *st = SceneTree::get_singleton();
if (st) {
Node *scene = st->get_edited_scene_root();
if (scene) {
EditorData &ed = EditorNode::get_editor_data();
String path = ed.get_scene_path(ed.get_edited_scene());
convert_scene(scene, path.get_basename() + ".tres");
}
}
}
void PropEditorPlugin::convert_selected_scene_to_prop_data(Variant param) {
}
void PropEditorPlugin::convert_scene(Node *root, const String &path) {
Ref<PropData> data = PropUtils::get_singleton()->convert_tree(root);
ERR_FAIL_COND(!data.is_valid());
ResourceLoader l;
if (l.exists(path)) {
Ref<Resource> res = l.load(path);
ERR_FAIL_COND((res.is_valid() && res->get_class() != "PropData"));
res.unref();
}
ResourceSaver s;
s.save(path, data);
}
PropEditorPlugin::PropEditorPlugin(EditorNode *p_node) {
editor = p_node;
editor->add_tool_menu_item("Convert current scene to PropData", this, "convert_scene_to_prop_data");
editor->add_tool_menu_item("Convert active scene to PropData", this, "convert_active_scene_to_prop_data");
editor->add_tool_menu_item("Convert selected scene(s) to PropData", this, "convert_selected_scene_to_prop_data");
}
PropEditorPlugin::~PropEditorPlugin() {
}
void PropEditorPlugin::_bind_methods() {
ClassDB::bind_method(D_METHOD("convert_scene_to_prop_data"), &PropEditorPlugin::convert_scene_to_prop_data);
ClassDB::bind_method(D_METHOD("convert_active_scene_to_prop_data"), &PropEditorPlugin::convert_active_scene_to_prop_data);
ClassDB::bind_method(D_METHOD("convert_selected_scene_to_prop_data"), &PropEditorPlugin::convert_selected_scene_to_prop_data);
}

View File

@ -43,7 +43,9 @@ public:
virtual bool handles(Object *p_object) const { return false; }
virtual void make_visible(bool p_visible) {}
void convert_scene_to_prop_data(Variant param);
void convert_active_scene_to_prop_data(Variant param);
void convert_selected_scene_to_prop_data(Variant param);
void convert_scene(Node *root, const String &path);
PropEditorPlugin(EditorNode *p_node);
~PropEditorPlugin();

View File

@ -33,10 +33,50 @@ PropUtils *PropUtils::get_singleton() {
}
Ref<PropData> PropUtils::convert_tree(Node *root) {
return Ref<PropData>();
ERR_FAIL_COND_V(!ObjectDB::instance_validate(root), Ref<PropData>());
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) {
ERR_FAIL_COND(!ObjectDB::instance_validate(node));
for (int i = 0; i < PropUtils::_processors.size(); ++i) {
Ref<PropDataProcessor> proc = PropUtils::_processors.get(i);
ERR_CONTINUE(!proc.is_valid());
if (proc->handles(node)) {
proc->process(prop_data, node, transform);
break;
}
}
Spatial *sp = Object::cast_to<Spatial>(node);
if (!sp) {
//reset transform
Transform t;
for (int i = 0; i < node->get_child_count(); ++i) {
_convert_tree(prop_data, node->get_child(i), t);
}
} else {
for (int i = 0; i < node->get_child_count(); ++i) {
_convert_tree(prop_data, node->get_child(i), transform * sp->get_transform());
}
}
}
int PropUtils::add_processor(const Ref<PropDataProcessor> &processor) {
ERR_FAIL_COND_V(!processor.is_valid(), 0);
PropUtils::_processors.push_back(processor);
return PropUtils::_processors.size() - 1;

View File

@ -39,6 +39,7 @@ public:
static PropUtils *get_singleton();
Ref<PropData> convert_tree(Node *root);
void _convert_tree(Ref<PropData> prop_data, Node *node, const Transform &transform);
static int add_processor(const Ref<PropDataProcessor> &processor);
static Ref<PropDataProcessor> get_processor(const int index);