RealtimeStyleTransferRuntime/Source/LyraGame/Interaction/InteractionStatics.cpp

89 lines
2.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "InteractionStatics.h"
#include "Components/ActorComponent.h"
#include "Components/PrimitiveComponent.h"
#include "Engine/HitResult.h"
#include "Engine/OverlapResult.h"
#include "GameFramework/Actor.h"
#include "IInteractableTarget.h"
#include "Misc/AssertionMacros.h"
#include "Templates/Casts.h"
#include "UObject/Object.h"
UInteractionStatics::UInteractionStatics()
: Super(FObjectInitializer::Get())
{
}
AActor* UInteractionStatics::GetActorFromInteractableTarget(TScriptInterface<IInteractableTarget> InteractableTarget)
{
if (UObject* Object = InteractableTarget.GetObject())
{
if (AActor* Actor = Cast<AActor>(Object))
{
return Actor;
}
else if (UActorComponent* ActorComponent = Cast<UActorComponent>(Object))
{
return ActorComponent->GetOwner();
}
else
{
unimplemented();
}
}
return nullptr;
}
void UInteractionStatics::GetInteractableTargetsFromActor(AActor* Actor, TArray<TScriptInterface<IInteractableTarget>>& OutInteractableTargets)
{
// If the actor is directly interactable, return that.
TScriptInterface<IInteractableTarget> InteractableActor(Actor);
if (InteractableActor)
{
OutInteractableTargets.Add(InteractableActor);
}
// If the actor isn't interactable, it might have a component that has a interactable interface.
TArray<UActorComponent*> InteractableComponents = Actor ? Actor->GetComponentsByInterface(UInteractableTarget::StaticClass()) : TArray<UActorComponent*>();
for (UActorComponent* InteractableComponent : InteractableComponents)
{
OutInteractableTargets.Add(TScriptInterface<IInteractableTarget>(InteractableComponent));
}
}
void UInteractionStatics::AppendInteractableTargetsFromOverlapResults(const TArray<FOverlapResult>& OverlapResults, TArray<TScriptInterface<IInteractableTarget>>& OutInteractableTargets)
{
for (const FOverlapResult& Overlap : OverlapResults)
{
TScriptInterface<IInteractableTarget> InteractableActor(Overlap.GetActor());
if (InteractableActor)
{
OutInteractableTargets.AddUnique(InteractableActor);
}
TScriptInterface<IInteractableTarget> InteractableComponent(Overlap.GetComponent());
if (InteractableComponent)
{
OutInteractableTargets.AddUnique(InteractableComponent);
}
}
}
void UInteractionStatics::AppendInteractableTargetsFromHitResult(const FHitResult& HitResult, TArray<TScriptInterface<IInteractableTarget>>& OutInteractableTargets)
{
TScriptInterface<IInteractableTarget> InteractableActor(HitResult.GetActor());
if (InteractableActor)
{
OutInteractableTargets.AddUnique(InteractableActor);
}
TScriptInterface<IInteractableTarget> InteractableComponent(HitResult.GetComponent());
if (InteractableComponent)
{
OutInteractableTargets.AddUnique(InteractableComponent);
}
}