From 1943e7f150ae8af266dfd881c820373390394858 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 12 Jun 2022 16:19:27 +0200 Subject: [PATCH] Cleaned up CurveView. --- ] | 124 ++++++++++ .../editor/widgets/curve_edit/curve_view.cpp | 216 ++++++++---------- .../widgets/curve_edit/curve_view.ctscn | 46 ---- .../editor/widgets/curve_edit/curve_view.h | 47 ++-- 4 files changed, 236 insertions(+), 197 deletions(-) create mode 100644 ] delete mode 100644 modules/material_maker/editor/widgets/curve_edit/curve_view.ctscn diff --git a/] b/] new file mode 100644 index 000000000..05f43e3c1 --- /dev/null +++ b/] @@ -0,0 +1,124 @@ + +#include "curve_view.h" + +#include "../../../nodes/bases/curve_base.h" +#include "core/vector.h" + +bool CurveView::get_show_axes() const { + return show_axes; +} + +void CurveView::set_show_axes(const bool val) { + show_axes = val; +} + +Ref CurveView::get_curve() { + return curve; +} + +void CurveView::set_curve(const Ref &val) { + curve = val; + update(); +} + +Vector2 CurveView::transform_point(const Vector2 &p) { + return (Vector2(0.0, 1.0) + Vector2(1.0, -1.0) * p) * get_size(); +} + +Vector2 CurveView::reverse_transform_point(const Vector2 &p) { + return Vector2(0.0, 1.0) + Vector2(1.0, -1.0) * p / get_size(); +} + +void CurveView::_draw() { + if (!curve.is_valid()) { + return; + } + + // var current_theme : Theme = get_node("/root/MainWindow").theme; + //; + // var bg = current_theme.get_stylebox("panel", "Panel").bg_color; + // var fg = current_theme.get_color("font_color", "Label"); + //; + // var axes_color : Color = bg.linear_interpolate(fg, 0.25); + // var curve_color : Color = bg.linear_interpolate(fg, 0.75); + Color axes_color = Color(0.9, 0.9, 0.9, 1); + Color curve_color = Color(1, 1, 1, 1); + + if (show_axes) { + for (int i = 0; i < 5; ++i) { //i in range(5) + Vector2 p = transform_point(0.25 * Vector2(i, i)); + draw_line(Vector2(p.x, 0), Vector2(p.x, get_size().y - 1), axes_color); + draw_line(Vector2(0, p.y), Vector2(get_size().x - 1, p.y), axes_color); + } + } + + Vector points = curve->get_points(); + + for (int i = 0; i < points.size() - 1; ++i) { //i in range(points.size() - 1) + Vector2 p1 = points[i].p; + Vector2 p2 = points[i + 1].p; + float d = (p2.x - p1.x) / 3.0; + float yac = p1.y + d * points[i].rs; + float ybc = p2.y - d * points[i + 1].ls; + Vector2 p = transform_point(p1); + int count = max(1, int((transform_point(p2).x - p.x / 5.0))); + + for (int tt = 0; tt < count; ++tt) { //tt in range(count) + Variant = (tt + 1.0) / count; + Variant = (1.0 - t); + Variant = omt * omt; + Variant = omt2 * omt; + Variant = t * t; + Variant = t2 * t; + Variant = p1.x + (p2.x - p1.x) * t; + Variant = transform_point(Vector2(x, p1.y * omt3 + yac * omt2 * t * 3.0 + ybc * omt * t2 * 3.0 + p2.y * t3)); + draw_line(p, np, curve_color); + p = np; + } + } +} + +void CurveView::_on_resize() { + update(); +} + +CurveView::CurveView() { + show_axes = false; + + set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_MINSIZE, 4); + + set_mouse_filter(MOUSE_FILTER_IGNORE); +} + +CurveView::~CurveView() { +} + +void CurveView::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_POSTINITIALIZE: { + connect("resized", this, "_on_resize"); + update(); + } break; + case NOTIFICATION_DRAW: { + _draw(); + } break; + default: { + } break; + } +} + +void CurveView::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_show_axes"), &CurveView::get_show_axes); + ClassDB::bind_method(D_METHOD("set_show_axes", "value"), &CurveView::set_show_axes); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_axes"), "set_show_axes", "get_show_axes"); + + ClassDB::bind_method(D_METHOD("get_curve"), &CurveView::get_curve); + ClassDB::bind_method(D_METHOD("set_curve", "value"), &CurveView::set_curve); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveBase"), "set_curve", "get_curve"); + + ClassDB::bind_method(D_METHOD("_on_resize"), &CurveView::_on_resize); + + ClassDB::bind_method(D_METHOD("set_curve", "val"), &CurveView::set_curve); + ClassDB::bind_method(D_METHOD("transform_point", "p"), &CurveView::transform_point); + ClassDB::bind_method(D_METHOD("reverse_transform_point", "p"), &CurveView::reverse_transform_point); +} diff --git a/modules/material_maker/editor/widgets/curve_edit/curve_view.cpp b/modules/material_maker/editor/widgets/curve_edit/curve_view.cpp index d2acbe35c..2fa21e2ed 100644 --- a/modules/material_maker/editor/widgets/curve_edit/curve_view.cpp +++ b/modules/material_maker/editor/widgets/curve_edit/curve_view.cpp @@ -1,160 +1,124 @@ #include "curve_view.h" - -Variant CurveView::get_Variant() { - return Variant; -} - -void CurveView::set_Variant(const Variant &val) { -Variant = val; -} - +#include "../../../nodes/bases/curve_base.h" +#include "core/vector.h" bool CurveView::get_show_axes() const { - return show_axes; + return show_axes; } void CurveView::set_show_axes(const bool val) { -show_axes = val; + show_axes = val; } - -Variant CurveView::get_Variant() { - return Variant; +Ref CurveView::get_curve() { + return curve; } -void CurveView::set_Variant(const Variant &val) { -Variant = val; +void CurveView::set_curve(const Ref &val) { + curve = val; + update(); } - - - //tool; - //var MMCurve = preload("res://addons/mat_maker_gd/nodes/bases/curve_base.gd"); - //export ; - bool show_axes = false; - //: MMCurve; - Variant ; - - void CurveView::_ready() { - // curve = MMCurve.new(); - connect("resized", self, "_on_resize"); - update(); +Vector2 CurveView::transform_point(const Vector2 &p) { + return (Vector2(0.0, 1.0) + Vector2(1.0, -1.0) * p) * get_size(); } - - void CurveView::set_curve(const Variant &val) { - curve = val; - update(); +Vector2 CurveView::reverse_transform_point(const Vector2 &p) { + return Vector2(0.0, 1.0) + Vector2(1.0, -1.0) * p / get_size(); } +void CurveView::_draw() { + if (!curve.is_valid()) { + return; + } - Vector2 CurveView::transform_point(const Vector2 &p) { - return (Vector2(0.0, 1.0)+Vector2(1.0, -1.0)*p)*rect_size; + // var current_theme : Theme = get_node("/root/MainWindow").theme; + //; + // var bg = current_theme.get_stylebox("panel", "Panel").bg_color; + // var fg = current_theme.get_color("font_color", "Label"); + //; + // var axes_color : Color = bg.linear_interpolate(fg, 0.25); + // var curve_color : Color = bg.linear_interpolate(fg, 0.75); + Color axes_color = Color(0.9, 0.9, 0.9, 1); + Color curve_color = Color(1, 1, 1, 1); + + if (show_axes) { + for (int i = 0; i < 5; ++i) { //i in range(5) + Vector2 p = transform_point(0.25 * Vector2(i, i)); + draw_line(Vector2(p.x, 0), Vector2(p.x, get_size().y - 1), axes_color); + draw_line(Vector2(0, p.y), Vector2(get_size().x - 1, p.y), axes_color); + } + } + + Vector points = curve->get_points(); + + for (int i = 0; i < points.size() - 1; ++i) { + Vector2 p1 = points[i].p; + Vector2 p2 = points[i + 1].p; + float d = (p2.x - p1.x) / 3.0; + float yac = p1.y + d * points[i].rs; + float ybc = p2.y - d * points[i + 1].ls; + Vector2 p = transform_point(p1); + int count = MAX(1, static_cast((transform_point(p2).x - p.x / 5.0))); + + for (int tt = 0; tt < count; ++tt) { + float t = (tt + 1.0) / count; + float omt = (1.0 - t); + float omt2 = omt * omt; + float omt3 = omt2 * omt; + float t2 = t * t; + float t3 = t2 * t; + float x = p1.x + (p2.x - p1.x) * t; + Vector2 np = transform_point(Vector2(x, p1.y * omt3 + yac * omt2 * t * 3.0 + ybc * omt * t2 * 3.0 + p2.y * t3)); + draw_line(p, np, curve_color); + p = np; + } + } } - - Vector2 CurveView::reverse_transform_point(const Vector2 &p) { - return Vector2(0.0, 1.0)+Vector2(1.0, -1.0)*p/rect_size; +void CurveView::_on_resize() { + update(); } +CurveView::CurveView() { + show_axes = false; - void CurveView::_draw() { + set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_MINSIZE, 4); - if (!curve) { - return; + set_mouse_filter(MOUSE_FILTER_IGNORE); } - // var current_theme : Theme = get_node("/root/MainWindow").theme; - //; - // var bg = current_theme.get_stylebox("panel", "Panel").bg_color; - // var fg = current_theme.get_color("font_color", "Label"); - //; - // var axes_color : Color = bg.linear_interpolate(fg, 0.25); - // var curve_color : Color = bg.linear_interpolate(fg, 0.75); - Color axes_color = Color(0.9, 0.9, 0.9, 1); - Color curve_color = Color(1, 1, 1, 1); - - if (show_axes) { - - for (int i = 0; i < 5; ++i) { //i in range(5) - Variant = transform_point(0.25*Vector2(i, i)); - draw_line(Vector2(p.x, 0), Vector2(p.x, rect_size.y-1), axes_color); - draw_line(Vector2(0, p.y), Vector2(rect_size.x-1, p.y), axes_color); +CurveView::~CurveView() { } +void CurveView::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_POSTINITIALIZE: { + connect("resized", this, "_on_resize"); + update(); + } break; + case NOTIFICATION_DRAW: { + _draw(); + } break; + default: { + } break; + } } - Variant = curve.get_points(); +void CurveView::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_show_axes"), &CurveView::get_show_axes); + ClassDB::bind_method(D_METHOD("set_show_axes", "value"), &CurveView::set_show_axes); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_axes"), "set_show_axes", "get_show_axes"); - for (int i = 0; i < points.size() - 1; ++i) { //i in range(points.size() - 1) - Variant = points[i].p; - Variant = points[i+1].p; - Variant = (p2.x-p1.x)/3.0; - Variant = p1.y+d*points[i].rs; - Variant = p2.y-d*points[i+1].ls; - Variant = transform_point(p1); - int count = max(1, int((transform_point(p2).x-p.x/5.0))); + ClassDB::bind_method(D_METHOD("get_curve"), &CurveView::get_curve); + ClassDB::bind_method(D_METHOD("set_curve", "value"), &CurveView::set_curve); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveBase"), "set_curve", "get_curve"); - for (int tt = 0; tt < count; ++tt) { //tt in range(count) - Variant = (tt+1.0)/count; - Variant = (1.0 - t); - Variant = omt * omt; - Variant = omt2 * omt; - Variant = t * t; - Variant = t2 * t; - Variant = p1.x+(p2.x-p1.x)*t; - Variant = transform_point(Vector2(x, p1.y*omt3 + yac*omt2*t*3.0 + ybc*omt*t2*3.0 + p2.y*t3)); - draw_line(p, np, curve_color); - p = np; + ClassDB::bind_method(D_METHOD("_on_resize"), &CurveView::_on_resize); + + ClassDB::bind_method(D_METHOD("set_curve", "val"), &CurveView::set_curve); + ClassDB::bind_method(D_METHOD("transform_point", "p"), &CurveView::transform_point); + ClassDB::bind_method(D_METHOD("reverse_transform_point", "p"), &CurveView::reverse_transform_point); } - -} - -} - - - void CurveView::_on_resize() { - update(); -} - -} - - CurveView::CurveView() { - //var MMCurve = preload("res://addons/mat_maker_gd/nodes/bases/curve_base.gd"); - show_axes = false; - ; - } - - CurveView::~CurveView() { - } - - - static void CurveView::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_Variant"), &CurveView::get_Variant); - ClassDB::bind_method(D_METHOD("set_Variant", "value"), &CurveView::set_Variant); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "Variant", PROPERTY_HINT_RESOURCE_TYPE, "Variant"), "set_Variant", "get_Variant"); - - - ClassDB::bind_method(D_METHOD("get_show_axes"), &CurveView::get_show_axes); - ClassDB::bind_method(D_METHOD("set_show_axes", "value"), &CurveView::set_show_axes); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_axes"), "set_show_axes", "get_show_axes"); - - - ClassDB::bind_method(D_METHOD("get_Variant"), &CurveView::get_Variant); - ClassDB::bind_method(D_METHOD("set_Variant", "value"), &CurveView::set_Variant); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "Variant", PROPERTY_HINT_RESOURCE_TYPE, "Variant"), "set_Variant", "get_Variant"); - - - ClassDB::bind_method(D_METHOD("_ready"), &CurveView::_ready); - ClassDB::bind_method(D_METHOD("set_curve", "val"), &CurveView::set_curve); - ClassDB::bind_method(D_METHOD("transform_point", "p"), &CurveView::transform_point); - ClassDB::bind_method(D_METHOD("reverse_transform_point", "p"), &CurveView::reverse_transform_point); - ClassDB::bind_method(D_METHOD("_draw"), &CurveView::_draw); - ClassDB::bind_method(D_METHOD("_on_resize"), &CurveView::_on_resize); - - } - - - diff --git a/modules/material_maker/editor/widgets/curve_edit/curve_view.ctscn b/modules/material_maker/editor/widgets/curve_edit/curve_view.ctscn deleted file mode 100644 index 3818b08f4..000000000 --- a/modules/material_maker/editor/widgets/curve_edit/curve_view.ctscn +++ /dev/null @@ -1,46 +0,0 @@ - -void construct() { - -//Script: res://addons/mat_maker_gd/widgets/curve_edit/curve_view.gd -Control *curveview = memnew(Control); -curveview->set_name("CurveView"); - -curveview->set_name("CurveView"); -//curveview->set("name", CurveView)); - -curveview->set_filename("res://addons/mat_maker_gd/widgets/curve_edit/curve_view.tscn"); -//curveview->set("filename", "res://addons/mat_maker_gd/widgets/curve_edit/curve_view.tscn"); - -curveview->set_anchor_right(1); -//curveview->set("anchor_right", 1); - -curveview->set_anchor_bottom(1); -//curveview->set("anchor_bottom", 1); - -curveview->set_margin_left(4); -//curveview->set("margin_left", 4); - -curveview->set_margin_top(4); -//curveview->set("margin_top", 4); - -curveview->set_margin_right(-4); -//curveview->set("margin_right", -4); - -curveview->set_margin_bottom(-4); -//curveview->set("margin_bottom", -4); - -curveview->set_rect_position(Vector2(4, 4)); -//curveview->set("rect_position", Vector2(4, 4)); - -curveview->set_rect_global_position(Vector2(4, 4)); -//curveview->set("rect_global_position", Vector2(4, 4)); - -curveview->set_mouse_filter(2); -//curveview->set("mouse_filter", 2); - -//curveview property __meta__ TYPE_DICTIONARY value: {_edit_use_anchors_:False} - - - - -} diff --git a/modules/material_maker/editor/widgets/curve_edit/curve_view.h b/modules/material_maker/editor/widgets/curve_edit/curve_view.h index 58b086f28..40335a536 100644 --- a/modules/material_maker/editor/widgets/curve_edit/curve_view.h +++ b/modules/material_maker/editor/widgets/curve_edit/curve_view.h @@ -1,41 +1,38 @@ #ifndef CURVE_VIEW_H #define CURVE_VIEW_H +#include "core/reference.h" + +#include "scene/gui/control.h" + +class CurveBase; class CurveView : public Control { - GDCLASS(CurveView, Control); + GDCLASS(CurveView, Control); - public: +public: + bool get_show_axes() const; + void set_show_axes(const bool val); - Variant get_Variant(); - void set_Variant(const Variant &val); + Ref get_curve(); + void set_curve(const Ref &val); - bool get_show_axes() const; - void set_show_axes(const bool val); + Vector2 transform_point(const Vector2 &p); + Vector2 reverse_transform_point(const Vector2 &p); - Variant get_Variant(); - void set_Variant(const Variant &val); + void _draw(); + void _on_resize(); - void _ready(); - void set_curve(const Variant &val); - Vector2 transform_point(const Vector2 &p); - Vector2 reverse_transform_point(const Vector2 &p); - void _draw(); - void _on_resize(); + CurveView(); + ~CurveView(); - CurveView(); - ~CurveView(); +protected: + void _notification(int p_what); - protected: - static void _bind_methods(); + static void _bind_methods(); - //tool - Variant = preload("res://addons/mat_maker_gd/nodes/bases/curve_base.gd"); - //export - bool show_axes = false; - //: MMCurve - Variant ; + bool show_axes = false; + Ref curve; }; - #endif