Compare commits

...

2 Commits

Author SHA1 Message Date
281db9a375 Add move input processor 2026-04-26 00:49:21 +08:00
794fcbb8a9 Add input system to player character 2026-04-26 00:46:03 +08:00
8 changed files with 232 additions and 10 deletions

View File

@@ -2,3 +2,4 @@
DefaultMappingPriority=0
bRouteInputThroughGenericInputSystem=True
bBlockGameplayInputWhenUIFocused=True
DefaultMoveInputProcessorClass="/Script/PHY.PHYInputProcessor_Move"

View File

@@ -7,11 +7,14 @@
#include "Camera/CameraComponent.h"
#include "Camera/PHYUGCSpringArmComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GIPS_InputSystemComponent.h"
#include "Player/PHYPlayerState.h"
APHYPlayerCharacter::APHYPlayerCharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
InputSystemComponent = CreateDefaultSubobject<UGIPS_InputSystemComponent>(TEXT("InputSystemComponent"));
CameraBoom = CreateDefaultSubobject<UPHYUGCSpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetRootComponent());
CameraBoom->TargetArmLength = 320.0f;

View File

@@ -0,0 +1,83 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "Input/PHYInputProcessor_Move.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(PHYInputProcessor_Move)
#include "Characters/PHYPlayerCharacter.h"
#include "GIPS_InputSystemComponent.h"
#include "PHYGameplayTags.h"
#include "Player/PHYPlayerController.h"
UPHYInputProcessor_Move::UPHYInputProcessor_Move()
{
InputTags.AddTag(PHYGameplayTags::Input_Move);
TriggerEvents = {
ETriggerEvent::Started,
ETriggerEvent::Triggered,
ETriggerEvent::Ongoing,
ETriggerEvent::Completed,
ETriggerEvent::Canceled
};
}
bool UPHYInputProcessor_Move::CheckCanHandleInput_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, const FGameplayTag InputTag, const ETriggerEvent TriggerEvent) const
{
(void)ActionData;
(void)TriggerEvent;
if (!IC || InputTag != PHYGameplayTags::Input_Move)
{
return false;
}
const APHYPlayerCharacter* PlayerCharacter = Cast<APHYPlayerCharacter>(IC->GetControlledPawn());
return PlayerCharacter && PlayerCharacter->GetController<APHYPlayerController>();
}
void UPHYInputProcessor_Move::HandleInputTriggered_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, const FGameplayTag InputTag) const
{
(void)InputTag;
RouteMoveInput(IC, ActionData, ETriggerEvent::Triggered);
}
void UPHYInputProcessor_Move::HandleInputStarted_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, const FGameplayTag InputTag) const
{
(void)InputTag;
RouteMoveInput(IC, ActionData, ETriggerEvent::Started);
}
void UPHYInputProcessor_Move::HandleInputOngoing_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, const FGameplayTag InputTag) const
{
(void)InputTag;
RouteMoveInput(IC, ActionData, ETriggerEvent::Ongoing);
}
void UPHYInputProcessor_Move::HandleInputCanceled_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, const FGameplayTag InputTag) const
{
(void)InputTag;
RouteMoveInput(IC, ActionData, ETriggerEvent::Canceled);
}
void UPHYInputProcessor_Move::HandleInputCompleted_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, const FGameplayTag InputTag) const
{
(void)InputTag;
RouteMoveInput(IC, ActionData, ETriggerEvent::Completed);
}
FString UPHYInputProcessor_Move::GetEditorFriendlyName_Implementation() const
{
return TEXT("PHY Move");
}
bool UPHYInputProcessor_Move::RouteMoveInput(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, const ETriggerEvent TriggerEvent) const
{
if (!IC)
{
return false;
}
APHYPlayerCharacter* PlayerCharacter = Cast<APHYPlayerCharacter>(IC->GetControlledPawn());
APHYPlayerController* PlayerController = PlayerCharacter ? PlayerCharacter->GetController<APHYPlayerController>() : nullptr;
return PlayerController ? PlayerController->RouteMoveInputFromProcessor(ActionData, TriggerEvent) : false;
}

