terrain-editor/modules/terrain_editor/point_primitive_node.cpp

83 lines
2.6 KiB
C++

#include "point_primitive_node.h"
#include "scene/3d/node_3d.h"
#include "terrain_editor/edit_history.h"
#include "terrain_editor/macros.h"
#include "terrain_editor/terrain_primitive.h"
void PointPrimitiveNode::_bind_methods() {
BIND_GET_SET(primitive);
ClassDB::bind_method(D_METHOD("push_transform_changes"), &self_type::push_transform_changes);
ClassDB::bind_method(D_METHOD("preview_transform_changes"), &self_type::preview_transform_changes);
}
void PointPrimitiveNode::on_underlying_changed() {
if (!this->pushing_change && this->is_inside_tree()) {
this->pushing_change = true;
Vector2 const center{ this->primitive->get_center() };
set_global_position({ center.x, this->primitive->get_height(), center.y });
this->pushing_change = false;
}
}
void PointPrimitiveNode::_notification(int what) {
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
if (this->primitive.is_valid()) {
on_underlying_changed();
}
return;
}
}
void PointPrimitiveNode::preview_transform_changes() {
if (this->primitive.is_valid()) {
this->pushing_change = true;
Vector3 const position{ get_global_position() };
Vector2 const center{ position.x, position.z };
if (center != this->primitive->get_center()) {
this->primitive->set_center(center);
}
if (position.y != this->primitive->get_height()) {
this->primitive->set_height(position.y);
}
this->pushing_change = false;
}
}
void PointPrimitiveNode::push_transform_changes() {
if (this->primitive.is_valid()) {
this->pushing_change = true;
Vector3 const position{ get_global_position() };
Vector2 const center{ position.x, position.z };
if (center != this->primitive->get_center()) {
EditHistory::get_singleton()->push_action(
callable_mp(*this->primitive, &PointPrimitive::set_center).bind(center),
callable_mp(*this->primitive, &PointPrimitive::set_center).bind(this->primitive->get_center()));
}
if (position.y != this->primitive->get_height()) {
EditHistory::get_singleton()->push_action(
callable_mp(*this->primitive, &PointPrimitive::set_height).bind(position.y),
callable_mp(*this->primitive, &PointPrimitive::set_height).bind(this->primitive->get_height()));
}
this->pushing_change = false;
}
}
void PointPrimitiveNode::set_primitive(Ref<PointPrimitive> primitive) {
if (this->primitive.is_valid()) {
this->primitive->disconnect_changed(underlying_changed_callable);
}
this->primitive = primitive;
if (this->primitive.is_valid()) {
primitive->connect_changed(underlying_changed_callable);
on_underlying_changed();
}
}
Ref<PointPrimitive> PointPrimitiveNode::get_primitive() const {
return this->primitive;
}