Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-52 Work with scene node's script properties & methods #63

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/editor/graph/actions/action_menu_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ struct OrchestratorGraphActionFilter
/// then the action is filtered out.
Vector<StringName> target_classes;

/// The target object to expressly get context details about
Object* target_object;
};

#endif // ORCHESTRATOR_GRAPH_ACTION_MENU_ITEM_FILTER_H
28 changes: 24 additions & 4 deletions src/editor/graph/actions/default_action_registrar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "common/variant_utils.h"
#include "common/string_utils.h"
#include "editor/graph/graph_edit.h"
#include "editor/graph/graph_node_pin.h"
#include "editor/graph/graph_node_spawner.h"
#include "script/nodes/script_nodes.h"

Expand Down Expand Up @@ -337,7 +338,11 @@ void OrchestratorDefaultGraphActionRegistrar::_register_graph_items(const Orches
return;

PackedStringArray classes;
if (!p_context.filter->target_classes.is_empty())
if (p_context.filter->target_object)
{
classes.push_back(p_context.filter->target_object->get_class());
}
else if (!p_context.filter->target_classes.is_empty())
{
for (String target_class : p_context.filter->target_classes)
{
Expand Down Expand Up @@ -366,7 +371,12 @@ void OrchestratorDefaultGraphActionRegistrar::_register_graph_items(const Orches
void OrchestratorDefaultGraphActionRegistrar::_register_class_properties(const OrchestratorGraphActionRegistrarContext& p_context,
const StringName& p_class_name)
{
TypedArray<Dictionary> properties = ClassDB::class_get_property_list(p_class_name, true);
TypedArray<Dictionary> properties;
if (p_context.filter->target_object)
properties = p_context.filter->target_object->get_property_list();
else
properties = ClassDB::class_get_property_list(p_class_name, true);

for (int i = 0; i < properties.size(); i++)
{
const PropertyInfo pi = DictionaryUtils::to_property(properties[i]);
Expand Down Expand Up @@ -400,7 +410,12 @@ void OrchestratorDefaultGraphActionRegistrar::_register_class_properties(const O
void OrchestratorDefaultGraphActionRegistrar::_register_class_methods(const OrchestratorGraphActionRegistrarContext& p_context,
const StringName& p_class_name)
{
TypedArray<Dictionary> methods = ClassDB::class_get_method_list(p_class_name, true);
TypedArray<Dictionary> methods;
if (p_context.filter->target_object)
methods = p_context.filter->target_object->get_method_list();
else
methods = ClassDB::class_get_method_list(p_class_name, true);

for (int i = 0; i < methods.size(); i++)
{
const MethodInfo mi = DictionaryUtils::to_method(methods[i]);
Expand Down Expand Up @@ -428,7 +443,12 @@ void OrchestratorDefaultGraphActionRegistrar::_register_class_methods(const Orch
void OrchestratorDefaultGraphActionRegistrar::_register_class_signals(const OrchestratorGraphActionRegistrarContext& p_context,
const StringName& p_class_name)
{
TypedArray<Dictionary> signals = ClassDB::class_get_signal_list(p_class_name, true);
TypedArray<Dictionary> signals;
if (p_context.filter->target_object)
signals = p_context.filter->target_object->get_signal_list();
else
signals = ClassDB::class_get_signal_list(p_class_name, true);

for (int i = 0; i < signals.size(); i++)
{
const MethodInfo si = DictionaryUtils::to_method(signals[i]);
Expand Down
16 changes: 15 additions & 1 deletion src/editor/graph/graph_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,13 @@ void OrchestratorGraphEdit::_on_attempt_connection_from_empty(const StringName&
filter.context.pins.push_back(pin);

ResolvedType resolved_type = pin->resolve_type();
if (resolved_type.object)
{
// When a resolved object isp provided, it takes precedence
filter.target_classes.push_back(resolved_type.object->get_class());
filter.target_object = resolved_type.object;
filter.context.pins.clear();
}
if (resolved_type.is_class_type())
{
filter.target_classes.push_back(resolved_type.class_name);
Expand All @@ -757,7 +764,14 @@ void OrchestratorGraphEdit::_on_attempt_connection_to_empty(const StringName& p_
filter.context.pins.push_back(pin);

ResolvedType resolved_type = pin->resolve_type();
if (resolved_type.is_class_type())
if (resolved_type.object)
{
// When a resolved object is provided, it takes precedence
filter.target_classes.push_back(resolved_type.object->get_class());
filter.target_object = resolved_type.object;
filter.context.pins.clear();
}
else if (resolved_type.is_class_type())
{
filter.target_classes.push_back(resolved_type.class_name);
filter.context.pins.clear();
Expand Down
3 changes: 3 additions & 0 deletions src/editor/graph/graph_node_pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ ResolvedType OrchestratorGraphNodePin::resolve_type() const
else
resolved_type.class_name = "Object";

// Primarily used by SceneNode to get scene attributes about the target
resolved_type.object = _pin->resolve_target();

return resolved_type;
}

Expand Down
1 change: 1 addition & 0 deletions src/editor/graph/graph_node_pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct ResolvedType
{
Variant::Type type{ Variant::NIL };
StringName class_name;
Object* object;

_FORCE_INLINE_ bool is_non_object_type() const { return type != Variant::NIL && type != Variant::OBJECT; }
_FORCE_INLINE_ bool is_class_type() const { return !class_name.is_empty(); }
Expand Down
5 changes: 5 additions & 0 deletions src/script/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ class OScriptNode : public Resource
/// @return the resolved class type
virtual StringName resolve_type_class(const Ref<OScriptNodePin>& p_pin) const { return ""; }

/// Resolves the target object of the specified pin
/// @param p_pin the pin
/// @return the resolved target object
virtual Object* resolve_target(const Ref<OScriptNodePin>& p_pin) const { return nullptr; }

/// Create a pin associated with this node.
/// @param p_direction the pin direction, input or output
/// @param p_name the pin name
Expand Down
5 changes: 5 additions & 0 deletions src/script/node_pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,9 @@ bool OScriptNodePin::has_any_connections() const
Vector<Ref<OScriptNodePin>> OScriptNodePin::get_connections() const
{
return get_owning_node()->get_owning_script()->get_connections(this);
}

Object* OScriptNodePin::resolve_target()
{
return get_owning_node()->resolve_target(this);
}
4 changes: 4 additions & 0 deletions src/script/node_pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ class OScriptNodePin : public Resource
/// Return whether this pin acts as an execution pin.
/// @return true if the pin is a control flow, execution pin, false otherwise
_FORCE_INLINE_ bool is_execution() const { return get_flags().has_flag(OScriptNodePin::Flags::EXECUTION); }

/// Attempts to resolve the target object of this pin.
/// @return the target object of the pin or {@code nullptr} if there is no target.
Object* resolve_target();
};

VARIANT_BITFIELD_CAST(OScriptNodePin::Flags)
Expand Down
15 changes: 15 additions & 0 deletions src/script/nodes/scene/scene_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ String OScriptNodeSceneNode::get_icon() const
return "NodeInfo";
}

Object* OScriptNodeSceneNode::resolve_target(const Ref<OScriptNodePin>& p_pin) const
{
if (_is_in_editor() && p_pin.is_valid() && p_pin->is_output() && !p_pin->is_execution())
{
SceneTree* st = Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop());
if (st && st->get_edited_scene_root())
{
Node* root = st->get_edited_scene_root();
if (root)
return root->get_node_or_null(_node_path);
}
}
return super::resolve_target(p_pin);
}

OScriptNodeInstance* OScriptNodeSceneNode::instantiate(OScriptInstance* p_instance)
{
OScriptNodeSceneNodeInstance* i = memnew(OScriptNodeSceneNodeInstance);
Expand Down
1 change: 1 addition & 0 deletions src/script/nodes/scene/scene_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class OScriptNodeSceneNode : public OScriptNode
String get_node_title() const override;
String get_node_title_color_name() const override { return "scene"; }
String get_icon() const override;
Object* resolve_target(const Ref<OScriptNodePin>& p_pin) const override;
OScriptNodeInstance* instantiate(OScriptInstance* p_instance) override;
void initialize(const OScriptNodeInitContext& p_context) override;
//~ End OScriptNode Interface
Expand Down
Loading