Skip to content

Commit

Permalink
GH-593 Fix collapse to function bugs
Browse files Browse the repository at this point in the history
- Pins should retain type
- Multiple pins should not have the same name
- Optional return node
- Script function call nodes use argument namse
  • Loading branch information
Naros committed Jul 27, 2024
1 parent 5317029 commit ef26766
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/editor/script_editor_viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void OrchestratorScriptEditorViewport::_collapse_selected_to_function(Orchestrat
ERR_FAIL_COND_EDMSG(connections.outputs.size() > 2, "Cannot output more than one execution and one data pin.");

const String new_function_name = NameUtils::create_unique_name("NewFunction", _orchestration->get_function_names());
Ref<OScriptFunction> function = _create_new_function(new_function_name, true);
Ref<OScriptFunction> function = _create_new_function(new_function_name, !connections.outputs.is_empty());
if (!function.is_valid())
return;

Expand Down Expand Up @@ -328,8 +328,20 @@ void OrchestratorScriptEditorViewport::_collapse_selected_to_function(Orchestrat
{
const size_t size = function->get_argument_count() + 1;
function->resize_argument_list(size);
function->set_argument_name(size - 1, target_pin->get_pin_name());
function->set_argument_type(size - 1, target_pin->get_type());

PropertyInfo property = target_pin->get_property_info();
if (!target_pin->get_label().is_empty() && property.name != target_pin->get_label())
property.name = target_pin->get_label();

PackedStringArray names;
for (const PropertyInfo& argument : function->get_method_info().arguments)
if (!names.has(argument.name))
names.push_back(argument.name);

if (names.has(property.name))
property.name = NameUtils::create_unique_name(property.name, names);

function->set_argument(size - 1, property);

// Wire entry data output to this connection
target_graph->link(entry->get_id(), input_index++, E.to_node, E.to_port);
Expand Down
9 changes: 9 additions & 0 deletions src/script/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ void OScriptFunction::set_argument_type(size_t p_index, Variant::Type p_type)
}
}

void OScriptFunction::set_argument(size_t p_index, const PropertyInfo& p_property)
{
if (_method.arguments.size() > p_index && _user_defined)
{
_method.arguments[p_index] = p_property;
emit_changed();
}
}

void OScriptFunction::set_arguments(const TypedArray<Dictionary>& p_arguments)
{
if (_user_defined)
Expand Down
5 changes: 5 additions & 0 deletions src/script/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ class OScriptFunction : public Resource
/// @param p_type the new argument type
void set_argument_type(size_t p_index, Variant::Type p_type);

/// Sets the argument property details
/// @param p_index the argument index
/// @param p_property the argument details
void set_argument(size_t p_index, const PropertyInfo& p_property);

/// Sets the arguments for this function
/// @param p_arguments the argument list
void set_arguments(const TypedArray<Dictionary>& p_arguments);
Expand Down
2 changes: 1 addition & 1 deletion src/script/nodes/functions/call_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void OScriptNodeCallFunction::_create_pins_for_method(const MethodInfo& p_method
if (pin.is_valid())
{
const String arg_class_name = pin->get_property_info().class_name;
if (!arg_class_name.is_empty())
if (!arg_class_name.is_empty() && _use_argument_class_name())
{
if (arg_class_name.contains("."))
pin->set_label(arg_class_name.substr(arg_class_name.find(".") + 1));
Expand Down
4 changes: 4 additions & 0 deletions src/script/nodes/functions/call_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class OScriptNodeCallFunction : public OScriptNode
/// @return true if the MethodInfo is saved as part of the node's data, false if it isn't
virtual bool _is_method_info_serialized() const { return true; }

/// Specifies whether arguments that are class types should be labeled by class names
/// @return true to use class names, return false to use pin label or name
virtual bool _use_argument_class_name() const { return true; }

/// Return whether the return value pin should be labeled
/// @param p_pin the return value pin
/// @return true if the pin is labeled, false otherwise
Expand Down
1 change: 1 addition & 0 deletions src/script/nodes/functions/call_script_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class OScriptNodeCallScriptFunction : public OScriptNodeCallFunction
//~ Begin OScriptNodeCallFunction Interface
bool _is_method_info_serialized() const override { return false; }
bool _has_execution_pins(const MethodInfo& p_method) const override { return true; }
bool _use_argument_class_name() const override { return false; }
MethodInfo get_method_info() override { return _function->get_method_info(); }
int get_argument_count() const override { return (int) _function->get_argument_count(); }
//~ End OScriptNodeCallFunction Interface
Expand Down

0 comments on commit ef26766

Please sign in to comment.