summaryrefslogtreecommitdiff
path: root/scene/resources/visual_shader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/resources/visual_shader.cpp')
-rw-r--r--scene/resources/visual_shader.cpp48
1 files changed, 45 insertions, 3 deletions
diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp
index dd595d9ff8..7265c9b457 100644
--- a/scene/resources/visual_shader.cpp
+++ b/scene/resources/visual_shader.cpp
@@ -257,7 +257,7 @@ bool VisualShader::can_connect_nodes(Type p_type, int p_from_node, int p_from_po
VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
- if (MAX(0, from_port_type - 2) != (MAX(0, to_port_type - 2))) {
+ if (!is_port_types_compatible(from_port_type, to_port_type)) {
return false;
}
@@ -271,6 +271,10 @@ bool VisualShader::can_connect_nodes(Type p_type, int p_from_node, int p_from_po
return true;
}
+bool VisualShader::is_port_types_compatible(int p_a, int p_b) const {
+ return MAX(0, p_a - 2) == (MAX(0, p_b - 2));
+}
+
void VisualShader::connect_nodes_forced(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
ERR_FAIL_INDEX(p_type, TYPE_MAX);
Graph *g = &graph[p_type];
@@ -295,8 +299,8 @@ Error VisualShader::connect_nodes(Type p_type, int p_from_node, int p_from_port,
VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
- if (MAX(0, from_port_type - 2) != (MAX(0, to_port_type - 2))) {
- ERR_EXPLAIN("Incompatible port types (scalar/vec/bool with transform");
+ if (!is_port_types_compatible(from_port_type, to_port_type)) {
+ ERR_EXPLAIN("Incompatible port types (scalar/vec/bool) with transform");
ERR_FAIL_V(ERR_INVALID_PARAMETER);
return ERR_INVALID_PARAMETER;
}
@@ -1762,6 +1766,7 @@ void VisualShaderNodeGroupBase::set_inputs(const String &p_inputs) {
for (int i = 0; i < input_port_count; i++) {
Vector<String> arr = input_strings[i].split(",");
+ ERR_FAIL_COND(arr.size() != 3);
int port_idx = arr[0].to_int();
int port_type = arr[1].to_int();
@@ -1794,6 +1799,7 @@ void VisualShaderNodeGroupBase::set_outputs(const String &p_outputs) {
for (int i = 0; i < output_port_count; i++) {
Vector<String> arr = output_strings[i].split(",");
+ ERR_FAIL_COND(arr.size() != 3);
int port_idx = arr[0].to_int();
int port_type = arr[1].to_int();
@@ -1810,6 +1816,23 @@ String VisualShaderNodeGroupBase::get_outputs() const {
return outputs;
}
+bool VisualShaderNodeGroupBase::is_valid_port_name(const String &p_name) const {
+ if (!p_name.is_valid_identifier()) {
+ return false;
+ }
+ for (int i = 0; i < get_input_port_count(); i++) {
+ if (get_input_port_name(i) == p_name) {
+ return false;
+ }
+ }
+ for (int i = 0; i < get_output_port_count(); i++) {
+ if (get_output_port_name(i) == p_name) {
+ return false;
+ }
+ }
+ return true;
+}
+
void VisualShaderNodeGroupBase::add_input_port(int p_id, int p_type, const String &p_name) {
String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
@@ -1849,6 +1872,8 @@ void VisualShaderNodeGroupBase::add_input_port(int p_id, int p_type, const Strin
void VisualShaderNodeGroupBase::remove_input_port(int p_id) {
+ ERR_FAIL_COND(!has_input_port(p_id));
+
Vector<String> inputs_strings = inputs.split(";", false);
int count = 0;
int index = 0;
@@ -1917,6 +1942,8 @@ void VisualShaderNodeGroupBase::add_output_port(int p_id, int p_type, const Stri
void VisualShaderNodeGroupBase::remove_output_port(int p_id) {
+ ERR_FAIL_COND(!has_output_port(p_id));
+
Vector<String> outputs_strings = outputs.split(";", false);
int count = 0;
int index = 0;
@@ -1956,6 +1983,9 @@ void VisualShaderNodeGroupBase::clear_output_ports() {
void VisualShaderNodeGroupBase::set_input_port_type(int p_id, int p_type) {
+ ERR_FAIL_COND(!has_input_port(p_id));
+ ERR_FAIL_COND(p_type < 0 || p_type > PORT_TYPE_TRANSFORM);
+
if (input_ports[p_id].type == p_type)
return;
@@ -1986,6 +2016,9 @@ VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_input_port_ty
void VisualShaderNodeGroupBase::set_input_port_name(int p_id, const String &p_name) {
+ ERR_FAIL_COND(!has_input_port(p_id));
+ ERR_FAIL_COND(!is_valid_port_name(p_name));
+
if (input_ports[p_id].name == p_name)
return;
@@ -2016,6 +2049,9 @@ String VisualShaderNodeGroupBase::get_input_port_name(int p_id) const {
void VisualShaderNodeGroupBase::set_output_port_type(int p_id, int p_type) {
+ ERR_FAIL_COND(!has_output_port(p_id));
+ ERR_FAIL_COND(p_type < 0 || p_type > PORT_TYPE_TRANSFORM);
+
if (output_ports[p_id].type == p_type)
return;
@@ -2045,6 +2081,10 @@ VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_output_port_t
}
void VisualShaderNodeGroupBase::set_output_port_name(int p_id, const String &p_name) {
+
+ ERR_FAIL_COND(!has_output_port(p_id));
+ ERR_FAIL_COND(!is_valid_port_name(p_name));
+
if (output_ports[p_id].name == p_name)
return;
@@ -2125,6 +2165,8 @@ void VisualShaderNodeGroupBase::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_outputs", "outputs"), &VisualShaderNodeGroupBase::set_outputs);
ClassDB::bind_method(D_METHOD("get_outputs"), &VisualShaderNodeGroupBase::get_outputs);
+ ClassDB::bind_method(D_METHOD("is_valid_port_name", "name"), &VisualShaderNodeGroupBase::is_valid_port_name);
+
ClassDB::bind_method(D_METHOD("add_input_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_input_port);
ClassDB::bind_method(D_METHOD("remove_input_port", "id"), &VisualShaderNodeGroupBase::remove_input_port);
ClassDB::bind_method(D_METHOD("get_input_port_count"), &VisualShaderNodeGroupBase::get_input_port_count);