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,33 +1,20 @@
#include "curve_editor.h" #include "curve_editor.h"
void CurveEditor::set_curve(const Variant &c) {
//tool;
signal value_changed(value);
void CurveEditor::_ready() {
update_controls();
}
void CurveEditor::set_curve(const Variant &c) {
curve = c; curve = c;
update(); update();
update_controls(); update_controls();
} }
void CurveEditor::update_controls() {
void CurveEditor::update_controls() {
if (!curve) { if (!curve) {
return; return;
} }
for (c in get_children()) { for (c in get_children()) {
c.queue_free(); c.queue_free();
} }
Variant = curve.get_points(); Variant = curve.get_points();
@ -36,116 +23,107 @@
//var control_point = preload("res://addons/mat_maker_gd/widgets/curve_edit/control_point.tscn").instance(); //var control_point = preload("res://addons/mat_maker_gd/widgets/curve_edit/control_point.tscn").instance();
add_child(control_point); add_child(control_point);
control_point.initialize(p); control_point.initialize(p);
control_point.rect_position = transform_point(p.p)-control_point.OFFSET; control_point.rect_position = transform_point(p.p) - control_point.OFFSET;
if (i == 0 || i == points.size()-1) { 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.rect_position.x, control_point.rect_position.x, -control_point.OFFSET.y, rect_size.y - control_point.OFFSET.y);
if (i == 0) { if (i == 0) {
control_point.get_child(0).visible = false; control_point.get_child(0).visible = false;
} }
else { else {
control_point.get_child(1).visible = false; control_point.get_child(1).visible = false;
} }
}
}
else { else {
Variant = transform_point(points[i-1].p).x+1; Variant = transform_point(points[i - 1].p).x + 1;
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.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("moved", self, "_on_ControlPoint_moved");
control_point.connect("removed", self, "_on_ControlPoint_removed"); control_point.connect("removed", self, "_on_ControlPoint_removed");
} }
emit_signal("value_changed", curve); emit_signal("value_changed", curve);
} }
void CurveEditor::_on_ControlPoint_moved(const Variant &index) {
void CurveEditor::_on_ControlPoint_moved(const Variant &index) {
Array points = curve.get_points(); Array points = curve.get_points();
Variant = get_child(index); Variant = get_child(index);
points[index].p = reverse_transform_point(control_point.rect_position+control_point.OFFSET); points[index].p = reverse_transform_point(control_point.rect_position + control_point.OFFSET);
if (control_point.has_node("LeftSlope")) { if (control_point.has_node("LeftSlope")) {
Variant = control_point.get_node("LeftSlope").rect_position/rect_size; Variant = control_point.get_node("LeftSlope").rect_position / rect_size;
if (slope_vector.x != 0) { if (slope_vector.x != 0) {
points[index].ls = -slope_vector.y / slope_vector.x; points[index].ls = -slope_vector.y / slope_vector.x;
} }
}
}
if (control_point.has_node("RightSlope")) { if (control_point.has_node("RightSlope")) {
Variant = control_point.get_node("RightSlope").rect_position/rect_size; Variant = control_point.get_node("RightSlope").rect_position / rect_size;
if (slope_vector.x != 0) { if (slope_vector.x != 0) {
points[index].rs = -slope_vector.y / slope_vector.x; points[index].rs = -slope_vector.y / slope_vector.x;
} }
}
}
curve.set_points(points, false); curve.set_points(points, false);
update(); update();
emit_signal("value_changed", curve); emit_signal("value_changed", curve);
} }
void CurveEditor::_on_ControlPoint_removed(const Variant &index) {
void CurveEditor::_on_ControlPoint_removed(const Variant &index) {
if (curve.remove_point(index)) { if (curve.remove_point(index)) {
update(); update();
update_controls(); update_controls();
}
} }
} void CurveEditor::_on_CurveEditor_gui_input(const Variant &event) {
void CurveEditor::_on_CurveEditor_gui_input(const Variant &event) {
if (event is InputEventMouseButton) { if (event is InputEventMouseButton) {
if (event.button_index == BUTTON_LEFT && event.doubleclick) { if (event.button_index == BUTTON_LEFT && event.doubleclick) {
Variant = reverse_transform_point(get_local_mouse_position()); Variant = reverse_transform_point(get_local_mouse_position());
curve.add_point(new_point_position.x, new_point_position.y, 0.0, 0.0); curve.add_point(new_point_position.x, new_point_position.y, 0.0, 0.0);
update_controls(); update_controls();
}
}
} }
} void CurveEditor::_on_resize() {
CurveView::_on_resize();
}
void CurveEditor::_on_resize() {
._on_resize();
update_controls(); update_controls();
} }
CurveEditor::CurveEditor() {
set_mouse_filter(MOUSE_FILTER_STOP);
} }
CurveEditor::CurveEditor() { CurveEditor::~CurveEditor() {
}
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);
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("set_curve", "c"), &CurveEditor::set_curve);
ClassDB::bind_method(D_METHOD("update_controls"), &CurveEditor::update_controls); 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_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_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_CurveEditor_gui_input", "event"), &CurveEditor::_on_CurveEditor_gui_input);
ClassDB::bind_method(D_METHOD("_on_resize"), &CurveEditor::_on_resize); ClassDB::bind_method(D_METHOD("_on_resize"), &CurveEditor::_on_resize);
}
}

View File

@ -1,15 +1,15 @@
#ifndef CURVE_EDITOR_H #ifndef CURVE_EDITOR_H
#define 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" { class CurveEditor : public CurveView {
GDCLASS(CurveEditor, "res://addons/mat_maker_gd/widgets/curve_edit/curve_view.gd"); GDCLASS(CurveEditor, CurveView);
public: public:
void _ready();
void set_curve(const Variant &c); void set_curve(const Variant &c);
void update_controls(); void update_controls();
void _on_ControlPoint_moved(const Variant &index); void _on_ControlPoint_moved(const Variant &index);
void _on_ControlPoint_removed(const Variant &index); void _on_ControlPoint_removed(const Variant &index);
void _on_CurveEditor_gui_input(const Variant &event); void _on_CurveEditor_gui_input(const Variant &event);
@ -18,12 +18,10 @@ class CurveEditor : public "res://addons/mat_maker_gd/widgets/curve_edit/curve_v
CurveEditor(); CurveEditor();
~CurveEditor(); ~CurveEditor();
protected: protected:
static void _bind_methods(); void _notification(int p_what);
//tool static void _bind_methods();
signal value_changed(value);
}; };
#endif #endif