Files
UnrealEngineUWP/Engine/Source/Editor/BlueprintGraph/Private/K2Node_ConstructObjectFromClass.cpp
Maciej Mroz c650c628c0 K2Node_ConstructObjectFromClass - links to exposed properties are not broken when the class is changed.
#codereview Nick.Darnell

[CL 2579798 by Maciej Mroz in Main branch]
2015-06-08 06:06:24 -04:00

380 lines
13 KiB
C++

// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#include "BlueprintGraphPrivatePCH.h"
#include "KismetCompiler.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
#include "BlueprintNodeSpawner.h"
#include "EditorCategoryUtils.h"
#include "BlueprintActionDatabaseRegistrar.h"
struct FK2Node_ConstructObjectFromClassHelper
{
static FString WorldContextPinName;
static FString ClassPinName;
static FString OuterPinName;
};
FString FK2Node_ConstructObjectFromClassHelper::WorldContextPinName(TEXT("WorldContextObject"));
FString FK2Node_ConstructObjectFromClassHelper::ClassPinName(TEXT("Class"));
FString FK2Node_ConstructObjectFromClassHelper::OuterPinName(TEXT("Outer"));
#define LOCTEXT_NAMESPACE "K2Node_ConstructObjectFromClass"
UK2Node_ConstructObjectFromClass::UK2Node_ConstructObjectFromClass(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
NodeTooltip = LOCTEXT("NodeTooltip", "Attempts to spawn a new object");
}
UClass* UK2Node_ConstructObjectFromClass::GetClassPinBaseClass() const
{
return UObject::StaticClass();
}
bool UK2Node_ConstructObjectFromClass::UseWorldContext() const
{
auto BP = GetBlueprint();
const UClass* ParentClass = BP ? BP->ParentClass : nullptr;
return ParentClass ? ParentClass->HasMetaData(FBlueprintMetadata::MD_ShowWorldContextPin) : false;
}
void UK2Node_ConstructObjectFromClass::AllocateDefaultPins()
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
// Add execution pins
CreatePin(EGPD_Input, K2Schema->PC_Exec, TEXT(""), NULL, false, false, K2Schema->PN_Execute);
CreatePin(EGPD_Output, K2Schema->PC_Exec, TEXT(""), NULL, false, false, K2Schema->PN_Then);
// If required add the world context pin
if (UseWorldContext())
{
CreatePin(EGPD_Input, K2Schema->PC_Object, TEXT(""), UObject::StaticClass(), false, false, FK2Node_ConstructObjectFromClassHelper::WorldContextPinName);
}
// Add blueprint pin
UEdGraphPin* ClassPin = CreatePin(EGPD_Input, K2Schema->PC_Class, TEXT(""), GetClassPinBaseClass(), false, false, FK2Node_ConstructObjectFromClassHelper::ClassPinName);
SetPinToolTip(*ClassPin, LOCTEXT("ClassPinDescription", "The object class you want to spawn"));
// Result pin
UEdGraphPin* ResultPin = CreatePin(EGPD_Output, K2Schema->PC_Object, TEXT(""), GetClassPinBaseClass(), false, false, K2Schema->PN_ReturnValue);
SetPinToolTip(*ResultPin, LOCTEXT("ResultPinDescription", "The spawned object"));
if (UseOuter())
{
UEdGraphPin* OuterPin = CreatePin(EGPD_Input, K2Schema->PC_Object, TEXT(""), UObject::StaticClass(), false, false, FK2Node_ConstructObjectFromClassHelper::OuterPinName);
SetPinToolTip(*OuterPin, LOCTEXT("OuterPinDescription", "Owner of the created object"));
}
Super::AllocateDefaultPins();
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetOuterPin() const
{
UEdGraphPin* Pin = FindPinChecked(FK2Node_ConstructObjectFromClassHelper::OuterPinName);
ensure(nullptr == Pin || Pin->Direction == EGPD_Input);
return Pin;
}
void UK2Node_ConstructObjectFromClass::SetPinToolTip(UEdGraphPin& MutatablePin, const FText& PinDescription) const
{
MutatablePin.PinToolTip = UEdGraphSchema_K2::TypeToText(MutatablePin.PinType).ToString();
UEdGraphSchema_K2 const* const K2Schema = Cast<const UEdGraphSchema_K2>(GetSchema());
if (K2Schema != nullptr)
{
MutatablePin.PinToolTip += TEXT(" ");
MutatablePin.PinToolTip += K2Schema->GetPinDisplayName(&MutatablePin).ToString();
}
MutatablePin.PinToolTip += FString(TEXT("\n")) + PinDescription.ToString();
}
void UK2Node_ConstructObjectFromClass::CreatePinsForClass(UClass* InClass, TArray<UEdGraphPin*>* OutClassPins)
{
check(InClass != NULL);
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
const UObject* const ClassDefaultObject = InClass->GetDefaultObject(false);
for (TFieldIterator<UProperty> PropertyIt(InClass, EFieldIteratorFlags::IncludeSuper); PropertyIt; ++PropertyIt)
{
UProperty* Property = *PropertyIt;
UClass* PropertyClass = CastChecked<UClass>(Property->GetOuter());
const bool bIsDelegate = Property->IsA(UMulticastDelegateProperty::StaticClass());
const bool bIsExposedToSpawn = UEdGraphSchema_K2::IsPropertyExposedOnSpawn(Property);
const bool bIsSettableExternally = !Property->HasAnyPropertyFlags(CPF_DisableEditOnInstance);
if( bIsExposedToSpawn &&
!Property->HasAnyPropertyFlags(CPF_Parm) &&
bIsSettableExternally &&
Property->HasAllPropertyFlags(CPF_BlueprintVisible) &&
!bIsDelegate &&
(NULL == FindPin(Property->GetName()) ) )
{
UEdGraphPin* Pin = CreatePin(EGPD_Input, TEXT(""), TEXT(""), NULL, false, false, Property->GetName());
const bool bPinGood = (Pin != NULL) && K2Schema->ConvertPropertyToPinType(Property, /*out*/ Pin->PinType);
if (OutClassPins && Pin)
{
OutClassPins->Add(Pin);
}
if (ClassDefaultObject && Pin != NULL && K2Schema->PinDefaultValueIsEditable(*Pin))
{
FString DefaultValueAsString;
const bool bDefaultValueSet = FBlueprintEditorUtils::PropertyValueToString(Property, reinterpret_cast<const uint8*>(ClassDefaultObject), DefaultValueAsString);
check( bDefaultValueSet );
K2Schema->TrySetDefaultValue(*Pin, DefaultValueAsString);
}
// Copy tooltip from the property.
if (Pin != nullptr)
{
K2Schema->ConstructBasicPinTooltip(*Pin, Property->GetToolTipText(), Pin->PinToolTip);
}
}
}
// Change class of output pin
UEdGraphPin* ResultPin = GetResultPin();
ResultPin->PinType.PinSubCategoryObject = InClass;
}
UClass* UK2Node_ConstructObjectFromClass::GetClassToSpawn(const TArray<UEdGraphPin*>* InPinsToSearch /*=NULL*/) const
{
UClass* UseSpawnClass = NULL;
const TArray<UEdGraphPin*>* PinsToSearch = InPinsToSearch ? InPinsToSearch : &Pins;
UEdGraphPin* ClassPin = GetClassPin(PinsToSearch);
if(ClassPin && ClassPin->DefaultObject != NULL && ClassPin->LinkedTo.Num() == 0)
{
UseSpawnClass = CastChecked<UClass>(ClassPin->DefaultObject);
}
else if (ClassPin && ClassPin->LinkedTo.Num())
{
auto ClassSource = ClassPin->LinkedTo[0];
UseSpawnClass = ClassSource ? Cast<UClass>(ClassSource->PinType.PinSubCategoryObject.Get()) : nullptr;
}
return UseSpawnClass;
}
void UK2Node_ConstructObjectFromClass::ReallocatePinsDuringReconstruction(TArray<UEdGraphPin*>& OldPins)
{
AllocateDefaultPins();
UClass* UseSpawnClass = GetClassToSpawn(&OldPins);
if( UseSpawnClass != NULL )
{
CreatePinsForClass(UseSpawnClass);
}
}
bool UK2Node_ConstructObjectFromClass::IsSpawnVarPin(UEdGraphPin* Pin)
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
return( Pin->PinName != K2Schema->PN_Execute &&
Pin->PinName != K2Schema->PN_Then &&
Pin->PinName != K2Schema->PN_ReturnValue &&
Pin->PinName != FK2Node_ConstructObjectFromClassHelper::ClassPinName &&
Pin->PinName != FK2Node_ConstructObjectFromClassHelper::WorldContextPinName &&
Pin->PinName != FK2Node_ConstructObjectFromClassHelper::OuterPinName);
}
void UK2Node_ConstructObjectFromClass::OnClassPinChanged()
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
// Remove all pins related to archetype variables
TArray<UEdGraphPin*> OldPins = Pins;
TArray<UEdGraphPin*> OldClassPins;
for (int32 i = 0; i < OldPins.Num(); i++)
{
UEdGraphPin* OldPin = OldPins[i];
if (IsSpawnVarPin(OldPin))
{
Pins.Remove(OldPin);
OldClassPins.Add(OldPin);
}
}
CachedNodeTitle.MarkDirty();
UClass* UseSpawnClass = GetClassToSpawn();
TArray<UEdGraphPin*> NewClassPins;
if (UseSpawnClass != NULL)
{
CreatePinsForClass(UseSpawnClass, &NewClassPins);
}
// Rewire the old pins to the new pins so connections are maintained if possible
RewireOldPinsToNewPins(OldClassPins, NewClassPins);
// Destroy the old pins
DestroyPinList(OldClassPins);
// Refresh the UI for the graph so the pin changes show up
UEdGraph* Graph = GetGraph();
Graph->NotifyGraphChanged();
// Mark dirty
FBlueprintEditorUtils::MarkBlueprintAsModified(GetBlueprint());
}
void UK2Node_ConstructObjectFromClass::PinConnectionListChanged(UEdGraphPin* Pin)
{
if (Pin && (Pin->PinName == FK2Node_ConstructObjectFromClassHelper::ClassPinName))
{
OnClassPinChanged();
}
}
void UK2Node_ConstructObjectFromClass::PinDefaultValueChanged(UEdGraphPin* ChangedPin)
{
if (ChangedPin && (ChangedPin->PinName == FK2Node_ConstructObjectFromClassHelper::ClassPinName))
{
OnClassPinChanged();
}
}
FText UK2Node_ConstructObjectFromClass::GetTooltipText() const
{
return NodeTooltip;
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetThenPin()const
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
UEdGraphPin* Pin = FindPinChecked(K2Schema->PN_Then);
check(Pin->Direction == EGPD_Output);
return Pin;
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetClassPin(const TArray<UEdGraphPin*>* InPinsToSearch /*= NULL*/) const
{
const TArray<UEdGraphPin*>* PinsToSearch = InPinsToSearch ? InPinsToSearch : &Pins;
UEdGraphPin* Pin = NULL;
for( auto PinIt = PinsToSearch->CreateConstIterator(); PinIt; ++PinIt )
{
UEdGraphPin* TestPin = *PinIt;
if( TestPin && TestPin->PinName == FK2Node_ConstructObjectFromClassHelper::ClassPinName )
{
Pin = TestPin;
break;
}
}
check(Pin == NULL || Pin->Direction == EGPD_Input);
return Pin;
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetWorldContextPin() const
{
UEdGraphPin* Pin = FindPin(FK2Node_ConstructObjectFromClassHelper::WorldContextPinName);
check(Pin == NULL || Pin->Direction == EGPD_Input);
return Pin;
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetResultPin() const
{
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
UEdGraphPin* Pin = FindPinChecked(K2Schema->PN_ReturnValue);
check(Pin->Direction == EGPD_Output);
return Pin;
}
FLinearColor UK2Node_ConstructObjectFromClass::GetNodeTitleColor() const
{
return Super::GetNodeTitleColor();
}
FText UK2Node_ConstructObjectFromClass::GetBaseNodeTitle() const
{
return NSLOCTEXT("K2Node", "ConstructObject_BaseTitle", "Construct Object from Class");
}
FText UK2Node_ConstructObjectFromClass::GetNodeTitleFormat() const
{
return NSLOCTEXT("K2Node", "Construct", "Construct {ClassName}");
}
FText UK2Node_ConstructObjectFromClass::GetNodeTitle(ENodeTitleType::Type TitleType) const
{
if (TitleType == ENodeTitleType::ListView || TitleType == ENodeTitleType::MenuTitle)
{
return GetBaseNodeTitle();
}
else if (auto ClassToSpawn = GetClassToSpawn())
{
if (CachedNodeTitle.IsOutOfDate(this))
{
FFormatNamedArguments Args;
Args.Add(TEXT("ClassName"), ClassToSpawn->GetDisplayNameText());
// FText::Format() is slow, so we cache this to save on performance
CachedNodeTitle.SetCachedText(FText::Format(GetNodeTitleFormat(), Args), this);
}
return CachedNodeTitle;
}
return NSLOCTEXT("K2Node", "ConstructObject_Title_NONE", "Construct NONE");
}
bool UK2Node_ConstructObjectFromClass::IsCompatibleWithGraph(const UEdGraph* TargetGraph) const
{
UBlueprint* Blueprint = FBlueprintEditorUtils::FindBlueprintForGraph(TargetGraph);
return Super::IsCompatibleWithGraph(TargetGraph) && (!Blueprint || FBlueprintEditorUtils::FindUserConstructionScript(Blueprint) != TargetGraph);
}
void UK2Node_ConstructObjectFromClass::GetNodeAttributes( TArray<TKeyValuePair<FString, FString>>& OutNodeAttributes ) const
{
UClass* ClassToSpawn = GetClassToSpawn();
const FString ClassToSpawnStr = ClassToSpawn ? ClassToSpawn->GetName() : TEXT( "InvalidClass" );
OutNodeAttributes.Add( TKeyValuePair<FString, FString>( TEXT( "Type" ), TEXT( "ConstructObjectFromClass" ) ));
OutNodeAttributes.Add( TKeyValuePair<FString, FString>( TEXT( "Class" ), GetClass()->GetName() ));
OutNodeAttributes.Add( TKeyValuePair<FString, FString>( TEXT( "Name" ), GetName() ));
OutNodeAttributes.Add( TKeyValuePair<FString, FString>( TEXT( "ObjectClass" ), ClassToSpawnStr ));
}
void UK2Node_ConstructObjectFromClass::GetMenuActions(FBlueprintActionDatabaseRegistrar& ActionRegistrar) const
{
// actions get registered under specific object-keys; the idea is that
// actions might have to be updated (or deleted) if their object-key is
// mutated (or removed)... here we use the node's class (so if the node
// type disappears, then the action should go with it)
UClass* ActionKey = GetClass();
// to keep from needlessly instantiating a UBlueprintNodeSpawner, first
// check to make sure that the registrar is looking for actions of this type
// (could be regenerating actions for a specific asset, and therefore the
// registrar would only accept actions corresponding to that asset)
if (ActionRegistrar.IsOpenForRegistration(ActionKey))
{
UBlueprintNodeSpawner* NodeSpawner = UBlueprintNodeSpawner::Create(GetClass());
check(NodeSpawner != nullptr);
ActionRegistrar.AddBlueprintAction(ActionKey, NodeSpawner);
}
}
FText UK2Node_ConstructObjectFromClass::GetMenuCategory() const
{
return FEditorCategoryUtils::GetCommonCategory(FCommonEditorCategory::Gameplay);
}
bool UK2Node_ConstructObjectFromClass::HasExternalDependencies(TArray<class UStruct*>* OptionalOutput) const
{
UClass* SourceClass = GetClassToSpawn();
const UBlueprint* SourceBlueprint = GetBlueprint();
const bool bResult = (SourceClass != NULL) && (SourceClass->ClassGeneratedBy != SourceBlueprint);
if (bResult && OptionalOutput)
{
OptionalOutput->AddUnique(SourceClass);
}
const bool bSuperResult = Super::HasExternalDependencies(OptionalOutput);
return bSuperResult || bResult;
}
#undef LOCTEXT_NAMESPACE