Light clean pass on CurveEditor.

This commit is contained in:
Relintai 2022-06-12 22:24:36 +02:00
parent 28720be0be
commit 20b291a2ea
3 changed files with 119 additions and 239 deletions

View File

@ -1,96 +0,0 @@
void construct() {
//Script: res://addons/mat_maker_gd/widgets/curve_edit/curve_edit.gd
Button *curveedit = memnew(Button);
curveedit->set_name("CurveEdit");
curveedit->set_name("CurveEdit");
//curveedit->set("name", CurveEdit));
curveedit->set_filename("res://addons/mat_maker_gd/widgets/curve_edit/curve_edit.tscn");
//curveedit->set("filename", "res://addons/mat_maker_gd/widgets/curve_edit/curve_edit.tscn");
curveedit->set_anchor_left(1);
//curveedit->set("anchor_left", 1);
curveedit->set_anchor_right(1);
//curveedit->set("anchor_right", 1);
curveedit->set_anchor_bottom(1);
//curveedit->set("anchor_bottom", 1);
curveedit->set_margin_left(-1280);
//curveedit->set("margin_left", -1280);
curveedit->set_margin_right(-1220);
//curveedit->set("margin_right", -1220);
curveedit->set_margin_bottom(-700);
//curveedit->set("margin_bottom", -700);
curveedit->set_rect_position(Vector2(-1280, 0));
//curveedit->set("rect_position", Vector2(-1280, 0));
curveedit->set_rect_global_position(Vector2(-1280, 0));
//curveedit->set("rect_global_position", Vector2(-1280, 0));
curveedit->set_rect_size(Vector2(60, 20));
//curveedit->set("rect_size", Vector2(60, 20));
curveedit->set_rect_min_size(Vector2(60, 20));
//curveedit->set("rect_min_size", Vector2(60, 20));
curveedit->set_focus_mode(1);
//curveedit->set("focus_mode", 1);
//curveedit property __meta__ TYPE_DICTIONARY value: {_edit_use_anchors_:False}
//Script: res://addons/mat_maker_gd/widgets/curve_edit/curve_view.gd
Control *curveview_curveedit = memnew(Control);
curveview_curveedit->set_name("CurveView");
curveedit->add_child(curveview_curveedit);
curveview_curveedit->set_name("CurveView");
//curveview_curveedit->set("name", CurveView));
curveview_curveedit->set_filename("res://addons/mat_maker_gd/widgets/curve_edit/curve_view.tscn");
//curveview_curveedit->set("filename", "res://addons/mat_maker_gd/widgets/curve_edit/curve_view.tscn");
//curveview_curveedit property owner TYPE_OBJECT value: CurveEdit:[Button:59797]
curveview_curveedit->set_anchor_right(1);
//curveview_curveedit->set("anchor_right", 1);
curveview_curveedit->set_anchor_bottom(1);
//curveview_curveedit->set("anchor_bottom", 1);
curveview_curveedit->set_margin_left(4);
//curveview_curveedit->set("margin_left", 4);
curveview_curveedit->set_margin_top(4);
//curveview_curveedit->set("margin_top", 4);
curveview_curveedit->set_margin_right(-4);
//curveview_curveedit->set("margin_right", -4);
curveview_curveedit->set_margin_bottom(-4);
//curveview_curveedit->set("margin_bottom", -4);
curveview_curveedit->set_rect_position(Vector2(4, 4));
//curveview_curveedit->set("rect_position", Vector2(4, 4));
curveview_curveedit->set_rect_global_position(Vector2(4, 4));
//curveview_curveedit->set("rect_global_position", Vector2(4, 4));
curveview_curveedit->set_mouse_filter(2);
//curveview_curveedit->set("mouse_filter", 2);
//curveview_curveedit property __meta__ TYPE_DICTIONARY value: {_edit_use_anchors_:False}
}

View File

