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

401 lines
14 KiB
C++
Raw Normal View History

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
Copying //UE4/Dev-Build to //UE4/Dev-Main (Source: //UE4/Dev-Build @ 3209340) #lockdown Nick.Penwarden #rb none ========================== MAJOR FEATURES + CHANGES ========================== Change 3209340 on 2016/11/23 by Ben.Marsh Convert UE4 codebase to an "include what you use" model - where every header just includes the dependencies it needs, rather than every source file including large monolithic headers like Engine.h and UnrealEd.h. Measured full rebuild times around 2x faster using XGE on Windows, and improvements of 25% or more for incremental builds and full rebuilds on most other platforms. * Every header now includes everything it needs to compile. * There's a CoreMinimal.h header that gets you a set of ubiquitous types from Core (eg. FString, FName, TArray, FVector, etc...). Most headers now include this first. * There's a CoreTypes.h header that sets up primitive UE4 types and build macros (int32, PLATFORM_WIN64, etc...). All headers in Core include this first, as does CoreMinimal.h. * Every .cpp file includes its matching .h file first. * This helps validate that each header is including everything it needs to compile. * No engine code includes a monolithic header such as Engine.h or UnrealEd.h any more. * You will get a warning if you try to include one of these from the engine. They still exist for compatibility with game projects and do not produce warnings when included there. * There have only been minor changes to our internal games down to accommodate these changes. The intent is for this to be as seamless as possible. * No engine code explicitly includes a precompiled header any more. * We still use PCHs, but they're force-included on the compiler command line by UnrealBuildTool instead. This lets us tune what they contain without breaking any existing include dependencies. * PCHs are generated by a tool to get a statistical amount of coverage for the source files using it, and I've seeded the new shared PCHs to contain any header included by > 15% of source files. Tool used to generate this transform is at Engine\Source\Programs\IncludeTool. [CL 3209342 by Ben Marsh in Main branch]
2016-11-23 15:48:37 -05:00
#include "K2Node_AnimGetter.h"
#include "Animation/AnimBlueprint.h"
#include "Kismet2/BlueprintEditorUtils.h"
#include "Animation/AnimInstance.h"
#include "AnimationGraphSchema.h"
#include "AnimationTransitionSchema.h"
#include "BlueprintActionDatabaseRegistrar.h"
#include "BlueprintNodeSpawner.h"
#include "AnimGraphNode_AssetPlayerBase.h"
#include "AnimGraphNode_StateMachine.h"
#include "AnimStateNode.h"
#include "AnimationStateMachineGraph.h"
#include "AnimationCustomTransitionSchema.h"
#include "AnimStateTransitionNode.h"
#define LOCTEXT_NAMESPACE "AnimGetter"
void UK2Node_AnimGetter::AllocateDefaultPins()
{
Super::AllocateDefaultPins();
TArray<UEdGraphPin*> PinsToHide;
TArray<FString> PinNames;
// TODO: Find a nicer way to maybe pull these down from the instance class and allow
// projects to add new parameters from derived instances
PinNames.Add(TEXT("CurrentTime"));
PinNames.Add(TEXT("AssetPlayerIndex"));
PinNames.Add(TEXT("MachineIndex"));
PinNames.Add(TEXT("StateIndex"));
PinNames.Add(TEXT("TransitionIndex"));
for(FString& PinName : PinNames)
{
if(UEdGraphPin* FoundPin = FindPin(PinName))
{
PinsToHide.Add(FoundPin);
}
}
for(UEdGraphPin* Pin : PinsToHide)
{
Pin->bHidden = true;
}
}
FText UK2Node_AnimGetter::GetNodeTitle(ENodeTitleType::Type TitleType) const
{
return CachedTitle;
}
bool UK2Node_AnimGetter::CanCreateUnderSpecifiedSchema(const UEdGraphSchema* Schema) const
{
return Cast<UAnimationGraphSchema>(Schema) != NULL || Cast<UAnimationTransitionSchema>(Schema) != NULL;
}
void UK2Node_AnimGetter::GetMenuActions(FBlueprintActionDatabaseRegistrar& ActionRegistrar) const
{
// First cache the available functions for getters
UClass* ActionKey = GetClass();
const UAnimBlueprint* AnimBlueprint = Cast<UAnimBlueprint>(ActionRegistrar.GetActionKeyFilter());
if(AnimBlueprint && ActionRegistrar.IsOpenForRegistration(AnimBlueprint))
{
UClass* BPClass = (AnimBlueprint) ? *AnimBlueprint->ParentClass : UAnimInstance::StaticClass();
while(BPClass && !BPClass->HasAnyClassFlags(CLASS_Native))
{
BPClass = BPClass->GetSuperClass();
}
if(BPClass)
{
TArray<UFunction*> AnimGetters;
for(TFieldIterator<UFunction> FuncIter(BPClass) ; FuncIter ; ++FuncIter)
{
UFunction* Func = *FuncIter;
if(Func->HasMetaData(TEXT("AnimGetter")) && Func->HasAnyFunctionFlags(FUNC_Native))
{
AnimGetters.Add(Func);
}
}
auto UiSpecOverride = [](const FBlueprintActionContext& /*Context*/, const IBlueprintNodeBinder::FBindingSet& Bindings, FBlueprintActionUiSpec* UiSpecOut, FText Title)
{
UiSpecOut->MenuName = Title;
};
TArray<UAnimGraphNode_AssetPlayerBase*> AssetPlayerNodes;
TArray<UAnimGraphNode_StateMachine*> MachineNodes;
TArray<UAnimStateNode*> StateNodes;
TArray<UAnimStateTransitionNode*> TransitionNodes;
FBlueprintEditorUtils::GetAllNodesOfClass(AnimBlueprint, AssetPlayerNodes);
FBlueprintEditorUtils::GetAllNodesOfClass(AnimBlueprint, MachineNodes);
FBlueprintEditorUtils::GetAllNodesOfClass(AnimBlueprint, StateNodes);
FBlueprintEditorUtils::GetAllNodesOfClass(AnimBlueprint, TransitionNodes);
for(UFunction* Getter : AnimGetters)
{
FNodeSpawnData Params;
Params.AnimInstanceClass = BPClass;
Params.Getter = Getter;
Params.SourceBlueprint = AnimBlueprint;
Params.GetterContextString = Getter->GetMetaData(TEXT("GetterContext"));
if(GetterRequiresParameter(Getter, TEXT("AssetPlayerIndex")))
{
for(UAnimGraphNode_Base* AssetNode : AssetPlayerNodes)
{
// Should always succeed
if(UAnimationAsset* NodeAsset = AssetNode->GetAnimationAsset())
{
FText Title = FText::Format(LOCTEXT("NodeTitle", "{0} ({1})"), Getter->GetDisplayNameText(), FText::FromString(*NodeAsset->GetName()));
Params.SourceNode = AssetNode;
Params.CachedTitle = Title;
UBlueprintNodeSpawner* Spawner = UBlueprintNodeSpawner::Create(UK2Node_AnimGetter::StaticClass(), /*AssetNode->GetGraph()*/nullptr, UBlueprintNodeSpawner::FCustomizeNodeDelegate::CreateUObject(this, &UK2Node_AnimGetter::PostSpawnNodeSetup, Params));
Spawner->DynamicUiSignatureGetter = UBlueprintNodeSpawner::FUiSpecOverrideDelegate::CreateStatic(UiSpecOverride, Title);
ActionRegistrar.AddBlueprintAction(AnimBlueprint, Spawner);
}
}
}
else if(GetterRequiresParameter(Getter, TEXT("MachineIndex")))
{
if(GetterRequiresParameter(Getter, TEXT("StateIndex")))
{
for(UAnimStateNode* StateNode : StateNodes)
{
// Get the state machine node from the outer chain
UAnimationStateMachineGraph* Graph = Cast<UAnimationStateMachineGraph>(StateNode->GetOuter());
if(Graph)
{
if(UAnimGraphNode_StateMachine* MachineNode = Cast<UAnimGraphNode_StateMachine>(Graph->GetOuter()))
{
Params.SourceNode = MachineNode;
}
}
FText Title = FText::Format(LOCTEXT("NodeTitle", "{0} ({1})"), Getter->GetDisplayNameText(), StateNode->GetNodeTitle(ENodeTitleType::ListView));
Params.SourceStateNode = StateNode;
Params.CachedTitle = Title;
UBlueprintNodeSpawner* Spawner = UBlueprintNodeSpawner::Create(UK2Node_AnimGetter::StaticClass(), /*StateNode->GetGraph()*/nullptr, UBlueprintNodeSpawner::FCustomizeNodeDelegate::CreateUObject(this, &UK2Node_AnimGetter::PostSpawnNodeSetup, Params));
Spawner->DynamicUiSignatureGetter = UBlueprintNodeSpawner::FUiSpecOverrideDelegate::CreateStatic(UiSpecOverride, Title);
ActionRegistrar.AddBlueprintAction(AnimBlueprint, Spawner);
}
}
else if(GetterRequiresParameter(Getter, TEXT("TransitionIndex")))
{
for(UAnimStateTransitionNode* TransitionNode : TransitionNodes)
{
UAnimationStateMachineGraph* Graph = Cast<UAnimationStateMachineGraph>(TransitionNode->GetOuter());
if(Graph)
{
if(UAnimGraphNode_StateMachine* MachineNode = Cast<UAnimGraphNode_StateMachine>(Graph->GetOuter()))
{
Params.SourceNode = MachineNode;
}
}
FText Title = FText::Format(LOCTEXT("NodeTitle", "{0} ({1})"), Getter->GetDisplayNameText(), TransitionNode->GetNodeTitle(ENodeTitleType::ListView));
Params.SourceStateNode = TransitionNode;
Params.CachedTitle = Title;
UBlueprintNodeSpawner* Spawner = UBlueprintNodeSpawner::Create(UK2Node_AnimGetter::StaticClass(), /*TransitionNode->GetGraph()*/nullptr, UBlueprintNodeSpawner::FCustomizeNodeDelegate::CreateUObject(this, &UK2Node_AnimGetter::PostSpawnNodeSetup, Params));
Spawner->DynamicUiSignatureGetter = UBlueprintNodeSpawner::FUiSpecOverrideDelegate::CreateStatic(UiSpecOverride, Title);
ActionRegistrar.AddBlueprintAction(AnimBlueprint, Spawner);
}
}
else
{
// Only requires the state machine
for(UAnimGraphNode_StateMachine* MachineNode : MachineNodes)
{
FText Title = FText::Format(LOCTEXT("NodeTitle", "{0} ({1})"), Getter->GetDisplayNameText(), MachineNode->GetNodeTitle(ENodeTitleType::ListView));
Params.SourceNode = MachineNode;
Params.CachedTitle = Title;
UBlueprintNodeSpawner* Spawner = UBlueprintNodeSpawner::Create(UK2Node_AnimGetter::StaticClass(), /*MachineNode*/nullptr, UBlueprintNodeSpawner::FCustomizeNodeDelegate::CreateUObject(this, &UK2Node_AnimGetter::PostSpawnNodeSetup, Params));
Spawner->DynamicUiSignatureGetter = UBlueprintNodeSpawner::FUiSpecOverrideDelegate::CreateStatic(UiSpecOverride, Title);
ActionRegistrar.AddBlueprintAction(AnimBlueprint, Spawner);
}
}
}
else
{
// Doesn't operate on a node, only need one entry
FText Title = FText::Format(LOCTEXT("NodeTitleNoNode", "{0}"), Getter->GetDisplayNameText());
Params.CachedTitle = Title;
UBlueprintNodeSpawner* Spawner = UBlueprintNodeSpawner::Create(UK2Node_AnimGetter::StaticClass(), nullptr, UBlueprintNodeSpawner::FCustomizeNodeDelegate::CreateUObject(this, &UK2Node_AnimGetter::PostSpawnNodeSetup, Params));
Spawner->DynamicUiSignatureGetter = UBlueprintNodeSpawner::FUiSpecOverrideDelegate::CreateStatic(UiSpecOverride, Title);
ActionRegistrar.AddBlueprintAction(AnimBlueprint, Spawner);
}
}
}
}
}
bool UK2Node_AnimGetter::IsActionFilteredOut(FBlueprintActionFilter const& Filter)
{
if(Filter.Context.Graphs.Num() > 0)
{
if(Filter.Context.Blueprints.Num() > 0)
{
UAnimBlueprint* AnimBlueprint = Cast<UAnimBlueprint>(Filter.Context.Blueprints[0]);
check(AnimBlueprint);
if(SourceAnimBlueprint == AnimBlueprint)
{
// Get the native anim instance derived class
UClass* NativeInstanceClass = AnimBlueprint->ParentClass;
while(NativeInstanceClass && !NativeInstanceClass->HasAnyClassFlags(CLASS_Native))
{
NativeInstanceClass = NativeInstanceClass->GetSuperClass();
}
if(GetterClass != NativeInstanceClass)
{
// If the anim instance containing the getter is not the class we're currently using then bail
return true;
}
const UEdGraphSchema* Schema = Filter.Context.Graphs[0]->GetSchema();
// Bail if we aren't meant for this graph at all
if(!IsContextValidForSchema(Schema))
{
return true;
}
if(Cast<UAnimationTransitionSchema>(Schema) || Cast<UAnimationCustomTransitionSchema>(Schema))
{
if(!SourceNode && !SourceStateNode)
{
// No dependancies, always allow
return false;
}
// Inside a transition graph
if(SourceNode)
{
if(UAnimBlueprintGeneratedClass* BPSkelClass = AnimBlueprint->GetAnimBlueprintSkeletonClass())
{
auto GetTransitionNodeFromGraphLambda = [](const FAnimBlueprintDebugData& DebugData, const UEdGraph* Graph) -> UAnimStateTransitionNode*
{
if(const TWeakObjectPtr<UAnimStateTransitionNode>* TransNodePtr = DebugData.TransitionGraphToNodeMap.Find(Graph))
{
return TransNodePtr->Get();
}
if(const TWeakObjectPtr<UAnimStateTransitionNode>* TransNodePtr = DebugData.TransitionBlendGraphToNodeMap.Find(Graph))
{
return TransNodePtr->Get();
}
return NULL;
};
if(UAnimStateTransitionNode* TransitionNode = GetTransitionNodeFromGraphLambda(BPSkelClass->GetAnimBlueprintDebugData(), Filter.Context.Graphs[0]))
{
if(SourceStateNode)
{
if(UAnimStateTransitionNode* SourceTransitionNode = Cast<UAnimStateTransitionNode>(SourceStateNode))
{
// if we have a transition node, make sure it's the same as the one we're in
if(SourceTransitionNode == TransitionNode)
{
return false;
}
}
else if(UAnimStateNode* PreviousStateNode = Cast<UAnimStateNode>(TransitionNode->GetPreviousState()))
{
// Only allow actions using states that are referencing the previous state
if(SourceStateNode == PreviousStateNode)
{
return false;
}
}
}
else if(UAnimGraphNode_StateMachine* MachineNode = Cast<UAnimGraphNode_StateMachine>(SourceNode))
{
// Available everywhere
return false;
}
else if(UAnimStateNode* PrevStateNode = Cast<UAnimStateNode>(TransitionNode->GetPreviousState()))
{
// Make sure the attached asset node is in the source graph
if(SourceNode && SourceNode->GetGraph() == PrevStateNode->BoundGraph)
{
return false;
}
}
}
}
}
}
else if(Cast<UAnimationGraphSchema>(Schema))
{
// Inside normal anim graph
if(SourceStateNode)
{
for(UBlueprint* Blueprint : Filter.Context.Blueprints)
{
TArray<UAnimStateNode*> StateNodes;
FBlueprintEditorUtils::GetAllNodesOfClass(Blueprint, StateNodes);
if(StateNodes.Contains(SourceStateNode))
{
return false;
}
}
}
}
}
}
}
return true;
}
bool UK2Node_AnimGetter::GetterRequiresParameter(const UFunction* Getter, FString ParamName) const
{
bool bRequiresParameter = false;
for(TFieldIterator<UProperty> PropIt(Getter); PropIt && (PropIt->PropertyFlags & CPF_Parm); ++PropIt)
{
UProperty* Prop = *PropIt;
if(Prop->GetName() == ParamName)
{
bRequiresParameter = true;
break;
}
}
return bRequiresParameter;
}
void UK2Node_AnimGetter::PostSpawnNodeSetup(UEdGraphNode* NewNode, bool bIsTemplateNode, FNodeSpawnData SpawnData)
{
UK2Node_AnimGetter* TypedNode = CastChecked<UK2Node_AnimGetter>(NewNode);
// Apply parameters
TypedNode->SourceNode = SpawnData.SourceNode;
TypedNode->SourceStateNode = SpawnData.SourceStateNode;
TypedNode->GetterClass = SpawnData.AnimInstanceClass;
TypedNode->SourceAnimBlueprint = SpawnData.SourceBlueprint;
TypedNode->SetFromFunction((UFunction*)SpawnData.Getter);
TypedNode->CachedTitle = SpawnData.CachedTitle;
SpawnData.GetterContextString.ParseIntoArray(TypedNode->Contexts, TEXT("|"), 1);
}
bool UK2Node_AnimGetter::IsContextValidForSchema(const UEdGraphSchema* Schema) const
{
if(Contexts.Num() == 0)
{
// Valid in all graphs
return true;
}
for(const FString& Context : Contexts)
{
UClass* ClassToCheck = nullptr;
if(Context == TEXT("CustomBlend"))
{
ClassToCheck = UAnimationCustomTransitionSchema::StaticClass();
}
if(Context == TEXT("Transition"))
{
ClassToCheck = UAnimationTransitionSchema::StaticClass();
}
if(Context == TEXT("AnimGraph"))
{
ClassToCheck = UAnimationGraphSchema::StaticClass();
}
return Schema->GetClass() == ClassToCheck;
}
return false;
}
FNodeSpawnData::FNodeSpawnData()
: SourceNode(nullptr)
, SourceStateNode(nullptr)
, AnimInstanceClass(nullptr)
, SourceBlueprint(nullptr)
, Getter(nullptr)
{
}
#undef LOCTEXT_NAMESPACE