2022-05-23 18:41:30 +00:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
2022-09-13 07:18:28 +00:00
|
|
|
#include "Player/LyraLocalPlayer.h"
|
|
|
|
|
2022-05-23 18:41:30 +00:00
|
|
|
#include "AudioMixerBlueprintLibrary.h"
|
2022-09-13 07:18:28 +00:00
|
|
|
#include "Delegates/Delegate.h"
|
|
|
|
#include "Engine/World.h"
|
2022-05-23 18:41:30 +00:00
|
|
|
#include "GameFramework/PlayerController.h"
|
2022-09-13 07:18:28 +00:00
|
|
|
#include "Settings/LyraSettingsLocal.h"
|
|
|
|
#include "Settings/LyraSettingsShared.h"
|
|
|
|
#include "Templates/Casts.h"
|
|
|
|
#include "UObject/NameTypes.h"
|
|
|
|
#include "UObject/ObjectPtr.h"
|
|
|
|
#include "UObject/WeakObjectPtr.h"
|
|
|
|
|
|
|
|
class UObject;
|
2022-05-23 18:41:30 +00:00
|
|
|
|
|
|
|
ULyraLocalPlayer::ULyraLocalPlayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::PostInitProperties()
|
|
|
|
{
|
|
|
|
Super::PostInitProperties();
|
|
|
|
|
|
|
|
if (ULyraSettingsLocal* LocalSettings = GetLocalSettings())
|
|
|
|
{
|
|
|
|
LocalSettings->OnAudioOutputDeviceChanged.AddUObject(this, &ULyraLocalPlayer::OnAudioOutputDeviceChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::SwitchController(class APlayerController* PC)
|
|
|
|
{
|
|
|
|
Super::SwitchController(PC);
|
|
|
|
|
|
|
|
OnPlayerControllerChanged(PlayerController);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ULyraLocalPlayer::SpawnPlayActor(const FString& URL, FString& OutError, UWorld* InWorld)
|
|
|
|
{
|
|
|
|
const bool bResult = Super::SpawnPlayActor(URL, OutError, InWorld);
|
|
|
|
|
|
|
|
OnPlayerControllerChanged(PlayerController);
|
|
|
|
|
|
|
|
return bResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::InitOnlineSession()
|
|
|
|
{
|
|
|
|
OnPlayerControllerChanged(PlayerController);
|
|
|
|
|
|
|
|
Super::InitOnlineSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::OnPlayerControllerChanged(APlayerController* NewController)
|
|
|
|
{
|
|
|
|
// Stop listening for changes from the old controller
|
|
|
|
FGenericTeamId OldTeamID = FGenericTeamId::NoTeam;
|
|
|
|
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(LastBoundPC.Get()))
|
|
|
|
{
|
|
|
|
OldTeamID = ControllerAsTeamProvider->GetGenericTeamId();
|
|
|
|
ControllerAsTeamProvider->GetTeamChangedDelegateChecked().RemoveAll(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the current team ID and listen for future changes
|
|
|
|
FGenericTeamId NewTeamID = FGenericTeamId::NoTeam;
|
|
|
|
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(NewController))
|
|
|
|
{
|
|
|
|
NewTeamID = ControllerAsTeamProvider->GetGenericTeamId();
|
|
|
|
ControllerAsTeamProvider->GetTeamChangedDelegateChecked().AddDynamic(this, &ThisClass::OnControllerChangedTeam);
|
|
|
|
LastBoundPC = NewController;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConditionalBroadcastTeamChanged(this, OldTeamID, NewTeamID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::SetGenericTeamId(const FGenericTeamId& NewTeamID)
|
|
|
|
{
|
|
|
|
// Do nothing, we merely observe the team of our associated player controller
|
|
|
|
}
|
|
|
|
|
|
|
|
FGenericTeamId ULyraLocalPlayer::GetGenericTeamId() const
|
|
|
|
{
|
|
|
|
if (ILyraTeamAgentInterface* ControllerAsTeamProvider = Cast<ILyraTeamAgentInterface>(PlayerController))
|
|
|
|
{
|
|
|
|
return ControllerAsTeamProvider->GetGenericTeamId();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FGenericTeamId::NoTeam;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FOnLyraTeamIndexChangedDelegate* ULyraLocalPlayer::GetOnTeamIndexChangedDelegate()
|
|
|
|
{
|
|
|
|
return &OnTeamChangedDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
ULyraSettingsLocal* ULyraLocalPlayer::GetLocalSettings() const
|
|
|
|
{
|
|
|
|
return ULyraSettingsLocal::Get();
|
|
|
|
}
|
|
|
|
|
|
|
|
ULyraSettingsShared* ULyraLocalPlayer::GetSharedSettings() const
|
|
|
|
{
|
|
|
|
if (!SharedSettings)
|
|
|
|
{
|
|
|
|
SharedSettings = ULyraSettingsShared::LoadOrCreateSettings(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SharedSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::OnAudioOutputDeviceChanged(const FString& InAudioOutputDeviceId)
|
|
|
|
{
|
|
|
|
FOnCompletedDeviceSwap DevicesSwappedCallback;
|
|
|
|
DevicesSwappedCallback.BindUFunction(this, FName("OnCompletedAudioDeviceSwap"));
|
|
|
|
UAudioMixerBlueprintLibrary::SwapAudioOutputDevice(GetWorld(), InAudioOutputDeviceId, DevicesSwappedCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::OnCompletedAudioDeviceSwap(const FSwapAudioOutputResult& SwapResult)
|
|
|
|
{
|
|
|
|
if (SwapResult.Result == ESwapAudioOutputDeviceResultState::Failure)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ULyraLocalPlayer::OnControllerChangedTeam(UObject* TeamAgent, int32 OldTeam, int32 NewTeam)
|
|
|
|
{
|
|
|
|
ConditionalBroadcastTeamChanged(this, IntegerToGenericTeamId(OldTeam), IntegerToGenericTeamId(NewTeam));
|
|
|
|
}
|