Add binding to Enhanced Input Actions so UI can use hot keys

This commit is contained in:
Steve Streeting 2024-06-25 18:05:07 +01:00
parent 7f1d0e9c0e
commit 78d203e7c1
2 changed files with 72 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#include "StevesGameSubsystem.h" #include "StevesGameSubsystem.h"
#include "EngineUtils.h" #include "EngineUtils.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h" #include "EnhancedInputSubsystems.h"
#include "StevesGameViewportClientBase.h" #include "StevesGameViewportClientBase.h"
#include "StevesPluginSettings.h" #include "StevesPluginSettings.h"
@ -107,6 +108,34 @@ TSoftObjectPtr<UInputAction> UStevesGameSubsystem::FindEnhancedInputAction(const
return nullptr; return nullptr;
} }
void UStevesGameSubsystem::RegisterInterestInEnhancedInputAction(const UInputAction* Action, ETriggerEvent TriggerEvent)
{
// Avoid registering duplicate interest
FEnhancedInputInterest Interest(Action, TriggerEvent);
if (!RegisteredEnhancedInputActionInterests.Contains(Interest))
{
if (auto GI = GetGameInstance())
{
if (auto PC = GI->GetFirstLocalPlayerController())
{
if (auto EIC = Cast<UEnhancedInputComponent>(PC->InputComponent))
{
EIC->BindAction(Action, TriggerEvent, this, &ThisClass::EnhancedInputActionTriggered);
RegisteredEnhancedInputActionInterests.Add(Interest);
}
}
}
}
}
void UStevesGameSubsystem::EnhancedInputActionTriggered(const FInputActionInstance& InputActionInstance)
{
OnEnhancedInputActionTriggered.Broadcast(InputActionInstance.GetSourceAction(), InputActionInstance.GetTriggerEvent());
}
void UStevesGameSubsystem::InitTheme() void UStevesGameSubsystem::InitTheme()
{ {
DefaultUiTheme = LoadObject<UUiTheme>(nullptr, *DefaultUiThemePath, nullptr); DefaultUiTheme = LoadObject<UUiTheme>(nullptr, *DefaultUiThemePath, nullptr);

View File

@ -10,12 +10,14 @@
#include "StevesUI/FocusSystem.h" #include "StevesUI/FocusSystem.h"
#include "StevesUI/InputImage.h" #include "StevesUI/InputImage.h"
#include "StevesUI/UiTheme.h" #include "StevesUI/UiTheme.h"
#include "Templates/TypeHash.h"
#include "StevesGameSubsystem.generated.h" #include "StevesGameSubsystem.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnInputModeChanged, int, PlayerIndex, EInputMode, InputMode); DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnInputModeChanged, int, PlayerIndex, EInputMode, InputMode);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnEnhancedInputMappingsChanged); DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnEnhancedInputMappingsChanged);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnWindowForegroundChanged, bool, bFocussed); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnWindowForegroundChanged, bool, bFocussed);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnEnhancedInputActionTriggered, const UInputAction*, Action, ETriggerEvent, TriggeredEvent);
/// Entry point for all the top-level features of the helper system /// Entry point for all the top-level features of the helper system
UCLASS(Config=Game) UCLASS(Config=Game)
@ -34,6 +36,32 @@ protected:
UPROPERTY(Config) UPROPERTY(Config)
FString DefaultUiThemePath; FString DefaultUiThemePath;
struct FEnhancedInputInterest
{
TWeakObjectPtr<const UInputAction> Action;
ETriggerEvent Trigger;
FEnhancedInputInterest(const UInputAction* TheAction, ETriggerEvent TheTriggerEvent) : Action(TheAction), Trigger(TheTriggerEvent) {}
friend uint32 GetTypeHash(const FEnhancedInputInterest& Arg)
{
return HashCombine(GetTypeHash(Arg.Action), GetTypeHash(Arg.Trigger));
}
friend bool operator==(const FEnhancedInputInterest& Lhs, const FEnhancedInputInterest& RHS)
{
return Lhs.Action == RHS.Action
&& Lhs.Trigger == RHS.Trigger;
}
friend bool operator!=(const FEnhancedInputInterest& Lhs, const FEnhancedInputInterest& RHS)
{
return !(Lhs == RHS);
}
};
TSet<FEnhancedInputInterest> RegisteredEnhancedInputActionInterests;
public: public:
virtual void Initialize(FSubsystemCollectionBase& Collection) override; virtual void Initialize(FSubsystemCollectionBase& Collection) override;
@ -143,6 +171,8 @@ protected:
TSoftObjectPtr<UDataTable> GetGamepadImages(int PlayerIndex, const UUiTheme* Theme); TSoftObjectPtr<UDataTable> GetGamepadImages(int PlayerIndex, const UUiTheme* Theme);
UPaperSprite* GetImageSpriteFromTable(const FKey& Key, const TSoftObjectPtr<UDataTable>& Asset); UPaperSprite* GetImageSpriteFromTable(const FKey& Key, const TSoftObjectPtr<UDataTable>& Asset);
UFUNCTION()
void EnhancedInputActionTriggered(const FInputActionInstance& InputActionInstance);
public: public:
@ -168,6 +198,11 @@ public:
UPROPERTY(BlueprintAssignable) UPROPERTY(BlueprintAssignable)
FOnEnhancedInputMappingsChanged OnEnhancedInputMappingsChanged; FOnEnhancedInputMappingsChanged OnEnhancedInputMappingsChanged;
/// Event fired when an enhanced input event that an interest has previously been registered in triggers.
/// Nothing will fire on this event unless you call RegisterInterestInEnhancedInputAction to listen for it.
UPROPERTY(BlueprintAssignable)
FOnEnhancedInputActionTriggered OnEnhancedInputActionTriggered;
/// Event raised when the game window's foreground status changes /// Event raised when the game window's foreground status changes
UPROPERTY(BlueprintAssignable) UPROPERTY(BlueprintAssignable)
FOnWindowForegroundChanged OnWindowForegroundChanged; FOnWindowForegroundChanged OnWindowForegroundChanged;
@ -301,4 +336,10 @@ public:
*/ */
TSoftObjectPtr<UInputAction> FindEnhancedInputAction(const FString& Name); TSoftObjectPtr<UInputAction> FindEnhancedInputAction(const FString& Name);
/// Register an interest in an enhanced input action. Calling this will result in OnEnhancedInputActionTriggered being called
/// when this action is triggered.
/// This is mainly for use in UI bindings. You only need to call it once for each UI-specific action.
UFUNCTION(BlueprintCallable)
void RegisterInterestInEnhancedInputAction(const UInputAction* Action, ETriggerEvent TriggerEvent);
}; };