Files
UnrealEngineUWP/Engine/Source/Editor/BlueprintGraph/Private/K2Node_ConstructObjectFromClass.cpp
dan oconnor 9fb46d63db #ROBOMERGE-AUTHOR: dan.oconnor
Add warning to detect default value changes when assets were saved in earlier versions of the editor, which may have had bad default value information on some call function nodes
#rb Ben.Zeigler
#jira UE-58298

#ROBOMERGE-SOURCE: CL 4117293 in //UE4/Release-4.20/...
#ROBOMERGE-BOT: RELEASE (Release-4.20 -> Release-Staging-4.20)

[CL 4117294 by dan oconnor in Staging-4.20 branch]
2018-06-06 22:05:17 -04:00

402 lines
14 KiB
C++

// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#include "K2Node_ConstructObjectFromClass.h"
#include "UObject/UnrealType.h"
#include "EdGraphSchema_K2.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "BlueprintNodeSpawner.h"
#include "EditorCategoryUtils.h"
#include "BlueprintActionDatabaseRegistrar.h"
struct FK2Node_ConstructObjectFromClassHelper
{
static FName WorldContextPinName;
static FName ClassPinName;
static FName OuterPinName;
};
FName FK2Node_ConstructObjectFromClassHelper::WorldContextPinName(TEXT("WorldContextObject"));
FName FK2Node_ConstructObjectFromClassHelper::ClassPinName(TEXT("Class"));
FName 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
{
UBlueprint* BP = GetBlueprint();
const UClass* ParentClass = BP ? BP->ParentClass : nullptr;
return ParentClass ? ParentClass->HasMetaDataHierarchical(FBlueprintMetadata::MD_ShowWorldContextPin) != nullptr : false;
}
void UK2Node_ConstructObjectFromClass::AllocateDefaultPins()
{
// Add execution pins
CreatePin(EGPD_Input, UEdGraphSchema_K2::PC_Exec, UEdGraphSchema_K2::PN_Execute);
CreatePin(EGPD_Output, UEdGraphSchema_K2::PC_Exec, UEdGraphSchema_K2::PN_Then);
// If required add the world context pin
if (UseWorldContext())
{
CreatePin(EGPD_Input, UEdGraphSchema_K2::PC_Object, UObject::StaticClass(), FK2Node_ConstructObjectFromClassHelper::WorldContextPinName);
}
// Add blueprint pin
UEdGraphPin* ClassPin = CreatePin(EGPD_Input, UEdGraphSchema_K2::PC_Class, GetClassPinBaseClass(), FK2Node_ConstructObjectFromClassHelper::ClassPinName);
// Result pin
UEdGraphPin* ResultPin = CreatePin(EGPD_Output, UEdGraphSchema_K2::PC_Object, GetClassPinBaseClass(), UEdGraphSchema_K2::PN_ReturnValue);
if (UseOuter())
{
UEdGraphPin* OuterPin = CreatePin(EGPD_Input, UEdGraphSchema_K2::PC_Object, UObject::StaticClass(), FK2Node_ConstructObjectFromClassHelper::OuterPinName);
}
Super::AllocateDefaultPins();
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetOuterPin() const
{
UEdGraphPin* Pin = FindPin(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);
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 &&
(nullptr == FindPin(Property->GetFName()) ) &&
FBlueprintEditorUtils::PropertyStillExists(Property))
{
if (UEdGraphPin* Pin = CreatePin(EGPD_Input, NAME_None, Property->GetFName()))
{
K2Schema->ConvertPropertyToPinType(Property, /*out*/ Pin->PinType);
if (OutClassPins)
{
OutClassPins->Add(Pin);
}
if (ClassDefaultObject && K2Schema->PinDefaultValueIsEditable(*Pin))
{
FString DefaultValueAsString;
const bool bDefaultValueSet = FBlueprintEditorUtils::PropertyValueToString(Property, reinterpret_cast<const uint8*>(ClassDefaultObject), DefaultValueAsString);
check(bDefaultValueSet);
K2Schema->SetPinAutogeneratedDefaultValue(Pin, DefaultValueAsString);
}
// Copy tooltip from the property.
K2Schema->ConstructBasicPinTooltip(*Pin, Property->GetToolTipText(), Pin->PinToolTip);
}
}
}
// Change class of output pin
UEdGraphPin* ResultPin = GetResultPin();
ResultPin->PinType.PinSubCategoryObject = InClass->GetAuthoritativeClass();
}
UClass* UK2Node_ConstructObjectFromClass::GetClassToSpawn(const TArray<UEdGraphPin*>* InPinsToSearch /*=NULL*/) const
{
UClass* UseSpawnClass = nullptr;
const TArray<UEdGraphPin*>* PinsToSearch = InPinsToSearch ? InPinsToSearch : &Pins;
UEdGraphPin* ClassPin = GetClassPin(PinsToSearch);
if (ClassPin && ClassPin->DefaultObject && ClassPin->LinkedTo.Num() == 0)
{
UseSpawnClass = CastChecked<UClass>(ClassPin->DefaultObject);
}
else if (ClassPin && ClassPin->LinkedTo.Num())
{
UEdGraphPin* ClassSource = ClassPin->LinkedTo[0];
UseSpawnClass = ClassSource ? Cast<UClass>(ClassSource->PinType.PinSubCategoryObject.Get()) : nullptr;
}
return UseSpawnClass;
}
void UK2Node_ConstructObjectFromClass::ReallocatePinsDuringReconstruction(TArray<UEdGraphPin*>& OldPins)
{
AllocateDefaultPins();
if (UClass* UseSpawnClass = GetClassToSpawn(&OldPins))
{
CreatePinsForClass(UseSpawnClass);
}
RestoreSplitPins(OldPins);
}
void UK2Node_ConstructObjectFromClass::PostPlacedNewNode()
{
Super::PostPlacedNewNode();
if (UClass* UseSpawnClass = GetClassToSpawn())
{
CreatePinsForClass(UseSpawnClass);
}
}
bool UK2Node_ConstructObjectFromClass::IsSpawnVarPin(UEdGraphPin* Pin) const
{
return( Pin->PinName != UEdGraphSchema_K2::PN_Execute &&
Pin->PinName != UEdGraphSchema_K2::PN_Then &&
Pin->PinName != UEdGraphSchema_K2::PN_ReturnValue &&
Pin->PinName != FK2Node_ConstructObjectFromClassHelper::ClassPinName &&
Pin->PinName != FK2Node_ConstructObjectFromClassHelper::WorldContextPinName &&
Pin->PinName != FK2Node_ConstructObjectFromClassHelper::OuterPinName);
}
void UK2Node_ConstructObjectFromClass::OnClassPinChanged()
{
// Remove all pins related to archetype variables
TArray<UEdGraphPin*> OldPins = Pins;
TArray<UEdGraphPin*> OldClassPins;
for (UEdGraphPin* OldPin : OldPins)
{
if (IsSpawnVarPin(OldPin))
{
Pins.Remove(OldPin);
OldClassPins.Add(OldPin);
}
}
CachedNodeTitle.MarkDirty();
TArray<UEdGraphPin*> NewClassPins;
if (UClass* UseSpawnClass = GetClassToSpawn())
{
CreatePinsForClass(UseSpawnClass, &NewClassPins);
}
RestoreSplitPins(OldPins);
UEdGraphPin* ResultPin = GetResultPin();
// Cache all the pin connections to the ResultPin, we will attempt to recreate them
TArray<UEdGraphPin*> ResultPinConnectionList = ResultPin->LinkedTo;
// Because the archetype has changed, we break the output link as the output pin type will change
ResultPin->BreakAllPinLinks(true);
const UEdGraphSchema_K2* K2Schema = GetDefault<UEdGraphSchema_K2>();
// Recreate any pin links to the Result pin that are still valid
for (UEdGraphPin* Connections : ResultPinConnectionList)
{
K2Schema->TryCreateConnection(ResultPin, Connections);
}
// Rewire the old pins to the new pins so connections are maintained if possible
RewireOldPinsToNewPins(OldClassPins, Pins, nullptr);
// Refresh the UI for the graph so the pin changes show up
GetGraph()->NotifyGraphChanged();
// Mark dirty
FBlueprintEditorUtils::MarkBlueprintAsModified(GetBlueprint());
}
void UK2Node_ConstructObjectFromClass::PinConnectionListChanged(UEdGraphPin* Pin)
{
Super::PinConnectionListChanged(Pin);
if (Pin && (Pin->PinName == FK2Node_ConstructObjectFromClassHelper::ClassPinName))
{
OnClassPinChanged();
}
}
void UK2Node_ConstructObjectFromClass::GetPinHoverText(const UEdGraphPin& Pin, FString& HoverTextOut) const
{
if (UEdGraphPin* ClassPin = GetClassPin())
{
SetPinToolTip(*ClassPin, LOCTEXT("ClassPinDescription", "The object class you want to construct"));
}
if (UEdGraphPin* ResultPin = GetResultPin())
{
SetPinToolTip(*ResultPin, LOCTEXT("ResultPinDescription", "The constructed object"));
}
if (UEdGraphPin* OuterPin = (UseOuter() ? GetOuterPin() : nullptr))
{
SetPinToolTip(*OuterPin, LOCTEXT("OuterPinDescription", "Owner of the constructed object"));
}
return Super::GetPinHoverText(Pin, HoverTextOut);
}
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
{
UEdGraphPin* Pin = FindPinChecked(UEdGraphSchema_K2::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 = nullptr;
for (UEdGraphPin* TestPin : *PinsToSearch)
{
if (TestPin && TestPin->PinName == FK2Node_ConstructObjectFromClassHelper::ClassPinName)
{
Pin = TestPin;
break;
}
}
check(Pin == nullptr || Pin->Direction == EGPD_Input);
return Pin;
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetWorldContextPin() const
{
UEdGraphPin* Pin = FindPin(FK2Node_ConstructObjectFromClassHelper::WorldContextPinName);
check(Pin == nullptr || Pin->Direction == EGPD_Input);
return Pin;
}
UEdGraphPin* UK2Node_ConstructObjectFromClass::GetResultPin() const
{
UEdGraphPin* Pin = FindPinChecked(UEdGraphSchema_K2::PN_ReturnValue);
check(Pin->Direction == EGPD_Output);
return Pin;
}
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 (UClass* 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 && (SourceClass->ClassGeneratedBy != SourceBlueprint));
if (bResult && OptionalOutput)
{
OptionalOutput->AddUnique(SourceClass);
}
const bool bSuperResult = Super::HasExternalDependencies(OptionalOutput);
return bSuperResult || bResult;
}
#undef LOCTEXT_NAMESPACE