From fc969f43319b57808e5a6f85c074ca10dcbbfc89 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 4 Mar 2023 15:11:55 +0100 Subject: [PATCH] Now the MMTones Editor actually works. --- .../widgets/tones_editor/tones_editor.cpp | 28 ++++++---- .../widgets/tones_editor/tones_editor.h | 2 + .../tones_editor/tones_editor_cursor.cpp | 54 ++++++++++++++++--- .../tones_editor/tones_editor_cursor.h | 2 + modules/material_maker/nodes/filter/tones.cpp | 9 ++++ modules/material_maker/nodes/filter/tones.h | 6 +++ 6 files changed, 85 insertions(+), 16 deletions(-) diff --git a/modules/material_maker/editor/widgets/tones_editor/tones_editor.cpp b/modules/material_maker/editor/widgets/tones_editor/tones_editor.cpp index 8d7482ffb..930a21a4d 100644 --- a/modules/material_maker/editor/widgets/tones_editor/tones_editor.cpp +++ b/modules/material_maker/editor/widgets/tones_editor/tones_editor.cpp @@ -26,6 +26,8 @@ void MMTonesEditor::set_value(const Ref &v) { if (_node.is_valid()) { _node->get_image()->connect("changed", this, "on_input_property_changed"); + + set_mode(static_cast(_node->get_current_mode())); } on_input_property_changed(); @@ -39,6 +41,17 @@ enum ParameterTypes { PARAMETER_TYPE_OUT_MAX, }; +void MMTonesEditor::set_mode(Modes mode) { + _current_mode = mode; + + _mode_ob->select(static_cast(mode)); + _cursor_in_min->set_value(get_parameter_current_mode(PARAMETER_TYPE_IN_MIN)); + _cursor_in_mid->set_value(get_parameter_current_mode(PARAMETER_TYPE_IN_MID)); + _cursor_in_max->set_value(get_parameter_current_mode(PARAMETER_TYPE_IN_MAX)); + _cursor_out_min->set_value(get_parameter_current_mode(PARAMETER_TYPE_OUT_MIN)); + _cursor_out_max->set_value(get_parameter_current_mode(PARAMETER_TYPE_OUT_MAX)); +} + Color MMTonesEditor::get_parameter(ParameterTypes type) { if (!_node.is_valid()) { return Color(); @@ -195,9 +208,11 @@ MMTonesEditor::MMTonesEditor() { auto_button->set_tooltip("Set levels automatically"); auto_button->set_v_size_flags(Control::SIZE_SHRINK_CENTER); + /* Control *spacer = memnew(Control); spacer->set_custom_minimum_size(Vector2(0, 4)); add_child(spacer); + */ _histogram_tr = memnew(TextureRect); _histogram_tr->set_custom_minimum_size(Vector2(0, 100)); @@ -228,11 +243,11 @@ MMTonesEditor::MMTonesEditor() { _cursor_out_min->connect("cursor_value_changed", this, "on_cursor_value_changed"); _cursor_out_max->connect("cursor_value_changed", this, "on_cursor_value_changed"); + /* spacer = memnew(Control); spacer->set_custom_minimum_size(Vector2(0, 4)); add_child(spacer); - - on_mode_item_selected(MODE_LUMINANCE); + */ } MMTonesEditor::~MMTonesEditor() { @@ -293,13 +308,8 @@ void MMTonesEditor::on_auto_levels_pressed() { } void MMTonesEditor::on_mode_item_selected(int id) { - _current_mode = static_cast(id); - - _cursor_in_min->set_value(get_parameter_current_mode(PARAMETER_TYPE_IN_MIN)); - _cursor_in_mid->set_value(get_parameter_current_mode(PARAMETER_TYPE_IN_MID)); - _cursor_in_max->set_value(get_parameter_current_mode(PARAMETER_TYPE_IN_MAX)); - _cursor_out_min->set_value(get_parameter_current_mode(PARAMETER_TYPE_OUT_MIN)); - _cursor_out_max->set_value(get_parameter_current_mode(PARAMETER_TYPE_OUT_MAX)); + _node->set_current_mode(id); + set_mode(static_cast(id)); } void MMTonesEditor::on_cursor_value_changed(Control *cursor, float position) { diff --git a/modules/material_maker/editor/widgets/tones_editor/tones_editor.h b/modules/material_maker/editor/widgets/tones_editor/tones_editor.h index 068fe163e..451fd542b 100644 --- a/modules/material_maker/editor/widgets/tones_editor/tones_editor.h +++ b/modules/material_maker/editor/widgets/tones_editor/tones_editor.h @@ -37,6 +37,8 @@ public: PARAMETER_TYPE_OUT_MAX, }; + void set_mode(Modes mode); + Color get_parameter(ParameterTypes type); float get_parameter_current_mode(ParameterTypes type); diff --git a/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.cpp b/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.cpp index 08f37bd3a..14c69bc99 100644 --- a/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.cpp +++ b/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.cpp @@ -48,10 +48,26 @@ void MMTonesEditorCursor::resize() { } void MMTonesEditorCursor::_gui_input(const Ref &p_event) { - Ref iemm = p_event; + Ref iemb = p_event; + + if (iemb.is_valid()) { + if (iemb->is_pressed()) { + if (_pointer_index != -1) { + return; + } + + if (iemb->get_button_index() != BUTTON_LEFT) { + return; + } + + _pointer_index = 1; + } else { + if (_pointer_index != 1) { + return; + } + + _pointer_index = -1; - if (iemm.is_valid()) { - if (((iemm->get_button_mask() & 1) != 0)) { Control *parent = get_parent_control(); if (!parent) { @@ -62,15 +78,39 @@ void MMTonesEditorCursor::_gui_input(const Ref &p_event) { Vector2 pos = get_position(); - pos.x += iemm->get_relative().x; - pos.x = MIN(MAX(-0.5 * CURSOR_WIDTH, pos.x), parent_size.x - 0.5 * CURSOR_WIDTH); - - update_value((pos.x + 0.5 * CURSOR_WIDTH) / parent_size.x); + update_value(pos.x / parent_size.x); } + + return; + } + + if (_pointer_index != 1) { + return; + } + + Ref iemm = p_event; + + if (iemm.is_valid()) { + Control *parent = get_parent_control(); + + if (!parent) { + return; + } + + Vector2 parent_size = parent->get_size(); + + Vector2 pos = get_position(); + + pos.x += iemm->get_relative().x; + pos.x = MIN(MAX(-0.5 * CURSOR_WIDTH, pos.x), parent_size.x - 0.5 * CURSOR_WIDTH); + + set_value((pos.x + 0.5 * CURSOR_WIDTH) / parent_size.x); } } MMTonesEditorCursor::MMTonesEditorCursor() { + _pointer_index = -1; + set_mouse_filter(MOUSE_FILTER_STOP); } MMTonesEditorCursor::~MMTonesEditorCursor() { diff --git a/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.h b/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.h index ff5acc996..09b3458fa 100644 --- a/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.h +++ b/modules/material_maker/editor/widgets/tones_editor/tones_editor_cursor.h @@ -31,6 +31,8 @@ protected: Color _color; float _position; bool _top; + + int _pointer_index; }; #endif diff --git a/modules/material_maker/nodes/filter/tones.cpp b/modules/material_maker/nodes/filter/tones.cpp index 41cbb7e80..d9374bc74 100644 --- a/modules/material_maker/nodes/filter/tones.cpp +++ b/modules/material_maker/nodes/filter/tones.cpp @@ -61,6 +61,13 @@ void MMTones::set_out_min(const Color &val) { set_dirty(true); } +int MMTones::get_current_mode() { + return _current_mode; +} +void MMTones::set_current_mode(int val) { + _current_mode = val; +} + void MMTones::_init_properties() { if (!input.is_valid()) { input.instance(); @@ -113,6 +120,8 @@ MMTones::MMTones() { _in_min = Color(0, 0, 0, 0); _out_max = Color(1, 1, 1, 1); _out_min = Color(0, 0, 0, 0); + + _current_mode = 0; } MMTones::~MMTones() { diff --git a/modules/material_maker/nodes/filter/tones.h b/modules/material_maker/nodes/filter/tones.h index be13cb333..bb2a012b5 100644 --- a/modules/material_maker/nodes/filter/tones.h +++ b/modules/material_maker/nodes/filter/tones.h @@ -29,6 +29,10 @@ public: Color get_out_min() const; void set_out_min(const Color &val); + // For the editor + int get_current_mode(); + void set_current_mode(int val); + void _init_properties(); void _register_methods(MMGraphNode *mm_graph_node); void _render(const Ref &material); @@ -48,6 +52,8 @@ protected: Color _in_min; Color _out_max; Color _out_min; + + int _current_mode; }; #endif