Files
UnrealEngineUWP/Engine/Source/Editor/BlueprintGraph/Private/K2Node.cpp
Maciej Mroz 8ee2a47122 UE-14582 Many warnings about bSelfContext == true, but no scope supplied opening QA-Blueprints
#codereview Mike.Beach, Marc.Audy

[CL 2529278 by Maciej Mroz in Main branch]
2015-04-28 17:43:29 -04:00

1133 lines
35 KiB
C++

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "BlueprintGraphPrivatePCH.h"
#include "Engine/Breakpoint.h"
#include "K2Node.h"
#include "KismetDebugUtilities.h" // for HasDebuggingData(), GetWatchText()
#include "KismetCompiler.h"
#include "GraphEditorSettings.h"
#define LOCTEXT_NAMESPACE "K2Node"
// File-Scoped Globals
static const uint32 MaxArrayPinTooltipLineCount = 10;
/////////////////////////////////////////////////////
// UK2Node
UK2Node::UK2Node(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
void UK2Node::PostLoad()
{
Super::PostLoad();
// fix up pin default values
FixupPinDefaultValues();
}
void UK2Node::FixupPinDefaultValues()
{
const int32 LinkerUE4Version = GetLinkerUE4Version();
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
// Swap "new" default error tolerance value with zero on vector/rotator equality nodes, in order to preserve current behavior in existing blueprints.
if(LinkerUE4Version < VER_UE4_BP_MATH_VECTOR_EQUALITY_USES_EPSILON)
{
static const FString VectorsEqualFunctionEpsilonPinName = TEXT("KismetMathLibrary.EqualEqual_VectorVector.ErrorTolerance");
static const FString VectorsNotEqualFunctionEpsilonPinName = TEXT("KismetMathLibrary.NotEqual_VectorVector.ErrorTolerance");
static const FString RotatorsEqualFunctionEpsilonPinName = TEXT("KismetMathLibrary.EqualEqual_RotatorRotator.ErrorTolerance");
static const FString RotatorsNotEqualFunctionEpsilonPinName = TEXT("KismetMathLibrary.NotEqual_RotatorRotator.ErrorTolerance");
bool bFoundPin = false;
for(int32 i = 0; i < Pins.Num() && !bFoundPin; ++i)
{
UEdGraphPin* Pin = Pins[i];
check(Pin);
TArray<FString> RedirectPinNames;
GetRedirectPinNames(*Pin, RedirectPinNames);
for(auto PinNameIter = RedirectPinNames.CreateConstIterator(); PinNameIter && !bFoundPin; ++PinNameIter)
{
if((Pin->DefaultValue == Pin->AutogeneratedDefaultValue)
&& (*PinNameIter == VectorsEqualFunctionEpsilonPinName
|| *PinNameIter == VectorsNotEqualFunctionEpsilonPinName
|| *PinNameIter == RotatorsEqualFunctionEpsilonPinName
|| *PinNameIter == RotatorsNotEqualFunctionEpsilonPinName))
{
bFoundPin = true;
K2Schema->TrySetDefaultValue(*Pin, TEXT("0.0"));
}
}
}
}
}
FText UK2Node::GetToolTipHeading() const
{
FText Heading = FText::GetEmpty();
if (UBreakpoint* ExistingBreakpoint = FKismetDebugUtilities::FindBreakpointForNode(GetBlueprint(), this))
{
if (ExistingBreakpoint->IsEnabled())
{
Heading = LOCTEXT("EnabledBreakpoint", "Active Breakpoint");
FText ActiveBreakpointToolTipText = GetActiveBreakpointToolTipText();
if (!ActiveBreakpointToolTipText.IsEmpty())
{
Heading = FText::Format(FText::FromString("{0} - {1}"), Heading, ActiveBreakpointToolTipText);
}
}
else
{
Heading = LOCTEXT("DisabledBreakpoint", "Disabled Breakpoint");
}
}
return Heading;
}
FText UK2Node::GetActiveBreakpointToolTipText() const
{
return LOCTEXT("ActiveBreakpointToolTip", "Execution will break at this location.");
}
bool UK2Node::CreatePinsForFunctionEntryExit(const UFunction* Function, bool bForFunctionEntry)
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
// Create the inputs and outputs
bool bAllPinsGood = true;
for (TFieldIterator<UProperty> PropIt(Function); PropIt && (PropIt->PropertyFlags & CPF_Parm); ++PropIt)
{
UProperty* Param = *PropIt;
const bool bIsFunctionInput = !Param->HasAnyPropertyFlags(CPF_OutParm) || Param->HasAnyPropertyFlags(CPF_ReferenceParm);
if (bIsFunctionInput == bForFunctionEntry)
{
const EEdGraphPinDirection Direction = bForFunctionEntry ? EGPD_Output : EGPD_Input;
UEdGraphPin* Pin = CreatePin(Direction, TEXT(""), TEXT(""), NULL, false, false, Param->GetName());
const bool bPinGood = K2Schema->ConvertPropertyToPinType(Param, /*out*/ Pin->PinType);
K2Schema->SetPinDefaultValueBasedOnType(Pin);
UK2Node_CallFunction::GeneratePinTooltipFromFunction(*Pin, Function);
bAllPinsGood = bAllPinsGood && bPinGood;
}
}
return bAllPinsGood;
}
void UK2Node::AutowireNewNode(UEdGraphPin* FromPin)
{
const UEdGraphSchema_K2* K2Schema = CastChecked<UEdGraphSchema_K2>(GetSchema());
// Do some auto-connection
if (FromPin != NULL)
{
TSet<UEdGraphNode*> NodeList;
// sometimes we don't always find an ideal connection, but we want to exhaust
// all our options first... this stores a secondary less-ideal pin to connect to, if nothing better was found
UEdGraphPin* BackupConnection = NULL;
// If not dragging an exec pin, auto-connect from dragged pin to first compatible pin on the new node
for (int32 i=0; i<Pins.Num(); i++)
{
UEdGraphPin* Pin = Pins[i];
check(Pin);
ECanCreateConnectionResponse ConnectResponse = K2Schema->CanCreateConnection(FromPin, Pin).Response;
if (ConnectResponse == ECanCreateConnectionResponse::CONNECT_RESPONSE_MAKE)
{
if (K2Schema->TryCreateConnection(FromPin, Pin))
{
NodeList.Add(FromPin->GetOwningNode());
NodeList.Add(this);
}
// null out the backup connection (so we don't attempt to make it
// once we exit the loop... we successfully made this connection!)
BackupConnection = NULL;
break;
}
else if((FromPin->PinType.PinCategory == K2Schema->PC_Exec) && (ConnectResponse == ECanCreateConnectionResponse::CONNECT_RESPONSE_BREAK_OTHERS_A))
{
InsertNewNode(FromPin, Pin, NodeList);
// null out the backup connection (so we don't attempt to make it
// once we exit the loop... we successfully made this connection!)
BackupConnection = NULL;
break;
}
else if ((BackupConnection == NULL) && (ConnectResponse == ECanCreateConnectionResponse::CONNECT_RESPONSE_MAKE_WITH_CONVERSION_NODE))
{
// save this off, in-case we don't make any connection at all
BackupConnection = Pin;
}
}
// if we didn't find an ideal connection, then lets connect this pin to
// the BackupConnection (something, like a connection that requires a conversion node, etc.)
if ((BackupConnection != NULL) && K2Schema->TryCreateConnection(FromPin, BackupConnection))
{
NodeList.Add(FromPin->GetOwningNode());
NodeList.Add(this);
}
// If we were not dragging an exec pin, but it was an output pin, try and connect the Then and Execute pins
if ((FromPin->PinType.PinCategory != K2Schema->PC_Exec && FromPin->Direction == EGPD_Output))
{
UEdGraphNode* FromPinNode = FromPin->GetOwningNode();
UEdGraphPin* FromThenPin = FromPinNode->FindPin(K2Schema->PN_Then);
UEdGraphPin* ToExecutePin = FindPin(K2Schema->PN_Execute);
if ((FromThenPin != NULL) && (FromThenPin->LinkedTo.Num() == 0) && (ToExecutePin != NULL) && K2Schema->ArePinsCompatible(FromThenPin, ToExecutePin, NULL))
{
if (K2Schema->TryCreateConnection(FromThenPin, ToExecutePin))
{
NodeList.Add(FromPinNode);
NodeList.Add(this);
}
}
}
// Send all nodes that received a new pin connection a notification
for (auto It = NodeList.CreateConstIterator(); It; ++It)
{
UEdGraphNode* Node = (*It);
Node->NodeConnectionListChanged();
}
}
}
void UK2Node::InsertNewNode(UEdGraphPin* FromPin, UEdGraphPin* NewLinkPin, TSet<UEdGraphNode*>& OutNodeList)
{
const UEdGraphSchema_K2* K2Schema = CastChecked<UEdGraphSchema_K2>(GetSchema());
// The pin we are creating from already has a connection that needs to be broken. Being an exec pin, we want to "insert" the new node in between, so that the output of the new node is hooked up to
UEdGraphPin* OldLinkedPin = FromPin->LinkedTo[0];
check(OldLinkedPin);
FromPin->BreakAllPinLinks();
// Hook up the old linked pin to the first valid output pin on the new node
for (int32 OutpinPinIdx=0; OutpinPinIdx<Pins.Num(); OutpinPinIdx++)
{
UEdGraphPin* OutputExecPin = Pins[OutpinPinIdx];
check(OutputExecPin);
if (ECanCreateConnectionResponse::CONNECT_RESPONSE_MAKE == K2Schema->CanCreateConnection(OldLinkedPin, OutputExecPin).Response)
{
if (K2Schema->TryCreateConnection(OldLinkedPin, OutputExecPin))
{
OutNodeList.Add(OldLinkedPin->GetOwningNode());
OutNodeList.Add(this);
}
break;
}
}
if (K2Schema->TryCreateConnection(FromPin, NewLinkPin))
{
OutNodeList.Add(FromPin->GetOwningNode());
OutNodeList.Add(this);
}
}
void UK2Node::GetNodeAttributes( TArray<TKeyValuePair<FString, FString>>& OutNodeAttributes ) const
{
OutNodeAttributes.Add( TKeyValuePair<FString, FString>( TEXT( "Type" ), TEXT( "GraphNode" ) ));
OutNodeAttributes.Add( TKeyValuePair<FString, FString>( TEXT( "Class" ), GetClass()->GetName() ));
OutNodeAttributes.Add( TKeyValuePair<FString, FString>( TEXT( "Name" ), GetName() ));
}
void UK2Node::PinConnectionListChanged(UEdGraphPin* Pin)
{
// If the pin has been connected, clear the default values so that we don't hold on to references
if(Pin->LinkedTo.Num() > 0)
{
// Verify that we have a proper outer at this point, such that the schema will be valid
UEdGraph* OuterGraph = GetGraph();
if( OuterGraph && OuterGraph->Schema )
{
const UEdGraphSchema_K2* Schema = Cast<const UEdGraphSchema_K2>(GetSchema());
Pin->ResetDefaultValue();
if (EEdGraphPinDirection::EGPD_Input == Pin->Direction)
{
Schema->SetPinDefaultValueBasedOnType(Pin);
}
}
}
NotifyPinConnectionListChanged(Pin);
}
void UK2Node::ReallocatePinsDuringReconstruction(TArray<UEdGraphPin*>& OldPins)
{
AllocateDefaultPins();
for (auto OldPin : OldPins)
{
if (OldPin->ParentPin)
{
// find the new pin that corresponds to parent, and split it if it isn't already split
for (auto NewPin : Pins)
{
if (FCString::Stricmp(*(NewPin->PinName), *(OldPin->ParentPin->PinName)) == 0)
{
// Make sure we're not dealing with a menu node
UEdGraph* OuterGraph = GetGraph();
if (OuterGraph && OuterGraph->Schema && NewPin->SubPins.Num() == 0)
{
NewPin->PinType = OldPin->ParentPin->PinType;
GetSchema()->SplitPin(NewPin);
}
}
}
}
}
}
void UK2Node::PostReconstructNode()
{
if (!IsTemplate())
{
// Make sure we're not dealing with a menu node
UEdGraph* OuterGraph = GetGraph();
if( OuterGraph && OuterGraph->Schema )
{
const UEdGraphSchema_K2* Schema = Cast<const UEdGraphSchema_K2>(GetSchema());
// fix up any pin data if it needs to
for( auto PinIt = Pins.CreateIterator(); PinIt; ++PinIt )
{
UEdGraphPin* CurrentPin = *PinIt;
const FString& PinCategory = CurrentPin->PinType.PinCategory;
// fix up enum names if it exists in enum redirects
if (PinCategory == Schema->PC_Byte)
{
UEnum* EnumPtr = Cast<UEnum>(CurrentPin->PinType.PinSubCategoryObject.Get());
if (EnumPtr)
{
const FString& PinValue = CurrentPin->DefaultValue;
// see if this enum is in EnumRedirects
int32 EnumIndex = UEnum::FindEnumRedirects(EnumPtr, *PinValue);
if (EnumIndex != INDEX_NONE)
{
FString EnumName = EnumPtr->GetEnumName(EnumIndex);
// if the name does not match with pin value, override pin value
if (EnumName != PinValue)
{
// I'm not marking package as dirty
// as I know that's not going to work during serialize or post load
CurrentPin->DefaultValue = EnumName;
continue;
}
}
}
}
else if (PinCategory == Schema->PC_Object)
{
UClass const* PinClass = Cast<UClass const>(CurrentPin->PinType.PinSubCategoryObject.Get());
if ((PinClass != nullptr) && PinClass->IsChildOf(UInterface::StaticClass()))
{
CurrentPin->PinType.PinCategory = Schema->PC_Interface;
}
}
}
}
}
}
void UK2Node::ReconstructNode()
{
Modify();
UBlueprint* Blueprint = GetBlueprint();
// Break any links to 'orphan' pins
for (int32 PinIndex = 0; PinIndex < Pins.Num(); ++PinIndex)
{
UEdGraphPin* Pin = Pins[PinIndex];
TArray<class UEdGraphPin*> LinkedToCopy = Pin->LinkedTo;
for (int32 LinkIdx = 0; LinkIdx < LinkedToCopy.Num(); LinkIdx++)
{
UEdGraphPin* OtherPin = LinkedToCopy[LinkIdx];
// If we are linked to a pin that its owner doesn't know about, break that link
if ((OtherPin == NULL) || !OtherPin->GetOwningNodeUnchecked() || !OtherPin->GetOwningNode()->Pins.Contains(OtherPin))
{
Pin->LinkedTo.Remove(OtherPin);
}
}
}
// Move the existing pins to a saved array
TArray<UEdGraphPin*> OldPins(Pins);
Pins.Empty();
// Recreate the new pins
ReallocatePinsDuringReconstruction(OldPins);
bool bDestroyOldPins = true;
if (Pins.Num() == 0)
{
//keep old pins on callfunction so that graph doesn't get broken up just because function is missing
if (IsA(UK2Node_CallFunction::StaticClass()) || IsA(UK2Node_MacroInstance::StaticClass()))
{
Pins = OldPins;
bDestroyOldPins = false;
}
}
else
{
RewireOldPinsToNewPins(OldPins, Pins);
}
if (bDestroyOldPins)
{
DestroyPinList(OldPins);
}
// Let subclasses do any additional work
PostReconstructNode();
GetGraph()->NotifyGraphChanged();
}
void UK2Node::GetRedirectPinNames(const UEdGraphPin& Pin, TArray<FString>& RedirectPinNames) const
{
RedirectPinNames.Add(Pin.PinName);
}
UK2Node::ERedirectType UK2Node::ShouldRedirectParam(const TArray<FString>& OldPinNames, FName& NewPinName, const UK2Node * NewPinNode) const
{
if ( ensure(NewPinNode) )
{
const TMultiMap<UClass*, FParamRemapInfo>& ParamRedirectMap = FMemberReference::GetParamRedirectMap();
if ( ParamRedirectMap.Num() > 0 )
{
// convert TArray<FString> to TArray<FName>, faster to search
TArray<FName> OldPinFNames;
for (auto NameIter=OldPinNames.CreateConstIterator(); NameIter; ++NameIter)
{
const FString& Name = *NameIter;
OldPinFNames.AddUnique(*Name);
}
// go through for the NewPinNode
for(TMultiMap<UClass*, FParamRemapInfo>::TConstKeyIterator ParamIter(ParamRedirectMap, NewPinNode->GetClass()); ParamIter; ++ParamIter)
{
const FParamRemapInfo& ParamRemap = ParamIter.Value();
// if it has it, return true
if (OldPinFNames.Contains(ParamRemap.OldParam)
&& (ParamRemap.NodeTitle == NAME_None || ParamRemap.NodeTitle.ToString() == NewPinNode->GetNodeTitle(ENodeTitleType::FullTitle).ToString()))
{
NewPinName = ParamRemap.NewParam;
return (ParamRemap.bCustomValueMapping ? ERedirectType_Custom : (ParamRemap.ParamValueMap.Num() ? ERedirectType_Value : ERedirectType_Name));
}
}
}
}
return ERedirectType_None;
}
UK2Node::ERedirectType UK2Node::DoPinsMatchForReconstruction(const UEdGraphPin* NewPin, int32 NewPinIndex, const UEdGraphPin* OldPin, int32 OldPinIndex) const
{
ERedirectType RedirectType = ERedirectType_None;
// if the pin names do match
if (FCString::Stricmp(*(NewPin->PinName), *(OldPin->PinName)) == 0)
{
// Make sure we're not dealing with a menu node
UEdGraph* OuterGraph = GetGraph();
if( OuterGraph && OuterGraph->Schema )
{
const UEdGraphSchema_K2* K2Schema = Cast<const UEdGraphSchema_K2>(GetSchema());
if( !K2Schema || K2Schema->IsSelfPin(*NewPin) || K2Schema->ArePinTypesCompatible(OldPin->PinType, NewPin->PinType) )
{
RedirectType = ERedirectType_Name;
}
else
{
RedirectType = ERedirectType_None;
}
}
}
else
{
// try looking for a redirect if it's a K2 node
if (UK2Node* Node = Cast<UK2Node>(NewPin->GetOwningNode()))
{
if (OldPin->ParentPin == NULL)
{
// if you don't have matching pin, now check if there is any redirect param set
TArray<FString> OldPinNames;
GetRedirectPinNames(*OldPin, OldPinNames);
FName NewPinName;
RedirectType = ShouldRedirectParam(OldPinNames, /*out*/ NewPinName, Node);
// make sure they match
if ((RedirectType != ERedirectType_None) && FCString::Stricmp(*(NewPin->PinName), *(NewPinName.ToString())) != 0)
{
RedirectType = ERedirectType_None;
}
}
else
{
struct FPropertyDetails
{
const UEdGraphPin* Pin;
FString PropertyName;
FPropertyDetails(const UEdGraphPin* InPin, const FString& InPropertyName)
: Pin(InPin), PropertyName(InPropertyName)
{
}
};
TArray<FPropertyDetails> ParentHierarchy;
FString NewPinNameStr;
{
const UEdGraphPin* CurPin = OldPin;
do
{
ParentHierarchy.Add(FPropertyDetails(CurPin, CurPin->PinName.RightChop(CurPin->ParentPin->PinName.Len() + 1)));
CurPin = CurPin->ParentPin;
} while (CurPin->ParentPin);
// if you don't have matching pin, now check if there is any redirect param set
TArray<FString> OldPinNames;
GetRedirectPinNames(*CurPin, OldPinNames);
FName NewPinName;
RedirectType = ShouldRedirectParam(OldPinNames, /*out*/ NewPinName, Node);
NewPinNameStr = (RedirectType == ERedirectType_None ? CurPin->PinName : NewPinName.ToString());
}
for (int32 ParentIndex = ParentHierarchy.Num() - 1; ParentIndex >= 0; --ParentIndex)
{
const UEdGraphPin* CurPin = ParentHierarchy[ParentIndex].Pin;
const UEdGraphPin* ParentPin = CurPin ? CurPin->ParentPin : nullptr;
const UObject* SubCategoryObject = ParentPin ? ParentPin->PinType.PinSubCategoryObject.Get() : nullptr;
TMap<FName, FName>* StructRedirects = SubCategoryObject ? UStruct::TaggedPropertyRedirects.Find(SubCategoryObject->GetFName()) : nullptr;
if (StructRedirects)
{
FName* PropertyRedirect = StructRedirects->Find(FName(*ParentHierarchy[ParentIndex].PropertyName));
if (PropertyRedirect)
{
NewPinNameStr += FString("_") + PropertyRedirect->ToString();
}
else
{
NewPinNameStr += FString("_") + ParentHierarchy[ParentIndex].PropertyName;
}
}
else
{
NewPinNameStr += FString("_") + ParentHierarchy[ParentIndex].PropertyName;
}
}
// make sure they match
RedirectType = ((FCString::Stricmp(*(NewPin->PinName), *NewPinNameStr) != 0) ? ERedirectType_None : ERedirectType_Name);
}
}
}
return RedirectType;
}
void UK2Node::CustomMapParamValue(UEdGraphPin& Pin)
{
}
void UK2Node::ReconstructSinglePin(UEdGraphPin* NewPin, UEdGraphPin* OldPin, ERedirectType RedirectType)
{
UBlueprint* Blueprint = GetBlueprint();
check(NewPin && OldPin);
// Copy over modified persistent data
NewPin->CopyPersistentDataFromOldPin(*OldPin);
if (NewPin->DefaultValue != NewPin->AutogeneratedDefaultValue)
{
if (RedirectType == ERedirectType_Value)
{
TArray<FString> OldPinNames;
GetRedirectPinNames(*OldPin, OldPinNames);
// convert TArray<FString> to TArray<FName>, faster to search
TArray<FName> OldPinFNames;
for (auto NameIter=OldPinNames.CreateConstIterator(); NameIter; ++NameIter)
{
const FString& Name = *NameIter;
OldPinFNames.AddUnique(*Name);
}
// go through for the NewPinNode
for(TMultiMap<UClass*, FParamRemapInfo>::TConstKeyIterator ParamIter(FMemberReference::GetParamRedirectMap(), Cast<UK2Node>(NewPin->GetOwningNode())->GetClass()); ParamIter; ++ParamIter)
{
const FParamRemapInfo& ParamRemap = ParamIter.Value();
// once we find it, see about remapping the value
if (OldPinFNames.Contains(ParamRemap.OldParam))
{
const FString* NewValue = ParamRemap.ParamValueMap.Find(NewPin->DefaultValue);
if (NewValue)
{
NewPin->DefaultValue = *NewValue;
}
break;
}
}
}
else if (RedirectType == ERedirectType_Custom)
{
CustomMapParamValue(*NewPin);
}
}
// Update the blueprints watched pins as the old pin will be going the way of the dodo
for (int32 WatchIndex = 0; WatchIndex < Blueprint->PinWatches.Num(); ++WatchIndex)
{
UEdGraphPin*& WatchedPin = Blueprint->PinWatches[WatchIndex];
if( WatchedPin == OldPin )
{
WatchedPin = NewPin;
break;
}
}
OldPin->Rename(NULL, GetTransientPackage(), (REN_DontCreateRedirectors|(Blueprint->bIsRegeneratingOnLoad ? REN_ForceNoResetLoaders : REN_None)));
}
void UK2Node::RewireOldPinsToNewPins(TArray<UEdGraphPin*>& InOldPins, TArray<UEdGraphPin*>& InNewPins)
{
// Rewire any connection to pins that are matched by name (O(N^2) right now)
//@TODO: Can do moderately smart things here if only one pin changes name by looking at it's relative position, etc...,
// rather than just failing to map it and breaking the links
for (int32 OldPinIndex = 0; OldPinIndex < InOldPins.Num(); ++OldPinIndex)
{
UEdGraphPin* OldPin = InOldPins[OldPinIndex];
for (int32 NewPinIndex = 0; NewPinIndex < InNewPins.Num(); ++NewPinIndex)
{
UEdGraphPin* NewPin = InNewPins[NewPinIndex];
const ERedirectType RedirectType = DoPinsMatchForReconstruction(NewPin, NewPinIndex, OldPin, OldPinIndex);
if (RedirectType != ERedirectType_None)
{
ReconstructSinglePin(NewPin, OldPin, RedirectType);
break;
}
}
}
}
void UK2Node::DestroyPinList(TArray<UEdGraphPin*>& InPins)
{
UBlueprint* Blueprint = GetBlueprint();
// Throw away the original pins
for (UEdGraphPin* Pin : InPins)
{
Pin->Modify();
Pin->BreakAllPinLinks();
// just in case this pin was set to watch (don't want to save PinWatches with dead pins)
Blueprint->PinWatches.Remove(Pin);
#if 0
UEdGraphNode::ReturnPinToPool(Pin);
#else
Pin->Rename(NULL, GetTransientPackage(), (Blueprint->bIsRegeneratingOnLoad ? REN_ForceNoResetLoaders : REN_None));
Pin->RemoveFromRoot();
Pin->MarkPendingKill();
#endif
}
}
bool UK2Node::AllowSplitPins() const
{
return true;
}
void UK2Node::ExpandSplitPin(FKismetCompilerContext* CompilerContext, UEdGraph* SourceGraph, UEdGraphPin* Pin)
{
const UEdGraphSchema_K2* Schema = CastChecked<UEdGraphSchema_K2>(CompilerContext ? CompilerContext->GetSchema() : SourceGraph->GetSchema());
UK2Node* ExpandedNode = Schema->CreateSplitPinNode(Pin, CompilerContext, SourceGraph);
int32 SubPinIndex = 0;
for (int32 ExpandedPinIndex=0; ExpandedPinIndex < ExpandedNode->Pins.Num(); ++ExpandedPinIndex)
{
UEdGraphPin* ExpandedPin = ExpandedNode->Pins[ExpandedPinIndex];
if (!ExpandedPin->bHidden)
{
if (ExpandedPin->Direction == Pin->Direction)
{
UEdGraphPin* SubPin = Pin->SubPins[SubPinIndex++];
if (CompilerContext)
{
CompilerContext->MovePinLinksToIntermediate(*SubPin, *ExpandedPin);
}
else
{
Schema->MovePinLinks(*SubPin, *ExpandedPin);
}
Pins.Remove(SubPin);
}
else
{
Schema->TryCreateConnection(Pin, ExpandedPin);
}
}
}
Pin->SubPins.Empty();
}
void UK2Node::ExpandNode(FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)
{
// We iterate the array in reverse so we can both remove the subpins safely after we've read them and
// so we have split nested structs we combine them back together in the right order
for (int32 PinIndex=Pins.Num() - 1; PinIndex >= 0; --PinIndex)
{
UEdGraphPin* Pin = Pins[PinIndex];
if (Pin->SubPins.Num() > 0)
{
ExpandSplitPin(&CompilerContext, SourceGraph, Pin);
}
}
}
bool UK2Node::HasValidBlueprint() const
{
// Perform an unchecked search here, so we don't crash if this is a transient node from a list refresh without a valid outer blueprint
return (FBlueprintEditorUtils::FindBlueprintForNode(this) != NULL);
}
UBlueprint* UK2Node::GetBlueprint() const
{
return FBlueprintEditorUtils::FindBlueprintForNodeChecked(this);
}
FLinearColor UK2Node::GetNodeTitleColor() const
{
// Different color for pure operations
if (IsNodePure())
{
return GetDefault<UGraphEditorSettings>()->PureFunctionCallNodeTitleColor;
}
return GetDefault<UGraphEditorSettings>()->FunctionCallNodeTitleColor;
}
ERenamePinResult UK2Node::RenameUserDefinedPin(const FString& OldName, const FString& NewName, bool bTest)
{
UEdGraphPin* Pin = NULL;
for (int32 PinIdx=0; PinIdx<Pins.Num(); PinIdx++)
{
if (OldName == Pins[PinIdx]->PinName)
{
Pin = Pins[PinIdx];
}
else if(NewName == Pins[PinIdx]->PinName)
{
return ERenamePinResult::ERenamePinResult_NameCollision;
}
}
if(!Pin)
{
return ERenamePinResult::ERenamePinResult_NoSuchPin;
}
if(!bTest)
{
Pin->Modify();
Pin->PinName = NewName;
if(!Pin->DefaultTextValue.IsEmpty())
{
Pin->GetSchema()->TrySetDefaultText(*Pin, Pin->DefaultTextValue);
}
if (Pin->SubPins.Num() > 0)
{
TArray<UEdGraphPin*> PinsToUpdate = Pin->SubPins;
while (PinsToUpdate.Num() > 0)
{
UEdGraphPin* PinToRename = PinsToUpdate.Pop(/*bAllowShrinking=*/ false);
if (PinToRename->SubPins.Num() > 0)
{
PinsToUpdate.Append(PinToRename->SubPins);
}
PinToRename->Modify();
PinToRename->PinName = NewName + PinToRename->PinName.RightChop(OldName.Len());
PinToRename->PinFriendlyName = FText::FromString(NewName + PinToRename->PinFriendlyName.ToString().RightChop(OldName.Len()));
}
}
}
return ERenamePinResult::ERenamePinResult_Success;
}
/////////////////////////////////////////////////////
// FOptionalPinManager
void FOptionalPinManager::GetRecordDefaults(UProperty* TestProperty, FOptionalPinFromProperty& Record) const
{
Record.bShowPin = true;
Record.bCanToggleVisibility = true;
}
bool FOptionalPinManager::CanTreatPropertyAsOptional(UProperty* TestProperty) const
{
return TestProperty->HasAnyPropertyFlags(CPF_Edit|CPF_BlueprintVisible); // TODO: ANIMREFACTOR: Maybe only CPF_Edit?
}
void FOptionalPinManager::RebuildPropertyList(TArray<FOptionalPinFromProperty>& Properties, UStruct* SourceStruct)
{
// Save the old visibility
TMap<FName, bool> OldVisibility;
for (auto ExtraPropertyIt = Properties.CreateIterator(); ExtraPropertyIt; ++ExtraPropertyIt)
{
FOptionalPinFromProperty& PropertyEntry = *ExtraPropertyIt;
OldVisibility.Add(PropertyEntry.PropertyName, PropertyEntry.bShowPin);
}
// Rebuild the property list
Properties.Empty();
for (TFieldIterator<UProperty> It(SourceStruct, EFieldIteratorFlags::IncludeSuper); It; ++It)
{
UProperty* TestProperty = *It;
if (CanTreatPropertyAsOptional(TestProperty))
{
FOptionalPinFromProperty* Record = new (Properties) FOptionalPinFromProperty;
Record->PropertyName = TestProperty->GetFName();
Record->PropertyFriendlyName = UEditorEngine::GetFriendlyName(TestProperty, SourceStruct);
Record->PropertyTooltip = TestProperty->GetToolTipText();
// Get the defaults
GetRecordDefaults(TestProperty, *Record);
// If this is a refresh, propagate the old visibility
if (Record->bCanToggleVisibility)
{
if (bool* pShowHide = OldVisibility.Find(Record->PropertyName))
{
Record->bShowPin = *pShowHide;
}
}
}
}
}
void FOptionalPinManager::CreateVisiblePins(TArray<FOptionalPinFromProperty>& Properties, UStruct* SourceStruct, EEdGraphPinDirection Direction, UK2Node* TargetNode, uint8* StructBasePtr)
{
const UEdGraphSchema_K2* Schema = GetDefault<UEdGraphSchema_K2>();
for (auto ExtraPropertyIt = Properties.CreateIterator(); ExtraPropertyIt; ++ExtraPropertyIt)
{
FOptionalPinFromProperty& PropertyEntry = *ExtraPropertyIt;
if (UProperty* OuterProperty = FindFieldChecked<UProperty>(SourceStruct, PropertyEntry.PropertyName))
{
// Do we treat an array property as one pin, or a pin per entry in the array?
// Depends on if we have an instance of the struct to work with.
UArrayProperty* ArrayProperty = Cast<UArrayProperty>(OuterProperty);
if ((ArrayProperty != NULL) && (StructBasePtr != NULL))
{
UProperty* InnerProperty = ArrayProperty->Inner;
FEdGraphPinType PinType;
if (Schema->ConvertPropertyToPinType(InnerProperty, /*out*/ PinType))
{
FScriptArrayHelper_InContainer ArrayHelper(ArrayProperty, StructBasePtr);
for (int32 Index = 0; Index < ArrayHelper.Num(); ++Index)
{
// Create the pin
UEdGraphPin* NewPin = NULL;
if (PropertyEntry.bShowPin)
{
FFormatNamedArguments Args;
Args.Add(TEXT("PinName"), FText::FromName(PropertyEntry.PropertyName));
Args.Add(TEXT("Index"), Index);
const FText PinFriendlyName = FText::Format(LOCTEXT("PinFriendlyNameWithIndex", "{PinName}_{Index}"), Args);
const FString PinName = PinFriendlyName.ToString();
NewPin = TargetNode->CreatePin(Direction, PinType.PinCategory, PinType.PinSubCategory, PinType.PinSubCategoryObject.Get(), PinType.bIsArray, PinType.bIsReference, PinName);
NewPin->PinFriendlyName = PinFriendlyName;
Schema->ConstructBasicPinTooltip(*NewPin, PropertyEntry.PropertyTooltip, NewPin->PinToolTip);
// Allow the derived class to customize the created pin
CustomizePinData(NewPin, PropertyEntry.PropertyName, Index, InnerProperty);
}
// Let derived classes take a crack at transferring default values
uint8* ValuePtr = ArrayHelper.GetRawPtr(Index);
if (NewPin != NULL)
{
PostInitNewPin(NewPin, PropertyEntry, Index, ArrayProperty->Inner, ValuePtr);
}
else
{
PostRemovedOldPin(PropertyEntry, Index, ArrayProperty->Inner, ValuePtr);
}
}
}
}
else
{
// Not an array property
FEdGraphPinType PinType;
if (Schema->ConvertPropertyToPinType(OuterProperty, /*out*/ PinType))
{
// Create the pin
UEdGraphPin* NewPin = NULL;
if (PropertyEntry.bShowPin)
{
const FString PinName = PropertyEntry.PropertyName.ToString();
NewPin = TargetNode->CreatePin(Direction, PinType.PinCategory, PinType.PinSubCategory, PinType.PinSubCategoryObject.Get(), PinType.bIsArray, PinType.bIsReference, PinName);
NewPin->PinFriendlyName = PropertyEntry.PropertyFriendlyName.IsEmpty() ? FText::FromString(PinName) : FText::FromString(PropertyEntry.PropertyFriendlyName);
Schema->ConstructBasicPinTooltip(*NewPin, PropertyEntry.PropertyTooltip, NewPin->PinToolTip);
// Allow the derived class to customize the created pin
CustomizePinData(NewPin, PropertyEntry.PropertyName, INDEX_NONE, OuterProperty);
}
// Let derived classes take a crack at transferring default values
if (StructBasePtr != NULL)
{
uint8* ValuePtr = OuterProperty->ContainerPtrToValuePtr<uint8>(StructBasePtr);
if (NewPin != NULL)
{
PostInitNewPin(NewPin, PropertyEntry, INDEX_NONE, OuterProperty, ValuePtr);
}
else
{
PostRemovedOldPin(PropertyEntry, INDEX_NONE, OuterProperty, ValuePtr);
}
}
}
}
}
}
}
UEdGraphPin* UK2Node::GetExecPin() const
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
UEdGraphPin* Pin = FindPin(K2Schema->PN_Execute);
check(Pin == NULL || Pin->Direction == EGPD_Input); // If pin exists, it must be input
return Pin;
}
UEdGraphPin* UK2Node::GetPassThroughPin(const UEdGraphPin* FromPin) const
{
UEdGraphPin* MatchedPin = nullptr;
if(FromPin && Pins.Contains(FromPin))
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
if(K2Schema->IsExecPin(*FromPin))
{
if(FromPin->Direction == EGPD_Input)
{
MatchedPin = FindPin(K2Schema->PN_Then);
}
else
{
MatchedPin = FindPin(K2Schema->PN_Execute);
}
}
}
return MatchedPin;
}
bool UK2Node::CanCreateUnderSpecifiedSchema(const UEdGraphSchema* DesiredSchema) const
{
return DesiredSchema->GetClass()->IsChildOf(UEdGraphSchema_K2::StaticClass());
}
void UK2Node::Message_Note(const FString& Message)
{
UBlueprint* OwningBP = GetBlueprint();
if( OwningBP )
{
OwningBP->Message_Note(Message);
}
else
{
UE_LOG(LogBlueprint, Log, TEXT("%s"), *Message);
}
}
void UK2Node::Message_Warn(const FString& Message)
{
UBlueprint* OwningBP = GetBlueprint();
if( OwningBP )
{
OwningBP->Message_Warn(Message);
}
else
{
UE_LOG(LogBlueprint, Warning, TEXT("%s"), *Message);
}
}
void UK2Node::Message_Error(const FString& Message)
{
UBlueprint* OwningBP = GetBlueprint();
if( OwningBP )
{
OwningBP->Message_Error(Message);
}
else
{
UE_LOG(LogBlueprint, Error, TEXT("%s"), *Message);
}
}
FString UK2Node::GetDocumentationLink() const
{
return TEXT("Shared/GraphNodes/Blueprint");
}
void UK2Node::GetPinHoverText(const UEdGraphPin& Pin, FString& HoverTextOut) const
{
// start with the default hover text (from the pin's tool-tip)
Super::GetPinHoverText(Pin, HoverTextOut);
// if the Pin wasn't initialized with a tool-tip of its own
if (HoverTextOut.IsEmpty())
{
UEdGraphSchema const* Schema = GetSchema();
check(Schema != NULL);
Schema->ConstructBasicPinTooltip(Pin, FText::GetEmpty(), HoverTextOut);
}
UBlueprint* Blueprint = FBlueprintEditorUtils::FindBlueprintForNodeChecked(this);
check(Blueprint != NULL);
// If this resides in an intermediate graph, show the UObject name for debug purposes
if(Blueprint->IntermediateGeneratedGraphs.Contains(GetGraph()))
{
HoverTextOut = FString::Printf(TEXT("%s\n\n%s"), *Pin.GetName(), *HoverTextOut);
}
UObject* ActiveObject = Blueprint->GetObjectBeingDebugged();
// if there is no object being debugged, then we don't need to tack on any of the following data
if (ActiveObject == NULL)
{
return;
}
// Switch the blueprint to the one that generated the object being debugged (e.g. in case we're inside a Macro BP while debugging)
Blueprint = Cast<UBlueprint>(ActiveObject->GetClass()->ClassGeneratedBy);
if (Blueprint == NULL)
{
return;
}
// if the blueprint doesn't have debug data, notify the user
/*if (!FKismetDebugUtilities::HasDebuggingData(Blueprint))
{
HoverTextOut += TEXT("\n(NO DEBUGGING INFORMATION GENERATED, NEED TO RECOMPILE THE BLUEPRINT)");
}*/
//@TODO: For exec pins, show when they were last executed
// grab the debug value of the pin
FString WatchText;
const FKismetDebugUtilities::EWatchTextResult WatchStatus = FKismetDebugUtilities::GetWatchText(/*inout*/ WatchText, Blueprint, ActiveObject, &Pin);
// if this is an array pin, then we possibly have too many lines (too many entries)
if (Pin.PinType.bIsArray)
{
int32 LineCounter = 0;
int32 OriginalWatchTextLen = WatchText.Len();
// walk the string, finding line breaks (counting lines)
for (int32 NewWatchTextLen = 0; NewWatchTextLen < OriginalWatchTextLen; )
{
++LineCounter;
int32 NewLineIndex = WatchText.Find("\n", ESearchCase::IgnoreCase, ESearchDir::FromStart, NewWatchTextLen);
// if we've reached the end of the string (it's not to long)
if (NewLineIndex == INDEX_NONE)
{
break;
}
NewWatchTextLen = NewLineIndex + 1;
// if we're at the end of the string (but it ends with a newline)
if (NewWatchTextLen >= OriginalWatchTextLen)
{
break;
}
// if we've hit the max number of lines allowed in a tooltip
if (LineCounter >= MaxArrayPinTooltipLineCount)
{
// truncate WatchText so it contains a finite number of lines
WatchText = WatchText.Left(NewWatchTextLen);
WatchText += "..."; // WatchText should already have a trailing newline (no need to prepend this with one)
break;
}
}
} // if Pin.PinType.bIsArray...
switch (WatchStatus)
{
case FKismetDebugUtilities::EWTR_Valid:
HoverTextOut += FString::Printf(TEXT("\nCurrent value = %s"), *WatchText); //@TODO: Print out object being debugged name?
break;
case FKismetDebugUtilities::EWTR_NotInScope:
HoverTextOut += TEXT("\n(Variable is not in scope)");
break;
default:
case FKismetDebugUtilities::EWTR_NoDebugObject:
case FKismetDebugUtilities::EWTR_NoProperty:
break;
}
}
UClass* UK2Node::GetBlueprintClassFromNode() const
{
auto BP = FBlueprintEditorUtils::FindBlueprintForNode(this);
UClass* BPClass = BP
? (BP->SkeletonGeneratedClass ? BP->SkeletonGeneratedClass : BP->GeneratedClass)
: nullptr;
return BPClass;
}
#undef LOCTEXT_NAMESPACE