diff --git a/SCsub b/SCsub index 8210e90..6bce890 100644 --- a/SCsub +++ b/SCsub @@ -28,6 +28,8 @@ sources = [ "prop_ess_entity.cpp", "prop_instance_job.cpp", + "prop_scene_instance.cpp", + "singleton/prop_utils.cpp", "editor/prop_editor_plugin.cpp", diff --git a/config.py b/config.py index d4d3c60..2721c83 100644 --- a/config.py +++ b/config.py @@ -26,6 +26,8 @@ def get_doc_classes(): "PropInstance", "PropMeshUtils", + "PropSceneInstance", + "PropUtils", ] diff --git a/prop_scene_instance.cpp b/prop_scene_instance.cpp new file mode 100644 index 0000000..a3487df --- /dev/null +++ b/prop_scene_instance.cpp @@ -0,0 +1,86 @@ +#include "prop_scene_instance.h" + +#include "core/engine.h" + +Ref PropSceneInstance::get_scene() { + return _scene; +} +void PropSceneInstance::set_scene(const Ref &data) { + if (_scene == data) + return; + + _scene = data; + + build(); +} + +bool PropSceneInstance::get_snap_to_mesh() const { + return _snap_to_mesh; +} +void PropSceneInstance::set_snap_to_mesh(const bool value) { + _snap_to_mesh = value; +} + +Vector3 PropSceneInstance::get_snap_axis() const { + return _snap_axis; +} +void PropSceneInstance::set_snap_axis(const Vector3 &value) { + _snap_axis = value; +} + +void PropSceneInstance::build() { + if (!is_inside_tree()) { + return; + } + + for (int i = 0; i < get_child_count(); ++i) { + Node *n = get_child(i); + + //this way we won't delete the user's nodes + if (n->get_owner() == NULL) { + n->queue_delete(); + } + } + + if (!_scene.is_valid()) + return; + + Node *n = _scene->instance(); + + add_child(n); + + //if (Engine::get_singleton()->is_editor_hint()) + // n->set_owner(get_tree()->get_edited_scene_root()); +} + +PropSceneInstance::PropSceneInstance() { + _snap_to_mesh = false; + _snap_axis = Vector3(0, -1, 0); +} +PropSceneInstance::~PropSceneInstance() { + _scene.unref(); +} + +void PropSceneInstance::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + build(); + } + } +} + +void PropSceneInstance::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_scene"), &PropSceneInstance::get_scene); + ClassDB::bind_method(D_METHOD("set_scene", "value"), &PropSceneInstance::set_scene); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scene", PROPERTY_HINT_RESOURCE_TYPE, "PackedScene"), "set_scene", "get_scene"); + + ClassDB::bind_method(D_METHOD("get_snap_to_mesh"), &PropSceneInstance::get_snap_to_mesh); + ClassDB::bind_method(D_METHOD("set_snap_to_mesh", "value"), &PropSceneInstance::set_snap_to_mesh); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_to_mesh"), "set_snap_to_mesh", "get_snap_to_mesh"); + + ClassDB::bind_method(D_METHOD("get_snap_axis"), &PropSceneInstance::get_snap_axis); + ClassDB::bind_method(D_METHOD("set_snap_axis", "value"), &PropSceneInstance::set_snap_axis); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "snap_axis"), "set_snap_axis", "get_snap_axis"); + + ClassDB::bind_method(D_METHOD("build"), &PropSceneInstance::build); +} diff --git a/prop_scene_instance.h b/prop_scene_instance.h new file mode 100644 index 0000000..6c22107 --- /dev/null +++ b/prop_scene_instance.h @@ -0,0 +1,69 @@ +/* +Copyright (c) 2020 Péter Magyar + +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. +*/ + +#ifndef PROP_SCENE_INSTANCE_H +#define PROP_SCENE_INSTANCE_H + +#include "core/version.h" + +#if VERSION_MAJOR < 4 +#include "scene/3d/spatial.h" +#else +#include "scene/3d/node_3d.h" + +#define Spatial Node3D +#endif + +#include "core/math/vector3.h" + +#include "props/prop_data.h" +#include "scene/resources/packed_scene.h" + +class PropSceneInstance : public Spatial { + GDCLASS(PropSceneInstance, Spatial); + +public: + Ref get_scene(); + void set_scene(const Ref &data); + + bool get_snap_to_mesh() const; + void set_snap_to_mesh(const bool value); + + Vector3 get_snap_axis() const; + void set_snap_axis(const Vector3 &value); + + void build(); + + PropSceneInstance(); + ~PropSceneInstance(); + +protected: + void _notification(int p_what); + static void _bind_methods(); + +private: + Ref _scene; + bool _snap_to_mesh; + Vector3 _snap_axis; +}; + +#endif diff --git a/props/prop_data_scene.cpp b/props/prop_data_scene.cpp index 80d35d7..f918471 100644 --- a/props/prop_data_scene.cpp +++ b/props/prop_data_scene.cpp @@ -22,10 +22,13 @@ SOFTWARE. #include "prop_data_scene.h" -Ref PropDataScene::get_scene() const { +#include "../prop_scene_instance.h" +#include "prop_data.h" + +Ref PropDataScene::get_scene() { return _scene; } -void PropDataScene::set_scene(const Ref value) { +void PropDataScene::set_scene(const Ref &value) { _scene = value; } @@ -43,6 +46,33 @@ void PropDataScene::set_snap_axis(Vector3 value) { _snap_axis = value; } +bool PropDataScene::_processor_handles(Node *node) { + PropSceneInstance *i = Object::cast_to(node); + + return i; +} + +void PropDataScene::_processor_process(Ref prop_data, Node *node, const Transform &transform) { + PropSceneInstance *i = Object::cast_to(node); + + ERR_FAIL_COND(!i); + + Ref l; + l.instance(); + l->set_scene(i->get_scene()); + l->set_transform(transform * i->get_transform()); + prop_data->add_prop(l); +} + +Node *PropDataScene::_processor_get_node_for(const Transform &transform) { + PropSceneInstance *i = memnew(PropSceneInstance); + + i->set_scene(get_scene()); + i->set_transform(get_transform()); + + return i; +} + PropDataScene::PropDataScene() { _snap_to_mesh = true; _snap_axis = Vector3(0, 1, 0); diff --git a/props/prop_data_scene.h b/props/prop_data_scene.h index c9c4e62..5e59cff 100644 --- a/props/prop_data_scene.h +++ b/props/prop_data_scene.h @@ -32,8 +32,8 @@ class PropDataScene : public PropDataEntry { GDCLASS(PropDataScene, PropDataEntry); public: - Ref get_scene() const; - void set_scene(const Ref value); + Ref get_scene(); + void set_scene(const Ref &value); bool get_snap_to_mesh(); void set_snap_to_mesh(bool value); @@ -41,6 +41,10 @@ public: Vector3 get_snap_axis(); void set_snap_axis(Vector3 value); + bool _processor_handles(Node *node); + void _processor_process(Ref prop_data, Node *node, const Transform &transform); + Node *_processor_get_node_for(const Transform &transform); + PropDataScene(); ~PropDataScene(); diff --git a/register_types.cpp b/register_types.cpp index 5502e82..79030fb 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -37,6 +37,8 @@ SOFTWARE. #include "prop_instance_job.h" +#include "prop_scene_instance.h" + #include "core/engine.h" #include "singleton/prop_utils.h" @@ -61,6 +63,8 @@ void register_props_types() { ClassDB::register_class(); + ClassDB::register_class(); + prop_utils = memnew(PropUtils); ClassDB::register_class(); Engine::get_singleton()->add_singleton(Engine::Singleton("PropUtils", PropUtils::get_singleton())); @@ -71,6 +75,9 @@ void register_props_types() { Ref prop_processor = Ref(memnew(PropDataProp)); PropUtils::add_processor(prop_processor); + Ref scene_processor = Ref(memnew(PropDataScene)); + PropUtils::add_processor(scene_processor); + #ifdef TOOLS_ENABLED EditorPlugins::add_by_type(); #endif