File size: 7,621 Bytes
7fd553e | 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | // Copyright Epic Games, Inc. All Rights Reserved.
#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 // WITH_EDITOR
#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)
{
// By default, only primarily controller platforms require a disconnect screen.
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 we can display a controller disconnect screen, then listen for the controller state change delegates
if (ShouldPlatformDisplayControllerDisconnectScreen())
{
// Bind to when input device connections change
IPlatformInputDeviceMapper& DeviceMapper = IPlatformInputDeviceMapper::Get();
DeviceMapper.GetOnInputDeviceConnectionChange().AddUObject(this, &ThisClass::HandleInputDeviceConnectionChanged);
DeviceMapper.GetOnInputDevicePairingChange().AddUObject(this, &ThisClass::HandleInputDevicePairingChanged);
}
}
void ULyraHUDLayout::NativeDestruct()
{
Super::NativeDestruct();
// Remove bindings to input device connection changing
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());
// This device connection change happened to a different player, ignore it for us.
if (PlatformUserId != OwningLocalPlayerId)
{
return;
}
NotifyControllerStateChangeForDisconnectScreen();
}
void ULyraHUDLayout::HandleInputDevicePairingChanged(FInputDeviceId InputDeviceId, FPlatformUserId NewUserPlatformId, FPlatformUserId OldUserPlatformId)
{
const FPlatformUserId OwningLocalPlayerId = GetOwningLocalPlayer()->GetPlatformUserId();
ensure(OwningLocalPlayerId.IsValid());
// If this pairing change was related to our local player, notify of a change.
if (NewUserPlatformId == OwningLocalPlayerId || OldUserPlatformId == OwningLocalPlayerId)
{
NotifyControllerStateChangeForDisconnectScreen();
}
}
bool ULyraHUDLayout::ShouldPlatformDisplayControllerDisconnectScreen() const
{
// We only want this menu on primarily controller platforms
bool bHasAllRequiredTags = ICommonUIModule::GetSettings().GetPlatformTraits().HasAll(PlatformRequiresControllerDisconnectScreen);
// Check the tags that we may be emulating in the editor too
#if WITH_EDITOR
const FGameplayTagContainer& PlatformEmulationTags = UCommonUIVisibilitySubsystem::Get(GetOwningLocalPlayer())->GetVisibilityTags();
bHasAllRequiredTags |= PlatformEmulationTags.HasAll(PlatformRequiresControllerDisconnectScreen);
#endif // WITH_EDITOR
return bHasAllRequiredTags;
}
void ULyraHUDLayout::NotifyControllerStateChangeForDisconnectScreen()
{
// We should only ever get here if we have bound to the controller state change delegates
ensure(ShouldPlatformDisplayControllerDisconnectScreen());
// If we haven't already, queue the processing of device state for next tick.
if (!RequestProcessControllerStateHandle.IsValid())
{
RequestProcessControllerStateHandle = FTSTicker::GetCoreTicker().AddTicker(FTickerDelegate::CreateWeakLambda(this, [this](float DeltaTime)
{
RequestProcessControllerStateHandle.Reset();
ProcessControllerDevicesHavingChangedForDisconnectScreen();
return false;
}));
}
}
void ULyraHUDLayout::ProcessControllerDevicesHavingChangedForDisconnectScreen()
{
// We should only ever get here if we have bound to the controller state change delegates
ensure(ShouldPlatformDisplayControllerDisconnectScreen());
const FPlatformUserId OwningLocalPlayerId = GetOwningLocalPlayer()->GetPlatformUserId();
ensure(OwningLocalPlayerId.IsValid());
// Get all input devices mapped to our player
const IPlatformInputDeviceMapper& DeviceMapper = IPlatformInputDeviceMapper::Get();
TArray<FInputDeviceId> MappedInputDevices;
const int32 NumDevicesMappedToUser = DeviceMapper.GetAllInputDevicesForUser(OwningLocalPlayerId, OUT MappedInputDevices);
// Check if there are any other connected GAMEPAD devices mapped to this platform user.
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 there are no gamepad input devices mapped to this user, then we want to pop the toast saying to re-connect them
if (!bHasConnectedController)
{
DisplayControllerDisconnectedMenu();
}
// Otherwise we can hide the screen if it is currently being shown
else if (SpawnedControllerDisconnectScreen)
{
HideControllerDisconnectedMenu();
}
}
void ULyraHUDLayout::DisplayControllerDisconnectedMenu_Implementation()
{
UE_LOG(LogLyra, Log, TEXT("[%hs] Display controller disconnected menu!"), __func__);
if (ControllerDisconnectedScreen)
{
// Push the "controller disconnected" widget to the menu layer
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;
} |