From 392faff205aa8851b8a5d37dac3409259d15786f Mon Sep 17 00:00:00 2001 From: jyelon Date: Fri, 17 Apr 2026 02:53:32 -0400 Subject: [PATCH] Clean up the player controller --- Source/Integration/LuprexGameModeBase.cpp | 1 - Source/Integration/LuprexUserWidget.cpp | 8 +- Source/Integration/LuprexUserWidget.h | 4 +- Source/Integration/PlayerControllerBase.cpp | 108 -------------------- Source/Integration/PlayerControllerBase.h | 30 ------ 5 files changed, 6 insertions(+), 145 deletions(-) diff --git a/Source/Integration/LuprexGameModeBase.cpp b/Source/Integration/LuprexGameModeBase.cpp index f35bebfd..4910c2d7 100644 --- a/Source/Integration/LuprexGameModeBase.cpp +++ b/Source/Integration/LuprexGameModeBase.cpp @@ -172,7 +172,6 @@ void ALuprexGameModeBase::OnWorldPostActorTick(UWorld* InWorld, ELevelTick InLev if (PC != nullptr) { PC->UpdateLookAt(); - PC->UpdateEventDispatch(); } } } diff --git a/Source/Integration/LuprexUserWidget.cpp b/Source/Integration/LuprexUserWidget.cpp index 66a2a164..cef5705b 100644 --- a/Source/Integration/LuprexUserWidget.cpp +++ b/Source/Integration/LuprexUserWidget.cpp @@ -28,7 +28,7 @@ void UlxUserWidget::BackupInputComponent() } } -void UlxUserWidget::DisableEventBinding(const UInputAction* InputAction) +void UlxUserWidget::DisableInputAction(const UInputAction* InputAction) { UEnhancedInputComponent* EIC = Cast(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(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(InputComponent); if (!EIC) return; diff --git a/Source/Integration/LuprexUserWidget.h b/Source/Integration/LuprexUserWidget.h index ce3fd355..0672afed 100644 --- a/Source/Integration/LuprexUserWidget.h +++ b/Source/Integration/LuprexUserWidget.h @@ -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 diff --git a/Source/Integration/PlayerControllerBase.cpp b/Source/Integration/PlayerControllerBase.cpp index 1e41dafb..8ba0771a 100644 --- a/Source/Integration/PlayerControllerBase.cpp +++ b/Source/Integration/PlayerControllerBase.cpp @@ -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(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(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 ViewportWidget = GVC->GetGameViewportWidget(); - if (ViewportWidget.IsValid()) - { - TSharedPtr 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 &Requests = EventRequests.GetRequests(); - - if (CurrentInputMode == InputMode::UIOnly) - { - SetInputMode(FInputModeUIOnly().SetWidgetToFocus(Requests[0].Widget->GetCachedWidget())); - } - else - { - SetInputMode(FInputModeGameOnly()); - - HotkeyInputComponent->KeyBindings.Empty(); - TSet 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(); diff --git a/Source/Integration/PlayerControllerBase.h b/Source/Integration/PlayerControllerBase.h index dd7cab8c..8e70f54f 100644 --- a/Source/Integration/PlayerControllerBase.h +++ b/Source/Integration/PlayerControllerBase.h @@ -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; };