From 0eb97c11cfd272e3b763522565f9e7639515d74b Mon Sep 17 00:00:00 2001 From: Yuri Roubinsky Date: Sat, 3 Jul 2021 11:11:10 +0300 Subject: [PATCH] Few improvements for constants in visual shader --- .../plugins/visual_shader_editor_plugin.cpp | 103 +++++++----------- editor/plugins/visual_shader_editor_plugin.h | 8 +- scene/resources/visual_shader_nodes.cpp | 12 +- 3 files changed, 52 insertions(+), 71 deletions(-) diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 038ce1467ea..7aff2816c15 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -110,7 +110,6 @@ void VisualShaderGraphPlugin::_bind_methods() { ClassDB::bind_method("set_expression", &VisualShaderGraphPlugin::set_expression); ClassDB::bind_method("update_curve", &VisualShaderGraphPlugin::update_curve); ClassDB::bind_method("update_curve_xyz", &VisualShaderGraphPlugin::update_curve_xyz); - ClassDB::bind_method("update_constant", &VisualShaderGraphPlugin::update_constant); } void VisualShaderGraphPlugin::register_shader(VisualShader *p_shader) { @@ -237,18 +236,6 @@ int VisualShaderGraphPlugin::get_constant_index(float p_constant) const { return 0; } -void VisualShaderGraphPlugin::update_constant(VisualShader::Type p_type, int p_node_id) { - if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links[p_node_id].const_op) { - return; - } - VisualShaderNodeFloatConstant *float_const = Object::cast_to(links[p_node_id].visual_node); - if (!float_const) { - return; - } - links[p_node_id].const_op->select(get_constant_index(float_const->get_constant())); - links[p_node_id].graph_node->set_size(Size2(-1, -1)); -} - void VisualShaderGraphPlugin::set_expression(VisualShader::Type p_type, int p_node_id, const String &p_expression) { if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links[p_node_id].expression_edit) { return; @@ -267,10 +254,6 @@ void VisualShaderGraphPlugin::register_default_input_button(int p_node_id, int p links[p_node_id].input_ports.insert(p_port_id, { p_button }); } -void VisualShaderGraphPlugin::register_constant_option_btn(int p_node_id, OptionButton *p_button) { - links[p_node_id].const_op = p_button; -} - void VisualShaderGraphPlugin::register_expression_edit(int p_node_id, CodeEdit *p_expression_edit) { links[p_node_id].expression_edit = p_expression_edit; } @@ -322,7 +305,7 @@ void VisualShaderGraphPlugin::make_dirty(bool p_enabled) { } void VisualShaderGraphPlugin::register_link(VisualShader::Type p_type, int p_id, VisualShaderNode *p_visual_node, GraphNode *p_graph_node) { - links.insert(p_id, { p_type, p_visual_node, p_graph_node, p_visual_node->get_output_port_for_preview() != -1, -1, Map(), Map(), nullptr, nullptr, nullptr, nullptr, { nullptr, nullptr, nullptr } }); + links.insert(p_id, { p_type, p_visual_node, p_graph_node, p_visual_node->get_output_port_for_preview() != -1, -1, Map(), Map(), nullptr, nullptr, nullptr, { nullptr, nullptr, nullptr } }); } void VisualShaderGraphPlugin::register_output_port(int p_node_id, int p_port, TextureButton *p_button) { @@ -498,23 +481,6 @@ void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id) { custom_editor = hbox; } - Ref float_const = vsnode; - if (float_const.is_valid()) { - HBoxContainer *hbox = memnew(HBoxContainer); - - hbox->add_child(custom_editor); - OptionButton *btn = memnew(OptionButton); - hbox->add_child(btn); - register_constant_option_btn(p_id, btn); - btn->add_item(""); - for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) { - btn->add_item(float_constant_defs[i].name); - } - btn->select(get_constant_index(float_const->get_constant())); - btn->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_float_constant_selected), varray(p_id)); - custom_editor = hbox; - } - if (custom_editor && !vsnode->is_use_prop_slots() && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) { //will be embedded in first port } else if (custom_editor) { @@ -2934,6 +2900,7 @@ void VisualShaderEditor::_graph_gui_input(const Ref &p_event) { selected_constants.clear(); selected_uniforms.clear(); selected_comment = -1; + selected_float_constant = -1; List to_change; for (int i = 0; i < graph->get_child_count(); i++) { @@ -2953,6 +2920,10 @@ void VisualShaderEditor::_graph_gui_input(const Ref &p_event) { if (constant_node != nullptr) { selected_constants.insert(id); } + VisualShaderNodeFloatConstant *float_constant_node = Object::cast_to(node.ptr()); + if (float_constant_node != nullptr) { + selected_float_constant = id; + } VisualShaderNodeUniform *uniform_node = Object::cast_to(node.ptr()); if (uniform_node != nullptr && uniform_node->is_convertible_to_constant()) { selected_uniforms.insert(id); @@ -2963,6 +2934,7 @@ void VisualShaderEditor::_graph_gui_input(const Ref &p_event) { if (to_change.size() > 1) { selected_comment = -1; + selected_float_constant = -1; } if (to_change.is_empty() && copy_nodes_buffer.is_empty()) { @@ -2977,6 +2949,10 @@ void VisualShaderEditor::_graph_gui_input(const Ref &p_event) { if (temp != -1) { popup_menu->remove_item(temp); } + temp = popup_menu->get_item_index(NodeMenuOptions::FLOAT_CONSTANTS); + if (temp != -1) { + popup_menu->remove_item(temp); + } temp = popup_menu->get_item_index(NodeMenuOptions::CONVERT_CONSTANTS_TO_UNIFORMS); if (temp != -1) { popup_menu->remove_item(temp); @@ -2998,14 +2974,23 @@ void VisualShaderEditor::_graph_gui_input(const Ref &p_event) { popup_menu->remove_item(temp); } - if (selected_comment != -1) { - popup_menu->add_separator("", NodeMenuOptions::SEPARATOR2); - popup_menu->add_item(TTR("Set Comment Title"), NodeMenuOptions::SET_COMMENT_TITLE); - popup_menu->add_item(TTR("Set Comment Description"), NodeMenuOptions::SET_COMMENT_DESCRIPTION); - } - if (selected_constants.size() > 0 || selected_uniforms.size() > 0) { - popup_menu->add_separator("", NodeMenuOptions::SEPARATOR3); + popup_menu->add_separator("", NodeMenuOptions::SEPARATOR2); + + if (selected_float_constant != -1) { + popup_menu->add_submenu_item(TTR("Float Constants"), "FloatConstants", int(NodeMenuOptions::FLOAT_CONSTANTS)); + + if (!constants_submenu) { + constants_submenu = memnew(PopupMenu); + constants_submenu->set_name("FloatConstants"); + + for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) { + constants_submenu->add_item(float_constant_defs[i].name, i); + } + popup_menu->add_child(constants_submenu); + constants_submenu->connect("index_pressed", callable_mp(this, &VisualShaderEditor::_float_constant_selected)); + } + } if (selected_constants.size() > 0) { popup_menu->add_item(TTR("Convert Constant(s) to Uniform(s)"), NodeMenuOptions::CONVERT_CONSTANTS_TO_UNIFORMS); @@ -3016,6 +3001,12 @@ void VisualShaderEditor::_graph_gui_input(const Ref &p_event) { } } + if (selected_comment != -1) { + popup_menu->add_separator("", NodeMenuOptions::SEPARATOR3); + popup_menu->add_item(TTR("Set Comment Title"), NodeMenuOptions::SET_COMMENT_TITLE); + popup_menu->add_item(TTR("Set Comment Description"), NodeMenuOptions::SET_COMMENT_DESCRIPTION); + } + menu_point = graph->get_local_mouse_position(); Point2 gpos = Input::get_singleton()->get_mouse_position(); popup_menu->set_position(gpos); @@ -3485,27 +3476,20 @@ void VisualShaderEditor::_uniform_select_item(Ref p_ undo_redo->commit_action(); } -void VisualShaderEditor::_float_constant_selected(int p_index, int p_node) { - if (p_index == 0) { - graph_plugin->update_node_size(p_node); - return; - } - - --p_index; - - ERR_FAIL_INDEX(p_index, MAX_FLOAT_CONST_DEFS); +void VisualShaderEditor::_float_constant_selected(int p_which) { + ERR_FAIL_INDEX(p_which, MAX_FLOAT_CONST_DEFS); VisualShader::Type type = get_current_shader_type(); - Ref node = visual_shader->get_node(type, p_node); - if (!node.is_valid()) { - return; + Ref node = visual_shader->get_node(type, selected_float_constant); + ERR_FAIL_COND(!node.is_valid()); + + if (Math::is_equal_approx(node->get_constant(), float_constant_defs[p_which].value)) { + return; // same } - undo_redo->create_action(TTR("Set constant")); - undo_redo->add_do_method(node.ptr(), "set_constant", float_constant_defs[p_index].value); + undo_redo->create_action(vformat(TTR("Set Constant: %s"), float_constant_defs[p_which].name)); + undo_redo->add_do_method(node.ptr(), "set_constant", float_constant_defs[p_which].value); undo_redo->add_undo_method(node.ptr(), "set_constant", node->get_constant()); - undo_redo->add_do_method(graph_plugin.ptr(), "update_constant", type, p_node); - undo_redo->add_undo_method(graph_plugin.ptr(), "update_constant", type, p_node); undo_redo->commit_action(); } @@ -4743,9 +4727,6 @@ public: if (p_property != "constant") { undo_redo->add_do_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id); undo_redo->add_undo_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id); - } else { - undo_redo->add_do_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_constant", shader_type, node_id); - undo_redo->add_undo_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_constant", shader_type, node_id); } undo_redo->commit_action(); diff --git a/editor/plugins/visual_shader_editor_plugin.h b/editor/plugins/visual_shader_editor_plugin.h index f53726edb90..87bab16a45e 100644 --- a/editor/plugins/visual_shader_editor_plugin.h +++ b/editor/plugins/visual_shader_editor_plugin.h @@ -73,7 +73,6 @@ private: Map output_ports; VBoxContainer *preview_box = nullptr; LineEdit *uniform_name = nullptr; - OptionButton *const_op = nullptr; CodeEdit *expression_edit = nullptr; CurveEditor *curve_editors[3] = { nullptr, nullptr, nullptr }; }; @@ -95,7 +94,6 @@ public: void register_output_port(int p_id, int p_port, TextureButton *p_button); void register_uniform_name(int p_id, LineEdit *p_uniform_name); void register_default_input_button(int p_node_id, int p_port_id, Button *p_button); - void register_constant_option_btn(int p_node_id, OptionButton *p_button); void register_expression_edit(int p_node_id, CodeEdit *p_expression_edit); void register_curve_editor(int p_node_id, int p_index, CurveEditor *p_curve_editor); void clear_links(); @@ -118,7 +116,6 @@ public: void set_uniform_name(VisualShader::Type p_type, int p_node_id, const String &p_name); void update_curve(int p_node_id); void update_curve_xyz(int p_node_id); - void update_constant(VisualShader::Type p_type, int p_node_id); void set_expression(VisualShader::Type p_type, int p_node_id, const String &p_expression); int get_constant_index(float p_constant) const; void update_node_size(int p_node_id); @@ -164,6 +161,7 @@ class VisualShaderEditor : public VBoxContainer { ConfirmationDialog *members_dialog; PopupMenu *popup_menu; + PopupMenu *constants_submenu = nullptr; MenuButton *tools; PopupPanel *comment_title_change_popup = nullptr; @@ -214,6 +212,7 @@ class VisualShaderEditor : public VBoxContainer { DELETE, DUPLICATE, SEPARATOR2, // ignore + FLOAT_CONSTANTS, CONVERT_CONSTANTS_TO_UNIFORMS, CONVERT_UNIFORMS_TO_CONSTANTS, SEPARATOR3, // ignore @@ -347,6 +346,7 @@ class VisualShaderEditor : public VBoxContainer { Set selected_constants; Set selected_uniforms; int selected_comment = -1; + int selected_float_constant = -1; void _convert_constants_to_uniforms(bool p_vice_versa); void _replace_node(VisualShader::Type p_type_id, int p_node_id, const StringName &p_from, const StringName &p_to); @@ -396,7 +396,7 @@ class VisualShaderEditor : public VBoxContainer { void _input_select_item(Ref input, String name); void _uniform_select_item(Ref p_uniform, String p_name); - void _float_constant_selected(int p_index, int p_node); + void _float_constant_selected(int p_which); VisualShader::Type get_current_shader_type() const; diff --git a/scene/resources/visual_shader_nodes.cpp b/scene/resources/visual_shader_nodes.cpp index 6fd6fd8f3b9..6545a621ab1 100644 --- a/scene/resources/visual_shader_nodes.cpp +++ b/scene/resources/visual_shader_nodes.cpp @@ -38,7 +38,7 @@ VisualShaderNodeConstant::VisualShaderNodeConstant() { ////////////// Scalar(Float) String VisualShaderNodeFloatConstant::get_caption() const { - return "ScalarFloat"; + return "FloatConstant"; } int VisualShaderNodeFloatConstant::get_input_port_count() const { @@ -97,7 +97,7 @@ VisualShaderNodeFloatConstant::VisualShaderNodeFloatConstant() { ////////////// Scalar(Int) String VisualShaderNodeIntConstant::get_caption() const { - return "ScalarInt"; + return "IntConstant"; } int VisualShaderNodeIntConstant::get_input_port_count() const { @@ -156,7 +156,7 @@ VisualShaderNodeIntConstant::VisualShaderNodeIntConstant() { ////////////// Boolean String VisualShaderNodeBooleanConstant::get_caption() const { - return "Boolean"; + return "BooleanConstant"; } int VisualShaderNodeBooleanConstant::get_input_port_count() const { @@ -215,7 +215,7 @@ VisualShaderNodeBooleanConstant::VisualShaderNodeBooleanConstant() { ////////////// Color String VisualShaderNodeColorConstant::get_caption() const { - return "Color"; + return "ColorConstant"; } int VisualShaderNodeColorConstant::get_input_port_count() const { @@ -285,7 +285,7 @@ VisualShaderNodeColorConstant::VisualShaderNodeColorConstant() { ////////////// Vector String VisualShaderNodeVec3Constant::get_caption() const { - return "Vector"; + return "VectorConstant"; } int VisualShaderNodeVec3Constant::get_input_port_count() const { @@ -344,7 +344,7 @@ VisualShaderNodeVec3Constant::VisualShaderNodeVec3Constant() { ////////////// Transform3D String VisualShaderNodeTransformConstant::get_caption() const { - return "Transform3D"; + return "TransformConstant"; } int VisualShaderNodeTransformConstant::get_input_port_count() const {