Files
UnrealEngineUWP/Engine/Source/Editor/AnimGraph/Private/AnimGraphNode_CustomProperty.cpp

423 lines
13 KiB
C++
Raw Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#include "AnimGraphNode_CustomProperty.h"
#include "EdGraphSchema_K2.h"
#include "Kismet2/CompilerResultsLog.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "DetailLayoutBuilder.h"
#include "AnimationGraphSchema.h"
#include "CustomPropertyOptionalPinManager.h"
#include "DetailCategoryBuilder.h"
#include "PropertyCustomizationHelpers.h"
#include "KismetCompiler.h"
#include "IAnimBlueprintCompilationContext.h"
Anim blueprint encapsulation improvements Adds 'template' anim BP concept. These anim BPs have no TargetSkeleton and as such cannot have direct references to animations placed inside of their anim graphs Adds function call support from anim nodes. All anim graph nodes can now call functions when initialized, updated, evaluated or when they first become relevant. Relevancy is established using a new FAnimSubsystemInstance_NodeRelevancy which tracks nodes in a local map, per UAnimInstance, if nodes require it. Functions are displayed on the node if bound, and in the details panel. Added a new override point to FAnimSubsystemInstance to allow for WT init. Moved FMemberReference customization into a public header so it can be used on anim node functions. Wrapped functions up into FAnimNodeFunctionRef structure so they can be re-used more effectively. Converted CallFunction node to use them. Added a couple of simple BP function libraries to demonstrate the use of the new FAnimNodeContext (this is intended to be a generic way of exposing FAnimNode_Base types to script without the node types themselves having to be known to script directly). Added the ability to set exposed properties as 'always dynamic' so they appear in mutable data rather than being merged into constants. This allows for the anim node data API to be changed to be less strict (and more script friendly). Now values can be set in mutable data (via GET_MUTABLE_ANIM_NODE_DATA_PTR) and attempted sets to constant data can be ignored and reported to client code. A few minor crash fixes with edge cases (inc when trying to open an asset editor fails because of a missing skeleton/cancellation). Also fixes an issue where literal linked anim graph/control rig/custom poroperty node inputs didnt get copied #rb Jurre.deBaare,Aaron.Cox #ROBOMERGE-SOURCE: CL 16703644 in //UE5/Main/... #ROBOMERGE-BOT: STARSHIP (Main -> Release-Engine-Test) (v835-16672529) [CL 16703653 by thomas sarkanen in ue5-release-engine-test branch]
2021-06-17 08:59:23 -04:00
#include "IAnimBlueprintCopyTermDefaultsContext.h"
#include "UObject/UE5ReleaseStreamObjectVersion.h"
#include "AnimGraphNodeBinding.h"
#define LOCTEXT_NAMESPACE "CustomPropNode"
void UAnimGraphNode_CustomProperty::Serialize(FArchive& Ar)
{
Super::Serialize(Ar);
Ar.UsingCustomVersion(FUE5ReleaseStreamObjectVersion::GUID);
if(Ar.IsLoading() && Ar.CustomVer(FUE5ReleaseStreamObjectVersion::GUID) < FUE5ReleaseStreamObjectVersion::CustomPropertyAnimGraphNodesUseOptionalPinManager)
{
// Port exposed property names to optional pins
for(FName KnownExposablePropertyName : KnownExposableProperties_DEPRECATED)
{
FOptionalPinFromProperty OptionalPin;
OptionalPin.PropertyName = KnownExposablePropertyName;
OptionalPin.bCanToggleVisibility = true;
OptionalPin.bShowPin = ExposedPropertyNames_DEPRECATED.Contains(KnownExposablePropertyName);
CustomPinProperties.Add(OptionalPin);
}
KnownExposableProperties_DEPRECATED.Empty();
ExposedPropertyNames_DEPRECATED.Empty();
}
}
void UAnimGraphNode_CustomProperty::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
FName PropertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
FName MemberPropertyName = (PropertyChangedEvent.MemberProperty != nullptr) ? PropertyChangedEvent.MemberProperty->GetFName() : NAME_None;
if (PropertyName == GET_MEMBER_NAME_CHECKED(FOptionalPinFromProperty, bShowPin) && MemberPropertyName == GET_MEMBER_NAME_CHECKED(UAnimGraphNode_Base, ShowPinForProperties))
{
FOptionalPinManager::EvaluateOldShownPins(ShowPinForProperties, OldShownPins, this);
ReconstructNode();
}
}
void UAnimGraphNode_CustomProperty::CreateClassVariablesFromBlueprint(IAnimBlueprintVariableCreationContext& InCreationContext)
{
for (UEdGraphPin* Pin : Pins)
{
if (!Pin->bOrphanedPin && !UAnimationGraphSchema::IsPosePin(Pin->PinType))
{
// avoid to add properties which already exist on the custom node.
// for example the ControlRig_CustomNode has a pin called "alpha" which is not custom.
if (FStructProperty* NodeProperty = CastField<FStructProperty>(GetClass()->FindPropertyByName(TEXT("Node"))))
{
if(NodeProperty->Struct->FindPropertyByName(Pin->GetFName()))
{
continue;
}
}
// Add prefix to avoid collisions
FString PrefixedName = GetPinTargetVariableName(Pin);
// Create a property on the new class to hold the pin data
InCreationContext.CreateVariable(FName(*PrefixedName), Pin->PinType);
}
}
}
void UAnimGraphNode_CustomProperty::OnProcessDuringCompilation(IAnimBlueprintCompilationContext& InCompilationContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
for (UEdGraphPin* Pin : Pins)
{
if (!Pin->bOrphanedPin && !UAnimationGraphSchema::IsPosePin(Pin->PinType))
{
// avoid to add properties which already exist on the custom node.
// for example the ControlRig_CustomNode has a pin called "alpha" which is not custom.
if (FStructProperty* NodeProperty = CastField<FStructProperty>(GetClass()->FindPropertyByName(TEXT("Node"))))
{
if(NodeProperty->Struct->FindPropertyByName(Pin->GetFName()))
{
continue;
}
}
FString PrefixedName = GetPinTargetVariableName(Pin);
// Add mappings to the node
UClass* InstClass = GetTargetSkeletonClass();
if (FProperty* FoundProperty = FindFProperty<FProperty>(InstClass, Pin->PinName))
{
AddSourceTargetProperties(*PrefixedName, FoundProperty->GetFName());
}
else
{
AddSourceTargetProperties(*PrefixedName, Pin->GetFName());
}
}
}
}
Anim blueprint encapsulation improvements Adds 'template' anim BP concept. These anim BPs have no TargetSkeleton and as such cannot have direct references to animations placed inside of their anim graphs Adds function call support from anim nodes. All anim graph nodes can now call functions when initialized, updated, evaluated or when they first become relevant. Relevancy is established using a new FAnimSubsystemInstance_NodeRelevancy which tracks nodes in a local map, per UAnimInstance, if nodes require it. Functions are displayed on the node if bound, and in the details panel. Added a new override point to FAnimSubsystemInstance to allow for WT init. Moved FMemberReference customization into a public header so it can be used on anim node functions. Wrapped functions up into FAnimNodeFunctionRef structure so they can be re-used more effectively. Converted CallFunction node to use them. Added a couple of simple BP function libraries to demonstrate the use of the new FAnimNodeContext (this is intended to be a generic way of exposing FAnimNode_Base types to script without the node types themselves having to be known to script directly). Added the ability to set exposed properties as 'always dynamic' so they appear in mutable data rather than being merged into constants. This allows for the anim node data API to be changed to be less strict (and more script friendly). Now values can be set in mutable data (via GET_MUTABLE_ANIM_NODE_DATA_PTR) and attempted sets to constant data can be ignored and reported to client code. A few minor crash fixes with edge cases (inc when trying to open an asset editor fails because of a missing skeleton/cancellation). Also fixes an issue where literal linked anim graph/control rig/custom poroperty node inputs didnt get copied #rb Jurre.deBaare,Aaron.Cox #ROBOMERGE-SOURCE: CL 16703644 in //UE5/Main/... #ROBOMERGE-BOT: STARSHIP (Main -> Release-Engine-Test) (v835-16672529) [CL 16703653 by thomas sarkanen in ue5-release-engine-test branch]
2021-06-17 08:59:23 -04:00
void UAnimGraphNode_CustomProperty::OnCopyTermDefaultsToDefaultObject(IAnimBlueprintCopyTermDefaultsContext& InCompilationContext, IAnimBlueprintNodeCopyTermDefaultsContext& InPerNodeContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
// Copy pin default values to generated properties
for (UEdGraphPin* Pin : Pins)
{
if (!Pin->bOrphanedPin && Pin->LinkedTo.Num() == 0 && !UAnimationGraphSchema::IsPosePin(Pin->PinType))
Anim blueprint encapsulation improvements Adds 'template' anim BP concept. These anim BPs have no TargetSkeleton and as such cannot have direct references to animations placed inside of their anim graphs Adds function call support from anim nodes. All anim graph nodes can now call functions when initialized, updated, evaluated or when they first become relevant. Relevancy is established using a new FAnimSubsystemInstance_NodeRelevancy which tracks nodes in a local map, per UAnimInstance, if nodes require it. Functions are displayed on the node if bound, and in the details panel. Added a new override point to FAnimSubsystemInstance to allow for WT init. Moved FMemberReference customization into a public header so it can be used on anim node functions. Wrapped functions up into FAnimNodeFunctionRef structure so they can be re-used more effectively. Converted CallFunction node to use them. Added a couple of simple BP function libraries to demonstrate the use of the new FAnimNodeContext (this is intended to be a generic way of exposing FAnimNode_Base types to script without the node types themselves having to be known to script directly). Added the ability to set exposed properties as 'always dynamic' so they appear in mutable data rather than being merged into constants. This allows for the anim node data API to be changed to be less strict (and more script friendly). Now values can be set in mutable data (via GET_MUTABLE_ANIM_NODE_DATA_PTR) and attempted sets to constant data can be ignored and reported to client code. A few minor crash fixes with edge cases (inc when trying to open an asset editor fails because of a missing skeleton/cancellation). Also fixes an issue where literal linked anim graph/control rig/custom poroperty node inputs didnt get copied #rb Jurre.deBaare,Aaron.Cox #ROBOMERGE-SOURCE: CL 16703644 in //UE5/Main/... #ROBOMERGE-BOT: STARSHIP (Main -> Release-Engine-Test) (v835-16672529) [CL 16703653 by thomas sarkanen in ue5-release-engine-test branch]
2021-06-17 08:59:23 -04:00
{
FString PrefixedName = GetPinTargetVariableName(Pin);
if (FProperty* GeneratedProperty = FindFProperty<FProperty>(InPerNodeContext.GetClassDefaultObject()->GetClass(), *PrefixedName))
{
if(!FBlueprintEditorUtils::PropertyValueFromString(GeneratedProperty, Pin->GetDefaultAsString(), (uint8*)InPerNodeContext.GetClassDefaultObject(), InPerNodeContext.GetClassDefaultObject()))
{
InCompilationContext.GetMessageLog().Warning(TEXT("Unable to push default value for pin @@ on @@"), Pin, this);
}
}
}
}
}
void UAnimGraphNode_CustomProperty::ValidateAnimNodeDuringCompilation(USkeleton* ForSkeleton, FCompilerResultsLog& MessageLog)
{
Super::ValidateAnimNodeDuringCompilation(ForSkeleton, MessageLog);
UAnimBlueprint* AnimBP = CastChecked<UAnimBlueprint>(GetBlueprint());
UObject* OriginalNode = MessageLog.FindSourceObject(this);
if(NeedsToSpecifyValidTargetClass())
{
// Check we have a class set
UClass* TargetClass = GetTargetClass();
if(!TargetClass)
{
MessageLog.Error(TEXT("Linked graph node @@ has no valid instance class to spawn."), this);
}
}
}
void UAnimGraphNode_CustomProperty::GetInstancePinProperty(const IAnimBlueprintCompilationContext& InCompilationContext, UEdGraphPin* InInputPin, FProperty*& OutProperty)
{
// The actual name of the instance property
FString FullName = GetPinTargetVariableName(InInputPin);
if(FProperty* Property = InCompilationContext.FindClassFProperty<FProperty>(*FullName))
{
OutProperty = Property;
}
else
{
OutProperty = nullptr;
}
}
FString UAnimGraphNode_CustomProperty::GetPinTargetVariableName(const UEdGraphPin* InPin) const
{
return GetPinTargetVariableName(InPin->GetFName());
}
FString UAnimGraphNode_CustomProperty::GetPinTargetVariableName(FName InPinName) const
{
return GetPinTargetVariableNameBase(InPinName) + NodeGuid.ToString();
}
FString UAnimGraphNode_CustomProperty::GetPinTargetVariableNameBase(FName InPinName) const
{
return TEXT("__CustomProperty_") + InPinName.ToString() + TEXT("_");
}
FText UAnimGraphNode_CustomProperty::GetPropertyTypeText(FProperty* Property)
{
FText PropertyTypeText;
if(FStructProperty* StructProperty = CastField<FStructProperty>(Property))
{
PropertyTypeText = StructProperty->Struct->GetDisplayNameText();
}
else if(FObjectProperty* ObjectProperty = CastField<FObjectProperty>(Property))
{
PropertyTypeText = ObjectProperty->PropertyClass->GetDisplayNameText();
}
else if(FFieldClass* PropClass = Property->GetClass())
{
PropertyTypeText = PropClass->GetDisplayNameText();
}
else
{
PropertyTypeText = LOCTEXT("PropertyTypeUnknown", "Unknown");
}
return PropertyTypeText;
}
void UAnimGraphNode_CustomProperty::OnInstanceClassChanged(IDetailLayoutBuilder* DetailBuilder)
{
if(DetailBuilder)
{
DetailBuilder->ForceRefreshDetails();
}
}
UObject* UAnimGraphNode_CustomProperty::GetJumpTargetForDoubleClick() const
{
UClass* InstanceClass = GetTargetClass();
if(InstanceClass)
{
return InstanceClass->ClassGeneratedBy;
}
return nullptr;
}
bool UAnimGraphNode_CustomProperty::HasExternalDependencies(TArray<class UStruct*>* OptionalOutput /*= NULL*/) const
{
UClass* InstanceClassToUse = GetTargetClass();
// Add our instance class... If that changes we need a recompile
if(InstanceClassToUse && OptionalOutput)
{
OptionalOutput->AddUnique(InstanceClassToUse);
}
bool bSuperResult = Super::HasExternalDependencies(OptionalOutput);
return InstanceClassToUse || bSuperResult;
}
void UAnimGraphNode_CustomProperty::CustomizeDetails(IDetailLayoutBuilder& DetailBuilder)
{
Super::CustomizeDetails(DetailBuilder);
IDetailCategoryBuilder& CategoryBuilder = DetailBuilder.EditCategory(FName(TEXT("Settings")));
// Customize InstanceClass
{
TSharedRef<IPropertyHandle> ClassHandle = DetailBuilder.GetProperty(TEXT("Node.InstanceClass"), GetClass());
if (ClassHandle->IsValidHandle())
{
ClassHandle->SetOnPropertyValueChanged(FSimpleDelegate::CreateUObject(this, &UAnimGraphNode_CustomProperty::OnStructuralPropertyChanged, &DetailBuilder));
}
}
}
void UAnimGraphNode_CustomProperty::CreateCustomPins(TArray<UEdGraphPin*>* OldPins)
{
if(UClass* TargetSkeletonClass = GetTargetSkeletonClass())
{
FCustomPropertyOptionalPinManager OptionalPinManager(this, OldPins);
OptionalPinManager.CreateCustomPins(TargetSkeletonClass);
}
}
FProperty* UAnimGraphNode_CustomProperty::GetPinProperty(FName InPinName) const
{
if(UClass* TargetSkeletonClass = GetTargetSkeletonClass())
{
if(FProperty* Property = TargetSkeletonClass->FindPropertyByName(InPinName))
{
return Property;
}
}
return Super::GetPinProperty(InPinName);
}
bool UAnimGraphNode_CustomProperty::IsPinBindable(const UEdGraphPin* InPin) const
{
if(const FProperty* PinProperty = GetPinProperty(InPin->GetFName()))
{
const int32 OptionalPinIndex = CustomPinProperties.IndexOfByPredicate([PinProperty](const FOptionalPinFromProperty& InOptionalPin)
{
return PinProperty->GetFName() == InOptionalPin.PropertyName;
});
return OptionalPinIndex != INDEX_NONE;
}
return Super::IsPinBindable(InPin);
}
bool UAnimGraphNode_CustomProperty::GetPinBindingInfo(FName InPinName, FName& OutBindingName, FProperty*& OutPinProperty, int32& OutOptionalPinIndex) const
{
OutPinProperty = GetPinProperty(InPinName);
if(OutPinProperty)
{
OutOptionalPinIndex = CustomPinProperties.IndexOfByPredicate([OutPinProperty](const FOptionalPinFromProperty& InOptionalPin)
{
return OutPinProperty->GetFName() == InOptionalPin.PropertyName;
});
if (OutOptionalPinIndex != INDEX_NONE)
{
OutBindingName = *GetPinTargetVariableName(InPinName);
return true;
}
}
return Super::GetPinBindingInfo(InPinName, OutBindingName, OutPinProperty, OutOptionalPinIndex);
}
bool UAnimGraphNode_CustomProperty::HasBinding(FName InBindingName) const
{
if (GetBinding())
{
return GetBinding()->HasBinding(InBindingName, true);
}
return false;
}
void UAnimGraphNode_CustomProperty::AddSourceTargetProperties(const FName& InSourcePropertyName, const FName& InTargetPropertyName)
{
FAnimNode_CustomProperty* CustomPropAnimNode = GetCustomPropertyNode();
if (CustomPropAnimNode)
{
CustomPropAnimNode->SourcePropertyNames.Add(InSourcePropertyName);
CustomPropAnimNode->DestPropertyNames.Add(InTargetPropertyName);
}
}
UClass* UAnimGraphNode_CustomProperty::GetTargetClass() const
{
const FAnimNode_CustomProperty* CustomPropAnimNode = GetCustomPropertyNode();
if (CustomPropAnimNode)
{
return CustomPropAnimNode->GetTargetClass();
}
return nullptr;
}
UClass* UAnimGraphNode_CustomProperty::GetTargetSkeletonClass() const
{
UClass* TargetClass = GetTargetClass();
if(TargetClass && TargetClass->ClassGeneratedBy)
{
UBlueprint* Blueprint = CastChecked<UBlueprint>(TargetClass->ClassGeneratedBy);
if(Blueprint)
{
if (Blueprint->SkeletonGeneratedClass)
{
return Blueprint->SkeletonGeneratedClass;
}
}
}
return TargetClass;
}
void UAnimGraphNode_CustomProperty::OnStructuralPropertyChanged(IDetailLayoutBuilder* DetailBuilder)
{
if(DetailBuilder)
{
DetailBuilder->ForceRefreshDetails();
}
}
void UAnimGraphNode_CustomProperty::SetCustomPinVisibility(bool bInVisible, int32 InOptionalPinIndex)
{
if(CustomPinProperties[InOptionalPinIndex].bShowPin != bInVisible)
{
FOptionalPinManager::CacheShownPins(CustomPinProperties, OldShownPins);
CustomPinProperties[InOptionalPinIndex].bShowPin = bInVisible;
FOptionalPinManager::EvaluateOldShownPins(CustomPinProperties, OldShownPins, this);
ReconstructNode();
}
}
void UAnimGraphNode_CustomProperty::PinConnectionListChanged(UEdGraphPin* Pin)
{
if(Pin->LinkedTo.Num() > 0)
{
// If we have links, clear any bindings
RemoveBindings(*GetPinTargetVariableName(Pin));
}
}
void UAnimGraphNode_CustomProperty::PostDuplicate(bool bDuplicateForPIE)
{
Super::PostDuplicate(bDuplicateForPIE);
if (GetBinding())
{
const FString NewGUID = NodeGuid.ToString();
GetMutableBinding()->UpdateBindingNames([this, NewGUID](const FString& InOldBindingName)
{
// Check pins
for (UEdGraphPin* Pin : Pins)
{
FString PinTargetVariableNameBase = GetPinTargetVariableNameBase(Pin->GetFName());
if (InOldBindingName.StartsWith(PinTargetVariableNameBase))
{
// Replace GUID with this node's if it differs
FString OldGUID = InOldBindingName.RightChop(PinTargetVariableNameBase.Len());
if (OldGUID != NewGUID)
{
return InOldBindingName.Replace(*OldGUID, *NewGUID);
}
}
}
return InOldBindingName;
});
}
}
#undef LOCTEXT_NAMESPACE