|
|
|
|
| #include "LyraHUDLayout.h"
|
|
|
| #include "CommonUIExtensions.h"
|
| #include "CommonUISettings.h"
|
| #include "GameFramework/InputDeviceSubsystem.h"
|
| #include "GameFramework/InputSettings.h"
|
| #include "GenericPlatform/GenericPlatformInputDeviceMapper.h"
|
| #include "Input/CommonUIInputTypes.h"
|
| #include "ICommonUIModule.h"
|
| #include "LyraLogChannels.h"
|
| #include "NativeGameplayTags.h"
|
| #include "UI/Foundation/LyraControllerDisconnectedScreen.h"
|
| #include "UI/LyraActivatableWidget.h"
|
|
|
| #if WITH_EDITOR
|
| #include "CommonUIVisibilitySubsystem.h"
|
| #endif
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraHUDLayout)
|
|
|
| UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_UI_LAYER_MENU, "UI.Layer.Menu");
|
| UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_UI_ACTION_ESCAPE, "UI.Action.Escape");
|
| UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_Platform_Trait_Input_PrimarlyController, "Platform.Trait.Input.PrimarlyController");
|
|
|
| ULyraHUDLayout::ULyraHUDLayout(const FObjectInitializer& ObjectInitializer)
|
| : Super(ObjectInitializer)
|
| , SpawnedControllerDisconnectScreen(nullptr)
|
| {
|
|
|
| PlatformRequiresControllerDisconnectScreen.AddTag(TAG_Platform_Trait_Input_PrimarlyController);
|
| }
|
|
|
| void ULyraHUDLayout::NativeOnInitialized()
|
| {
|
| Super::NativeOnInitialized();
|
|
|
| RegisterUIActionBinding(FBindUIActionArgs(FUIActionTag::ConvertChecked(TAG_UI_ACTION_ESCAPE), false, FSimpleDelegate::CreateUObject(this, &ThisClass::HandleEscapeAction)));
|
|
|
|
|
| if (ShouldPlatformDisplayControllerDisconnectScreen())
|
| {
|
|
|
| IPlatformInputDeviceMapper& DeviceMapper = IPlatformInputDeviceMapper::Get();
|
| DeviceMapper.GetOnInputDeviceConnectionChange().AddUObject(this, &ThisClass::HandleInputDeviceConnectionChanged);
|
| DeviceMapper.GetOnInputDevicePairingChange().AddUObject(this, &ThisClass::HandleInputDevicePairingChanged);
|
| }
|
| }
|
|
|
| void ULyraHUDLayout::NativeDestruct()
|
| {
|
| Super::NativeDestruct();
|
|
|
|
|
| IPlatformInputDeviceMapper& DeviceMapper = IPlatformInputDeviceMapper::Get();
|
| DeviceMapper.GetOnInputDeviceConnectionChange().RemoveAll(this);
|
| DeviceMapper.GetOnInputDevicePairingChange().RemoveAll(this);
|
|
|
| if (RequestProcessControllerStateHandle.IsValid())
|
| {
|
| FTSTicker::GetCoreTicker().RemoveTicker(RequestProcessControllerStateHandle);
|
| RequestProcessControllerStateHandle.Reset();
|
| }
|
| }
|
|
|
| void ULyraHUDLayout::HandleEscapeAction()
|
| {
|
| if (ensure(!EscapeMenuClass.IsNull()))
|
| {
|
| UCommonUIExtensions::PushStreamedContentToLayer_ForPlayer(GetOwningLocalPlayer(), TAG_UI_LAYER_MENU, EscapeMenuClass);
|
| }
|
| }
|
|
|
| void ULyraHUDLayout::HandleInputDeviceConnectionChanged(EInputDeviceConnectionState NewConnectionState, FPlatformUserId PlatformUserId, FInputDeviceId InputDeviceId)
|
| {
|
| const FPlatformUserId OwningLocalPlayerId = GetOwningLocalPlayer()->GetPlatformUserId();
|
|
|
| ensure(OwningLocalPlayerId.IsValid());
|
|
|
|
|
| if (PlatformUserId != OwningLocalPlayerId)
|
| {
|
| return;
|
| }
|
|
|
| NotifyControllerStateChangeForDisconnectScreen();
|
| }
|
|
|
| void ULyraHUDLayout::HandleInputDevicePairingChanged(FInputDeviceId InputDeviceId, FPlatformUserId NewUserPlatformId, FPlatformUserId OldUserPlatformId)
|
| {
|
| const FPlatformUserId OwningLocalPlayerId = GetOwningLocalPlayer()->GetPlatformUserId();
|
|
|
| ensure(OwningLocalPlayerId.IsValid());
|
|
|
|
|
| if (NewUserPlatformId == OwningLocalPlayerId || OldUserPlatformId == OwningLocalPlayerId)
|
| {
|
| NotifyControllerStateChangeForDisconnectScreen();
|
| }
|
| }
|
|
|
| bool ULyraHUDLayout::ShouldPlatformDisplayControllerDisconnectScreen() const
|
| {
|
|
|
| bool bHasAllRequiredTags = ICommonUIModule::GetSettings().GetPlatformTraits().HasAll(PlatformRequiresControllerDisconnectScreen);
|
|
|
|
|
| #if WITH_EDITOR
|
| const FGameplayTagContainer& PlatformEmulationTags = UCommonUIVisibilitySubsystem::Get(GetOwningLocalPlayer())->GetVisibilityTags();
|
| bHasAllRequiredTags |= PlatformEmulationTags.HasAll(PlatformRequiresControllerDisconnectScreen);
|
| #endif
|
|
|
| return bHasAllRequiredTags;
|
| }
|
|
|
| void ULyraHUDLayout::NotifyControllerStateChangeForDisconnectScreen()
|
| {
|
|
|
| ensure(ShouldPlatformDisplayControllerDisconnectScreen());
|
|
|
|
|
| if (!RequestProcessControllerStateHandle.IsValid())
|
| {
|
| RequestProcessControllerStateHandle = FTSTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateWeakLambda(this, [this](float DeltaTime)
|
| {
|
| RequestProcessControllerStateHandle.Reset();
|
| ProcessControllerDevicesHavingChangedForDisconnectScreen();
|
| return false;
|
| }));
|
| }
|
| }
|
|
|
| void ULyraHUDLayout::ProcessControllerDevicesHavingChangedForDisconnectScreen()
|
| {
|
|
|
| ensure(ShouldPlatformDisplayControllerDisconnectScreen());
|
|
|
| const FPlatformUserId OwningLocalPlayerId = GetOwningLocalPlayer()->GetPlatformUserId();
|
|
|
| ensure(OwningLocalPlayerId.IsValid());
|
|
|
|
|
| const IPlatformInputDeviceMapper& DeviceMapper = IPlatformInputDeviceMapper::Get();
|
| TArray<FInputDeviceId> MappedInputDevices;
|
| const int32 NumDevicesMappedToUser = DeviceMapper.GetAllInputDevicesForUser(OwningLocalPlayerId, OUT MappedInputDevices);
|
|
|
|
|
| bool bHasConnectedController = false;
|
|
|
| for (const FInputDeviceId MappedDevice : MappedInputDevices)
|
| {
|
| if (DeviceMapper.GetInputDeviceConnectionState(MappedDevice) == EInputDeviceConnectionState::Connected)
|
| {
|
| const FHardwareDeviceIdentifier HardwareInfo = UInputDeviceSubsystem::Get()->GetInputDeviceHardwareIdentifier(MappedDevice);
|
| if (HardwareInfo.PrimaryDeviceType == EHardwareDevicePrimaryType::Gamepad)
|
| {
|
| bHasConnectedController = true;
|
| }
|
| }
|
| }
|
|
|
|
|
| if (!bHasConnectedController)
|
| {
|
| DisplayControllerDisconnectedMenu();
|
| }
|
|
|
| else if (SpawnedControllerDisconnectScreen)
|
| {
|
| HideControllerDisconnectedMenu();
|
| }
|
| }
|
|
|
| void ULyraHUDLayout::DisplayControllerDisconnectedMenu_Implementation()
|
| {
|
| UE_LOG(LogLyra, Log, TEXT("[%hs] Display controller disconnected menu!"), __func__);
|
|
|
| if (ControllerDisconnectedScreen)
|
| {
|
|
|
| SpawnedControllerDisconnectScreen = UCommonUIExtensions::PushContentToLayer_ForPlayer(GetOwningLocalPlayer(), TAG_UI_LAYER_MENU, ControllerDisconnectedScreen);
|
| }
|
| }
|
|
|
| void ULyraHUDLayout::HideControllerDisconnectedMenu_Implementation()
|
| {
|
| UE_LOG(LogLyra, Log, TEXT("[%hs] Hide controller disconnected menu!"), __func__);
|
|
|
| UCommonUIExtensions::PopContentFromLayer(SpawnedControllerDisconnectScreen);
|
| SpawnedControllerDisconnectScreen = nullptr;
|
| } |