File size: 2,946 Bytes
b315adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright Epic Games, Inc. All Rights Reserved.

#include "ShooterTestsAnimationTestHelper.h"

#include "Animation/AnimInstance.h"
#include "Animation/AnimInstanceProxy.h"
#include "Animation/AnimMontage.h"
#include "Components/SkeletalMeshComponent.h"
#include "Engine/SkeletalMesh.h"
#include "UObject/UObjectIterator.h"

namespace
{

bool IsExpectedAnimationPlaying(const UAnimationAsset* ExpectedAnimation, const TArray<FAnimTickRecord>& Records)

{
	for (int32 PlayerIndex = 0; PlayerIndex < Records.Num(); ++PlayerIndex)
	{
		const FAnimTickRecord& TickRecord = Records[PlayerIndex];
		if (TickRecord.SourceAsset == ExpectedAnimation)
		{
			return true;
		}
	}

	return false;
}

} //anonymous

UAnimationAsset* FShooterTestsAnimationTestHelper::FindAnimationAsset(USkeletalMeshComponent* SkeletalMeshComponent, const FString& AnimationName)

{
	check(SkeletalMeshComponent);

	if (const USkeletalMesh* SkeletalMesh = SkeletalMeshComponent->GetSkeletalMeshAsset()) 
	{
		if (const USkeleton* Skeleton = SkeletalMesh->GetSkeleton()) 
		{
			for (TObjectIterator<UAnimationAsset> Itr; Itr; ++Itr) 
			{
				UAnimationAsset* AnimationAsset = (*Itr);
				if (!IsValid(AnimationAsset)) 
				{
					continue;
				}

				if (Skeleton == AnimationAsset->GetSkeleton() && AnimationAsset->GetName().Equals(AnimationName)) 
				{
					return AnimationAsset;
				}
			}
		}
	}

	return nullptr;
}

bool FShooterTestsAnimationTestHelper::IsAnimationPlaying(USkeletalMeshComponent* SkeletalMeshComponent, const UAnimationAsset* ExpectedAnimation)

{
	check(SkeletalMeshComponent);
	
	bool bIsAnimationPlaying = false;
	SkeletalMeshComponent->ForEachAnimInstance([&ExpectedAnimation, &bIsAnimationPlaying](UAnimInstance* AnimInstance)
		{
			// Early out if we found our animation from a prior AnimInstance
			if (bIsAnimationPlaying)
			{
				return;
			}

			FAnimMontageInstance* AnimMontageInstance = AnimInstance->GetActiveMontageInstance();
			if (AnimMontageInstance && AnimMontageInstance->IsPlaying() && IsValid(AnimMontageInstance->Montage))
			{
				bIsAnimationPlaying = AnimMontageInstance->Montage == ExpectedAnimation;
			}
			else
			{
				const FAnimInstanceProxy::FSyncGroupMap& SyncGroupMap = AnimInstance->GetSyncGroupMapRead();
				const TArray<FAnimTickRecord>& UngroupedActivePlayers = AnimInstance->GetUngroupedActivePlayersRead();
				for (const auto& SyncGroupPair : SyncGroupMap)
				{
					const FAnimGroupInstance& SyncGroup = SyncGroupPair.Value;
					if (SyncGroup.ActivePlayers.Num() > 0)
					{
						if (SyncGroup.GroupLeaderIndex != -1)
						{
							bIsAnimationPlaying = IsExpectedAnimationPlaying(ExpectedAnimation, SyncGroup.ActivePlayers);
							return;
						}
					}
				}

				bIsAnimationPlaying = IsExpectedAnimationPlaying(ExpectedAnimation, UngroupedActivePlayers);
			}
		});

	return bIsAnimationPlaying;
}