Files
UnrealEngineUWP/Engine/Source/Runtime/UMG/Private/Components/InputKeySelector.cpp
Chris Gagnon 930e33cb48 Copying //UE4/Dev-Editor to Dev-Main (//UE4/Dev-Main) for 4.23 From CL 6837861
#rb none

[CL 6838042 by Chris Gagnon in Main branch]
2019-06-04 15:42:48 -04:00

206 lines
6.2 KiB
C++

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "Components/InputKeySelector.h"
#include "Engine/Font.h"
#include "UObject/ConstructorHelpers.h"
#include "UObject/FrameworkObjectVersion.h"
#include "Widgets/DeclarativeSyntaxSupport.h"
#include "Widgets/Input/SInputKeySelector.h"
#include "Internationalization/Internationalization.h"
#define LOCTEXT_NAMESPACE "UMG"
static FButtonStyle* DefaultInputKeySelectorButtonStyle = nullptr;
static FTextBlockStyle* DefaultInputKeySelectorTextStyle = nullptr;
UInputKeySelector::UInputKeySelector( const FObjectInitializer& ObjectInitializer )
: Super(ObjectInitializer)
{
if (DefaultInputKeySelectorButtonStyle == nullptr)
{
// HACK: THIS SHOULD NOT COME FROM CORESTYLE AND SHOULD INSTEAD BE DEFINED BY ENGINE TEXTURES/PROJECT SETTINGS
DefaultInputKeySelectorButtonStyle = new FButtonStyle(FCoreStyle::Get().GetWidgetStyle<FButtonStyle>("Button"));
// Unlink UMG default colors from the editor settings colors.
DefaultInputKeySelectorButtonStyle->UnlinkColors();
}
if (DefaultInputKeySelectorTextStyle == nullptr)
{
// HACK: THIS SHOULD NOT COME FROM CORESTYLE AND SHOULD INSTEAD BE DEFINED BY ENGINE TEXTURES/PROJECT SETTINGS
DefaultInputKeySelectorTextStyle = new FTextBlockStyle(FCoreStyle::Get().GetWidgetStyle<FTextBlockStyle>("NormalText"));
// Unlink UMG default colors from the editor settings colors.
DefaultInputKeySelectorTextStyle->UnlinkColors();
}
WidgetStyle = *DefaultInputKeySelectorButtonStyle;
TextStyle = *DefaultInputKeySelectorTextStyle;
KeySelectionText = NSLOCTEXT("InputKeySelector", "DefaultKeySelectionText", "...");
NoKeySpecifiedText = NSLOCTEXT("InputKeySelector", "DefaultEmptyText", "Empty");
SelectedKey = FInputChord(EKeys::Invalid);
bAllowModifierKeys = true;
bAllowGamepadKeys = false;
EscapeKeys.AddUnique(EKeys::Gamepad_Special_Right); // In most (if not all) cases this is going to be the menu button
if (!IsRunningDedicatedServer())
{
static ConstructorHelpers::FObjectFinder<UFont> RobotoFontObj(*UWidget::GetDefaultFontName());
TextStyle.Font = FSlateFontInfo(RobotoFontObj.Object, 24, FName("Bold"));
}
}
void UInputKeySelector::Serialize(FArchive& Ar)
{
Super::Serialize(Ar);
Ar.UsingCustomVersion(FFrameworkObjectVersion::GUID);
}
void UInputKeySelector::PostLoad()
{
Super::PostLoad();
if (GetLinkerCustomVersion(FFrameworkObjectVersion::GUID) < FFrameworkObjectVersion::InputKeySelectorTextStyle)
{
TextStyle.Font = Font_DEPRECATED;
TextStyle.ColorAndOpacity = ColorAndOpacity_DEPRECATED;
}
}
void UInputKeySelector::SetSelectedKey( const FInputChord& InSelectedKey )
{
if ( MyInputKeySelector.IsValid() )
{
MyInputKeySelector->SetSelectedKey( InSelectedKey );
}
SelectedKey = InSelectedKey;
}
void UInputKeySelector::SetKeySelectionText( FText InKeySelectionText )
{
if ( MyInputKeySelector.IsValid() )
{
MyInputKeySelector->SetKeySelectionText( InKeySelectionText );
}
KeySelectionText = MoveTemp(InKeySelectionText);
}
void UInputKeySelector::SetNoKeySpecifiedText(FText InNoKeySpecifiedText)
{
if (MyInputKeySelector.IsValid())
{
MyInputKeySelector->SetNoKeySpecifiedText(InNoKeySpecifiedText);
}
NoKeySpecifiedText = MoveTemp(InNoKeySpecifiedText);
}
void UInputKeySelector::SetAllowModifierKeys( const bool bInAllowModifierKeys )
{
if ( MyInputKeySelector.IsValid() )
{
MyInputKeySelector->SetAllowModifierKeys( bInAllowModifierKeys );
}
bAllowModifierKeys = bInAllowModifierKeys;
}
void UInputKeySelector::SetAllowGamepadKeys(const bool bInAllowGamepadKeys)
{
if (MyInputKeySelector.IsValid())
{
MyInputKeySelector->SetAllowGamepadKeys(bInAllowGamepadKeys);
}
bAllowGamepadKeys = bInAllowGamepadKeys;
}
bool UInputKeySelector::GetIsSelectingKey() const
{
return MyInputKeySelector.IsValid() ? MyInputKeySelector->GetIsSelectingKey() : false;
}
void UInputKeySelector::SetButtonStyle( const FButtonStyle* InButtonStyle )
{
if ( MyInputKeySelector.IsValid() )
{
MyInputKeySelector->SetButtonStyle(InButtonStyle);
}
WidgetStyle = *InButtonStyle;
}
void UInputKeySelector::SetEscapeKeys(const TArray<FKey>& InKeys)
{
if (MyInputKeySelector.IsValid())
{
MyInputKeySelector->SetEscapeKeys(InKeys);
}
EscapeKeys = InKeys;
}
#if WITH_EDITOR
const FText UInputKeySelector::GetPaletteCategory()
{
return LOCTEXT("Advanced", "Advanced");
}
#endif
void UInputKeySelector::SynchronizeProperties()
{
Super::SynchronizeProperties();
MyInputKeySelector->SetSelectedKey( SelectedKey );
MyInputKeySelector->SetMargin( Margin );
MyInputKeySelector->SetButtonStyle( &WidgetStyle );
MyInputKeySelector->SetTextStyle( &TextStyle );
MyInputKeySelector->SetKeySelectionText( KeySelectionText );
MyInputKeySelector->SetNoKeySpecifiedText(NoKeySpecifiedText);
MyInputKeySelector->SetAllowModifierKeys( bAllowModifierKeys );
MyInputKeySelector->SetAllowGamepadKeys(bAllowGamepadKeys);
MyInputKeySelector->SetEscapeKeys(EscapeKeys);
}
void UInputKeySelector::ReleaseSlateResources(bool bReleaseChildren)
{
Super::ReleaseSlateResources(bReleaseChildren);
MyInputKeySelector.Reset();
}
TSharedRef<SWidget> UInputKeySelector::RebuildWidget()
{
MyInputKeySelector = SNew(SInputKeySelector)
.SelectedKey(SelectedKey)
.Margin(Margin)
.ButtonStyle(&WidgetStyle)
.TextStyle(&TextStyle)
.KeySelectionText(KeySelectionText)
.NoKeySpecifiedText(NoKeySpecifiedText)
.AllowModifierKeys(bAllowModifierKeys)
.AllowGamepadKeys(bAllowGamepadKeys)
.EscapeKeys(EscapeKeys)
.OnKeySelected( BIND_UOBJECT_DELEGATE( SInputKeySelector::FOnKeySelected, HandleKeySelected ) )
.OnIsSelectingKeyChanged( BIND_UOBJECT_DELEGATE( SInputKeySelector::FOnIsSelectingKeyChanged, HandleIsSelectingKeyChanged ) );
return MyInputKeySelector.ToSharedRef();
}
void UInputKeySelector::HandleKeySelected(const FInputChord& InSelectedKey)
{
SelectedKey = InSelectedKey;
OnKeySelected.Broadcast(SelectedKey);
}
void UInputKeySelector::HandleIsSelectingKeyChanged()
{
OnIsSelectingKeyChanged.Broadcast();
}
void UInputKeySelector::SetTextBlockVisibility(const ESlateVisibility InVisibility)
{
if (MyInputKeySelector.IsValid())
{
EVisibility SlateVisibility = UWidget::ConvertSerializedVisibilityToRuntime(InVisibility);
MyInputKeySelector->SetTextBlockVisibility(SlateVisibility);
}
}
#undef LOCTEXT_NAMESPACE