Skip to content

Commit

Permalink
Improve and streamline VisualScriptFuncNodes Call Set Get
Browse files Browse the repository at this point in the history
This PR improves and streamlines the workflow for VisualScriptFunctionNodes Call Set Get
Uniform design.
Drag in set-get from tree is now working.
port 'pass' not backported to 3.x to keep script backwards compatibility
  • Loading branch information
Gallilus committed Jul 22, 2021
1 parent 40b5731 commit af1ea18
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 66 deletions.
98 changes: 70 additions & 28 deletions modules/visual_script/visual_script_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2242,41 +2242,33 @@ void VisualScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_da
undo_redo->create_action(TTR("Add Node(s) From Tree"));
int base_id = script->get_available_id();

if (nodes.size() > 1) {
use_node = true;
}

for (int i = 0; i < nodes.size(); i++) {
NodePath np = nodes[i];
Node *node = get_node(np);
if (!node) {
continue;
}

Ref<VisualScriptNode> n;
if (use_node || nodes.size() > 1) {
for (int i = 0; i < nodes.size(); i++) {
NodePath np = nodes[i];
Node *node = get_node(np);
if (!node) {
continue;
}

if (use_node) {
Ref<VisualScriptNode> n;
Ref<VisualScriptSceneNode> scene_node;
scene_node.instance();
scene_node->set_node_path(sn->get_path_to(node));
n = scene_node;
} else {
// ! Doesn't work properly
Ref<VisualScriptFunctionCall> call;
call.instance();
call->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
call->set_base_path(sn->get_path_to(node));
call->set_base_type(node->get_class());
n = call;
method_select->select_from_instance(node, "", true, node->get_class());
selecting_method_id = base_id;
}

undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, n, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, base_id);
undo_redo->add_do_method(script.ptr(), "add_node", default_func, base_id, n, pos);
undo_redo->add_undo_method(script.ptr(), "remove_node", default_func, base_id);

base_id++;
pos += Vector2(25, 25);
base_id++;
pos += Vector2(25, 25);
}
} else {
NodePath np = nodes[0];
Node *node = get_node(np);
drop_position = pos;
drop_node = node;
drop_path = sn->get_path_to(node);
new_connect_node_select->select_from_instance(node, "", false, node->get_class());
}
undo_redo->add_do_method(this, "_update_graph");
undo_redo->add_undo_method(this, "_update_graph");
Expand Down Expand Up @@ -3485,6 +3477,11 @@ void VisualScriptEditor::_selected_connect_node(const String &p_text, const Stri

Set<int> vn;

if (drop_position != Vector2()) {
pos = drop_position;
}
drop_position = Vector2();

bool port_node_exists = true;

StringName func = _get_function_of_node(port_action_node);
Expand Down Expand Up @@ -3539,18 +3536,63 @@ void VisualScriptEditor::_selected_connect_node(const String &p_text, const Stri
if (p_category == String("method")) {
Ref<VisualScriptFunctionCall> n;
n.instance();

if (!drop_path.is_empty()) {
if (drop_path == String(".")) {
n->set_call_mode(VisualScriptFunctionCall::CALL_MODE_SELF);
} else {
n->set_call_mode(VisualScriptFunctionCall::CALL_MODE_NODE_PATH);
n->set_base_path(drop_path);
}
}
if (drop_node) {
n->set_base_type(drop_node->get_class());
if (drop_node->get_script_instance()) {
n->set_base_script(drop_node->get_script_instance()->get_script()->get_path());
}
}
vnode = n;
} else if (p_category == String("set")) {
Ref<VisualScriptPropertySet> n;
n.instance();
if (!drop_path.is_empty()) {
if (drop_path == String(".")) {
n->set_call_mode(VisualScriptPropertySet::CALL_MODE_SELF);
} else {
n->set_call_mode(VisualScriptPropertySet::CALL_MODE_NODE_PATH);
n->set_base_path(drop_path);
}
}
if (drop_node) {
n->set_base_type(drop_node->get_class());
if (drop_node->get_script_instance()) {
n->set_base_script(drop_node->get_script_instance()->get_script()->get_path());
}
}
vnode = n;
script_prop_set = n;
} else if (p_category == String("get")) {
Ref<VisualScriptPropertyGet> n;
n.instance();
n->set_property(p_text);
if (!drop_path.is_empty()) {
if (drop_path == String(".")) {
n->set_call_mode(VisualScriptPropertyGet::CALL_MODE_SELF);
} else {
n->set_call_mode(VisualScriptPropertyGet::CALL_MODE_NODE_PATH);
n->set_base_path(drop_path);
}
}
if (drop_node) {
n->set_base_type(drop_node->get_class());
if (drop_node->get_script_instance()) {
n->set_base_script(drop_node->get_script_instance()->get_script()->get_path());
}
}
vnode = n;
}
drop_path = String();
drop_node = nullptr;

if (p_category == String("action")) {
if (p_text == "VisualScriptCondition") {
Expand Down
3 changes: 3 additions & 0 deletions modules/visual_script/visual_script_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ class VisualScriptEditor : public ScriptEditorBase {

void _port_action_menu(int p_option, const StringName &p_func);

NodePath drop_path;
Node *drop_node = nullptr;
Vector2 drop_position;
void connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id);

void _selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting = true);
Expand Down
108 changes: 70 additions & 38 deletions modules/visual_script/visual_script_func_nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,25 +254,31 @@ PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) con
}

String VisualScriptFunctionCall::get_caption() const {
if (call_mode == CALL_MODE_SELF) {
return " " + String(function) + "()";
}
if (call_mode == CALL_MODE_SINGLETON) {
return String(singleton) + ":" + String(function) + "()";
} else if (call_mode == CALL_MODE_BASIC_TYPE) {
return Variant::get_type_name(basic_type) + "." + String(function) + "()";
} else if (call_mode == CALL_MODE_NODE_PATH) {
return " [" + String(base_path.simplified()) + "]." + String(function) + "()";
} else {
return " " + base_type + "." + String(function) + "()";
}
return " " + String(function) + "()";
}

String VisualScriptFunctionCall::get_text() const {
String text;

if (call_mode == CALL_MODE_BASIC_TYPE) {
text = String("On ") + Variant::get_type_name(basic_type);
} else if (call_mode == CALL_MODE_INSTANCE) {
text = String("On ") + base_type;
} else if (call_mode == CALL_MODE_NODE_PATH) {
text = "[" + String(base_path.simplified()) + "]";
} else if (call_mode == CALL_MODE_SELF) {
text = "On Self";
} else if (call_mode == CALL_MODE_SINGLETON) {
text = String(singleton) + ":" + String(function) + "()";
}

if (rpc_call_mode) {
return "RPC";
text += " RPC";
if (rpc_call_mode == RPC_UNRELIABLE || rpc_call_mode == RPC_UNRELIABLE_TO_ID) {
text += " UNREL";
}
}
return "";
return text;
}

void VisualScriptFunctionCall::set_basic_type(Variant::Type p_type) {
Expand Down Expand Up @@ -895,11 +901,11 @@ static Ref<VisualScriptNode> create_function_call_node(const String &p_name) {
//////////////////////////////////////////

int VisualScriptPropertySet::get_output_sequence_port_count() const {
return call_mode != CALL_MODE_BASIC_TYPE ? 1 : 0;
return 1;
}

bool VisualScriptPropertySet::has_input_sequence_port() const {
return call_mode != CALL_MODE_BASIC_TYPE;
return 1;
}

Node *VisualScriptPropertySet::_get_base_node() const {
Expand Down Expand Up @@ -993,21 +999,24 @@ PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const
ClassDB::get_property_list(_get_base_type(), &props, false);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
if (E->get().name == property) {
PropertyInfo pinfo = PropertyInfo(E->get().type, "value", PROPERTY_HINT_TYPE_STRING, E->get().hint_string);
String detail_prop_name = property;
if (index != StringName()) {
detail_prop_name += "." + String(index);
}
PropertyInfo pinfo = PropertyInfo(E->get().type, detail_prop_name, PROPERTY_HINT_TYPE_STRING, E->get().hint_string);
_adjust_input_index(pinfo);
return pinfo;
}
}

PropertyInfo pinfo = type_cache;
pinfo.name = "value";
_adjust_input_index(pinfo);
return pinfo;
}

PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const {
if (call_mode == CALL_MODE_BASIC_TYPE) {
return PropertyInfo(basic_type, "out");
return PropertyInfo(basic_type, "pass");
} else if (call_mode == CALL_MODE_INSTANCE) {
return PropertyInfo(Variant::OBJECT, "pass", PROPERTY_HINT_TYPE_STRING, get_base_type());
} else {
Expand All @@ -1029,17 +1038,18 @@ String VisualScriptPropertySet::get_caption() const {
}

String VisualScriptPropertySet::get_text() const {
if (!has_input_sequence_port()) {
return "";
}
if (call_mode == CALL_MODE_BASIC_TYPE) {
return String("On ") + Variant::get_type_name(basic_type);
} else if (call_mode == CALL_MODE_INSTANCE) {
return String("On ") + base_type;
} else if (call_mode == CALL_MODE_NODE_PATH) {
return " [" + String(base_path.simplified()) + "]";
} else {
return "On Self";
}

static const char *cname[3] = {
"Self",
"Scene Node",
"Instance"
};

return String("On ") + cname[call_mode];
}

void VisualScriptPropertySet::_update_base_type() {
Expand Down Expand Up @@ -1692,6 +1702,7 @@ int VisualScriptPropertyGet::get_input_value_port_count() const {
return (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0;
}
int VisualScriptPropertyGet::get_output_value_port_count() const {
// port 'pass' not backported to 3.x to keep script backwards compatibility
return 1;
}

Expand All @@ -1712,33 +1723,41 @@ PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const
}

PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const {
// port 'pass' not backported to 3.x to keep script backwards compatibility
List<PropertyInfo> props;
ClassDB::get_property_list(_get_base_type(), &props, false);
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
if (E->get().name == property) {
return PropertyInfo(E->get().type, "value." + String(index));
PropertyInfo pinfo = PropertyInfo(E->get().type, String(property) + "." + String(index), E->get().hint, E->get().hint_string);
_adjust_input_index(pinfo);
return pinfo;
}
}

return PropertyInfo(type_cache, "value");
PropertyInfo pinfo = PropertyInfo(type_cache, "value");
_adjust_input_index(pinfo);
return pinfo;
}

String VisualScriptPropertyGet::get_caption() const {
return String("Get ") + property;
String prop = String("Get ") + property;
if (index != StringName()) {
prop += "." + String(index);
}

return prop;
}

String VisualScriptPropertyGet::get_text() const {
if (call_mode == CALL_MODE_BASIC_TYPE) {
return String("On ") + Variant::get_type_name(basic_type);
} else if (call_mode == CALL_MODE_INSTANCE) {
return String("On ") + base_type;
} else if (call_mode == CALL_MODE_NODE_PATH) {
return " [" + String(base_path.simplified()) + "]";
} else {
return "On Self";
}

static const char *cname[3] = {
"Self",
"Scene Node",
"Instance"
};

return String("On ") + cname[call_mode];
}

void VisualScriptPropertyGet::set_base_type(const StringName &p_type) {
Expand Down Expand Up @@ -1916,6 +1935,18 @@ Variant::Type VisualScriptPropertyGet::_get_type_cache() const {
return type_cache;
}

void VisualScriptPropertyGet::_adjust_input_index(PropertyInfo &pinfo) const {
if (index != StringName()) {
Variant v;
Variant::CallError ce;
v = Variant::construct(pinfo.type, nullptr, 0, ce);
Variant i = v.get(index);
pinfo.type = i.get_type();
} else {
pinfo.name = String(property);
}
}

void VisualScriptPropertyGet::set_index(const StringName &p_type) {
if (index == p_type) {
return;
Expand Down Expand Up @@ -2141,6 +2172,7 @@ class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
bool valid;
Variant v = *p_inputs[0];

// port 'pass' not backported to 3.x to keep script backwards compatibility
*p_outputs[0] = v.get(property, &valid);
if (index != StringName()) {
*p_outputs[0] = p_outputs[0]->get_named(index);
Expand Down
2 changes: 2 additions & 0 deletions modules/visual_script/visual_script_func_nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ class VisualScriptPropertyGet : public VisualScriptNode {
void _set_type_cache(Variant::Type p_type);
Variant::Type _get_type_cache() const;

void _adjust_input_index(PropertyInfo &pinfo) const;

protected:
virtual void _validate_property(PropertyInfo &property) const;

Expand Down

0 comments on commit af1ea18

Please sign in to comment.