View File

@@ -64,22 +64,42 @@ APHYPlayerCharacter* APHYPlayerController::GetPHYPlayerCharacter() const
return Cast<APHYPlayerCharacter>(GetPawn());
}
void APHYPlayerController::BindInputEvents()
UGIPS_InputSystemComponent* APHYPlayerController::GetInputSystemComponent() const
{
if (!InputSystemComponent)
if (const APHYPlayerCharacter* PHYCharacter = GetPHYPlayerCharacter())
{
return;
if (UGIPS_InputSystemComponent* CharacterInputSystemComponent = PHYCharacter->GetInputSystemComponent())
{
return CharacterInputSystemComponent;
}
}
InputSystemComponent->OnReceivedInput.RemoveDynamic(this, &ThisClass::OnReceivedInput);
InputSystemComponent->OnReceivedInput.AddDynamic(this, &ThisClass::OnReceivedInput);
return InputSystemComponent;
}
void APHYPlayerController::BindInputEvents()
{
if (BoundInputSystemComponent)
{
BoundInputSystemComponent->OnReceivedInput.RemoveDynamic(this, &ThisClass::OnReceivedInput);
}
BoundInputSystemComponent = GetInputSystemComponent();
if (BoundInputSystemComponent)
{
BoundInputSystemComponent->OnReceivedInput.RemoveDynamic(this, &ThisClass::OnReceivedInput);
BoundInputSystemComponent->OnReceivedInput.AddDynamic(this, &ThisClass::OnReceivedInput);
}
}
void APHYPlayerController::OnReceivedInput(const FInputActionInstance& ActionData, const FGameplayTag& InputTag, const ETriggerEvent TriggerEvent)
{
if (InputTag == PHYGameplayTags::Input_Move)
{
HandleMoveInput(ActionData, TriggerEvent);
if (!ConsumeMoveProcessorGuard(TriggerEvent))
{
RouteMoveInput(ActionData, TriggerEvent);
}
return;
}
@@ -95,6 +115,18 @@ void APHYPlayerController::OnReceivedInput(const FInputActionInstance& ActionDat
}
}
bool APHYPlayerController::RouteMoveInput(const FInputActionInstance& ActionData, const ETriggerEvent TriggerEvent)
{
return HandleMoveInput(ActionData, TriggerEvent);
}
bool APHYPlayerController::RouteMoveInputFromProcessor(const FInputActionInstance& ActionData, const ETriggerEvent TriggerEvent)
{
bMoveInputHandledByProcessor = true;
LastMoveInputProcessorTriggerEvent = TriggerEvent;
return RouteMoveInput(ActionData, TriggerEvent);
}
bool APHYPlayerController::HandleMoveInput(const FInputActionInstance& ActionData, const ETriggerEvent TriggerEvent)
{
APHYPlayerCharacter* PHYCharacter = GetPHYPlayerCharacter();
@@ -123,6 +155,20 @@ bool APHYPlayerController::HandleMoveInput(const FInputActionInstance& ActionDat
return true;
}
bool APHYPlayerController::ConsumeMoveProcessorGuard(const ETriggerEvent TriggerEvent)
{
if (bMoveInputHandledByProcessor && LastMoveInputProcessorTriggerEvent == TriggerEvent)
{
bMoveInputHandledByProcessor = false;
LastMoveInputProcessorTriggerEvent = ETriggerEvent::None;
return true;
}
bMoveInputHandledByProcessor = false;
LastMoveInputProcessorTriggerEvent = ETriggerEvent::None;
return false;
}
bool APHYPlayerController::HandleLookInput(const FInputActionInstance& ActionData, const ETriggerEvent TriggerEvent)
{
CachedLookInput = IsInputReleased(TriggerEvent) ? FVector2D::ZeroVector : ExtractAxis2D(ActionData.GetValue());

View File

@@ -7,12 +7,13 @@
#include "PHYPlayerCharacter.generated.h"
class UCameraComponent;
class UGIPS_InputSystemComponent;
class UPHYUGCSpringArmComponent;
/**
* @brief PHY 玩家角色。
*
* 玩家角色从 PlayerState 获取 ASC持有相机组件,不直接持有输入组件。
* 玩家角色从 PlayerState 获取 ASC并持有玩家输入组件与 UGC 相机组件。
*/
UCLASS(BlueprintType, Blueprintable)
class PHY_API APHYPlayerCharacter : public APHYCharacterBase
@@ -37,10 +38,18 @@ public:
UFUNCTION(BlueprintCallable, BlueprintPure, Category="PHY|Camera")
UCameraComponent* GetFollowCamera() const { return FollowCamera; }
/** @brief 获取玩家角色上的 GenericInputSystem 组件。 */
UFUNCTION(BlueprintCallable, BlueprintPure, Category="PHY|Input")
UGIPS_InputSystemComponent* GetInputSystemComponent() const { return InputSystemComponent; }
protected:
/** @brief 根据当前 PlayerState 初始化 ASC。 */
virtual void InitializeAbilitySystemFromPlayerState();
/** @brief 玩家角色上的 GenericInputSystem 路由组件。 */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="PHY|Input")
TObjectPtr<UGIPS_InputSystemComponent> InputSystemComponent;
/** @brief UGC 项目相机臂。 */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="PHY|Camera")
TObjectPtr<UPHYUGCSpringArmComponent> CameraBoom;

