Clean up the player controller

This commit is contained in:
2026-04-17 02:53:32 -04:00
parent ae1ad7640d
commit 392faff205
5 changed files with 6 additions and 145 deletions

View File

@@ -172,7 +172,6 @@ void ALuprexGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLev
if (PC != nullptr)
{
PC->UpdateLookAt();
PC->UpdateEventDispatch();
}
}
}

View File

@@ -28,7 +28,7 @@ void UlxUserWidget::BackupInputComponent()
}
}
void UlxUserWidget::DisableEventBinding(const UInputAction* InputAction)
void UlxUserWidget::DisableInputAction(const UInputAction* InputAction)
{
UEnhancedInputComponent* EIC = Cast<UEnhancedInputComponent>(InputComponent);
if (!EIC) return;
@@ -40,9 +40,9 @@ void UlxUserWidget::DisableEventBinding(const UInputAction* InputAction)
});
}
void UlxUserWidget::RestoreInputBinding(const UInputAction* InputAction)
void UlxUserWidget::RestoreInputAction(const UInputAction* InputAction)
{
DisableEventBinding(InputAction);
DisableInputAction(InputAction);
UEnhancedInputComponent* EIC = Cast<UEnhancedInputComponent>(InputComponent);
if (!EIC) return;
@@ -59,7 +59,7 @@ void UlxUserWidget::RestoreInputBinding(const UInputAction* InputAction)
void UlxUserWidget::RedirectInputAction(const UInputAction* From, const UInputAction* To)
{
DisableEventBinding(From);
DisableInputAction(From);
UEnhancedInputComponent* EIC = Cast<UEnhancedInputComponent>(InputComponent);
if (!EIC) return;

View File

@@ -25,13 +25,13 @@ public:
// Remove every live event binding whose action is InputAction.
// No-op if there are none, or if InputComponent isn't enhanced.
UFUNCTION(BlueprintCallable, Category="Luprex|Widget Enhanced Input")
void DisableEventBinding(const UInputAction* InputAction);
void DisableInputAction(const UInputAction* InputAction);
// Replace any live bindings for InputAction with fresh clones of every
// saved binding for that action. Leaves the backup array intact so this
// can be called repeatedly.
UFUNCTION(BlueprintCallable, Category="Luprex|Widget Enhanced Input")
void RestoreInputBinding(const UInputAction* InputAction);
void RestoreInputAction(const UInputAction* InputAction);
// Install live bindings on From that, when fired, dispatch through a
// clone of each saved binding for To. Clears any pre-existing live

View File

@@ -4,24 +4,6 @@
#include "TangibleManager.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/GameInstance.h"
#include "Engine/GameViewportClient.h"
#include "Framework/Application/SlateApplication.h"
#include "Widgets/SViewport.h"
#include "Slate/SObjectWidget.h"
FString AlxPlayerControllerBase::GetUserWidgetName(SWidget *W)
{
while (W)
{
if (W->GetType() == FName("SObjectWidget"))
{
UUserWidget *UW = static_cast<SObjectWidget*>(W)->GetWidgetObject();
if (UW) return UW->GetClass()->GetName();
}
W = W->GetParentWidget().Get();
}
return TEXT("Unknown Widget");
}
AlxPlayerControllerBase *AlxPlayerControllerBase::FromContext(const UObject *Context)
{
@@ -71,96 +53,6 @@ FVector2D AlxPlayerControllerBase::GetLookAtPixel(const UObject *Context)
return ScreenPosition;
}
void AlxPlayerControllerBase::BeginPlay()
{
Super::BeginPlay();
HotkeyInputComponent = NewObject<UInputComponent>(this);
HotkeyInputComponent->bBlockInput = false;
PushInputComponent(HotkeyInputComponent);
}
void AlxPlayerControllerBase::UpdateEventDispatch()
{
EventRequests.GarbageCollect();
// If we're in GameOnly mode, check that focus is still on the viewport.
if (CurrentInputMode == InputMode::GameOnly)
{
UGameViewportClient *GVC = GetWorld() ? GetWorld()->GetGameViewport() : nullptr;
if (GVC)
{
TSharedPtr<SViewport> ViewportWidget = GVC->GetGameViewportWidget();
if (ViewportWidget.IsValid())
{
TSharedPtr<SWidget> Focused = FSlateApplication::Get().GetKeyboardFocusedWidget();
if (Focused.Get() != ViewportWidget.Get())
{
UE_LOG(LogLuprexIntegration, Error, TEXT("In GameOnly mode, keyboard focus must stay on viewport, but was stolen by: %s. Restoring."), *GetUserWidgetName(Focused.Get()));
EventRequests.SetDirty();
}
if (!ViewportWidget->HasMouseCapture())
{
UE_LOG(LogLuprexIntegration, Error, TEXT("In GameOnly mode, viewport must have mouse capture, but lost it. Restoring."));
EventRequests.SetDirty();
}
}
}
}
if (!EventRequests.IsDirty()) return;
EventRequests.ClearDirty();
CurrentInputMode = EventRequests.GetRequestedMode();
const TArray<FlxEventRequest> &Requests = EventRequests.GetRequests();
if (CurrentInputMode == InputMode::UIOnly)
{
SetInputMode(FInputModeUIOnly().SetWidgetToFocus(Requests[0].Widget->GetCachedWidget()));
}
else
{
SetInputMode(FInputModeGameOnly());
HotkeyInputComponent->KeyBindings.Empty();
TSet<FKey> BoundKeys;
for (const FlxEventRequest &Req : Requests)
{
for (const FKey &Key : Req.Hotkeys)
{
if (!BoundKeys.Contains(Key))
{
BoundKeys.Add(Key);
HotkeyInputComponent->BindKey(Key, IE_Pressed, this, &AlxPlayerControllerBase::ForwardKeyEvent);
}
}
}
}
}
void AlxPlayerControllerBase::ForwardKeyEvent(FKey Key)
{
// TODO: implement
}
void AlxPlayerControllerBase::RequestEvents(const FlxEventRequest &Request)
{
if (!FlxEventRequests::SanityCheck(Request)) return;
AlxPlayerControllerBase *PC = FromContext(Request.Widget);
PC->EventRequests.Request(Request);
}
void AlxPlayerControllerBase::UnRequestEvents(UUserWidget *Widget)
{
if (Widget == nullptr)
{
UE_LOG(LogLuprexIntegration, Error, TEXT("UnRequestEvents called with null widget."));
return;
}
AlxPlayerControllerBase *PC = FromContext(Widget);
PC->EventRequests.Remove(Widget);
}
void AlxPlayerControllerBase::UpdateLookAt()
{
UlxTangibleManager *TM = GetGameInstance()->GetSubsystem<UlxTangibleManager>();

View File

@@ -3,7 +3,6 @@
#include "CoreMinimal.h"
#include "Engine/HitResult.h"
#include "GameFramework/PlayerController.h"
#include "InputEvents.h"
#include "PlayerControllerBase.generated.h"
UCLASS(BlueprintType, Blueprintable)
@@ -12,8 +11,6 @@ class INTEGRATION_API AlxPlayerControllerBase : public APlayerController
GENERATED_BODY()
public:
using InputMode = FlxEventRequests::InputMode;
UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static void SetLookAt(const UObject *Context, const FHitResult &HitResult);
@@ -29,12 +26,6 @@ public:
UFUNCTION(BlueprintCallable, meta = (WorldContext = "Context"), Category = "Luprex|Look-At Detection")
static void SetLookAtChanged(const UObject *Context);
UFUNCTION(BlueprintCallable, Category = "Luprex|Input Events")
static void RequestEvents(const FlxEventRequest &Request);
UFUNCTION(BlueprintCallable, Category = "Luprex|Input Events")
static void UnRequestEvents(UUserWidget *Widget);
// Blueprint events
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex|Look-At Detection")
void CalculateLookAt();
@@ -42,35 +33,14 @@ public:
UFUNCTION(BlueprintImplementableEvent, Category = "Luprex|Look-At Detection")
void LookAtChanged();
virtual void BeginPlay() override;
// Called by GameMode each tick.
void UpdateLookAt();
// Rebuild input component and switch input mode.
void UpdateEventDispatch();
// Handler for GameOnly mode hotkey presses.
void ForwardKeyEvent(FKey Key);
// Walk up from a Slate widget to find the nearest UMG widget class name.
static FString GetUserWidgetName(SWidget *W);
// Get the player controller, cast to AlxPlayerControllerBase.
static AlxPlayerControllerBase *FromContext(const UObject *Context);
UPROPERTY()
FHitResult CurrentLookAt;
UPROPERTY()
FlxEventRequests EventRequests;
// Input component for GameOnly mode: catches hotkeys only.
UPROPERTY()
UInputComponent *HotkeyInputComponent = nullptr;
// Current input mode.
InputMode CurrentInputMode = InputMode::GameOnly;
bool MustCallLookAtChanged = false;
};