RealtimeStyleTransferRuntime/Source/LyraGame/Settings/CustomSettings/LyraSettingKeyboardInput.cpp

176 lines
5.5 KiB
C++
Raw Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraSettingKeyboardInput.h"
2022-09-13 07:18:28 +00:00
2022-05-23 18:41:30 +00:00
#include "../LyraSettingsLocal.h"
2022-09-13 07:18:28 +00:00
#include "Containers/UnrealString.h"
#include "Delegates/Delegate.h"
#include "GameSetting.h"
#include "GameSettingFilterState.h"
#include "Internationalization/Internationalization.h"
#include "Misc/AssertionMacros.h"
2022-05-23 18:41:30 +00:00
#include "Player/LyraLocalPlayer.h"
#include "PlayerMappableInputConfig.h"
2022-09-13 07:18:28 +00:00
#include "Templates/Casts.h"
#include "UObject/NameTypes.h"
#include "UObject/UnrealNames.h"
class ULocalPlayer;
2022-05-23 18:41:30 +00:00
#define LOCTEXT_NAMESPACE "LyraSettings"
void FKeyboardOption::ResetToDefault()
{
if (OwningConfig)
{
InputMapping = OwningConfig->GetMappingByName(InputMapping.PlayerMappableOptions.Name);
}
// If we don't have an owning config, then there is no default binding for this and it can simply be removed
else
{
InputMapping = FEnhancedActionKeyMapping();
}
}
void FKeyboardOption::SetInitialValue(FKey InKey)
{
InitialMapping = InKey;
}
ULyraSettingKeyboardInput::ULyraSettingKeyboardInput()
{
bReportAnalytics = false;
}
void ULyraSettingKeyboardInput::OnInitialized()
{
DynamicDetails = FGetGameSettingsDetails::CreateLambda([this](ULocalPlayer&) {
return FText::Format(LOCTEXT("DynamicDetails_KeyboardInputAction", "Bindings for {0}"), FirstMappableOption.InputMapping.PlayerMappableOptions.DisplayName);
});
Super::OnInitialized();
}
void ULyraSettingKeyboardInput::SetInputData(FEnhancedActionKeyMapping& BaseMapping, const UPlayerMappableInputConfig* InOwningConfig, int32 InKeyBindSlot)
{
if (InKeyBindSlot == 0)
{
FirstMappableOption.InputMapping = BaseMapping;
FirstMappableOption.OwningConfig = InOwningConfig;
FirstMappableOption.SetInitialValue(BaseMapping.Key);
}
else if (InKeyBindSlot == 1)
{
SecondaryMappableOption.InputMapping = BaseMapping;
SecondaryMappableOption.OwningConfig = InOwningConfig;
SecondaryMappableOption.SetInitialValue(BaseMapping.Key);
}
else
{
ensureMsgf(false, TEXT("Invalid key bind slot provided!"));
}
ensure(FirstMappableOption.InputMapping.PlayerMappableOptions.Name != NAME_None && !FirstMappableOption.InputMapping.PlayerMappableOptions.DisplayName.IsEmpty());
const FString NameString = TEXT("KBM_Input_") + FirstMappableOption.InputMapping.PlayerMappableOptions.Name.ToString();
SetDevName(*NameString);
SetDisplayName(FirstMappableOption.InputMapping.PlayerMappableOptions.DisplayName);
}
FText ULyraSettingKeyboardInput::GetPrimaryKeyText() const
{
return FirstMappableOption.InputMapping.Key.GetDisplayName();
}
FText ULyraSettingKeyboardInput::GetSecondaryKeyText() const
{
return SecondaryMappableOption.InputMapping.Key.GetDisplayName();
}
void ULyraSettingKeyboardInput::ResetToDefault()
{
// Find the UPlayerMappableInputConfig that this came from and reset it to the value in there
FirstMappableOption.ResetToDefault();
SecondaryMappableOption.ResetToDefault();
}
void ULyraSettingKeyboardInput::StoreInitial()
{
FirstMappableOption.SetInitialValue(FirstMappableOption.InputMapping.Key);
SecondaryMappableOption.SetInitialValue(SecondaryMappableOption.InputMapping.Key);
}
void ULyraSettingKeyboardInput::RestoreToInitial()
{
ChangeBinding(0, FirstMappableOption.GetInitialStoredValue());
ChangeBinding(1, SecondaryMappableOption.GetInitialStoredValue());
}
bool ULyraSettingKeyboardInput::ChangeBinding(int32 InKeyBindSlot, FKey NewKey)
{
// Early out if they hit the same button that is already bound. This allows for them to exit binding if they made a mistake.
if ((InKeyBindSlot == 0 && FirstMappableOption.InputMapping.Key == NewKey) || (InKeyBindSlot == 1 && SecondaryMappableOption.InputMapping.Key == NewKey))
{
return false;
}
if (!NewKey.IsGamepadKey())
{
ULyraLocalPlayer* LyraLocalPlayer = CastChecked<ULyraLocalPlayer>(LocalPlayer);
ULyraSettingsLocal* LocalSettings = LyraLocalPlayer->GetLocalSettings();
if (InKeyBindSlot == 0)
{
LocalSettings->AddOrUpdateCustomKeyboardBindings(FirstMappableOption.InputMapping.PlayerMappableOptions.Name, NewKey, LyraLocalPlayer);
FirstMappableOption.InputMapping.Key = NewKey;
}
else if (InKeyBindSlot == 1)
{
// If there is no default secondary binding then we can create one based off of data from the primary binding
if (SecondaryMappableOption.InputMapping.PlayerMappableOptions.Name == NAME_None)
{
SecondaryMappableOption = FKeyboardOption(FirstMappableOption);
}
LocalSettings->AddOrUpdateCustomKeyboardBindings(SecondaryMappableOption.InputMapping.PlayerMappableOptions.Name, NewKey, LyraLocalPlayer);
SecondaryMappableOption.InputMapping.Key = NewKey;
}
else
{
ensureMsgf(false, TEXT("Invalid key bind slot provided!"));
}
// keybindings are never reset to default or initial
NotifySettingChanged(EGameSettingChangeReason::Change);
return true;
}
return false;
}
2022-09-13 07:18:28 +00:00
void ULyraSettingKeyboardInput::GetAllMappedActionsFromKey(int32 InKeyBindSlot, FKey Key, TArray<FName>& OutActionNames) const
{
if (InKeyBindSlot == 1)
{
if (SecondaryMappableOption.InputMapping.Key == Key)
{
return;
}
}
else
{
if (FirstMappableOption.InputMapping.Key == Key)
{
return;
}
}
if (const ULyraLocalPlayer* LyraLocalPlayer = CastChecked<ULyraLocalPlayer>(LocalPlayer))
{
ULyraSettingsLocal* LocalSettings = LyraLocalPlayer->GetLocalSettings();
LocalSettings->GetAllMappingNamesFromKey(Key, OutActionNames);
}
}
2022-05-23 18:41:30 +00:00
#undef LOCTEXT_NAMESPACE