View File

@@ -0,0 +1,48 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GIPS_InputProcessor.h"
#include "PHYInputProcessor_Move.generated.h"
/**
* @brief PHY 移动输入处理器。
*
* 该处理器处理 `Input.Move`,并把二维输入值路由到当前玩家控制器和玩家角色。
*/
UCLASS(BlueprintType, Blueprintable, EditInlineNew, DefaultToInstanced)
class PHY_API UPHYInputProcessor_Move : public UGIPS_InputProcessor
{
GENERATED_BODY()
public:
/** @brief 构造默认响应的输入 Tag 和触发事件。 */
UPHYInputProcessor_Move();
protected:
/** @brief 检查当前输入组件是否能处理移动输入。 */
virtual bool CheckCanHandleInput_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, FGameplayTag InputTag, ETriggerEvent TriggerEvent) const override;
/** @brief 处理 Triggered 移动输入。 */
virtual void HandleInputTriggered_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, FGameplayTag InputTag) const override;
/** @brief 处理 Started 移动输入。 */
virtual void HandleInputStarted_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, FGameplayTag InputTag) const override;
/** @brief 处理 Ongoing 移动输入。 */
virtual void HandleInputOngoing_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, FGameplayTag InputTag) const override;
/** @brief 处理 Canceled 移动输入。 */
virtual void HandleInputCanceled_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, FGameplayTag InputTag) const override;
/** @brief 处理 Completed 移动输入。 */
virtual void HandleInputCompleted_Implementation(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, FGameplayTag InputTag) const override;
/** @brief 返回编辑器里显示的处理器名称。 */
virtual FString GetEditorFriendlyName_Implementation() const override;
private:
/** @brief 将移动输入发送给玩家控制器。 */
bool RouteMoveInput(UGIPS_InputSystemComponent* IC, const FInputActionInstance& ActionData, ETriggerEvent TriggerEvent) const;
};

View File

