More progress on prompt
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
#include "PromptWidget.h"
|
||||
#include "UtilityLibrary.h"
|
||||
#include "PlayerControllerBase.h"
|
||||
#include "Engine/Engine.h"
|
||||
#include "GameFramework/InputDeviceSubsystem.h"
|
||||
#include "InputAction.h"
|
||||
#include "InputMappingContext.h"
|
||||
#include "Widgets/SOverlay.h"
|
||||
#include "Widgets/Images/SImage.h"
|
||||
#include "Widgets/Text/STextBlock.h"
|
||||
@@ -57,25 +62,30 @@ int32 UlxPromptWidget::ChooseIcon(bool Playstation, FKey Key) const
|
||||
|
||||
void UlxPromptWidget::ChooseAppearance(int32 &OutIcon, FString &OutGlyph)
|
||||
{
|
||||
UWorld* World = GetWorld();
|
||||
APlayerController* PC = World ? World->GetFirstPlayerController() : nullptr;
|
||||
AlxPlayerControllerBase* LPC = Cast<AlxPlayerControllerBase>(PC);
|
||||
|
||||
ElxInputMode Mode = LPC ? LPC->CurrentInputMode : ElxInputMode::KeyboardMouse;
|
||||
|
||||
FKey Key = (Mode == ElxInputMode::KeyboardMouse) ? KeyboardKey : GamepadKey;
|
||||
|
||||
OutIcon = ChooseIcon(Mode == ElxInputMode::PlayStationGamepad, Key);
|
||||
FKey Key = (ControllerType == ElxControllerType::KeyboardMouse) ? KeyboardKey : GamepadKey;
|
||||
OutIcon = ChooseIcon(ControllerType == ElxControllerType::PlayStationGamepad, Key);
|
||||
if (OutIcon == 0) OutGlyph = Key.GetDisplayName().ToString();
|
||||
else OutGlyph = TEXT("");
|
||||
}
|
||||
|
||||
void UlxPromptWidget::OnHardwareDeviceChanged(const FPlatformUserId UserId, const FInputDeviceId DeviceId)
|
||||
{
|
||||
ElxControllerType Type = UlxUtilityLibrary::DetectControllerType(GetOwningLocalPlayer());
|
||||
if (ControllerType != Type)
|
||||
{
|
||||
ControllerType = Type;
|
||||
SynchronizeProperties();
|
||||
}
|
||||
}
|
||||
|
||||
FBox2f UlxPromptWidget::GetIconUVs(int32 IconIndex)
|
||||
{
|
||||
const float ColWidth = 1.0f / 4.0f;
|
||||
const float RowHeight = 1.0f / 8.0f;
|
||||
float U = (IconIndex % 4) * ColWidth;
|
||||
float V = (IconIndex / 4) * RowHeight;
|
||||
int32 Col = IconIndex % 4;
|
||||
int32 Row = IconIndex / 4;
|
||||
float U = Col * ColWidth;
|
||||
float V = Row * RowHeight;
|
||||
return FBox2f(FVector2f(U, V), FVector2f(U + ColWidth, V + RowHeight));
|
||||
}
|
||||
|
||||
@@ -151,13 +161,50 @@ void UlxPromptWidget::SetKeys(FKey InGamepadKey, FKey InKeyboardKey)
|
||||
}
|
||||
}
|
||||
|
||||
void UlxPromptWidget::SetKeysFromBindings(const UInputMappingContext* InputMappingContext, const UInputAction* EnhancedInputAction)
|
||||
{
|
||||
check(InputMappingContext);
|
||||
check(EnhancedInputAction);
|
||||
|
||||
FKey NewFirstKey;
|
||||
FKey NewGamepadKey;
|
||||
FKey NewKeyboardKey;
|
||||
|
||||
for (const FEnhancedActionKeyMapping& Mapping : InputMappingContext->GetMappings())
|
||||
{
|
||||
if (Mapping.Action != EnhancedInputAction) continue;
|
||||
FKey Key = Mapping.Key;
|
||||
if (Key.IsDigital())
|
||||
{
|
||||
if (!NewFirstKey.IsValid()) NewFirstKey = Mapping.Key;
|
||||
if (Key.IsTouch()) { /* not supported */ }
|
||||
else if (Key.IsGesture()) { /* not supported */ }
|
||||
else if (Key.IsGamepadKey()) { if (!NewGamepadKey.IsValid()) NewGamepadKey = Key; }
|
||||
else { if (!NewKeyboardKey.IsValid()) NewKeyboardKey = Key; }
|
||||
}
|
||||
}
|
||||
|
||||
if (!NewGamepadKey.IsValid()) NewGamepadKey = NewFirstKey;
|
||||
if (!NewKeyboardKey.IsValid()) NewKeyboardKey = NewFirstKey;
|
||||
SetKeys(NewGamepadKey, NewKeyboardKey);
|
||||
}
|
||||
|
||||
TSharedRef<SWidget> UlxPromptWidget::RebuildWidget()
|
||||
{
|
||||
if (!IsDesignTime())
|
||||
{
|
||||
ControllerType = UlxUtilityLibrary::DetectControllerType(GetOwningLocalPlayer());
|
||||
if (UInputDeviceSubsystem* IDS = GEngine->GetEngineSubsystem<UInputDeviceSubsystem>())
|
||||
{
|
||||
IDS->OnInputHardwareDeviceChanged.AddDynamic(this, &UlxPromptWidget::OnHardwareDeviceChanged);
|
||||
}
|
||||
}
|
||||
|
||||
int32 Icon = 0;
|
||||
FString Glyph;
|
||||
ChooseAppearance(Icon, Glyph);
|
||||
|
||||
MyBrush.SetResourceObject(ButtonAtlas);
|
||||
MyBrush.SetResourceObject(ButtonAtlas.Get());
|
||||
MyBrush.ImageSize = Size;
|
||||
MyBrush.SetUVRegion(GetIconUVs(Icon));
|
||||
|
||||
@@ -184,6 +231,13 @@ TSharedRef<SWidget> UlxPromptWidget::RebuildWidget()
|
||||
void UlxPromptWidget::ReleaseSlateResources(bool bReleaseChildren)
|
||||
{
|
||||
Super::ReleaseSlateResources(bReleaseChildren);
|
||||
if (!IsDesignTime())
|
||||
{
|
||||
if (UInputDeviceSubsystem* IDS = GEngine->GetEngineSubsystem<UInputDeviceSubsystem>())
|
||||
{
|
||||
IDS->OnInputHardwareDeviceChanged.RemoveDynamic(this, &UlxPromptWidget::OnHardwareDeviceChanged);
|
||||
}
|
||||
}
|
||||
MyOverlay.Reset();
|
||||
MyImage.Reset();
|
||||
MyScaleBox.Reset();
|
||||
|
||||
Reference in New Issue
Block a user