Merge pull request #50114 from Chaosus/vs_constants

Few improvements for constants in visual shader
This commit is contained in:
Rémi Verschelde 2021-08-13 14:45:13 +02:00 committed by GitHub
commit 24ec20414a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 71 deletions

View File

@ -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<VisualShaderNodeFloatConstant>(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<int, InputPort>(), Map<int, Port>(), 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<int, InputPort>(), Map<int, Port>(), 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<VisualShaderNodeFloatConstant> 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) {
@ -2954,6 +2920,7 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
selected_constants.clear();
selected_uniforms.clear();
selected_comment = -1;
selected_float_constant = -1;
List<int> to_change;
for (int i = 0; i < graph->get_child_count(); i++) {
@ -2973,6 +2940,10 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
if (constant_node != nullptr) {
selected_constants.insert(id);
}
VisualShaderNodeFloatConstant *float_constant_node = Object::cast_to<VisualShaderNodeFloatConstant>(node.ptr());
if (float_constant_node != nullptr) {
selected_float_constant = id;
}
VisualShaderNodeUniform *uniform_node = Object::cast_to<VisualShaderNodeUniform>(node.ptr());
if (uniform_node != nullptr && uniform_node->is_convertible_to_constant()) {
selected_uniforms.insert(id);
@ -2983,6 +2954,7 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
if (to_change.size() > 1) {
selected_comment = -1;
selected_float_constant = -1;
}
if (to_change.is_empty() && copy_nodes_buffer.is_empty()) {
@ -2997,6 +2969,10 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &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);
@ -3018,14 +2994,23 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &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);
@ -3036,6 +3021,12 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &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);
@ -3505,27 +3496,20 @@ void VisualShaderEditor::_uniform_select_item(Ref<VisualShaderNodeUniformRef> 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<VisualShaderNodeFloatConstant> node = visual_shader->get_node(type, p_node);
if (!node.is_valid()) {
return;
Ref<VisualShaderNodeFloatConstant> 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();
}
@ -4768,9 +4752,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();

View File

@ -73,7 +73,6 @@ private:
Map<int, Port> 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<int> selected_constants;
Set<int> 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<VisualShaderNodeInput> input, String name);
void _uniform_select_item(Ref<VisualShaderNodeUniformRef> 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;

View File

@ -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 {