@@ -4,8 +4,11 @@
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "UObject/SoftObjectPtr.h"
#include "PHYConfigSettings.generated.h"
class UGIPS_InputProcessor;
/**
* @brief PHY 项目核心配置。
*/
@@ -52,6 +55,10 @@ public:
/** @brief UI 聚焦时是否阻断 Gameplay 输入。 */
UPROPERTY(Config)
bool bBlockGameplayInputWhenUIFocused = true;
/** @brief 默认移动输入处理器类,用于创建 GenericInputSystem 控制配置。 */
UPROPERTY(Config)
TSoftClassPtr<UGIPS_InputProcessor> DefaultMoveInputProcessorClass;
};
/**

View File

@@ -13,7 +13,7 @@ class UGIPS_InputSystemComponent;
/**
* @brief PHY 玩家控制器。
*
* 控制器持有 GenericInputSystem 组件,并将 Input GameplayTag 路由到当前玩家角色、ASC、移动和相机输入缓存。
* 控制器优先绑定当前玩家角色的 GenericInputSystem 组件,并将 Input GameplayTag 路由到角色、ASC、移动和相机输入缓存。
*/
UCLASS(BlueprintType, Blueprintable)
class PHY_API APHYPlayerController : public APlayerController
@@ -30,9 +30,13 @@ public:
/** @brief 接管 Pawn 时刷新输入委托。 */
virtual void OnPossess(APawn* InPawn) override;
/** @brief 获取 GenericInputSystem 组件。 */
/** @brief 获取当前实际绑定的 GenericInputSystem 组件,优先返回玩家角色上的组件。 */
UFUNCTION(BlueprintCallable, BlueprintPure, Category="PHY|Input")
UGIPS_InputSystemComponent* GetInputSystemComponent() const { return InputSystemComponent; }
UGIPS_InputSystemComponent* GetInputSystemComponent() const;
/** @brief 获取控制器自身保留的 GenericInputSystem 组件。 */
UFUNCTION(BlueprintCallable, BlueprintPure, Category="PHY|Input")
UGIPS_InputSystemComponent* GetControllerInputSystemComponent() const { return InputSystemComponent; }
/** @brief 获取当前 PHY 玩家角色。 */
UFUNCTION(BlueprintCallable, BlueprintPure, Category="PHY|Input")
@@ -46,6 +50,12 @@ public:
UFUNCTION(BlueprintCallable, BlueprintPure, Category="PHY|Input")
FVector GetCachedMovementControlInput() const { return FVector(CachedMovementInput.X, CachedMovementInput.Y, 0.0f); }
/** @brief 从输入处理器或控制器 fallback 路由移动输入。 */
bool RouteMoveInput(const FInputActionInstance& ActionData, ETriggerEvent TriggerEvent);
/** @brief 从 GenericInputSystem 处理器路由移动输入,并阻止同一事件被广播 fallback 重复执行。 */
bool RouteMoveInputFromProcessor(const FInputActionInstance& ActionData, ETriggerEvent TriggerEvent);
protected:
/** @brief 绑定 GenericInputSystem 输入委托。 */
virtual void BindInputEvents();
@@ -60,10 +70,17 @@ protected:
/** @brief 处理视角输入。 */
virtual bool HandleLookInput(const FInputActionInstance& ActionData, ETriggerEvent TriggerEvent);
/** @brief 查询并消费移动处理器留下的重复执行保护。 */
virtual bool ConsumeMoveProcessorGuard(ETriggerEvent TriggerEvent);
/** @brief GenericInputSystem 路由组件。 */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="PHY|Input")
TObjectPtr<UGIPS_InputSystemComponent> InputSystemComponent;
/** @brief 当前已经绑定输入委托的组件。 */
UPROPERTY(Transient)
TObjectPtr<UGIPS_InputSystemComponent> BoundInputSystemComponent;
/** @brief 本地移动输入缓存。 */
UPROPERTY(Transient)
FVector2D CachedMovementInput = FVector2D::ZeroVector;
@@ -75,4 +92,12 @@ protected:
/** @brief UGC 相机管理器读取的旋转输入缓存。 */
UPROPERTY(Transient)
FRotator CachedRotationInput = FRotator::ZeroRotator;
/** @brief 输入处理器是否已经处理了最近一次 Move 输入。 */
UPROPERTY(Transient)
bool bMoveInputHandledByProcessor = false;
/** @brief 最近一次由输入处理器处理的 Move 触发事件。 */
UPROPERTY(Transient)
ETriggerEvent LastMoveInputProcessorTriggerEvent = ETriggerEvent::None;
};