From d6d034b8e9890e159223fab114080bf827d64e1d Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 2 Jul 2020 23:04:25 +0200 Subject: [PATCH] Implement prop conversion logic. --- editor/prop_editor_plugin.cpp | 43 ++++++++++++++++++++++++++++++++--- editor/prop_editor_plugin.h | 4 +++- singleton/prop_utils.cpp | 42 +++++++++++++++++++++++++++++++++- singleton/prop_utils.h | 1 + 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/editor/prop_editor_plugin.cpp b/editor/prop_editor_plugin.cpp index b7f3504..fcf4726 100644 --- a/editor/prop_editor_plugin.cpp +++ b/editor/prop_editor_plugin.cpp @@ -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 data = PropUtils::get_singleton()->convert_tree(root); + + ERR_FAIL_COND(!data.is_valid()); + + ResourceLoader l; + if (l.exists(path)) { + Ref 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); } diff --git a/editor/prop_editor_plugin.h b/editor/prop_editor_plugin.h index 2079869..62b0cd2 100644 --- a/editor/prop_editor_plugin.h +++ b/editor/prop_editor_plugin.h @@ -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(); diff --git a/singleton/prop_utils.cpp b/singleton/prop_utils.cpp index 90d3963..207b6a8 100644 --- a/singleton/prop_utils.cpp +++ b/singleton/prop_utils.cpp @@ -33,10 +33,50 @@ PropUtils *PropUtils::get_singleton() { } Ref PropUtils::convert_tree(Node *root) { - return Ref(); + ERR_FAIL_COND_V(!ObjectDB::instance_validate(root), Ref()); + + Ref data; + data.instance(); + Transform t; + + _convert_tree(data, root, t); + + return data; +} + +void PropUtils::_convert_tree(Ref prop_data, Node *node, const Transform &transform) { + ERR_FAIL_COND(!ObjectDB::instance_validate(node)); + + for (int i = 0; i < PropUtils::_processors.size(); ++i) { + Ref 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(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 &processor) { + ERR_FAIL_COND_V(!processor.is_valid(), 0); + PropUtils::_processors.push_back(processor); return PropUtils::_processors.size() - 1; diff --git a/singleton/prop_utils.h b/singleton/prop_utils.h index ba4f9ae..964aec7 100644 --- a/singleton/prop_utils.h +++ b/singleton/prop_utils.h @@ -39,6 +39,7 @@ public: static PropUtils *get_singleton(); Ref convert_tree(Node *root); + void _convert_tree(Ref prop_data, Node *node, const Transform &transform); static int add_processor(const Ref &processor); static Ref get_processor(const int index);