@ -1,151 +1,129 @@
#include "curve_editor.h"
//tool;
signal value_changed(value);
void CurveEditor::_ready() {
update_controls();
void CurveEditor::set_curve(const Variant &c) {
curve = c;
update();
update_controls();
}
void CurveEditor::update_controls() {
if (!curve) {
return;
}
void CurveEditor::set_curve(const Variant &c) {
curve = c;
update();
update_controls();
for (c in get_children()) {
c.queue_free();
}
Variant = 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();
add_child(control_point);
control_point.initialize(p);
control_point.rect_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);
if (i == 0) {
control_point.get_child(0).visible = false;
}
else {
control_point.get_child(1).visible = false;
}
}
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");
}
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);
void CurveEditor::update_controls() {
if (control_point.has_node("LeftSlope")) {
Variant = control_point.get_node("LeftSlope").rect_position / rect_size;
if (!curve) {
return;
if (slope_vector.x != 0) {
points[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 (slope_vector.x != 0) {
points[index].rs = -slope_vector.y / slope_vector.x;
}
}
curve.set_points(points, false);
update();
emit_signal("value_changed", curve);
}
for (c in get_children()) {
c.queue_free();
void CurveEditor::_on_ControlPoint_removed(const Variant &index) {
if (curve.remove_point(index)) {
update();
update_controls();
}
}
Variant = 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();
add_child(control_point);
control_point.initialize(p);
control_point.rect_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);
if (i == 0) {
control_point.get_child(0).visible = false;
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);
update_controls();
}
}
}
else {
control_point.get_child(1).visible = false;
void CurveEditor::_on_resize() {
CurveView::_on_resize();
update_controls();
}
CurveEditor::CurveEditor() {
set_mouse_filter(MOUSE_FILTER_STOP);
}
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);
CurveEditor::~CurveEditor() {
}
control_point.connect("moved", self, "_on_ControlPoint_moved");
control_point.connect("removed", self, "_on_ControlPoint_removed");
void CurveView::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POSTINITIALIZE: {
connect("gui_input", this, "_on_CurveEditor_gui_input");
update_controls();
} break;
default: {
} break;
}
}
void CurveEditor::_bind_methods() {
//signal value_changed(value);
emit_signal("value_changed", curve);
ClassDB::bind_method(D_METHOD("set_curve", "c"), &CurveEditor::set_curve);
ClassDB::bind_method(D_METHOD("update_controls"), &CurveEditor::update_controls);
ClassDB::bind_method(D_METHOD("_on_ControlPoint_moved", "index"), &CurveEditor::_on_ControlPoint_moved);
ClassDB::bind_method(D_METHOD("_on_ControlPoint_removed", "index"), &CurveEditor::_on_ControlPoint_removed);
ClassDB::bind_method(D_METHOD("_on_CurveEditor_gui_input", "event"), &CurveEditor::_on_CurveEditor_gui_input);
ClassDB::bind_method(D_METHOD("_on_resize"), &CurveEditor::_on_resize);
}
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);
if (control_point.has_node("LeftSlope")) {
Variant = control_point.get_node("LeftSlope").rect_position/rect_size;
if (slope_vector.x != 0) {
points[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 (slope_vector.x != 0) {
points[index].rs = -slope_vector.y / slope_vector.x;
}
}
curve.set_points(points, false);
update();
emit_signal("value_changed", curve);
}
void CurveEditor::_on_ControlPoint_removed(const Variant &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);
update_controls();
}
}
}
void CurveEditor::_on_resize() {
._on_resize();
update_controls();
}
}
CurveEditor::CurveEditor() {
}
CurveEditor::~CurveEditor() {
}
static void CurveEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_ready"), &CurveEditor::_ready);
ClassDB::bind_method(D_METHOD("set_curve", "c"), &CurveEditor::set_curve);
ClassDB::bind_method(D_METHOD("update_controls"), &CurveEditor::update_controls);
ClassDB::bind_method(D_METHOD("_on_ControlPoint_moved", "index"), &CurveEditor::_on_ControlPoint_moved);
ClassDB::bind_method(D_METHOD("_on_ControlPoint_removed", "index"), &CurveEditor::_on_ControlPoint_removed);
ClassDB::bind_method(D_METHOD("_on_CurveEditor_gui_input", "event"), &CurveEditor::_on_CurveEditor_gui_input);
ClassDB::bind_method(D_METHOD("_on_resize"), &CurveEditor::_on_resize);
}

View File

@ -1,29 +1,27 @@
#ifndef CURVE_EDITOR_H
#define CURVE_EDITOR_H
#include "curve_view.h"
class CurveEditor : public "res://addons/mat_maker_gd/widgets/curve_edit/curve_view.gd" {
GDCLASS(CurveEditor, "res://addons/mat_maker_gd/widgets/curve_edit/curve_view.gd");
class CurveEditor : public CurveView {
GDCLASS(CurveEditor, CurveView);
public:
public:
void set_curve(const Variant &c);
void update_controls();
void _ready();
void set_curve(const Variant &c);
void update_controls();
void _on_ControlPoint_moved(const Variant &index);
void _on_ControlPoint_removed(const Variant &index);
void _on_CurveEditor_gui_input(const Variant &event);
void _on_resize();
void _on_ControlPoint_moved(const Variant &index);
void _on_ControlPoint_removed(const Variant &index);
void _on_CurveEditor_gui_input(const Variant &event);
void _on_resize();
CurveEditor();
~CurveEditor();
CurveEditor();
~CurveEditor();
protected:
static void _bind_methods();
protected:
void _notification(int p_what);
//tool
signal value_changed(value);
static void _bind_methods();
};
#endif