From 35fadcb6bb3e9887f82695bad315609d4e73a164 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 14 Jun 2022 16:43:44 +0200 Subject: [PATCH] Fixed the remaining compile issues for the gradient editor, and added it to the build. --- modules/material_maker/SCsub | 6 +- .../gradient_editor/gradient_cursor.cpp | 6 +- .../widgets/gradient_editor/gradient_cursor.h | 6 +- .../gradient_editor/gradient_editor.cpp | 183 ++++++++++-------- .../widgets/gradient_editor/gradient_editor.h | 10 +- .../gradient_editor/gradient_popup.cpp | 4 +- .../widgets/gradient_editor/gradient_popup.h | 2 +- 7 files changed, 120 insertions(+), 97 deletions(-) diff --git a/modules/material_maker/SCsub b/modules/material_maker/SCsub index 865cc072a..3d9c08b0c 100644 --- a/modules/material_maker/SCsub +++ b/modules/material_maker/SCsub @@ -37,9 +37,9 @@ sources = [ "editor/widgets/float_edit/float_edit.cpp", - #"editor/widgets/gradient_editor/gradient_cursor.cpp", - #"editor/widgets/gradient_editor/gradient_editor.cpp", - #"editor/widgets/gradient_editor/gradient_popup.cpp", + "editor/widgets/gradient_editor/gradient_cursor.cpp", + "editor/widgets/gradient_editor/gradient_editor.cpp", + "editor/widgets/gradient_editor/gradient_popup.cpp", #"editor/widgets//.cpp", #"editor/widgets//.cpp", diff --git a/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.cpp b/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.cpp index 6ee230ce2..41a6bf171 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.cpp +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.cpp @@ -95,8 +95,8 @@ float GradientCursor::get_cursor_position() { return get_position().x / (get_parent_control()->get_size().x - WIDTH); } -bool GradientCursor::sort(GradientCursor *a, const GradientCursor *b) { - return a->get_position() < b->get_position(); +bool GradientCursor::operator<(const GradientCursor &b) const { + return get_position() < b.get_position(); } bool GradientCursor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { @@ -144,3 +144,5 @@ void GradientCursor::_bind_methods() { ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &GradientCursor::can_drop_data_fw); ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &GradientCursor::drop_data_fw); } + +const int GradientCursor::WIDTH = 10; diff --git a/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.h b/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.h index ecd2e8503..4703552e9 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.h +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.h @@ -27,7 +27,7 @@ public: float get_cursor_position(); - static bool sort(GradientCursor *a, GradientCursor *b); + bool operator<(const GradientCursor &b) const; bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const; void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from); @@ -35,6 +35,8 @@ public: GradientCursor(); ~GradientCursor(); + static const int WIDTH; + protected: void _notification(int p_what); @@ -43,8 +45,6 @@ protected: Color color; bool sliding; Label *label; - - const int WIDTH = 10; }; #endif diff --git a/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.cpp b/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.cpp index 7455518db..edd5034d6 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.cpp +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.cpp @@ -2,11 +2,19 @@ #include "gradient_editor.h" #include "../../../nodes/bases/gradient_base.h" +#include "../../../nodes/mm_node_universal_property.h" #include "../../mm_graph_node.h" +#include "gradient_cursor.h" +#include "gradient_popup.h" + +#include "../color_picker_popup/color_picker_popup.h" +#include "scene/gui/color_picker.h" + #include "scene/gui/color_rect.h" #include "scene/gui/label.h" #include "scene/gui/option_button.h" +#include "scene/gui/texture_rect.h" #include "scene/resources/material.h" MMGraphNode *GradientEditor::get_graph_node() { @@ -23,6 +31,8 @@ Ref GradientEditor::get_value() { void GradientEditor::set_value(const Ref &val) { value = val; + update_preview(); + call_deferred("update_cursors"); } bool GradientEditor::get_embedded() const { @@ -84,77 +94,75 @@ void GradientEditor::undo_redo_save_color_state() { op.push_back(_saved_points[i]); } - for (v in value->get_points()) { - np.push_back(v); + PoolRealArray array = value->get_points(); + + for (int i = 0; i < array.size(); ++i) { + np.push_back(array[i]); } - _undo_redo.create_action("MMGD: gradient colors changed"); - _undo_redo.add_do_method(value, "set_points", np); - _undo_redo.add_undo_method(value, "set_points", op); - _undo_redo.commit_action(); + _undo_redo->create_action("MMGD: gradient colors changed"); + _undo_redo->add_do_method(*value, "set_points", np); + _undo_redo->add_undo_method(*value, "set_points", op); + _undo_redo->commit_action(); ignore_changes(false); } -void GradientEditor::set_undo_redo(const UndoRedo &ur) { - _undo_redo = ur; -} - -void GradientEditor::set_value(const Variant &v) { - value = v; - update_preview(); - call_deferred("update_cursors"); -} - void GradientEditor::update_cursors() { - for (c in get_children()) { - if (c is GradientCursor) { + for (int i = 0; i < get_child_count(); ++i) { + GradientCursor *c = Object::cast_to(get_child(i)); + + if (c) { remove_child(c); - c.free(); + c->queue_delete(); } } - int vs = value.get_point_count(); + int vs = value->get_point_count(); for (int i = 0; i < vs; ++i) { //i in range(vs) - add_cursor(value.get_point_value(i) * (rect_size.x - GradientCursor.WIDTH), value.get_point_color(i)); + add_cursor(value->get_point_value(i) * (get_size().x - GradientCursor::WIDTH), value->get_point_color(i)); } - $Interpolation.selected = value.interpolation_type; + interpolation->select(value->get_interpolation_type()); } void GradientEditor::update_value() { - value.clear(); - Array sc = get_sorted_cursors(); - PoolRealArray points = PoolRealArray(); + value->clear(); + Vector sc = get_sorted_cursors(); + PoolRealArray points; - for (c in sc) { - points.push_back(c.rect_position.x / (rect_size.x - GradientCursor.WIDTH)); - Color color = c.color; + for (int i = 0; i < sc.size(); ++i) { + GradientCursor *c = sc[i]; + + points.push_back(c->get_position().x / (get_size().x - GradientCursor::WIDTH)); + Color color = c->get_color(); points.push_back(color.r); points.push_back(color.g); points.push_back(color.b); points.push_back(color.a); } - value.set_points(points); + value->set_points(points); update_preview(); } -void GradientEditor::add_cursor(const Variant &x, const Variant &color) { - GradientCursor *cursor = GradientCursor.new(); - cursor->set_label(label); - +void GradientEditor::add_cursor(const float x, const Color &color) { + GradientCursor *cursor = memnew(GradientCursor); add_child(cursor); - cursor.rect_position.x = x; - cursor.color = color; + + Vector2 rp = cursor->get_position(); + rp.x = x; + + cursor->set_position(rp); + cursor->set_color(color); } void GradientEditor::_gui_input(const Ref &ev) { Ref iemb = ev; - if (iemb.is_valid() && ev->get_button_index() == 1 && iemb->get_doubleclick()) { + if (iemb.is_valid() && iemb->get_button_index() == 1 && iemb->is_doubleclick()) { if (iemb->get_position().y > 15) { - Variant = clamp(iemb->get_position().x, 0, rect_size.x - GradientCursor.WIDTH); + float p = CLAMP(iemb->get_position().x, 0, get_size().x - GradientCursor::WIDTH); save_color_state(); add_cursor(p, get_gradient_color(p)); update_value(); @@ -163,10 +171,10 @@ void GradientEditor::_gui_input(const Ref &ev) { GradientPopup *popup = memnew(GradientPopup); add_child(popup); Vector2 popup_size = popup->get_size(); - popup->popup(Rect2(ev->get_global_position(), Vector2(0, 0))); - popup->set_global_position(ev->get_global_position() - Vector2(popup_size.x / 2, popup_size.y)); + popup->popup(Rect2(iemb->get_global_position(), Vector2(0, 0))); + popup->set_global_position(iemb->get_global_position() - Vector2(popup_size.x / 2, popup_size.y)); popup->init(value, graph_node, _undo_redo); - popup->connect("updated", self, "set_value"); + popup->connect("updated", this, "set_value"); popup->connect("popup_hide", popup, "queue_free"); } @@ -174,61 +182,67 @@ void GradientEditor::_gui_input(const Ref &ev) { } } -void GradientEditor::select_color(const GradientCursor *cursor, const Vector2 &position) { +void GradientEditor::select_color(GradientCursor *cursor, const Vector2 &position) { active_cursor = cursor; - //var color_picker_popup = preload("res://addons/mat_maker_gd/widgets/color_picker_popup/color_picker_popup.tscn").instance(); + + ColorPickerPopup *color_picker_popup = memnew(ColorPickerPopup); add_child(color_picker_popup); - Variant = color_picker_popup.get_node("ColorPicker"); - color_picker.color = cursor.color; - color_picker.connect("color_changed", cursor, "set_color"); - color_picker_popup.rect_position = position; - color_picker_popup.connect("popup_hide", self, "undo_redo_save_color_state"); - color_picker_popup.connect("popup_hide", color_picker_popup, "queue_free"); - color_picker_popup.popup(); + + ColorPicker *color_picker = color_picker_popup->color_picker; + color_picker->set_pick_color(cursor->get_color()); + color_picker->connect("color_changed", cursor, "set_color"); + + color_picker_popup->set_position(position); + color_picker_popup->connect("popup_hide", this, "undo_redo_save_color_state"); + color_picker_popup->connect("popup_hide", color_picker_popup, "queue_free"); + color_picker_popup->popup(); } // Calculating a color from the gradient and generating the shader; -Array GradientEditor::get_sorted_cursors() { - Variant = []; +Vector GradientEditor::get_sorted_cursors() { + Vector array; - for (c in get_children()) { - if (c is GradientCursor) { - array.append(c); + for (int i = 0; i < get_child_count(); ++i) { + GradientCursor *c = Object::cast_to(get_child(i)); + + if (c) { + array.push_back(c); } } - array.sort_custom(GradientCursor, "sort"); + array.sort(); + return array; } void GradientEditor::generate_preview_image() { - Ref tex = $Gradient.texture; + Ref tex = gradient->get_texture(); if (!tex.is_valid()) { tex.instance(); - $Gradient.texture = tex; + gradient->set_texture(tex); } - Ref img = tex.get_data(); - float w = $Gradient.rect_size.x; - float h = $Gradient.rect_size.y; + Ref img = tex->get_data(); + float w = gradient->get_size().x; + float h = gradient->get_size().y; if (!img.is_valid()) { - img = memnew(Image); + img.instance(); } - if (img->get_size().x != w || img.get_size().y != h) { - img->create(w, h, false, Image.FORMAT_RGBA8); + if (img->get_size().x != w || img->get_size().y != h) { + img->create(w, h, false, Image::FORMAT_RGBA8); } img->lock(); - for (int i = 0; i < w; ++i) { //i in range(w) + for (int i = 0; i < w; ++i) { float x = float(i) / float(w); Color col = value->get_gradient_color(x); - for (int j = 0; j < h; ++j) { //j in range(h) + for (int j = 0; j < h; ++j) { img->set_pixel(i, j, col); } } @@ -238,7 +252,7 @@ void GradientEditor::generate_preview_image() { } Color GradientEditor::get_gradient_color(const float x) { - return value->get_gradient_color(x / (get_size().x - GradientCursor->WIDTH)); + return value->get_gradient_color(x / (get_size().x - GradientCursor::WIDTH)); } void GradientEditor::update_preview() { @@ -247,10 +261,10 @@ void GradientEditor::update_preview() { void GradientEditor::_on_Interpolation_item_selected(const int ID) { ignore_changes(true); - _undo_redo.create_action("MMGD: gradient interpolation_type changed"); - _undo_redo.add_do_method(value, "set_interpolation_type", ID); - _undo_redo.add_undo_method(value, "set_interpolation_type", value->get_interpolation_type()); - _undo_redo.commit_action(); + _undo_redo->create_action("MMGD: gradient interpolation_type changed"); + _undo_redo->add_do_method(value.ptr(), "set_interpolation_type", ID); + _undo_redo->add_undo_method(value.ptr(), "set_interpolation_type", value->get_interpolation_type()); + _undo_redo->commit_action(); ignore_changes(false); update_preview(); } @@ -287,22 +301,22 @@ GradientEditor::GradientEditor() { Ref background_control_prop_material; background_control_prop_material.instance(); - background_control_prop_material->set_shader(bg_shader_code); + background_control_prop_material->set_shader(bg_shader); background_control->set_material(background_control_prop_material); add_child(background_control); - TextureRect *gradient_control = memnew(TextureRect); - add_child(gradient_control); - gradient_control->set_rect_min_size(Vector2(112, 17)); - gradient_control->set_mouse_filter(MOUSE_FILTER_IGNORE); + gradient = memnew(TextureRect); + gradient->set_custom_minimum_size(Vector2(112, 17)); + gradient->set_mouse_filter(MOUSE_FILTER_IGNORE); + add_child(gradient); Ref gradient_control_prop_theme; gradient_control_prop_theme.instance(); - gradient_control->set_theme(gradient_control_prop_theme); + gradient->set_theme(gradient_control_prop_theme); OptionButton *interpolation_control = memnew(OptionButton); - interpolation_control->set_rect_scale(Vector2(0.5, 0.5)); + interpolation_control->set_scale(Vector2(0.5, 0.5)); interpolation_control->add_item("0", 0); interpolation_control->add_item("1", 1); @@ -314,19 +328,20 @@ GradientEditor::GradientEditor() { //interpolation_control->set_icon(interpolation_control_prop_icon); //interpolation_control->set("icon", interpolation_control_prop_icon); //interpolation_control property items TYPE_ARRAY value: [, [AtlasTexture:19169], False, 0, Null, , [AtlasTexture:19168], False, 1, Null, , [AtlasTexture:19170], False, 2, Null, , [AtlasTexture:19171], False, 3, Null] - interpolation_control->set_selected(1); + interpolation_control->select(1); interpolation_control->connect("item_selected", this, "_on_Interpolation_item_selected"); add_child(interpolation_control); Label *value_control = memnew(Label); - value_control->set_align(ALIGN_CENTER); - add_child(value_control); + value_control->set_align(Label::ALIGN_CENTER); value_control->set("custom_colors/font_color", Color(1, 1, 1, 1)); value_control->set("custom_colors/font_color_shadow", Color(0, 0, 0, 1)); value_control->set("custom_constants/shadow_offset_x", 1); value_control->set("custom_constants/shadow_offset_y", 1); value_control->set("custom_constants/shadow_as_outline", 1); + + add_child(value_control); } GradientEditor::~GradientEditor() { @@ -351,8 +366,8 @@ void GradientEditor::_bind_methods() { //ClassDB::bind_method(D_METHOD("set_undo_redo", "value"), &GradientEditor::set_undo_redo); //ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "undo_redo", PROPERTY_HINT_RESOURCE_TYPE, "UndoRedo"), "set_undo_redo", "get_undo_redo"); - ClassDB::bind_method(D_METHOD("get_saved_points"), &GradientEditor::get__saved_points); - ClassDB::bind_method(D_METHOD("set_saved_points", "value"), &GradientEditor::set__saved_points); + ClassDB::bind_method(D_METHOD("get_saved_points"), &GradientEditor::get_saved_points); + ClassDB::bind_method(D_METHOD("set_saved_points", "value"), &GradientEditor::set_saved_points); ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "saved_points"), "set_saved_points", "get_saved_points"); //ClassDB::bind_method(D_METHOD("get_active_cursor"), &GradientEditor::get_active_cursor); @@ -374,9 +389,9 @@ void GradientEditor::_bind_methods() { ClassDB::bind_method(D_METHOD("_gui_input", "ev"), &GradientEditor::_gui_input); - ClassDB::bind_method(D_METHOD("select_color", "cursor", " position"), &GradientEditor::select_color); + //ClassDB::bind_method(D_METHOD("select_color", "cursor", " position"), &GradientEditor::select_color); - ClassDB::bind_method(D_METHOD("get_sorted_cursors"), &GradientEditor::get_sorted_cursors); + //ClassDB::bind_method(D_METHOD("get_sorted_cursors"), &GradientEditor::get_sorted_cursors); ClassDB::bind_method(D_METHOD("generate_preview_image"), &GradientEditor::generate_preview_image); diff --git a/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.h b/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.h index 3c912fc45..1dc5bc277 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.h +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.h @@ -6,12 +6,15 @@ #include "core/reference.h" #include "core/undo_redo.h" #include "core/variant.h" +#include "core/vector.h" #include "scene/gui/control.h" class MMGraphNode; class GradientBase; class GradientCursor; +class TextureRect; +class OptionButton; class GradientEditor : public Control { GDCLASS(GradientEditor, Control); @@ -41,7 +44,6 @@ public: void save_color_state(); void undo_redo_save_color_state(); - void set_undo_redo(const UndoRedo &ur); void update_cursors(); void update_value(); @@ -49,9 +51,9 @@ public: void add_cursor(const float x, const Color &color); void _gui_input(const Ref &ev); - void select_color(const GradientCursor *cursor, const Vector2 &position); + void select_color(GradientCursor *cursor, const Vector2 &position); - Array get_sorted_cursors(); + Vector get_sorted_cursors(); void generate_preview_image(); Color get_gradient_color(const float x); @@ -72,6 +74,8 @@ protected: UndoRedo *_undo_redo; PoolRealArray _saved_points; GradientCursor *active_cursor; + TextureRect *gradient; + OptionButton *interpolation; }; #endif diff --git a/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.cpp b/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.cpp index 361e501ac..caef45d81 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.cpp +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.cpp @@ -4,9 +4,11 @@ #include "core/object.h" #include "gradient_editor.h" +#include "../../../nodes/bases/gradient_base.h" +#include "../../../nodes/mm_node_universal_property.h" #include "scene/gui/panel.h" -void GradientPopup::init(const Ref &value, MMGraphNode *graph_node, UndoRedo &undo_redo) { +void GradientPopup::init(const Ref &value, MMGraphNode *graph_node, UndoRedo *undo_redo) { _gradient_editor->set_undo_redo(undo_redo); _gradient_editor->set_graph_node(graph_node); _gradient_editor->set_value(value); diff --git a/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.h b/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.h index 16770fcee..b64d7c79c 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.h +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_popup.h @@ -14,7 +14,7 @@ class GradientPopup : public Popup { GDCLASS(GradientPopup, Popup); public: - void init(const Ref &value, MMGraphNode *graph_node, UndoRedo &undo_redo); + void init(const Ref &value, MMGraphNode *graph_node, UndoRedo *undo_redo); void _on_Control_updated(const Ref &value); void _on_GradientPopup_popup_hide();