From 95b565e2a404f8d9ec63108ea731d329cd959409 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 Jun 2020 21:44:45 +0200 Subject: [PATCH] Added a MeshDataInstance node. --- SCsub | 2 + config.py | 1 + nodes/mesh_data_instance.cpp | 91 ++++++++++++++++++++++++++++++++++++ nodes/mesh_data_instance.h | 74 +++++++++++++++++++++++++++++ register_types.cpp | 4 ++ 5 files changed, 172 insertions(+) create mode 100644 nodes/mesh_data_instance.cpp create mode 100644 nodes/mesh_data_instance.h diff --git a/SCsub b/SCsub index 23c5904..a70226a 100644 --- a/SCsub +++ b/SCsub @@ -11,3 +11,5 @@ module_env.add_source_files(env.modules_sources,"plugin_collada/editor_plugin_co module_env.add_source_files(env.modules_sources,"plugin_gltf/editor_import_gltf_mdr.cpp") module_env.add_source_files(env.modules_sources,"plugin_gltf/editor_plugin_gltf_mdr.cpp") + +module_env.add_source_files(env.modules_sources,"nodes/mesh_data_instance.cpp") diff --git a/config.py b/config.py index 2943163..9d0f78b 100644 --- a/config.py +++ b/config.py @@ -11,6 +11,7 @@ def configure(env): def get_doc_classes(): return [ "MeshDataResource", + "MeshDataInstance", ] def get_doc_path(): diff --git a/nodes/mesh_data_instance.cpp b/nodes/mesh_data_instance.cpp new file mode 100644 index 0000000..8295612 --- /dev/null +++ b/nodes/mesh_data_instance.cpp @@ -0,0 +1,91 @@ +#include "mesh_data_instance.h" + +bool MeshDataInstance::get_snap_to_mesh() const { + return _snap_to_mesh; +} +void MeshDataInstance::set_snap_to_mesh(const bool value) { + _snap_to_mesh = value; +} + +Vector3 MeshDataInstance::get_snap_axis() const { + return _snap_axis; +} +void MeshDataInstance::set_snap_axis(const Vector3 &value) { + _snap_axis = value; +} + +Ref MeshDataInstance::get_mesh() { + return _mesh; +} +void MeshDataInstance::set_mesh(const Ref &mesh) { + _mesh = mesh; +} + +Ref MeshDataInstance::get_texture() { + return _texture; +} +void MeshDataInstance::set_texture(const Ref &texture) { + _texture = texture; +} + +MeshDataInstance::MeshDataInstance() { + _snap_to_mesh = false; + _snap_axis = Vector3(0, -1, 0); +} +MeshDataInstance::~MeshDataInstance() { + _mesh.unref(); + _texture.unref(); +} + +void MeshDataInstance::notification(int p_what) { + /* + switch (p_what) { + case NOTIFICATION_READY: { + if (get_parent() == NULL) + return; + } + //fallthrough + case NOTIFICATION_PARENTED: { + Node *n = this; + + while (n) { + + PropInstance *pi = Object::cast_to(n); + + if (pi) { + _owner = pi; + pi->register_prop_mesh_data_instance(this); + return; + } + + n = n->get_parent(); + } + + } break; + case NOTIFICATION_EXIT_TREE: + case NOTIFICATION_UNPARENTED: { + if (_owner) { + _owner->unregister_prop_mesh_data_instance(this); + } + break; + } + }*/ +} + +void MeshDataInstance::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_snap_to_mesh"), &MeshDataInstance::get_snap_to_mesh); + ClassDB::bind_method(D_METHOD("set_snap_to_mesh", "value"), &MeshDataInstance::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"), &MeshDataInstance::get_snap_axis); + ClassDB::bind_method(D_METHOD("set_snap_axis", "value"), &MeshDataInstance::set_snap_axis); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "snap_axis"), "set_snap_axis", "get_snap_axis"); + + ClassDB::bind_method(D_METHOD("get_mesh"), &MeshDataInstance::get_mesh); + ClassDB::bind_method(D_METHOD("set_mesh", "value"), &MeshDataInstance::set_mesh); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "MeshDataResource"), "set_mesh", "get_mesh"); + + ClassDB::bind_method(D_METHOD("get_texture"), &MeshDataInstance::get_texture); + ClassDB::bind_method(D_METHOD("set_texture", "value"), &MeshDataInstance::set_texture); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); +} diff --git a/nodes/mesh_data_instance.h b/nodes/mesh_data_instance.h new file mode 100644 index 0000000..6c575fa --- /dev/null +++ b/nodes/mesh_data_instance.h @@ -0,0 +1,74 @@ +/* +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_MESH_DATA_INSTANCE_H +#define PROP_MESH_DATA_INSTANCE_H + +#include "core/version.h" +#include "scene/resources/texture.h" + +#if VERSION_MAJOR < 4 +#include "scene/3d/spatial.h" +#else +#include "scene/3d/node_3d.h" + +#define Spatial Node3D +#define Texture Texture2D +#endif + +#include "core/math/vector3.h" + +#include "../mesh_data_resource.h" + +class PropInstance; + +class MeshDataInstance : public Spatial { + GDCLASS(MeshDataInstance, Spatial); + +public: + 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); + + Ref get_mesh(); + void set_mesh(const Ref &mesh); + + Ref get_texture(); + void set_texture(const Ref &texture); + + MeshDataInstance(); + ~MeshDataInstance(); + +protected: + void notification(int p_what); + static void _bind_methods(); + +private: + bool _snap_to_mesh; + Vector3 _snap_axis; + Ref _mesh; + Ref _texture; +}; + +#endif diff --git a/register_types.cpp b/register_types.cpp index 5bac408..a275c4d 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -29,12 +29,16 @@ SOFTWARE. #include "plugin_collada/editor_plugin_collada_mdr.h" +#include "nodes/mesh_data_instance.h" + #include "plugin_gltf/editor_plugin_gltf_mdr.h" #endif void register_mesh_data_resource_types() { ClassDB::register_class(); + ClassDB::register_class(); + #ifdef TOOLS_ENABLED EditorPlugins::add_by_type();