terrain-editor/modules/terrain_editor/terrain_mesh_editor.cpp

54 lines
1.6 KiB
C++

#include "terrain_mesh_editor.h"
#include "scene/3d/node_3d.h"
#include "scene/resources/packed_scene.h"
#include "terrain_editor/macros.h"
#include "terrain_editor/point_primitive_node.h"
#include "terrain_editor/terrain_primitive.h"
void TerrainMeshEditor::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, point_primitive_object, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
}
void TerrainMeshEditor::ready() {
connect(sig_primitive_list_changed, callable_mp(this, &self_type::on_primitive_list_changed));
on_primitive_list_changed(get_primitives());
}
void TerrainMeshEditor::on_primitive_list_changed(Array primitives) {
for (Node3D *existing : this->primitive_nodes) {
existing->queue_free();
}
this->primitive_nodes.clear();
for (Variant var : primitives) {
if (this->point_primitive_object.is_valid() && this->point_primitive_object->get_state()->get_node_type(0) == PointPrimitiveNode::get_class_static()) {
Ref<PointPrimitive> point{ var };
if (point.is_valid()) {
PointPrimitiveNode *primitive_node{ cast_to<PointPrimitiveNode>(this->point_primitive_object->instantiate()) };
primitive_node->set_primitive(point);
this->add_child(primitive_node);
}
}
}
}
void TerrainMeshEditor::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void TerrainMeshEditor::set_point_primitive_object(Ref<PackedScene> scene) {
this->point_primitive_object = scene;
}
Ref<PackedScene> TerrainMeshEditor::get_point_primitive_object() const {
return this->point_primitive_object;
}