mirror of
https://github.com/Relintai/props.git
synced 2024-11-12 10:15:25 +01:00
Added PropSceneInstance, and added a processor for it.
This commit is contained in:
parent
59a874bd68
commit
f9d94ee635
2
SCsub
2
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",
|
||||
|
@ -26,6 +26,8 @@ def get_doc_classes():
|
||||
"PropInstance",
|
||||
"PropMeshUtils",
|
||||
|
||||
"PropSceneInstance",
|
||||
|
||||
"PropUtils",
|
||||
]
|
||||
|
||||
|
86
prop_scene_instance.cpp
Normal file
86
prop_scene_instance.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "prop_scene_instance.h"
|
||||
|
||||
#include "core/engine.h"
|
||||
|
||||
Ref<PackedScene> PropSceneInstance::get_scene() {
|
||||
return _scene;
|
||||
}
|
||||
void PropSceneInstance::set_scene(const Ref<PackedScene> &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);
|
||||
}
|
69
prop_scene_instance.h
Normal file
69
prop_scene_instance.h
Normal file
@ -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<PackedScene> get_scene();
|
||||
void set_scene(const Ref<PackedScene> &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<PackedScene> _scene;
|
||||
bool _snap_to_mesh;
|
||||
Vector3 _snap_axis;
|
||||
};
|
||||
|
||||
#endif
|
@ -22,10 +22,13 @@ SOFTWARE.
|
||||
|
||||
#include "prop_data_scene.h"
|
||||
|
||||
Ref<PackedScene> PropDataScene::get_scene() const {
|
||||
#include "../prop_scene_instance.h"
|
||||
#include "prop_data.h"
|
||||
|
||||
Ref<PackedScene> PropDataScene::get_scene() {
|
||||
return _scene;
|
||||
}
|
||||
void PropDataScene::set_scene(const Ref<PackedScene> value) {
|
||||
void PropDataScene::set_scene(const Ref<PackedScene> &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<PropSceneInstance>(node);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void PropDataScene::_processor_process(Ref<PropData> prop_data, Node *node, const Transform &transform) {
|
||||
PropSceneInstance *i = Object::cast_to<PropSceneInstance>(node);
|
||||
|
||||
ERR_FAIL_COND(!i);
|
||||
|
||||
Ref<PropDataScene> 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);
|
||||
|
@ -32,8 +32,8 @@ class PropDataScene : public PropDataEntry {
|
||||
GDCLASS(PropDataScene, PropDataEntry);
|
||||
|
||||
public:
|
||||
Ref<PackedScene> get_scene() const;
|
||||
void set_scene(const Ref<PackedScene> value);
|
||||
Ref<PackedScene> get_scene();
|
||||
void set_scene(const Ref<PackedScene> &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<PropData> prop_data, Node *node, const Transform &transform);
|
||||
Node *_processor_get_node_for(const Transform &transform);
|
||||
|
||||
PropDataScene();
|
||||
~PropDataScene();
|
||||
|
||||
|
@ -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<PropInstanceJob>();
|
||||
|
||||
ClassDB::register_class<PropSceneInstance>();
|
||||
|
||||
prop_utils = memnew(PropUtils);
|
||||
ClassDB::register_class<PropUtils>();
|
||||
Engine::get_singleton()->add_singleton(Engine::Singleton("PropUtils", PropUtils::get_singleton()));
|
||||
@ -71,6 +75,9 @@ void register_props_types() {
|
||||
Ref<PropDataProp> prop_processor = Ref<PropDataProp>(memnew(PropDataProp));
|
||||
PropUtils::add_processor(prop_processor);
|
||||
|
||||
Ref<PropDataScene> scene_processor = Ref<PropDataScene>(memnew(PropDataScene));
|
||||
PropUtils::add_processor(scene_processor);
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
EditorPlugins::add_by_type<PropEditorPlugin>();
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user