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 04c5f96dc..6130380b3 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.cpp +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_cursor.cpp @@ -14,9 +14,9 @@ void GradientCursor::set_cursor_color(const Color &val) { void GradientCursor::set_cursor_color_notify(const Color &val) { color = val; - MMGradientEditor *ged = Object::cast_to(get_parent()); - ERR_FAIL_COND(!ged); - ged->update_value(); + //MMGradientEditor *ged = Object::cast_to(get_parent()); + //ERR_FAIL_COND(!ged); + //ged->update_value(); update(); } @@ -57,27 +57,31 @@ void GradientCursor::_gui_input(const Ref &ev) { if (iemb.is_valid()) { if (iemb->get_button_index() == BUTTON_LEFT) { if (iemb->is_doubleclick()) { - ged->save_color_state(); - ged->select_color(this, iemb->get_global_position()); + //ged->save_color_state(); + //ged->select_color(this, iemb->get_global_position()); + ged->cursor_doubleclicked(this, iemb->get_global_position()); } else if (iemb->is_pressed()) { - ged->save_color_state(); + ged->cursor_move_started(this); + //ged->save_color_state(); sliding = true; label->set_visible(true); label->set_text(String::num(get_cursor_position(), 3)); } else { if (sliding) { - ged->undo_redo_save_color_state(); + //ged->undo_redo_save_color_state(); + ged->cursor_move_ended(); } sliding = false; label->set_visible(false); } } else if (iemb->get_button_index() == BUTTON_RIGHT && ged->get_sorted_cursors().size() > 2) { - ged->save_color_state(); - ged->remove_child(this); - ged->update_value(); - ged->undo_redo_save_color_state(); - queue_delete(); + //ged->save_color_state(); + //ged->remove_child(this); + //ged->update_value(); + //ged->undo_redo_save_color_state(); + //queue_delete(); + ged->cursor_delete_request(this); } } else if (iemm.is_valid() && (iemm->get_button_mask() & BUTTON_MASK_LEFT) != 0 && sliding) { Vector2 position = get_position(); @@ -91,7 +95,7 @@ void GradientCursor::_gui_input(const Ref &ev) { position.x = MIN(MAX(0, position.x), ged->get_size().x - get_size().x); set_position(position); - ged->update_value(); + //ged->update_value(); label->set_text(String::num(get_cursor_position(), 3)); } } 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 a019c6229..731cf3c8a 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.cpp +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.cpp @@ -2,6 +2,7 @@ #include "gradient_editor.h" #include "../../../nodes/bases/gradient_base.h" +#include "../../../nodes/mm_material.h" #include "../../../nodes/mm_node_universal_property.h" #include "../../mm_graph_node.h" @@ -23,6 +24,8 @@ MMGraphNode *MMGradientEditor::get_graph_node() { void MMGradientEditor::set_graph_node(MMGraphNode *val) { graph_node = val; + + _material = graph_node->get_mm_material(); } Ref MMGradientEditor::get_value() { @@ -31,8 +34,12 @@ Ref MMGradientEditor::get_value() { void MMGradientEditor::set_value(const Ref &val) { value = val; - update_preview(); - call_deferred("update_cursors"); + + _update_preview_queued = true; + _update_cursors_queued = true; + + //update_preview(); + //call_deferred("update_cursors"); } bool MMGradientEditor::get_embedded() const { @@ -59,18 +66,6 @@ void MMGradientEditor::set_saved_points(const PoolRealArray &val) { _saved_points = val; } -GradientCursor *MMGradientEditor::get_active_cursor() { - return active_cursor; -} - -void MMGradientEditor::set_active_cursor(GradientCursor *val) { - active_cursor = val; -} - -void MMGradientEditor::_init() { - connect("resized", this, "on_resized"); -} - void MMGradientEditor::ignore_changes(const bool val) { graph_node->ignore_changes(val); } @@ -107,16 +102,40 @@ void MMGradientEditor::undo_redo_save_color_state() { ignore_changes(false); } +void MMGradientEditor::cursor_move_started(GradientCursor *c) { + _cursor_started_moving = true; + _cursor_moving = true; +} +void MMGradientEditor::cursor_move_ended() { + _cursor_stopped_moving = true; + _cursor_moving = false; +} +void MMGradientEditor::cursor_delete_request(GradientCursor *c) { + _cursor_delete_request = c; +} +void MMGradientEditor::cursor_doubleclicked(GradientCursor *c, const Vector2 &position) { + _cursor_doubleclicked = c; + _cursor_doubleclick_position = position; +} + void MMGradientEditor::update_cursors() { + LocalVector cursors; + for (int i = 0; i < get_child_count(); ++i) { GradientCursor *c = Object::cast_to(get_child(i)); if (c) { - remove_child(c); + cursors.push_back(c); c->queue_delete(); } } + for (int i = 0; i < cursors.size(); ++i) { + remove_child(cursors[i]); + } + + cursors.clear(); + int vs = value->get_point_count(); for (int i = 0; i < vs; ++i) { //i in range(vs) @@ -143,7 +162,8 @@ void MMGradientEditor::update_value() { } value->set_points(points); - update_preview(); + //_update_cursors_queued = true; + //update_preview(); } void MMGradientEditor::add_cursor(const float x, const Color &color) { @@ -164,10 +184,10 @@ void MMGradientEditor::_gui_input(const Ref &ev) { if (iemb.is_valid() && iemb->get_button_index() == 1 && iemb->is_doubleclick()) { if (iemb->get_position().y > 15) { 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(); - undo_redo_save_color_state(); + + _cursor_add_queued = true; + _cursor_add_x = p; + } else if (embedded) { GradientPopup *popup = memnew(GradientPopup); add_child(popup); @@ -184,8 +204,6 @@ void MMGradientEditor::_gui_input(const Ref &ev) { } void MMGradientEditor::select_color(GradientCursor *cursor, const Vector2 &position) { - active_cursor = cursor; - ColorPickerPopup *color_picker_popup = memnew(ColorPickerPopup); add_child(color_picker_popup); @@ -194,7 +212,7 @@ void MMGradientEditor::select_color(GradientCursor *cursor, const Vector2 &posit color_picker->connect("color_changed", cursor, "set_cursor_color_notify"); color_picker_popup->set_position(position); - color_picker_popup->connect("popup_hide", this, "undo_redo_save_color_state"); + color_picker_popup->connect("popup_hide", this, "on_color_selector_closed"); color_picker_popup->connect("popup_hide", color_picker_popup, "queue_free"); color_picker_popup->popup(); } @@ -261,13 +279,19 @@ Color MMGradientEditor::get_gradient_color(const float x) { } void MMGradientEditor::update_preview() { - call_deferred("generate_preview_image"); + //call_deferred("generate_preview_image"); + generate_preview_image(); } void MMGradientEditor::_on_Interpolation_item_selected(const int ID) { + _new_interpolation = ID; + _interpolation_change_queued = true; +} + +void MMGradientEditor::apply_new_interpolation() { ignore_changes(true); _undo_redo->create_action("MMGD: gradient interpolation_type changed"); - _undo_redo->add_do_method(value.ptr(), "set_interpolation_type", ID); + _undo_redo->add_do_method(value.ptr(), "set_interpolation_type", _new_interpolation); _undo_redo->add_undo_method(value.ptr(), "set_interpolation_type", value->get_interpolation_type()); _undo_redo->commit_action(); ignore_changes(false); @@ -275,17 +299,34 @@ void MMGradientEditor::_on_Interpolation_item_selected(const int ID) { } void MMGradientEditor::on_resized() { - if (value.is_valid()) { - update_preview(); - call_deferred("update_cursors"); - } + _update_preview_queued = true; + _update_cursors_queued = true; +} + +void MMGradientEditor::on_color_selector_closed() { + _color_selection_done = true; + _selecting_color = false; } MMGradientEditor::MMGradientEditor() { graph_node = nullptr; embedded = false; _undo_redo = nullptr; - active_cursor = nullptr; + _update_preview_queued = false; + _update_cursors_queued = false; + + _cursor_moving = false; + _cursor_started_moving = false; + _cursor_stopped_moving = false; + _cursor_doubleclicked = nullptr; + _cursor_delete_request = nullptr; + _cursor_add_queued = false; + _cursor_add_x = 0; + _selecting_color = false; + _color_selection_done = false; + + _interpolation_change_queued = false; + _new_interpolation = 0; set_custom_minimum_size(Vector2(120, 32)); set_focus_mode(FOCUS_CLICK); @@ -347,11 +388,103 @@ MMGradientEditor::MMGradientEditor() { cursor_value_label->set("custom_constants/shadow_as_outline", 1); add_child(cursor_value_label); + + set_process_internal(true); } MMGradientEditor::~MMGradientEditor() { } +void MMGradientEditor::_notification(int p_what) { + if (p_what == NOTIFICATION_POSTINITIALIZE) { + connect("resized", this, "on_resized"); + } else if (p_what == NOTIFICATION_INTERNAL_PROCESS) { + if (value.is_valid() && _material.is_valid()) { + if (_material->get_rendering()) { + return; + } + + if (_selecting_color) { + //update_preview(); + return; + } + + if (_color_selection_done) { + _color_selection_done = false; + + save_color_state(); + update_value(); + undo_redo_save_color_state(); + } + + if (_cursor_started_moving) { + _cursor_started_moving = false; + save_color_state(); + } + + if (_cursor_moving) { + //update_preview(); + return; + } + + if (_cursor_stopped_moving) { + _cursor_stopped_moving = false; + + update_value(); + undo_redo_save_color_state(); + _update_preview_queued = true; + } + + if (_cursor_doubleclicked) { + if (ObjectDB::instance_validate(_cursor_doubleclicked)) { + select_color(_cursor_doubleclicked, _cursor_doubleclick_position); + } + + _cursor_doubleclicked = nullptr; + return; + } + + if (_interpolation_change_queued) { + _interpolation_change_queued = false; + apply_new_interpolation(); + } + + if (_cursor_add_queued) { + _cursor_add_queued = false; + + save_color_state(); + add_cursor(_cursor_add_x, get_gradient_color(_cursor_add_x)); + update_value(); + undo_redo_save_color_state(); + + _update_preview_queued = true; + } + + if (_cursor_delete_request) { + if (ObjectDB::instance_validate(_cursor_delete_request)) { + save_color_state(); + _cursor_delete_request->queue_delete(); + remove_child(_cursor_delete_request); + update_value(); + undo_redo_save_color_state(); + } + + _cursor_delete_request = nullptr; + } + + if (_update_cursors_queued) { + _update_cursors_queued = false; + update_cursors(); + } + + if (_update_preview_queued) { + _update_preview_queued = false; + update_preview(); + } + } + } +} + void MMGradientEditor::_bind_methods() { ADD_SIGNAL(MethodInfo("updated", PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "GradientBase"))); @@ -375,10 +508,6 @@ void MMGradientEditor::_bind_methods() { ClassDB::bind_method(D_METHOD("set_saved_points", "value"), &MMGradientEditor::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"), &MMGradientEditor::get_active_cursor); - //ClassDB::bind_method(D_METHOD("set_active_cursor", "value"), &MMGradientEditor::set_active_cursor); - //ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "active_cursor", PROPERTY_HINT_RESOURCE_TYPE, "GradientCursor "), "set_active_cursor", "get_active_cursor"); - ClassDB::bind_method(D_METHOD("ignore_changes", "val"), &MMGradientEditor::ignore_changes); ClassDB::bind_method(D_METHOD("save_color_state"), &MMGradientEditor::save_color_state); @@ -405,4 +534,5 @@ void MMGradientEditor::_bind_methods() { ClassDB::bind_method(D_METHOD("_on_Interpolation_item_selected", "ID"), &MMGradientEditor::_on_Interpolation_item_selected); ClassDB::bind_method(D_METHOD("on_resized"), &MMGradientEditor::on_resized); + ClassDB::bind_method(D_METHOD("on_color_selector_closed"), &MMGradientEditor::on_color_selector_closed); } 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 60f9957bc..f91de9aae 100644 --- a/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.h +++ b/modules/material_maker/editor/widgets/gradient_editor/gradient_editor.h @@ -16,10 +16,13 @@ class GradientCursor; class TextureRect; class Label; class OptionButton; +class MMMaterial; class MMGradientEditor : public Control { GDCLASS(MMGradientEditor, Control); + //todo mmnode changed event sub + handle + public: MMGraphNode *get_graph_node(); void set_graph_node(MMGraphNode *val); @@ -36,16 +39,16 @@ public: PoolRealArray get_saved_points(); void set_saved_points(const PoolRealArray &val); - GradientCursor *get_active_cursor(); - void set_active_cursor(GradientCursor *val); - - void _init(); - void ignore_changes(const bool val); - void save_color_state(); + void save_color_state(); void undo_redo_save_color_state(); + void cursor_move_started(GradientCursor *c); + void cursor_move_ended(); + void cursor_delete_request(GradientCursor *c); + void cursor_doubleclicked(GradientCursor *c, const Vector2 &position); + void update_cursors(); void update_value(); @@ -61,16 +64,22 @@ public: void update_preview(); void _on_Interpolation_item_selected(const int ID); + + void apply_new_interpolation(); void on_resized(); + void on_color_selector_closed(); MMGradientEditor(); ~MMGradientEditor(); protected: + void _notification(int p_what); + static void _bind_methods(); MMGraphNode *graph_node; Ref value; + Ref _material; bool embedded; UndoRedo *_undo_redo; PoolRealArray _saved_points; @@ -78,6 +87,24 @@ protected: TextureRect *gradient; OptionButton *interpolation; Label *cursor_value_label; + + bool _update_preview_queued; + bool _update_cursors_queued; + //bool _value_update_queued; + + bool _cursor_moving; + bool _cursor_started_moving; + bool _cursor_stopped_moving; + GradientCursor *_cursor_doubleclicked; + Vector2 _cursor_doubleclick_position; + GradientCursor *_cursor_delete_request; + bool _cursor_add_queued; + float _cursor_add_x; + bool _selecting_color; + bool _color_selection_done; + + bool _interpolation_change_queued; + int _new_interpolation; }; #endif