Fixed the remaining issues with the curve editor.

This commit is contained in:
Relintai 2022-06-13 22:52:12 +02:00
parent b8b0294501
commit fbde39b27f
3 changed files with 56 additions and 51 deletions

View File

@ -66,7 +66,8 @@ void ControlPoint::_draw() {
}
void ControlPoint::initialize(const CurveBase::Point &p) {
set_rect_position(get_parent_control()->transform_point(p.p) - OFFSET);
Vector2 parc = get_parent_control()->call("transform_point", p.p);
set_position(parc - OFFSET);
if (p.ls != Math_INF) {
left_slope_point->set_position(left_slope_point->get_distance() * (get_parent_control()->get_size() * Vector2(1.0, -p.ls)).normalized());
@ -94,7 +95,7 @@ void ControlPoint::_on_ControlPoint_gui_input(const Ref<InputEvent> &event) {
moving = true;
} else {
moving = false;
get_parent()->update_controls();
get_parent()->call("update_controls");
}
} else if (iemb->get_button_index() == BUTTON_RIGHT && iemb->is_pressed()) {
emit_signal("removed", get_index());

View File

@ -37,6 +37,11 @@ public:
ControlPoint();
~ControlPoint();
const Vector2 OFFSET = Vector2(3, 3);
SlopePoint *left_slope_point;
SlopePoint *right_slope_point;
protected:
void _notification(int p_what);
@ -47,10 +52,6 @@ protected:
float max_x;
float min_y;
float max_y;
const Vector2 OFFSET = Vector2(3, 3);
SlopePoint *left_slope_point;
SlopePoint *right_slope_point;
};
#endif

View File

@ -1,6 +1,11 @@
#include "curve_editor.h"
#include "../../../nodes/bases/curve_base.h"
#include "control_point.h"
#include "core/object.h"
#include "slope_point.h"
void CurveEditor::set_curve(const Variant &c) {
curve = c;
update();
@ -8,87 +13,85 @@ void CurveEditor::set_curve(const Variant &c) {
}
void CurveEditor::update_controls() {
if (!curve) {
if (!curve.is_valid()) {
return;
}
for (c in get_children()) {
c.queue_free();
for (int i = 0; i < get_child_count(); ++i) {
Node *c = get_child(i);
c->queue_delete();
}
Variant = curve.get_points();
Vector<CurveBase::Point> points = curve->get_points();
for (i in points.size()) {
Variant = points[i];
//var control_point = preload("res://addons/mat_maker_gd/widgets/curve_edit/control_point.tscn").instance();
for (int i = 0; i < points.size(); ++i) {
CurveBase::Point p = points[i];
ControlPoint *control_point = memnew(ControlPoint);
add_child(control_point);
control_point.initialize(p);
control_point.rect_position = transform_point(p.p) - control_point.OFFSET;
control_point->initialize(p);
control_point->set_position(transform_point(p.p) - control_point->OFFSET);
if (i == 0 || i == points.size() - 1) {
control_point.set_constraint(control_point.rect_position.x, control_point.rect_position.x, -control_point.OFFSET.y, rect_size.y - control_point.OFFSET.y);
control_point->set_constraint(control_point->get_position().x, control_point->get_position().x, -control_point->OFFSET.y, get_size().y - control_point->OFFSET.y);
if (i == 0) {
control_point.get_child(0).visible = false;
Object::cast_to<Control>(control_point->get_child(0))->set_visible(false);
} else {
Object::cast_to<Control>(control_point->get_child(1))->set_visible(false);
}
else {
control_point.get_child(1).visible = false;
}
} else {
float min_x = transform_point(points[i - 1].p).x + 1;
float max_x = transform_point(points[i + 1].p).x - 1;
control_point->set_constraint(min_x, max_x, -control_point->OFFSET.y, get_size().y - control_point->OFFSET.y);
}
else {
Variant = transform_point(points[i - 1].p).x + 1;
Variant = transform_point(points[i + 1].p).x - 1;
control_point.set_constraint(min_x, max_x, -control_point.OFFSET.y, rect_size.y - control_point.OFFSET.y);
}
control_point.connect("moved", self, "_on_ControlPoint_moved");
control_point.connect("removed", self, "_on_ControlPoint_removed");
control_point->connect("moved", this, "_on_ControlPoint_moved");
control_point->connect("removed", this, "_on_ControlPoint_removed");
}
emit_signal("value_changed", curve);
}
void CurveEditor::_on_ControlPoint_moved(const Variant &index) {
Array points = curve.get_points();
Variant = get_child(index);
points[index].p = reverse_transform_point(control_point.rect_position + control_point.OFFSET);
Vector<CurveBase::Point> points = curve->get_points();
ControlPoint *control_point = Object::cast_to<ControlPoint>(get_child(index));
points.write[index].p = reverse_transform_point(control_point->get_position() + control_point->OFFSET);
if (control_point.has_node("LeftSlope")) {
Variant = control_point.get_node("LeftSlope").rect_position / rect_size;
//if (control_point.has_node("LeftSlope")) {
Vector2 slope_vector = control_point->left_slope_point->get_position() / get_size();
if (slope_vector.x != 0) {
points[index].ls = -slope_vector.y / slope_vector.x;
}
if (slope_vector.x != 0) {
points.write[index].ls = -slope_vector.y / slope_vector.x;
}
//}
if (control_point.has_node("RightSlope")) {
Variant = control_point.get_node("RightSlope").rect_position / rect_size;
//if (control_point.has_node("RightSlope")) {
slope_vector = control_point->right_slope_point->get_position() / get_size();
if (slope_vector.x != 0) {
points[index].rs = -slope_vector.y / slope_vector.x;
}
if (slope_vector.x != 0) {
points.write[index].rs = -slope_vector.y / slope_vector.x;
}
//}
curve.set_points(points, false);
curve->set_points(points, false);
update();
emit_signal("value_changed", curve);
}
void CurveEditor::_on_ControlPoint_removed(const Variant &index) {
if (curve.remove_point(index)) {
if (curve->remove_point(index)) {
update();
update_controls();
}
}
void CurveEditor::_on_CurveEditor_gui_input(const Variant &event) {
if (event is InputEventMouseButton) {
if (event.button_index == BUTTON_LEFT && event.doubleclick) {
Variant = reverse_transform_point(get_local_mouse_position());
curve.add_point(new_point_position.x, new_point_position.y, 0.0, 0.0);
Ref<InputEventMouseButton> iemb = event;
if (iemb.is_valid()) {
if (iemb->get_button_index() == BUTTON_LEFT && iemb->is_doubleclick()) {
Vector2 new_point_position = reverse_transform_point(get_local_mouse_position());
curve->add_point(new_point_position.x, new_point_position.y, 0.0, 0.0);
update_controls();
}
}
@ -106,7 +109,7 @@ CurveEditor::CurveEditor() {
CurveEditor::~CurveEditor() {
}
void CurveView::_notification(int p_what) {
void CurveEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POSTINITIALIZE: {
connect("gui_input", this, "_on_CurveEditor_gui_input");
@ -117,7 +120,7 @@ void CurveView::_notification(int p_what) {
}
}
void CurveEditor::_bind_methods() {
//signal value_changed(value);
ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "CurveBase")));
ClassDB::bind_method(D_METHOD("set_curve", "c"), &CurveEditor::set_curve);
ClassDB::bind_method(D_METHOD("update_controls"), &CurveEditor::update_controls);