// Copyright Epic Games, Inc. All Rights Reserved. #include "RigVMModel/RigVMNode.h" #include "RigVMModel/RigVMGraph.h" #include "RigVMCore/RigVMExecuteContext.h" #include "RigVMCore/RigVMStruct.h" const FString URigVMNode::NodeColorName = TEXT("NodeColor"); URigVMNode::URigVMNode() : UObject() , Position(FVector2D::ZeroVector) , Size(FVector2D::ZeroVector) , NodeColor(FLinearColor::Black) , InstructionIndex(INDEX_NONE) , BlockIndex(INDEX_NONE) , GetSliceContextBracket(0) { } URigVMNode::~URigVMNode() { } FString URigVMNode::GetNodePath() const { return GetName(); } int32 URigVMNode::GetNodeIndex() const { int32 Index = INDEX_NONE; URigVMGraph* Graph = GetGraph(); if (Graph != nullptr) { Graph->GetNodes().Find((URigVMNode*)this, Index); } return Index; } int32 URigVMNode::GetInstructionIndex() const { return InstructionIndex; } int32 URigVMNode::GetBlockIndex() const { return BlockIndex; } const TArray& URigVMNode::GetPins() const { return Pins; } TArray URigVMNode::GetAllPinsRecursively() const { struct Local { static void VisitPinRecursively(URigVMPin* InPin, TArray& OutPins) { OutPins.Add(InPin); for (URigVMPin* SubPin : InPin->GetSubPins()) { VisitPinRecursively(SubPin, OutPins); } } }; TArray Result; for (URigVMPin* Pin : GetPins()) { Local::VisitPinRecursively(Pin, Result); } return Result; } URigVMPin* URigVMNode::FindPin(const FString& InPinPath) const { FString Left, Right; if (!URigVMPin::SplitPinPathAtStart(InPinPath, Left, Right)) { Left = InPinPath; } for (URigVMPin* Pin : GetPins()) { if (Pin->GetName() == Left) { if (Right.IsEmpty()) { return Pin; } return Pin->FindSubPin(Right); } } return nullptr; } URigVMGraph* URigVMNode::GetGraph() const { if (URigVMGraph* Graph = Cast(GetOuter())) { return Graph; } if (URigVMInjectionInfo* InjectionInfo = GetInjectionInfo()) { return InjectionInfo->GetGraph(); } return nullptr; } URigVMInjectionInfo* URigVMNode::GetInjectionInfo() const { return Cast(GetOuter()); } FString URigVMNode::GetNodeTitle() const { if (!NodeTitle.IsEmpty()) { return NodeTitle; } return GetName(); } FVector2D URigVMNode::GetPosition() const { return Position; } FVector2D URigVMNode::GetSize() const { return Size; } FLinearColor URigVMNode::GetNodeColor() const { return NodeColor; } FText URigVMNode::GetToolTipText() const { return FText::FromName(GetFName()); } FText URigVMNode::GetToolTipTextForPin(const URigVMPin* InPin) const { return FText::FromName(InPin->GetFName()); } bool URigVMNode::IsSelected() const { URigVMGraph* Graph = GetGraph(); if (Graph) { return Graph->IsNodeSelected(GetFName()); } return false; } bool URigVMNode::IsInjected() const { return Cast(GetOuter()) != nullptr; } bool URigVMNode::IsVisibleInUI() const { return !IsInjected(); } bool URigVMNode::IsPure() const { if(IsMutable()) { return false; } for (URigVMPin* Pin : GetPins()) { if(Pin->GetDirection() == ERigVMPinDirection::Hidden) { return false; } } return true; } bool URigVMNode::IsMutable() const { URigVMPin* ExecutePin = FindPin(FRigVMStruct::ExecuteContextName.ToString()); if (ExecutePin) { if (ExecutePin->GetScriptStruct()->IsChildOf(FRigVMExecuteContext::StaticStruct())) { return true; } } return false; } bool URigVMNode::IsEvent() const { return IsMutable() && !HasInputPin(true /* include io */) && !GetEventName().IsNone(); } FName URigVMNode::GetEventName() const { return NAME_None; } bool URigVMNode::HasInputPin(bool bIncludeIO) const { if (HasPinOfDirection(ERigVMPinDirection::Input)) { return true; } if (bIncludeIO) { return HasPinOfDirection(ERigVMPinDirection::IO); } return false; } bool URigVMNode::HasIOPin() const { return HasPinOfDirection(ERigVMPinDirection::IO); } bool URigVMNode::HasOutputPin(bool bIncludeIO) const { if (HasPinOfDirection(ERigVMPinDirection::Output)) { return true; } if (bIncludeIO) { return HasPinOfDirection(ERigVMPinDirection::IO); } return false; } bool URigVMNode::HasPinOfDirection(ERigVMPinDirection InDirection) const { for (URigVMPin* Pin : GetPins()) { if (Pin->GetDirection() == InDirection) { return true; } } return false; } bool URigVMNode::IsLinkedTo(URigVMNode* InNode) const { if (InNode == nullptr) { return false; } if (InNode == this) { return false; } if (GetGraph() != InNode->GetGraph()) { return false; } for (URigVMPin* Pin : GetPins()) { if (IsLinkedToRecursive(Pin, InNode)) { return true; } } return false; } bool URigVMNode::IsLinkedToRecursive(URigVMPin* InPin, URigVMNode* InNode) const { for (URigVMPin* LinkedPin : InPin->GetLinkedSourcePins()) { if (LinkedPin->GetNode() == InNode) { return true; } } for (URigVMPin* LinkedPin : InPin->GetLinkedTargetPins()) { if (LinkedPin->GetNode() == InNode) { return true; } } for (URigVMPin* SubPin : InPin->GetSubPins()) { if (IsLinkedToRecursive(SubPin, InNode)) { return true; } } return false; } TArray URigVMNode::GetLinks() const { TArray Links; struct Local { static void Traverse(URigVMPin* InPin, TArray& Links) { Links.Append(InPin->GetLinks()); for (URigVMPin* SubPin : InPin->GetSubPins()) { Local::Traverse(SubPin, Links); } } }; for (URigVMPin* Pin : GetPins()) { Local::Traverse(Pin, Links); } return Links; } TArray URigVMNode::GetLinkedSourceNodes() const { TArray Nodes; for (URigVMPin* Pin : GetPins()) { GetLinkedNodesRecursive(Pin, true, Nodes); } return Nodes; } TArray URigVMNode::GetLinkedTargetNodes() const { TArray Nodes; for (URigVMPin* Pin : GetPins()) { GetLinkedNodesRecursive(Pin, false, Nodes); } return Nodes; } void URigVMNode::GetLinkedNodesRecursive(URigVMPin* InPin, bool bLookForSources, TArray& OutNodes) const { TArray LinkedPins = bLookForSources ? InPin->GetLinkedSourcePins() : InPin->GetLinkedTargetPins(); for (URigVMPin* LinkedPin : LinkedPins) { OutNodes.AddUnique(LinkedPin->GetNode()); } for (URigVMPin* SubPin : InPin->GetSubPins()) { GetLinkedNodesRecursive(SubPin, bLookForSources, OutNodes); } } FName URigVMNode::GetSliceContextForPin(URigVMPin* InRootPin, const FRigVMUserDataArray& InUserData) { return NAME_None; } int32 URigVMNode::GetNumSlices(const FRigVMUserDataArray& InUserData) { return GetNumSlicesForContext(NAME_None, InUserData); } int32 URigVMNode::GetNumSlicesForContext(const FName& InContextName, const FRigVMUserDataArray& InUserData) { for (URigVMPin* RootPin : GetPins()) { if (RootPin->GetFName() == InContextName) { return RootPin->GetNumSlices(InUserData); } } int32 MaxSlices = 1; if (GetSliceContextBracket == 0) { TGuardValue ReentrantGuard(GetSliceContextBracket, GetSliceContextBracket + 1); for (URigVMPin* Pin : GetPins()) { TArray SourcePins = Pin->GetLinkedSourcePins(true /* recursive */); if (SourcePins.Num() > 0) { for (URigVMPin* SourcePin : SourcePins) { int32 NumSlices = SourcePin->GetNumSlices(InUserData); MaxSlices = FMath::Max(NumSlices, MaxSlices); } } } } return MaxSlices; }