[3.2] Prevents default values of VSNodeCustom from overriding by a script

This commit is contained in:
Yuri Roubinsky 2021-04-12 13:29:44 +03:00
parent acb92783c4
commit ac91e2ca0d
3 changed files with 54 additions and 5 deletions

View file

@ -552,6 +552,11 @@ void VisualShaderEditor::_update_graph() {
bool is_group = !group_node.is_null();
Size2 size = Size2(0, 0);
VisualShaderNodeCustom *custom = Object::cast_to<VisualShaderNodeCustom>(vsnode.ptr());
if (custom) {
custom->_set_initialized(true);
}
Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(group_node.ptr());
bool is_expression = !expression_node.is_null();
String expression = "";
@ -1293,10 +1298,15 @@ void VisualShaderEditor::_port_edited() {
Variant value = property_editor->get_variant();
Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
ERR_FAIL_COND(!vsn.is_valid());
undo_redo->create_action(TTR("Set Input Default Port"));
undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
Ref<VisualShaderNodeCustom> custom = Object::cast_to<VisualShaderNodeCustom>(vsn.ptr());
if (custom.is_valid()) {
undo_redo->add_do_method(custom.ptr(), "_set_input_port_default_value", editing_port, value);
undo_redo->add_undo_method(custom.ptr(), "_set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
} else {
undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
}
undo_redo->add_do_method(this, "_update_graph");
undo_redo->add_undo_method(this, "_update_graph");
undo_redo->commit_action();

View file

@ -295,6 +295,30 @@ String VisualShaderNodeCustom::generate_global_per_node(Shader::Mode p_mode, Vis
return "";
}
void VisualShaderNodeCustom::set_input_port_default_value(int p_port, const Variant &p_value) {
if (!is_initialized) {
VisualShaderNode::set_input_port_default_value(p_port, p_value);
}
}
void VisualShaderNodeCustom::set_default_input_values(const Array &p_values) {
if (!is_initialized) {
VisualShaderNode::set_default_input_values(p_values);
}
}
void VisualShaderNodeCustom::_set_input_port_default_value(int p_port, const Variant &p_value) {
VisualShaderNode::set_input_port_default_value(p_port, p_value);
}
bool VisualShaderNodeCustom::_is_initialized() {
return is_initialized;
}
void VisualShaderNodeCustom::_set_initialized(bool p_enabled) {
is_initialized = p_enabled;
}
void VisualShaderNodeCustom::_bind_methods() {
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_name"));
@ -310,6 +334,12 @@ void VisualShaderNodeCustom::_bind_methods() {
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_output_port_name", PropertyInfo(Variant::INT, "port")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_code", PropertyInfo(Variant::ARRAY, "input_vars"), PropertyInfo(Variant::ARRAY, "output_vars"), PropertyInfo(Variant::INT, "mode"), PropertyInfo(Variant::INT, "type")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_global_code", PropertyInfo(Variant::INT, "mode")));
ClassDB::bind_method(D_METHOD("_set_initialized", "enabled"), &VisualShaderNodeCustom::_set_initialized);
ClassDB::bind_method(D_METHOD("_is_initialized"), &VisualShaderNodeCustom::_is_initialized);
ClassDB::bind_method(D_METHOD("_set_input_port_default_value", "port", "value"), &VisualShaderNodeCustom::_set_input_port_default_value);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_initialized", "_is_initialized");
}
VisualShaderNodeCustom::VisualShaderNodeCustom() {

View file

@ -199,10 +199,10 @@ public:
virtual PortType get_input_port_type(int p_port) const = 0;
virtual String get_input_port_name(int p_port) const = 0;
void set_input_port_default_value(int p_port, const Variant &p_value);
virtual void set_input_port_default_value(int p_port, const Variant &p_value);
Variant get_input_port_default_value(int p_port) const; // if NIL (default if node does not set anything) is returned, it means no default value is wanted if disconnected, thus no input var must be supplied (empty string will be supplied)
Array get_default_input_values() const;
void set_default_input_values(const Array &p_values);
virtual void set_default_input_values(const Array &p_values);
virtual int get_output_port_count() const = 0;
virtual PortType get_output_port_type(int p_port) const = 0;
@ -246,6 +246,7 @@ class VisualShaderNodeCustom : public VisualShaderNode {
int type;
};
bool is_initialized = false;
List<Port> input_ports;
List<Port> output_ports;
@ -262,7 +263,12 @@ protected:
virtual PortType get_output_port_type(int p_port) const;
virtual String get_output_port_name(int p_port) const;
virtual void set_input_port_default_value(int p_port, const Variant &p_value);
virtual void set_default_input_values(const Array &p_values);
protected:
void _set_input_port_default_value(int p_port, const Variant &p_value);
virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const;
virtual String generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const;
@ -271,6 +277,9 @@ protected:
public:
VisualShaderNodeCustom();
void update_ports();
bool _is_initialized();
void _set_initialized(bool p_enabled);
};
/////